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