• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,uint32_t * devType)92 bool GetPeerDeviceOnlineStatus(const DeviceIdentify *devId, uint32_t *devType)
93 {
94     LockMutex(&g_mutex);
95     if (g_messenger == NULL) {
96         UnlockMutex(&g_mutex);
97         return false;
98     }
99     if (devId == NULL || devType == NULL) {
100         UnlockMutex(&g_mutex);
101         return false;
102     }
103     bool ret = GetDeviceOnlineStatus(g_messenger, devId, devType);
104     UnlockMutex(&g_mutex);
105     return ret;
106 }
107 
GetSelfDevice(uint32_t * devType)108 const DeviceIdentify *GetSelfDevice(uint32_t *devType)
109 {
110     LockMutex(&g_mutex);
111     static uint32_t type = 0;
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, &type);
116         }
117     }
118     if (devType != NULL) {
119         *devType = type;
120     }
121     UnlockMutex(&g_mutex);
122     return &deviceId;
123 }
124 
GetMessengerPackageName(void)125 __attribute__((weak)) const char *GetMessengerPackageName(void)
126 {
127     return "ohos.dslm";
128 }
129 
GetMessengerPrimarySessionName(void)130 __attribute__((weak)) const char *GetMessengerPrimarySessionName(void)
131 {
132     return "device.security.level";
133 }
134 
GetMessengerSecondarySessionName(void)135 __attribute__((weak)) const char *GetMessengerSecondarySessionName(void)
136 {
137 #ifdef SECONDARY_SOCKET_NAME
138     return SECONDARY_SOCKET_NAME;
139 #else
140     return NULL;
141 #endif
142 }