1 /*
2 * Copyright (c) 2025 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 <inttypes.h>
17
18 #include "client_callback.h"
19 #include "rpc_errno.h"
20 #include "rpc_log.h"
21
22 typedef struct {
23 int32_t funIdType;
24 void (*handler)(IpcIo *);
25 } ClientCallbackHandler;
26
OnBoolRecv(IpcIo * reply)27 static void OnBoolRecv(IpcIo *reply)
28 {
29 bool recvData;
30 ReadBool(reply, &recvData);
31 RPC_LOG_INFO("[ipc_test_client] OnBoolRecv called, recv=%s", recvData ? "true" : "false");
32 return;
33 }
34
OnInt8Recv(IpcIo * reply)35 static void OnInt8Recv(IpcIo *reply)
36 {
37 int8_t recvData;
38 ReadInt8(reply, &recvData);
39 RPC_LOG_INFO("[ipc_test_client] OnInt8Recv called, recv=%" PRId8 "", recvData);
40 return;
41 }
42
OnInt16Recv(IpcIo * reply)43 static void OnInt16Recv(IpcIo *reply)
44 {
45 int16_t recvData;
46 ReadInt16(reply, &recvData);
47 RPC_LOG_INFO("[ipc_test_client] OnInt16Recv called, recv=%" PRId16 "", recvData);
48 return;
49 }
50
OnInt32Recv(IpcIo * reply)51 static void OnInt32Recv(IpcIo *reply)
52 {
53 int32_t recvData;
54 ReadInt32(reply, &recvData);
55 RPC_LOG_INFO("[ipc_test_client] OnInt16Recv called, recv=%" PRId32 "", recvData);
56 return;
57 }
58
OnInt64Recv(IpcIo * reply)59 static void OnInt64Recv(IpcIo *reply)
60 {
61 int64_t recvData;
62 ReadInt64(reply, &recvData);
63 RPC_LOG_INFO("[ipc_test_client] OnInt64Recv called, recv=%" PRId64 "", recvData);
64 return;
65 }
66
OnUint8Recv(IpcIo * reply)67 static void OnUint8Recv(IpcIo *reply)
68 {
69 uint8_t recvData;
70 ReadUint8(reply, &recvData);
71 RPC_LOG_INFO("[ipc_test_client] OnUint8Recv called, recv=%" PRIu8 "", recvData);
72 return;
73 }
74
OnUint16Recv(IpcIo * reply)75 static void OnUint16Recv(IpcIo *reply)
76 {
77 uint16_t recvData;
78 ReadUint16(reply, &recvData);
79 RPC_LOG_INFO("[ipc_test_client] OnUint16Recv called, recv=%" PRIu16 "", recvData);
80 return;
81 }
82
OnUint32Recv(IpcIo * reply)83 static void OnUint32Recv(IpcIo *reply)
84 {
85 uint32_t recvData;
86 ReadUint32(reply, &recvData);
87 RPC_LOG_INFO("[ipc_test_client] OnUint32Recv called, recv=%" PRIu32 "", recvData);
88 return;
89 }
90
OnUint64Recv(IpcIo * reply)91 static void OnUint64Recv(IpcIo *reply)
92 {
93 uint64_t recvData;
94 ReadUint64(reply, &recvData);
95 RPC_LOG_INFO("[ipc_test_client] OnUint64Recv called, recv=%" PRIu64 "", recvData);
96 return;
97 }
98
OnFloatRecv(IpcIo * reply)99 static void OnFloatRecv(IpcIo *reply)
100 {
101 float recvData;
102 ReadFloat(reply, &recvData);
103 RPC_LOG_INFO("[ipc_test_client] OnFloatRecv called, recv=%f", recvData);
104 return;
105 }
106
OnDoubleRecv(IpcIo * reply)107 static void OnDoubleRecv(IpcIo *reply)
108 {
109 double recvData;
110 ReadDouble(reply, &recvData);
111 RPC_LOG_INFO("[ipc_test_client] OnDoubleRecv called, recv=%f", recvData);
112 return;
113 }
114
OnInt8VectorRecv(IpcIo * reply)115 static void OnInt8VectorRecv(IpcIo *reply)
116 {
117 int8_t *dataVector = NULL;
118 size_t size = 0;
119 dataVector = ReadInt8Vector(reply, &size);
120 if (dataVector == NULL || size == 0) {
121 RPC_LOG_ERROR("[ipc_test_client] OnInt8VectorRecv error, vector is null");
122 return;
123 }
124 RPC_LOG_INFO("[ipc_test_client] OnInt8VectorRecv called, size=%zu, start=%" PRId8 ", end=%" PRId8 "",
125 size, dataVector[0], dataVector[size - 1]);
126 return;
127 }
128
OnInt16VectorRecv(IpcIo * reply)129 static void OnInt16VectorRecv(IpcIo *reply)
130 {
131 int16_t *dataVector = NULL;
132 size_t size = 0;
133 dataVector = ReadInt16Vector(reply, &size);
134 if (dataVector == NULL || size == 0) {
135 RPC_LOG_ERROR("[ipc_test_client] OnInt16VectorRecv error, vector is null");
136 return;
137 }
138 RPC_LOG_INFO("[ipc_test_client] OnInt16VectorRecv called, size=%zu, start=%" PRId16 ", end=%" PRId16 "",
139 size, dataVector[0], dataVector[size - 1]);
140 return;
141 }
142
OnInt32VectorRecv(IpcIo * reply)143 static void OnInt32VectorRecv(IpcIo *reply)
144 {
145 int32_t *dataVector = NULL;
146 size_t size = 0;
147 dataVector = ReadInt32Vector(reply, &size);
148 if (dataVector == NULL || size == 0) {
149 RPC_LOG_ERROR("[ipc_test_client] OnInt32VectorRecv error, vector is null");
150 return;
151 }
152 RPC_LOG_INFO("[ipc_test_client] OnInt32VectorRecv called, size=%zu, start=%" PRId32 ", end=%" PRId32 "",
153 size, dataVector[0], dataVector[size - 1]);
154 return;
155 }
156
OnInt64VectorRecv(IpcIo * reply)157 static void OnInt64VectorRecv(IpcIo *reply)
158 {
159 int64_t *dataVector = NULL;
160 size_t size = 0;
161 dataVector = ReadInt64Vector(reply, &size);
162 if (dataVector == NULL || size == 0) {
163 RPC_LOG_ERROR("[ipc_test_client] OnInt64VectorRecv error, vector is null");
164 return;
165 }
166 RPC_LOG_INFO("[ipc_test_client] OnInt64VectorRecv called, size=%zu, start=%" PRId64 ", end=%" PRId64 "",
167 size, dataVector[0], dataVector[size - 1]);
168 return;
169 }
170
OnUint8VectorRecv(IpcIo * reply)171 static void OnUint8VectorRecv(IpcIo *reply)
172 {
173 uint8_t *dataVector = NULL;
174 size_t size = 0;
175 dataVector = ReadUInt8Vector(reply, &size);
176 if (dataVector == NULL || size == 0) {
177 RPC_LOG_ERROR("[ipc_test_client] OnUint8VectorRecv error, vector is null");
178 return;
179 }
180 RPC_LOG_INFO("[ipc_test_client] OnUint8VectorRecv called, size=%zu, start=%" PRIu8 ", end=%" PRIu8 "",
181 size, dataVector[0], dataVector[size - 1]);
182 return;
183 }
184
OnUint16VectorRecv(IpcIo * reply)185 static void OnUint16VectorRecv(IpcIo *reply)
186 {
187 uint16_t *dataVector = NULL;
188 size_t size = 0;
189 dataVector = ReadUInt16Vector(reply, &size);
190 if (dataVector == NULL || size == 0) {
191 RPC_LOG_ERROR("[ipc_test_client] OnUint16VectorRecv error, vector is null");
192 return;
193 }
194 RPC_LOG_INFO("[ipc_test_client] OnUint16VectorRecv called, size=%zu, start=%" PRIu16 ", end=%" PRIu16 "",
195 size, dataVector[0], dataVector[size - 1]);
196 return;
197 }
198
OnUint32VectorRecv(IpcIo * reply)199 static void OnUint32VectorRecv(IpcIo *reply)
200 {
201 uint32_t *dataVector = NULL;
202 size_t size = 0;
203 dataVector = ReadUInt32Vector(reply, &size);
204 if (dataVector == NULL || size == 0) {
205 RPC_LOG_ERROR("[ipc_test_client] OnUint32VectorRecv error, vector is null");
206 return;
207 }
208 RPC_LOG_INFO("[ipc_test_client] OnUint32VectorRecv called, size=%zu, start=%" PRIu32 ", end=%" PRIu32 "",
209 size, dataVector[0], dataVector[size - 1]);
210 return;
211 }
212
OnUint64VectorRecv(IpcIo * reply)213 static void OnUint64VectorRecv(IpcIo *reply)
214 {
215 uint64_t *dataVector = NULL;
216 size_t size = 0;
217 dataVector = ReadUInt64Vector(reply, &size);
218 if (dataVector == NULL || size == 0) {
219 RPC_LOG_ERROR("[ipc_test_client] OnUint64VectorRecv error, vector is null");
220 return;
221 }
222 RPC_LOG_INFO("[ipc_test_client] OnUint64VectorRecv called, size=%zu, start=%" PRIu64 ", end=%" PRIu64 "",
223 size, dataVector[0], dataVector[size - 1]);
224 return;
225 }
226
OnFloatVectorRecv(IpcIo * reply)227 static void OnFloatVectorRecv(IpcIo *reply)
228 {
229 float *dataVector = NULL;
230 size_t size = 0;
231 dataVector = ReadFloatVector(reply, &size);
232 if (dataVector == NULL || size == 0) {
233 RPC_LOG_ERROR("[ipc_test_client] OnFloatVectorRecv error, vector is null");
234 return;
235 }
236 RPC_LOG_INFO("[ipc_test_client] OnFloatVectorRecv called, size=%zu, start=%f, end=%f",
237 size, dataVector[0], dataVector[size - 1]);
238 return;
239 }
240
OnDoubleVectorRecv(IpcIo * reply)241 static void OnDoubleVectorRecv(IpcIo *reply)
242 {
243 double *dataVector = NULL;
244 size_t size = 0;
245 dataVector = ReadDoubleVector(reply, &size);
246 if (dataVector == NULL || size == 0) {
247 RPC_LOG_ERROR("[ipc_test_client] OnFloatVectorRecv error, vector is null");
248 return;
249 }
250 RPC_LOG_INFO("[ipc_test_client] OnFloatVectorRecv called, size=%zu, start=%f, end=%f",
251 size, dataVector[0], dataVector[size - 1]);
252 return;
253 }
254
OnStringRecv(IpcIo * reply)255 static void OnStringRecv(IpcIo *reply)
256 {
257 size_t len = 0;
258 const char *recvData = (char *)ReadString(reply, &len);
259 if (recvData == NULL) {
260 RPC_LOG_ERROR("[ipc_test_client] OnStringRecv error, recvData is null");
261 return;
262 }
263 RPC_LOG_INFO("[ipc_test_client] OnStringRecv called, recv=%s", recvData);
264 return;
265 }
266
OnFileDescriptorRecv(IpcIo * reply)267 static void OnFileDescriptorRecv(IpcIo *reply)
268 {
269 (void)reply;
270 RPC_LOG_INFO("[ipc_test_client] OnFileDescriptorRecv called");
271 return;
272 }
273
OnRawDataRecv(IpcIo * reply)274 static void OnRawDataRecv(IpcIo *reply)
275 {
276 SvcIdentity *recvData = NULL;
277 recvData = ReadRawData(reply, sizeof(SvcIdentity));
278 if (recvData == NULL) {
279 RPC_LOG_ERROR("[ipc_test_client] OnRawDataRecv error, recvData is null");
280 return;
281 }
282 RPC_LOG_ERROR("[ipc_test_client] OnRawDataRecv success");
283 return;
284 }
285
OnBufferRecv(IpcIo * reply)286 static void OnBufferRecv(IpcIo *reply)
287 {
288 SvcIdentity *recvData = NULL;
289 recvData = ReadBuffer(reply, sizeof(SvcIdentity));
290 if (recvData == NULL) {
291 RPC_LOG_ERROR("[ipc_test_client] OnBufferRecv error, recvData is null");
292 return;
293 }
294 RPC_LOG_ERROR("[ipc_test_client] OnBufferRecv success");
295 return;
296 }
297
298 static ClientCallbackHandler g_clientCallbackHandler[] = {
299 { BOOL_TYPE, OnBoolRecv },
300 { INT8_TYPE, OnInt8Recv },
301 { INT16_TYPE, OnInt16Recv },
302 { INT32_TYPE, OnInt32Recv },
303 { INT64_TYPE, OnInt64Recv },
304 { UINT8_TYPE, OnUint8Recv },
305 { UINT16_TYPE, OnUint16Recv },
306 { UINT32_TYPE, OnUint32Recv },
307 { UINT64_TYPE, OnUint64Recv },
308 { FLOAT_TYPE, OnFloatRecv },
309 { DOUBLE_TYPE, OnDoubleRecv },
310
311 { VECTOR_INT8_TYPE, OnInt8VectorRecv },
312 { VECTOR_INT16_TYPE, OnInt16VectorRecv },
313 { VECTOR_INT32_TYPE, OnInt32VectorRecv },
314 { VECTOR_INT64_TYPE, OnInt64VectorRecv },
315 { VECTOR_UINT8_TYPE, OnUint8VectorRecv },
316 { VECTOR_UINT16_TYPE, OnUint16VectorRecv },
317 { VECTOR_UINT32_TYPE, OnUint32VectorRecv },
318 { VECTOR_UINT64_TYPE, OnUint64VectorRecv },
319 { VECTOR_FLOAT_TYPE, OnFloatVectorRecv },
320 { VECTOR_DOUBLE_TYPE, OnDoubleVectorRecv },
321
322 { CHAR_TYPE, OnStringRecv },
323 { FD_TYPE, OnFileDescriptorRecv },
324 { RAW_DATA_TYPE, OnRawDataRecv },
325 { BUFFER_TYPE, OnBufferRecv },
326 };
327
ClientSendSyncMessageCallback(void * info,int ret,IpcIo * reply)328 int32_t ClientSendSyncMessageCallback(void *info, int ret, IpcIo *reply)
329 {
330 if (ret != ERR_NONE) {
331 RPC_LOG_ERROR("[ipc_test_client] OnSendSyncMessageCallback error, ret=%d", ret);
332 return ERR_FAILED;
333 }
334 Reply *replyInfo = (Reply *)info;
335 for (uint32_t i = 0; i < sizeof(g_clientCallbackHandler) / sizeof(ClientCallbackHandler); i++) {
336 if (g_clientCallbackHandler[i].funIdType == replyInfo->id) {
337 g_clientCallbackHandler[i].handler(reply);
338 return ERR_NONE;
339 }
340 }
341 RPC_LOG_ERROR("[ipc_test_client] OnSendSyncMessageCallback invalid func, id=%d", replyInfo->id);
342 return ERR_INVALID_PARAM;
343 }