blob: facca4974b6bcf98002f35236174964dc9d6ce8b [file] [log] [blame]
liuyang57759622024-11-29 17:52:17 +08001#include "mbtk_log.h"
2#include "mbtk_wifi_ap.h"
3
4#include <stdlib.h>
5#include <stdio.h>
6
7
8int mbtk_wifi_get_setting(const char *path, const char *key, char *value, int value_max_len)
9{
10 int ret = 0;
11 int cnt = 0;
12 int key_len;
13 int line_len;
14 int value_len;
15 FILE *fp;
16 char line_buf[SETTING_LINE_MAX_LEN] = {0};
17
18 if (NULL == path || NULL == key || NULL == value || 0 >= value_max_len) {
19 return -1;
20 }
21
22 key_len = strlen(key);
23 if (0 >= key_len || SETTING_KEY_MAX_LEN < key_len) {
24 return -2;
25 }
26
27 if (F_OK != access(path, F_OK)) {
28 return -3;
29 }
30
31 fp = fopen(path, "r");
32 if (NULL == fp) {
33 return -4;
34 }
35
36 while(NULL != fgets(line_buf, SETTING_LINE_MAX_LEN-1, fp)) {
37 line_len = strlen(line_buf);
38 if (line_len > key_len) {
39 if (0 == strncmp(line_buf, key, key_len) && SETTING_SPLIT_CHAR == line_buf[key_len]) {
40 ret = 1;
41 break;
42 }
43 }
44 memset(line_buf, 0, sizeof(line_buf));
45
46 cnt++;
47 if (SETTING_LINE_MAX < cnt) {
48 break;
49 }
50 }
51
52 fclose(fp);
53
54 if (1 == ret) {
55 value_len = line_len - key_len - 1;
56 if (0 < value_len) {
57 if (value_len > value_max_len) {
58 value_len = value_max_len;
59 }
60
61 strncpy(value, &line_buf[key_len + 1], value_len);
62 value[value_len] = '\0';
63
64
65 if ('\n' == value[value_len-1]) {
66 value[value_len-1] = '\0';
67 }
68 if (2 <= value_len && '\r' == value[value_len-2]) {
69 value[value_len-2] = '\0';
70 }
71
72 } else {
73 value[0] = '\0';
74 }
75 }
76
77 return ret;
78}
79
80
81int mbtk_wifi_set_setting(const char *path, const char *key, const char *value)
82{
83 int i, j;
84 int fd;
85 int tmp_data_cnt = 0;
86 int ret = 0;
87 int cnt = 0;
88 int key_len;
89 int line_len;
90 int value_len;
91 int len;
92 FILE *fp = NULL;
93 char tmp_key[SETTING_KEY_MAX_LEN+1] = {0};
94 char tmp_value[SETTING_VALUE_MAX_LEN+1] = {0};
95 char line_buf[SETTING_LINE_MAX_LEN] = {0};
96 char tmp_data_buf[SETTING_LINE_MAX][SETTING_LINE_MAX_LEN] = {0};
97
98
99 if (NULL == path || NULL == key || NULL == value) {
100 return -1;
101 }
102
103 key_len = strlen(key);
104 if (0 >= key_len || SETTING_KEY_MAX_LEN < key_len) {
105 return -2;
106 }
107
108 for (i = 0, j = 0; i < key_len; i++) {
109 if ('\r' == key[i] || '\n' == key[i]) {
110 break;
111 }
112 tmp_key[j++] = key[i];
113 }
114 key_len = j;
115
116 value_len = strlen(value);
117 if (SETTING_VALUE_MAX_LEN < value_len) {
118 value_len = SETTING_VALUE_MAX_LEN;
119 } else if (value_len <= 0) {
120 value_len = 0;
121 }
122
123 for (i = 0, j = 0; i < value_len; i++) {
124 if ('\r' == value[i] || '\n' == value[i]) {
125 break;
126 }
127 tmp_value[j++] = value[i];
128 }
129 value_len = j;
130
131
132 /*
133 if (F_OK != access(path, F_OK))
134 {
135 fp = fopen(path, "w+");
136 if (NULL == fp) {
137 return -3;
138 }
139
140
141 sprintf(line_buf, "%s%c%s\n", SETTING_VERSION_KEY, SETTING_SPLIT_CHAR, SETTING_VERSION_VALUE);
142 line_len = strlen(line_buf);
143 ret = fwrite(line_buf, 1, line_len, fp);
144
145
146 memset(line_buf, 0, sizeof(line_buf));
147 sprintf(line_buf, "%s%c%s\n", tmp_key, SETTING_SPLIT_CHAR, tmp_value);
148 line_len = strlen(line_buf);
149 if (line_len != fwrite(line_buf, 1, line_len, fp)) {
150 ret = -4;
151 } else {
152 ret = 2;
153 }
154
155 fclose(fp);
156 return ret;
157 }
158 */
159
160 if (NULL == fp) {
161 fp = fopen(path, "r+");
162 if (NULL == fp) {
163 return -5;
164 }
165 }
166
167 while(NULL != fgets(line_buf, SETTING_LINE_MAX_LEN-1, fp)) {
168 line_len = strlen(line_buf);
169 if (line_len > key_len) {
170 if (1 != ret && 0 == strncmp(line_buf, tmp_key, key_len) && SETTING_SPLIT_CHAR == line_buf[key_len]) {
171 ret = 1;
172 sprintf(line_buf, "%s%c%s\n", tmp_key, SETTING_SPLIT_CHAR, tmp_value);
173 }
174 }
175
176 strcpy(tmp_data_buf[tmp_data_cnt++], line_buf);
177
178 memset(line_buf, 0, sizeof(line_buf));
179
180 cnt++;
181 if (SETTING_LINE_MAX < cnt) {
182 break;
183 }
184 }
185
186
187 if (1 == ret) {
188 rewind(fp);
189 for (i = 0; i < tmp_data_cnt; i++) {
190 fputs(tmp_data_buf[i], fp);
191 }
192
193 len = ftell(fp);
194 fd = fileno(fp);
195 if(0 > ftruncate(fd, len))
196 {
197 return -1;
198 }
199
200 } else {
201 if (SETTING_LINE_MAX > cnt) {
202 sprintf(line_buf, "%s%c%s\n", tmp_key, SETTING_SPLIT_CHAR, tmp_value);
203 line_len = strlen(line_buf);
204 if (line_len != fwrite(line_buf, 1, line_len, fp)) {
205 ret = -6;
206 }
207 } else {
208 ret = -7;
209 }
210 }
211
212 fclose(fp);
213
214 return ret;
215}
216
217
218
219int mbtk_wifi_ap_start(void)
220{
liuyangb2d36862025-05-22 13:28:38 +0800221 if(0 > system("hostapd -B /etc/wifi/hostapd.conf"))
liuyang57759622024-11-29 17:52:17 +0800222 {
223 return -1;
224 }
225
226 if(0 > system("sleep 5s"))
227 {
228 return -1;
229 }
230
231 if(0 > system("brctl addif br-lan wlan0"))
232 {
233 return -1;
234 }
235
236 if(0 > system("iptables -t nat -A POSTROUTING -o ccinet0 -j MASQUERADE --random"))
237 {
238 return -1;
239 }
240
241 return 0;
242}
243
244int mbtk_wifi_ap_stop(void)
245{
246 if(0 > system("killall hostapd"))
247 {
248 return -1;
249 }
250
251 return 0;
252
253}
254
liuyangad56fc92024-12-17 19:10:11 +0800255int mbtk_wifi_set_file(const char *path, const char *value)
256{
257 FILE *file;
258
259 file = fopen(path, "w");
260 if (!file)
261 {
262 return -1;
263 }
264
265 fputs(value, file);
266
267
268 fclose(file);
269 return 0;
270}
271
272int mbtk_wifi_get_file(const char *path, char *value, int value_max_len)
273{
274 FILE *file;
liuyang36d803c2024-12-26 13:16:09 +0800275 char buf[SETTING_LINE_MAX_LEN] = {0};
276 int len = 0;
liuyangad56fc92024-12-17 19:10:11 +0800277
278 file = fopen(path, "r");
279 if (!file)
280 {
281 return -1;
282 }
283
liuyang36d803c2024-12-26 13:16:09 +0800284 while(NULL != fgets(buf, SETTING_LINE_MAX_LEN, file))
liuyangad56fc92024-12-17 19:10:11 +0800285 {
liuyang36d803c2024-12-26 13:16:09 +0800286 LOGD("mbtk_wifi_get_file:%s\n", buf);
287 len += strlen(buf);
288 if(len > value_max_len)
289 {
290 break;
291 }
292 strncat(value, buf, strlen(buf));
293 LOGD("value:%s\n", value);
liuyangad56fc92024-12-17 19:10:11 +0800294 }
295
296
297 fclose(file);
298 return 0;
299}
300
liuyang4f7075a2024-12-20 17:35:06 +0800301int mbtk_wifi_get_pkt(mbtk_wifi_pkt_stats_t* pkt_stat)
302{
303 if(NULL == pkt_stat)
304 {
305 return -1;
306 }
307
308 char buf[1024] = {0};
309 char* ptr = NULL;
310 char* ptr2 = NULL;
311 unsigned long long ull_temp = 0;
312 FILE* fp = NULL;
313 fp = popen("cat /proc/net/dev | grep wlan0", "r");
314 if(NULL == fgets(buf, 1024, fp))
315 {
316 return -1;
317 }
318 pclose(fp);
319
320 LOGD("pkt:%s, len:%d\n", buf, strlen(buf));
321
322
323 pkt_stat->rx_bytes = strtoull(buf + 7, &ptr, 10);
324
325 pkt_stat->rx_packets = strtoull(ptr, &ptr2, 10);
326
327 pkt_stat->rx_errors = strtoull(ptr2, &ptr, 10);
328
329 pkt_stat->rx_dropped = strtoull(ptr, &ptr2, 10);
330
331 ull_temp = strtoull(ptr2, &ptr, 10);
332
333 ull_temp = strtoull(ptr, &ptr2, 10);
334
335 ull_temp = strtoull(ptr2, &ptr, 10);
336
337 ull_temp = strtoull(ptr, &ptr2, 10);
338
339 pkt_stat->tx_bytes = strtoull(ptr2, &ptr, 10);
340
341 pkt_stat->tx_packets = strtoull(ptr, &ptr2, 10);
342
343 pkt_stat->tx_errors = strtoull(ptr2, &ptr, 10);
344
345 pkt_stat->tx_dropped = strtoull(ptr, &ptr2, 10);
346
347
348
349 return 0;
350}
liuyangad56fc92024-12-17 19:10:11 +0800351
liuyang36d803c2024-12-26 13:16:09 +0800352int mbtk_wifi_get_dhcp(mbtk_lanhost_ts* lanhost_arr)
353{
354 if(NULL == lanhost_arr)
355 {
356 return -1;
357 }
358
359 int lanhost_num = 0;
360 int i = 0;
361 char* ptr = NULL;
362 FILE *file;
363 char buf[SETTING_LINE_MAX_LEN] = {0};
364
365 file = fopen(DHCP_PATH, "r");
366 if (!file)
367 {
368 return -1;
369 }
370
371 lanhost_arr->array_len = 0;
372 while(NULL != fgets(buf, SETTING_LINE_MAX_LEN, file))
373 {
374 LOGD("mbtk_wifi_get_file:%s\n", buf);
375 ptr = buf;
376
377 for(i = 0; i < 4; i++)
378 {
379 lanhost_arr->array[lanhost_num].name[i] = *ptr;
380 ptr++;
381 }
382
383 LOGD("mbtk_wifi_get_file name:%s\n", lanhost_arr->array[lanhost_num].name);
384
385 for(i = 0; i < 17; i++)
386 {
387 ptr++;
388 lanhost_arr->array[lanhost_num].macaddr[i]= *ptr;
389 }
390
391 LOGD("mbtk_wifi_get_file mac:%s\n", lanhost_arr->array[lanhost_num].macaddr);
392 ptr++;
393 for(i = 0; i < 15; i++)
394 {
395 ptr++;
396 lanhost_arr->array[lanhost_num].addr[i]= *ptr;
397 }
398 LOGD("mbtk_wifi_get_file addr:%s\n", lanhost_arr->array[lanhost_num].addr);
399 memcpy(lanhost_arr->array[lanhost_num].ifname, DEF_INTERFACE, strlen(DEF_INTERFACE));
400 lanhost_num++;
401
402 }
403
404
405 fclose(file);
406
407 return 0;
408}
409
410int mbkt_wifi_get_uptime(mbtk_lanhost_ts* lanhost_arr)
411{
412
413 if(NULL == lanhost_arr)
414 {
415 return -1;
416 }
417
418 unsigned long long connected_time = 0;
419 char buf[1024] = {0};
420 int i = 0;
421 FILE* fp = NULL;
422
423 lanhost_arr->array_len = 0;
424 fp = popen("hostapd_cli all_sta | grep connected_time", "r");
425 while(NULL != fgets(buf, 1024, fp))
426 {
427 if(0 > sscanf(buf, "connected_time=%llu", &connected_time))
428 {
429 return -1;
430 }
431
432 LOGD("qser wifi connected_time%llu", connected_time);
433 lanhost_arr->array[i].uptime = connected_time;
434 lanhost_arr->array_len++;
435 connected_time = 0;
436 i++;
437 }
438 pclose(fp);
439
440 return 0;
441}
442
liuyang57759622024-11-29 17:52:17 +0800443