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