1 /*
2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
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 #include "wifi_device.h"
16 #include "wifi_config.h"
17 #include "rtos_ohos_al.h"
18 #include "dbg.h"
19
20 typedef struct {
21 rtos_mutex eventMutex;
22 LOS_DL_LIST eventLst;
23 uint8_t eventCnt;
24 uint8_t initFlag;
25 } WifiEventData;
26
27 WifiEventData g_eventData = {0};
28
EventLock()29 static int32_t EventLock()
30 {
31 if (g_eventData.eventMutex == NULL) {
32 if (rtos_mutex_create(&g_eventData.eventMutex) != 0) {
33 dbg("wifiDevice:mutex create err\r\n");
34 return ERROR_WIFI_NOT_AVAILABLE;
35 }
36 }
37 if (g_eventData.eventMutex != NULL) { \
38 if (rtos_mutex_lock(g_eventData.eventMutex, osWaitForever) != 0) {
39 dbg("wifiDevice:mutex lock err\r\n");
40 return ERROR_WIFI_NOT_AVAILABLE;
41 }
42 }
43 return WIFI_SUCCESS;
44 }
45
EventUnLock()46 static int32_t EventUnLock()
47 {
48 if (g_eventData.eventMutex == NULL) {
49 dbg("wifiDevice:mutex lock null\r\n");
50 return ERROR_WIFI_NOT_AVAILABLE;
51 }
52
53 if (rtos_mutex_unlock(g_eventData.eventMutex) != 0) {
54 dbg("wifiDevice:mutex unlock err\r\n");
55 return ERROR_WIFI_NOT_AVAILABLE;
56 }
57 return WIFI_SUCCESS;
58 }
59
RegisterWifiEvent(WifiEvent * event)60 WifiErrorCode RegisterWifiEvent(WifiEvent *event)
61 {
62 PARAM_CHECK(event);
63
64 EventLock();
65
66 if (g_eventData.eventLst.pstNext == NULL) {
67 LOS_ListInit(&g_eventData.eventLst);
68 }
69
70 if (g_eventData.eventCnt >= WIFI_MAX_EVENT_SIZE) {
71 dbg("wifiDevice: RegisterWifiEvent! eventCnt overflow %d\r\n", g_eventData.eventCnt);
72 EventUnLock();
73 return WIFI_SUCCESS;
74 }
75
76 WifiEventNode *eventNode = (WifiEventNode *)rtos_calloc(1, sizeof(*eventNode));
77 if (eventNode == NULL) {
78 EventUnLock();
79 return ERROR_WIFI_UNKNOWN;
80 }
81
82 eventNode->event = event;
83 LOS_ListTailInsert(&g_eventData.eventLst, &eventNode->node);
84 g_eventData.eventCnt++;
85 EventUnLock();
86
87 dbg("wifiDevice: RegisterWifiEvent!\r\n");
88 return WIFI_SUCCESS;
89 }
90
UnRegisterWifiEvent(const WifiEvent * event)91 WifiErrorCode UnRegisterWifiEvent(const WifiEvent *event)
92 {
93 PARAM_CHECK(event);
94 WifiEventNode *pos = NULL;
95
96 EventLock();
97
98 if (g_eventData.eventCnt == 0) {
99 dbg("wifiDevice: eventCnt = 0!\r\n");
100 EventUnLock();
101 return ERROR_WIFI_UNKNOWN;
102 }
103
104 LOS_DL_LIST_FOR_EACH_ENTRY(pos, &g_eventData.eventLst, WifiEventNode, node) {
105 if (pos->event == event) {
106 LOS_ListDelete(&pos->node);
107 rtos_free(pos);
108 g_eventData.eventCnt--;
109 EventUnLock();
110 return WIFI_SUCCESS;
111 }
112 }
113 EventUnLock();
114 return ERROR_WIFI_UNKNOWN;
115 }
116
DoScanCallBack(int state,uint8_t size)117 void DoScanCallBack(int state, uint8_t size)
118 {
119 WifiEventNode *pos = NULL;
120 EventLock();
121 /* scan callback */
122 LOS_DL_LIST_FOR_EACH_ENTRY(pos, &g_eventData.eventLst, WifiEventNode, node) {
123 if (pos->event->OnWifiScanStateChanged != NULL) {
124 pos->event->OnWifiScanStateChanged(state, size);
125 }
126 }
127 EventUnLock();
128 }
129
DoStaConnectCallBack(int state,WifiLinkedInfo * info)130 void DoStaConnectCallBack(int state, WifiLinkedInfo *info)
131 {
132 WifiEventNode *pos = NULL;
133 EventLock();
134 if (info != NULL) {
135 LOS_DL_LIST_FOR_EACH_ENTRY(pos, &g_eventData.eventLst, WifiEventNode, node) {
136 if (pos->event->OnWifiConnectionChanged != NULL) {
137 pos->event->OnWifiConnectionChanged(state, info);
138 }
139 }
140 }
141 EventUnLock();
142 }
143
DoStaJoinCallBack(StationInfo * info)144 void DoStaJoinCallBack(StationInfo *info)
145 {
146 WifiEventNode *pos = NULL;
147 EventLock();
148 if (info != NULL) {
149 LOS_DL_LIST_FOR_EACH_ENTRY(pos, &g_eventData.eventLst, WifiEventNode, node) {
150 if (pos->event->OnHotspotStaJoin != NULL) {
151 pos->event->OnHotspotStaJoin(info);
152 }
153 }
154 }
155 EventUnLock();
156 }
157
DoStaLeaveCallBack(StationInfo * info)158 void DoStaLeaveCallBack(StationInfo *info)
159 {
160 WifiEventNode *pos = NULL;
161 dbg("DoStaLeaveCallBack\r\n");
162 EventLock();
163 if (info != NULL) {
164 LOS_DL_LIST_FOR_EACH_ENTRY(pos, &g_eventData.eventLst, WifiEventNode, node) {
165 if (pos->event->OnHotspotStaLeave != NULL) {
166 pos->event->OnHotspotStaLeave(info);
167 }
168 }
169 }
170 EventUnLock();
171 }
172
DoApStateCallBack(int state)173 void DoApStateCallBack(int state)
174 {
175 WifiEventNode *pos = NULL;
176 EventLock();
177 /* scan callback */
178 LOS_DL_LIST_FOR_EACH_ENTRY(pos, &g_eventData.eventLst, WifiEventNode, node) {
179 if (pos->event->OnHotspotStateChanged != NULL) {
180 pos->event->OnHotspotStateChanged(state);
181 }
182 }
183 EventUnLock();
184 }
185