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(ChannelProxy * proxy)26 int32_t InitChannelManager(ChannelProxy *proxy)
27 {
28     if (g_initialized || !IsSoftBusChannelSupported()) {
29         return HC_SUCCESS;
30     }
31     int32_t res = InitSoftBusChannelModule(proxy);
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 (returnChannelId == NULL) {
63         LOGE("returnChannelId is NULL ptr.");
64         return HC_ERR_NULL_PTR;
65     }
66     if (channelType == SERVICE_CHANNEL) {
67         *returnChannelId = DEFAULT_CHANNEL_ID;
68         return HC_SUCCESS;
69     } else if (channelType == SOFT_BUS) {
70         const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS);
71         if (connectParams == NULL) {
72             LOGE("Failed to get connectParams from jsonParams!");
73             return HC_ERR_JSON_GET;
74         }
75         int64_t channelId = DEFAULT_CHANNEL_ID;
76         int32_t result = GetSoftBusInstance()->openChannel(connectParams, requestId, &channelId);
77         if (result != HC_SUCCESS) {
78             return HC_ERR_CHANNEL_NOT_EXIST;
79         }
80         *returnChannelId = channelId;
81         return HC_SUCCESS;
82     } else {
83         return HC_ERR_CHANNEL_NOT_EXIST;
84     }
85 }
86 
CloseChannel(ChannelType channelType,int64_t channelId)87 void CloseChannel(ChannelType channelType, int64_t channelId)
88 {
89     if (channelType == SOFT_BUS) {
90         GetSoftBusInstance()->closeChannel(channelId);
91     }
92 }
93 
HcSendMsg(ChannelType channelType,int64_t requestId,int64_t channelId,const DeviceAuthCallback * callback,const char * data)94 int32_t HcSendMsg(ChannelType channelType, int64_t requestId, int64_t channelId,
95     const DeviceAuthCallback *callback, const char *data)
96 {
97     if (channelType == SERVICE_CHANNEL) {
98         if (ProcessTransmitCallback(requestId, (uint8_t *)data, HcStrlen(data) + 1, callback)) {
99             return HC_SUCCESS;
100         }
101         return HC_ERR_TRANSMIT_FAIL;
102     } else if (channelType == SOFT_BUS) {
103         return GetSoftBusInstance()->sendMsg(channelId, (uint8_t *)data, HcStrlen(data) + 1);
104     } else {
105         return HC_ERR_CHANNEL_NOT_EXIST;
106     }
107 }
108 
NotifyBindResult(ChannelType channelType,int64_t channelId)109 void NotifyBindResult(ChannelType channelType, int64_t channelId)
110 {
111     if (channelType == SOFT_BUS) {
112         GetSoftBusInstance()->notifyResult(channelId);
113     }
114 }
115