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