1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 <stdlib.h>
17 #include <string.h>
18 #include <stdint.h>
19 #include <pthread.h>
20
21 #include "cmsis_os2.h"
22 #include "ohos_init.h"
23 #include "rpc_mini_samgr.h"
24 #include "ipc_skeleton.h"
25 #include "serializer.h"
26 #include "rpc_log.h"
27 #include "rpc_errno.h"
28
29 #define SAID 16
30 #define DEVICEID "0.0.0.0"
31 #define DEFAULT_THREAD_STACK_SIZE 10240
32 #define TEST_DELAY_MILLISECONDS 20000
33 #define TEST_CLIENT_DELAY_MILLISECONDS 10000
34 #define NUMBER_A 12
35 #define NUMBER_B 17
36
37 #define IPC_LENGTH 64
38 #define IPC_LENGTH_LONG 128
39
40 enum {
41 OP_ADD = 1,
42 OP_SUB = 2,
43 OP_MULTI = 3,
44 };
45
RpcClientMain(void)46 static void RpcClientMain(void)
47 {
48 osDelay(TEST_CLIENT_DELAY_MILLISECONDS);
49 RPC_LOG_INFO("RpcClientMain start");
50
51 IpcIo reply;
52 uint8_t tmpData[IPC_LENGTH_LONG];
53 IpcIoInit(&reply, tmpData, IPC_LENGTH_LONG, 0);
54 if (GetRemoteSystemAbility(SAID, DEVICEID, &reply) != ERR_NONE) {
55 RPC_LOG_INFO("GetRemoteSystemAbility failed");
56 return;
57 }
58
59 SvcIdentity svc;
60 ReadRemoteObject(&reply, &svc);
61
62 IpcIo data2;
63 uint8_t tmpData2[IPC_LENGTH];
64 IpcIoInit(&data2, tmpData2, IPC_LENGTH, 0);
65 WriteInt32(&data2, NUMBER_A);
66 WriteInt32(&data2, NUMBER_B);
67 RPC_LOG_INFO("RpcClientMain serializer");
68
69 IpcIo reply2;
70 uintptr_t ptr2 = 0;
71 MessageOption option;
72 MessageOptionInit(&option);
73 int32_t ret = SendRequest(svc, OP_ADD, &data2, &reply2, option, &ptr2);
74 if (ret != ERR_NONE) {
75 RPC_LOG_INFO("OP_ADD failed");
76 FreeBuffer((void *)ptr2);
77 return;
78 }
79 RPC_LOG_INFO("RpcClientMain SendRequest");
80
81 int32_t sum;
82 ReadInt32(&reply2, &sum);
83 RPC_LOG_INFO("%d + %d = %d", NUMBER_A, NUMBER_B, sum);
84 FreeBuffer((void *)ptr2);
85
86 return;
87 }
88
RpcClientTest(void)89 static void RpcClientTest(void)
90 {
91 osDelay(TEST_DELAY_MILLISECONDS);
92 printf("[%s:%d]: %s\n", __FILE__, __LINE__, __func__);
93 printf("RpcClientTest\n");
94
95 pthread_t threadId;
96 pthread_attr_t threadAttr;
97 int ret = pthread_attr_init(&threadAttr);
98 if (ret != 0) {
99 RPC_LOG_ERROR("pthread_attr_init failed %d", ret);
100 return ERR_FAILED;
101 }
102
103 if (pthread_attr_setstacksize(&threadAttr, DEFAULT_THREAD_STACK_SIZE) != 0) {
104 RPC_LOG_ERROR("pthread_attr_setstacksize failed");
105 return ERR_FAILED;
106 }
107
108 ret = pthread_create(&threadId, &threadAttr, RpcStartSamgr, NULL);
109 if (ret != 0) {
110 RPC_LOG_ERROR("pthread_create failed %d", ret);
111 return ERR_FAILED;
112 }
113 pthread_detach(threadId);
114
115 pthread_t threadId2;
116 pthread_attr_t threadAttr2;
117 ret = pthread_attr_init(&threadAttr2);
118 if (ret != 0) {
119 RPC_LOG_ERROR("pthread_attr_init failed %d", ret);
120 return ERR_FAILED;
121 }
122
123 if (pthread_attr_setstacksize(&threadAttr2, DEFAULT_THREAD_STACK_SIZE) != 0) {
124 RPC_LOG_ERROR("pthread_attr_setstacksize failed");
125 return ERR_FAILED;
126 }
127
128 ret = pthread_create(&threadId2, &threadAttr2, RpcClientMain, NULL);
129 if (ret != 0) {
130 RPC_LOG_ERROR("pthread_create failed %d", ret);
131 return ERR_FAILED;
132 }
133 pthread_detach(threadId2);
134 }
135
136 APP_FEATURE_INIT(RpcClientTest);