1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "message_router.h"
10 #include "hdf_base.h"
11 #ifdef USERSPACE_CLIENT_SUPPORT
12 #include <signal.h>
13 #include <unistd.h>
14 #endif
15 #include "utils/hdf_log.h"
16 #include "osal/osal_mutex.h"
17 #include "securec.h"
18 #include "message_router_inner.h"
19 #include "message_dispatcher.h"
20
21 #ifdef USERSPACE_CLIENT_SUPPORT
22 #define HDF_LOG_TAG UMsgEngine
23 #else
24 #define HDF_LOG_TAG KMsgEngine
25 #endif
26
27 #ifndef UINT8_MAX
28 #define UINT8_MAX 255
29 #endif
30
31 #if MESSAGE_ENGINE_MAX_DISPATCHER > UINT8_MAX
32 #error Max MESSAGE_ENGINE_MAX_DISPATCHER is UINT8_MAX
33 #endif
34
35 IMPLEMENT_SHARED_OBJ(MessageNode);
36 IMPLEMENT_SHARED_OBJ(RemoteService);
37
38 typedef struct {
39 uint8_t nodeIndex;
40 DispatcherId dispatcherId;
41 RemoteService *remoteService;
42 } ServiceInfo;
43
44 #define MAX_NODE_COUNT 2
45
46 OSAL_DECLARE_MUTEX(g_routerMutex) = {
47 .realMutex = NULL
48 };
49
50 static ServiceInfo g_servicesIndex[MESSAGE_ENGINE_MAX_SERVICE] = {0};
51
52 static MessageNode *g_messageNodes[MAX_NODE_COUNT] = { 0, 0};
53
54 MessageDispatcher *g_dispatchers[MESSAGE_ENGINE_MAX_DISPATCHER] = {0};
55
56 static uint8_t g_routerStatus = ME_STATUS_STOPPED;
57
ReleaseRemoteService(RemoteService * service)58 static void ReleaseRemoteService(RemoteService *service)
59 {
60 if (service == NULL) {
61 return;
62 }
63 if (service->Shutdown != NULL) {
64 service->Shutdown(service);
65 }
66 if (service->Disref != NULL) {
67 service->Disref(service);
68 }
69 }
70
RefDispatcherInner(const DispatcherId dispatcherId,bool requireLock)71 static MessageDispatcher *RefDispatcherInner(const DispatcherId dispatcherId, bool requireLock)
72 {
73 MessageDispatcher *result = NULL;
74 if (dispatcherId >= MESSAGE_ENGINE_MAX_DISPATCHER) {
75 HDF_LOGE("%s:Input ID is too big.input=%u", __func__, dispatcherId);
76 return NULL;
77 }
78
79 if (requireLock) {
80 HDF_STATUS status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
81 if (status != HDF_SUCCESS) {
82 HDF_LOGE("Unable to get lock!status=%d", status);
83 return NULL;
84 }
85 }
86
87 do {
88 if (g_dispatchers[dispatcherId] == NULL) {
89 break;
90 }
91
92 if (g_dispatchers[dispatcherId]->Ref == NULL) {
93 break;
94 }
95 result = g_dispatchers[dispatcherId]->Ref(g_dispatchers[dispatcherId]);
96 } while (false);
97
98 if (requireLock) {
99 HDF_STATUS status = OsalMutexUnlock(&g_routerMutex);
100 if (status != HDF_SUCCESS) {
101 HDF_LOGE("Unable to unlock!status=%d", status);
102 }
103 }
104 return result;
105 }
106
RegDispatcher(DispatcherId dispatcherId,MessageDispatcher * dispatcher)107 static ErrorCode RegDispatcher(DispatcherId dispatcherId, MessageDispatcher *dispatcher)
108 {
109 HDF_STATUS status;
110 ErrorCode errCode;
111 if (dispatcherId >= MESSAGE_ENGINE_MAX_DISPATCHER) {
112 HDF_LOGE("%s:dispatcher id is too big!id=%u", __func__, dispatcherId);
113 return ME_ERROR_PARA_WRONG;
114 }
115
116 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
117 if (status != HDF_SUCCESS) {
118 HDF_LOGE("Unable to get lock!status=%d", status);
119 return ME_ERROR_OPER_MUTEX_FAILED;
120 }
121 errCode = ME_SUCCESS;
122 do {
123 if (g_routerStatus != ME_STATUS_RUNNING) {
124 errCode = ME_ERROR_WRONG_STATUS;
125 break;
126 }
127 if (g_dispatchers[dispatcherId] != NULL) {
128 HDF_LOGE("%s:DispatcherId conflict!ID=%u", __func__, dispatcherId);
129 errCode = ME_ERROR_DISPATCHERID_CONFLICT;
130 } else {
131 g_dispatchers[dispatcherId] = dispatcher;
132 }
133 } while (false);
134
135 status = OsalMutexUnlock(&g_routerMutex);
136 if (status != HDF_SUCCESS) {
137 HDF_LOGE("Unable to unlock!status=%d", status);
138 }
139 return errCode;
140 }
141
AddDispatcher(DispatcherConfig * config)142 ErrorCode AddDispatcher(DispatcherConfig *config)
143 {
144 ErrorCode errCode;
145 MessageDispatcher *dispatcher = NULL;
146 if (config == NULL) {
147 return ME_ERROR_NULL_PTR;
148 }
149 errCode = CreateLocalDispatcher(&dispatcher, config);
150 if (errCode != ME_SUCCESS) {
151 return errCode;
152 }
153 if (dispatcher == NULL) {
154 HDF_LOGE("%s:CreateDispatcher return NULL!", __func__);
155 return ME_ERROR_NULL_PTR;
156 }
157 do {
158 if (dispatcher->Start != NULL) {
159 errCode = dispatcher->Start(dispatcher);
160 if (errCode != ME_SUCCESS) {
161 HDF_LOGE("%s:Start dispatcher %u failed!errCode=%d", __func__, config->dispatcherId, errCode);
162 break;
163 }
164 }
165 errCode = RegDispatcher(config->dispatcherId, dispatcher);
166 } while (false);
167
168 if (errCode != ME_SUCCESS && dispatcher->Shutdown != NULL) {
169 dispatcher->Shutdown(dispatcher);
170 }
171 if (errCode != ME_SUCCESS && dispatcher->Disref != NULL) {
172 dispatcher->Disref(dispatcher);
173 }
174 return errCode;
175 }
176
NotifyAllNodesServiceDel(const NodeId nodeId,ServiceId serviceId)177 static void NotifyAllNodesServiceDel(const NodeId nodeId, ServiceId serviceId)
178 {
179 uint8_t i;
180 for (i = 0; i < MAX_NODE_COUNT; i++) {
181 if (i == nodeId) {
182 continue;
183 }
184 if (g_messageNodes[i] != NULL && g_messageNodes[i]->NotifyServiceDel != NULL) {
185 ErrorCode subErrCode = g_messageNodes[i]->NotifyServiceDel(g_messageNodes[i], serviceId);
186 if (subErrCode != ME_SUCCESS) {
187 HDF_LOGE("%s:Rollback service add to node failed!nodeId=%d,serviceId=%d,errCode=%d", __func__, i,
188 serviceId, subErrCode);
189 }
190 }
191 }
192 }
193
NotifyAllNodesServiceAdd(const NodeId nodeId,struct ServiceDef * mapper)194 static ErrorCode NotifyAllNodesServiceAdd(const NodeId nodeId, struct ServiceDef *mapper)
195 {
196 uint8_t i;
197 uint8_t notifyNodeIndex;
198 ErrorCode errCode;
199 if (mapper == NULL) {
200 return ME_ERROR_NULL_PTR;
201 }
202
203 errCode = ME_SUCCESS;
204 for (notifyNodeIndex = 0; notifyNodeIndex < MAX_NODE_COUNT; notifyNodeIndex++) {
205 if (notifyNodeIndex == nodeId) {
206 continue;
207 }
208 if (g_messageNodes[notifyNodeIndex] != NULL && g_messageNodes[notifyNodeIndex]->NotifyServiceAdd != NULL) {
209 errCode = g_messageNodes[notifyNodeIndex]->NotifyServiceAdd(g_messageNodes[notifyNodeIndex], mapper);
210 if (errCode != ME_SUCCESS) {
211 HDF_LOGE("%s:Notify service add to node failed!nodeId=%d,serviceId=%d,errCode=%d", __func__,
212 notifyNodeIndex, mapper->serviceId, errCode);
213 break;
214 }
215 }
216 }
217
218 if (errCode == ME_SUCCESS) {
219 return ME_SUCCESS;
220 }
221 for (i = 0; i < MAX_NODE_COUNT && i < notifyNodeIndex; i++) {
222 if (i == nodeId) {
223 continue;
224 }
225 if (g_messageNodes[i] != NULL && g_messageNodes[i]->NotifyServiceDel != NULL) {
226 ErrorCode subErrCode = g_messageNodes[i]->NotifyServiceDel(g_messageNodes[i], mapper->serviceId);
227 if (subErrCode != ME_SUCCESS) {
228 HDF_LOGE("%s:Rollback service add to node failed!nodeId=%d,serviceId=%d,errCode=%d", __func__, i,
229 mapper->serviceId, subErrCode);
230 }
231 }
232 }
233 return errCode;
234 }
235
DoRegistService(const NodeId nodeId,const DispatcherId dispatcherId,RemoteService * remoteService)236 static ErrorCode DoRegistService(const NodeId nodeId, const DispatcherId dispatcherId, RemoteService *remoteService)
237 {
238 if (remoteService == NULL) {
239 return ME_ERROR_NULL_PTR;
240 }
241
242 if (remoteService->serviceId >= MESSAGE_ENGINE_MAX_SERVICE) {
243 return ME_ERROR_PARA_WRONG;
244 }
245
246 if (g_routerStatus != ME_STATUS_RUNNING) {
247 return ME_ERROR_WRONG_STATUS;
248 }
249
250 HDF_LOGW("%s:Register service Node:%d;Dispatcher:%u;Service:%u", __func__, nodeId, dispatcherId,
251 remoteService->serviceId);
252
253 if (g_servicesIndex[remoteService->serviceId].remoteService != NULL) {
254 HDF_LOGE("%s:Router index find conflict serviceId!", __func__);
255 return ME_ERROR_SERVICEID_CONFLICT;
256 }
257
258 g_servicesIndex[remoteService->serviceId].remoteService = remoteService;
259 g_servicesIndex[remoteService->serviceId].nodeIndex = nodeId;
260 g_servicesIndex[remoteService->serviceId].dispatcherId = dispatcherId;
261
262 return ME_SUCCESS;
263 }
264
RegistServiceInner(const NodeId nodeId,const DispatcherId dispatcherId,struct ServiceDef * mapper)265 static ErrorCode RegistServiceInner(const NodeId nodeId, const DispatcherId dispatcherId, struct ServiceDef *mapper)
266 {
267 MessageNode *node = NULL;
268 RemoteService *remoteService = NULL;
269 MessageDispatcher *dispatcher = NULL;
270 ErrorCode errCode = ME_ERROR_NOT_SUPPORTED;
271 if (mapper == NULL || mapper->serviceId >= MESSAGE_ENGINE_MAX_SERVICE) {
272 return ME_ERROR_NULL_PTR;
273 }
274
275 if (OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER) != HDF_SUCCESS) {
276 HDF_LOGE("Unable to get lock!");
277 return ME_ERROR_OPER_MUTEX_FAILED;
278 }
279
280 node = RefMessageNode(nodeId, false);
281 if (node == NULL) {
282 OsalMutexUnlock(&g_routerMutex);
283 return ME_ERROR_NO_SUCH_NODE;
284 }
285 do {
286 if (node->CreateRemoteService == NULL) {
287 HDF_LOGE("%s:Can not reg service to node %d", __func__, nodeId);
288 break;
289 }
290 dispatcher = RefDispatcherInner(dispatcherId, false);
291
292 remoteService = node->CreateRemoteService(node, dispatcher, mapper);
293 if (remoteService == NULL) {
294 HDF_LOGE("%s:Node create service failed!node=%d", __func__, nodeId);
295 break;
296 }
297
298 errCode = NotifyAllNodesServiceAdd(nodeId, mapper);
299 if (errCode != ME_SUCCESS) {
300 HDF_LOGE("%s:NotifyAllNodesServiceAdd failed!err=%d", __func__, errCode);
301 break;
302 }
303
304 errCode = DoRegistService(nodeId, dispatcherId, remoteService);
305 if (errCode != ME_SUCCESS) {
306 HDF_LOGE("%s:DoRegistService failed!err=%d.", __func__, errCode);
307 NotifyAllNodesServiceDel(nodeId, mapper->serviceId);
308 break;
309 }
310 } while (false);
311
312 OsalMutexUnlock(&g_routerMutex);
313 if (dispatcher != NULL && dispatcher->Disref != NULL) {
314 dispatcher->Disref(dispatcher);
315 }
316 if (node != NULL && node->Disref != NULL) {
317 node->Disref(node);
318 }
319
320 if (errCode != ME_SUCCESS) {
321 ReleaseRemoteService(remoteService);
322 }
323
324 return errCode;
325 }
326
RegistLocalService(const DispatcherId dispatcherId,struct ServiceDef * mapper)327 ErrorCode RegistLocalService(const DispatcherId dispatcherId, struct ServiceDef *mapper)
328 {
329 return RegistServiceInner(LOCAL_NODE_INDEX, dispatcherId, mapper);
330 }
331
RegistRemoteService(NodeId nodeId,RemoteService * service)332 ErrorCode RegistRemoteService(NodeId nodeId, RemoteService *service)
333 {
334 HDF_STATUS status;
335 ErrorCode errCode;
336 if (service == NULL) {
337 return ME_ERROR_NULL_PTR;
338 }
339 if (service->serviceId >= MESSAGE_ENGINE_MAX_SERVICE) {
340 HDF_LOGE("%s:serviceId exceed max value! ServiceId=%d", __func__, service->serviceId);
341 return ME_ERROR_PARA_WRONG;
342 }
343 if (nodeId >= MAX_NODE_COUNT) {
344 HDF_LOGE("%s:NodeId exceed max value! NodeId=%d", __func__, nodeId);
345 return ME_ERROR_PARA_WRONG;
346 }
347 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
348 if (status != HDF_SUCCESS) {
349 HDF_LOGE("Unable to get lock!status=%d", status);
350 return ME_ERROR_OPER_MUTEX_FAILED;
351 }
352
353 errCode = DoRegistService(nodeId, BAD_DISPATCHER_ID, service);
354 if (errCode != ME_SUCCESS) {
355 HDF_LOGE("%s:RegService failed! errCode=%d", __func__, errCode);
356 }
357
358 status = OsalMutexUnlock(&g_routerMutex);
359 if (status != HDF_SUCCESS) {
360 HDF_LOGE("Unable to unlock!status=%d", status);
361 }
362 return errCode;
363 }
UnregistServiceInner(const NodeId nodeId,const DispatcherId dispatcherId,const ServiceId serviceId)364 static ErrorCode UnregistServiceInner(const NodeId nodeId, const DispatcherId dispatcherId, const ServiceId serviceId)
365 {
366 RemoteService *service = NULL;
367 HDF_STATUS status;
368 ErrorCode errCode;
369 if (serviceId >= MESSAGE_ENGINE_MAX_SERVICE) {
370 return ME_ERROR_PARA_WRONG;
371 }
372
373 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
374 if (status != HDF_SUCCESS) {
375 HDF_LOGE("Unable to get lock!status=%d", status);
376 return ME_ERROR_OPER_MUTEX_FAILED;
377 }
378 errCode = ME_SUCCESS;
379 do {
380 if (g_servicesIndex[serviceId].nodeIndex != nodeId || g_servicesIndex[serviceId].dispatcherId != dispatcherId) {
381 errCode = ME_ERROR_NO_SUCH_SERVICE;
382 break;
383 }
384 if (g_servicesIndex[serviceId].remoteService == NULL) {
385 errCode = ME_ERROR_NO_SUCH_SERVICE;
386 break;
387 }
388 service = g_servicesIndex[serviceId].remoteService;
389 ReleaseRemoteService(service);
390 g_servicesIndex[serviceId].remoteService = NULL;
391 g_servicesIndex[serviceId].nodeIndex = NO_SUCH_NODE_INDEX;
392 g_servicesIndex[serviceId].dispatcherId = BAD_DISPATCHER_ID;
393 NotifyAllNodesServiceDel(nodeId, serviceId);
394 } while (false);
395 status = OsalMutexUnlock(&g_routerMutex);
396 if (status != HDF_SUCCESS) {
397 HDF_LOGE("Unable to unlock!status=%d", status);
398 }
399 return errCode;
400 }
401
UnregistLocalService(const DispatcherId dispatcherId,ServiceId serviceId)402 ErrorCode UnregistLocalService(const DispatcherId dispatcherId, ServiceId serviceId)
403 {
404 return UnregistServiceInner(LOCAL_NODE_INDEX, dispatcherId, serviceId);
405 }
406
UnregistRemoteService(NodeId nodeId,ServiceId serviceId)407 ErrorCode UnregistRemoteService(NodeId nodeId, ServiceId serviceId)
408 {
409 (void)nodeId;
410 return UnregistServiceInner(REMOTE_NODE_INDEX, BAD_DISPATCHER_ID, serviceId);
411 }
CheckServiceID(ServiceId serviceId,bool allowSync)412 static bool CheckServiceID(ServiceId serviceId, bool allowSync)
413 {
414 if (serviceId >= MESSAGE_ENGINE_MAX_SERVICE) {
415 HDF_LOGE("receiverId exceed MaxServiceID.Route failed!");
416 return false;
417 }
418 (void)allowSync;
419
420 if (g_servicesIndex[serviceId].remoteService != NULL) {
421 return true;
422 }
423 #ifdef USERSPACE_CLIENT_SUPPORT
424 if (!allowSync) {
425 return false;
426 }
427 for (uint8_t i = 0; i < MAX_NODE_COUNT; i++) {
428 if (g_messageNodes[i] != NULL && g_messageNodes[i]->SyncService != NULL) {
429 (void)g_messageNodes[i]->SyncService(g_messageNodes[i]);
430 }
431 }
432 return CheckServiceID(serviceId, false);
433 #else
434 return false;
435 #endif
436 }
437
RefRemoteService(ServiceId serviceId)438 RemoteService *RefRemoteService(ServiceId serviceId)
439 {
440 HDF_STATUS status;
441 RemoteService *remoteService = NULL;
442 RemoteService *service = NULL;
443 if (serviceId >= MESSAGE_ENGINE_MAX_SERVICE) {
444 return NULL;
445 }
446
447 if (!CheckServiceID(serviceId, true)) {
448 return NULL;
449 }
450 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
451 if (status != HDF_SUCCESS) {
452 HDF_LOGE("Unable to get lock!status=%d", status);
453 return NULL;
454 }
455
456 do {
457 remoteService = g_servicesIndex[serviceId].remoteService;
458 if (remoteService != NULL && remoteService->Ref != NULL) {
459 service = remoteService->Ref(remoteService);
460 }
461 } while (false);
462
463 status = OsalMutexUnlock(&g_routerMutex);
464 if (status != HDF_SUCCESS) {
465 HDF_LOGE("Unable to get lock!status=%d", status);
466 }
467 return service;
468 }
469
SendMessage(MessageContext * context)470 ErrorCode SendMessage(MessageContext *context)
471 {
472 RemoteService *service = NULL;
473 ErrorCode errCode;
474 service = RefRemoteService(context->receiverId);
475 if (service == NULL) {
476 return ME_ERROR_NO_SUCH_SERVICE;
477 }
478 do {
479 if (service->SendMessage == NULL) {
480 errCode = ME_ERROR_NOT_SUPPORTED;
481 break;
482 }
483 errCode = service->SendMessage(service, context);
484 } while (false);
485
486 if (service->Disref != NULL) {
487 service->Disref(service);
488 }
489 return errCode;
490 }
491
492 #if defined(KERNEL_SERVER_SUPPORT) || defined(USERSPACE_CLIENT_SUPPORT)
493 #error("define both KERNEL_SERVER_SUPPORT and USERSPACE_CLIENT_SUPPORT is not allowed!")
494 #endif
495
496 #if defined(KERNEL_SERVER_SUPPORT) || defined(USERSPACE_CLIENT_SUPPORT)
CreateRemoteMessageNode(uint8_t nodesConfig)497 static ErrorCode CreateRemoteMessageNode(uint8_t nodesConfig)
498 {
499 ErrorCode errCode = ME_SUCCESS;
500 (void)nodesConfig;
501
502 #ifdef KERNEL_SERVER_SUPPORT
503 if ((nodesConfig & MESSAGE_NODE_REMOTE_KERNEL_SERVER) != 0) {
504 HDF_LOGI("Creating kernel server node...");
505 errCode = CreateKernelServerNode(&g_messageNodes[REMOTE_NODE_INDEX]);
506 }
507 #endif
508
509 #ifdef USERSPACE_CLIENT_SUPPORT
510 if ((nodesConfig & MESSAGE_NODE_REMOTE_USERSPACE_CLIENT) != 0) {
511 HDF_LOGI("Creating UserspaceClient server node...");
512 errCode = CreateUserspaceClientNode(&g_messageNodes[REMOTE_NODE_INDEX]);
513 }
514 #endif
515 return errCode;
516 }
517 #endif
518
InitNodes(void)519 static ErrorCode InitNodes(void)
520 {
521 ErrorCode errCode = ME_SUCCESS;
522 uint8_t i;
523 for (i = 0; i < MAX_NODE_COUNT; i++) {
524 MessageNode *node = g_messageNodes[i];
525 if (node != NULL) {
526 if (node->Init != NULL) {
527 errCode = node->Init(node);
528 }
529 if (errCode != ME_SUCCESS) {
530 HDF_LOGE("%s: init node failed!id=%d,ret=%d", __func__, i, errCode);
531 return errCode;
532 }
533 }
534 }
535 return ME_SUCCESS;
536 }
537
ReleaseNodes(void)538 static void ReleaseNodes(void)
539 {
540 uint8_t i;
541 for (i = 0; i < MAX_NODE_COUNT; i++) {
542 MessageNode *node = g_messageNodes[i];
543 if (node == NULL) {
544 continue;
545 }
546 if (node->Disref != NULL) {
547 node->Disref(node);
548 }
549 g_messageNodes[i] = NULL;
550 }
551 }
552
DoStartMessageRouter(uint8_t nodesConfig)553 static ErrorCode DoStartMessageRouter(uint8_t nodesConfig)
554 {
555 uint8_t i;
556 ErrorCode errCode;
557 if (g_routerStatus != ME_STATUS_STOPPED) {
558 HDF_LOGE("Router have already started!");
559 return ME_ERROR_MUTI_INIT_NOT_ALLOWED;
560 }
561
562 for (i = 0; i < MESSAGE_ENGINE_MAX_SERVICE; i++) {
563 g_servicesIndex[i].remoteService = NULL;
564 g_servicesIndex[i].nodeIndex = NO_SUCH_NODE_INDEX;
565 g_servicesIndex[i].dispatcherId = BAD_DISPATCHER_ID;
566 }
567 do {
568 HDF_LOGE("%s:Create local node ...", __func__);
569 errCode = CreateLocalNode(&g_messageNodes[LOCAL_NODE_INDEX]);
570 if (errCode != ME_SUCCESS) {
571 HDF_LOGE("%s:Create local node failed!ret=%d", __func__, errCode);
572 break;
573 }
574 #if defined(KERNEL_SERVER_SUPPORT) || defined(USERSPACE_CLIENT_SUPPORT)
575 HDF_LOGE("%s:Create remote node ...", __func__);
576 errCode = CreateRemoteMessageNode(nodesConfig);
577 if (errCode != ME_SUCCESS) {
578 HDF_LOGE("%s:Create remote node failed!ret=%d", __func__, errCode);
579 break;
580 }
581 #else
582 (void)nodesConfig;
583 #endif
584 errCode = InitNodes();
585 } while (false);
586
587 if (errCode == ME_SUCCESS) {
588 g_routerStatus = ME_STATUS_RUNNING;
589 return ME_SUCCESS;
590 }
591
592 ReleaseNodes();
593 return ME_SUCCESS;
594 }
595
EnableDefaultDispatcher(void)596 ErrorCode EnableDefaultDispatcher(void)
597 {
598 ErrorCode errCode;
599 DispatcherConfig config = {
600 .dispatcherId = DEFAULT_DISPATCHER_ID,
601 .queueSize = DEFAULT_DISPATCHER_QUEUE_SIZE,
602 .priorityLevelCount = DEFAULT_DISPATCHER_PRIORITY_COUNT
603 };
604 HDF_LOGI("Register default dispatcher...");
605 errCode = AddDispatcher(&config);
606 if (errCode != ME_SUCCESS) {
607 HDF_LOGE("Register default dispatcher failed!ret=%d", errCode);
608 }
609 return errCode;
610 }
611
StartMessageRouter(uint8_t nodesConfig)612 ErrorCode StartMessageRouter(uint8_t nodesConfig)
613 {
614 HDF_STATUS status;
615 ErrorCode errCode;
616 if (g_routerMutex.realMutex == NULL) {
617 HDF_STATUS status = OsalMutexInit(&g_routerMutex);
618 if (status != HDF_SUCCESS) {
619 return ME_ERROR_OPER_MUTEX_FAILED;
620 }
621 }
622 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
623 if (status != HDF_SUCCESS) {
624 HDF_LOGE("Unable to get lock!status=%d", status);
625 return ME_ERROR_OPER_MUTEX_FAILED;
626 }
627 errCode = DoStartMessageRouter(nodesConfig);
628 status = OsalMutexUnlock(&g_routerMutex);
629 if (status != HDF_SUCCESS) {
630 HDF_LOGE("Unable to get lock!status=%d", status);
631 }
632 return errCode;
633 }
634
DoShutdownMessageRouter(void)635 static ErrorCode DoShutdownMessageRouter(void)
636 {
637 uint8_t i;
638 RemoteService *service = NULL;
639 if (g_routerStatus == ME_STATUS_STOPPED) {
640 return ME_SUCCESS;
641 }
642
643 g_routerStatus = ME_STATUS_STOPPING;
644 for (i = 0; i < MESSAGE_ENGINE_MAX_SERVICE; i++) {
645 if (g_servicesIndex[i].remoteService == NULL) {
646 continue;
647 }
648 service = g_servicesIndex[i].remoteService;
649 g_servicesIndex[i].remoteService = NULL;
650 g_servicesIndex[i].nodeIndex = NO_SUCH_NODE_INDEX;
651 g_servicesIndex[i].dispatcherId = BAD_DISPATCHER_ID;
652
653 ReleaseRemoteService(service);
654 }
655
656 for (i = 0; i < MESSAGE_ENGINE_MAX_DISPATCHER; i++) {
657 if (g_dispatchers[i] != NULL && g_dispatchers[i]->Shutdown != NULL) {
658 g_dispatchers[i]->Shutdown(g_dispatchers[i]);
659 }
660 if (g_dispatchers[i] != NULL && g_dispatchers[i]->Disref != NULL) {
661 g_dispatchers[i]->Disref(g_dispatchers[i]);
662 }
663 g_dispatchers[i] = NULL;
664 }
665
666 ReleaseNodes();
667 g_routerStatus = ME_STATUS_STOPPED;
668 return ME_SUCCESS;
669 }
670
ShutdownMessageRouter()671 ErrorCode ShutdownMessageRouter()
672 {
673 HDF_STATUS status;
674 ErrorCode errCode;
675 HDF_LOGW("%s:Shutdown router...", __func__);
676 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
677 if (status != HDF_SUCCESS) {
678 HDF_LOGE("Unable to get lock!status=%d", status);
679 return ME_ERROR_OPER_MUTEX_FAILED;
680 }
681 errCode = DoShutdownMessageRouter();
682 status = OsalMutexUnlock(&g_routerMutex);
683 if (status != HDF_SUCCESS) {
684 HDF_LOGE("Unable to get lock!status=%d", status);
685 }
686 if (errCode == ME_SUCCESS) {
687 HDF_LOGW("%s:Router down.", __func__);
688 }
689 return errCode;
690 }
691
RefMessageNode(const NodeId nodeId,bool isRequireLock)692 MessageNode *RefMessageNode(const NodeId nodeId, bool isRequireLock)
693 {
694 MessageNode *node = NULL;
695 HDF_STATUS status;
696 if (nodeId >= MAX_NODE_COUNT) {
697 HDF_LOGE("Input nodeId >= MAX_NODE_COUNT");
698 return NULL;
699 }
700 if (isRequireLock) {
701 status = OsalMutexTimedLock(&g_routerMutex, HDF_WAIT_FOREVER);
702 if (status != HDF_SUCCESS) {
703 HDF_LOGE("%s:require lock failed!", __func__);
704 return NULL;
705 }
706 }
707
708 if (g_messageNodes[nodeId] != NULL && g_messageNodes[nodeId]->Ref != NULL) {
709 node = g_messageNodes[nodeId]->Ref(g_messageNodes[nodeId]);
710 }
711
712 if (isRequireLock) {
713 status = OsalMutexUnlock(&g_routerMutex);
714 if (status != HDF_SUCCESS) {
715 HDF_LOGE("%s:Unlock mutex failed!", __func__);
716 }
717 }
718 return node;
719 }
720