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