• 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 "channel_manager.h"
17 
18 #include "callback_manager.h"
19 #include "device_auth_defines.h"
20 #include "hc_log.h"
21 #include "hc_types.h"
22 #include "soft_bus_channel.h"
23 
24 static bool g_initialized = false;
25 
InitChannelManager(void)26 int32_t InitChannelManager(void)
27 {
28     if (g_initialized || !IsSoftBusChannelSupported()) {
29         return HC_SUCCESS;
30     }
31     int32_t res = InitSoftBusChannelModule();
32     if (res == HC_SUCCESS) {
33         g_initialized = true;
34     }
35     return res;
36 }
37 
DestroyChannelManager(void)38 void DestroyChannelManager(void)
39 {
40     if (g_initialized && IsSoftBusChannelSupported()) {
41         DestroySoftBusChannelModule();
42         g_initialized = false;
43     }
44 }
45 
GetChannelType(const DeviceAuthCallback * callback,const CJson * jsonParams)46 ChannelType GetChannelType(const DeviceAuthCallback *callback, const CJson *jsonParams)
47 {
48     if (IsSoftBusChannelSupported()) {
49         const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS);
50         if (connectParams != NULL) {
51             return SOFT_BUS;
52         }
53     }
54     if ((callback != NULL) && (callback->onTransmit != NULL)) {
55         return SERVICE_CHANNEL;
56     }
57     return NO_CHANNEL;
58 }
59 
OpenChannel(ChannelType channelType,const CJson * jsonParams,int64_t requestId,int64_t * returnChannelId)60 int32_t OpenChannel(ChannelType channelType, const CJson *jsonParams, int64_t requestId, int64_t *returnChannelId)
61 {
62     if (channelType == SERVICE_CHANNEL) {
63         *returnChannelId = DEFAULT_CHANNEL_ID;
64         return HC_SUCCESS;
65     } else if (channelType == SOFT_BUS) {
66         const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS);
67         if (connectParams == NULL) {
68             LOGE("Failed to get connectParams from jsonParams!");
69             return HC_ERR_JSON_GET;
70         }
71         int64_t channelId = DEFAULT_CHANNEL_ID;
72         int32_t result = GetSoftBusInstance()->openChannel(connectParams, requestId, &channelId);
73         if (result != HC_SUCCESS) {
74             return HC_ERR_CHANNEL_NOT_EXIST;
75         }
76         *returnChannelId = channelId;
77         return HC_SUCCESS;
78     } else {
79         return HC_ERR_CHANNEL_NOT_EXIST;
80     }
81 }
82 
CloseChannel(ChannelType channelType,int64_t channelId)83 void CloseChannel(ChannelType channelType, int64_t channelId)
84 {
85     if (channelType == SOFT_BUS) {
86         GetSoftBusInstance()->closeChannel(channelId);
87     }
88 }
89 
HcSendMsg(ChannelType channelType,int64_t requestId,int64_t channelId,const DeviceAuthCallback * callback,const char * data)90 int32_t HcSendMsg(ChannelType channelType, int64_t requestId, int64_t channelId,
91     const DeviceAuthCallback *callback, const char *data)
92 {
93     if (channelType == SERVICE_CHANNEL) {
94         if (ProcessTransmitCallback(requestId, (uint8_t *)data, HcStrlen(data) + 1, callback)) {
95             return HC_SUCCESS;
96         }
97         return HC_ERR_TRANSMIT_FAIL;
98     } else if (channelType == SOFT_BUS) {
99         return GetSoftBusInstance()->sendMsg(channelId, (uint8_t *)data, HcStrlen(data) + 1);
100     } else {
101         return HC_ERR_CHANNEL_NOT_EXIST;
102     }
103 }
104 
NotifyBindResult(ChannelType channelType,int64_t channelId)105 void NotifyBindResult(ChannelType channelType, int64_t channelId)
106 {
107     if (channelType == SOFT_BUS) {
108         GetSoftBusInstance()->notifyResult(channelId);
109     }
110 }
111