blob: 531afff3522d494f11b990b0a95c9e0f01cd90cc [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/* //device/system/reference-ril/at_tok.c
2**
3** Copyright 2006, The Android Open Source Project
4**
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
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
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.
16*/
17
18#include "at_tok.h"
19#include <string.h>
20#include <ctype.h>
21#include <stdlib.h>
22
23#include "mbtk_log.h"
24/**
25 * Starts tokenizing an AT response string
26 * returns -1 if this is not a valid response string, 0 on success.
27 * updates *p_cur with current position
28 */
29int at_tok_start(char **p_cur)
30{
31 if (*p_cur == NULL) {
32 return -1;
33 }
34
35 // skip prefix
36 // consume "^[^:]:"
37
38 *p_cur = strchr(*p_cur, ':');
39
40 if (*p_cur == NULL) {
41 return -1;
42 }
43
44 (*p_cur)++;
45/*
46 while(**p_cur == ' ') {
47 (*p_cur)++;
48 }
49*/
50 return 0;
51}
52
53static void skipWhiteSpace(char **p_cur)
54{
55 if (*p_cur == NULL) return;
56
57 while (**p_cur != '\0' && isspace(**p_cur)) {
58 (*p_cur)++;
59 }
60}
61
62static void skipNextComma(char **p_cur)
63{
64 if (*p_cur == NULL) return;
65
66 while (**p_cur != '\0' && **p_cur != ',') {
67 (*p_cur)++;
68 }
69
70 if (**p_cur == ',') {
71 (*p_cur)++;
72 }
73}
74
75static char * nextTok(char **p_cur)
76{
77 char *ret = NULL;
78
79 skipWhiteSpace(p_cur);
80
81 if (*p_cur == NULL) {
82 ret = NULL;
83 } else if (**p_cur == '"') {
84 (*p_cur)++;
85 ret = strsep(p_cur, "\"");
86 skipNextComma(p_cur);
87 } else {
88 ret = strsep(p_cur, ",");
89 }
90
91 return ret;
92}
93
94
95/**
96 * Parses the next integer in the AT response line and places it in *p_out
97 * returns 0 on success and -1 on fail
98 * updates *p_cur
99 * "base" is the same as the base param in strtol
100 */
101
102static int at_tok_nextint_base(char **p_cur, int *p_out, int base, int uns)
103{
104 char *ret;
105
106 if (*p_cur == NULL) {
107 return -1;
108 }
109
110 ret = nextTok(p_cur);
111
112 if (ret == NULL) {
113 return -1;
114 } else {
115 long l;
116 char *end;
117
118 if (uns)
119 l = strtoul(ret, &end, base);
120 else
121 l = strtol(ret, &end, base);
122
123 *p_out = (int)l;
124
125 if (end == ret) {
126 return -1;
127 }
128 }
129
130 return 0;
131}
132
133/**
134 * Parses the next base 10 integer in the AT response line
135 * and places it in *p_out
136 * returns 0 on success and -1 on fail
137 * updates *p_cur
138 */
139int at_tok_nextint(char **p_cur, int *p_out)
140{
141 return at_tok_nextint_base(p_cur, p_out, 10, 0);
142}
143
144/**
145 * Parses the next base 16 integer in the AT response line
146 * and places it in *p_out
147 * returns 0 on success and -1 on fail
148 * updates *p_cur
149 */
150int at_tok_nexthexint(char **p_cur, int *p_out)
151{
152 return at_tok_nextint_base(p_cur, p_out, 16, 1);
153}
154
155int at_tok_nextbool(char **p_cur, char *p_out)
156{
157 int ret;
158 int result;
159
160 ret = at_tok_nextint(p_cur, &result);
161
162 if (ret < 0) {
163 return -1;
164 }
165
166 // booleans should be 0 or 1
167 if (!(result == 0 || result == 1)) {
168 return -1;
169 }
170
171 if (p_out != NULL) {
172 *p_out = (char)result;
173 }
174
175 return ret;
176}
177
178int at_tok_nextstr(char **p_cur, char **p_out)
179{
180 if (*p_cur == NULL) {
181 return -1;
182 }
183
184 *p_out = nextTok(p_cur);
185
186 return 0;
187}
188
189/** returns 1 on "has more tokens" and 0 if no */
190int at_tok_hasmore(char **p_cur)
191{
192 return ! (*p_cur == NULL || **p_cur == '\0');
193}
194