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_base.h"
17 #include <stdlib.h>
18 #include "serial.h"
19 #include "wifi_hal_base_interface.h"
20 #include "wifi_hal_define.h"
21
ReadIntArray(Context * context,int size)22 int *ReadIntArray(Context *context, int size)
23 {
24 if (size <= 0) {
25 return NULL;
26 }
27
28 int *pArray = (int *)calloc(size, sizeof(int));
29 if (pArray == NULL) {
30 return NULL;
31 }
32 for (int i = 0; i < size; ++i) {
33 if (ReadInt(context, pArray + i) < 0) {
34 free(pArray);
35 pArray = NULL;
36 return NULL;
37 }
38 }
39 return pArray;
40 }
41
ReadCharArray(Context * context,int size)42 char **ReadCharArray(Context *context, int size)
43 {
44 char **pArray = NULL;
45 if (size > 0) {
46 pArray = (char **)calloc(size, sizeof(char *));
47 }
48 if (pArray == NULL) {
49 return NULL;
50 }
51 int i = 0;
52 for (; i < size; ++i) {
53 int len = 0;
54 if (ReadInt(context, &len) < 0) {
55 break;
56 }
57 pArray[i] = (char *)calloc(len + 1, sizeof(char));
58 if (pArray[i] == NULL) {
59 break;
60 }
61 if (ReadStr(context, pArray[i], len + 1) < 0) {
62 break;
63 }
64 }
65 if (i < size) {
66 for (int j = 0; j < size; ++j) {
67 free(pArray[j]);
68 pArray[j] = NULL;
69 }
70 free(pArray);
71 pArray = NULL;
72 return NULL;
73 }
74 return pArray;
75 }
76
RpcGetName(RpcServer * server,Context * context)77 int RpcGetName(RpcServer *server, Context *context)
78 {
79 if (server == NULL || context == NULL) {
80 return HAL_FAILURE;
81 }
82 int size = 0;
83 if (ReadInt(context, &size) < 0 || size <= 0) {
84 return HAL_FAILURE;
85 }
86 char *ifname = (char *)calloc(size, sizeof(char));
87 if (ifname == NULL) {
88 return HAL_FAILURE;
89 }
90 WifiErrorNo err = GetName(ifname, size);
91 WriteBegin(context, 0);
92 WriteInt(context, err);
93 if (err == WIFI_HAL_SUCCESS) {
94 WriteStr(context, ifname);
95 }
96 WriteEnd(context);
97 free(ifname);
98 ifname = NULL;
99 return HAL_SUCCESS;
100 }
101
RpcGetType(RpcServer * server,Context * context)102 int RpcGetType(RpcServer *server, Context *context)
103 {
104 if (server == NULL || context == NULL) {
105 return HAL_FAILURE;
106 }
107 int type = 0;
108 WifiErrorNo err = GetType(&type);
109 WriteBegin(context, 0);
110 WriteInt(context, err);
111 if (err == WIFI_HAL_SUCCESS) {
112 WriteInt(context, type);
113 }
114 WriteEnd(context);
115 return HAL_SUCCESS;
116 }