• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <stdlib.h>
17 #include <string.h>
18 
19 #include "dbinder_service.h"
20 #include "ipc_process_skeleton.h"
21 #include "ipc_skeleton.h"
22 #include "rpc_errno.h"
23 #include "rpc_log.h"
24 #include "rpc_process_skeleton.h"
25 #include "rpc_trans.h"
26 #include "serializer.h"
27 
28 #define IPC_LENGTH 64
29 #define NUMBER_A 12
30 #define NUMBER_B 17
31 
32 enum {
33     OP_ADD = 1,
34     OP_SUB = 2,
35     OP_MULTI = 3,
36     OP_ADD_SERVICE = 4,
37     OP_DBINDER_CONNECT = 5,
38     OP_DBINDER_RECEIVED = 6,
39 };
40 
41 enum {
42     GET_REMOTE_SYSTEM_ABILITY_TRANSACTION = 3,
43     ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION = 4,
44 };
45 
46 static const int32_t g_saID = 16;
47 
ServerDead1(void)48 static void ServerDead1(void)
49 {
50     RPC_LOG_INFO("#### rpc server dead callback11 called");
51 }
52 
RpcClientTestOne(SvcIdentity sid,MessageOption option)53 static void RpcClientTestOne(SvcIdentity sid, MessageOption option)
54 {
55     IpcIo data2;
56     uint8_t tmpData2[IPC_LENGTH];
57     IpcIoInit(&data2, tmpData2, IPC_LENGTH, 0);
58     WriteInt32(&data2, NUMBER_A);
59     WriteInt32(&data2, NUMBER_B);
60 
61     IpcIo reply2;
62     uintptr_t ptr2 = 0;
63     int32_t ret = SendRequest(sid, OP_ADD, &data2, &reply2, option, &ptr2);
64     if (ret != ERR_NONE) {
65         RPC_LOG_ERROR("SendRequest OP_ADD failed, error = %d", ret);
66         FreeBuffer((void *)ptr2);
67         return;
68     }
69 
70     int32_t sum;
71     ReadInt32(&reply2, &sum);
72     RPC_LOG_INFO("%d + %d = %d", NUMBER_A, NUMBER_B, sum);
73     FreeBuffer((void *)ptr2);
74 }
75 
RpcClientTestTwo(SvcIdentity sid,MessageOption option)76 static void RpcClientTestTwo(SvcIdentity sid, MessageOption option)
77 {
78     IpcIo data3;
79     uint8_t tmpData3[IPC_LENGTH];
80     IpcIoInit(&data3, tmpData3, IPC_LENGTH, 0);
81     WriteInt32(&data3, NUMBER_A);
82     WriteInt32(&data3, NUMBER_B);
83 
84     IpcIo reply3;
85     uintptr_t ptr3 = 0;
86     int32_t ret = SendRequest(sid, OP_MULTI, &data3, &reply3, option, &ptr3);
87     if (ret != ERR_NONE) {
88         RPC_LOG_ERROR("SendRequest OP_MULTI failed, error = %d", ret);
89         FreeBuffer((void *)ptr3);
90         return;
91     }
92 
93     int32_t mutil;
94     ReadInt32(&reply3, &mutil);
95     RPC_LOG_INFO("%d * %d = %d", NUMBER_A, NUMBER_B, mutil);
96     FreeBuffer((void *)ptr3);
97 }
98 
main(int argc,char * argv[])99 int main(int argc, char *argv[])
100 {
101     RPC_LOG_INFO("Enter System Ability Client .... ");
102 
103     if (argc == 1) {
104         RPC_LOG_INFO("input deviceid please");
105         return -1;
106     }
107 
108     const char *deviceId = argv[1];
109     RPC_LOG_INFO("input deviceid is %s", deviceId);
110 
111     IpcIo data1;
112     uint8_t tmpData1[IPC_LENGTH];
113     IpcIoInit(&data1, tmpData1, IPC_LENGTH, 0);
114     WriteInt32(&data1, g_saID);
115     WriteString(&data1, deviceId);
116 
117     IpcIo reply1;
118     MessageOption option;
119     MessageOptionInit(&option);
120 
121     SvcIdentity target = {
122         .handle = 0
123     };
124 
125     RPC_LOG_INFO("get remote system ability from samgr.");
126     uintptr_t ptr = 0;
127     int32_t ret = SendRequest(target, GET_REMOTE_SYSTEM_ABILITY_TRANSACTION, &data1, &reply1, option, &ptr);
128     SvcIdentity sid;
129     ReadRemoteObject(&reply1, &sid);
130     RPC_LOG_INFO("call server add func server handle = %d.", sid.handle);
131     FreeBuffer((void *)ptr);
132 
133     uint32_t cbId1 = 0;
134     ret = AddDeathRecipient(sid, ServerDead1, NULL, &cbId1);
135     RPC_LOG_INFO("add death callback cbid1 = %d", ret);
136 
137     RpcClientTestOne(sid, option);
138     RpcClientTestTwo(sid, option);
139 
140     JoinWorkThread();
141 
142     return 0;
143 }