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 "softbus_proxychannel_network.h"
17 
18 #include <securec.h>
19 #include "softbus_def.h"
20 #include "softbus_errcode.h"
21 #include "softbus_proxychannel_manager.h"
22 #include "softbus_transmission_interface.h"
23 #include "trans_log.h"
24 
25 #define MAX_LISTENER_CNT 2
26 
27 typedef struct {
28     char sessionName[SESSION_NAME_SIZE_MAX];
29     INetworkingListener listener;
30 } INetworkingListenerEntry;
31 
32 static INetworkingListenerEntry g_listeners[MAX_LISTENER_CNT] = { 0 };
33 
FindListenerEntry(const char * sessionName)34 static INetworkingListenerEntry *FindListenerEntry(const char *sessionName)
35 {
36     for (int32_t i = 0; i < MAX_LISTENER_CNT; i++) {
37         if (strcmp(sessionName, g_listeners[i].sessionName) == 0) {
38             return &g_listeners[i];
39         }
40     }
41     return NULL;
42 }
43 
NotifyNetworkingChannelOpened(const char * sessionName,int32_t channelId,const AppInfo * appInfo,unsigned char isServer)44 int32_t NotifyNetworkingChannelOpened(
45     const char *sessionName, int32_t channelId, const AppInfo *appInfo, unsigned char isServer)
46 {
47     INetworkingListenerEntry *entry = FindListenerEntry(sessionName);
48     if (entry == NULL || entry->listener.onChannelOpened == NULL) {
49         TRANS_LOGE(TRANS_CTRL, "net onChannelOpened is null");
50         return SOFTBUS_NO_INIT;
51     }
52 
53     if (entry->listener.onChannelOpened(channelId, appInfo->peerData.deviceId, isServer) != SOFTBUS_OK) {
54         TRANS_LOGE(TRANS_CTRL, "notify channel open fail");
55         return SOFTBUS_TRANS_CHANNEL_OPEN_FAILED;
56     }
57 
58     return SOFTBUS_OK;
59 }
60 
NotifyNetworkingChannelOpenFailed(const char * sessionName,int32_t channelId,const char * networkId)61 void NotifyNetworkingChannelOpenFailed(const char *sessionName, int32_t channelId, const char *networkId)
62 {
63     INetworkingListenerEntry *entry = FindListenerEntry(sessionName);
64     if (entry == NULL || entry->listener.onChannelOpenFailed == NULL) {
65         TRANS_LOGE(TRANS_CTRL, "net onChannelOpenFailed is null");
66         return;
67     }
68     entry->listener.onChannelOpenFailed(channelId, networkId);
69 }
70 
NotifyNetworkingChannelClosed(const char * sessionName,int32_t channelId)71 void NotifyNetworkingChannelClosed(const char *sessionName, int32_t channelId)
72 {
73     INetworkingListenerEntry *entry = FindListenerEntry(sessionName);
74     if (entry == NULL || entry->listener.onChannelClosed == NULL) {
75         TRANS_LOGE(TRANS_CTRL, "net onChannelClosed is null");
76         return;
77     }
78     entry->listener.onChannelClosed(channelId);
79 }
80 
NotifyNetworkingMsgReceived(const char * sessionName,int32_t channelId,const char * data,uint32_t len)81 void NotifyNetworkingMsgReceived(const char *sessionName, int32_t channelId, const char *data, uint32_t len)
82 {
83     INetworkingListenerEntry *entry = FindListenerEntry(sessionName);
84     if (entry == NULL || entry->listener.onMessageReceived == NULL) {
85         return;
86     }
87     entry->listener.onMessageReceived(channelId, data, len);
88 }
89 
TransRegisterNetworkingChannelListener(const char * sessionName,const INetworkingListener * listener)90 int TransRegisterNetworkingChannelListener(const char *sessionName, const INetworkingListener *listener)
91 {
92     int32_t unuse = -1;
93     for (int32_t i = 0; i < MAX_LISTENER_CNT; i++) {
94         if (strlen(g_listeners[i].sessionName) == 0) {
95             unuse = i;
96             break;
97         }
98         if (strcmp(sessionName, g_listeners[i].sessionName) == 0) {
99             return SOFTBUS_OK;
100         }
101     }
102     if (unuse == -1) {
103         TRANS_LOGE(TRANS_CTRL, "exceed max listener registered. maxlisten=%{public}d", MAX_LISTENER_CNT);
104         return SOFTBUS_TRANS_REGISTER_LISTENER_FAILED;
105     }
106 
107     if (strcpy_s(g_listeners[unuse].sessionName, SESSION_NAME_SIZE_MAX, sessionName) != EOK) {
108         TRANS_LOGE(TRANS_CTRL, "strcpy_s session name failed");
109         return SOFTBUS_STRCPY_ERR;
110     }
111     g_listeners[unuse].listener = *listener;
112     TRANS_LOGI(TRANS_CTRL, "register net listener ok");
113     return SOFTBUS_OK;
114 }
115