• 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  * @file session.h
32  *
33  * @brief Declares unified data transmission interfaces.
34  *
35  * This file provides data transmission capabilities, including creating and removing a session server,
36  * opening and closing sessions, receiving data, and querying basic session information. \n
37  * After multiple nearby devices are discovered and networked, these interfaces can be used to
38  * transmit data across devices. \n
39  *
40  * @since 1.0
41  * @version 1.0
42  */
43 #ifndef SESSION_H
44 #define SESSION_H
45 
46 #include <stdint.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 /**
52  * @brief business type of session
53  *
54  * @since 1.0
55  * @version 1.0
56  */
57 typedef enum {
58     TYPE_MESSAGE = 1,
59     TYPE_BYTES,
60     TYPE_FILE,
61     TYPE_STREAM,
62     TYPE_BUTT,
63 } SessionType;
64 
65 typedef enum  {
66     INVALID = -1,
67     /*
68      * Send any segment of a frame each time.
69      */
70     RAW_STREAM,
71     /*
72      * Send a whole video frame each time.
73      */
74     COMMON_VIDEO_STREAM,
75     /*
76      * Send a whole audio frame each time.
77      */
78     COMMON_AUDIO_STREAM,
79     /*
80      * Slice frame mode.
81      */
82     VIDEO_SLICE_STREAM,
83 } StreamType;
84 
85 typedef enum  {
86     LINK_TYPE_WIFI_WLAN_5G = 1,
87     LINK_TYPE_WIFI_WLAN_2G = 2,
88     LINK_TYPE_WIFI_P2P = 3,
89     LINK_TYPE_BR = 4,
90     LINK_TYPE_MAX = 4,
91 } LinkType;
92 
93 /**
94  * @brief session attribute.
95  *
96  * control the attribute of session.
97  *
98  * @since 1.0
99  * @version 1.0
100  */
101 typedef struct {
102     /** @brief dataType{@link SessionType} */
103     int dataType;
104     int linkTypeNum;
105     LinkType linkType[LINK_TYPE_MAX];
106     union {
107         struct StreamAttr {
108             int streamType;
109         } streamAttr;
110     } attr;
111 } SessionAttribute;
112 
113 typedef struct {
114     char *buf;
115     int bufLen;
116 } StreamData;
117 
118 typedef struct {
119     int type;
120     int64_t value;
121 } TV;
122 
123 typedef struct {
124     int frameType;
125     int64_t timeStamp;
126     int seqNum;
127     int seqSubNum;
128     int level;
129     int bitMap;
130     int tvCount;
131     TV *tvList;
132 } StreamFrameInfo;
133 
134 typedef enum {
135     QOS_IMPROVE = 0,
136     QOS_RECOVER = 1
137 } QosQuality;
138 
139 typedef enum {
140     TRANS_STREAM_QUALITY_EVENT = 1,
141     TRANS_CHANNEL_QUALITY_EVENT,
142     TRANS_CAN_DELAY_EVENT,
143     TRANS_CANT_DELAY_EVENT,
144     QOS_EVENT_MAX
145 } QosEvent;
146 
147 typedef enum {
148     WIFI_CHANNEL_QUALITY = 1,
149     FRAME_REALTIME_STATUS = 2,
150     BANDWIDTH_ESTIMATE_VALUE = 3,
151     JITTER_DETECTION_VALUE = 4,
152     STREAM_TRAFFIC_STASTICS = 5,
153 } TransEnumEventType;
154 
155 typedef struct {
156     int32_t channel;
157     int32_t score;
158 } WifiChannelQuality;
159 
160 typedef struct {
161     int32_t streamId;
162     int32_t seqNum;
163     int32_t level;
164     int32_t transStatus;
165     int32_t interval;
166 } FrameStatus;
167 
168 typedef struct {
169     uint32_t trend;
170     uint32_t rate;  /* kbps */
171 } BandwidthDetection;
172 
173 typedef struct {
174     int32_t jitterLevel;
175     uint32_t bufferTime;  /* ms */
176 } JitterEstimation;
177 
178 typedef struct {
179     uint64_t statisticsGotTime; /* time point that stream traficc statistics are obtained (ms) */
180     uint64_t periodRecvBits;
181     uint32_t pktNum;
182     uint32_t periodRecvPkts;
183     uint32_t periodRecvPktLoss;
184     uint32_t periodRecvRate; /* kbps */
185     uint64_t periodRecvRateBps; /* bps */
186     uint32_t periodRtt; /* ms */
187     uint32_t periodRecvPktLossHighPrecision; /* for example when lost rate is 1.10%, then 110 will returned */
188     uint32_t periodSendLostPkts;
189     uint32_t periodSendPkts;
190     uint32_t periodSendPktLossHighPrecision; /* for example when lost rate is 1.10%, then 110 will returned */
191     uint64_t periodSendBits;
192     uint64_t periodSendRateBps; /* bps */
193 } StreamStatistics;
194 
195 typedef struct {
196     TransEnumEventType type;
197     union {
198         WifiChannelQuality wifiChannelInfo;
199         FrameStatus frameStatusInfo;
200         BandwidthDetection bandwidthInfo;
201         JitterEstimation jitterInfo;
202         StreamStatistics appStatistics;
203     } info;
204 } QosTv;
205 
206 /**
207  * @brief Defines session callbacks.
208  *
209  * When a session is opened or closed, or there is data to process, the related callback is invoked.
210  *
211  * @since 1.0
212  * @version 1.0
213  */
214 typedef struct {
215     /**
216      * @brief Called when a session is opened.
217      *
218      * This function can be used to verify the session or initialize resources related to the session.
219      *
220      * @param sessionId Indicates the session ID.
221      * @param result 0 if the session is opened successfully, returns an error code otherwise.
222      * @return Returns <b>0</b> if the session connection is accepted; returns a non-zero value
223      * otherwise (you do not need to call {@link CloseSession} to close the session).
224      * @since 1.0
225      * @version 1.0
226      */
227     int (*OnSessionOpened)(int sessionId, int result);
228 
229     /**
230      * @brief Called when a session is closed.
231      *
232      * This function can be used to release resources related to the session.
233      * You do not need to call {@link CloseSession}.
234      *
235      * @param sessionId Indicates the session ID.
236      * @since 1.0
237      * @version 1.0
238      */
239     void (*OnSessionClosed)(int sessionId);
240 
241     /**
242      * @brief Called when data is received.
243      *
244      * This function is used to notify that data is received.
245      *
246      * @param sessionId Indicates the session ID.
247      * @param data Indicates the pointer to the data received.
248      * @param dataLen Indicates the length of the data received.
249      * @since 1.0
250      * @version 1.0
251      */
252     void (*OnBytesReceived)(int sessionId, const void *data, unsigned int dataLen);
253 
254     /**
255      * @brief Called when message is received.
256      *
257      * This function is used to notify that message is received.
258      *
259      * @param sessionId Indicates the session ID.
260      * @param data Indicates the pointer to the message data received.
261      * @param dataLen Indicates the length of the message received.
262      * @since 1.0
263      * @version 1.0
264      */
265     void (*OnMessageReceived)(int sessionId, const void *data, unsigned int dataLen);
266 
267     void (*OnStreamReceived)(int sessionId, const StreamData *data, const StreamData *ext,
268         const StreamFrameInfo *param);
269 
270     /**
271      * @brief Called when QoS information is retrieved.
272      *
273      * This function is used to notify that QoS information is retrieved.
274      *
275      * @param sessionId Indicates the session ID.
276      * @param eventId Indicates the type of QoS information, e.g., channel quality and stream quality
277      * @param tvCount Indicates the number of structure returned in the fourth parameters, i.e., tvList.
278      * @param tvList Indicates the detailed information of data transmission.
279      * @since 1.0
280      * @version 1.0
281      */
282     void (*OnQosEvent)(int sessionId, int eventId, int tvCount, const QosTv *tvList);
283 } ISessionListener;
284 
285 typedef struct {
286     int (*OnReceiveFileStarted)(int sessionId, const char *files, int fileCnt);
287     int (*OnReceiveFileProcess)(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal);
288     void (*OnReceiveFileFinished)(int sessionId, const char *files, int fileCnt);
289     void (*OnFileTransError)(int sessionId);
290 } IFileReceiveListener;
291 
292 typedef struct {
293     int (*OnSendFileProcess)(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal);
294     int (*OnSendFileFinished)(int sessionId, const char *firstFile);
295     void (*OnFileTransError)(int sessionId);
296 } IFileSendListener;
297 
298 /**
299  * @brief Creates a session server based on a package name and session name.
300  *
301  * A maximum of 18 session servers can be created.
302  *
303  * @param pkgName Indicates the pointer to the package name, which can be used to check whether the
304  * session server is in this package. The value cannot be empty and can contain a maximum of 64 characters.
305  * @param sessionName Indicates the pointer to the session name, which is the unique ID of the session server.
306  * The value cannot be empty and can contain a maximum of 64 characters.
307  * @param listener Indicates the pointer to the session callback structure, which cannot be empty.
308  * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
309  * @see RemoveSessionServer
310  * @since 1.0
311  * @version 1.0
312  */
313 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener);
314 
315 /**
316  * @brief Removes a session server based on a package name and session name.
317  *
318  * @param pkgName Indicates the pointer to the name of the registered package, which can be used to check
319  * whether the session server is in this package. The value cannot be empty and can contain a maximum of 64 characters.
320  * @param sessionName Indicates the pointer to the session name. The value cannot be empty and can contain
321  * a maximum of 64 characters.
322  * @return Returns <b>0</b> if the operation is successful, returns <b>-1</b> otherwise.
323  * @see CreateSessionServer
324  * @since 1.0
325  * @version 1.0
326  */
327 int RemoveSessionServer(const char *pkgName, const char *sessionName);
328 
329 /**
330  * @brief Initiate a session open request, which is an asynchronous process.
331  *
332  * The session connection is opened based on the service name to trigger the first packet interaction process.
333  * According to the {@link OnSessionOpened} Notify the user whether the session is successfully opened.
334  * Data can be transmitted only after the session is successfully opened.
335  *
336  * @param mySessionName local session name.
337  * @param peerSessionName remote session name.
338  * @param peerDeviceId remote device id.
339  * @param groupId group id.
340  * @param attr session attribute {@link SessionAttribute}.
341  * @return return sessionId if the session is opened successfully, returns an error code otherwise.
342  * @since 1.0
343  * @version 1.0
344  */
345 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId,
346     const char *groupId, const SessionAttribute* attr);
347 
348 /**
349  * @brief Closes a connected session based on a session ID.
350  *
351  * @param sessionId Indicates the session ID.
352  * @return no return value.
353  * @since 1.0
354  * @version 1.0
355  */
356 void CloseSession(int sessionId);
357 
358 /**
359  * @brief Sends data based on a session ID.
360  *
361  * @param sessionId Indicates the session ID.
362  * @param data Indicates the pointer to the data to send, which cannot be <b>NULL</b>.
363  * @param len Indicates the length of the data to send. The maximum length cannot exceed 984 characters.
364  * @return Returns <b>0</b> if the function is called successfully; returns <b>-1</b> otherwise.
365  * @since 1.0
366  * @version 1.0
367  */
368 int SendBytes(int sessionId, const void *data, unsigned int len);
369 
370 /**
371  * @brief Sends message based on a session ID.
372  *
373  * @param sessionId Indicates the session ID.
374  * @param data Indicates the pointer to the message data to send, which cannot be <b>NULL</b>.
375  * @param len Indicates the length of the message to send.
376  * @return Returns <b>0</b> if the function is called successfully, returns an error code otherwise.
377  * @since 1.0
378  * @version 1.0
379  */
380 int SendMessage(int sessionId, const void *data, unsigned int len);
381 
382 int SendStream(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
383 
384 /**
385  * @brief Obtains the session name registered by the local device based on the session ID.
386  *
387  * @param sessionId Indicates the session ID.
388  * @param sessionName Indicates the pointer to the buffer for storing the session name.
389  * @param len Indicates the length of the buffer.
390  * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
391  * @since 1.0
392  * @version 1.0
393  */
394 int GetMySessionName(int sessionId, char *sessionName, unsigned int len);
395 
396 /**
397  * @brief Obtains the session name registered by the peer device based on the session ID.
398  *
399  * @param sessionId Indicates the session ID.
400  * @param sessionName Indicates the pointer to the buffer for storing the session name.
401  * @param len Indicates the length of the buffer.
402  * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
403  * @since 1.0
404  * @version 1.0
405  */
406 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len);
407 
408 /**
409  * @brief Obtains the peer device ID based on a session ID.
410  *
411  * @param sessionId Indicates the session ID.
412  * @param devId Indicates the pointer to the buffer for storing the device ID.
413  * @param len Indicates the length of the buffer.
414  * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
415  * @since 1.0
416  * @version 1.0
417  */
418 int GetPeerDeviceId(int sessionId, char *devId, unsigned int len);
419 
420 int GetSessionSide(int sessionId);
421 
422 int SetFileReceiveListener(const char *pkgName, const char *sessionName,
423     const IFileReceiveListener *recvListener, const char *rootDir);
424 
425 int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener);
426 
427 int SendFile(int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
428 
429 int32_t QosReport(int32_t sessionId, int32_t appType, int32_t quality);
430 
431 #ifdef __cplusplus
432 }
433 #endif
434 #endif  // SESSION_H
435