• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <math.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "common.h"
22 #include "ipc_skeleton.h"
23 #include "rpc_errno.h"
24 #include "rpc_log.h"
25 #include "securec.h"
26 #include "serializer.h"
27 #include "server_business_impl.h"
28 
29 #define PKG_NAME_LIMIT 65
30 #define SERVER_TEST_NUMBER 101
31 #define HALF_PI (acosl(-1) / 2)
32 #define SERVER_VECTOR_LEN 20
33 #define SERVER_SEND_STRING "im server"
34 
35 typedef struct DeathCbArg {
36     char *pkgName;
37     int32_t pid;
38     uint32_t cbId;
39 } DeathCbArg;
40 
41 // only support one client connected
42 typedef struct ClientSvc {
43     char name[PKG_NAME_LIMIT];
44     SvcIdentity clientSvc;
45     bool isConnect;
46 } ClientSvc;
47 
48 typedef struct {
49     enum DataType id;
50     int32_t (*func)(IpcIo *req, IpcIo *reply);
51 } ServerInvokeCmd;
52 
53 static ClientSvc g_clientSvc = {.isConnect = false};
54 
ClientDeathCb(void * args)55 static void ClientDeathCb(void *args)
56 {
57     if (args == NULL) {
58         RPC_LOG_INFO("[ipc_test_server] args NULL");
59         return;
60     }
61     DeathCbArg* deathArg = (DeathCbArg*)args;
62     RPC_LOG_INFO("[ipc_test_server] client dead, reset, pid=%d, pkgname=%s", deathArg->pid, deathArg->pkgName);
63     free(deathArg->pkgName);
64     free(deathArg);
65     ReleaseSvc(g_clientSvc.clientSvc);
66     memset_s(&g_clientSvc, sizeof(ClientSvc), 0, sizeof(ClientSvc));
67     g_clientSvc.isConnect = false;
68     return;
69 }
70 
AddDeathCb(const char * pkgName,size_t len)71 static void AddDeathCb(const char *pkgName, size_t len)
72 {
73     if (pkgName == NULL || len <= 0 || len > PKG_NAME_LIMIT) {
74         RPC_LOG_ERROR("[ipc_test_server] pkgName or len error");
75         return;
76     }
77     // DeathCbArg free in death callback
78     DeathCbArg *argStrcut = (DeathCbArg*)malloc(sizeof(DeathCbArg));
79     if (argStrcut == NULL) {
80         RPC_LOG_ERROR("[ipc_test_server] argStrcut molloc null");
81         return;
82     }
83 
84     char *tmpName = (char *)malloc(len + 1);
85     if (tmpName == NULL) {
86         RPC_LOG_ERROR("[ipc_test_server] tmpName molloc null");
87         free(argStrcut);
88         return;
89     }
90     if (strcpy_s(tmpName, len + 1, pkgName) != ERR_NONE) {
91         RPC_LOG_ERROR("[ipc_test_server] strcpy_s pkgName error");
92         free(tmpName);
93         free(argStrcut);
94         return;
95     }
96     argStrcut->pkgName = tmpName;
97     argStrcut->pid = GetCallingPid();
98     AddDeathRecipient(g_clientSvc.clientSvc, ClientDeathCb, argStrcut, &argStrcut->cbId);
99     return;
100 }
101 
RegisterOnService(IpcIo * req,IpcIo * reply)102 static int32_t RegisterOnService(IpcIo *req, IpcIo *reply)
103 {
104     RPC_LOG_INFO("[ipc_test_server] registe to service");
105     if (g_clientSvc.isConnect) {
106         RPC_LOG_INFO("[ipc_test_server] client already connected, pkgname=%s", g_clientSvc.name);
107         return ERR_NONE;
108     }
109 
110     // recv client pkgname and svc
111     size_t len = 0;
112     const char *pkgName = (const char*)ReadString(req, &len);
113     if ((pkgName == NULL) || (len == 0)) {
114         RPC_LOG_ERROR("[ipc_test_server] RegisterOnService read pkgname or len failed");
115         return ERR_FAILED;
116     }
117     SvcIdentity svc;
118     ReadRemoteObject(req, &svc);
119     RPC_LOG_ERROR("[ipc_test_server] recv pkgname from client, pkgname=%s", pkgName);
120 
121     // save on server
122     if (strncpy_s(g_clientSvc.name, PKG_NAME_LIMIT, pkgName, strlen(pkgName)) != ERR_NONE ||
123         memcpy_s(&g_clientSvc.clientSvc, sizeof(SvcIdentity), &svc, sizeof(SvcIdentity)) != ERR_NONE) {
124         RPC_LOG_ERROR("[ipc_test_server] g_clientSvc set value failed");
125         return ERR_FAILED;
126     }
127     g_clientSvc.isConnect = true;
128 
129     AddDeathCb(pkgName, len);
130     RPC_LOG_INFO("[ipc_test_server] registe to server end");
131     return ERR_NONE;
132 }
133 
134 
OnBoolReceived(IpcIo * req,IpcIo * reply)135 static int32_t OnBoolReceived(IpcIo *req, IpcIo *reply)
136 {
137     RPC_LOG_INFO("[ipc_test_server] RecvBool called");
138     if (req == NULL || reply == NULL) {
139         RPC_LOG_ERROR("[ipc_test_server] invalid param");
140         return ERR_INVALID_PARAM;
141     }
142     bool data;
143     ReadBool(req, &data);
144     // reply to client
145     bool replyData = true;
146     WriteBool(reply, replyData);
147 
148     RPC_LOG_INFO("[ipc_test_server] RecvBool success, recv=%s", data ? "true" : "false");
149     return ERR_NONE;
150 }
151 
OnInt8Received(IpcIo * req,IpcIo * reply)152 static int32_t OnInt8Received(IpcIo *req, IpcIo *reply)
153 {
154     RPC_LOG_INFO("[ipc_test_server] OnInt8Received called");
155     if (req == NULL || reply == NULL) {
156         RPC_LOG_ERROR("[ipc_test_server] invalid param");
157         return ERR_INVALID_PARAM;
158     }
159     int8_t data;
160     ReadInt8(req, &data);
161     // reply to client
162     int8_t replyData = SERVER_TEST_NUMBER;
163     WriteInt8(reply, replyData);
164 
165     RPC_LOG_INFO("[ipc_test_server] OnInt8Received success, recv=%" PRId8"", data);
166     return ERR_NONE;
167 }
168 
OnInt16Received(IpcIo * req,IpcIo * reply)169 static int32_t OnInt16Received(IpcIo *req, IpcIo *reply)
170 {
171     RPC_LOG_INFO("[ipc_test_server] OnInt16Received called");
172     if (req == NULL || reply == NULL) {
173         RPC_LOG_ERROR("[ipc_test_server] invalid param");
174         return ERR_INVALID_PARAM;
175     }
176     int16_t data;
177     ReadInt16(req, &data);
178     // reply to client
179     int16_t replyData = SERVER_TEST_NUMBER;
180     WriteInt16(reply, replyData);
181 
182     RPC_LOG_INFO("[ipc_test_server] OnInt16Received success, recv=%" PRId16 "", data);
183     return ERR_NONE;
184 }
185 
OnInt32Received(IpcIo * req,IpcIo * reply)186 static int32_t OnInt32Received(IpcIo *req, IpcIo *reply)
187 {
188     RPC_LOG_INFO("[ipc_test_server] OnInt32Received called");
189     if (req == NULL || reply == NULL) {
190         RPC_LOG_ERROR("[ipc_test_server] invalid param");
191         return ERR_INVALID_PARAM;
192     }
193     int32_t data;
194     ReadInt32(req, &data);
195     // reply to client
196     int32_t replyData = SERVER_TEST_NUMBER;
197     WriteInt32(reply, replyData);
198 
199     RPC_LOG_INFO("[ipc_test_server] OnInt32Received success, recv=%" PRId32 "", data);
200     return ERR_NONE;
201 }
202 
OnInt64Received(IpcIo * req,IpcIo * reply)203 static int32_t OnInt64Received(IpcIo *req, IpcIo *reply)
204 {
205     RPC_LOG_INFO("[ipc_test_server] OnInt64Received called");
206     if (req == NULL || reply == NULL) {
207         RPC_LOG_ERROR("[ipc_test_server] invalid param");
208         return ERR_INVALID_PARAM;
209     }
210     int64_t data;
211     ReadInt64(req, &data);
212     // reply to client
213     int64_t replyData = SERVER_TEST_NUMBER;
214     WriteInt64(reply, replyData);
215 
216     RPC_LOG_INFO("[ipc_test_server] OnInt64Received success, recv=%" PRId64 "", data);
217     return ERR_NONE;
218 }
219 
OnUint8Received(IpcIo * req,IpcIo * reply)220 static int32_t OnUint8Received(IpcIo *req, IpcIo *reply)
221 {
222     RPC_LOG_INFO("[ipc_test_server] OnUint8Received called");
223     if (req == NULL || reply == NULL) {
224         RPC_LOG_ERROR("[ipc_test_server] invalid param");
225         return ERR_INVALID_PARAM;
226     }
227     uint8_t data;
228     ReadUint8(req, &data);
229     // reply to client
230     uint8_t replyData = SERVER_TEST_NUMBER;
231     WriteUint8(reply, replyData);
232 
233     RPC_LOG_INFO("[ipc_test_server] OnUint8Received success, recv=%" PRIu8 "", data);
234     return ERR_NONE;
235 }
236 
OnUint16Received(IpcIo * req,IpcIo * reply)237 static int32_t OnUint16Received(IpcIo *req, IpcIo *reply)
238 {
239     RPC_LOG_INFO("[ipc_test_server] OnUint16Received called");
240     if (req == NULL || reply == NULL) {
241         RPC_LOG_ERROR("[ipc_test_server] invalid param");
242         return ERR_INVALID_PARAM;
243     }
244     uint16_t data;
245     ReadUint16(req, &data);
246     // reply to client
247     uint16_t replyData = SERVER_TEST_NUMBER;
248     WriteUint16(reply, replyData);
249 
250     RPC_LOG_INFO("[ipc_test_server] OnUint16Received success, recv=%" PRIu16 "", data);
251     return ERR_NONE;
252 }
253 
OnUint32Received(IpcIo * req,IpcIo * reply)254 static int32_t OnUint32Received(IpcIo *req, IpcIo *reply)
255 {
256     RPC_LOG_INFO("[ipc_test_server] OnUint32Received called");
257     if (req == NULL || reply == NULL) {
258         RPC_LOG_ERROR("[ipc_test_server] invalid param");
259         return ERR_INVALID_PARAM;
260     }
261     uint32_t data;
262     ReadUint32(req, &data);
263     // reply to client
264     uint32_t replyData = SERVER_TEST_NUMBER;
265     WriteUint32(reply, replyData);
266 
267     RPC_LOG_INFO("[ipc_test_server] OnUint32Received success, recv=%" PRIu32 "", data);
268     return ERR_NONE;
269 }
270 
OnUint64Received(IpcIo * req,IpcIo * reply)271 static int32_t OnUint64Received(IpcIo *req, IpcIo *reply)
272 {
273     RPC_LOG_INFO("[ipc_test_server] OnUint64Received called");
274     if (req == NULL || reply == NULL) {
275         RPC_LOG_ERROR("[ipc_test_server] invalid param");
276         return ERR_INVALID_PARAM;
277     }
278     uint64_t data;
279     ReadUint64(req, &data);
280     // reply to client
281     uint64_t replyData = SERVER_TEST_NUMBER;
282     WriteUint64(reply, replyData);
283 
284     RPC_LOG_INFO("[ipc_test_server] OnUint64Received success, recv=%" PRIu64 "", data);
285     return ERR_NONE;
286 }
287 
OnFloatReceived(IpcIo * req,IpcIo * reply)288 static int32_t OnFloatReceived(IpcIo *req, IpcIo *reply)
289 {
290     RPC_LOG_INFO("[ipc_test_server] OnFloatReceived called");
291     if (req == NULL || reply == NULL) {
292         RPC_LOG_ERROR("[ipc_test_server] invalid param");
293         return ERR_INVALID_PARAM;
294     }
295     float data;
296     ReadFloat(req, &data);
297     // reply to client
298     float replyData = (float)HALF_PI;
299     WriteFloat(reply, replyData);
300 
301     RPC_LOG_INFO("[ipc_test_server] OnFloatReceived success, recv=%f", data);
302     return ERR_NONE;
303 }
304 
OnDoubleReceived(IpcIo * req,IpcIo * reply)305 static int32_t OnDoubleReceived(IpcIo *req, IpcIo *reply)
306 {
307     RPC_LOG_INFO("[ipc_test_server] OnDoubleReceived called");
308     if (req == NULL || reply == NULL) {
309         RPC_LOG_ERROR("[ipc_test_server] invalid param");
310         return ERR_INVALID_PARAM;
311     }
312     double data;
313     ReadDouble(req, &data);
314     // reply to client
315     double replyData = HALF_PI;
316     WriteDouble(reply, replyData);
317 
318     RPC_LOG_INFO("[ipc_test_server] OnDoubleReceived success, recv=%f", data);
319     return ERR_NONE;
320 }
321 
OnInt8VectorReceived(IpcIo * req,IpcIo * reply)322 static int32_t OnInt8VectorReceived(IpcIo *req, IpcIo *reply)
323 {
324     RPC_LOG_INFO("[ipc_test_server] OnInt8VectorReceived called");
325     if (req == NULL || reply == NULL) {
326         RPC_LOG_ERROR("[ipc_test_server] invalid param");
327         return ERR_INVALID_PARAM;
328     }
329     int8_t *data = NULL;
330     size_t size = 0;
331     data = ReadInt8Vector(req, &size);
332     if (data == NULL || size <= 0) {
333         RPC_LOG_ERROR("[ipc_test_server] OnInt8VectorReceived error, vector is null or empty");
334         return ERR_FAILED;
335     }
336 
337     // reply to client
338     int8_t replyData[SERVER_VECTOR_LEN];
339     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
340         replyData[i] = SERVER_TEST_NUMBER + i;
341     }
342     WriteInt8Vector(reply, replyData, SERVER_VECTOR_LEN);
343 
344     RPC_LOG_INFO("[ipc_test_server] OnInt8VectorReceived called, size=%zu, start=%" PRId8 ", end=%" PRId8 "",
345         size, data[0], data[size - 1]);
346     return ERR_NONE;
347 }
348 
OnInt16VectorReceived(IpcIo * req,IpcIo * reply)349 static int32_t OnInt16VectorReceived(IpcIo *req, IpcIo *reply)
350 {
351     RPC_LOG_INFO("[ipc_test_server] OnInt16VectorReceived called");
352     if (req == NULL || reply == NULL) {
353         RPC_LOG_ERROR("[ipc_test_server] invalid param");
354         return ERR_INVALID_PARAM;
355     }
356     int16_t *data = NULL;
357     size_t size = 0;
358     data = ReadInt16Vector(req, &size);
359     if (data == NULL || size <= 0) {
360         RPC_LOG_ERROR("[ipc_test_server] OnInt16VectorReceived error, vector is null or empty");
361         return ERR_FAILED;
362     }
363 
364     // reply to client
365     int16_t replyData[SERVER_VECTOR_LEN];
366     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
367         replyData[i] = SERVER_TEST_NUMBER + i;
368     }
369     WriteInt16Vector(reply, replyData, SERVER_VECTOR_LEN);
370 
371     RPC_LOG_INFO("[ipc_test_server] OnInt16VectorReceived called, size=%zu, start=%" PRId16 ", end=%" PRId16 "",
372         size, data[0], data[size - 1]);
373     return ERR_NONE;
374 }
375 
OnInt32VectorReceived(IpcIo * req,IpcIo * reply)376 static int32_t OnInt32VectorReceived(IpcIo *req, IpcIo *reply)
377 {
378     RPC_LOG_INFO("[ipc_test_server] OnInt32VectorReceived called");
379     if (req == NULL || reply == NULL) {
380         RPC_LOG_ERROR("[ipc_test_server] invalid param");
381         return ERR_INVALID_PARAM;
382     }
383     int32_t *data = NULL;
384     size_t size = 0;
385     data = ReadInt32Vector(req, &size);
386     if (data == NULL || size <= 0) {
387         RPC_LOG_ERROR("[ipc_test_server] OnInt32VectorReceived error, vector is null or empty");
388         return ERR_FAILED;
389     }
390 
391     // reply to client
392     int32_t replyData[SERVER_VECTOR_LEN];
393     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
394         replyData[i] = SERVER_TEST_NUMBER + i;
395     }
396     WriteInt32Vector(reply, replyData, SERVER_VECTOR_LEN);
397 
398     RPC_LOG_INFO("[ipc_test_server] OnInt32VectorReceived called, size=%zu, start=%" PRId32 ", end=%" PRId32 "",
399         size, data[0], data[size - 1]);
400     return ERR_NONE;
401 }
402 
OnInt64VectorReceived(IpcIo * req,IpcIo * reply)403 static int32_t OnInt64VectorReceived(IpcIo *req, IpcIo *reply)
404 {
405     RPC_LOG_INFO("[ipc_test_server] OnInt64VectorReceived called");
406     if (req == NULL || reply == NULL) {
407         RPC_LOG_ERROR("[ipc_test_server] invalid param");
408         return ERR_INVALID_PARAM;
409     }
410     int64_t *data = NULL;
411     size_t size = 0;
412     data = ReadInt64Vector(req, &size);
413     if (data == NULL || size <= 0) {
414         RPC_LOG_ERROR("[ipc_test_server] OnInt64VectorReceived error, vector is null or empty");
415         return ERR_FAILED;
416     }
417 
418     // reply to client
419     int64_t replyData[SERVER_VECTOR_LEN];
420     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
421         replyData[i] = SERVER_TEST_NUMBER + i;
422     }
423     WriteInt64Vector(reply, replyData, SERVER_VECTOR_LEN);
424 
425     RPC_LOG_INFO("[ipc_test_server] OnInt64VectorReceived called, size=%zu, start=%" PRId64 ", end=%" PRId64 "",
426         size, data[0], data[size - 1]);
427     return ERR_NONE;
428 }
429 
OnUint8VectorReceived(IpcIo * req,IpcIo * reply)430 static int32_t OnUint8VectorReceived(IpcIo *req, IpcIo *reply)
431 {
432     RPC_LOG_INFO("[ipc_test_server] OnUint8VectorReceived called");
433     if (req == NULL || reply == NULL) {
434         RPC_LOG_ERROR("[ipc_test_server] invalid param");
435         return ERR_INVALID_PARAM;
436     }
437     uint8_t *data = NULL;
438     size_t size = 0;
439     data = ReadUInt8Vector(req, &size);
440     if (data == NULL || size <= 0) {
441         RPC_LOG_ERROR("[ipc_test_server] OnUint8VectorReceived error, vector is null or empty");
442         return ERR_FAILED;
443     }
444 
445     // reply to client
446     uint8_t replyData[SERVER_VECTOR_LEN];
447     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
448         replyData[i] = SERVER_TEST_NUMBER + i;
449     }
450     WriteUInt8Vector(reply, replyData, SERVER_VECTOR_LEN);
451 
452     RPC_LOG_INFO("[ipc_test_server] OnUint8VectorReceived called, size=%zu, start=%" PRIu8 ", end=%" PRIu8 "",
453         size, data[0], data[size - 1]);
454     return ERR_NONE;
455 }
456 
OnUint16VectorReceived(IpcIo * req,IpcIo * reply)457 static int32_t OnUint16VectorReceived(IpcIo *req, IpcIo *reply)
458 {
459     RPC_LOG_INFO("[ipc_test_server] OnUint16VectorReceived called");
460     if (req == NULL || reply == NULL) {
461         RPC_LOG_ERROR("[ipc_test_server] invalid param");
462         return ERR_INVALID_PARAM;
463     }
464     uint16_t *data = NULL;
465     size_t size = 0;
466     data = ReadUInt16Vector(req, &size);
467     if (data == NULL || size <= 0) {
468         RPC_LOG_ERROR("[ipc_test_server] OnUint16VectorReceived error, vector is null or empty");
469         return ERR_FAILED;
470     }
471 
472     // reply to client
473     uint16_t replyData[SERVER_VECTOR_LEN];
474     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
475         replyData[i] = SERVER_TEST_NUMBER + i;
476     }
477     WriteUInt16Vector(reply, replyData, SERVER_VECTOR_LEN);
478 
479     RPC_LOG_INFO("[ipc_test_server] OnUint16VectorReceived called, size=%zu, start=%" PRIu16 ", end=%" PRIu16 "",
480         size, data[0], data[size - 1]);
481     return ERR_NONE;
482 }
483 
OnUint32VectorReceived(IpcIo * req,IpcIo * reply)484 static int32_t OnUint32VectorReceived(IpcIo *req, IpcIo *reply)
485 {
486     RPC_LOG_INFO("[ipc_test_server] OnUint32VectorReceived called");
487     if (req == NULL || reply == NULL) {
488         RPC_LOG_ERROR("[ipc_test_server] invalid param");
489         return ERR_INVALID_PARAM;
490     }
491     uint32_t *data = NULL;
492     size_t size = 0;
493     data = ReadUInt32Vector(req, &size);
494     if (data == NULL || size <= 0) {
495         RPC_LOG_ERROR("[ipc_test_server] OnUint32VectorReceived error, vector is null or empty");
496         return ERR_FAILED;
497     }
498 
499     // reply to client
500     uint32_t replyData[SERVER_VECTOR_LEN];
501     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
502         replyData[i] = SERVER_TEST_NUMBER + i;
503     }
504     WriteUInt32Vector(reply, replyData, SERVER_VECTOR_LEN);
505 
506     RPC_LOG_INFO("[ipc_test_server] OnUint32VectorReceived called, size=%zu, start=%" PRIu32 ", end=%" PRIu32 "",
507         size, data[0], data[size - 1]);
508     return ERR_NONE;
509 }
510 
OnUint64VectorReceived(IpcIo * req,IpcIo * reply)511 static int32_t OnUint64VectorReceived(IpcIo *req, IpcIo *reply)
512 {
513     RPC_LOG_INFO("[ipc_test_server] OnUint64VectorReceived called");
514     if (req == NULL || reply == NULL) {
515         RPC_LOG_ERROR("[ipc_test_server] invalid param");
516         return ERR_INVALID_PARAM;
517     }
518     uint64_t *data = NULL;
519     size_t size = 0;
520     data = ReadUInt64Vector(req, &size);
521     if (data == NULL || size <= 0) {
522         RPC_LOG_ERROR("[ipc_test_server] OnUint64VectorReceived error, vector is null or empty");
523         return ERR_FAILED;
524     }
525 
526     // reply to client
527     uint64_t replyData[SERVER_VECTOR_LEN];
528     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
529         replyData[i] = SERVER_TEST_NUMBER + i;
530     }
531     WriteUInt64Vector(reply, replyData, SERVER_VECTOR_LEN);
532 
533     RPC_LOG_INFO("[ipc_test_server] OnUint64VectorReceived called, size=%zu, start=%" PRIu64 ", end=%" PRIu64 "",
534         size, data[0], data[size - 1]);
535     return ERR_NONE;
536 }
537 
OnFloatVectorReceived(IpcIo * req,IpcIo * reply)538 static int32_t OnFloatVectorReceived(IpcIo *req, IpcIo *reply)
539 {
540     RPC_LOG_INFO("[ipc_test_server] OnFloatVectorReceived called");
541     if (req == NULL || reply == NULL) {
542         RPC_LOG_ERROR("[ipc_test_server] invalid param");
543         return ERR_INVALID_PARAM;
544     }
545     float *data = NULL;
546     size_t size = 0;
547     data = ReadFloatVector(req, &size);
548     if (data == NULL || size <= 0) {
549         RPC_LOG_ERROR("[ipc_test_server] OnFloatVectorReceived error, vector is null or empty");
550         return ERR_FAILED;
551     }
552 
553     // reply to client
554     float replyData[SERVER_VECTOR_LEN];
555     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
556         replyData[i] = HALF_PI + i;
557     }
558     WriteFloatVector(reply, replyData, SERVER_VECTOR_LEN);
559 
560     RPC_LOG_INFO("[ipc_test_server] OnFloatVectorReceived called, size=%zu, start=%f, end=%f",
561         size, data[0], data[size - 1]);
562     return ERR_NONE;
563 }
564 
OnDoubleVectorReceived(IpcIo * req,IpcIo * reply)565 static int32_t OnDoubleVectorReceived(IpcIo *req, IpcIo *reply)
566 {
567     RPC_LOG_INFO("[ipc_test_server] OnDoubleVectorReceived called");
568     if (req == NULL || reply == NULL) {
569         RPC_LOG_ERROR("[ipc_test_server] invalid param");
570         return ERR_INVALID_PARAM;
571     }
572     double *data = NULL;
573     size_t size = 0;
574     data = ReadDoubleVector(req, &size);
575     if (data == NULL || size <= 0) {
576         RPC_LOG_ERROR("[ipc_test_server] OnDoubleVectorReceived error, vector is null or empty");
577         return ERR_FAILED;
578     }
579 
580     // reply to client
581     double replyData[SERVER_VECTOR_LEN];
582     for (int i = 0; i < SERVER_VECTOR_LEN; i++) {
583         replyData[i] = HALF_PI + i;
584     }
585     WriteDoubleVector(reply, replyData, SERVER_VECTOR_LEN);
586 
587     RPC_LOG_INFO("[ipc_test_server] OnDoubleVectorReceived called, size=%zu, start=%f, end=%f",
588         size, data[0], data[size - 1]);
589     return ERR_NONE;
590 }
591 
OnStringReceived(IpcIo * req,IpcIo * reply)592 static int32_t OnStringReceived(IpcIo *req, IpcIo *reply)
593 {
594     RPC_LOG_INFO("[ipc_test_server] OnStringReceived called");
595     if (req == NULL || reply == NULL) {
596         RPC_LOG_ERROR("[ipc_test_server] invalid param");
597         return ERR_INVALID_PARAM;
598     }
599     size_t len;
600     const char *data = (char *)ReadString(req, &len);
601     if (data == NULL || len == 0) {
602         RPC_LOG_ERROR("[ipc_test_server] OnStringReceived error");
603         return ERR_FAILED;
604     }
605 
606     // reply to client
607     WriteString(reply, (char *)SERVER_SEND_STRING);
608 
609     RPC_LOG_INFO("[ipc_test_server] OnStringReceived success, recv=%s", data);
610     return ERR_NONE;
611 }
612 
OnFileDescriptorReceived(IpcIo * req,IpcIo * reply)613 static int32_t OnFileDescriptorReceived(IpcIo *req, IpcIo *reply)
614 {
615     RPC_LOG_INFO("[ipc_test_server] OnFileDescriptorReceived called");
616     int fd = ReadFileDescriptor(req);
617     if (fd < 0) {
618         RPC_LOG_ERROR("[ipc_test_server] OnFileDescriptorReceived fd error");
619         return ERR_FAILED;
620     }
621 
622     RPC_LOG_INFO("[ipc_test_server] OnFileDescriptorReceived fd=%d", fd);
623     const char *data = (char *)SERVER_SEND_STRING;
624     ssize_t bytesWritten = write(fd, data, strnlen(data, MAX_IO_SIZE));
625     if (bytesWritten == -1) {
626         RPC_LOG_ERROR("[ipc_test_server] OnFileDescriptorReceived write error");
627         close(fd);
628         return ERR_FAILED;
629     }
630     close(fd);
631     RPC_LOG_INFO("[ipc_test_server] OnFileDescriptorReceived success");
632     return ERR_NONE;
633 }
634 
OnRawDataReceived(IpcIo * req,IpcIo * reply)635 static int32_t OnRawDataReceived(IpcIo *req, IpcIo *reply)
636 {
637     RPC_LOG_INFO("[ipc_test_server] OnRawDataReceived called");
638     if (req == NULL || reply == NULL) {
639         RPC_LOG_ERROR("[ipc_test_server] invalid param");
640         return ERR_INVALID_PARAM;
641     }
642     SvcIdentity *data = (SvcIdentity *)ReadRawData(req, sizeof(SvcIdentity));
643     if (data == NULL) {
644         RPC_LOG_ERROR("[ipc_test_server] OnRawDataReceived error, recv null");
645         return ERR_FAILED;
646     }
647 
648     // reply to client
649     WriteRawData(reply, data, sizeof(SvcIdentity));
650     RPC_LOG_INFO("[ipc_test_server] OnRawDataReceived success");
651     return ERR_NONE;
652 }
653 
OnBufferReceived(IpcIo * req,IpcIo * reply)654 static int32_t OnBufferReceived(IpcIo *req, IpcIo *reply)
655 {
656     RPC_LOG_INFO("[ipc_test_server] OnBufferReceived called");
657     if (req == NULL || reply == NULL) {
658         RPC_LOG_ERROR("[ipc_test_server] invalid param");
659         return ERR_INVALID_PARAM;
660     }
661     SvcIdentity *data = (SvcIdentity *)ReadBuffer(req, sizeof(SvcIdentity));
662     if (data == NULL) {
663         RPC_LOG_ERROR("[ipc_test_server] OnBufferReceived error, recv null");
664         return ERR_FAILED;
665     }
666 
667     // reply to client
668     WriteBuffer(reply, data, sizeof(SvcIdentity));
669     RPC_LOG_INFO("[ipc_test_server] OnBufferReceived success");
670     return ERR_NONE;
671 }
672 
673 static ServerInvokeCmd g_serverInvokeCmdTbl[] = {
674     { REGISTER_ON_SERVICE, RegisterOnService},
675     { BOOL_TYPE, OnBoolReceived },
676 
677     { INT8_TYPE, OnInt8Received },
678     { INT16_TYPE, OnInt16Received },
679     { INT32_TYPE, OnInt32Received },
680     { INT64_TYPE, OnInt64Received },
681     { UINT8_TYPE, OnUint8Received },
682     { UINT16_TYPE, OnUint16Received },
683     { UINT32_TYPE, OnUint32Received },
684     { UINT64_TYPE, OnUint64Received },
685     { FLOAT_TYPE, OnFloatReceived },
686     { DOUBLE_TYPE, OnDoubleReceived },
687 
688     { VECTOR_INT8_TYPE, OnInt8VectorReceived },
689     { VECTOR_INT16_TYPE, OnInt16VectorReceived },
690     { VECTOR_INT32_TYPE, OnInt32VectorReceived },
691     { VECTOR_INT64_TYPE, OnInt64VectorReceived },
692     { VECTOR_UINT8_TYPE, OnUint8VectorReceived },
693     { VECTOR_UINT16_TYPE, OnUint16VectorReceived },
694     { VECTOR_UINT32_TYPE, OnUint32VectorReceived },
695     { VECTOR_UINT64_TYPE, OnUint64VectorReceived },
696     { VECTOR_FLOAT_TYPE, OnFloatVectorReceived },
697     { VECTOR_DOUBLE_TYPE, OnDoubleVectorReceived },
698 
699     { CHAR_TYPE, OnStringReceived },
700     { FD_TYPE, OnFileDescriptorReceived },
701     { RAW_DATA_TYPE, OnRawDataReceived },
702     { BUFFER_TYPE, OnBufferReceived },
703 };
704 
DispatchInvoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)705 int32_t DispatchInvoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
706 {
707     RPC_LOG_INFO("[ipc_test_server] Dispatch recv funcId=%d", funcId);
708     for (uint32_t i = 0; i < sizeof(g_serverInvokeCmdTbl) / sizeof(ServerInvokeCmd); i++) {
709         if (funcId == g_serverInvokeCmdTbl[i].id) {
710             return g_serverInvokeCmdTbl[i].func(req, reply);
711         }
712     }
713     RPC_LOG_INFO("[ipc_test_server]not support funcId=%d", funcId);
714     return ERR_NONE;
715 }