blob: e73121d9e5b40bcd47bae6880bce736fa52283e9 [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;
Hong_Liu496d67b2023-01-12 02:16:27 -0800154 system("uci set lynq_uci.rndis.status='1'");
155 usleep(10*1000);
156 system("uci commit");
llbd9cc172022-12-30 16:36:18 +0800157 lynq_response_ok();
158 }
159 else if (mode == 0)
160 {
161 g_gtarndis_mode = mode;
Hong_Liu496d67b2023-01-12 02:16:27 -0800162 system("uci set lynq_uci.rndis.status='0'");
163 usleep(10*1000);
164 system("uci commit");
llbd9cc172022-12-30 16:36:18 +0800165 lynq_response_ok();
166 }
167 else
168 {
169 lynq_response_error();
170 }
171 }
172 else if(type == AT_TEST_MODE)//list
173 {
174 sprintf(buf,"+GTARNDIS:(0-1)");
175 handle_output(buf, strlen(buf), Response);
176 lynq_response_ok();
177 }
178 else if(type == AT_READ_MODE)//get
179 {
180 sprintf(buf,"+GTARNDIS:%d", g_gtarndis_mode);
181 handle_output(buf, strlen(buf), Response);
182 lynq_response_ok();
183 }
184 else
185 {
186 lynq_response_error();
187 }
188 return;
189}
190
191void lynq_handle_version(char *input,int type)
192{
193 RLOGD("lynq_handle_version type %d\n", type);
194 char buf[128] = {0};
195 if(type == AT_SET_MODE)//set
196 {
197 int mode;
198 if (SYS_FAIL == atci_at_to_equal(&input))
199 {
200 lynq_response_error();
201 return SYS_FAIL;
202 }
203 if (SYS_FAIL == atci_at_get_nexthexint(&input, &mode))
204 {
205 lynq_response_error();
206 return SYS_FAIL;
207 }
208 if(mode == 1)
209 {
210 g_version_mode = mode;
211 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_INSIDE_VERSION");
212 //handle_output(buf, strlen(buf), Response);
213 lynq_response_ok();
214 }
215 else if (mode == 0)
216 {
217 g_version_mode = mode;
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
223 {
224 lynq_response_error();
225 }
226 }
227 else if(type == AT_TEST_MODE)//list
228 {
229 sprintf(buf,"+CGIR:(0-1)");
230 handle_output(buf, strlen(buf), Response);
231 lynq_response_ok();
232 }
233 else if(type == AT_READ_MODE)//get
234 {
235 sprintf(buf,"+CGIR:%d", g_version_mode);
236 handle_output(buf, strlen(buf), Response);
237 lynq_response_ok();
238 }
239 else if(type == AT_ACTIVE_MODE)//active
240 {
241 if(g_version_mode == 0)
242 {
243 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_VERSION");
244 //handle_output(buf, strlen(buf), Response);
245 lynq_response_ok();
246 }
247 else if(g_version_mode == 1)
248 {
249 lynq_get_poepn_buf("uci get lynq_uci_ro.lynq_version.LYNQ_SW_INSIDE_VERSION");
250 //handle_output(buf, strlen(buf), Response);
251 lynq_response_ok();
252 }
253 }
254 return;
255}
256
257static Command commands[] =
258{
259{"at+mnetcall",lynq_handle_rndis},
260{"at+gtarndis",lynq_handle_rndis_configure},
261{"at+cgir",lynq_handle_version},
262{NULL, NULL}
263};
264
265Command* find_command (char *input)
266{
267 RLOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
268 int i;
269 int ret = -1;
270 for (i = 0; commands[i].cmd; i++)
271 {
272 ret = strcmp(input, commands[i].cmd);
273 if(ret == 0)
274 {
275 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);
276 return (&commands[i]);
277 }
278 }
279 RLOGD("function %s line %d not find ret %d \n", __FUNCTION__, __LINE__, ret);
280 return ((Command *)NULL);
281}
282
283void lynq_at_common_cb(char *input, int input_max_size)
284{
285 if(handle_output != NULL)
286 {
287 RLOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
288 if(input != NULL)
289 {
290 char *prefix = NULL;
291 prefix = atci_get_cmd_prefix(input);
292 if (NULL == prefix) {
293 RLOGD("atci_cut_cmd_prefix error");
294 return SYS_FAIL;
295 }
296 RLOGD("find prefix [%s]", prefix);
297 Command *cmd = find_command(prefix);
298 if(cmd != NULL)
299 {
300 int cmd_mode = atci_get_cmd_mode(input);
301 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
302 (*(cmd->func))(input,cmd_mode);
303 free(prefix);
304 return;
305 }
306 else
307 {
308 RLOGD("not find prefix in list");
309 }
310 free(prefix);
311 }
312 }
313}
314
315lynq_atsvc_incb lynq_register_at_common(lynq_atsvc_outcb out_cb)
316{
317 if(out_cb != NULL)
318 {
319 handle_output = out_cb;
320 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
321 return lynq_at_common_cb;
322 }
323}