• 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 "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_define.h"
23 #include "wifi_idl_inner_interface.h"
24 #include "wifi_log.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_IDL_OPT_OK) {
50         return WIFI_IDL_OPT_FAILED;
51     }
52     int result = WIFI_IDL_OPT_FAILED;
53     ReadInt(context, &result);
54     if (result != WIFI_IDL_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_IDL_OPT_OK) {
74         return WIFI_IDL_OPT_FAILED;
75     }
76     int result = WIFI_IDL_OPT_FAILED;
77     ReadInt(context, &result);
78     if (result != WIFI_IDL_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_IDL_OPT_OK) {
102         return WIFI_IDL_OPT_FAILED;
103     }
104     int result = WIFI_IDL_OPT_FAILED;
105     ReadInt(context, &result);
106     if (result != WIFI_IDL_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_IDL_OPT_OK) {
131         return WIFI_IDL_OPT_FAILED;
132     }
133     int result = WIFI_IDL_OPT_FAILED;
134     ReadInt(context, &result);
135     if (result != WIFI_IDL_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_IDL_OPT_OK) {
155         return WIFI_IDL_OPT_FAILED;
156     }
157     int result = WIFI_IDL_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_IDL_OPT_OK) {
173         return WIFI_IDL_OPT_FAILED;
174     }
175     int result = WIFI_IDL_OPT_FAILED;
176     ReadInt(context, &result);
177     if (result != WIFI_IDL_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_IDL_OPT_OK) {
197         return WIFI_IDL_OPT_FAILED;
198     }
199     int result = WIFI_IDL_OPT_FAILED;
200     ReadInt(context, &result);
201     if (result != WIFI_IDL_OPT_OK) {
202         LOGE("server GetSupportedComboModes deal failed!");
203     } else {
204         ReadInt(context, size);
205         for (int i = 0; i < *size; ++i) {
206             ReadInt(context, modes + i);
207         }
208     }
209     ReadClientEnd(client);
210     UnlockRpcClient(client);
211     return result;
212 }
213 
ConfigComboModes(int32_t mode)214 WifiErrorNo ConfigComboModes(int32_t mode)
215 {
216     RpcClient *client = GetChipRpcClient();
217     LockRpcClient(client);
218     Context *context = client->context;
219     WriteBegin(context, 0);
220     WriteFunc(context, "ConfigComboModes");
221     WriteInt(context, mode);
222     WriteEnd(context);
223     if (RpcClientCall(client, "ConfigComboModes") != WIFI_IDL_OPT_OK) {
224         return WIFI_IDL_OPT_FAILED;
225     }
226     int result = WIFI_IDL_OPT_FAILED;
227     ReadInt(context, &result);
228     ReadClientEnd(client);
229     UnlockRpcClient(client);
230     return result;
231 }
232 
GetComboModes(int32_t * id)233 WifiErrorNo GetComboModes(int32_t *id)
234 {
235     RpcClient *client = GetChipRpcClient();
236     LockRpcClient(client);
237     Context *context = client->context;
238     WriteBegin(context, 0);
239     WriteFunc(context, "GetComboModes");
240     WriteEnd(context);
241     if (RpcClientCall(client, "GetComboModes") != WIFI_IDL_OPT_OK) {
242         return WIFI_IDL_OPT_FAILED;
243     }
244     int result = WIFI_IDL_OPT_FAILED;
245     ReadInt(context, &result);
246     if (result != WIFI_IDL_OPT_OK) {
247         LOGE("server GetComboModes deal failed!");
248     } else {
249         ReadInt(context, id);
250     }
251     ReadClientEnd(client);
252     UnlockRpcClient(client);
253     return result;
254 }
255 
RegisterEventCallback(IWifiChipEventCallback callback)256 WifiErrorNo RegisterEventCallback(IWifiChipEventCallback callback)
257 {
258     int num = 0;
259     if (callback.onIfaceAdded != NULL) {
260         ++num;
261     }
262     if (callback.onIfaceRemoved != NULL) {
263         ++num;
264     }
265     RpcClient *client = GetChipRpcClient();
266     LockRpcClient(client);
267     Context *context = client->context;
268     WriteBegin(context, 0);
269     if (num == 0) {
270         WriteFunc(context, "UnRegisterEventCallback");
271         WriteInt(context, EVENTS_IFACE_ADD_DEL_NUM);
272         WriteInt(context, WIFI_IDL_CBK_CMD_ADD_IFACE);
273         WriteInt(context, WIFI_IDL_CBK_CMD_REMOVE_IFACE);
274     } else {
275         WriteFunc(context, "RegisterEventCallback");
276         WriteInt(context, num);
277         if (callback.onIfaceAdded != NULL) {
278             WriteInt(context, WIFI_IDL_CBK_CMD_ADD_IFACE);
279         }
280         if (callback.onIfaceRemoved != NULL) {
281             WriteInt(context, WIFI_IDL_CBK_CMD_REMOVE_IFACE);
282         }
283     }
284     WriteEnd(context);
285     if (RpcClientCall(client, "RegisterEventCallback") != WIFI_IDL_OPT_OK) {
286         if (num == 0) {
287             SetWifiChipEventCallback(callback);
288         }
289         return WIFI_IDL_OPT_FAILED;
290     }
291     int result = WIFI_IDL_OPT_FAILED;
292     ReadInt(context, &result);
293     if (result == WIFI_IDL_OPT_OK || num == 0) {
294         SetWifiChipEventCallback(callback);
295     }
296     ReadClientEnd(client);
297     UnlockRpcClient(client);
298     return result;
299 }
300 
RequestFirmwareDebugDump(unsigned char * bytes,int32_t * size)301 WifiErrorNo RequestFirmwareDebugDump(unsigned char *bytes, int32_t *size)
302 {
303     RpcClient *client = GetChipRpcClient();
304     LockRpcClient(client);
305     Context *context = client->context;
306     WriteBegin(context, 0);
307     WriteFunc(context, "RequestFirmwareDebugDump");
308     WriteInt(context, *size);
309     WriteEnd(context);
310     if (RpcClientCall(client, "RequestFirmwareDebugDump") != WIFI_IDL_OPT_OK) {
311         return WIFI_IDL_OPT_FAILED;
312     }
313     int result = WIFI_IDL_OPT_FAILED;
314     ReadInt(context, &result);
315     if (result != WIFI_IDL_OPT_OK) {
316         LOGE("server RequestFirmwareDebugDump deal failed!");
317     } else {
318         ReadInt(context, size);
319         ReadUStr(context, bytes, *size + 1);
320     }
321     ReadClientEnd(client);
322     UnlockRpcClient(client);
323     return result;
324 }
325 
IsChipSupportDbdc(bool * isSupport)326 WifiErrorNo IsChipSupportDbdc(bool *isSupport)
327 {
328     RpcClient *client = GetChipRpcClient();
329     LockRpcClient(client);
330     Context *context = client->context;
331     WriteBegin(context, 0);
332     WriteFunc(context, "IsChipSupportDbdc");
333     WriteEnd(context);
334     if (RpcClientCall(client, "IsChipSupportDbdc") != WIFI_IDL_OPT_OK) {
335         return false;
336     }
337     int result = WIFI_IDL_OPT_FAILED;
338     ReadInt(context, &result);
339     int retValue = WIFI_IDL_FALSE;
340     if (result == WIFI_IDL_OPT_OK) {
341         ReadInt(context, &retValue);
342         *isSupport = (retValue == WIFI_IDL_TRUE);
343     }
344     ReadClientEnd(client);
345     UnlockRpcClient(client);
346     return result;
347 }
348 
IsChipSupportCsa(bool * isSupport)349 WifiErrorNo IsChipSupportCsa(bool *isSupport)
350 {
351     RpcClient *client = GetChipRpcClient();
352     LockRpcClient(client);
353     Context *context = client->context;
354     WriteBegin(context, 0);
355     WriteFunc(context, "IsChipSupportCsa");
356     WriteEnd(context);
357     if (RpcClientCall(client, "IsChipSupportCsa") != WIFI_IDL_OPT_OK) {
358         return false;
359     }
360     int result = WIFI_IDL_OPT_FAILED;
361     ReadInt(context, &result);
362     int retValue = WIFI_IDL_FALSE;
363     if (result == WIFI_IDL_OPT_OK) {
364         ReadInt(context, &retValue);
365         *isSupport = (retValue == WIFI_IDL_TRUE);
366     }
367     ReadClientEnd(client);
368     UnlockRpcClient(client);
369     return result;
370 }
371 
IsChipSupportRadarDetect(bool * isSupport)372 WifiErrorNo IsChipSupportRadarDetect(bool *isSupport)
373 {
374     RpcClient *client = GetChipRpcClient();
375     LockRpcClient(client);
376     Context *context = client->context;
377     WriteBegin(context, 0);
378     WriteFunc(context, "IsChipSupportRadarDetect");
379     WriteEnd(context);
380     if (RpcClientCall(client, "IsChipSupportRadarDetect") != WIFI_IDL_OPT_OK) {
381         return false;
382     }
383     int result = WIFI_IDL_OPT_FAILED;
384     ReadInt(context, &result);
385     int retValue = WIFI_IDL_FALSE;
386     if (result == WIFI_IDL_OPT_OK) {
387         ReadInt(context, &retValue);
388         *isSupport = (retValue == WIFI_IDL_TRUE);
389     }
390     ReadClientEnd(client);
391     UnlockRpcClient(client);
392     return result;
393 }
394 
IsChipSupportDfsChannel(bool * isSupport)395 WifiErrorNo IsChipSupportDfsChannel(bool *isSupport)
396 {
397     RpcClient *client = GetChipRpcClient();
398     LockRpcClient(client);
399     Context *context = client->context;
400     WriteBegin(context, 0);
401     WriteFunc(context, "IsChipSupportDfsChannel");
402     WriteEnd(context);
403     if (RpcClientCall(client, "IsChipSupportDfsChannel") != WIFI_IDL_OPT_OK) {
404         return false;
405     }
406     int result = WIFI_IDL_OPT_FAILED;
407     ReadInt(context, &result);
408     int retValue = WIFI_IDL_FALSE;
409     if (result == WIFI_IDL_OPT_OK) {
410         ReadInt(context, &retValue);
411         *isSupport = (retValue == WIFI_IDL_TRUE);
412     }
413     ReadClientEnd(client);
414     UnlockRpcClient(client);
415     return result;
416 }
417 
IsChipSupportIndoorChannel(bool * isSupport)418 WifiErrorNo IsChipSupportIndoorChannel(bool *isSupport)
419 {
420     RpcClient *client = GetChipRpcClient();
421     LockRpcClient(client);
422     Context *context = client->context;
423     WriteBegin(context, 0);
424     WriteFunc(context, "IsChipSupportIndoorChannel");
425     WriteEnd(context);
426     if (RpcClientCall(client, "IsChipSupportIndoorChannel") != WIFI_IDL_OPT_OK) {
427         return false;
428     }
429     int result = WIFI_IDL_OPT_FAILED;
430     ReadInt(context, &result);
431     int retValue = WIFI_IDL_FALSE;
432     if (result == WIFI_IDL_OPT_OK) {
433         ReadInt(context, &retValue);
434         *isSupport = (retValue == WIFI_IDL_TRUE);
435     }
436     ReadClientEnd(client);
437     UnlockRpcClient(client);
438     return result;
439 }
440