1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdlib.h>
17 #include <poll.h>
18 #include <string.h>
19 #include "securec.h"
20 #include "wifi_common_hal.h"
21 #include "wifi_hal_crpc_server.h"
22 #include "wifi_log.h"
23 #include "wifi_wpa_common.h"
24 #include "wifi_hal_common_func.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "WifiCommonHal"
28
29 WifiWpaChbaInterface *g_wpaChbaInterface = NULL;
30
31 #define WPA_CMD_BUF_LEN 400
32 #define WPA_CMD_REPLY_BUF_SMALL_LEN 64
33
SendComCmd(const char * sendcmd)34 int SendComCmd(const char* sendcmd)
35 {
36 if (sendcmd == NULL) {
37 return -1;
38 }
39 char buf[WPA_CMD_REPLY_BUF_SMALL_LEN] = {0};
40 char cmd[WPA_CMD_BUF_LEN] = {0};
41
42 int ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "%s", sendcmd);
43 if (ret < 0) {
44 LOGE("snprint err is %d", ret);
45 return -1;
46 }
47 if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
48 LOGE("WpaCliCmd err");
49 return -1;
50 }
51 size_t bufLen = strlen(buf);
52 if (strncmp(cmd, "IFNAME=chba0 STATUS", strlen("IFNAME=chba0 STATUS")) == 0) {
53 for (int i = 0; i < bufLen; i++) {
54 buf[i] = buf[i] == '\n' ? '*' : buf[i];
55 }
56 char *sep = "*";
57 char *retbuf = strtok(buf, sep);
58 retbuf = strtok(NULL, sep);
59 HalCallbackNotify(retbuf);
60 }
61 return 0;
62 }
63
HalEmitEventCallbackMsg(WifiHalEventCallbackMsg * pCbkMsg,WifiHalEvent event)64 static void HalEmitEventCallbackMsg(WifiHalEventCallbackMsg *pCbkMsg, WifiHalEvent event)
65 {
66 if (pCbkMsg == NULL) {
67 return;
68 }
69 RpcServer *server = GetRpcServer();
70 if (server == NULL) {
71 LOGE("Rpc server not exists!");
72 free(pCbkMsg);
73 pCbkMsg = NULL;
74 return;
75 }
76 if (PushBackCallbackMsg(event, pCbkMsg) != 0) {
77 free(pCbkMsg);
78 pCbkMsg = NULL;
79 return;
80 }
81 if (EmitEvent(server, event) < 0) {
82 PopBackCallbackMsg(event);
83 free(pCbkMsg);
84 pCbkMsg = NULL;
85 }
86 return;
87 }
88
HalCallbackNotify(const char * event)89 int HalCallbackNotify(const char* event)
90 {
91 if (event == NULL) {
92 LOGE("recv notify message is null");
93 return -1;
94 }
95 WifiHalEventCallbackMsg *pCbMsg = (WifiHalEventCallbackMsg *)calloc(1, sizeof(WifiHalEventCallbackMsg));
96 if (pCbMsg == NULL) {
97 LOGE("create callback message failed!");
98 return -1;
99 }
100 strcpy_s(pCbMsg->msg.commsg.event, sizeof(pCbMsg->msg.commsg.event), event);
101 HalEmitEventCallbackMsg(pCbMsg, WIFI_HAL_COMMON_EVENT);
102 return 0;
103 }
104
GetWifiWpaChbaInterface()105 WifiWpaChbaInterface *GetWifiWpaChbaInterface()
106 {
107 if (g_wpaChbaInterface != NULL) {
108 return g_wpaChbaInterface;
109 }
110 g_wpaChbaInterface = (WifiWpaChbaInterface *)calloc(1, sizeof(WifiWpaChbaInterface));
111 if (g_wpaChbaInterface == NULL) {
112 LOGE("alloc memory for chba interface failed!");
113 return NULL;
114 }
115 strcpy_s(g_wpaChbaInterface->ifname, sizeof(g_wpaChbaInterface->ifname), "chba0");
116 return g_wpaChbaInterface;
117 }
118
ReleaseWpaChbaInterface(void)119 void ReleaseWpaChbaInterface(void)
120 {
121 if (g_wpaChbaInterface != NULL) {
122 free(g_wpaChbaInterface);
123 g_wpaChbaInterface = NULL;
124 }
125 }
126