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_ap.h"
17 #include <securec.h>
18 #include "serial.h"
19 #include "wifi_hal_ap_interface.h"
20 #include "wifi_hal_define.h"
21 
22 #define IFACENAME_LEN 6
23 #define WIFI_IDL_GET_MAX_BANDS 32
24 
RpcStartSoftAp(RpcServer * server,Context * context)25 int RpcStartSoftAp(RpcServer *server, Context *context)
26 {
27     if (server == NULL || context == NULL) {
28         return HAL_FAILURE;
29     }
30     int id = 0;
31     if (ReadInt(context, &id) < 0 || id < 0) {
32         return HAL_FAILURE;
33     }
34     char ifaceName[IFACENAME_LEN];
35     if (ReadStr(context, ifaceName, sizeof(ifaceName)) != 0) {
36         return HAL_FAILURE;
37     }
38     WifiErrorNo err = StartSoftAp(id, ifaceName);
39     WriteBegin(context, 0);
40     WriteInt(context, err);
41     WriteEnd(context);
42     return HAL_SUCCESS;
43 }
44 
RpcStopSoftAp(RpcServer * server,Context * context)45 int RpcStopSoftAp(RpcServer *server, Context *context)
46 {
47     if (server == NULL || context == NULL) {
48         return HAL_FAILURE;
49     }
50     int id = 0;
51     if (ReadInt(context, &id) < 0 || id < 0) {
52         return HAL_FAILURE;
53     }
54 
55     WifiErrorNo err = StopSoftAp(id);
56     WriteBegin(context, 0);
57     WriteInt(context, err);
58     WriteEnd(context);
59     return HAL_SUCCESS;
60 }
61 
RpcSetHostapdConfig(RpcServer * server,Context * context)62 int RpcSetHostapdConfig(RpcServer *server, Context *context)
63 {
64     if (server == NULL || context == NULL) {
65         return HAL_FAILURE;
66     }
67     int id = 0;
68     HostapdConfig config;
69     if (memset_s(&config, sizeof(config), 0, sizeof(config)) != EOK) {
70         return HAL_FAILURE;
71     }
72 
73     if (ReadStr(context, config.ssid, sizeof(config.ssid)) != 0 || ReadInt(context, &config.ssidLen) < 0 ||
74         ReadStr(context, config.preSharedKey, sizeof(config.preSharedKey)) != 0 ||
75         ReadInt(context, &config.preSharedKeyLen) < 0 || ReadInt(context, &config.securityType) < 0 ||
76         ReadInt(context, &config.band) < 0 || ReadInt(context, &config.channel) < 0 ||
77         ReadInt(context, &config.maxConn) < 0 || ReadInt(context, &id) < 0 || id < 0) {
78         return HAL_FAILURE;
79     }
80     WifiErrorNo err = SetHostapdConfig(&config, id);
81     WriteBegin(context, 0);
82     WriteInt(context, err);
83     WriteEnd(context);
84     return HAL_SUCCESS;
85 }
86 
RpcGetStaInfos(RpcServer * server,Context * context)87 int RpcGetStaInfos(RpcServer *server, Context *context)
88 {
89     if (server == NULL || context == NULL) {
90         return HAL_FAILURE;
91     }
92     int maxSize = 0;
93     int id = 0;
94     if (ReadInt(context, &maxSize) < 0 || maxSize <= 0 || ReadInt(context, &id) < 0 || id < 0) {
95         return HAL_FAILURE;
96     }
97     char *infos = (char *)calloc(maxSize, sizeof(char));
98     if (infos == NULL) {
99         return HAL_FAILURE;
100     }
101     WifiErrorNo err = GetStaInfos(infos, &maxSize, id);
102     WriteBegin(context, 0);
103     WriteInt(context, err);
104     if (err == WIFI_HAL_SUCCESS) {
105         WriteInt(context, maxSize);
106         WriteStr(context, infos);
107     }
108     WriteEnd(context);
109     free(infos);
110     infos = NULL;
111     return HAL_SUCCESS;
112 }
113 
RpcSetCountryCode(RpcServer * server,Context * context)114 int RpcSetCountryCode(RpcServer *server, Context *context)
115 {
116     if (server == NULL || context == NULL) {
117         return HAL_FAILURE;
118     }
119     char countryCode[WIFI_COUNTRY_CODE_MAXLEN + 1] = {0};
120     int id = 0;
121     if (ReadStr(context, countryCode, sizeof(countryCode)) != 0 || ReadInt(context, &id) < 0 || id < 0) {
122         return HAL_FAILURE;
123     }
124     WifiErrorNo err = SetCountryCode(countryCode, id);
125     WriteBegin(context, 0);
126     WriteInt(context, err);
127     WriteEnd(context);
128     return HAL_SUCCESS;
129 }
130 
RpcSetMacFilter(RpcServer * server,Context * context)131 int RpcSetMacFilter(RpcServer *server, Context *context)
132 {
133     if (server == NULL || context == NULL) {
134         return HAL_FAILURE;
135     }
136     int lenMac = 0;
137     int id = 0;
138     if (ReadInt(context, &lenMac) < 0 || lenMac <= 0) {
139         return HAL_FAILURE;
140     }
141     int len = lenMac + 1;
142     unsigned char *mac = (unsigned char *)calloc(len, sizeof(unsigned char));
143     if (mac == NULL) {
144         return HAL_FAILURE;
145     }
146     if (ReadUStr(context, mac, len) != 0 || ReadInt(context, &id) < 0 || id < 0) {
147         free(mac);
148         mac = NULL;
149         return HAL_FAILURE;
150     }
151     WifiErrorNo err = SetMacFilter(mac, lenMac, id);
152     WriteBegin(context, 0);
153     WriteInt(context, err);
154     WriteEnd(context);
155     free(mac);
156     mac = NULL;
157     return HAL_SUCCESS;
158 }
159 
RpcDelMacFilter(RpcServer * server,Context * context)160 int RpcDelMacFilter(RpcServer *server, Context *context)
161 {
162     if (server == NULL || context == NULL) {
163         return HAL_FAILURE;
164     }
165     int lenMac = 0;
166     int id = 0;
167     if (ReadInt(context, &lenMac) < 0 || lenMac <= 0) {
168         return HAL_FAILURE;
169     }
170     int len = lenMac + 1;
171     unsigned char *mac = (unsigned char *)calloc(len, sizeof(unsigned char));
172     if (mac == NULL) {
173         return HAL_FAILURE;
174     }
175     if (ReadUStr(context, mac, len) != 0 || ReadInt(context, &id) < 0 || id < 0) {
176         free(mac);
177         mac = NULL;
178         return HAL_FAILURE;
179     }
180     WifiErrorNo err = DelMacFilter(mac, lenMac, id);
181     WriteBegin(context, 0);
182     WriteInt(context, err);
183     WriteEnd(context);
184     free(mac);
185     mac = NULL;
186     return HAL_SUCCESS;
187 }
188 
RpcDisassociateSta(RpcServer * server,Context * context)189 int RpcDisassociateSta(RpcServer *server, Context *context)
190 {
191     if (server == NULL || context == NULL) {
192         return HAL_FAILURE;
193     }
194     int lenMac = 0;
195     int id = 0;
196     if (ReadInt(context, &lenMac) < 0 || lenMac <= 0) {
197         return HAL_FAILURE;
198     }
199     int len = lenMac + 1;
200     unsigned char *mac = (unsigned char *)calloc(len, sizeof(unsigned char));
201     if (mac == NULL) {
202         return HAL_FAILURE;
203     }
204 
205     if (ReadUStr(context, mac, len) != 0 || ReadInt(context, &id) < 0 || id < 0) {
206         free(mac);
207         mac = NULL;
208         return HAL_FAILURE;
209     }
210     WifiErrorNo err = DisassociateSta(mac, lenMac, id);
211     WriteBegin(context, 0);
212     WriteInt(context, err);
213     WriteEnd(context);
214     free(mac);
215     mac = NULL;
216     return HAL_SUCCESS;
217 }
218 
RpcGetValidFrequenciesForBand(RpcServer * server,Context * context)219 int RpcGetValidFrequenciesForBand(RpcServer *server, Context *context)
220 {
221     if (server == NULL || context == NULL) {
222         return HAL_FAILURE;
223     }
224     int band = 0;
225     int size = 0;
226     int id = 0;
227     if (ReadInt(context, &band) < 0 || ReadInt(context, &size) < 0  ||
228         ReadInt(context, &id) < 0 || id < 0) {
229         return HAL_FAILURE;
230     }
231     if (size <= 0 || size > WIFI_IDL_GET_MAX_BANDS) {
232         return HAL_FAILURE;
233     }
234     int *frequencies = (int *)calloc(size, sizeof(int));
235     if (frequencies == NULL) {
236         return HAL_FAILURE;
237     }
238     WifiErrorNo err = GetValidFrequenciesForBand(band, frequencies, &size, id);
239     WriteBegin(context, 0);
240     WriteInt(context, err);
241     if (err == WIFI_HAL_SUCCESS) {
242         WriteInt(context, size);
243         for (int i = 0; i < size; ++i) {
244             WriteInt(context, frequencies[i]);
245         }
246     }
247     WriteEnd(context);
248     free(frequencies);
249     frequencies = NULL;
250     return HAL_SUCCESS;
251 }
252 
RpcSetPowerModel(RpcServer * server,Context * context)253 int RpcSetPowerModel(RpcServer *server, Context *context)
254 {
255     if (server == NULL || context == NULL) {
256         return HAL_FAILURE;
257     }
258     int mode = -1;
259     if (ReadInt(context, &mode) < 0) {
260         return HAL_FAILURE;
261     }
262     int id = 0;
263     if (ReadInt(context, &id) < 0) {
264         return HAL_FAILURE;
265     }
266     WifiErrorNo err = WifiSetPowerModel(mode, id);
267     WriteBegin(context, 0);
268     WriteInt(context, err);
269     WriteEnd(context);
270     return HAL_SUCCESS;
271 }
272 
RpcGetPowerModel(RpcServer * server,Context * context)273 int RpcGetPowerModel(RpcServer *server, Context *context)
274 {
275     if (server == NULL || context == NULL) {
276         return HAL_FAILURE;
277     }
278     int mode = -1;
279     int id = 0;
280     if (ReadInt(context, &id) < 0) {
281         return HAL_FAILURE;
282     }
283     WifiErrorNo err = WifiGetPowerModel(&mode, id);
284     WriteBegin(context, 0);
285     WriteInt(context, err);
286     if (err == WIFI_HAL_SUCCESS) {
287         WriteInt(context, mode);
288     }
289     WriteEnd(context);
290     return HAL_SUCCESS;
291 }
292