blob: 6afe8cc611b04ef3caf977efddf21f3a470dcd57 [file] [log] [blame]
llbd9cc172022-12-30 16:36:18 +08001
2
3#include <string.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <log/log.h>
7#include "include/libat_common/lynq_at_common.h"
8#include "atci_at_util.h"
9
10#undef LOG_TAG
11#define LOG_TAG "AT_COMMON"
12
13int g_mnetcall_mode = 0;
14int g_gtarndis_mode = 0;
15int g_version_mode = 0;
16
17lynq_atsvc_outcb handle_output;
18
19typedef struct
20{
21 char *cmd;
22 void (*func)(char *input,int mode);
23}Command;
24
25enum
26{
27 Response = 0,
28 Urc
29};
30
31void lynq_response_ok()
32{
33 char *str = "\r\nOK\r\n";
34 handle_output(str, strlen(str), Response);
35}
36
37void lynq_response_error()
38{
39 char *str = "\r\n+CME ERROR:100\r\n";
40 handle_output(str, strlen(str), Response);
41}
42
43static int lynq_catrndis_mode(void)
44{
45 FILE *fp;
46 char buf[2];
47 fp = popen("cat /data/rndis.conf","r");
48 fgets(buf,sizeof(buf),fp);
49 if(buf != NULL)
50 {
51 if(buf[0] == '0')
52 {
53 return 0;
54 }
55 if(buf[0] == '1')
56 {
57 return 1;
58 }
59 else
60 {
61 return 2;//not config
62 }
63 }
64 pclose(fp);
65 return;
66}
67
68static void lynq_get_poepn_buf(char *cmd)
69{
70 FILE *fp;
71 char buf[128] = {0};
72 fp = popen(cmd,"r");
73 while(fgets(buf, sizeof(buf), fp) != NULL){}
74 pclose(fp);
75 RLOGD("buf is %s size %d\n", buf, sizeof(buf));
76 handle_output(buf, strlen(buf), Response);
77 return;
78}
79
80void lynq_handle_rndis(char *input,int type)
81{
82 RLOGD("lynq_handle_rndis type %d\n", type);
83 char buf[128] = {0};
84 if(type == AT_SET_MODE)//set
85 {
86 int mode;
87 if (SYS_FAIL == atci_at_to_equal(&input))
88 {
89 lynq_response_error();
90 return SYS_FAIL;
91 }
92 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
93 {
94 lynq_response_error();
95 return SYS_FAIL;
96 }
97 if(mode == 1)
98 {
99 g_mnetcall_mode = mode;
100 system("connmanctl enable gadget");
101 system("connmanctl tether gadget on");
102 lynq_response_ok();
103 }
104 else if (mode == 0)
105 {
106 g_mnetcall_mode = mode;
107 system("connmanctl disable gadget");
108 lynq_response_ok();
109 }
110 else
111 {
112 lynq_response_error();
113 }
114 }
115 else if(type == AT_TEST_MODE)//list
116 {
117 sprintf(buf,"+MNETCALL:(0-1)");
118 handle_output(buf, strlen(buf), Response);
119 lynq_response_ok();
120 }
121 else if(type == AT_READ_MODE)//get
122 {
123 sprintf(buf,"+MNETCALL:%d", g_mnetcall_mode);
124 handle_output(buf, strlen(buf), Response);
125 lynq_response_ok();
126 }
127 else
128 {
129 lynq_response_error();
130 }
131 return;
132}
133
134void lynq_handle_rndis_configure(char *input,int type)
135{
136 RLOGD("lynq_handle_rndis_configure type %d\n", type);
137 char buf[128] = {0};
138 if(type == AT_SET_MODE)//set
139 {
140 int mode;
141 if (SYS_FAIL == atci_at_to_equal(&input))
142 {
143 lynq_response_error();
144 return SYS_FAIL;
145 }
146 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
147 {
148 lynq_response_error();
149 return SYS_FAIL;
150 }
151 if(mode == 1)
152 {
153 g_gtarndis_mode = mode;
154 system("echo \"1\" > /data/rndis.conf");
155 lynq_response_ok();
156 }
157 else if (mode == 0)
158 {
159 g_gtarndis_mode = mode;
160 system("echo \"0\" > /data/rndis.conf");
161 lynq_response_ok();
162 }
163 else
164 {
165 lynq_response_error();
166 }
167 }
168 else if(type == AT_TEST_MODE)//list
169 {
170 sprintf(buf,"+GTARNDIS:(0-1)");
171 handle_output(buf, strlen(buf), Response);
172 lynq_response_ok();
173 }
174 else if(type == AT_READ_MODE)//get
175 {
176 sprintf(buf,"+GTARNDIS:%d", g_gtarndis_mode);
177 handle_output(buf, strlen(buf), Response);
178 lynq_response_ok();
179 }
180 else
181 {
182 lynq_response_error();
183 }
184 return;
185}
186
187void lynq_handle_version(char *input,int type)
188{
189 RLOGD("lynq_handle_version type %d\n", type);
190 char buf[128] = {0};
191 if(type == AT_SET_MODE)//set
192 {
193 int mode;
194 if (SYS_FAIL == atci_at_to_equal(&input))
195 {
196 lynq_response_error();
197 return SYS_FAIL;
198 }
199 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
200 {
201 lynq_response_error();
202 return SYS_FAIL;
203 }
204 if(mode == 1)
205 {
206 g_version_mode = mode;
207 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_INSIDE_VERSION");
208 //handle_output(buf, strlen(buf), Response);
209 lynq_response_ok();
210 }
211 else if (mode == 0)
212 {
213 g_version_mode = mode;
214 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_VERSION");
215 //handle_output(buf, strlen(buf), Response);
216 lynq_response_ok();
217 }
218 else
219 {
220 lynq_response_error();
221 }
222 }
223 else if(type == AT_TEST_MODE)//list
224 {
225 sprintf(buf,"+CGIR:(0-1)");
226 handle_output(buf, strlen(buf), Response);
227 lynq_response_ok();
228 }
229 else if(type == AT_READ_MODE)//get
230 {
231 sprintf(buf,"+CGIR:%d", g_version_mode);
232 handle_output(buf, strlen(buf), Response);
233 lynq_response_ok();
234 }
235 else if(type == AT_ACTIVE_MODE)//active
236 {
237 if(g_version_mode == 0)
238 {
239 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_VERSION");
240 //handle_output(buf, strlen(buf), Response);
241 lynq_response_ok();
242 }
243 else if(g_version_mode == 1)
244 {
245 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_INSIDE_VERSION");
246 //handle_output(buf, strlen(buf), Response);
247 lynq_response_ok();
248 }
249 }
250 return;
251}
252
253static Command commands[] =
254{
255{"at+mnetcall",lynq_handle_rndis},
256{"at+gtarndis",lynq_handle_rndis_configure},
257{"at+cgir",lynq_handle_version},
258{NULL, NULL}
259};
260
261Command* find_command (char *input)
262{
263 RLOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
264 int i;
265 int ret = -1;
266 for (i = 0; commands[i].cmd; i++)
267 {
268 ret = strcmp(input, commands[i].cmd);
269 if(ret == 0)
270 {
271 RLOGD("function %s line %d find input %s commands[i].cmd %s strlen %d ret %d\n", __FUNCTION__, __LINE__, input, commands[i].cmd, strlen(commands[i].cmd), ret);
272 return (&commands[i]);
273 }
274 }
275 RLOGD("function %s line %d not find ret %d \n", __FUNCTION__, __LINE__, ret);
276 return ((Command *)NULL);
277}
278
279void lynq_at_common_cb(char *input, int input_max_size)
280{
281 if(handle_output != NULL)
282 {
283 RLOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
284 if(input != NULL)
285 {
286 char *prefix = NULL;
287 prefix = atci_get_cmd_prefix(input);
288 if (NULL == prefix) {
289 RLOGD("atci_cut_cmd_prefix error");
290 return SYS_FAIL;
291 }
292 RLOGD("find prefix [%s]", prefix);
293 Command *cmd = find_command(prefix);
294 if(cmd != NULL)
295 {
296 int cmd_mode = atci_get_cmd_mode(input);
297 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
298 (*(cmd->func))(input,cmd_mode);
299 free(prefix);
300 return;
301 }
302 else
303 {
304 RLOGD("not find prefix in list");
305 }
306 free(prefix);
307 }
308 }
309}
310
311lynq_atsvc_incb lynq_register_at_common(lynq_atsvc_outcb out_cb)
312{
313 if(out_cb != NULL)
314 {
315 handle_output = out_cb;
316 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
317 return lynq_at_common_cb;
318 }
319}