• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiHope Community.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "wifi_device_util.h"
30 
31 #include <stdio.h>
32 
33 #include "cmsis_os2.h"
34 #include "ohos_init.h"
35 #include "wifi_hotspot_config.h"
36 #include "wm_type_def.h"
37 #include "wm_wifi.h"
38 
39 #define WIFI_MUTEX_TIMEOUT osWaitForever
40 
41 static osMutexId_t g_wifiGlobalLock = NULL;
42 static osMutexId_t g_wifiEventLock = NULL;
43 
InitWifiGlobalLock(void)44 static void InitWifiGlobalLock(void)
45 {
46     if (g_wifiGlobalLock == NULL) {
47         osMutexAttr_t globalMutexAttr = {
48             "WifiGloablLock",
49             osMutexRecursive | osMutexPrioInherit,
50             NULL,
51             0U
52         };
53         g_wifiGlobalLock = osMutexNew(&globalMutexAttr);
54     }
55 }
56 
InitWifiEventLock(void)57 static void InitWifiEventLock(void)
58 {
59     if (g_wifiEventLock == NULL) {
60         osMutexAttr_t eventMutexAttr = {
61             "WifiEventLock",
62             osMutexRecursive | osMutexPrioInherit,
63             NULL,
64             0U
65         };
66         g_wifiEventLock = osMutexNew(&eventMutexAttr);
67     }
68 }
69 
LockWifiGlobalLock(void)70 WifiErrorCode LockWifiGlobalLock(void)
71 {
72     if (g_wifiGlobalLock == NULL) {
73         InitWifiGlobalLock();
74     }
75 
76     osStatus_t ret = osMutexAcquire(g_wifiGlobalLock, WIFI_MUTEX_TIMEOUT);
77     if (ret != osOK) {
78         printf("[wifi_service] osMutexAcquire failed \n");
79         return ERROR_WIFI_UNKNOWN;
80     }
81 
82     return WIFI_SUCCESS;
83 }
84 
UnlockWifiGlobalLock(void)85 WifiErrorCode UnlockWifiGlobalLock(void)
86 {
87     if (g_wifiGlobalLock == NULL) {
88         return ERROR_WIFI_UNKNOWN;
89     }
90 
91     osStatus_t ret = osMutexRelease(g_wifiGlobalLock);
92     if (ret != osOK) {
93         printf("[wifi_service] osMutexUnlock failed \n");
94         return ERROR_WIFI_UNKNOWN;
95     }
96 
97     return WIFI_SUCCESS;
98 }
99 
LockWifiEventLock(void)100 WifiErrorCode LockWifiEventLock(void)
101 {
102     if (g_wifiEventLock == NULL) {
103         InitWifiEventLock();
104     }
105 
106     osStatus_t ret = osMutexAcquire(g_wifiEventLock, WIFI_MUTEX_TIMEOUT);
107     if (ret != osOK) {
108         printf("[wifi_service] osMutexAcquire event failed \n");
109         return ERROR_WIFI_UNKNOWN;
110     }
111 
112     return WIFI_SUCCESS;
113 }
114 
UnlockWifiEventLock(void)115 WifiErrorCode UnlockWifiEventLock(void)
116 {
117     if (g_wifiEventLock == NULL) {
118         return ERROR_WIFI_UNKNOWN;
119     }
120 
121     osStatus_t ret = osMutexRelease(g_wifiEventLock);
122     if (ret != osOK) {
123         printf("[wifi_service] osMutexUnlock event failed \n");
124         return ERROR_WIFI_UNKNOWN;
125     }
126 
127     return WIFI_SUCCESS;
128 }
129 
WmSec2HoSec(int mode)130 WifiSecurityType WmSec2HoSec(int mode)
131 {
132     switch (mode) {
133         case IEEE80211_ENCRYT_NONE:
134             return WIFI_SEC_TYPE_OPEN;
135         case IEEE80211_ENCRYT_WEP40:
136         case IEEE80211_ENCRYT_WEP104:
137             return WIFI_SEC_TYPE_WEP;
138         case IEEE80211_ENCRYT_TKIP_WPA:
139         case IEEE80211_ENCRYT_CCMP_WPA:
140         case IEEE80211_ENCRYT_TKIP_WPA2:
141         case IEEE80211_ENCRYT_CCMP_WPA2:
142         case IEEE80211_ENCRYT_AUTO_WPA:
143         case IEEE80211_ENCRYT_AUTO_WPA2:
144             return WIFI_SEC_TYPE_PSK;
145         default:
146             return -1;
147     }
148 }
149 
WmAuth2HoSec(int mode)150 WifiSecurityType WmAuth2HoSec(int mode)
151 {
152     switch (mode) {
153         case WM_WIFI_AUTH_MODE_OPEN:
154             return WIFI_SEC_TYPE_OPEN;
155         case WM_WIFI_AUTH_MODE_WEP_AUTO:
156             return WIFI_SEC_TYPE_WEP;
157         case WM_WIFI_AUTH_MODE_WPA_PSK_TKIP:
158         case WM_WIFI_AUTH_MODE_WPA_PSK_CCMP:
159         case WM_WIFI_AUTH_MODE_WPA_PSK_AUTO:
160         case WM_WIFI_AUTH_MODE_WPA2_PSK_TKIP:
161         case WM_WIFI_AUTH_MODE_WPA2_PSK_CCMP:
162         case WM_WIFI_AUTH_MODE_WPA2_PSK_AUTO:
163         case WM_WIFI_AUTH_MODE_WPA_WPA2_PSK_TKIP:
164         case WM_WIFI_AUTH_MODE_WPA_WPA2_PSK_CCMP:
165         case WM_WIFI_AUTH_MODE_WPA_WPA2_PSK_AUTO:
166             return WIFI_SEC_TYPE_PSK;
167         case WM_WIFI_AUTH_MODE_UNKNOWN:
168             return -1;
169         default:
170             return -1;
171     }
172     return -1;
173 }
174 
HoSec2WmSec(WifiSecurityType type)175 int HoSec2WmSec(WifiSecurityType type)
176 {
177     switch (type) {
178         case WIFI_SEC_TYPE_OPEN:
179             return IEEE80211_ENCRYT_NONE;
180         case WIFI_SEC_TYPE_WEP:
181             return IEEE80211_ENCRYT_WEP40;
182         case WIFI_SEC_TYPE_PSK:
183             return IEEE80211_ENCRYT_CCMP_WPA2;
184         default:
185             return -1;
186     }
187 }
188