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     g_messenger = NULL;
65     UnlockMutex(&g_mutex);
66     return SUCCESS;
67 }
68 
GetMessengerStatus(void)69 bool GetMessengerStatus(void)
70 {
71     LockMutex(&g_mutex);
72     if (g_messenger == NULL) {
73         UnlockMutex(&g_mutex);
74         return false;
75     }
76     bool ret = IsMessengerReady(g_messenger);
77     UnlockMutex(&g_mutex);
78     return ret;
79 }
80 
SendMsgToDevice(uint64_t transNo,const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen)81 void SendMsgToDevice(uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen)
82 {
83     LockMutex(&g_mutex);
84     if (g_messenger == NULL) {
85         UnlockMutex(&g_mutex);
86         return;
87     }
88     SendMsgTo(g_messenger, transNo, devId, msg, msgLen);
89     UnlockMutex(&g_mutex);
90     return;
91 }
92 
GetPeerDeviceOnlineStatus(const DeviceIdentify * devId,int32_t * level)93 bool GetPeerDeviceOnlineStatus(const DeviceIdentify *devId, int32_t *level)
94 {
95     LockMutex(&g_mutex);
96     if (g_messenger == NULL) {
97         UnlockMutex(&g_mutex);
98         return false;
99     }
100     if (devId == NULL || level == NULL) {
101         UnlockMutex(&g_mutex);
102         return false;
103     }
104     bool ret = GetDeviceOnlineStatus(g_messenger, devId, level);
105     UnlockMutex(&g_mutex);
106     return ret;
107 }
108 
GetSelfDevice(int32_t * level)109 const DeviceIdentify *GetSelfDevice(int32_t *level)
110 {
111     LockMutex(&g_mutex);
112     static DeviceIdentify deviceId = {0, {0}};
113     if (deviceId.length == 0 || deviceId.identity[0] == 0) {
114         if (g_messenger != NULL) {
115             GetSelfDeviceIdentify(g_messenger, &deviceId, level);
116         }
117     }
118     UnlockMutex(&g_mutex);
119     return &deviceId;
120 }
121 
GetMessengerPackageName(void)122 __attribute__((weak)) const char *GetMessengerPackageName(void)
123 {
124     return "ohos.dslm";
125 }
126 
GetMessengerPrimarySessionName(void)127 __attribute__((weak)) const char *GetMessengerPrimarySessionName(void)
128 {
129     return "device.security.level";
130 }
131 
GetMessengerSecondarySessionName(void)132 __attribute__((weak)) const char *GetMessengerSecondarySessionName(void)
133 {
134 #ifdef SECONDARY_SOCKET_NAME
135     return SECONDARY_SOCKET_NAME;
136 #else
137     return NULL;
138 #endif
139 }