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 distributed
10 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12 * language governing permissions and limitations under the License.
13 */
14
15 #include "wifi_hal_chba_interface.h"
16 #include "securec.h"
17 #include "wifi_common_hal.h"
18 #include "wifi_common_def.h"
19 #include "wifi_hal_adapter.h"
20 #include "wifi_hal_module_manage.h"
21 #include "wifi_log.h"
22 #include "wifi_wpa_hal.h"
23 #include "unistd.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "WifiHalChbaInterface"
27
28 #ifdef WPA_CTRL_IFACE_UNIX
29 #define CHBA_CMD "wpa_supplicant -c"CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf"\
30 " -g@abstract:"CONFIG_ROOR_DIR"/sockets/wpa/chba0"
31 #else
32 #define CHBA_CMD "wpa_supplicant -ichba0 -g"CONFIG_ROOR_DIR"/sockets/wpa"\
33 " -m"CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf"
34 #endif // WPA_CTRL_IFACE_UNIX
35
ChbaStartSupplicant(void)36 static WifiErrorNo ChbaStartSupplicant(void)
37 {
38 LOGD("Start chba supplicant");
39 if (CopyConfigFile("p2p_supplicant.conf") != 0) {
40 return WIFI_HAL_FAILED;
41 }
42 ModuleManageRetCode ret = StartModule(WPA_SUPPLICANT_NAME, CHBA_CMD);
43 if (ret == MM_SUCCESS) {
44 return WIFI_HAL_SUCCESS;
45 }
46 LOGE("start wpa_supplicant failed!");
47 return WIFI_HAL_FAILED;
48 }
49
ChbaConnectSupplicant(void)50 static WifiErrorNo ChbaConnectSupplicant(void)
51 {
52 LOGD("Ready to connect chba_wpa_supplicant.");
53 WifiWpaChbaInterface *pMainIfc = GetWifiWpaChbaInterface();
54 if (pMainIfc == NULL) {
55 return WIFI_HAL_SUPPLICANT_NOT_INIT;
56 }
57 return WIFI_HAL_SUCCESS;
58 }
59
ChbaStopSupplicant(void)60 static WifiErrorNo ChbaStopSupplicant(void)
61 {
62 LOGD("stop chba supplicant");
63 ModuleManageRetCode ret = StopModule(WPA_SUPPLICANT_NAME, false);
64 if (ret == MM_FAILED) {
65 LOGE("stop chba_wpa_supplicant failed!");
66 return WIFI_HAL_FAILED;
67 }
68 if (ret == MM_SUCCESS) {
69 ReleaseWpaGlobalInterface();
70 }
71 return WIFI_HAL_SUCCESS;
72 }
73
ChbaDisconnectSupplicant(void)74 static WifiErrorNo ChbaDisconnectSupplicant(void)
75 {
76 LOGD("Ready to disconnect chba_wpa_supplicant.");
77 WifiWpaChbaInterface *pMainIfc = GetWifiWpaChbaInterface();
78 if (pMainIfc == NULL) {
79 return WIFI_HAL_SUPPLICANT_NOT_INIT;
80 }
81 LOGD("Disconnect chba_wpa_supplicant finish!");
82 return WIFI_HAL_SUCCESS;
83 }
84
StopChbaWpaAndWpaHal(void)85 static WifiErrorNo StopChbaWpaAndWpaHal(void)
86 {
87 if (ChbaDisconnectSupplicant() != WIFI_HAL_SUCCESS) {
88 LOGE("chba_wpa_s hal already stop!");
89 }
90 WifiWpaInterface *pWpaInterface = GetWifiWapGlobalInterface();
91 if (pWpaInterface != NULL) {
92 pWpaInterface->wpaCliRemoveIface(pWpaInterface, "chba0");
93 }
94 if (ChbaStopSupplicant() != WIFI_HAL_SUCCESS) {
95 LOGE("chba_wpa_supplicant stop failed!");
96 return WIFI_HAL_FAILED;
97 }
98 LOGD("chba_wpa_supplicant stop success!");
99 ReleaseWpaChbaInterface();
100 return WIFI_HAL_SUCCESS;
101 }
102
AddChbaIface(void)103 static WifiErrorNo AddChbaIface(void)
104 {
105 WifiWpaInterface *pWpaInterface = GetWifiWapGlobalInterface();
106 if (pWpaInterface == NULL) {
107 LOGE("chba Get wpa interface failed!");
108 return WIFI_HAL_FAILED;
109 }
110 if (pWpaInterface->wpaCliConnect(pWpaInterface) < 0) {
111 LOGE("chba Failed to connect to wpa!");
112 return WIFI_HAL_FAILED;
113 }
114 AddInterfaceArgv argv;
115 if (strcpy_s(argv.name, sizeof(argv.name), "chba0") != EOK ||
116 strcpy_s(argv.confName, sizeof(argv.confName), "/data/service/el1/public/wifi/wpa_supplicant/p2p_supplicant.conf") != EOK) {
117 return WIFI_HAL_FAILED;
118 }
119 if (pWpaInterface->wpaCliAddIface(pWpaInterface, &argv, true) < 0) {
120 LOGE("Failed to add wpa iface!");
121 return WIFI_HAL_FAILED;
122 }
123 return WIFI_HAL_SUCCESS;
124 }
125
ChbaStart(void)126 WifiErrorNo ChbaStart(void)
127 {
128 if (ChbaStartSupplicant() != WIFI_HAL_SUCCESS) {
129 LOGE("chba_wpa_supplicant start failed!");
130 return WIFI_HAL_OPEN_SUPPLICANT_FAILED;
131 }
132 if (AddChbaIface() != WIFI_HAL_SUCCESS || ChbaConnectSupplicant() != WIFI_HAL_SUCCESS) {
133 LOGE("Supplicant connect chba_wpa_supplicant failed!");
134 StopChbaWpaAndWpaHal();
135 return WIFI_HAL_CONN_SUPPLICANT_FAILED;
136 }
137 int startchba = 1;
138 char eventStr[25];
139 if (sprintf_s(eventStr, sizeof(eventStr), "P2P-CONNECTED status =%d", startchba) < 0) {
140 LOGE("ChbaStop sprintf_s failed! ");
141 return WIFI_HAL_FAILED;
142 }
143 HalCallbackNotify(eventStr);
144 LOGD("Supplicant connect chba_wpa_supplicant success!");
145 return WIFI_HAL_SUCCESS;
146 }
147
ChbaStop(void)148 WifiErrorNo ChbaStop(void)
149 {
150 WifiErrorNo ret = StopChbaWpaAndWpaHal();
151 if (ret == WIFI_HAL_FAILED) {
152 return WIFI_HAL_FAILED;
153 }
154 int stopchba = 0;
155 char eventStr[25];
156 if (sprintf_s(eventStr, sizeof(eventStr), "P2P-CONNECTED status =%d", stopchba) < 0) {
157 LOGE("ChbaStop sprintf_s failed! ");
158 return WIFI_HAL_FAILED;
159 }
160 HalCallbackNotify(eventStr);
161 return WIFI_HAL_SUCCESS;
162 }