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_chip.h"
17 #include <securec.h>
18 #include "serial.h"
19 #include "wifi_hal_chip_interface.h"
20 #include "wifi_hal_define.h"
21 
22 #define WIFI_IDL_INTERFACE_SUPPORT_COMBINATIONS 32
23 
RpcGetWifiChip(RpcServer * server,Context * context)24 int RpcGetWifiChip(RpcServer *server, Context *context)
25 {
26     if (server == NULL || context == NULL) {
27         return HAL_FAILURE;
28     }
29     int chipId = 0;
30     if (ReadInt(context, &chipId) < 0) {
31         return HAL_FAILURE;
32     }
33     WifiChip wifiChip;
34     if (memset_s(&wifiChip, sizeof(wifiChip), 0, sizeof(wifiChip)) != EOK) {
35         return HAL_FAILURE;
36     }
37     WifiErrorNo err = GetWifiChip(chipId, &wifiChip);
38     WriteBegin(context, 0);
39     WriteInt(context, err);
40     if (err == WIFI_HAL_SUCCESS) {
41         WriteInt(context, wifiChip.chip);
42     }
43     WriteEnd(context);
44     return HAL_SUCCESS;
45 }
46 
RpcGetWifiChipIds(RpcServer * server,Context * context)47 int RpcGetWifiChipIds(RpcServer *server, Context *context)
48 {
49     if (server == NULL || context == NULL) {
50         return HAL_FAILURE;
51     }
52     int maxSize = 0;
53     if (ReadInt(context, &maxSize) < 0) {
54         return HAL_FAILURE;
55     }
56     if (maxSize <= 0) {
57         return HAL_FAILURE;
58     }
59     uint8_t *chipIds = (uint8_t *)calloc(maxSize * sizeof(uint8_t), sizeof(uint8_t));
60     if (chipIds == NULL) {
61         return HAL_FAILURE;
62     }
63     WifiErrorNo err = GetWifiChipIds(chipIds, &maxSize);
64     WriteBegin(context, 0);
65     WriteInt(context, err);
66     if (err == WIFI_HAL_SUCCESS) {
67         WriteInt(context, maxSize);
68         for (int i = 0; i < maxSize; ++i) {
69             WriteInt(context, chipIds[i]);
70         }
71     }
72     WriteEnd(context);
73     free(chipIds);
74     chipIds = NULL;
75     return HAL_SUCCESS;
76 }
77 
RpcGetChipId(RpcServer * server,Context * context)78 int RpcGetChipId(RpcServer *server, Context *context)
79 {
80     if (server == NULL || context == NULL) {
81         return HAL_FAILURE;
82     }
83     int chipId = 0;
84     WifiErrorNo err = GetChipId(&chipId);
85     WriteBegin(context, 0);
86     WriteInt(context, err);
87     if (err == WIFI_HAL_SUCCESS) {
88         WriteInt(context, chipId);
89     }
90     WriteEnd(context);
91     return HAL_SUCCESS;
92 }
93 
RpcCreateIface(RpcServer * server,Context * context)94 int RpcCreateIface(RpcServer *server, Context *context)
95 {
96     if (server == NULL || context == NULL) {
97         return HAL_FAILURE;
98     }
99     int type = 0;
100     if (ReadInt(context, &type) < 0) {
101         return HAL_FAILURE;
102     }
103     WifiIface wifiIface;
104     if (memset_s(&wifiIface, sizeof(wifiIface), 0, sizeof(wifiIface)) != EOK) {
105         return HAL_FAILURE;
106     }
107     WifiErrorNo err = CreateIface(type, &wifiIface);
108     WriteBegin(context, 0);
109     WriteInt(context, err);
110     if (err == WIFI_HAL_SUCCESS) {
111         WriteInt(context, wifiIface.index);
112         WriteInt(context, wifiIface.type);
113         WriteStr(context, wifiIface.name);
114         WriteStr(context, wifiIface.macAddr);
115     }
116     WriteEnd(context);
117     return HAL_SUCCESS;
118 }
119 
RpcGetIface(RpcServer * server,Context * context)120 int RpcGetIface(RpcServer *server, Context *context)
121 {
122     if (server == NULL || context == NULL) {
123         return HAL_FAILURE;
124     }
125     WifiIface wifiIface;
126     if (memset_s(&wifiIface, sizeof(wifiIface), 0, sizeof(wifiIface)) != EOK) {
127         return HAL_FAILURE;
128     }
129     char ifname[WIFI_IFACE_NAME_MAXLEN] = {0};
130     char *pstr = NULL;
131     int ret = ReadStr(context, ifname, WIFI_IFACE_NAME_MAXLEN);
132     if (ret < 0) {
133         return HAL_FAILURE;
134     } else if (ret > 0) {
135         int len = ret + 1;
136         pstr = (char *)calloc(len, sizeof(char));
137         if (pstr == NULL) {
138             return HAL_FAILURE;
139         }
140         if (ReadStr(context, pstr, len) != 0) {
141             free(pstr);
142             pstr = NULL;
143             return HAL_FAILURE;
144         }
145     }
146     WifiErrorNo err = GetIface((pstr == NULL) ? ifname : pstr, &wifiIface);
147     WriteBegin(context, 0);
148     WriteInt(context, err);
149     if (err == WIFI_HAL_SUCCESS) {
150         WriteInt(context, wifiIface.index);
151         WriteInt(context, wifiIface.type);
152         WriteStr(context, wifiIface.name);
153         WriteStr(context, wifiIface.macAddr);
154     }
155     WriteEnd(context);
156     if (pstr != NULL) {
157         free(pstr);
158         pstr = NULL;
159     }
160     return HAL_SUCCESS;
161 }
162 
RpcGetIfaceNames(RpcServer * server,Context * context)163 int RpcGetIfaceNames(RpcServer *server, Context *context)
164 {
165     if (server == NULL || context == NULL) {
166         return HAL_FAILURE;
167     }
168     int type = 0;
169     int size = 0;
170     if (ReadInt(context, &type) < 0 || ReadInt(context, &size) < 0 || size <= 0) {
171         return HAL_FAILURE;
172     }
173     char *ifname = (char *)calloc(size, sizeof(char));
174     if (ifname == NULL) {
175         return HAL_FAILURE;
176     }
177     WifiErrorNo err = GetIfaceNames(type, ifname, size);
178     WriteBegin(context, 0);
179     WriteInt(context, err);
180     if (err == WIFI_HAL_SUCCESS) {
181         WriteStr(context, ifname);
182     }
183     WriteEnd(context);
184     free(ifname);
185     ifname = NULL;
186     return HAL_SUCCESS;
187 }
188 
RpcRemoveIface(RpcServer * server,Context * context)189 int RpcRemoveIface(RpcServer *server, Context *context)
190 {
191     if (server == NULL || context == NULL) {
192         return HAL_FAILURE;
193     }
194     char ifname[WIFI_IFACE_NAME_MAXLEN] = {0};
195     char *pstr = NULL;
196     int ret = ReadStr(context, ifname, WIFI_IFACE_NAME_MAXLEN);
197     if (ret < 0) {
198         return HAL_FAILURE;
199     } else if (ret > 0) {
200         int len = ret + 1;
201         pstr = (char *)calloc(len, sizeof(char));
202         if (pstr == NULL) {
203             return HAL_FAILURE;
204         }
205         if (ReadStr(context, pstr, len) != 0) {
206             free(pstr);
207             pstr = NULL;
208             return HAL_FAILURE;
209         }
210     }
211     WifiErrorNo err = RemoveIface((pstr == NULL) ? ifname : pstr);
212     WriteBegin(context, 0);
213     WriteInt(context, err);
214     WriteEnd(context);
215     if (pstr != NULL) {
216         free(pstr);
217         pstr = NULL;
218     }
219     return HAL_SUCCESS;
220 }
221 
RpcGetCapabilities(RpcServer * server,Context * context)222 int RpcGetCapabilities(RpcServer *server, Context *context)
223 {
224     if (server == NULL || context == NULL) {
225         return HAL_FAILURE;
226     }
227     uint32_t capabilities = 0;
228     WifiErrorNo err = GetCapabilities(&capabilities);
229     WriteBegin(context, 0);
230     WriteInt(context, err);
231     if (err == WIFI_HAL_SUCCESS) {
232         WriteInt(context, capabilities);
233     }
234     WriteEnd(context);
235     return HAL_SUCCESS;
236 }
237 
RpcGetSupportedComboModes(RpcServer * server,Context * context)238 int RpcGetSupportedComboModes(RpcServer *server, Context *context)
239 {
240     if (server == NULL || context == NULL) {
241         return HAL_FAILURE;
242     }
243     int maxSize = 0;
244     if (ReadInt(context, &maxSize) < 0 || maxSize <= 0 || maxSize > WIFI_IDL_INTERFACE_SUPPORT_COMBINATIONS) {
245         return HAL_FAILURE;
246     }
247     int *modes = (int *)calloc(maxSize, sizeof(int));
248     if (modes == NULL) {
249         return HAL_FAILURE;
250     }
251     WifiErrorNo err = GetSupportedComboModes(modes, &maxSize);
252     WriteBegin(context, 0);
253     WriteInt(context, err);
254     if (err == WIFI_HAL_SUCCESS) {
255         WriteInt(context, maxSize);
256         for (int i = 0; i < maxSize; ++i) {
257             WriteInt(context, modes[i]);
258         }
259     }
260     WriteEnd(context);
261     free(modes);
262     modes = NULL;
263     return HAL_SUCCESS;
264 }
265 
RpcConfigComboModes(RpcServer * server,Context * context)266 int RpcConfigComboModes(RpcServer *server, Context *context)
267 {
268     if (server == NULL || context == NULL) {
269         return HAL_FAILURE;
270     }
271     int mode = 0;
272     if (ReadInt(context, &mode) < 0) {
273         return HAL_FAILURE;
274     }
275     WifiErrorNo err = ConfigComboModes(mode);
276     WriteBegin(context, 0);
277     WriteInt(context, err);
278     WriteEnd(context);
279     return HAL_SUCCESS;
280 }
281 
RpcGetComboModes(RpcServer * server,Context * context)282 int RpcGetComboModes(RpcServer *server, Context *context)
283 {
284     if (server == NULL || context == NULL) {
285         return HAL_FAILURE;
286     }
287     int mode = 0;
288     WifiErrorNo err = GetComboModes(&mode);
289     WriteBegin(context, 0);
290     WriteInt(context, err);
291     if (err == WIFI_HAL_SUCCESS) {
292         WriteInt(context, mode);
293     }
294     WriteEnd(context);
295     return HAL_SUCCESS;
296 }
297 
RpcRequestFirmwareDebugDump(RpcServer * server,Context * context)298 int RpcRequestFirmwareDebugDump(RpcServer *server, Context *context)
299 {
300     if (server == NULL || context == NULL) {
301         return HAL_FAILURE;
302     }
303     int maxSize = 0;
304     if (ReadInt(context, &maxSize) < 0 || maxSize <= 0) {
305         return HAL_FAILURE;
306     }
307 
308     unsigned char *bytes = (unsigned char *)calloc(maxSize + 1, sizeof(unsigned char));
309     if (bytes == NULL) {
310         return HAL_FAILURE;
311     }
312 
313     WifiErrorNo err = RequestFirmwareDebugDump(bytes, &maxSize);
314     WriteBegin(context, 0);
315     WriteInt(context, err);
316     if (err == WIFI_HAL_SUCCESS) {
317         WriteInt(context, maxSize);
318         WriteUStr(context, bytes, maxSize);
319     }
320     WriteEnd(context);
321     free(bytes);
322     bytes = NULL;
323     return HAL_SUCCESS;
324 }
325 
RpcIsChipSupportDbdc(RpcServer * server,Context * context)326 int RpcIsChipSupportDbdc(RpcServer *server, Context *context)
327 {
328     if (server == NULL || context == NULL) {
329         return HAL_FAILURE;
330     }
331     int support = 0;
332     WifiErrorNo err = GetIsChipSupportDbdc(&support);
333     WriteBegin(context, 0);
334     WriteInt(context, err);
335     if (err == WIFI_HAL_SUCCESS) {
336         WriteInt(context, support);
337     }
338     WriteEnd(context);
339     return HAL_SUCCESS;
340 }
341 
RpcIsChipSupportCsa(RpcServer * server,Context * context)342 int RpcIsChipSupportCsa(RpcServer *server, Context *context)
343 {
344     if (server == NULL || context == NULL) {
345         return HAL_FAILURE;
346     }
347     int support = 0;
348     WifiErrorNo err = GetIsChipSupportCsa(&support);
349     WriteBegin(context, 0);
350     WriteInt(context, err);
351     if (err == WIFI_HAL_SUCCESS) {
352         WriteInt(context, support);
353     }
354     WriteEnd(context);
355     return HAL_SUCCESS;
356 }
357 
RpcIsChipSupportRadarDetect(RpcServer * server,Context * context)358 int RpcIsChipSupportRadarDetect(RpcServer *server, Context *context)
359 {
360     if (server == NULL || context == NULL) {
361         return HAL_FAILURE;
362     }
363     int support = 0;
364     WifiErrorNo err = GetIsChipSupportRadarDetect(&support);
365     WriteBegin(context, 0);
366     WriteInt(context, err);
367     if (err == WIFI_HAL_SUCCESS) {
368         WriteInt(context, support);
369     }
370     WriteEnd(context);
371     return HAL_SUCCESS;
372 }
373 
RpcIsChipSupportDfsChannel(RpcServer * server,Context * context)374 int RpcIsChipSupportDfsChannel(RpcServer *server, Context *context)
375 {
376     if (server == NULL || context == NULL) {
377         return HAL_FAILURE;
378     }
379     int support = 0;
380     WifiErrorNo err = GetIsChipSupportDfsChannel(&support);
381     WriteBegin(context, 0);
382     WriteInt(context, err);
383     if (err == WIFI_HAL_SUCCESS) {
384         WriteInt(context, support);
385     }
386     WriteEnd(context);
387     return HAL_SUCCESS;
388 }
389 
RpcIsChipSupportIndoorChannel(RpcServer * server,Context * context)390 int RpcIsChipSupportIndoorChannel(RpcServer *server, Context *context)
391 {
392     if (server == NULL || context == NULL) {
393         return HAL_FAILURE;
394     }
395     int support = 0;
396     WifiErrorNo err = GetIsChipSupportIndoorChannel(&support);
397     WriteBegin(context, 0);
398     WriteInt(context, err);
399     if (err == WIFI_HAL_SUCCESS) {
400         WriteInt(context, support);
401     }
402     WriteEnd(context);
403     return HAL_SUCCESS;
404 }