• 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 "messenger_impl.h"
17 
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21 
22 #include "messenger.h"
23 #include "messenger_device_session_manager.h"
24 #include "messenger_device_status_manager.h"
25 #include "utils_log.h"
26 #include "utils_mem.h"
27 #include "utils_work_queue.h"
28 
29 #define MESSENGER_MAGIC_HEAD 0x1234abcd
30 #define MESSENGER_PROCESS_QUEUE_SIZE 256
31 #define MESSENGER_PROCESS_QUEUE_NAME "messenger_queue"
32 typedef struct Messenger {
33     uint32_t magicHead;
34     WorkQueue *processQueue;
35 } Messenger;
36 
CreateMessengerImpl(const MessengerConfig * config)37 Messenger *CreateMessengerImpl(const MessengerConfig *config)
38 {
39     if (config == NULL) {
40         SECURITY_LOG_ERROR("config is null");
41         return NULL;
42     }
43 
44     WorkQueue *processQueue = CreateWorkQueue(MESSENGER_PROCESS_QUEUE_SIZE, MESSENGER_PROCESS_QUEUE_NAME);
45     if (processQueue == NULL) {
46         SECURITY_LOG_ERROR("create work queue failed");
47         return NULL;
48     }
49 
50     bool result = InitDeviceSessionManager(processQueue, config);
51     if (result == false) {
52         DestroyWorkQueue(processQueue);
53         SECURITY_LOG_ERROR("init session manager failed");
54         return NULL;
55     }
56 
57     result = InitDeviceStatusManager(processQueue, config->pkgName, config->statusReceiver);
58     if (result == false) {
59         DestroyWorkQueue(processQueue);
60         return NULL;
61     }
62 
63     Messenger *messenger = MALLOC(sizeof(Messenger));
64     if (messenger == NULL) {
65         DestroyWorkQueue(processQueue);
66         return NULL;
67     }
68     messenger->magicHead = MESSENGER_MAGIC_HEAD;
69     messenger->processQueue = processQueue;
70 
71     return messenger;
72 }
73 
DestroyMessengerImpl(Messenger * messenger)74 void DestroyMessengerImpl(Messenger *messenger)
75 {
76     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
77         SECURITY_LOG_ERROR("invalid params");
78         return;
79     }
80     DeInitDeviceStatusManager();
81     DeInitDeviceSessionManager();
82     messenger->magicHead = 0;
83     DestroyWorkQueue(messenger->processQueue);
84     FREE(messenger);
85 }
86 
IsMessengerReadyImpl(const Messenger * messenger)87 bool IsMessengerReadyImpl(const Messenger *messenger)
88 {
89     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
90         SECURITY_LOG_ERROR("invalid params");
91         return false;
92     }
93     return true;
94 }
95 
SendMsgToImpl(const Messenger * messenger,uint64_t transNo,const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen)96 void SendMsgToImpl(const Messenger *messenger, uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg,
97     uint32_t msgLen)
98 {
99     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
100         SECURITY_LOG_ERROR("invalid params");
101         return;
102     }
103     MessengerSendMsgTo(transNo, devId, msg, msgLen);
104 }
105 
GetDeviceOnlineStatusImpl(const Messenger * messenger,const DeviceIdentify * devId,uint32_t * devType)106 bool GetDeviceOnlineStatusImpl(const Messenger *messenger, const DeviceIdentify *devId, uint32_t *devType)
107 {
108     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
109         SECURITY_LOG_ERROR("invalid params");
110         return false;
111     }
112 
113     return MessengerGetDeviceOnlineStatus(devId, devType);
114 }
115 
GetSelfDeviceIdentifyImpl(const Messenger * messenger,DeviceIdentify * devId,uint32_t * devType)116 bool GetSelfDeviceIdentifyImpl(const Messenger *messenger, DeviceIdentify *devId, uint32_t *devType)
117 {
118     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
119         SECURITY_LOG_ERROR("invalid params");
120         return false;
121     }
122 
123     return MessengerGetSelfDeviceIdentify(devId, devType);
124 }
125 
ForEachDeviceProcessImpl(const Messenger * messenger,const DeviceProcessor processor,void * para)126 void ForEachDeviceProcessImpl(const Messenger *messenger, const DeviceProcessor processor, void *para)
127 {
128     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
129         SECURITY_LOG_ERROR("invalid params");
130         return;
131     }
132 
133     return MessengerForEachDeviceProcess(processor, para);
134 }
135 
GetDeviceStatisticInfoImpl(const Messenger * messenger,const DeviceIdentify * devId,StatisticInformation * info)136 bool GetDeviceStatisticInfoImpl(const Messenger *messenger, const DeviceIdentify *devId, StatisticInformation *info)
137 {
138     if (messenger == NULL || messenger->magicHead != MESSENGER_MAGIC_HEAD) {
139         SECURITY_LOG_ERROR("invalid params");
140         return false;
141     }
142     (void)devId;
143     (void)info;
144     return true;
145 }
146