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 #ifdef L2_STANDARD
35 .primarySockName = GetMessengerPrimarySessionName(),
36 .secondarySockName = GetMessengerSecondarySessionName(),
37 #else
38 .primarySessName = GetMessengerPrimarySessionName(),
39 .secondarySessName = GetMessengerSecondarySessionName(),
40 #endif
41 .messageReceiver = messageReceiver,
42 .statusReceiver = statusReceiver,
43 .sendResultNotifier = notifier,
44 };
45 InitMutex(&g_mutex);
46 LockMutex(&g_mutex);
47 g_messenger = CreateMessenger(&config);
48 UnlockMutex(&g_mutex);
49 if (g_messenger == NULL) {
50 return ERR_MSG_NOT_INIT;
51 }
52
53 return SUCCESS;
54 }
55
DeinitMessenger(void)56 uint32_t DeinitMessenger(void)
57 {
58 LockMutex(&g_mutex);
59 if (g_messenger == NULL) {
60 UnlockMutex(&g_mutex);
61 return SUCCESS;
62 }
63 DestroyMessenger(g_messenger);
64 UnlockMutex(&g_mutex);
65 return SUCCESS;
66 }
67
GetMessengerStatus(void)68 bool GetMessengerStatus(void)
69 {
70 LockMutex(&g_mutex);
71 if (g_messenger == NULL) {
72 UnlockMutex(&g_mutex);
73 return false;
74 }
75 bool ret = IsMessengerReady(g_messenger);
76 UnlockMutex(&g_mutex);
77 return ret;
78 }
79
SendMsgToDevice(uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen)80 void SendMsgToDevice(uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen)
81 {
82 LockMutex(&g_mutex);
83 if (g_messenger == NULL) {
84 UnlockMutex(&g_mutex);
85 return;
86 }
87 SendMsgTo(g_messenger, transNo, devId, msg, msgLen);
88 UnlockMutex(&g_mutex);
89 return;
90 }
91
GetPeerDeviceOnlineStatus(const DeviceIdentify *devId, int32_t *level)92 bool GetPeerDeviceOnlineStatus(const DeviceIdentify *devId, int32_t *level)
93 {
94 LockMutex(&g_mutex);
95 if (g_messenger == NULL) {
96 UnlockMutex(&g_mutex);
97 return false;
98 }
99 if (devId == NULL || level == NULL) {
100 UnlockMutex(&g_mutex);
101 return false;
102 }
103 bool ret = GetDeviceOnlineStatus(g_messenger, devId, level);
104 UnlockMutex(&g_mutex);
105 return ret;
106 }
107
GetSelfDevice(int32_t *level)108 const DeviceIdentify *GetSelfDevice(int32_t *level)
109 {
110 LockMutex(&g_mutex);
111 static DeviceIdentify deviceId = {0, {0}};
112 if (deviceId.length == 0 || deviceId.identity[0] == 0) {
113 if (g_messenger != NULL) {
114 GetSelfDeviceIdentify(g_messenger, &deviceId, level);
115 }
116 }
117 UnlockMutex(&g_mutex);
118 return &deviceId;
119 }
120
GetMessengerPackageName(void)121 __attribute__((weak)) const char *GetMessengerPackageName(void)
122 {
123 return "ohos.dslm";
124 }
125
GetMessengerPrimarySessionName(void)126 __attribute__((weak)) const char *GetMessengerPrimarySessionName(void)
127 {
128 return "device.security.level";
129 }
130
GetMessengerSecondarySessionName(void)131 __attribute__((weak)) const char *GetMessengerSecondarySessionName(void)
132 {
133 #ifdef SECONDARY_SOCKET_NAME
134 return SECONDARY_SOCKET_NAME;
135 #else
136 return NULL;
137 #endif
138 }