1 /*
2  * Copyright (c) 2021 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 <securec.h>
18 #include "dbinder_types.h"
19 #include "ipc_skeleton.h"
20 #include "rpc_errno.h"
21 #include "rpc_log.h"
22 #include "serializer.h"
23 
24 enum {
25     OP_ADD = 1,
26     OP_SUB = 2,
27     OP_MULTI = 3,
28     OP_ADD_SERVICE = 4,
29 };
30 
31 enum {
32     ADD_SYSTEM_ABILITY_TRANSACTION = 2,
33     GET_REMOTE_SYSTEM_ABILITY_TRANSACTION = 3,
34     ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION = 4,
35 };
36 
37 static const int32_t g_saID = 16;
38 static const uint32_t IPC_LENGTH = 128;
39 
RemoteRequestOne(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)40 static int32_t RemoteRequestOne(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
41 {
42     int32_t result = ERR_NONE;
43     RPC_LOG_INFO("server OnRemoteRequestOne called....");
44     switch (code) {
45         case OP_ADD: {
46             int32_t a;
47             ReadInt32(data, &a);
48             int32_t b;
49             ReadInt32(data, &b);
50             RPC_LOG_INFO("RemoteRequestOne add called a = %d, b = %d", a, b);
51             WriteInt32(reply, a + b);
52             break;
53         }
54         case OP_SUB: {
55             int32_t a;
56             ReadInt32(data, &a);
57             int32_t b;
58             ReadInt32(data, &b);
59             RPC_LOG_INFO("RemoteRequestOne sub called a = %d, b = %d", a, b);
60             WriteInt32(reply, a - b);
61             break;
62         }
63         case OP_MULTI: {
64             int32_t a;
65             ReadInt32(data, &a);
66             int32_t b;
67             ReadInt32(data, &b);
68             RPC_LOG_INFO("RemoteRequestOne mulit called a = %d, b = %d", a, b);
69             WriteInt32(reply, a * b);
70             break;
71         }
72         default:
73             RPC_LOG_ERROR("unknown code %u", code);
74             break;
75     }
76     return result;
77 }
78 
main(int argc,char * argv[])79 int main(int argc, char *argv[])
80 {
81     RPC_LOG_INFO("Enter System Ability Server .... ");
82 
83     IpcObjectStub objectStubOne = {
84         .func = RemoteRequestOne,
85         .isRemote = false
86     };
87 
88     SvcIdentity svcOne = {
89         .handle = -1,
90         .token  = (uintptr_t)&objectStubOne,
91         .cookie = (uintptr_t)&objectStubOne
92     };
93 
94     IpcIo data;
95     uint8_t tmpData1[IPC_LENGTH];
96     IpcIoInit(&data, tmpData1, IPC_LENGTH, 1);
97     WriteInt32(&data, g_saID);
98     WriteRemoteObject(&data, &svcOne);
99 
100     IpcIo reply;
101     MessageOption option;
102     MessageOptionInit(&option);
103 
104     SvcIdentity target = {
105         .handle = 0
106     };
107 
108     RPC_LOG_INFO("====== add ability one to samgr ======");
109     uintptr_t ptr = 0;
110     SendRequest(target, ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION, &data, &reply, option, &ptr);
111     int32_t ret;
112     ReadInt32(&reply, &ret);
113     RPC_LOG_INFO("send request one ret = %d .... ", ret);
114     FreeBuffer((void *)ptr);
115 
116     JoinWorkThread();
117     return -1;
118 }