1 /*
2 * Copyright (c) 2023 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 <stdlib.h>
17 #include <ohos_init.h>
18
19 #include "ipc_proxy.h"
20 #include "ipc_skeleton.h"
21 #include "iproxy_client.h"
22 #include "rpc_errno.h"
23 #include "rpc_log.h"
24 #include "serializer.h"
25 #include "samgr_lite.h"
26
27 #define WAIT_SERVER_READY_INTERVAL_COUNT 50
28
29 static IClientProxy *g_serverProxy = NULL;
30
ServerDeadCallback(void * arg)31 static void ServerDeadCallback(void *arg)
32 {
33 RPC_LOG_INFO("====== server dead ServerDeadCallback called ======");
34 }
35
AddDeathCallback()36 static void AddDeathCallback()
37 {
38 IpcIo reply;
39 SvcIdentity sid;
40 uint32_t cbId = 0;
41
42 SvcIdentity svcIdentity = SAMGR_GetRemoteIdentity(IPC_TEST_SERVICE, NULL);
43 int32_t ret = AddDeathRecipient(svcIdentity, ServerDeadCallback, NULL, &cbId);
44 if (ret != ERR_NONE) {
45 RPC_LOG_ERROR("[ipc_test_client] AddDeathRecipient failed, ret:[%d]", ret);
46 return;
47 }
48 RPC_LOG_INFO("[ipc_test_client] add death callback success, cbId = [%u]", cbId);
49 }
50
ServerAddCallback(IOwner owner,int code,IpcIo * reply)51 static int ServerAddCallback(IOwner owner, int code, IpcIo *reply)
52 {
53 RPC_LOG_INFO("ServerAddCallback start.");
54 if (code != 0) {
55 RPC_LOG_ERROR("server add callback error[%d].", code);
56 return -1;
57 }
58
59 ReadInt32(reply, (int *)owner);
60 int tmpSum = OP_A + OP_B;
61 RPC_LOG_INFO("ServerAddCallback return[%d].", *(int32_t*)owner);
62 EXPECT_EQ(*(int32_t*)owner, tmpSum);
63 return 0;
64 }
65
CallServerAdd(void)66 static void CallServerAdd(void)
67 {
68 RPC_LOG_INFO("====== CallServerAdd start ======");
69 IpcIo data;
70 uint8_t dataAdd[IPC_MAX_SIZE];
71 IpcIoInit(&data, dataAdd, IPC_MAX_SIZE, 0);
72 WriteInt32(&data, OP_A);
73 WriteInt32(&data, OP_B);
74
75 int32_t ret = -1;
76 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_ADD, &data, &ret, ServerAddCallback);
77 RPC_LOG_INFO("SERVER_OP_ADD callback ret=[%d] ans=[%d]", ret, ans);
78 if (ans != 0) {
79 RPC_LOG_ERROR("SERVER_OP_ADD callback ret [%d]", ret);
80 }
81
82 RPC_LOG_INFO("====== CallServerAdd end ======");
83 }
84
ServerMultiCallback(IOwner owner,int code,IpcIo * reply)85 static int ServerMultiCallback(IOwner owner, int code, IpcIo *reply)
86 {
87 if (code != 0) {
88 RPC_LOG_ERROR("server multi callback error[%d].", code);
89 return -1;
90 }
91
92 ReadInt32(reply, (int *)owner);
93 int tmpMulti = OP_A * OP_B;
94 RPC_LOG_INFO("ServerMultiCallback return[%d].", *(int32_t*)owner);
95 EXPECT_EQ(*(int32_t*)owner, tmpMulti);
96 return 0;
97 }
98
CallServerMulti(void)99 static void CallServerMulti(void)
100 {
101 RPC_LOG_INFO("====== call serverone OP_MULTI start======");
102 IpcIo data;
103 uint8_t dataMulti[IPC_MAX_SIZE];
104 IpcIoInit(&data, dataMulti, IPC_MAX_SIZE, 0);
105 WriteInt32(&data, OP_A);
106 WriteInt32(&data, OP_B);
107 int ret = -1;
108 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_MULTI, &data, &ret, ServerMultiCallback);
109 if (ans != 0) {
110 RPC_LOG_ERROR("SERVER_OP_MULTI callback ret [%d]", ret);
111 }
112
113 RPC_LOG_INFO("====== call serverone OP_MULTI end======");
114 }
115
ServerSubCallback(IOwner owner,int code,IpcIo * reply)116 static int ServerSubCallback(IOwner owner, int code, IpcIo *reply)
117 {
118 if (code != 0) {
119 RPC_LOG_ERROR("server multi callback error[%d].", code);
120 return -1;
121 }
122
123 ReadInt32(reply, (int *)owner);
124 int tmpMulti = OP_A - OP_B;
125 RPC_LOG_INFO("ServerSubCallback return[%d].", *(int32_t*)owner);
126 EXPECT_EQ(*(int32_t*)owner, tmpMulti);
127 return 0;
128 }
129
CallServerSub(void)130 static void CallServerSub(void)
131 {
132 RPC_LOG_INFO("====== call serverone OP_SUB start======");
133 IpcIo data;
134 uint8_t dataSub[IPC_MAX_SIZE];
135 IpcIoInit(&data, dataSub, IPC_MAX_SIZE, 0);
136 WriteInt32(&data, OP_A);
137 WriteInt32(&data, OP_B);
138 int ret = -1;
139 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SUB, &data, &ret, ServerSubCallback);
140 if (ans != 0) {
141 RPC_LOG_INFO("SERVER_OP_SUB callback ret [%d]", ret);
142 }
143 RPC_LOG_INFO("====== call serverone OP_SUB end======");
144 }
145
GetServerProxy(void)146 static IClientProxy *GetServerProxy(void)
147 {
148 IClientProxy *clientProxy = NULL;
149
150 RPC_LOG_INFO("[ipc_test_client] start get client proxy");
151 int32_t proxyInitCount = 0;
152 while (clientProxy == NULL) {
153 proxyInitCount++;
154 if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
155 RPC_LOG_ERROR("[ipc_test_client] get server proxy error");
156 return NULL;
157 }
158 IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(IPC_TEST_SERVICE);
159 if (iUnknown == NULL) {
160 RPC_LOG_ERROR("iUnknown is null");
161 sleep(1);
162 continue;
163 }
164 RPC_LOG_INFO("[ipc_test_client] GetDefaultFeatureApi success");
165
166 int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&clientProxy);
167 if (ret != EC_SUCCESS || clientProxy == NULL) {
168 RPC_LOG_ERROR("QueryInterface failed [%d]", ret);
169 sleep(1);
170 continue;
171 }
172 }
173
174 RPC_LOG_INFO("[ipc_test_client] get client proxy ok");
175 return clientProxy;
176 }
177
HOS_SystemInit(void)178 static void __attribute__((weak)) HOS_SystemInit(void)
179 {
180 SAMGR_Bootstrap();
181 return;
182 }
183
main(int argc,char * argv[])184 int main(int argc, char *argv[])
185 {
186 RPC_LOG_INFO("[ipc_test_client] Enter System Ability Client");
187 HOS_SystemInit();
188 RPC_LOG_INFO("[ipc_test_client] SystemInit end");
189
190 RPC_LOG_INFO("[ipc_test_client] GetServerProxy start");
191 g_serverProxy = GetServerProxy();
192 if (g_serverProxy == NULL) {
193 RPC_LOG_ERROR("get ipc client proxy failed");
194 return -1;
195 }
196 RPC_LOG_INFO("[ipc_test_client] GetServerProxy end");
197
198 AddDeathCallback();
199 CallServerAdd();
200 CallServerMulti();
201 CallServerSub();
202
203 while (1) {
204 pause();
205 }
206
207 return -1;
208 }