• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DEFAULT_THREAD_STACK_SIZE 10240
31 #define TEST_DELAY_MILLISECONDS 20000
32 #define TEST_SERVER_DELAY_MILLISECONDS 4000
33 #define IPC_LENGTH 64
34 #define IPC_LENGTH_LONG 128
35 
36 enum {
37     OP_ADD = 1,
38     OP_SUB = 2,
39     OP_MULTI = 3,
40 };
41 
RemoteRequestOne(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)42 static int32_t RemoteRequestOne(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
43 {
44     int32_t result = ERR_NONE;
45     RPC_LOG_INFO("server OnRemoteRequestOne called....");
46     sleep(1);
47     switch (code) {
48         case OP_ADD: {
49             int32_t a;
50             ReadInt32(data, &a);
51             int32_t b;
52             ReadInt32(data, &b);
53             RPC_LOG_INFO("RemoteRequestOne add called a = %d, b = %d", a, b);
54             WriteInt32(reply, a + b);
55             break;
56         }
57         case OP_SUB: {
58             int32_t a;
59             ReadInt32(data, &a);
60             int32_t b;
61             ReadInt32(data, &b);
62             RPC_LOG_INFO("RemoteRequestOne sub called a = %d, b = %d", a, b);
63             WriteInt32(reply, a - b);
64             break;
65         }
66         case OP_MULTI: {
67             int32_t a;
68             ReadInt32(data, &a);
69             int32_t b;
70             ReadInt32(data, &b);
71             RPC_LOG_INFO("RemoteRequestOne mulit called a = %d, b = %d", a, b);
72             WriteInt32(reply, a * b);
73             break;
74         }
75         default:
76             RPC_LOG_ERROR("unknown code %d", code);
77             break;
78     }
79     return result;
80 }
81 
RpcServerMain(void)82 static void RpcServerMain(void)
83 {
84     osDelay(TEST_SERVER_DELAY_MILLISECONDS);
85     RPC_LOG_INFO("RpcServerMain start");
86 
87     IpcObjectStub *objectStubOne = (IpcObjectStub *)calloc(1, sizeof(IpcObjectStub));
88     if (objectStubOne == NULL) {
89         RPC_LOG_ERROR("objectStubOne calloc failed");
90         return;
91     }
92     objectStubOne->func = RemoteRequestOne;
93     objectStubOne->isRemote = false;
94 
95     printf("RpcServerMain func %x\n", objectStubOne->func);
96     sleep(1);
97 
98     IpcIo data;
99     uint8_t tmpData[IPC_LENGTH_LONG];
100     IpcIoInit(&data, tmpData, IPC_LENGTH_LONG, 0);
101     SvcIdentity svcOne = {
102         .handle = -1,
103         .token  = (uintptr_t)objectStubOne,
104         .cookie = (uintptr_t)objectStubOne
105     };
106     WriteUint32(&data, SAID);
107     WriteRemoteObject(&data, &svcOne);
108     data.bufferCur = data.bufferBase;
109     data.offsetsCur = data.offsetsBase;
110 
111     if (AddRemoteSystemAbility(&data) != ERR_NONE) {
112         RPC_LOG_INFO("AddRemoteSystemAbility failed");
113         return;
114     }
115 
116     RPC_LOG_INFO("JoinWorkThread start");
117 
118     return;
119 }
120 
RpcServerTest(void)121 static void RpcServerTest(void)
122 {
123     osDelay(TEST_DELAY_MILLISECONDS);
124     printf("[%s:%d]: %s\n", __FILE__, __LINE__, __func__);
125     printf("RpcServerTest\n");
126     printf("RemoteRequestOne %x\n", RemoteRequestOne);
127 
128     pthread_t threadId;
129     pthread_attr_t threadAttr;
130     int ret = pthread_attr_init(&threadAttr);
131     if (ret != 0) {
132         RPC_LOG_ERROR("pthread_attr_init failed %d", ret);
133         return ERR_FAILED;
134     }
135 
136     if (pthread_attr_setstacksize(&threadAttr, DEFAULT_THREAD_STACK_SIZE) != 0) {
137         RPC_LOG_ERROR("pthread_attr_setstacksize failed");
138         return ERR_FAILED;
139     }
140 
141     ret = pthread_create(&threadId, &threadAttr, RpcStartSamgr, NULL);
142     if (ret != 0) {
143         RPC_LOG_ERROR("pthread_create failed %d", ret);
144         return ERR_FAILED;
145     }
146     pthread_detach(threadId);
147 
148     pthread_t threadId2;
149     pthread_attr_t threadAttr2;
150     ret = pthread_attr_init(&threadAttr2);
151     if (ret != 0) {
152         RPC_LOG_ERROR("pthread_attr_init failed %d", ret);
153         return ERR_FAILED;
154     }
155 
156     if (pthread_attr_setstacksize(&threadAttr2, DEFAULT_THREAD_STACK_SIZE) != 0) {
157         RPC_LOG_ERROR("pthread_attr_setstacksize failed");
158         return ERR_FAILED;
159     }
160 
161     ret = pthread_create(&threadId2, &threadAttr2, RpcServerMain, NULL);
162     if (ret != 0) {
163         RPC_LOG_ERROR("pthread_create failed %d", ret);
164         return ERR_FAILED;
165     }
166     pthread_detach(threadId2);
167 }
168 
169 APP_FEATURE_INIT(RpcServerTest);