• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "wifi_hal_crpc_server.h"
17 #include "wifi_hal_crpc_base.h"
18 #include "wifi_hal_crpc_chip.h"
19 #include "wifi_hal_crpc_supplicant.h"
20 #include "wifi_hal_crpc_sta.h"
21 #include "wifi_hal_crpc_ap.h"
22 #include "wifi_hal_crpc_common.h"
23 #include "wifi_hal_crpc_p2p.h"
24 #include "securec.h"
25 #include "wifi_log.h"
26 #include "wifi_hal_common_func.h"
27 
28 #undef LOG_TAG
29 #define LOG_TAG "WifiHalCrpcServer"
30 
31 /* Defines the mapping between global function names and functions. */
32 static WifiHalRpcFunc *g_rpcFuncHandle = NULL;
33 static RpcServer *g_rpcServer = NULL;
34 
SetRpcServerInited(RpcServer * server)35 void SetRpcServerInited(RpcServer *server)
36 {
37     g_rpcServer = server;
38     return;
39 }
40 
GetRpcServer(void)41 RpcServer *GetRpcServer(void)
42 {
43     return g_rpcServer;
44 }
45 
GetPos(const char * name)46 static int GetPos(const char *name)
47 {
48     int total = 0;
49     while (*name) {
50         total += *name;
51         ++name;
52     }
53     if (total < 0) {
54         total *= -1;
55     }
56     return total % RPC_FUNC_NUM;
57 }
58 
PushRpcFunc(const char * name,Rpcfunc func)59 static int PushRpcFunc(const char *name, Rpcfunc func)
60 {
61     if (g_rpcFuncHandle == NULL || name == NULL || func == NULL) {
62         return -1;
63     }
64     int pos = GetPos(name);
65     if (g_rpcFuncHandle[pos].func == NULL) {
66         StrSafeCopy(g_rpcFuncHandle[pos].funcname, sizeof(g_rpcFuncHandle[pos].funcname), name);
67         g_rpcFuncHandle[pos].func = func;
68     } else {
69         WifiHalRpcFunc *p = g_rpcFuncHandle + pos;
70         while (p->next != NULL) {
71             p = p->next;
72         }
73         WifiHalRpcFunc *q = (WifiHalRpcFunc *)calloc(1, sizeof(WifiHalRpcFunc));
74         if (q == NULL) {
75             return -1;
76         }
77         StrSafeCopy(q->funcname, sizeof(q->funcname), name);
78         q->func = func;
79         q->next = NULL;
80         p->next = q;
81     }
82     return 0;
83 }
84 
InitRpcFuncMapBase(void)85 static int InitRpcFuncMapBase(void)
86 {
87     int ret = 0;
88     ret += PushRpcFunc("GetName", RpcGetName);
89     ret += PushRpcFunc("GetType", RpcGetType);
90     return ret;
91 }
92 
InitRpcFuncMapChip(void)93 static int InitRpcFuncMapChip(void)
94 {
95     int ret = 0;
96     ret += PushRpcFunc("GetWifiChip", RpcGetWifiChip);
97     ret += PushRpcFunc("GetWifiChipIds", RpcGetWifiChipIds);
98     ret += PushRpcFunc("GetChipId", RpcGetChipId);
99     ret += PushRpcFunc("CreateIface", RpcCreateIface);
100     ret += PushRpcFunc("GetIface", RpcGetIface);
101     ret += PushRpcFunc("GetIfaceNames", RpcGetIfaceNames);
102     ret += PushRpcFunc("RemoveIface", RpcRemoveIface);
103     ret += PushRpcFunc("GetCapabilities", RpcGetCapabilities);
104     ret += PushRpcFunc("GetSupportedComboModes", RpcGetSupportedComboModes);
105     ret += PushRpcFunc("ConfigComboModes", RpcConfigComboModes);
106     ret += PushRpcFunc("GetComboModes", RpcGetComboModes);
107     ret += PushRpcFunc("RequestFirmwareDebugDump", RpcRequestFirmwareDebugDump);
108     ret += PushRpcFunc("SetPowerMode", RpcSetPowerMode);
109     return ret;
110 }
111 
InitRpcFuncMapSupplicant(void)112 static int InitRpcFuncMapSupplicant(void)
113 {
114     int ret = 0;
115     ret += PushRpcFunc("StartSupplicant", RpcStartSupplicant);
116     ret += PushRpcFunc("StopSupplicant", RpcStopSupplicant);
117     ret += PushRpcFunc("ConnectSupplicant", RpcConnectSupplicant);
118     ret += PushRpcFunc("DisconnectSupplicant", RpcDisconnectSupplicant);
119     ret += PushRpcFunc("RequestToSupplicant", RpcRequestToSupplicant);
120     ret += PushRpcFunc("SetPowerSave", RpcSetPowerSave);
121     ret += PushRpcFunc("WpaSetCountryCode", RpcWpaSetCountryCode);
122     ret += PushRpcFunc("WpaGetCountryCode", RpcWpaGetCountryCode);
123     return ret;
124 }
125 
InitRpcFuncMapSta(void)126 static int InitRpcFuncMapSta(void)
127 {
128     int ret = 0;
129     ret += PushRpcFunc("Start", RpcStart);
130     ret += PushRpcFunc("Stop", RpcStop);
131     ret += PushRpcFunc("StartScan", RpcStartScan);
132     ret += PushRpcFunc("GetScanInfos", RpcGetScanInfos);
133     ret += PushRpcFunc("StartPnoScan", RpcStartPnoScan);
134     ret += PushRpcFunc("StopPnoScan", RpcStopPnoScan);
135     ret += PushRpcFunc("Connect", RpcConnect);
136     ret += PushRpcFunc("Reconnect", RpcReconnect);
137     ret += PushRpcFunc("Reassociate", RpcReassociate);
138     ret += PushRpcFunc("Disconnect", RpcDisconnect);
139     ret += PushRpcFunc("GetStaCapabilities", RpcGetStaCapabilities);
140     ret += PushRpcFunc("GetDeviceMacAddress", RpcGetDeviceMacAddress);
141     ret += PushRpcFunc("GetFrequencies", RpcGetFrequencies);
142     ret += PushRpcFunc("SetAssocMacAddr", RpcSetAssocMacAddr);
143     ret += PushRpcFunc("SetScanningMacAddress", RpcSetScanningMacAddress);
144     ret += PushRpcFunc("DeauthLastRoamingBssid", RpcDeauthLastRoamingBssid);
145     ret += PushRpcFunc("GetSupportFeature", RpcGetSupportFeature);
146     ret += PushRpcFunc("RunCmd", RpcRunCmd);
147     ret += PushRpcFunc("SetWifiTxPower", RpcSetWifiTxPower);
148     ret += PushRpcFunc("RemoveNetwork", RpcRemoveNetwork);
149     ret += PushRpcFunc("AddNetwork", RpcAddNetwork);
150     ret += PushRpcFunc("EnableNetwork", RpcEnableNetwork);
151     ret += PushRpcFunc("DisableNetwork", RpcDisableNetwork);
152     ret += PushRpcFunc("SetNetwork", RpcSetNetwork);
153     ret += PushRpcFunc("SaveNetworkConfig", RpcSaveNetworkConfig);
154     ret += PushRpcFunc("StartWpsPbcMode", RpcStartWpsPbcMode);
155     ret += PushRpcFunc("StartWpsPinMode", RpcStartWpsPinMode);
156     ret += PushRpcFunc("StopWps", RpcStopWps);
157     ret += PushRpcFunc("GetRoamingCapabilities", RpcGetRoamingCapabilities);
158     ret += PushRpcFunc("SetRoamConfig", RpcSetRoamConfig);
159     ret += PushRpcFunc("WpaGetNetwork", RpcWpaGetNetwork);
160     ret += PushRpcFunc("WpaAutoConnect", RpcWpaAutoConnect);
161     ret += PushRpcFunc("WpaBlocklistClear", RpcWpaBlocklistClear);
162     ret += PushRpcFunc("GetNetworkList", RpcGetNetworkList);
163     ret += PushRpcFunc("GetConnectSignalInfo", RpcGetConnectSignalInfo);
164     return ret;
165 }
166 
InitRpcFuncMapAp(void)167 static int InitRpcFuncMapAp(void)
168 {
169     int ret = 0;
170     ret += PushRpcFunc("StartSoftAp", RpcStartSoftAp);
171     ret += PushRpcFunc("StopSoftAp", RpcStopSoftAp);
172     ret += PushRpcFunc("SetHostapdConfig", RpcSetHostapdConfig);
173     ret += PushRpcFunc("GetStaInfos", RpcGetStaInfos);
174     ret += PushRpcFunc("SetCountryCode", RpcSetCountryCode);
175     ret += PushRpcFunc("SetMacFilter", RpcSetMacFilter);
176     ret += PushRpcFunc("DelMacFilter", RpcDelMacFilter);
177     ret += PushRpcFunc("DisassociateSta", RpcDisassociateSta);
178     ret += PushRpcFunc("GetValidFrequenciesForBand", RpcGetValidFrequenciesForBand);
179     return ret;
180 }
181 
InitRpcFuncMapCommon(void)182 static int InitRpcFuncMapCommon(void)
183 {
184     int ret = 0;
185     ret += PushRpcFunc("RegisterEventCallback", RpcRegisterEventCallback);
186     ret += PushRpcFunc("UnRegisterEventCallback", RpcUnRegisterEventCallback);
187     ret += PushRpcFunc("NotifyClear", RpcNotifyClear);
188     return ret;
189 }
190 
InitRpcFuncMapP2p(void)191 static int InitRpcFuncMapP2p(void)
192 {
193     int ret = 0;
194     ret += PushRpcFunc("P2pStart", RpcP2pStart);
195     ret += PushRpcFunc("P2pStop", RpcP2pStop);
196     ret += PushRpcFunc("P2pSetRandomMac", RpcP2pSetRandomMac);
197     ret += PushRpcFunc("P2pSetDeviceName", RpcP2pSetDeviceName);
198     ret += PushRpcFunc("P2pSetSsidPostfixName", RpcP2pSetSsidPostfixName);
199     ret += PushRpcFunc("P2pSetWpsDeviceType", RpcP2pSetWpsDeviceType);
200     ret += PushRpcFunc("P2pSetWpsSecondaryDeviceType", RpcP2pSetWpsSecondaryDeviceType);
201     ret += PushRpcFunc("P2pSetWpsConfigMethods", RpcP2pSetWpsConfigMethods);
202     ret += PushRpcFunc("P2pGetDeviceAddress", RpcP2pGetDeviceAddress);
203     ret += PushRpcFunc("P2pFlush", RpcP2pFlush);
204     ret += PushRpcFunc("P2pFlushService", RpcP2pFlushService);
205     ret += PushRpcFunc("P2pSaveConfig", RpcP2pSaveConfig);
206     ret += PushRpcFunc("P2pSetupWpsPbc", RpcP2pSetupWpsPbc);
207     ret += PushRpcFunc("P2pSetupWpsPin", RpcP2pSetupWpsPin);
208     ret += PushRpcFunc("P2pRemoveNetwork", RpcP2pRemoveNetwork);
209     ret += PushRpcFunc("P2pListNetworks", RpcP2pListNetworks);
210     ret += PushRpcFunc("P2pSetGroupMaxIdle", RpcP2pSetGroupMaxIdle);
211     ret += PushRpcFunc("P2pSetPowerSave", RpcP2pSetPowerSave);
212     ret += PushRpcFunc("P2pSetWfdEnable", RpcP2pSetWfdEnable);
213     ret += PushRpcFunc("P2pSetWfdDeviceConfig", RpcP2pSetWfdDeviceConfig);
214     ret += PushRpcFunc("P2pStartFind", RpcP2pStartFind);
215     ret += PushRpcFunc("P2pStopFind", RpcP2pStopFind);
216     ret += PushRpcFunc("P2pSetExtListen", RpcP2pSetExtListen);
217     ret += PushRpcFunc("P2pSetListenChannel", RpcP2pSetListenChannel);
218     ret += PushRpcFunc("P2pConnect", RpcP2pConnect);
219     ret += PushRpcFunc("P2pCancelConnect", RpcP2pCancelConnect);
220     ret += PushRpcFunc("P2pProvisionDiscovery", RpcP2pProvisionDiscovery);
221     ret += PushRpcFunc("P2pAddGroup", RpcP2pAddGroup);
222     ret += PushRpcFunc("P2pRemoveGroup", RpcP2pRemoveGroup);
223     ret += PushRpcFunc("P2pInvite", RpcP2pInvite);
224     ret += PushRpcFunc("P2pReinvoke", RpcP2pReinvoke);
225     ret += PushRpcFunc("P2pGetGroupCapability", RpcP2pGetGroupCapability);
226     ret += PushRpcFunc("P2pAddService", RpcP2pAddService);
227     ret += PushRpcFunc("P2pRemoveService", RpcP2pRemoveService);
228     ret += PushRpcFunc("P2pReqServiceDiscovery", RpcP2pReqServiceDiscovery);
229     ret += PushRpcFunc("P2pCancelServiceDiscovery", RpcP2pCancelServiceDiscovery);
230     ret += PushRpcFunc("P2pSetMiracastType", RpcP2pSetMiracastType);
231     ret += PushRpcFunc("P2pRespServerDiscovery", RpcP2pRespServerDiscovery);
232     ret += PushRpcFunc("P2pSetServDiscExternal", RpcP2pSetServDiscExternal);
233     ret += PushRpcFunc("P2pSetPersistentReconnect", RpcP2pSetPersistentReconnect);
234     ret += PushRpcFunc("P2pGetPeer", RpcP2pGetPeer);
235     ret += PushRpcFunc("P2pGetFrequencies", RpcP2pGetFrequencies);
236     ret += PushRpcFunc("P2pSetGroupConfig", RpcP2pSetGroupConfig);
237     ret += PushRpcFunc("P2pGetGroupConfig", RpcP2pGetGroupConfig);
238     ret += PushRpcFunc("P2pAddNetwork", RpcP2pAddNetwork);
239     ret += PushRpcFunc("P2pHid2dConnect", RpcP2pHid2dConnect);
240     return ret;
241 }
242 
InitRpcFunc(void)243 int InitRpcFunc(void)
244 {
245     if (g_rpcFuncHandle != NULL) {
246         return 0;
247     }
248     g_rpcFuncHandle = (WifiHalRpcFunc *)calloc(RPC_FUNC_NUM, sizeof(WifiHalRpcFunc));
249     if (g_rpcFuncHandle == NULL) {
250         return -1;
251     }
252 
253     int ret = 0;
254     ret += InitRpcFuncMapBase();
255     ret += InitRpcFuncMapChip();
256     ret += InitRpcFuncMapSupplicant();
257     ret += InitRpcFuncMapSta();
258     ret += InitRpcFuncMapAp();
259     ret += InitRpcFuncMapCommon();
260     ret += InitRpcFuncMapP2p();
261     if (ret < 0) {
262         return -1;
263     }
264 
265     if (InitCallbackMsg() < 0) {
266         return -1;
267     }
268     return 0;
269 }
270 
ReleaseRpcFunc(void)271 void ReleaseRpcFunc(void)
272 {
273     for (int i = 0; i < RPC_FUNC_NUM; ++i) {
274         WifiHalRpcFunc *p = g_rpcFuncHandle[i].next;
275         while (p != NULL) {
276             WifiHalRpcFunc *q = p->next;
277             free(p);
278             p = q;
279         }
280     }
281     free(g_rpcFuncHandle);
282     g_rpcFuncHandle = NULL;
283     ReleaseCallbackMsg();
284     return;
285 }
286 
GetRpcFunc(const char * func)287 Rpcfunc GetRpcFunc(const char *func)
288 {
289     if (g_rpcFuncHandle == NULL || func == NULL) {
290         return NULL;
291     }
292     int pos = GetPos(func);
293     WifiHalRpcFunc *p = g_rpcFuncHandle + pos;
294     while (p && strcmp(p->funcname, func) != 0) {
295         p = p->next;
296     }
297     if (p == NULL) {
298         return NULL;
299     }
300     return p->func;
301 }
302 
303 /* Processing client requests */
OnTransact(RpcServer * server,Context * context)304 int OnTransact(RpcServer *server, Context *context)
305 {
306     if ((server == NULL) || (context == NULL)) {
307         return -1;
308     }
309 
310     char func[RPC_FUNCNAME_MAX_LEN] = {0};
311     int ret = ReadFunc(context, func, RPC_FUNCNAME_MAX_LEN);
312     if (ret < 0) {
313         return -1;
314     }
315     LOGI("run %{public}s", func);
316     Rpcfunc pFunc = GetRpcFunc(func);
317     if (pFunc == NULL) {
318         LOGD("unsupport function[%{public}s]", func);
319         WriteBegin(context, 0);
320         WriteInt(context, WIFI_HAL_FAILED);
321         WriteStr(context, "unsupport function");
322         WriteEnd(context);
323     } else {
324         ret = pFunc(server, context);
325         if (ret < 0) {
326             WriteBegin(context, 0);
327             WriteInt(context, WIFI_HAL_FAILED);
328             WriteStr(context, "server deal failed!");
329             WriteEnd(context);
330         }
331     }
332     return 0;
333 }
334 
335 /* Defines the bidirectional list of global callback event parameters. */
336 static WifiHalEventCallback *g_wifiHalEventCallback = NULL;
337 
InitCallbackMsg(void)338 int InitCallbackMsg(void)
339 {
340     if (g_wifiHalEventCallback != NULL) {
341         return 0;
342     }
343     g_wifiHalEventCallback = (WifiHalEventCallback *)calloc(1, sizeof(WifiHalEventCallback));
344     if (g_wifiHalEventCallback == NULL) {
345         return -1;
346     }
347     pthread_mutex_init(&g_wifiHalEventCallback->mutex, NULL);
348     for (int i = 0; i < WIFI_HAL_MAX_EVENT - WIFI_FAILURE_EVENT; ++i) {
349         g_wifiHalEventCallback->cbmsgs[i].pre = g_wifiHalEventCallback->cbmsgs + i;
350         g_wifiHalEventCallback->cbmsgs[i].next = g_wifiHalEventCallback->cbmsgs + i;
351     }
352     return 0;
353 }
354 
ReleaseCallbackMsg(void)355 void ReleaseCallbackMsg(void)
356 {
357     if (g_wifiHalEventCallback == NULL) {
358         return;
359     }
360     for (int i = 0; i < WIFI_HAL_MAX_EVENT - WIFI_FAILURE_EVENT; ++i) {
361         WifiHalEventCallbackMsg *head = g_wifiHalEventCallback->cbmsgs + i;
362         WifiHalEventCallbackMsg *p = head->next;
363         while (p != head) {
364             WifiHalEventCallbackMsg *q = p->next;
365             free(p);
366             p = q;
367         }
368     }
369     pthread_mutex_destroy(&g_wifiHalEventCallback->mutex);
370     free(g_wifiHalEventCallback);
371     g_wifiHalEventCallback = NULL;
372     return;
373 }
374 
PushBackCallbackMsg(int event,WifiHalEventCallbackMsg * msg)375 int PushBackCallbackMsg(int event, WifiHalEventCallbackMsg *msg)
376 {
377     if (g_wifiHalEventCallback == NULL || event >= WIFI_HAL_MAX_EVENT || event < WIFI_FAILURE_EVENT || msg == NULL) {
378         return -1;
379     }
380     int pos = event - WIFI_FAILURE_EVENT;
381     pthread_mutex_lock(&g_wifiHalEventCallback->mutex);
382     WifiHalEventCallbackMsg *head = g_wifiHalEventCallback->cbmsgs + pos;
383     if (head->next == head) { /* Empty Queue */
384         msg->pre = head;
385         head->next = msg;
386         msg->next = head;
387         head->pre = msg;
388     } else {
389         msg->pre = head->pre;
390         head->pre->next = msg;
391         msg->next = head;
392         head->pre = msg;
393     }
394     pthread_mutex_unlock(&g_wifiHalEventCallback->mutex);
395     return 0;
396 }
397 
PopBackCallbackMsg(int event)398 int PopBackCallbackMsg(int event)
399 {
400     if (g_wifiHalEventCallback == NULL || event >= WIFI_HAL_MAX_EVENT || event < WIFI_FAILURE_EVENT) {
401         return -1;
402     }
403     int pos = event - WIFI_FAILURE_EVENT;
404     pthread_mutex_lock(&g_wifiHalEventCallback->mutex);
405     WifiHalEventCallbackMsg *head = g_wifiHalEventCallback->cbmsgs + pos;
406     if (head->next != head) { /* The queue is not empty. */
407         WifiHalEventCallbackMsg *tail = head->pre;
408         head->pre = tail->pre;
409         tail->pre->next = head;
410     }
411     pthread_mutex_unlock(&g_wifiHalEventCallback->mutex);
412     return 0;
413 }
414 
FrontCallbackMsg(int event)415 WifiHalEventCallbackMsg *FrontCallbackMsg(int event)
416 {
417     if (g_wifiHalEventCallback == NULL || event >= WIFI_HAL_MAX_EVENT || event < WIFI_FAILURE_EVENT) {
418         return NULL;
419     }
420     int pos = event - WIFI_FAILURE_EVENT;
421     WifiHalEventCallbackMsg *head = g_wifiHalEventCallback->cbmsgs + pos;
422     if (head->next != head) { /* The queue is not empty. */
423         return head->next;
424     } else {
425         return NULL;
426     }
427 }
428 
PopFrontCallbackMsg(int event)429 int PopFrontCallbackMsg(int event)
430 {
431     if (g_wifiHalEventCallback == NULL || event >= WIFI_HAL_MAX_EVENT || event < WIFI_FAILURE_EVENT) {
432         return -1;
433     }
434     int pos = event - WIFI_FAILURE_EVENT;
435     pthread_mutex_lock(&g_wifiHalEventCallback->mutex);
436     WifiHalEventCallbackMsg *head = g_wifiHalEventCallback->cbmsgs + pos;
437     if (head->next != head) { /* The queue is not empty. */
438         WifiHalEventCallbackMsg *p = head->next;
439         head->next = p->next;
440         p->next->pre = head;
441         free(p);
442     }
443     pthread_mutex_unlock(&g_wifiHalEventCallback->mutex);
444     return 0;
445 }
446 
447 /* Processing callback messages */
DealCommonCbk(int event,Context * context)448 static void DealCommonCbk(int event, Context *context)
449 {
450     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
451     if (cbmsg != NULL) {
452         WriteInt(context, cbmsg->msg.scanStatus);
453     }
454     return;
455 }
456 
DealIfaceCbk(int event,Context * context)457 static void DealIfaceCbk(int event, Context *context)
458 {
459     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
460     if (cbmsg != NULL) {
461         WriteInt(context, cbmsg->msg.ifMsg.type);
462         WriteStr(context, cbmsg->msg.ifMsg.ifname);
463     }
464     return;
465 }
466 
DealConnectionChangedCbk(int event,Context * context)467 static void DealConnectionChangedCbk(int event, Context *context)
468 {
469     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
470     if (cbmsg != NULL) {
471         WriteInt(context, cbmsg->msg.connMsg.status);
472         WriteInt(context, cbmsg->msg.connMsg.networkId);
473         WriteStr(context, cbmsg->msg.connMsg.bssid);
474     }
475     return;
476 }
477 
DealConnectWpsResultCbk(int event,Context * context)478 static void DealConnectWpsResultCbk(int event, Context *context)
479 {
480     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
481     if (cbmsg != NULL) {
482         WriteInt(context, cbmsg->msg.connMsg.status);
483     }
484     return;
485 }
486 
DealStaApCallback(int event,Context * context)487 static void DealStaApCallback(int event, Context *context)
488 {
489     switch (event) {
490         case WIFI_ADD_IFACE_EVENT:
491         case WIFI_STA_JOIN_EVENT:
492         case WIFI_STA_LEAVE_EVENT:
493             DealIfaceCbk(event, context);
494             break;
495         case WIFI_SCAN_INFO_NOTIFY_EVENT:
496             DealCommonCbk(event, context);
497             break;
498         case WIFI_WPA_STATE_EVENT:
499         case WIFI_SSID_WRONG_KEY:
500         case WIFI_WPS_OVERLAP:
501         case WIFI_WPS_TIME_OUT:
502         case WIFI_CONNECTION_FULL_EVENT:
503         case WIFI_CONNECTION_REJECT_EVENT:
504             DealConnectWpsResultCbk(event, context);
505             break;
506         case WIFI_CONNECT_CHANGED_NOTIFY_EVENT:
507             DealConnectionChangedCbk(event, context);
508             break;
509         default:
510             break;
511     }
512     return;
513 }
514 
DealP2pDeviceFoundCbk(int event,Context * context)515 static void DealP2pDeviceFoundCbk(int event, Context *context)
516 {
517     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
518     if (cbmsg != NULL) {
519         WriteInt(context, cbmsg->msg.deviceInfo.configMethods);
520         WriteInt(context, cbmsg->msg.deviceInfo.deviceCapabilities);
521         WriteInt(context, cbmsg->msg.deviceInfo.groupCapabilities);
522         WriteInt(context, cbmsg->msg.deviceInfo.wfdLength);
523         WriteStr(context, cbmsg->msg.deviceInfo.srcAddress);
524         WriteStr(context, cbmsg->msg.deviceInfo.p2pDeviceAddress);
525         WriteStr(context, cbmsg->msg.deviceInfo.primaryDeviceType);
526         WriteStr(context, cbmsg->msg.deviceInfo.deviceName);
527         WriteStr(context, cbmsg->msg.deviceInfo.wfdDeviceInfo);
528     }
529     return;
530 }
531 
DealP2pNegoriationCbk(int event,Context * context)532 static void DealP2pNegoriationCbk(int event, Context *context)
533 {
534     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
535     if (cbmsg != NULL) {
536         if (event == P2P_DEVICE_LOST_EVENT) {
537             WriteStr(context, cbmsg->msg.connMsg.bssid);
538         }
539         if (event == P2P_GO_NEGOTIATION_REQUEST_EVENT) {
540             WriteInt(context, cbmsg->msg.connMsg.status);
541             WriteStr(context, cbmsg->msg.connMsg.bssid);
542         }
543     }
544     return;
545 }
546 
DealP2pInviationCbk(int event,Context * context)547 static void DealP2pInviationCbk(int event, Context *context)
548 {
549     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
550     if (cbmsg != NULL) {
551         if (event == P2P_INVITATION_RECEIVED_EVENT) {
552             WriteInt(context, cbmsg->msg.invitaInfo.type);
553             WriteInt(context, cbmsg->msg.invitaInfo.persistentNetworkId);
554             WriteInt(context, cbmsg->msg.invitaInfo.operatingFrequency);
555             WriteStr(context, cbmsg->msg.invitaInfo.srcAddress);
556             WriteStr(context, cbmsg->msg.invitaInfo.goDeviceAddress);
557             WriteStr(context, cbmsg->msg.invitaInfo.bssid);
558         }
559         if (event == P2P_INVITATION_RESULT_EVENT) {
560             WriteInt(context, cbmsg->msg.invitaInfo.persistentNetworkId);
561             WriteStr(context, cbmsg->msg.invitaInfo.bssid);
562         }
563         if (event == P2P_GROUP_FORMATION_FAILURE_EVENT) {
564             WriteStr(context, cbmsg->msg.invitaInfo.bssid);
565         }
566     }
567     return;
568 }
569 
DealP2pGroupInfoCbk(int event,Context * context)570 static void DealP2pGroupInfoCbk(int event, Context *context)
571 {
572     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
573     if (cbmsg != NULL) {
574         if (event == P2P_GROUP_STARTED_EVENT) {
575             WriteInt(context, cbmsg->msg.groupInfo.isGo);
576             WriteInt(context, cbmsg->msg.groupInfo.isPersistent);
577             WriteInt(context, cbmsg->msg.groupInfo.frequency);
578             WriteStr(context, cbmsg->msg.groupInfo.groupIfName);
579             WriteStr(context, cbmsg->msg.groupInfo.ssid);
580             WriteStr(context, cbmsg->msg.groupInfo.psk);
581             WriteStr(context, cbmsg->msg.groupInfo.passphrase);
582             WriteStr(context, cbmsg->msg.groupInfo.goDeviceAddress);
583         }
584         if (event == P2P_GROUP_REMOVED_EVENT) {
585             WriteInt(context, cbmsg->msg.groupInfo.isGo);
586             WriteStr(context, cbmsg->msg.groupInfo.groupIfName);
587         }
588     }
589     return;
590 }
591 
DealP2pDeviceInfoCbk(int event,Context * context)592 static void DealP2pDeviceInfoCbk(int event, Context *context)
593 {
594     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
595     if (cbmsg != NULL) {
596         if (event == P2P_PROV_DISC_PBC_REQ_EVENT || event == P2P_PROV_DISC_PBC_RSP_EVENT ||
597             event == P2P_PROV_DISC_ENTER_PIN_EVENT) {
598             WriteStr(context, cbmsg->msg.deviceInfo.srcAddress);
599         }
600         if (event == P2P_PROV_DISC_SHOW_PIN_EVENT) {
601             WriteStr(context, cbmsg->msg.deviceInfo.srcAddress);
602             WriteStr(context, cbmsg->msg.deviceInfo.deviceName);
603         }
604         if (event == AP_STA_DISCONNECTED_EVENT || event == AP_STA_CONNECTED_EVENT) {
605             WriteStr(context, cbmsg->msg.deviceInfo.p2pDeviceAddress);
606         }
607     }
608     return;
609 }
610 
DealP2pServerInfoCbk(int event,Context * context)611 static void DealP2pServerInfoCbk(int event, Context *context)
612 {
613     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
614     if (cbmsg != NULL) {
615         WriteInt(context, cbmsg->msg.serverInfo.updateIndicator);
616         WriteStr(context, cbmsg->msg.serverInfo.srcAddress);
617         if (cbmsg->msg.serverInfo.tlvs != NULL) {
618             WriteInt(context, strlen(cbmsg->msg.serverInfo.tlvs));
619             WriteStr(context, cbmsg->msg.serverInfo.tlvs);
620             free(cbmsg->msg.serverInfo.tlvs);
621         } else {
622             WriteInt(context, 0);
623         }
624     }
625     return;
626 }
627 
DealP2pServerDiscReqCbk(int event,Context * context)628 static void DealP2pServerDiscReqCbk(int event, Context *context)
629 {
630     WifiHalEventCallbackMsg *cbmsg = FrontCallbackMsg(event);
631     if (cbmsg != NULL) {
632         WriteInt(context, cbmsg->msg.serDiscReqInfo.freq);
633         WriteInt(context, cbmsg->msg.serDiscReqInfo.dialogToken);
634         WriteInt(context, cbmsg->msg.serDiscReqInfo.updateIndic);
635         WriteStr(context, cbmsg->msg.serDiscReqInfo.mac);
636         if (cbmsg->msg.serDiscReqInfo.tlvs != NULL) {
637             WriteInt(context, strlen(cbmsg->msg.serDiscReqInfo.tlvs));
638             WriteStr(context, cbmsg->msg.serDiscReqInfo.tlvs);
639             free(cbmsg->msg.serDiscReqInfo.tlvs);
640         } else {
641             WriteInt(context, 0);
642         }
643     }
644     return;
645 }
646 
DealP2pCallback(int event,Context * context)647 static void DealP2pCallback(int event, Context *context)
648 {
649     switch (event) {
650         case WIFI_P2P_SUP_CONNECTION_EVENT:
651         case P2P_GO_NEGOTIATION_FAILURE_EVENT:
652             DealCommonCbk(event, context);
653             break;
654         case P2P_DEVICE_FOUND_EVENT:
655             DealP2pDeviceFoundCbk(event, context);
656             break;
657         case P2P_DEVICE_LOST_EVENT:
658         case P2P_GO_NEGOTIATION_REQUEST_EVENT:
659             DealP2pNegoriationCbk(event, context);
660             break;
661         case P2P_INVITATION_RECEIVED_EVENT:
662         case P2P_INVITATION_RESULT_EVENT:
663         case P2P_GROUP_FORMATION_FAILURE_EVENT:
664             DealP2pInviationCbk(event, context);
665             break;
666         case P2P_GROUP_STARTED_EVENT:
667         case P2P_GROUP_REMOVED_EVENT:
668             DealP2pGroupInfoCbk(event, context);
669             break;
670         case P2P_PROV_DISC_PBC_REQ_EVENT:
671         case P2P_PROV_DISC_PBC_RSP_EVENT:
672         case P2P_PROV_DISC_ENTER_PIN_EVENT:
673         case P2P_PROV_DISC_SHOW_PIN_EVENT:
674         case AP_STA_DISCONNECTED_EVENT:
675         case AP_STA_CONNECTED_EVENT:
676             DealP2pDeviceInfoCbk(event, context);
677             break;
678         case P2P_SERV_DISC_RESP_EVENT:
679             DealP2pServerInfoCbk(event, context);
680             break;
681         case P2P_SERV_DISC_REQ_EVENT:
682             DealP2pServerDiscReqCbk(event, context);
683             break;
684         default:
685             break;
686     }
687     return;
688 }
689 
690 /* Callback request */
OnCallbackTransact(const RpcServer * server,int event,Context * context)691 int OnCallbackTransact(const RpcServer *server, int event, Context *context)
692 {
693     if (server == NULL || context == NULL) {
694         return -1;
695     }
696     WriteBegin(context, 1);
697     WriteInt(context, event);
698     DealStaApCallback(event, context);
699     DealP2pCallback(event, context);
700     WriteEnd(context);
701     return 0;
702 }
703 
EndCallbackTransact(const RpcServer * server,int event)704 int EndCallbackTransact(const RpcServer *server, int event)
705 {
706     if (server == NULL) {
707         return -1;
708     }
709     return PopFrontCallbackMsg(event);
710 }