• 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 "client_trans_message_service.h"
17 
18 #include "client_trans_channel_manager.h"
19 #include "client_trans_file.h"
20 #include "client_trans_file_listener.h"
21 #include "client_trans_session_manager.h"
22 #include "client_trans_session_service.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "softbus_feature_config.h"
26 #include "softbus_adapter_mem.h"
27 #include "trans_log.h"
28 
CheckSendLen(int32_t channelId,int32_t channelType,unsigned int len,int32_t businessType)29 int CheckSendLen(int32_t channelId, int32_t channelType, unsigned int len, int32_t businessType)
30 {
31     uint32_t dataConfig = INVALID_DATA_CONFIG;
32     if (ClientGetDataConfigByChannelId(channelId, channelType, &dataConfig) != SOFTBUS_OK) {
33         TRANS_LOGE(TRANS_SDK, "get config failed.");
34         return SOFTBUS_GET_CONFIG_VAL_ERR;
35     }
36     if (dataConfig == 0) {
37         ConfigType configType = (ConfigType)GetDefaultConfigType(channelType, businessType);
38         if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
39             TRANS_LOGE(TRANS_SDK, "Invalid channelType=%{public}d, businessType=%{public}d",
40                 channelType, businessType);
41             return SOFTBUS_INVALID_PARAM;
42         }
43         if (SoftbusGetConfig(configType, (unsigned char *)&dataConfig, sizeof(dataConfig)) != SOFTBUS_OK) {
44             TRANS_LOGE(TRANS_SDK, "get config failed, configType=%{public}d.", configType);
45             return SOFTBUS_GET_CONFIG_VAL_ERR;
46         }
47     }
48     TRANS_LOGI(TRANS_SDK, "channelId=%{public}d, sendDataLen=%{public}u, maxDataLen=%{public}u",
49         channelId, len, dataConfig);
50     if (len > dataConfig) {
51         TRANS_LOGE(TRANS_SDK, "send data over limit.len=%{public}u", len);
52         return SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT;
53     }
54 
55     return SOFTBUS_OK;
56 }
57 
SendBytes(int sessionId,const void * data,unsigned int len)58 int SendBytes(int sessionId, const void *data, unsigned int len)
59 {
60     TRANS_LOGI(TRANS_BYTES, "sessionId=%{public}d, len=%{public}d", sessionId, len);
61     if (data == NULL || len == 0) {
62         TRANS_LOGW(TRANS_BYTES, "Invalid param");
63         return SOFTBUS_INVALID_PARAM;
64     }
65 
66     int ret = CheckPermissionState(sessionId);
67     if (ret != SOFTBUS_OK) {
68         TRANS_LOGE(TRANS_BYTES, "SendBytes no permission, ret=%{public}d", ret);
69         return ret;
70     }
71 
72     int32_t channelId = INVALID_CHANNEL_ID;
73     int32_t channelType = CHANNEL_TYPE_BUTT;
74     bool isEnable = false;
75     if (ClientGetChannelBySessionId(sessionId, &channelId, &channelType, &isEnable) != SOFTBUS_OK) {
76         return SOFTBUS_TRANS_INVALID_SESSION_ID;
77     }
78 
79     int32_t businessType = BUSINESS_TYPE_BUTT;
80     if (ClientGetChannelBusinessTypeBySessionId(sessionId, &businessType) != SOFTBUS_OK) {
81         return SOFTBUS_TRANS_INVALID_SESSION_ID;
82     }
83     if ((businessType != BUSINESS_TYPE_BYTE) && (businessType != BUSINESS_TYPE_NOT_CARE) &&
84         (channelType != CHANNEL_TYPE_AUTH)) {
85         TRANS_LOGE(TRANS_BYTES, "BusinessType no match, businessType=%{public}d", businessType);
86         return SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH;
87     }
88 
89     int checkRet = CheckSendLen(channelId, channelType, len, BUSINESS_TYPE_BYTE);
90     if (checkRet != SOFTBUS_OK) {
91         return checkRet;
92     }
93 
94     if (!isEnable) {
95         return SOFTBUS_TRANS_SESSION_NO_ENABLE;
96     }
97     (void)ClientResetIdleTimeoutById(sessionId);
98     return ClientTransChannelSendBytes(channelId, channelType, data, len);
99 }
100 
SendMessage(int sessionId,const void * data,unsigned int len)101 int SendMessage(int sessionId, const void *data, unsigned int len)
102 {
103     TRANS_LOGI(TRANS_MSG, "sessionId=%{public}d, len=%{public}d", sessionId, len);
104     if (data == NULL || len == 0) {
105         TRANS_LOGW(TRANS_MSG, "Invalid param");
106         return SOFTBUS_INVALID_PARAM;
107     }
108     int ret = CheckPermissionState(sessionId);
109     if (ret != SOFTBUS_OK) {
110         TRANS_LOGE(TRANS_MSG, "SendMessage no permission, ret=%{public}d", ret);
111         return ret;
112     }
113 
114     int32_t channelId = INVALID_CHANNEL_ID;
115     int32_t channelType = CHANNEL_TYPE_BUTT;
116     bool isEnable = false;
117     if (ClientGetChannelBySessionId(sessionId, &channelId, &channelType, &isEnable) != SOFTBUS_OK) {
118         return SOFTBUS_TRANS_INVALID_SESSION_ID;
119     }
120 
121     int32_t businessType = BUSINESS_TYPE_BUTT;
122     if (ClientGetChannelBusinessTypeBySessionId(sessionId, &businessType) != SOFTBUS_OK) {
123         return SOFTBUS_TRANS_INVALID_SESSION_ID;
124     }
125     if ((businessType != BUSINESS_TYPE_MESSAGE) && (businessType != BUSINESS_TYPE_NOT_CARE) &&
126         (channelType != CHANNEL_TYPE_AUTH)) {
127         TRANS_LOGE(TRANS_MSG, "BusinessType no match, businessType=%{public}d", businessType);
128         return SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH;
129     }
130 
131     int checkRet = CheckSendLen(channelId, channelType, len, BUSINESS_TYPE_MESSAGE);
132     if (checkRet != SOFTBUS_OK) {
133         return checkRet;
134     }
135 
136     if (!isEnable) {
137         return SOFTBUS_TRANS_SESSION_NO_ENABLE;
138     }
139     (void)ClientResetIdleTimeoutById(sessionId);
140     return ClientTransChannelSendMessage(channelId, channelType, data, len);
141 }
142 
SendStream(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)143 int SendStream(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
144 {
145     if ((data == NULL) || (ext == NULL) || (param == NULL)) {
146         TRANS_LOGW(TRANS_STREAM, "Invalid param");
147         return SOFTBUS_INVALID_PARAM;
148     }
149     int ret = CheckPermissionState(sessionId);
150     if (ret != SOFTBUS_OK) {
151         TRANS_LOGE(TRANS_STREAM, "SendStream no permission, ret=%{public}d", ret);
152         return ret;
153     }
154 
155     int32_t channelId = INVALID_CHANNEL_ID;
156     int32_t type = CHANNEL_TYPE_BUTT;
157     bool isEnable = false;
158     if (ClientGetChannelBySessionId(sessionId, &channelId, &type, &isEnable) != SOFTBUS_OK) {
159         return SOFTBUS_TRANS_INVALID_SESSION_ID;
160     }
161     if (type != CHANNEL_TYPE_UDP) {
162         return SOFTBUS_TRANS_STREAM_ONLY_UDP_CHANNEL;
163     }
164 
165     int32_t businessType = BUSINESS_TYPE_BUTT;
166     if (ClientGetChannelBusinessTypeBySessionId(sessionId, &businessType) != SOFTBUS_OK) {
167         return SOFTBUS_TRANS_INVALID_SESSION_ID;
168     }
169     if ((businessType != BUSINESS_TYPE_STREAM) && (businessType != BUSINESS_TYPE_NOT_CARE)) {
170         TRANS_LOGE(TRANS_STREAM, "BusinessType no match, businessType=%{public}d", businessType);
171         return SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH;
172     }
173 
174     if (!isEnable) {
175         return SOFTBUS_TRANS_SESSION_NO_ENABLE;
176     }
177     (void)ClientResetIdleTimeoutById(sessionId);
178     return ClientTransChannelSendStream(channelId, type, data, ext, param);
179 }
180 
SendFile(int sessionId,const char * sFileList[],const char * dFileList[],uint32_t fileCnt)181 int SendFile(int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt)
182 {
183     TRANS_LOGI(TRANS_FILE, "sessionId=%{public}d", sessionId);
184     if ((sFileList == NULL) || (fileCnt == 0)) {
185         TRANS_LOGW(TRANS_FILE, "Invalid param");
186         return SOFTBUS_INVALID_PARAM;
187     }
188     int ret = CheckPermissionState(sessionId);
189     if (ret != SOFTBUS_OK) {
190         TRANS_LOGE(TRANS_FILE, "SendFile no permission, ret=%{public}d", ret);
191         return ret;
192     }
193 
194     FileSchemaListener *fileSchemaListener = (FileSchemaListener*)SoftBusCalloc(sizeof(FileSchemaListener));
195     if (fileSchemaListener == NULL) {
196         return SOFTBUS_MALLOC_ERR;
197     }
198     if (CheckFileSchema(sessionId, fileSchemaListener) == SOFTBUS_OK) {
199         if (SetSchemaCallback(fileSchemaListener->schema, sFileList, fileCnt) != SOFTBUS_OK) {
200             TRANS_LOGE(TRANS_FILE, "set schema callback failed");
201             SoftBusFree(fileSchemaListener);
202             return SOFTBUS_ERR;
203         }
204     }
205 
206     int32_t channelId = INVALID_CHANNEL_ID;
207     int32_t type = CHANNEL_TYPE_BUTT;
208     bool isEnable = false;
209     if (ClientGetChannelBySessionId(sessionId, &channelId, &type, &isEnable) != SOFTBUS_OK) {
210         SoftBusFree(fileSchemaListener);
211         return SOFTBUS_TRANS_INVALID_SESSION_ID;
212     }
213 
214     int32_t businessType = BUSINESS_TYPE_BUTT;
215     if (ClientGetChannelBusinessTypeBySessionId(sessionId, &businessType) != SOFTBUS_OK) {
216         SoftBusFree(fileSchemaListener);
217         return SOFTBUS_TRANS_INVALID_SESSION_ID;
218     }
219     if ((businessType != BUSINESS_TYPE_FILE) && (businessType != BUSINESS_TYPE_NOT_CARE)) {
220         TRANS_LOGE(TRANS_FILE, "BusinessType no match, businessType=%{public}d", businessType);
221         SoftBusFree(fileSchemaListener);
222         return SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH;
223     }
224 
225     if (!isEnable) {
226         SoftBusFree(fileSchemaListener);
227         return SOFTBUS_TRANS_SESSION_NO_ENABLE;
228     }
229     SoftBusFree(fileSchemaListener);
230     (void)ClientResetIdleTimeoutById(sessionId);
231     return ClientTransChannelSendFile(channelId, type, sFileList, dFileList, fileCnt);
232 }
233