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 "i_wifi_chip.h"
17 #include <stddef.h>
18 #include "client.h"
19 #include "context.h"
20 #include "i_wifi_public_func.h"
21 #include "serial.h"
22 #include "wifi_idl_inner_interface.h"
23 #include "wifi_log.h"
24 #include "wifi_native_define.h"
25 
26 #undef LOG_TAG
27 #define LOG_TAG "WifiIdlWifiChip"
28 
29 /* Defines the global wifichipeventcallback variable. */
30 static IWifiChipEventCallback g_wifiChipEventCallback = {0};
SetWifiChipEventCallback(IWifiChipEventCallback callback)31 void SetWifiChipEventCallback(IWifiChipEventCallback callback)
32 {
33     g_wifiChipEventCallback = callback;
34 }
35 
GetWifiChipEventCallback(void)36 IWifiChipEventCallback *GetWifiChipEventCallback(void)
37 {
38     return &g_wifiChipEventCallback;
39 }
40 
GetChipId(int32_t * id)41 WifiErrorNo GetChipId(int32_t *id)
42 {
43     RpcClient *client = GetChipRpcClient();
44     LockRpcClient(client);
45     Context *context = client->context;
46     WriteBegin(context, 0);
47     WriteFunc(context, "GetChipId");
48     WriteEnd(context);
49     if (RpcClientCall(client, "GetChipId") != WIFI_HAL_OPT_OK) {
50         return WIFI_HAL_OPT_FAILED;
51     }
52     int result = WIFI_HAL_OPT_FAILED;
53     ReadInt(context, &result);
54     if (result != WIFI_HAL_OPT_OK) {
55         LOGE("server GetChipId deal failed!");
56     } else {
57         ReadInt(context, id);
58     }
59     ReadClientEnd(client);
60     UnlockRpcClient(client);
61     return result;
62 }
63 
CreateIface(int32_t type,IWifiIface * iface)64 WifiErrorNo CreateIface(int32_t type, IWifiIface *iface)
65 {
66     RpcClient *client = GetChipRpcClient();
67     LockRpcClient(client);
68     Context *context = client->context;
69     WriteBegin(context, 0);
70     WriteFunc(context, "CreateIface");
71     WriteInt(context, type);
72     WriteEnd(context);
73     if (RpcClientCall(client, "CreateIface") != WIFI_HAL_OPT_OK) {
74         return WIFI_HAL_OPT_FAILED;
75     }
76     int result = WIFI_HAL_OPT_FAILED;
77     ReadInt(context, &result);
78     if (result != WIFI_HAL_OPT_OK) {
79         LOGE("server CreateIface deal failed!");
80     } else {
81         /* read IWifiIface struct */
82         ReadInt(context, &(iface->index));
83         ReadInt(context, &(iface->type));
84         ReadStr(context, iface->name, sizeof(iface->name));
85         ReadStr(context, iface->macAddr, sizeof(iface->macAddr));
86     }
87     ReadClientEnd(client);
88     UnlockRpcClient(client);
89     return result;
90 }
91 
GetIface(const char * ifname,IWifiIface * iface)92 WifiErrorNo GetIface(const char *ifname, IWifiIface *iface)
93 {
94     RpcClient *client = GetChipRpcClient();
95     LockRpcClient(client);
96     Context *context = client->context;
97     WriteBegin(context, 0);
98     WriteFunc(context, "GetIface");
99     WriteStr(context, ifname);
100     WriteEnd(context);
101     if (RpcClientCall(client, "GetIface") != WIFI_HAL_OPT_OK) {
102         return WIFI_HAL_OPT_FAILED;
103     }
104     int result = WIFI_HAL_OPT_FAILED;
105     ReadInt(context, &result);
106     if (result != WIFI_HAL_OPT_OK) {
107         LOGE("server GetIface deal failed!");
108     } else {
109         /*  read IWifiIface struct */
110         ReadInt(context, &(iface->index));
111         ReadInt(context, &(iface->type));
112         ReadStr(context, iface->name, sizeof(iface->name));
113         ReadStr(context, iface->macAddr, sizeof(iface->macAddr));
114     }
115     ReadClientEnd(client);
116     UnlockRpcClient(client);
117     return result;
118 }
119 
GetIfaceNames(int32_t type,char * ifaces,int32_t size)120 WifiErrorNo GetIfaceNames(int32_t type, char *ifaces, int32_t size)
121 {
122     RpcClient *client = GetChipRpcClient();
123     LockRpcClient(client);
124     Context *context = client->context;
125     WriteBegin(context, 0);
126     WriteFunc(context, "GetIfaceNames");
127     WriteInt(context, type);
128     WriteInt(context, size);
129     WriteEnd(context);
130     if (RpcClientCall(client, "GetIfaceNames") != WIFI_HAL_OPT_OK) {
131         return WIFI_HAL_OPT_FAILED;
132     }
133     int result = WIFI_HAL_OPT_FAILED;
134     ReadInt(context, &result);
135     if (result != WIFI_HAL_OPT_OK) {
136         LOGE("server GetIfaceNames deal failed!");
137     } else {
138         ReadStr(context, ifaces, size);
139     }
140     ReadClientEnd(client);
141     UnlockRpcClient(client);
142     return result;
143 }
144 
RemoveIface(const char * ifname)145 WifiErrorNo RemoveIface(const char *ifname)
146 {
147     RpcClient *client = GetChipRpcClient();
148     LockRpcClient(client);
149     Context *context = client->context;
150     WriteBegin(context, 0);
151     WriteFunc(context, "RemoveIface");
152     WriteStr(context, ifname);
153     WriteEnd(context);
154     if (RpcClientCall(client, "RemoveIface") != WIFI_HAL_OPT_OK) {
155         return WIFI_HAL_OPT_FAILED;
156     }
157     int result = WIFI_HAL_OPT_FAILED;
158     ReadInt(context, &result);
159     ReadClientEnd(client);
160     UnlockRpcClient(client);
161     return result;
162 }
163 
GetCapabilities(uint32_t * capabilities)164 WifiErrorNo GetCapabilities(uint32_t *capabilities)
165 {
166     RpcClient *client = GetChipRpcClient();
167     LockRpcClient(client);
168     Context *context = client->context;
169     WriteBegin(context, 0);
170     WriteFunc(context, "GetCapabilities");
171     WriteEnd(context);
172     if (RpcClientCall(client, "GetCapabilities") != WIFI_HAL_OPT_OK) {
173         return WIFI_HAL_OPT_FAILED;
174     }
175     int result = WIFI_HAL_OPT_FAILED;
176     ReadInt(context, &result);
177     if (result != WIFI_HAL_OPT_OK) {
178         LOGE("server GetCapabilities deal failed!");
179     } else {
180         ReadInt(context, (int *)capabilities);
181     }
182     ReadClientEnd(client);
183     UnlockRpcClient(client);
184     return result;
185 }
186 
GetSupportedComboModes(int32_t * modes,int32_t * size)187 WifiErrorNo GetSupportedComboModes(int32_t *modes, int32_t *size)
188 {
189     RpcClient *client = GetChipRpcClient();
190     LockRpcClient(client);
191     Context *context = client->context;
192     WriteBegin(context, 0);
193     WriteFunc(context, "GetSupportedComboModes");
194     WriteInt(context, *size);
195     WriteEnd(context);
196     if (RpcClientCall(client, "GetSupportedComboModes") != WIFI_HAL_OPT_OK) {
197         return WIFI_HAL_OPT_FAILED;
198     }
199     int result = WIFI_HAL_OPT_FAILED;
200     ReadInt(context, &result);
201     if (result != WIFI_HAL_OPT_OK) {
202         LOGE("server GetSupportedComboModes deal failed!");
203     } else {
204         ReadInt(context, size);
205         if (*size > WIFI_MAX_CHIP_IDS) {
206             LOGE("GetSupportedComboModes fail, size error: %{public}d", *size);
207             return WIFI_HAL_OPT_FAILED;
208         }
209         for (int i = 0; i < *size; ++i) {
210             ReadInt(context, modes + i);
211         }
212     }
213     ReadClientEnd(client);
214     UnlockRpcClient(client);
215     return result;
216 }
217 
ConfigComboModes(int32_t mode)218 WifiErrorNo ConfigComboModes(int32_t mode)
219 {
220     RpcClient *client = GetChipRpcClient();
221     LockRpcClient(client);
222     Context *context = client->context;
223     WriteBegin(context, 0);
224     WriteFunc(context, "ConfigComboModes");
225     WriteInt(context, mode);
226     WriteEnd(context);
227     if (RpcClientCall(client, "ConfigComboModes") != WIFI_HAL_OPT_OK) {
228         return WIFI_HAL_OPT_FAILED;
229     }
230     int result = WIFI_HAL_OPT_FAILED;
231     ReadInt(context, &result);
232     ReadClientEnd(client);
233     UnlockRpcClient(client);
234     return result;
235 }
236 
GetComboModes(int32_t * id)237 WifiErrorNo GetComboModes(int32_t *id)
238 {
239     RpcClient *client = GetChipRpcClient();
240     LockRpcClient(client);
241     Context *context = client->context;
242     WriteBegin(context, 0);
243     WriteFunc(context, "GetComboModes");
244     WriteEnd(context);
245     if (RpcClientCall(client, "GetComboModes") != WIFI_HAL_OPT_OK) {
246         return WIFI_HAL_OPT_FAILED;
247     }
248     int result = WIFI_HAL_OPT_FAILED;
249     ReadInt(context, &result);
250     if (result != WIFI_HAL_OPT_OK) {
251         LOGE("server GetComboModes deal failed!");
252     } else {
253         ReadInt(context, id);
254     }
255     ReadClientEnd(client);
256     UnlockRpcClient(client);
257     return result;
258 }
259 
RegisterEventCallback(IWifiChipEventCallback callback)260 WifiErrorNo RegisterEventCallback(IWifiChipEventCallback callback)
261 {
262     int num = 0;
263     if (callback.onIfaceAdded != NULL) {
264         ++num;
265     }
266     if (callback.onIfaceRemoved != NULL) {
267         ++num;
268     }
269     RpcClient *client = GetChipRpcClient();
270     LockRpcClient(client);
271     Context *context = client->context;
272     WriteBegin(context, 0);
273     if (num == 0) {
274         WriteFunc(context, "UnRegisterEventCallback");
275         WriteInt(context, EVENTS_IFACE_ADD_DEL_NUM);
276         WriteInt(context, HAL_CBK_CMD_ADD_IFACE);
277         WriteInt(context, HAL_CBK_CMD_REMOVE_IFACE);
278     } else {
279         WriteFunc(context, "RegisterEventCallback");
280         WriteInt(context, num);
281         if (callback.onIfaceAdded != NULL) {
282             WriteInt(context, HAL_CBK_CMD_ADD_IFACE);
283         }
284         if (callback.onIfaceRemoved != NULL) {
285             WriteInt(context, HAL_CBK_CMD_REMOVE_IFACE);
286         }
287     }
288     WriteEnd(context);
289     if (RpcClientCall(client, "RegisterEventCallback") != WIFI_HAL_OPT_OK) {
290         if (num == 0) {
291             SetWifiChipEventCallback(callback);
292         }
293         return WIFI_HAL_OPT_FAILED;
294     }
295     int result = WIFI_HAL_OPT_FAILED;
296     ReadInt(context, &result);
297     if (result == WIFI_HAL_OPT_OK || num == 0) {
298         SetWifiChipEventCallback(callback);
299     }
300     ReadClientEnd(client);
301     UnlockRpcClient(client);
302     return result;
303 }
304 
RequestFirmwareDebugDump(unsigned char * bytes,int32_t * size)305 WifiErrorNo RequestFirmwareDebugDump(unsigned char *bytes, int32_t *size)
306 {
307     RpcClient *client = GetChipRpcClient();
308     LockRpcClient(client);
309     Context *context = client->context;
310     WriteBegin(context, 0);
311     WriteFunc(context, "RequestFirmwareDebugDump");
312     WriteInt(context, *size);
313     WriteEnd(context);
314     if (RpcClientCall(client, "RequestFirmwareDebugDump") != WIFI_HAL_OPT_OK) {
315         return WIFI_HAL_OPT_FAILED;
316     }
317     int result = WIFI_HAL_OPT_FAILED;
318     ReadInt(context, &result);
319     if (result != WIFI_HAL_OPT_OK) {
320         LOGE("server RequestFirmwareDebugDump deal failed!");
321     } else {
322         ReadInt(context, size);
323         ReadUStr(context, bytes, *size + 1);
324     }
325     ReadClientEnd(client);
326     UnlockRpcClient(client);
327     return result;
328 }
329 
IsChipSupportDbdc(bool * isSupport)330 WifiErrorNo IsChipSupportDbdc(bool *isSupport)
331 {
332     RpcClient *client = GetChipRpcClient();
333     LockRpcClient(client);
334     Context *context = client->context;
335     WriteBegin(context, 0);
336     WriteFunc(context, "IsChipSupportDbdc");
337     WriteEnd(context);
338     if (RpcClientCall(client, "IsChipSupportDbdc") != WIFI_HAL_OPT_OK) {
339         return false;
340     }
341     int result = WIFI_HAL_OPT_FAILED;
342     ReadInt(context, &result);
343     int retValue = WIFI_IDL_FALSE;
344     if (result == WIFI_HAL_OPT_OK) {
345         ReadInt(context, &retValue);
346         *isSupport = (retValue == WIFI_IDL_TRUE);
347     }
348     ReadClientEnd(client);
349     UnlockRpcClient(client);
350     return result;
351 }
352 
IsChipSupportCsa(bool * isSupport)353 WifiErrorNo IsChipSupportCsa(bool *isSupport)
354 {
355     RpcClient *client = GetChipRpcClient();
356     LockRpcClient(client);
357     Context *context = client->context;
358     WriteBegin(context, 0);
359     WriteFunc(context, "IsChipSupportCsa");
360     WriteEnd(context);
361     if (RpcClientCall(client, "IsChipSupportCsa") != WIFI_HAL_OPT_OK) {
362         return false;
363     }
364     int result = WIFI_HAL_OPT_FAILED;
365     ReadInt(context, &result);
366     int retValue = WIFI_IDL_FALSE;
367     if (result == WIFI_HAL_OPT_OK) {
368         ReadInt(context, &retValue);
369         *isSupport = (retValue == WIFI_IDL_TRUE);
370     }
371     ReadClientEnd(client);
372     UnlockRpcClient(client);
373     return result;
374 }
375 
IsChipSupportRadarDetect(bool * isSupport)376 WifiErrorNo IsChipSupportRadarDetect(bool *isSupport)
377 {
378     RpcClient *client = GetChipRpcClient();
379     LockRpcClient(client);
380     Context *context = client->context;
381     WriteBegin(context, 0);
382     WriteFunc(context, "IsChipSupportRadarDetect");
383     WriteEnd(context);
384     if (RpcClientCall(client, "IsChipSupportRadarDetect") != WIFI_HAL_OPT_OK) {
385         return false;
386     }
387     int result = WIFI_HAL_OPT_FAILED;
388     ReadInt(context, &result);
389     int retValue = WIFI_IDL_FALSE;
390     if (result == WIFI_HAL_OPT_OK) {
391         ReadInt(context, &retValue);
392         *isSupport = (retValue == WIFI_IDL_TRUE);
393     }
394     ReadClientEnd(client);
395     UnlockRpcClient(client);
396     return result;
397 }
398 
IsChipSupportDfsChannel(bool * isSupport)399 WifiErrorNo IsChipSupportDfsChannel(bool *isSupport)
400 {
401     RpcClient *client = GetChipRpcClient();
402     LockRpcClient(client);
403     Context *context = client->context;
404     WriteBegin(context, 0);
405     WriteFunc(context, "IsChipSupportDfsChannel");
406     WriteEnd(context);
407     if (RpcClientCall(client, "IsChipSupportDfsChannel") != WIFI_HAL_OPT_OK) {
408         return false;
409     }
410     int result = WIFI_HAL_OPT_FAILED;
411     ReadInt(context, &result);
412     int retValue = WIFI_IDL_FALSE;
413     if (result == WIFI_HAL_OPT_OK) {
414         ReadInt(context, &retValue);
415         *isSupport = (retValue == WIFI_IDL_TRUE);
416     }
417     ReadClientEnd(client);
418     UnlockRpcClient(client);
419     return result;
420 }
421 
IsChipSupportIndoorChannel(bool * isSupport)422 WifiErrorNo IsChipSupportIndoorChannel(bool *isSupport)
423 {
424     RpcClient *client = GetChipRpcClient();
425     LockRpcClient(client);
426     Context *context = client->context;
427     WriteBegin(context, 0);
428     WriteFunc(context, "IsChipSupportIndoorChannel");
429     WriteEnd(context);
430     if (RpcClientCall(client, "IsChipSupportIndoorChannel") != WIFI_HAL_OPT_OK) {
431         return false;
432     }
433     int result = WIFI_HAL_OPT_FAILED;
434     ReadInt(context, &result);
435     int retValue = WIFI_IDL_FALSE;
436     if (result == WIFI_HAL_OPT_OK) {
437         ReadInt(context, &retValue);
438         *isSupport = (retValue == WIFI_IDL_TRUE);
439     }
440     ReadClientEnd(client);
441     UnlockRpcClient(client);
442     return result;
443 }
444