b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame^] | 1 | #include "sta_cli.h" |
| 2 | #include "lynq_wifi.h" |
| 3 | #include "mbtk_log.h" |
| 4 | |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include "mbtk_utils.h" |
| 10 | |
| 11 | |
| 12 | |
| 13 | #define STA_BUF_SIZE 2048 |
| 14 | lynq_wifi_event_handle_sta g_event_handle_sta_cb = NULL; |
| 15 | |
| 16 | |
| 17 | int qser_wifi_enable(void) |
| 18 | { |
| 19 | static char sta_cli_buf[STA_BUF_SIZE] = "OPEN"; |
| 20 | char reply[STA_BUF_SIZE]; |
| 21 | if(sta_cli_cmd_parse(sta_cli_buf, reply, STA_BUF_SIZE)){ |
| 22 | if(strlen(reply) > 0) |
| 23 | { |
| 24 | LOGE("reply data(%s).\n",reply); |
| 25 | }else{ |
| 26 | LOGE("No reply data(%s).\n",sta_cli_buf); |
| 27 | } |
| 28 | }else{ |
| 29 | LOGE("Parse cmd fail.\n"); |
| 30 | } |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | int qser_wifi_disable(void) |
| 37 | { |
| 38 | static char sta_cli_buf[STA_BUF_SIZE] = "CLOSE"; |
| 39 | char reply[STA_BUF_SIZE]; |
| 40 | if(sta_cli_cmd_parse(sta_cli_buf, reply, STA_BUF_SIZE)){ |
| 41 | if(strlen(reply) > 0) |
| 42 | { |
| 43 | LOGE("reply data(%s).\n",reply); |
| 44 | }else{ |
| 45 | LOGE("No reply data(%s).\n",sta_cli_buf); |
| 46 | } |
| 47 | }else{ |
| 48 | LOGE("Parse cmd fail.\n"); |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int qser_wifi_sta_param_set(lynq_wifi_sta_param_t *param_stat) |
| 54 | { |
| 55 | |
| 56 | sta_cli_ssid_set(param_stat->ssid); |
| 57 | sta_cli_psk_set(param_stat->passwd); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int qser_wifi_sta_param_get(lynq_wifi_sta_param_t *param_stat) |
| 63 | { |
| 64 | int len = 0; |
| 65 | sta_cli_ssid_get(param_stat->ssid); |
| 66 | len = strlen(param_stat->ssid); |
| 67 | param_stat->ssid[len - 1] = '\0'; |
| 68 | sta_cli_psk_get(param_stat->passwd); |
| 69 | len = strlen(param_stat->passwd); |
| 70 | param_stat->passwd[len - 1] = '\0'; |
| 71 | |
| 72 | LOGE("ssid: %s, passwd: %s", param_stat->ssid, param_stat->passwd); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int qser_wifi_sta_start(void) |
| 78 | { |
| 79 | char reply[STA_BUF_SIZE]; |
| 80 | |
| 81 | if(sta_cli_cmd_parse("ADD_NETWORK", reply, STA_BUF_SIZE)) |
| 82 | { |
| 83 | if(strlen(reply) > 0) |
| 84 | { |
| 85 | LOGE("reply data(%s).\n",reply); |
| 86 | }else |
| 87 | { |
| 88 | LOGE("No reply data\n"); |
| 89 | } |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | LOGE("Parse cmd fail.\n"); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | if(sta_cli_cmd_parse("SELECT_NETWORK", reply, STA_BUF_SIZE)) |
| 98 | { |
| 99 | if(strlen(reply) > 0) |
| 100 | { |
| 101 | LOGE("reply data(%s).\n",reply); |
| 102 | }else |
| 103 | { |
| 104 | LOGE("No reply data\n"); |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | LOGE("Parse cmd fail.\n"); |
| 110 | } |
| 111 | |
| 112 | if(sta_cli_cmd_parse("ENABLE_NETWORK", reply, STA_BUF_SIZE)) |
| 113 | { |
| 114 | if(strlen(reply) > 0) |
| 115 | { |
| 116 | LOGE("reply data(%s).\n",reply); |
| 117 | }else |
| 118 | { |
| 119 | LOGE("No reply data\n"); |
| 120 | } |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | LOGE("Parse cmd fail.\n"); |
| 125 | } |
| 126 | |
| 127 | mbtk_system("udhcpc -i wlan0"); |
| 128 | |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int qser_wifi_sta_stop(void) |
| 134 | { |
| 135 | char reply[STA_BUF_SIZE]; |
| 136 | |
| 137 | if(sta_cli_cmd_parse("DISABLE_NETWORK", reply, STA_BUF_SIZE)) |
| 138 | { |
| 139 | if(strlen(reply) > 0) |
| 140 | { |
| 141 | LOGE("reply data(%s).\n",reply); |
| 142 | }else |
| 143 | { |
| 144 | LOGE("No reply data\n"); |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | LOGE("Parse cmd fail.\n"); |
| 150 | } |
| 151 | |
| 152 | if(sta_cli_cmd_parse("REMOVE_NETWORK", reply, STA_BUF_SIZE)) |
| 153 | { |
| 154 | if(strlen(reply) > 0) |
| 155 | { |
| 156 | LOGE("reply data(%s).\n",reply); |
| 157 | }else |
| 158 | { |
| 159 | LOGE("No reply data\n"); |
| 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | LOGE("Parse cmd fail.\n"); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | int qser_wifi_sta_get_status(lynq_wifi_sta_status_t *status_stat) |
| 171 | { |
| 172 | char reply[STA_BUF_SIZE]; |
| 173 | int i = 0, j = 0; |
| 174 | int status_flag = 0; |
| 175 | |
| 176 | if(sta_cli_cmd_parse("STATUS", reply, STA_BUF_SIZE)) |
| 177 | { |
| 178 | if(strlen(reply) > 0) |
| 179 | { |
| 180 | LOGE("reply data(%s).\n",reply); |
| 181 | }else |
| 182 | { |
| 183 | LOGE("No reply data\n"); |
| 184 | } |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | LOGE("Parse cmd fail.\n"); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | for(i = 0; i < strlen(reply); i++) |
| 193 | { |
| 194 | if(',' == reply[i]) |
| 195 | { |
| 196 | j = 0; |
| 197 | status_flag++; |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | if(0 == status_flag) |
| 202 | { |
| 203 | status_stat->status = 4; |
| 204 | j++; |
| 205 | } |
| 206 | else if(1 == status_flag) |
| 207 | { |
| 208 | status_stat->ap_bssid[j] = reply[i]; |
| 209 | j++; |
| 210 | } |
| 211 | else if(2 == status_flag) |
| 212 | { |
| 213 | status_stat->ifname[j] = reply[i]; |
| 214 | j++; |
| 215 | } |
| 216 | else if(8 == status_flag) |
| 217 | { |
| 218 | status_stat->has_addr = '1'; |
| 219 | status_stat->addr[0].addr[j] = reply[i]; |
| 220 | j++; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | } |
| 226 | LOGE("state:%d, ap_bssid:%s, ifname:%s, has_addr:%c, addr:%s\n", status_stat->status, status_stat->ap_bssid, status_stat->ifname, status_stat->has_addr, |
| 227 | status_stat->addr[0].addr); |
| 228 | |
| 229 | return 0; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | int qser_wifi_register_handle(lynq_wifi_event_handle event_handle, lynq_wifi_event_handle_sta event_handle_sta, void *arg) |
| 236 | { |
| 237 | |
| 238 | |
| 239 | if(NULL != event_handle_sta) |
| 240 | { |
| 241 | g_event_handle_sta_cb = event_handle_sta; |
| 242 | |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | int qser_wifi_sta_start_scan(void) |
| 250 | { |
| 251 | char reply[STA_BUF_SIZE] = {0}; |
| 252 | lynq_wifi_sta_scan_list_t scan_list = {0}; |
| 253 | int i = 0, j = 0; |
| 254 | int status_flag = 0; |
| 255 | |
| 256 | if(sta_cli_cmd_parse("SCAN", reply, STA_BUF_SIZE)) |
| 257 | { |
| 258 | if(strlen(reply) > 0) |
| 259 | { |
| 260 | LOGE("reply data(%s).\n",reply); |
| 261 | }else |
| 262 | { |
| 263 | LOGE("No reply data\n"); |
| 264 | } |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | LOGE("Parse cmd fail.\n"); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | scan_list.cnt = 1; |
| 274 | |
| 275 | for(i = 0; i < strlen(reply); i++) |
| 276 | { |
| 277 | if(',' == reply[i]) |
| 278 | { |
| 279 | j = 0; |
| 280 | status_flag++; |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | if(0 == status_flag) |
| 285 | { |
| 286 | scan_list.info[0].bssid[j] = reply[i]; |
| 287 | j++; |
| 288 | } |
| 289 | else if(1 == status_flag) |
| 290 | { |
| 291 | //scan_list.info[0].bssid = reply[i]; |
| 292 | j++; |
| 293 | } |
| 294 | else if(2 == status_flag) |
| 295 | { |
| 296 | //scan_list.info[0].signal = atoi(reply[i]); |
| 297 | j++; |
| 298 | } |
| 299 | else if(4 == status_flag) |
| 300 | { |
| 301 | scan_list.info[0].essid[j] = reply[i]; |
| 302 | j++; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | g_event_handle_sta_cb(&scan_list); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | void lynq_wifi_event_handle_demo(lynq_wifi_event_s *event, void *arg) |
| 317 | { |
| 318 | if(event->id == LYNQ_WIFI_EVENT_AP_STATION) |
| 319 | LOGE("[lynq-wifi-demo] %s:event-id = %d- %d\n", __func__, event->id, event->status); |
| 320 | else if(event->id == LYNQ_WIFI_EVENT_AP_STA_STATUS) |
| 321 | LOGE("[lynq-wifi-demo] %s:event-id = %d,%d,%s,%s\n", __func__, event->id, |
| 322 | event->ap_sta_info.connected, event->ap_sta_info.mac,event->ap_sta_info.hostname); |
| 323 | if(event->id == LYNQ_WIFI_EVENT_STA_STATUS) |
| 324 | LOGE("[lynq-wifi-demo] %s:event-id = %d- %d\n", __func__, event->id, event->status); |
| 325 | else |
| 326 | return; |
| 327 | } |
| 328 | */ |
| 329 | |
| 330 | void lynq_wifi_event_handle_sta_demo(lynq_wifi_sta_scan_list_t *event) |
| 331 | { |
| 332 | int i = 0; |
| 333 | for (i = 0; i < event->cnt; i++) |
| 334 | { |
| 335 | LOGE("[lynq-wifi-demo] %s : ap[%d]:%s,%d,%d,%d,%s,%d,%d,%d\n", __func__, i, |
| 336 | event->info[i].essid, event->info[i].auth, |
| 337 | event->info[i].cipher, event->info[i].channel, event->info[i].bssid, |
| 338 | event->info[i].signal_level,event->info[i].frequency,event->info[i].signal); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | /* |
| 347 | |
| 348 | int qser_wifi_work_mode_set(lynq_wifi_work_mode_e type) |
| 349 | { |
| 350 | |
| 351 | } |
| 352 | |
| 353 | |
| 354 | int qser_wifi_work_mode_get(lynq_wifi_work_mode_e *type) |
| 355 | { |
| 356 | |
| 357 | } |
| 358 | |
| 359 | int qser_wifi_register_handle(lynq_wifi_event_handle event_handle, lynq_wifi_event_handle_staevent_handle_sta, void *arg) |
| 360 | { |
| 361 | |
| 362 | } |
| 363 | |
| 364 | |
| 365 | int qser_wifi_get_sta_pkt_stats(lynq_wifi_pkt_stats_t *pkt_stat) |
| 366 | { |
| 367 | |
| 368 | } |
| 369 | |
| 370 | int qser_wifi_sta_start_scan(void) |
| 371 | { |
| 372 | |
| 373 | } |
| 374 | |
| 375 | */ |
| 376 | |
| 377 | |
| 378 | |