1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "it_test_liteipc.h"
33 #include "signal.h"
34 #include "sys/wait.h"
35
36 #include "unistd.h"
37 #include "liteipc.h"
38 #include "stdlib.h"
39 #include "stdio.h"
40 #include "string.h"
41 #include "sys/ioctl.h"
42 #include "sys/time.h"
43
44 #include "smgr_demo.h"
45
46 ServiceName g_serviceNameMap[MAX_SREVICE_NUM];
47 BOOL g_cmsRunningFlag = FALSE;
48
InitCms()49 static void InitCms()
50 {
51 memset(g_serviceNameMap, 0, sizeof(g_serviceNameMap));
52 }
53
SetCms(int fd)54 uint32_t SetCms(int fd)
55 {
56 return ioctl(fd, IPC_SET_CMS, 200);
57 }
58
SendReply(int fd,IpcMsg * dataIn,uint32_t result,uint32_t serviceHandle)59 void SendReply(int fd, IpcMsg *dataIn, uint32_t result, uint32_t serviceHandle)
60 {
61 IpcContent data1;
62 IpcMsg dataOut;
63 unsigned int ret;
64 uint32_t ptr[2];
65
66 data1.flag = SEND | BUFF_FREE;
67 data1.buffToFree = dataIn;
68 data1.outMsg = &dataOut;
69 memset(data1.outMsg, 0, sizeof(IpcMsg));
70 data1.outMsg->type = MT_REPLY;
71 data1.outMsg->target.handle = dataIn->taskID;
72 data1.outMsg->target.token = dataIn->target.token;
73 data1.outMsg->code = dataIn->code;
74 #if (USE_TIMESTAMP == YES)
75 data1.outMsg->timestamp = dataIn->timestamp;
76 #endif
77 ptr[0] = result;
78 ptr[1] = serviceHandle;
79 data1.outMsg->dataSz = 8;
80 data1.outMsg->data = ptr;
81 ret = ioctl(fd, IPC_SEND_RECV_MSG, &data1);
82 if (ret) {
83 printf("SendReply failed\n");
84 }
85 }
86
FreeBuffer(int fd,IpcMsg * dataIn)87 void FreeBuffer(int fd, IpcMsg *dataIn)
88 {
89 IpcContent data1;
90 unsigned int ret;
91 data1.flag = BUFF_FREE;
92 data1.buffToFree = dataIn;
93 ret = ioctl(fd, IPC_SEND_RECV_MSG, &data1);
94 if (ret) {
95 printf("FreeBuffer failed\n");
96 }
97 }
98
SendCmsCmd(int fd,CmsCmdContent * content)99 static uint32_t SendCmsCmd(int fd, CmsCmdContent *content)
100 {
101 unsigned int ret;
102 ret = ioctl(fd, IPC_CMS_CMD, content);
103 if (ret) {
104 printf("SendCmsCmd failed\n");
105 }
106 return ret;
107 }
108
RegService(int fd,char * serviceName,uint32_t nameLen,uint32_t * serviceHandle)109 uint32_t RegService(int fd, char *serviceName, uint32_t nameLen, uint32_t *serviceHandle)
110 {
111 IpcContent data1;
112 IpcMsg dataIn;
113 IpcMsg dataOut;
114 uint32_t ret;
115 uint32_t *ptr = nullptr;
116 ServiceName name;
117
118 if (nameLen > NAME_LEN_MAX) {
119 return -1;
120 }
121 memcpy(name.serviceName, serviceName, nameLen);
122 name.nameLen = nameLen;
123
124 data1.flag = SEND | RECV;
125 data1.outMsg = &dataOut;
126 memset(data1.outMsg, 0, sizeof(IpcMsg));
127 data1.outMsg->type = MT_REQUEST;
128 data1.outMsg->target.handle = 0;
129 data1.outMsg->code = REG_CODE;
130 data1.outMsg->dataSz = sizeof(ServiceName);
131 data1.outMsg->data = &name;
132
133 ret = ioctl(fd, IPC_SEND_RECV_MSG, &data1);
134 if (ret != 0) {
135 printf("RegService failed\n");
136 return ret;
137 }
138 ptr = (uint32_t*)(data1.inMsg->data);
139 *serviceHandle = ptr[1];
140 FreeBuffer(fd, data1.inMsg);
141 return ptr[0];
142 }
143
GetService(int fd,char * serviceName,uint32_t nameLen,uint32_t * serviceHandle)144 uint32_t GetService(int fd, char *serviceName, uint32_t nameLen, uint32_t *serviceHandle)
145 {
146 IpcContent data1;
147 IpcMsg dataIn;
148 IpcMsg dataOut;
149 uint32_t ret;
150 uint32_t *ptr = nullptr;
151 ServiceName name;
152
153 if (nameLen > NAME_LEN_MAX) {
154 return -1;
155 }
156 memcpy(name.serviceName, serviceName, nameLen);
157 name.nameLen = nameLen;
158
159 data1.flag = SEND | RECV;
160 data1.outMsg = &dataOut;
161 memset(data1.outMsg, 0, sizeof(IpcMsg));
162 data1.outMsg->type = MT_REQUEST;
163 data1.outMsg->target.handle = 0;
164 data1.outMsg->code = GET_CODE;
165 data1.outMsg->dataSz = sizeof(ServiceName);
166 data1.outMsg->data = &name;
167
168 ret = ioctl(fd, IPC_SEND_RECV_MSG, &data1);
169 if (ret != 0) {
170 return ret;
171 }
172 ptr = (uint32_t*)(data1.inMsg->data);
173 *serviceHandle = ptr[1];
174 FreeBuffer(fd, data1.inMsg);
175 return ptr[0];
176 }
177
HandleServiceRegAndGet(int fd,IpcMsg * data)178 static void HandleServiceRegAndGet(int fd, IpcMsg *data)
179 {
180 uint32_t ret, i;
181
182 if (data->code == STOP_CODE) {
183 g_cmsRunningFlag = FALSE;
184 return;
185 }
186
187 ServiceName *info = (ServiceName*)(data->data);
188 CmsCmdContent content;
189 if ((info->nameLen == 0) || (info->serviceName == NULL)) {
190 goto ERROR_EXIT;
191 }
192 for (i = 0; i < MAX_SREVICE_NUM; i++) {
193 if (g_serviceNameMap[i].serviceName != NULL && g_serviceNameMap[i].nameLen == info->nameLen) {
194 if(memcmp(g_serviceNameMap[i].serviceName, info->serviceName, info->nameLen) == 0) {
195 break;
196 }
197 }
198 }
199 printf("recieve service request, code:%d, service name:%s\n", data->code, info->serviceName);
200 switch (data->code) {
201 case REG_CODE:
202 if (i == MAX_SREVICE_NUM) {
203 content.cmd = CMS_GEN_HANDLE;
204 content.taskID = data->taskID;
205 ret = SendCmsCmd(fd, &content);
206 if (ret) {
207 goto ERROR_EXIT;
208 }
209 if (g_serviceNameMap[content.serviceHandle].serviceName != NULL && g_serviceNameMap[content.serviceHandle].nameLen == info->nameLen) {
210 printf("the task has already a service named:%s\n", g_serviceNameMap[content.serviceHandle].serviceName);
211 goto ERROR_REG;
212 } else {
213 memcpy(g_serviceNameMap[content.serviceHandle].serviceName, info->serviceName, info->nameLen);
214 g_serviceNameMap[content.serviceHandle].nameLen = info->nameLen;
215 SendReply(fd, data, 0, content.serviceHandle);
216 }
217 }else {
218 printf("this service already registed\n");
219 goto ERROR_EXIT;
220 }
221 break;
222 case GET_CODE:
223 if (i == MAX_SREVICE_NUM) {
224 goto ERROR_EXIT;
225 }else {
226 content.cmd = CMS_ADD_ACCESS;
227 content.taskID = data->taskID;
228 content.serviceHandle = i;
229 SendCmsCmd(fd, &content);
230 SendReply(fd, data, 0, i);
231 }
232 break;
233 default:
234 break;
235 }
236 return;
237 ERROR_REG:
238 content.cmd = CMS_REMOVE_HANDLE;
239 SendCmsCmd(fd, &content);
240 ERROR_EXIT:
241 SendReply(fd, data, -1, 0);
242 }
243
CmsLoop(int fd)244 static uint32_t CmsLoop(int fd)
245 {
246 IpcContent data1;
247 IpcMsg dataIn;
248 IpcMsg dataOut;
249 uint32_t ret;
250 g_cmsRunningFlag = TRUE;
251 while (g_cmsRunningFlag == TRUE) {
252 data1.flag = RECV;
253 ret = ioctl(fd, IPC_SEND_RECV_MSG, &data1);
254 if (ret != 0) {
255 printf("bad request!\n");
256 continue;
257 }
258 switch (data1.inMsg->type) {
259 case MT_REQUEST:
260 HandleServiceRegAndGet(fd, data1.inMsg);
261 break;
262 default:
263 printf("request not support:%d!\n", data1.inMsg->type);
264 FreeBuffer(fd, data1.inMsg);
265 break;
266 }
267 }
268 }
269
StartCms(int fd)270 void StartCms(int fd)
271 {
272 InitCms();
273 CmsLoop(fd);
274 }
275
StopCms(int fd)276 void StopCms(int fd)
277 {
278 IpcContent data1;
279 IpcMsg dataOut;
280 int ret;
281
282 data1.flag = SEND;
283 data1.outMsg = &dataOut;
284 memset(data1.outMsg, 0, sizeof(IpcMsg));
285 data1.outMsg->type = MT_REQUEST;
286 data1.outMsg->target.handle = 0;
287 data1.outMsg->code = STOP_CODE;
288 data1.outMsg->dataSz = 0;
289 data1.outMsg->data = 0;
290
291 ret = ioctl(fd, IPC_SEND_RECV_MSG, &data1);
292 if (ret != 0) {
293 printf("StopCms failed ioctl ret:%d!\n", ret);
294 }
295 }
296
297