• 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 /**
17  * @addtogroup SoftBus
18  * @{
19  *
20  * @brief Provides high-speed, secure communication between devices.
21  *
22  * This module implements unified distributed communication capability management between
23  * nearby devices, and provides link-independent device discovery and transmission interfaces
24  * to support service publishing and data transmission.
25  *
26  * @since 1.0
27  * @version 1.0
28 */
29 /** @} */
30 
31 /**
32  * @file session.h
33  *
34  * @brief Declares unified data transmission interfaces.
35  *
36  * This file provides data transmission capabilities, including creating and removing a session server,
37  * opening and closing sessions, receiving data, and querying basic session information. \n
38  * After multiple nearby devices are discovered and networked, these interfaces can be used to
39  * transmit data across devices. \n
40  *
41  * @since 1.0
42  * @version 1.0
43  */
44 #ifndef SESSION_H
45 #define SESSION_H
46 
47 #include <stdint.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 /**
53  * @brief business type of session
54  *
55  * @since 1.0
56  * @version 1.0
57  */
58 typedef enum {
59     TYPE_MESSAGE = 1,
60     TYPE_BYTES,
61     TYPE_FILE,
62     TYPE_STREAM,
63     TYPE_BUTT,
64 } SessionType;
65 
66 typedef enum  {
67     INVALID = -1,
68     /*
69      * Send any segment of a frame each time.
70      */
71     RAW_STREAM,
72     /*
73      * Send a whole video frame each time.
74      */
75     COMMON_VIDEO_STREAM,
76     /*
77      * Send a whole audio frame each time.
78      */
79     COMMON_AUDIO_STREAM,
80     /*
81      * Slice frame mode.
82      */
83     VIDEO_SLICE_STREAM,
84 } StreamType;
85 
86 typedef enum  {
87     LINK_TYPE_WIFI_WLAN_5G = 1,
88     LINK_TYPE_WIFI_WLAN_2G = 2,
89     LINK_TYPE_WIFI_P2P = 3,
90     LINK_TYPE_BR = 4,
91     LINK_TYPE_MAX = 4,
92 } LinkType;
93 
94 /**
95  * @brief session attribute.
96  *
97  * control the attribute of session.
98  *
99  * @since 1.0
100  * @version 1.0
101  */
102 typedef struct {
103     /** @brief dataType{@link SessionType} */
104     int dataType;
105     int linkTypeNum;
106     LinkType linkType[LINK_TYPE_MAX];
107     union {
108         struct StreamAttr {
109             int streamType;
110         } streamAttr;
111     } attr;
112 } SessionAttribute;
113 
114 typedef struct {
115     char *buf;
116     int bufLen;
117 } StreamData;
118 
119 typedef struct {
120     int type;
121     int64_t value;
122 } TV;
123 
124 typedef struct {
125     int frameType;
126     int64_t timeStamp;
127     int seqNum;
128     int seqSubNum;
129     int level;
130     int bitMap;
131     int tvCount;
132     TV *tvList;
133 } StreamFrameInfo;
134 
135 typedef enum {
136     QOS_IMPROVE = 0,
137     QOS_RECOVER = 1
138 } QosQuality;
139 
140 typedef enum {
141     TRANS_STREAM_QUALITY_EVENT = 1,
142     TRANS_CHANNEL_QUALITY_EVENT,
143     TRANS_CAN_DELAY_EVENT,
144     TRANS_CANT_DELAY_EVENT,
145     QOS_EVENT_MAX
146 } QosEvent;
147 
148 typedef enum {
149     WIFI_CHANNEL_QUALITY = 1,
150     FRAME_REALTIME_STATUS = 2,
151     BANDWIDTH_ESTIMATE_VALUE = 3,
152     JITTER_DETECTION_VALUE = 4,
153     STREAM_TRAFFIC_STASTICS = 5,
154 } TransEnumEventType;
155 
156 typedef struct {
157     int32_t channel;
158     int32_t score;
159 } WifiChannelQuality;
160 
161 typedef struct {
162     int32_t streamId;
163     int32_t seqNum;
164     int32_t level;
165     int32_t transStatus;
166     int32_t interval;
167 } FrameStatus;
168 
169 typedef struct {
170     uint32_t trend;
171     uint32_t rate;  /* kbps */
172 } BandwidthDetection;
173 
174 typedef struct {
175     int32_t jitterLevel;
176     uint32_t bufferTime;  /* ms */
177 } JitterEstimation;
178 
179 typedef struct {
180     uint64_t statisticsGotTime; /* time point that stream traficc statistics are obtained (ms) */
181     uint64_t periodRecvBits;
182     uint32_t pktNum;
183     uint32_t periodRecvPkts;
184     uint32_t periodRecvPktLoss;
185     uint32_t periodRecvRate; /* kbps */
186     uint64_t periodRecvRateBps; /* bps */
187     uint32_t periodRtt; /* ms */
188     uint32_t periodRecvPktLossHighPrecision; /* for example when lost rate is 1.10%, then 110 will returned */
189     uint32_t periodSendLostPkts;
190     uint32_t periodSendPkts;
191     uint32_t periodSendPktLossHighPrecision; /* for example when lost rate is 1.10%, then 110 will returned */
192     uint64_t periodSendBits;
193     uint64_t periodSendRateBps; /* bps */
194 } StreamStatistics;
195 
196 typedef struct {
197     TransEnumEventType type;
198     union {
199         WifiChannelQuality wifiChannelInfo;
200         FrameStatus frameStatusInfo;
201         BandwidthDetection bandwidthInfo;
202         JitterEstimation jitterInfo;
203         StreamStatistics appStatistics;
204     } info;
205 } QosTv;
206 
207 typedef enum {
208     /* Value type of this option is uint32_t, this option only can be get */
209     SESSION_OPTION_MAX_SENDBYTES_SIZE = 0,
210     /* Value type of this option is uint32_t, this option only can be get */
211     SESSION_OPTION_MAX_SENDMESSAGE_SIZE,
212 
213     SESSION_OPTION_BUTT,
214 } SessionOption;
215 
216 /**
217  * @brief Defines session callbacks.
218  *
219  * When a session is opened or closed, or there is data to process, the related callback is invoked.
220  *
221  * @since 1.0
222  * @version 1.0
223  */
224 typedef struct {
225     /**
226      * @brief Called when a session is opened.
227      *
228      * This function can be used to verify the session or initialize resources related to the session.
229      *
230      * @param sessionId Indicates the session ID.
231      * @param result 0 if the session is opened successfully, returns an error code otherwise.
232      * @return Returns <b>0</b> if the session connection is accepted; returns a non-zero value
233      * otherwise (you do not need to call {@link CloseSession} to close the session).
234      * @since 1.0
235      * @version 1.0
236      */
237     int (*OnSessionOpened)(int sessionId, int result);
238 
239     /**
240      * @brief Called when a session is closed.
241      *
242      * This function can be used to release resources related to the session.
243      * You do not need to call {@link CloseSession}.
244      *
245      * @param sessionId Indicates the session ID.
246      * @since 1.0
247      * @version 1.0
248      */
249     void (*OnSessionClosed)(int sessionId);
250 
251     /**
252      * @brief Called when data is received.
253      *
254      * This function is used to notify that data is received.
255      *
256      * @param sessionId Indicates the session ID.
257      * @param data Indicates the pointer to the data received.
258      * @param dataLen Indicates the length of the data received.
259      * @since 1.0
260      * @version 1.0
261      */
262     void (*OnBytesReceived)(int sessionId, const void *data, unsigned int dataLen);
263 
264     /**
265      * @brief Called when message is received.
266      *
267      * This function is used to notify that message is received.
268      *
269      * @param sessionId Indicates the session ID.
270      * @param data Indicates the pointer to the message data received.
271      * @param dataLen Indicates the length of the message received.
272      * @since 1.0
273      * @version 1.0
274      */
275     void (*OnMessageReceived)(int sessionId, const void *data, unsigned int dataLen);
276 
277     /**
278      * @brief Called when stream is received.
279      *
280      * This function is used to notify that stream is received.
281      *
282      * @param sessionId Indicates the session ID.
283      * @param data Indicates the pointer to the stream data received.
284      * @param dataLen Indicates the length of the stream received.
285      * @since 1.0
286      * @version 1.0
287      */
288     void (*OnStreamReceived)(int sessionId, const StreamData *data, const StreamData *ext,
289         const StreamFrameInfo *param);
290 
291     /**
292      * @brief Called when QoS information is retrieved.
293      *
294      * This function is used to notify that QoS information is retrieved.
295      *
296      * @param sessionId Indicates the session ID.
297      * @param eventId Indicates the type of QoS information, e.g., channel quality and stream quality
298      * @param tvCount Indicates the number of structure returned in the fourth parameters, i.e., tvList.
299      * @param tvList Indicates the detailed information of data transmission.
300      * @since 1.0
301      * @version 1.0
302      */
303     void (*OnQosEvent)(int sessionId, int eventId, int tvCount, const QosTv *tvList);
304 } ISessionListener;
305 
306 typedef struct {
307     int (*OnReceiveFileStarted)(int sessionId, const char *files, int fileCnt);
308     int (*OnReceiveFileProcess)(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal);
309     void (*OnReceiveFileFinished)(int sessionId, const char *files, int fileCnt);
310     void (*OnFileTransError)(int sessionId);
311 } IFileReceiveListener;
312 
313 typedef struct {
314     int (*OnSendFileProcess)(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal);
315     int (*OnSendFileFinished)(int sessionId, const char *firstFile);
316     void (*OnFileTransError)(int sessionId);
317 } IFileSendListener;
318 
319 /**
320  * @brief Creates a session server based on a package name and session name.
321  *
322  * A maximum of 10 session servers can be created.
323  *
324  * @param pkgName Indicates the pointer to the package name, which can be used to check whether the
325  * session server is in this package. The value cannot be empty and can contain a maximum of 64 characters.
326  * @param sessionName Indicates the pointer to the session name, which is the unique ID of the session server.
327  * The value cannot be empty and can contain a maximum of 255 characters.
328  * @param listener Indicates the pointer to the session callback structure, which cannot be empty.
329  * The common error codes are as follows:
330  * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
331  * @see RemoveSessionServer
332  * @since 1.0
333  * @version 1.0
334  */
335 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener);
336 
337 /**
338  * @brief Removes a session server based on a package name and session name.
339  *
340  * @param pkgName Indicates the pointer to the name of the registered package, which can be used to check
341  * whether the session server is in this package. The value cannot be empty and can contain a maximum of 64 characters.
342  * @param sessionName Indicates the pointer to the session name. The value cannot be empty and can contain
343  * a maximum of 64 characters.
344  * The common error codes are as follows:
345  * @return Returns <b>0</b> if the operation is successful, returns <b>-1</b> otherwise.
346  * @see CreateSessionServer
347  * @since 1.0
348  * @version 1.0
349  */
350 int RemoveSessionServer(const char *pkgName, const char *sessionName);
351 
352 /**
353  * @brief Initiate a session open request, which is an asynchronous process.
354  *
355  * The session connection is opened based on the service name to trigger the first packet interaction process.
356  * According to the {@link OnSessionOpened} Notify the user whether the session is successfully opened.
357  * Data can be transmitted only after the session is successfully opened.
358  *
359  * @param mySessionName local session name.
360  * @param peerSessionName remote session name.
361  * @param peerNetworkId remote device id.
362  * @param groupId group id.
363  * @param attr session attribute {@link SessionAttribute}.
364  * The common error codes are as follows:
365  * @return <b>SOFTBUS_TRANS_INVALID_PARAM</b> invalid param.
366  * @return <b>INVALID_SESSION_ID</b> open session failed, and return invalid session id.
367  * @return return sessionId if the session is opened successfully,
368  * and the sessionId is greater than 0, returns other internal error codes otherwise.
369  * @since 1.0
370  * @version 1.0
371  */
372 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerNetworkId,
373     const char *groupId, const SessionAttribute* attr);
374 
375 /**
376  * @brief Closes a connected session based on a session ID.
377  *
378  * @param sessionId Indicates the session ID.
379  * @return no return value.
380  * @since 1.0
381  * @version 1.0
382  */
383 void CloseSession(int sessionId);
384 
385 /**
386  * @brief Sends data based on a session ID.
387  *
388  * @param sessionId Indicates the session ID.
389  * @param data Indicates the pointer to the data to send, which cannot be <b>NULL</b>.
390  * @param len Indicates the length of the data to send. The maximum length cannot exceed 984 characters.
391  * The common error codes are as follows:
392  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> param data or len of value is invalid.
393  * @return Returns <b>SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT</b> The data length exceeds the maximum limit.
394  * @return Returns <b>SOFTBUS_TRANS_INVALID_SESSION_ID</b> invalid session id.
395  * @return Returns <b>SOFTBUS_TRANS_SESSION_NO_ENABLE</b> session is currently disable.
396  * @return Returns <b>SOFTBUS_OK</b> if the function is called successfully, return other internal errorcode otherwise.
397  * @since 1.0
398  * @version 1.0
399  */
400 int SendBytes(int sessionId, const void *data, unsigned int len);
401 
402 /**
403  * @brief Sends message based on a session ID.
404  *
405  * @param sessionId Indicates the session ID.
406  * @param data Indicates the pointer to the message data to send, which cannot be <b>NULL</b>.
407  * @param len Indicates the length of the message to send.
408  * The common error codes are as follows:
409  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if the input data is NULL or len is Zero.
410  * @return Returns <b>SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT</b> The data length exceeds the maximum limit.
411  * @return Returns <b>SOFTBUS_INVALID_SESSION_ID</b> if the sessionId is invalid.
412  * @return Returns <b>SOFTBUS_TRANS_SESSION_NO_ENABLE</b> if the session current be enabled.
413  * @return Returns <b>SOFTBUS_OK</b> if the function is called successfully, return other internal errorcode otherwise.
414  * @since 1.0
415  * @version 1.0
416  */
417 int SendMessage(int sessionId, const void *data, unsigned int len);
418 
419 /**
420  * @brief Sends stream based on a session ID.
421  *
422  * @param sessionId Indicates the session ID.
423  * @param data Indicates the pointer to the stream data to send, which cannot be <b>NULL</b>.
424  * @param ext Indicates the pointer to the ext stream data to send, which cannot be <b>NULL</b>.
425  * @param param Indicates the pointer to the stream data of param, which cannot be <b>NULL</b>.
426  * The common error codes are as follows:
427  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if the input param is NULL.
428  * @return Returns <b>SOFTBUS_INVALID_SESSION_ID</b> if the sessionId is invalid.
429  * @return Returns <b>SOFTBUS_TRANS_SESSION_NO_ENABLE</b> if the session current be enabled.
430  * @return Returns <b>SOFTBUS_OK</b> if the function is called successfully, return other internal errorcode otherwise.
431  * @since 1.0
432  * @version 1.0
433  */
434 int SendStream(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
435 
436 /**
437  * @brief Obtains the session name registered by the local device based on the session ID.
438  *
439  * @param sessionId Indicates the session ID.
440  * @param sessionName Indicates the pointer to the buffer for storing the session name.
441  * @param len Indicates the length of the buffer.
442  * The common error codes are as follows:
443  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> Indicates invalid value for input param.
444  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful, returns other internal error codes otherwise.
445  * @since 1.0
446  * @version 1.0
447  */
448 int GetMySessionName(int sessionId, char *sessionName, unsigned int len);
449 
450 /**
451  * @brief Obtains the session name registered by the peer device based on the session ID.
452  *
453  * @param sessionId Indicates the session ID.
454  * @param sessionName Indicates the pointer to the buffer for storing the session name.
455  * @param len Indicates the length of the buffer.
456  * The common error codes are as follows:
457  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> Indicates invalid value for input param.
458  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful, returns other internal error codes otherwise.
459  * @since 1.0
460  * @version 1.0
461  */
462 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len);
463 
464 /**
465  * @brief Obtains the peer device ID based on a session ID.
466  *
467  * @param sessionId Indicates the session ID.
468  * @param networkId Indicates the pointer to the buffer for storing the device ID.
469  * @param len Indicates the length of the buffer.
470  * The common error codes are as follows:
471  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> Indicates invalid value for input param.
472  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful, returns other internal error codes otherwise.
473  * @since 1.0
474  * @version 1.0
475  */
476 int GetPeerDeviceId(int sessionId, char *networkId, unsigned int len);
477 
478 /**
479  * @brief Get session side based on a session ID.
480  *
481  * @param sessionId Indicates the session ID.
482  * @return Returns <b>-1</b> Indicates get session side failed.
483  * @return Returns <b>0</b> Indicates the session is server side.
484  * @return Returns <b>1</b> Indicates the session is client side.
485  * @since 1.0
486  * @version 1.0
487  */
488 int GetSessionSide(int sessionId);
489 
490 /**
491  * @brief Set file receive listener.
492  *
493  * @param pkgName Indicates the pointer to the name of the registered package, which can be used to check
494  * whether the session server is in this package. The value cannot be empty and can contain a maximum of 64 characters.
495  * @param sessionName Indicates the pointer to the buffer for storing the session name.
496  * @param recvListener Indicates the pointer to the file receive listener, which cannot be <b>NULL</b>.
497  * @param rootDir Indicates the length of the message to send.
498  * The common error codes are as follows:
499  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> Indicates invalid value for input param.
500  * @return Returns <b>SOFTBUS_TRANS_SESSION_ADDPKG_FAILED</b> if add pkgName failed.
501  * @return Returns <b>SOFTBUS_OK</b>if the function is called successfully, return other internal errorcodes otherwise.
502  * @since 1.0
503  * @version 1.0
504  */
505 int SetFileReceiveListener(const char *pkgName, const char *sessionName,
506     const IFileReceiveListener *recvListener, const char *rootDir);
507 
508 /**
509  * @brief Set file sendListener based on pkgName and sessionName .
510  *
511  * @param pkgName Indicates the pointer to the name of the registered package, which can be used to check
512  * whether the session server is in this package. The value cannot be empty and can contain a maximum of 64 characters.
513  * @param sessionName Indicates the pointer to the buffer for storing the session name.
514  * @param sendListener Indicates the pointer to the file send listener, which cannot be <b>NULL</b>.
515  * The common error codes are as follows:
516  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if the input param is invalid.
517  * @return Returns <b>SOFTBUS_TRANS_SESSION_ADDPKG_FAILED</b> if add pkgName failed.
518  * @return Returns <b>SOFTBUS_OK</b>if the function is called successfully, return other internal errorcodes otherwise.
519  * @since 1.0
520  * @version 1.0
521  */
522 int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener);
523 
524 /**
525  * @brief Sends file based on a session ID.
526  *
527  * @param sessionId Indicates the session ID.
528  * @param sFileList Indicates the pointer to the source file list to send, which cannot be <b>NULL</b>.
529  * @param dFileList Indicates the pointer to the destination file list to send, which cannot be <b>NULL</b>.
530  * @param fileCnt Indicates the number of files to send, whic cannot be <b>0</b>.
531  * The common error codes are as follows:
532  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if the sFileList is NULL or fileCnt is Zero.
533  * @return Returns <b>SOFTBUS_INVALID_SESSION_ID</b> if the sessionId is invalid.
534  * @return Returns <b>SOFTBUS_TRANS_SESSION_NO_ENABLE</b> if the session current be enabled.
535  * @return Returns <b>SOFTBUS_OK</b>if the function is called successfully, return other internal errorcodes otherwise.
536  * @since 1.0
537  * @version 1.0
538  */
539 int SendFile(int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
540 
541 /**
542  * @brief Get Session based on a session ID.
543  *
544  * @param sessionId Indicates the session ID.
545  * @param option Indicates the session option type to get.
546  * @param optionValue Indicates the session option value to get, which cannot be <b>NULL</b>.
547  * @param valueSize Indicates the size of data which optionValue point to, whic cannot be <b>0</b>.
548  * The common error codes are as follows:
549  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if the option is invalid, optionValue is NULL or valueSize is Zero.
550  * @return Returns <b>SOFTBUS_INVALID_SESSION_ID</b> if the sessionId is invalid.
551  * @return Returns <b>SOFTBUS_TRANS_SESSION_NO_ENABLE</b> if the session current be not enabled.
552  * @return Returns <b>SOFTBUS_OK</b>if the function is called successfully, return other internal errorcodes otherwise.
553  * @since 1.0
554  * @version 1.0
555  */
556 
557 int GetSessionOption(int sessionId, SessionOption option, void* optionValue, uint32_t valueSize);
558 
559 #ifdef __cplusplus
560 }
561 #endif
562 #endif  // SESSION_H
563