blob: ce8476aed5036ad2c761bdf061d2acfca6e239f7 [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
llbd9cc172022-12-30 16:36:18 +080043static void lynq_get_poepn_buf(char *cmd)
44{
45 FILE *fp;
46 char buf[128] = {0};
47 fp = popen(cmd,"r");
48 while(fgets(buf, sizeof(buf), fp) != NULL){}
49 pclose(fp);
50 RLOGD("buf is %s size %d\n", buf, sizeof(buf));
51 handle_output(buf, strlen(buf), Response);
52 return;
53}
54
55void lynq_handle_rndis(char *input,int type)
56{
57 RLOGD("lynq_handle_rndis type %d\n", type);
58 char buf[128] = {0};
59 if(type == AT_SET_MODE)//set
60 {
61 int mode;
62 if (SYS_FAIL == atci_at_to_equal(&input))
63 {
64 lynq_response_error();
65 return SYS_FAIL;
66 }
67 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
68 {
69 lynq_response_error();
70 return SYS_FAIL;
71 }
72 if(mode == 1)
73 {
74 g_mnetcall_mode = mode;
75 system("connmanctl enable gadget");
76 system("connmanctl tether gadget on");
77 lynq_response_ok();
78 }
79 else if (mode == 0)
80 {
81 g_mnetcall_mode = mode;
82 system("connmanctl disable gadget");
83 lynq_response_ok();
84 }
85 else
86 {
87 lynq_response_error();
88 }
89 }
90 else if(type == AT_TEST_MODE)//list
91 {
92 sprintf(buf,"+MNETCALL:(0-1)");
93 handle_output(buf, strlen(buf), Response);
94 lynq_response_ok();
95 }
96 else if(type == AT_READ_MODE)//get
97 {
98 sprintf(buf,"+MNETCALL:%d", g_mnetcall_mode);
99 handle_output(buf, strlen(buf), Response);
100 lynq_response_ok();
101 }
102 else
103 {
104 lynq_response_error();
105 }
106 return;
107}
108
109void lynq_handle_rndis_configure(char *input,int type)
110{
111 RLOGD("lynq_handle_rndis_configure type %d\n", type);
112 char buf[128] = {0};
113 if(type == AT_SET_MODE)//set
114 {
115 int mode;
116 if (SYS_FAIL == atci_at_to_equal(&input))
117 {
118 lynq_response_error();
119 return SYS_FAIL;
120 }
121 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
122 {
123 lynq_response_error();
124 return SYS_FAIL;
125 }
126 if(mode == 1)
127 {
128 g_gtarndis_mode = mode;
Hong_Liu79f7aa72023-01-12 08:20:41 -0800129 system("uci set lynq_uci.rndis=lynq_rndis");
Hong_Liu496d67b2023-01-12 02:16:27 -0800130 system("uci set lynq_uci.rndis.status='1'");
Hong_Liu496d67b2023-01-12 02:16:27 -0800131 system("uci commit");
llbd9cc172022-12-30 16:36:18 +0800132 lynq_response_ok();
133 }
134 else if (mode == 0)
135 {
136 g_gtarndis_mode = mode;
Hong_Liu79f7aa72023-01-12 08:20:41 -0800137 system("uci set lynq_uci.rndis=lynq_rndis");
Hong_Liu496d67b2023-01-12 02:16:27 -0800138 system("uci set lynq_uci.rndis.status='0'");
Hong_Liu496d67b2023-01-12 02:16:27 -0800139 system("uci commit");
llbd9cc172022-12-30 16:36:18 +0800140 lynq_response_ok();
141 }
142 else
143 {
144 lynq_response_error();
145 }
146 }
147 else if(type == AT_TEST_MODE)//list
148 {
149 sprintf(buf,"+GTARNDIS:(0-1)");
150 handle_output(buf, strlen(buf), Response);
151 lynq_response_ok();
152 }
153 else if(type == AT_READ_MODE)//get
154 {
155 sprintf(buf,"+GTARNDIS:%d", g_gtarndis_mode);
156 handle_output(buf, strlen(buf), Response);
157 lynq_response_ok();
158 }
159 else
160 {
161 lynq_response_error();
162 }
163 return;
164}
165
166void lynq_handle_version(char *input,int type)
167{
168 RLOGD("lynq_handle_version type %d\n", type);
169 char buf[128] = {0};
170 if(type == AT_SET_MODE)//set
171 {
172 int mode;
173 if (SYS_FAIL == atci_at_to_equal(&input))
174 {
175 lynq_response_error();
176 return SYS_FAIL;
177 }
178 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
179 {
180 lynq_response_error();
181 return SYS_FAIL;
182 }
183 if(mode == 1)
184 {
185 g_version_mode = mode;
186 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_INSIDE_VERSION");
187 //handle_output(buf, strlen(buf), Response);
188 lynq_response_ok();
189 }
190 else if (mode == 0)
191 {
192 g_version_mode = mode;
193 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_VERSION");
194 //handle_output(buf, strlen(buf), Response);
195 lynq_response_ok();
196 }
197 else
198 {
199 lynq_response_error();
200 }
201 }
202 else if(type == AT_TEST_MODE)//list
203 {
204 sprintf(buf,"+CGIR:(0-1)");
205 handle_output(buf, strlen(buf), Response);
206 lynq_response_ok();
207 }
208 else if(type == AT_READ_MODE)//get
209 {
210 sprintf(buf,"+CGIR:%d", g_version_mode);
211 handle_output(buf, strlen(buf), Response);
212 lynq_response_ok();
213 }
214 else if(type == AT_ACTIVE_MODE)//active
215 {
216 if(g_version_mode == 0)
217 {
218 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_VERSION");
219 //handle_output(buf, strlen(buf), Response);
220 lynq_response_ok();
221 }
222 else if(g_version_mode == 1)
223 {
224 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_INSIDE_VERSION");
225 //handle_output(buf, strlen(buf), Response);
226 lynq_response_ok();
227 }
228 }
229 return;
230}
231
llcb99a122023-01-13 13:54:56 +0800232void lynq_display_modem_mode(const char *cmd)
233{
234 FILE *fp;
235 char buf[128] = {0};
236 fp = popen(cmd,"r");
237 while(fgets(buf, sizeof(buf), fp) != NULL){}
238 pclose(fp);
239 RLOGD("buf is %s size %d\n", buf, sizeof(buf));
240 char tmp[16] = {0};
241 sprintf(tmp, "+LGMDS: %c", buf[4]);
242 handle_output(tmp, strlen(tmp), Response);
243 return;
244}
245
246void lynq_handle_modem_status(char *input,int type)
247{
248 RLOGD("lynq_handle_modem_status type %d\n", type);
249 if(type == AT_ACTIVE_MODE)//active
250 {
251 lynq_display_modem_mode("cat /sys/kernel/ccci/boot");
252 lynq_response_ok();
253 }
254 return;
255}
256
257void lynq_tmp_handle_rndis(char *input,int type)
258{
259 RLOGD("lynq_tmp_handle_rndis type %d\n", type);
260 char *connman_cmd1 = NULL;
261 char *connman_cmd2 = NULL;
262 if(type == AT_SET_MODE)//set
263 {
264 if (SYS_FAIL == atci_at_to_equal(&input))
265 {
266 lynq_response_error();
267 return ;
268 }
269 if (SYS_FAIL == atci_at_get_next_key(&input, &connman_cmd1))
270 {
271 lynq_response_error();
272 return ;
273 }
274 if (SYS_FAIL == atci_at_get_next_key(&input, &connman_cmd2))
275 {
276 //lynq_response_error();
277 //return SYS_FAIL;
278 }
279 if((connman_cmd1 != NULL) && (connman_cmd2 != NULL))
280 {
281 if(!strcmp(connman_cmd1, "connmanctl enable gadget"))
282 {
283 system("connmanctl enable gadget");
284 }
285 else
286 {
287 lynq_response_error();
288 return;
289 }
290 if(!strcmp(connman_cmd2, "connmanctl tether gadget on"))
291 {
292 system("connmanctl tether gadget on");
293 }
294 else
295 {
296 lynq_response_error();
297 return;
298 }
299 lynq_response_ok();
300 }
301 if(connman_cmd2 == NULL)
302 {
303 if(!strcmp(connman_cmd1, "connmanctl disable gadget"))
304 {
305 system("connmanctl disable gadget");
306 }
307 else
308 {
309 lynq_response_error();
310 return;
311 }
312 lynq_response_ok();
313 }
314 }
315 // else if(type == AT_TEST_MODE)//list
316 // {
317 // sprintf(buf,"+MNETCALL:(0-1)");
318 // handle_output(buf, strlen(buf), Response);
319 // lynq_response_ok();
320 // }
321 // else if(type == AT_READ_MODE)//get
322 // {
323 // sprintf(buf,"+MNETCALL:%d", g_mnetcall_mode);
324 // handle_output(buf, strlen(buf), Response);
325 // lynq_response_ok();
326 // }
327 else
328 {
329 lynq_response_error();
330 }
331 return;
332}
333
llbd9cc172022-12-30 16:36:18 +0800334static Command commands[] =
335{
336{"at+mnetcall",lynq_handle_rndis},
337{"at+gtarndis",lynq_handle_rndis_configure},
338{"at+cgir",lynq_handle_version},
llcb99a122023-01-13 13:54:56 +0800339{"at+lgmds",lynq_handle_modem_status},
340{"at+lrndishandle",lynq_tmp_handle_rndis},//tmp plan
llbd9cc172022-12-30 16:36:18 +0800341{NULL, NULL}
342};
343
344Command* find_command (char *input)
345{
346 RLOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
347 int i;
348 int ret = -1;
349 for (i = 0; commands[i].cmd; i++)
350 {
351 ret = strcmp(input, commands[i].cmd);
352 if(ret == 0)
353 {
354 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);
355 return (&commands[i]);
356 }
357 }
358 RLOGD("function %s line %d not find ret %d \n", __FUNCTION__, __LINE__, ret);
359 return ((Command *)NULL);
360}
361
362void lynq_at_common_cb(char *input, int input_max_size)
363{
364 if(handle_output != NULL)
365 {
366 RLOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
367 if(input != NULL)
368 {
369 char *prefix = NULL;
370 prefix = atci_get_cmd_prefix(input);
371 if (NULL == prefix) {
372 RLOGD("atci_cut_cmd_prefix error");
llcb99a122023-01-13 13:54:56 +0800373 return;
llbd9cc172022-12-30 16:36:18 +0800374 }
375 RLOGD("find prefix [%s]", prefix);
376 Command *cmd = find_command(prefix);
377 if(cmd != NULL)
378 {
379 int cmd_mode = atci_get_cmd_mode(input);
380 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
381 (*(cmd->func))(input,cmd_mode);
382 free(prefix);
383 return;
384 }
385 else
386 {
387 RLOGD("not find prefix in list");
388 }
389 free(prefix);
390 }
391 }
392}
393
394lynq_atsvc_incb lynq_register_at_common(lynq_atsvc_outcb out_cb)
395{
396 if(out_cb != NULL)
397 {
398 handle_output = out_cb;
399 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
400 return lynq_at_common_cb;
401 }
402}