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_common.h"
17 #include "serial.h"
18 #include "wifi_hal_crpc_base.h"
19 #include "wifi_hal_sta_interface.h"
20 #include "wifi_hal_ap_interface.h"
21 #include "wifi_hal_p2p_interface.h"
22 #include "wifi_hal_define.h"
23 #include "wifi_hostapd_hal.h"
24 #include "wifi_common_hal.h"
25 #include "wifi_hal_chba_interface.h"
26 
27 #define RPC_REGISTER_MAX_NUM 256
28 
RpcRegisterEventCallback(RpcServer * server,Context * context)29 int RpcRegisterEventCallback(RpcServer *server, Context *context)
30 {
31     if (server == NULL || context == NULL) {
32         return HAL_FAILURE;
33     }
34     int num = 0;
35     if (ReadInt(context, &num) < 0) {
36         return HAL_FAILURE;
37     }
38     if (num < 0 || num > RPC_REGISTER_MAX_NUM) {
39         return HAL_FAILURE;
40     }
41     int *events = ReadIntArray(context, num);
42     if (events == NULL) {
43         return HAL_FAILURE;
44     }
45     for (int i = 0; i < num; ++i) {
46         RegisterCallback(server, events[i], context);
47     }
48     WriteBegin(context, 0);
49     WriteInt(context, WIFI_HAL_SUCCESS);
50     WriteEnd(context);
51     free(events);
52     events = NULL;
53     return HAL_SUCCESS;
54 }
55 
RpcUnRegisterEventCallback(RpcServer * server,Context * context)56 int RpcUnRegisterEventCallback(RpcServer *server, Context *context)
57 {
58     if (server == NULL || context == NULL) {
59         return HAL_FAILURE;
60     }
61     int num = 0;
62     if (ReadInt(context, &num) < 0) {
63         return HAL_FAILURE;
64     }
65     if (num < 0 || num > RPC_REGISTER_MAX_NUM) {
66         return HAL_FAILURE;
67     }
68     int *events = ReadIntArray(context, num);
69     if (events == NULL) {
70         return HAL_FAILURE;
71     }
72     for (int i = 0; i < num; ++i) {
73         UnRegisterCallback(server, events[i], context);
74     }
75     WriteBegin(context, 0);
76     WriteInt(context, WIFI_HAL_SUCCESS);
77     WriteEnd(context);
78     free(events);
79     events = NULL;
80     return HAL_SUCCESS;
81 }
82 
RpcNotifyClear(RpcServer * server,Context * context)83 int RpcNotifyClear(RpcServer *server, Context *context)
84 {
85     if (server == NULL || context == NULL) {
86         return HAL_FAILURE;
87     }
88     ForceStop();
89     for (int id = 0; id < AP_MAX_INSTANCE; id++) {
90         StopSoftAp(id);
91     }
92     P2pForceStop();
93     WriteBegin(context, 0);
94     WriteInt(context, 0);
95     WriteEnd(context);
96     return HAL_SUCCESS;
97 }
98 
RpcGetCommonCmd(RpcServer * server,Context * context)99 int RpcGetCommonCmd(RpcServer *server, Context *context)
100 {
101     if (server == NULL || context == NULL) {
102         return HAL_FAILURE;
103     }
104     char recvCmd[WIFI_CMD_STR_LENGTH + 1] = {0};
105     if (ReadStr(context, recvCmd, sizeof(recvCmd)) != 0) {
106         return HAL_FAILURE;
107     }
108     WifiErrorNo err = SendComCmd(recvCmd);
109     WriteBegin(context, 0);
110     WriteInt(context, err);
111     WriteEnd(context);
112     return HAL_SUCCESS;
113 }
114 
RpcChbaStart(RpcServer * server,Context * context)115 int RpcChbaStart(RpcServer *server, Context *context)
116 {
117     if (server == NULL || context == NULL) {
118         return HAL_FAILURE;
119     }
120     WifiErrorNo err = ChbaStart();
121     WriteBegin(context, 0);
122     WriteInt(context, err);
123     WriteEnd(context);
124     return HAL_SUCCESS;
125 }
126 
RpcChbaStop(RpcServer * server,Context * context)127 int RpcChbaStop(RpcServer *server, Context *context)
128 {
129     if (server == NULL || context == NULL) {
130         return HAL_FAILURE;
131     }
132     WifiErrorNo err = ChbaStop();
133     WriteBegin(context, 0);
134     WriteInt(context, err);
135     WriteEnd(context);
136     return HAL_SUCCESS;
137 }