liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1 | #include <string.h>
|
| 2 | #include <stdio.h>
|
| 3 | #include <stdlib.h>
|
| 4 | #include <pthread.h>
|
| 5 | #include <unistd.h>
|
| 6 | #include <sys/epoll.h>
|
| 7 | #include <errno.h>
|
| 8 | #include <sys/wait.h>
|
| 9 | #include <sys/types.h>
|
| 10 |
|
| 11 | #include "wpa_ctrl.h"
|
| 12 | #include "sta_ctrl.h"
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 13 | #include "mbtk_log.h"
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 14 | #include "mbtk_utils.h"
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 15 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 16 | //#include "sta_log.h"
|
| 17 |
|
| 18 | #define WPA_SUPPLICANT_LOG_FILE "/data/wpa_supplicant.log"
|
| 19 |
|
| 20 | static void
|
| 21 | sta_ctrl_wpa_req_cb(char *msg, size_t len);
|
| 22 |
|
| 23 | static void*
|
| 24 | sta_ctrl_event_thread_run( void *arg );
|
| 25 |
|
| 26 | static sta_err_enum
|
| 27 | sta_ctrl_close_connection(void);
|
| 28 |
|
| 29 | static sta_err_enum
|
| 30 | sta_ctrl_open_connection(void);
|
| 31 |
|
| 32 | static sta_err_enum
|
| 33 | sta_ctrl_reconnect(void);
|
| 34 |
|
| 35 | static void
|
| 36 | sta_ctrl_recv_event(void);
|
| 37 |
|
| 38 | static sta_err_enum
|
| 39 | sta_ctrl_conf_file_parse
|
| 40 | (
|
| 41 | const char *key,
|
| 42 | char *value
|
| 43 | );
|
| 44 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 45 | // extern struct wpa_ctrl;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 46 |
|
| 47 | static struct wpa_ctrl *sta_ctrl_conn;
|
| 48 | static struct wpa_ctrl *sta_mon_conn;
|
| 49 | static int sta_ctrl_attached = 0;
|
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 50 | static pthread_t sta_event_thread_id;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 51 | static int sta_event_thread_is_running = 0;
|
| 52 | static char sta_ctrl_conf_file_path[50];
|
| 53 | static char sta_ctrl_ifname[10];
|
| 54 | static sta_ctrl_msg_cb sta_ctrl_msg = NULL;
|
| 55 | static int sta_ctrl_pipe_fd[2];
|
| 56 |
|
| 57 | static void
|
| 58 | sta_ctrl_wpa_req_cb(char *msg, size_t len)
|
| 59 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 60 | LOGE("%s\n", msg);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 61 | }
|
| 62 |
|
| 63 | bool
|
| 64 | sta_ctrl_system
|
| 65 | (
|
| 66 | const char *command
|
| 67 | )
|
| 68 | {
|
| 69 | int result = TRUE;
|
| 70 | FILE *stream = NULL;
|
| 71 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 72 | LOGE("system call: %s\n", command);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 73 |
|
| 74 | stream = popen( command, "w" );
|
| 75 | if( stream == NULL )
|
| 76 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 77 | LOGE("system command failed\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 78 | result = FALSE;
|
| 79 | }
|
| 80 | else if( 0 > pclose( stream ) )
|
| 81 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 82 | LOGE("pclose command failed\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 83 | }
|
| 84 |
|
| 85 | return result;
|
| 86 | }
|
| 87 |
|
| 88 | /*
|
| 89 | * Return TRUE if process <name> is not running after 2 second.
|
| 90 | */
|
| 91 | bool
|
| 92 | sta_ctrl_kill_check(const char *name)
|
| 93 | {
|
| 94 | #define PROCESS_KILL_RETRY 40
|
| 95 | bool result;
|
| 96 | int tmp = 0;
|
| 97 | FILE *cmd;
|
| 98 | char pid_s[STA_BUF_SIZE];
|
| 99 | int pid;
|
| 100 | tmp = snprintf(pid_s,STA_BUF_SIZE,
|
| 101 | "pidof %s",name);
|
| 102 | pid_s[tmp] = '\0';
|
| 103 | tmp = 0;
|
| 104 | while (tmp++ < PROCESS_KILL_RETRY)
|
| 105 | {
|
| 106 | usleep(50000);/*50 mili second*/
|
| 107 | cmd = popen(pid_s, "r");
|
| 108 | pid = 0;
|
| 109 | bzero(pid_s, STA_BUF_SIZE);
|
| 110 | if(cmd)
|
| 111 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 112 | if(fgets(pid_s, STA_BUF_SIZE, cmd) == NULL) {
|
| 113 |
|
| 114 | }
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 115 | pclose(cmd);
|
| 116 | }
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 117 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 118 | pid = atoi(pid_s);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 119 | LOGE("%s pid =%d\n", name,pid);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 120 | /* If pid is zero we break from while*/
|
| 121 | if(pid == 0)
|
| 122 | {
|
| 123 | result = TRUE;
|
| 124 | goto exit_end;
|
| 125 | }
|
| 126 | }
|
| 127 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 128 | LOGE("PID still running after waiting 2 second.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 129 | result = FALSE;
|
| 130 | exit_end:
|
| 131 | #undef PROCESS_KILL_RETRY
|
| 132 | return result;
|
| 133 | }
|
| 134 |
|
| 135 | /*
|
| 136 | * Return TRUE if process <name> is running.
|
| 137 | */
|
| 138 | bool
|
| 139 | sta_ctrl_process_running(const char *name)
|
| 140 | {
|
| 141 | char pid_s[STA_BUF_SIZE];
|
| 142 | int tmp = snprintf(pid_s,STA_BUF_SIZE,
|
| 143 | "pidof %s",name);
|
| 144 | pid_s[tmp] = '\0';
|
| 145 | FILE *cmd = popen(pid_s, "r");
|
| 146 | bzero(pid_s, STA_BUF_SIZE);
|
| 147 | if(cmd)
|
| 148 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 149 | if(fgets(pid_s, STA_BUF_SIZE, cmd) == NULL) {
|
| 150 |
|
| 151 | }
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 152 | pclose(cmd);
|
| 153 | }
|
| 154 |
|
| 155 | int pid = atoi(pid_s);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 156 | LOGE("%s pid =%d\n", name,pid);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 157 | /* If pid is zero we break from while*/
|
| 158 | if(pid == 0)
|
| 159 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 160 | LOGE("%s not runnig.\n",name);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 161 | return FALSE;
|
| 162 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 163 | LOGE("%s is runnig.\n",name);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 164 | return TRUE;
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 |
|
| 169 | static void*
|
| 170 | sta_ctrl_event_thread_run( void *arg )
|
| 171 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 172 | LOGE("Thread[%ld] run().\n",pthread_self());
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 173 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 174 | int nready = 0;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 175 | struct epoll_event ev_sock,ev_pipe,events[20];
|
| 176 | int epfd = epoll_create(256);
|
| 177 | ev_sock.data.fd = wpa_ctrl_get_fd(sta_mon_conn);
|
| 178 | ev_sock.events = EPOLLIN | EPOLLET;
|
| 179 | epoll_ctl(epfd,EPOLL_CTL_ADD,wpa_ctrl_get_fd(sta_mon_conn),&ev_sock);
|
| 180 |
|
| 181 | ev_pipe.data.fd = sta_ctrl_pipe_fd[0];
|
| 182 | ev_pipe.events = EPOLLIN | EPOLLET;
|
| 183 | epoll_ctl(epfd,EPOLL_CTL_ADD,sta_ctrl_pipe_fd[0],&ev_pipe);
|
| 184 |
|
| 185 | for ( ; ; ) {
|
| 186 | if(!sta_event_thread_is_running){
|
| 187 | break;
|
| 188 | }
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 189 | LOGE("epoll_wait waitting...\n",nready);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 190 | nready = epoll_wait(epfd,events,20,-1);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 191 | LOGE("epoll_wait return.(count = %d)\n",nready);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 192 | int i;
|
| 193 | for(i=0;i<nready;++i) {
|
| 194 | if (events[i].events & EPOLLIN) {// Read
|
| 195 | if (events[i].data.fd < 0)
|
| 196 | continue;
|
| 197 |
|
| 198 | if(sta_mon_conn // sta_mon_conn can not be NULL
|
| 199 | && events[i].data.fd == wpa_ctrl_get_fd(sta_mon_conn)){
|
| 200 | sta_ctrl_recv_event();
|
| 201 | }else if(events[i].data.fd == sta_ctrl_pipe_fd[0]){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 202 | LOGE("Thread end.[fd = %d]\n",events[i].data.fd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 203 | // End thread
|
| 204 | char buf_end[10] = {0};
|
| 205 | if(read(sta_ctrl_pipe_fd[0],buf_end,10) > 0
|
| 206 | && strcmp(buf_end,"0") == 0){
|
| 207 | sta_event_thread_is_running = 0;
|
| 208 | break;
|
| 209 | }
|
| 210 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 211 | LOGE("No such fd[%d].\n",events[i].data.fd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 212 | }
|
| 213 | } else {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 214 | LOGE("event error.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 215 | }
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | close(epfd);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 220 | LOGE("Thread exit.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 221 | return ((void*)0);
|
| 222 | }
|
| 223 |
|
| 224 | static sta_err_enum
|
| 225 | sta_ctrl_close_connection(void)
|
| 226 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 227 | LOGE("start.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 228 | if (sta_ctrl_conn == NULL)
|
| 229 | return STA_ERR_UNKNOWN;
|
| 230 |
|
| 231 | if (sta_ctrl_attached) {
|
| 232 | wpa_ctrl_detach(sta_mon_conn);
|
| 233 | sta_ctrl_attached = 0;
|
| 234 | }
|
| 235 | wpa_ctrl_close(sta_ctrl_conn);
|
| 236 | sta_ctrl_conn = NULL;
|
| 237 | if (sta_mon_conn) {
|
| 238 | wpa_ctrl_close(sta_mon_conn);
|
| 239 | sta_mon_conn = NULL;
|
| 240 | }
|
| 241 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 242 | LOGE("end.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 243 | return STA_ERR_SUCCESS;
|
| 244 | }
|
| 245 |
|
| 246 | static sta_err_enum
|
| 247 | sta_ctrl_open_connection(void)
|
| 248 | {
|
| 249 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 250 |
|
| 251 | if(sta_ctrl_conn){
|
| 252 | return STA_ERR_UNKNOWN;
|
| 253 | }
|
| 254 |
|
| 255 | char ctrl_path[100] = {0};
|
| 256 | result = sta_ctrl_conf_file_parse("ctrl_interface", ctrl_path);
|
| 257 | if(STA_ERR_SUCCESS != result){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 258 | LOGE("sta_ctrl_conf_file_parse() fail(%d).\n",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 259 | return result;
|
| 260 | }
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 261 | sprintf(ctrl_path + strlen(ctrl_path),
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 262 | "/%s",
|
| 263 | sta_ctrl_ifname);
|
| 264 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 265 | LOGE("ctrl_path = \"%s\"\n",ctrl_path);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 266 |
|
| 267 | sta_ctrl_conn = wpa_ctrl_open(ctrl_path);
|
| 268 | if (sta_ctrl_conn == NULL) {
|
| 269 | sleep(1);
|
| 270 | return sta_ctrl_open_connection();
|
| 271 | }
|
| 272 |
|
| 273 | sta_mon_conn = wpa_ctrl_open(ctrl_path);
|
| 274 | if (sta_mon_conn == NULL) {
|
| 275 | return STA_ERR_UNKNOWN;
|
| 276 | }
|
| 277 |
|
| 278 | if (wpa_ctrl_attach(sta_mon_conn) == 0) {
|
| 279 | sta_ctrl_attached = 1;
|
| 280 | } else {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 281 | LOGE("Warning: Failed to attach to "
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 282 | "wpa_supplicant.\n");
|
| 283 | sta_ctrl_close_connection();
|
| 284 | return STA_ERR_UNKNOWN;
|
| 285 | }
|
| 286 |
|
| 287 | if(!sta_event_thread_is_running) {
|
| 288 | sta_event_thread_is_running = 1;
|
| 289 | int ret = pthread_create(&sta_event_thread_id,
|
| 290 | NULL,
|
| 291 | sta_ctrl_event_thread_run,
|
| 292 | NULL);
|
| 293 | if( ret != 0 ) {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 294 | LOGE( "Create thread error!\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 295 | }
|
| 296 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 297 | LOGE("sta_event_thread is running.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 298 | return STA_ERR_UNKNOWN;
|
| 299 | }
|
| 300 | return result;
|
| 301 | }
|
| 302 |
|
| 303 | static sta_err_enum
|
| 304 | sta_ctrl_reconnect(void)
|
| 305 | {
|
| 306 | if(STA_ERR_SUCCESS == sta_ctrl_close_connection()){
|
| 307 | return sta_ctrl_open_connection();
|
| 308 | }else{
|
| 309 | return STA_ERR_UNKNOWN;
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | static void
|
| 314 | sta_ctrl_recv_event(void)
|
| 315 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 316 | LOGE("start.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 317 | if (sta_ctrl_conn == NULL) {
|
| 318 | sta_ctrl_reconnect();
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 319 | LOGE("sta_ctrl_conn == NULL:end.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 320 | return;
|
| 321 | }
|
| 322 |
|
| 323 | while (wpa_ctrl_pending(sta_mon_conn) > 0) {
|
| 324 | char buf[4096];
|
| 325 | size_t len = sizeof(buf) - 1;
|
| 326 | if (wpa_ctrl_recv(sta_mon_conn, buf, &len) == 0) {
|
| 327 | buf[len] = '\0';
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 328 | LOGE("<<%s>>\n",buf);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 329 | if(sta_ctrl_msg)
|
| 330 | sta_ctrl_msg(buf);
|
| 331 | } else {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 332 | LOGE("Could not read pending message.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 333 | break;
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | if (wpa_ctrl_pending(sta_mon_conn) < 0) {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 338 | LOGE("Connection to wpa_supplicant lost - trying to "
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 339 | "reconnect\n");
|
| 340 | sta_ctrl_reconnect();
|
| 341 | }
|
| 342 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 343 | LOGE("end.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 344 | }
|
| 345 |
|
| 346 | static sta_err_enum
|
| 347 | sta_ctrl_conf_file_parse
|
| 348 | (
|
| 349 | const char *key,
|
| 350 | char *value
|
| 351 | )
|
| 352 | {
|
| 353 | sta_err_enum result = STA_ERR_UNKNOWN;
|
| 354 | FILE *fd = fopen(sta_ctrl_conf_file_path,"r");
|
| 355 | if(!fd){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 356 | LOGE("Open file(%s) fail(%d).\n",sta_ctrl_conf_file_path,errno);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 357 | return STA_ERR_UNKNOWN;
|
| 358 | }
|
| 359 |
|
| 360 | char buf[1024];
|
| 361 | while(fgets(buf,1024,fd)){
|
| 362 | char *start = strstr(buf,key);
|
| 363 | if(start){ // Find key
|
| 364 | char *tmp = start + strlen(start) -1;
|
| 365 | while(*tmp){
|
| 366 | if(*tmp == '\r'
|
| 367 | || *tmp == '\n'){
|
| 368 | *tmp = '\0';
|
| 369 | }else{
|
| 370 | break;
|
| 371 | }
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 372 | tmp--;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 373 | }
|
| 374 |
|
| 375 | int size = snprintf(value,100,
|
| 376 | "%s",
|
| 377 | start + strlen(key) + 1);
|
| 378 | value[size] = '\0';
|
| 379 | result = STA_ERR_SUCCESS;
|
| 380 | break;
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 | fclose(fd);
|
| 385 |
|
| 386 | return result;
|
| 387 | }
|
| 388 |
|
| 389 | /***************************************************************/
|
| 390 | /************************ Public Fuction ***********************/
|
| 391 | /***************************************************************/
|
| 392 | sta_err_enum
|
| 393 | sta_ctrl_cmd_process
|
| 394 | (
|
| 395 | const char *cmd,
|
| 396 | char *reply,
|
| 397 | size_t reply_len
|
| 398 | )
|
| 399 | {
|
| 400 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 401 | bzero(reply,reply_len);
|
| 402 | reply_len = reply_len - 1;
|
| 403 | int ret = wpa_ctrl_request(sta_ctrl_conn,
|
| 404 | cmd,
|
| 405 | strlen(cmd),
|
| 406 | reply,
|
| 407 | &reply_len,
|
| 408 | sta_ctrl_wpa_req_cb);
|
| 409 | if (ret == -2) {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 410 | LOGE("command timed out.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 411 | result = STA_ERR_TIMEOUT;
|
| 412 | goto end_fail;
|
| 413 | } else if (ret < 0) {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 414 | LOGE("command failed.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 415 | result = STA_ERR_UNKNOWN;
|
| 416 | goto end_fail;
|
| 417 | } else {
|
| 418 | reply[reply_len] = '\0';
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 419 | LOGE("1:%s\n", reply);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 420 |
|
| 421 | if(reply_len > 0 && reply[reply_len - 1] != '\n')
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 422 | LOGE("\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 423 | }
|
| 424 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 425 | //end_success:
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 426 |
|
| 427 | return result;
|
| 428 | end_fail:
|
| 429 |
|
| 430 | return result;
|
| 431 | }
|
| 432 |
|
| 433 | /**
|
| 434 | * Open or close wlan driver.
|
| 435 | */
|
| 436 | sta_err_enum
|
| 437 | sta_ctrl_driver_init(bool open)
|
| 438 | {
|
| 439 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 440 |
|
| 441 | FILE *fd_tmp = NULL;
|
| 442 | if(open){
|
| 443 | fd_tmp = popen("/etc/wifi/mbtk_wifi_driver.sh sta start","r");
|
| 444 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 445 | fd_tmp = popen("/etc/wifi/mbtk_wifi_driver.sh sta stop","r");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 446 | }
|
| 447 |
|
| 448 | if(fd_tmp){
|
| 449 | char buf[200] = {0};
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 450 | if(fgets(buf,200,fd_tmp) == NULL) {
|
| 451 |
|
| 452 | }
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 453 | pclose(fd_tmp);
|
| 454 | if(strlen(buf) > 0){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 455 | LOGE("Driver %s fail.(%s)\n",(open?"open":"close"),buf);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 456 | result = STA_ERR_DRIVER;
|
| 457 | }else{// Open wpa_supplicant
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 458 | LOGE("Driver %s success.\n",(open?"open":"close"));
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 459 | }
|
| 460 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 461 | LOGE("Driver %s fail.(%s)\n",(open?"open":"close"));
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 462 | result = STA_ERR_DRIVER;
|
| 463 | }
|
| 464 |
|
| 465 | return result;
|
| 466 | }
|
| 467 |
|
| 468 | sta_err_enum
|
| 469 | sta_ctrl_wpa_init
|
| 470 | (
|
| 471 | const char *conf_file,
|
| 472 | const char *interface,
|
| 473 | sta_ctrl_msg_cb cb
|
| 474 | )
|
| 475 | {
|
| 476 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 477 | if(!conf_file
|
| 478 | || strlen(conf_file) == 0){
|
| 479 | result = STA_ERR_UNKNOWN;
|
| 480 | goto end_fail;
|
| 481 | }
|
| 482 |
|
| 483 | if(!interface
|
| 484 | || strlen(interface) == 0){
|
| 485 | result = STA_ERR_UNKNOWN;
|
| 486 | goto end_fail;
|
| 487 | }
|
| 488 |
|
| 489 | sta_ctrl_msg = cb;
|
| 490 |
|
| 491 | int size = snprintf(sta_ctrl_conf_file_path,
|
| 492 | 50,
|
| 493 | "%s",
|
| 494 | conf_file);
|
| 495 | sta_ctrl_conf_file_path[size] = '\0';
|
| 496 | size = snprintf(sta_ctrl_ifname,
|
| 497 | 10,
|
| 498 | "%s",
|
| 499 | interface);
|
| 500 | sta_ctrl_ifname[size] = '\0';
|
| 501 |
|
| 502 | if(pipe(sta_ctrl_pipe_fd)){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 503 | LOGE("pipe() fail(%d).\n",errno);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 504 | result = STA_ERR_UNKNOWN;
|
| 505 | goto end_fail;
|
| 506 | }
|
| 507 |
|
| 508 | FILE *fd_tmp = popen("pidof wpa_supplicant","r");
|
| 509 | if(fd_tmp){
|
| 510 | char buf[200] = {0};
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 511 | if(fgets(buf,200,fd_tmp) == NULL) {
|
| 512 |
|
| 513 | }
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 514 | pclose(fd_tmp);
|
| 515 | if(strlen(buf) > 0){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 516 | LOGE("wpa_supplicant is running.(%s)\n",buf);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 517 | }else{// Open wpa_supplicant
|
| 518 | bzero(buf,200);
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 519 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 520 | snprintf(buf,200,
|
| 521 | "wpa_supplicant -Dnl80211 -c%s -i%s -f%s -ddd &",
|
| 522 | conf_file,
|
| 523 | interface,
|
| 524 | WPA_SUPPLICANT_LOG_FILE);
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 525 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 526 | /*
|
| 527 | snprintf(buf,200,
|
| 528 | "wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wifi/wpa_supplicant.conf -B");
|
| 529 | */
|
| 530 | if (sta_ctrl_system(buf)){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 531 | LOGE("\"%s\" success.\n",buf);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 532 | sleep(1);
|
| 533 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 534 | LOGE("\"%s\" fail.\n",buf);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 535 | result = STA_ERR_UNKNOWN;
|
| 536 | goto end_fail;
|
| 537 | }
|
| 538 | }
|
| 539 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 540 | LOGE("\"pidof wpa_supplicant\" fail\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 541 | result = STA_ERR_UNKNOWN;
|
| 542 | goto end_fail;
|
| 543 | }
|
| 544 |
|
| 545 | result = sta_ctrl_open_connection();
|
| 546 | if(STA_ERR_SUCCESS != result) {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 547 | LOGE("sta_ctrl_open_connection() fail(%d).\n",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 548 | goto end_fail;
|
| 549 | }
|
| 550 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 551 | //end_success:
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 552 |
|
| 553 | return result;
|
| 554 | end_fail:
|
| 555 |
|
| 556 | return result;
|
| 557 | }
|
| 558 |
|
| 559 | sta_err_enum
|
| 560 | sta_ctrl_wpa_deinit
|
| 561 | (
|
| 562 | void
|
| 563 | )
|
| 564 | {
|
| 565 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 566 | result = sta_ctrl_close_connection();
|
| 567 | if(STA_ERR_SUCCESS != result){
|
| 568 | goto end_fail;
|
| 569 | }
|
| 570 |
|
| 571 | bzero(sta_ctrl_conf_file_path,50);
|
| 572 | bzero(sta_ctrl_ifname,10);
|
| 573 |
|
| 574 | sta_event_thread_is_running = 0;
|
| 575 | // End thread.
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 576 | mbtk_write(sta_ctrl_pipe_fd[1],"0",1);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 577 |
|
| 578 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 579 | LOGE("Waitting for thread(%ld) exit.\n",sta_event_thread_id);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 580 |
|
| 581 | pthread_join(sta_event_thread_id,NULL);
|
| 582 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 583 | LOGE("pthread_join() return.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 584 |
|
| 585 |
|
| 586 | close(sta_ctrl_pipe_fd[0]);
|
| 587 | close(sta_ctrl_pipe_fd[1]);
|
| 588 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 589 | sta_ctrl_msg = NULL;
|
| 590 |
|
| 591 | // Stop process wpa_supplicant
|
| 592 | if(sta_ctrl_system("killall -15 wpa_supplicant")
|
| 593 | && sta_ctrl_kill_check("wpa_supplicant")){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 594 | LOGE("\"killall -15 wpa_supplicant\" success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 595 | }else{
|
| 596 | if(sta_ctrl_system("killall -9 wpa_supplicant")){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 597 | LOGE("\"killall -9 wpa_supplicant\" success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 598 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 599 | LOGE("\"killall -9 wpa_supplicant\" fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 600 | }
|
| 601 | }
|
| 602 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 603 | //end_success:
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 604 | LOGE("sta_ctrl_wpa_deinit() end(success).\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 605 | return result;
|
| 606 | end_fail:
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 607 | LOGE("sta_ctrl_wpa_deinit() end(fail)[%s].\n",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 608 | return result;
|
| 609 | }
|
| 610 |
|