1 /*
2 * Copyright (c) 2022 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 "dslm_messenger_wrapper.h"
17
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <stdbool.h>
21
22 #include "messenger.h"
23 #include "utils_mutex.h"
24 #include "device_security_defines.h"
25
26 Messenger *g_messenger = NULL;
27 static Mutex g_mutex = INITED_MUTEX;
28
InitMessenger(const MessageReceiver messageReceiver,const StatusReceiver statusReceiver,const SendResultNotifier notifier)29 uint32_t InitMessenger(const MessageReceiver messageReceiver, const StatusReceiver statusReceiver,
30 const SendResultNotifier notifier)
31 {
32 MessengerConfig config = {
33 .pkgName = GetMessengerPackageName(),
34 .primarySessName = GetMessengerPrimarySessionName(),
35 .secondarySessName = GetMessengerSecondarySessionName(),
36 .messageReceiver = messageReceiver,
37 .statusReceiver = statusReceiver,
38 .sendResultNotifier = notifier,
39 };
40 InitMutex(&g_mutex);
41 LockMutex(&g_mutex);
42 g_messenger = CreateMessenger(&config);
43 UnlockMutex(&g_mutex);
44 if (g_messenger == NULL) {
45 return ERR_MSG_NOT_INIT;
46 }
47
48 return SUCCESS;
49 }
50
DeinitMessenger(void)51 uint32_t DeinitMessenger(void)
52 {
53 LockMutex(&g_mutex);
54 if (g_messenger == NULL) {
55 UnlockMutex(&g_mutex);
56 return SUCCESS;
57 }
58 DestroyMessenger(g_messenger);
59 UnlockMutex(&g_mutex);
60 return SUCCESS;
61 }
62
GetMessengerStatus(void)63 bool GetMessengerStatus(void)
64 {
65 LockMutex(&g_mutex);
66 if (g_messenger == NULL) {
67 UnlockMutex(&g_mutex);
68 return false;
69 }
70 bool ret = IsMessengerReady(g_messenger);
71 UnlockMutex(&g_mutex);
72 return ret;
73 }
74
SendMsgToDevice(uint64_t transNo,const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen)75 void SendMsgToDevice(uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen)
76 {
77 LockMutex(&g_mutex);
78 if (g_messenger == NULL) {
79 UnlockMutex(&g_mutex);
80 return;
81 }
82 SendMsgTo(g_messenger, transNo, devId, msg, msgLen);
83 UnlockMutex(&g_mutex);
84 return;
85 }
86
GetPeerDeviceOnlineStatus(const DeviceIdentify * devId,uint32_t * devType)87 bool GetPeerDeviceOnlineStatus(const DeviceIdentify *devId, uint32_t *devType)
88 {
89 LockMutex(&g_mutex);
90 if (g_messenger == NULL) {
91 UnlockMutex(&g_mutex);
92 return false;
93 }
94 if (devId == NULL || devType == NULL) {
95 UnlockMutex(&g_mutex);
96 return false;
97 }
98 bool ret = GetDeviceOnlineStatus(g_messenger, devId, devType);
99 UnlockMutex(&g_mutex);
100 return ret;
101 }
102
GetSelfDevice(uint32_t * devType)103 const DeviceIdentify *GetSelfDevice(uint32_t *devType)
104 {
105 LockMutex(&g_mutex);
106 static uint32_t type = 0;
107 static DeviceIdentify deviceId = {0, {0}};
108 if (deviceId.length == 0 || deviceId.identity[0] == 0) {
109 if (g_messenger != NULL) {
110 GetSelfDeviceIdentify(g_messenger, &deviceId, &type);
111 }
112 }
113 if (devType != NULL) {
114 *devType = type;
115 }
116 UnlockMutex(&g_mutex);
117 return &deviceId;
118 }
119
GetMessengerPackageName(void)120 __attribute__((weak)) const char *GetMessengerPackageName(void)
121 {
122 return "ohos.dslm";
123 }
124
GetMessengerPrimarySessionName(void)125 __attribute__((weak)) const char *GetMessengerPrimarySessionName(void)
126 {
127 return "device.security.level";
128 }
129
GetMessengerSecondarySessionName(void)130 __attribute__((weak)) const char *GetMessengerSecondarySessionName(void)
131 {
132 return NULL;
133 }