1 /* //device/system/reference-ril/at_tok.c
3 ** Copyright 2006, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
24 * Starts tokenizing an AT response string
25 * returns -1 if this is not a valid response string, 0 on success.
26 * updates *p_cur with current position
28 int at_tok_start(char **p_cur)
37 *p_cur = strchr(*p_cur, ':');
48 static void skipWhiteSpace(char **p_cur)
50 if (*p_cur == NULL) return;
52 while (**p_cur != '\0' && isspace(**p_cur)) {
57 static void skipNextComma(char **p_cur)
59 if (*p_cur == NULL) return;
61 while (**p_cur != '\0' && **p_cur != ',') {
70 static char * nextTok(char **p_cur)
74 skipWhiteSpace(p_cur);
78 } else if (**p_cur == '"') {
80 ret = strsep(p_cur, "\"");
83 ret = strsep(p_cur, ",");
91 * Parses the next integer in the AT response line and places it in *p_out
92 * returns 0 on success and -1 on fail
94 * "base" is the same as the base param in strtol
97 static int at_tok_nextint_base(char **p_cur, int *p_out, int base, int uns)
101 if (*p_cur == NULL) {
105 ret = nextTok(p_cur);
114 l = strtoul(ret, &end, base);
116 l = strtol(ret, &end, base);
129 * Parses the next base 10 integer in the AT response line
130 * and places it in *p_out
131 * returns 0 on success and -1 on fail
134 int at_tok_nextint(char **p_cur, int *p_out)
136 return at_tok_nextint_base(p_cur, p_out, 10, 0);
140 * Parses the next base 16 integer in the AT response line
141 * and places it in *p_out
142 * returns 0 on success and -1 on fail
145 int at_tok_nexthexint(char **p_cur, int *p_out)
147 return at_tok_nextint_base(p_cur, p_out, 16, 1);
150 int at_tok_nextbool(char **p_cur, char *p_out)
155 ret = at_tok_nextint(p_cur, &result);
161 // booleans should be 0 or 1
162 if (!(result == 0 || result == 1)) {
167 *p_out = (char)result;
173 int at_tok_nextstr(char **p_cur, char **p_out)
175 if (*p_cur == NULL) {
179 *p_out = nextTok(p_cur);
184 /** returns 1 on "has more tokens" and 0 if no */
185 int at_tok_hasmore(char **p_cur)
187 return ! (*p_cur == NULL || **p_cur == '\0');