1 /*
2  * Copyright (c) 2022 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 socket.h
32  *
33  * @brief Declares unified data transmission interfaces.
34  *
35  * This file provides data transmission capabilities, including creating and removing a socket server,
36  * opening and closing sessions, receiving data, and querying basic socket 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 SOCKET_H
44 #define SOCKET_H
45 
46 #include <stdint.h>
47 #include <string>
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 #define MAX_MAC_LEN 18
53 
54 /**
55  * @brief Enumerates the data types.
56  *
57  * @since 2.0
58  * @version 2.0
59  */
60 typedef enum {
61     DATA_TYPE_MESSAGE = 1,  /**< Message */
62     DATA_TYPE_BYTES,        /**< Bytes */
63     DATA_TYPE_FILE,         /**< File */
64     DATA_TYPE_RAW_STREAM,   /**< Raw data stream */
65     DATA_TYPE_VIDEO_STREAM, /**< Video data stream*/
66     DATA_TYPE_AUDIO_STREAM, /**< Audio data stream*/
67     DATA_TYPE_SLICE_STREAM, /**< Video slice stream*/
68     DATA_TYPE_BUTT,
69 } TransDataType;
70 
71 /**
72  * @brief Defines the description of the socket.
73  *
74  * @since 2.0
75  * @version 2.0
76  */
77 typedef struct {
78     char *name;             /**< My socket name */
79     char *peerName;         /**< Peer socket name */
80     char *peerNetworkId;    /**< Peer network ID */
81     char *pkgName;          /**< Package name */
82     TransDataType dataType; /**< Data type */
83 } SocketInfo;
84 
85 /**
86  * @brief Defines the description of the socket.
87  *
88  * @since 2.0
89  * @version 2.0
90  */
91 typedef struct {
92     char *name;             /**< Peer socket name */
93     char *networkId;         /**< Peer network ID */
94     char *pkgName;          /**< Peer package name */
95     TransDataType dataType; /**< Data type of peer socket*/
96 } PeerSocketInfo;
97 
98 /**
99  * @brief Enumerates the reason of the shutdown event.
100  *
101  * @since 2.0
102  * @version 2.0
103  */
104 typedef enum {
105     SHUTDOWN_REASON_UNKNOWN,       /**< Shutdown for unknown reason */
106     SHUTDOWN_REASON_LOCAL,         /**< Shutdown by local process */
107     SHUTDOWN_REASON_PEER,          /**< Shutdown by peer process */
108     SHUTDOWN_REASON_LNN_CHANGED,   /**< Shutdown for LNN changed */
109     SHUTDOWN_REASON_CONN_CHANGED,  /**< Shutdown for CONN Changed */
110     SHUTDOWN_REASON_TIMEOUT,       /**< Shutdown for timeout */
111     SHUTDOWN_REASON_SEND_FILE_ERR, /**< Shutdown for sending file error */
112     SHUTDOWN_REASON_RECV_FILE_ERR, /**< Shutdown for receiving file error */
113     SHUTDOWN_REASON_RECV_DATA_ERR, /**< Shutdown for receiving data error */
114     SHUTDOWN_REASON_UNEXPECTED,    /**< Shutdown for unexpected reason */
115 } ShutdownReason;
116 
117 /**
118  * @brief Enumerates the Qos transform reliability.
119  *
120  * @since 2.0
121  * @version 2.0
122  */
123 typedef enum {
124     QOS_RELIABILITY_NONE = 1, /**< Unreliable */
125     QOS_RELIABILITY_FULL,     /**< Reliable */
126     QOS_RELIABILITY_HALF,     /**< Semi-reliable */
127     QOS_RELIABILITY_BUTT,
128 } QosTransReliability;
129 
130 /**
131  * @brief Enumerates the QoS Assurance Items.
132  *
133  * @since 2.0
134  * @version 2.0
135  */
136 typedef enum {
137     QOS_TYPE_MIN_BW,            /**< Minimum bandwidth. */
138     QOS_TYPE_MAX_LATENCY,       /**< Maximum latency. */
139     QOS_TYPE_MIN_LATENCY,       /**< Minimum latency. */
140     QOS_TYPE_MAX_WAIT_TIMEOUT,  /**< Maximum timeout of transmission. */
141     QOS_TYPE_MAX_BUFFER,        /**< Maximum cache. */
142     QOS_TYPE_FIRST_PACKAGE,     /**< First packet size. */
143     QOS_TYPE_MAX_IDLE_TIMEOUT,  /**< Maximum idle time. */
144     QOS_TYPE_TRANS_RELIABILITY, /**< Transmission reliability. */
145     QOS_TYPE_BUTT,
146 } QosType;
147 
148 /**
149  * @brief Defines the Qos Type-Value structure.
150  *
151  * @since 2.0
152  * @version 2.0
153  */
154 typedef struct {
155     QosType qos;   /**< Qos type {@link QosType} */
156     int32_t value; /**< Value of Qos types */
157 } QosTV;
158 
159 /**
160  * @brief Enumerates the file types.
161  *
162  * @since 2.0
163  * @version 2.0
164  */
165 typedef enum {
166     FILE_EVENT_SEND_PROCESS,     /**< Sending file */
167     FILE_EVENT_SEND_FINISH,      /**< Send file end */
168     FILE_EVENT_SEND_ERROR,       /**< Send file failed */
169     FILE_EVENT_RECV_UPDATE_PATH, /**< Update root directory*/
170     FILE_EVENT_RECV_START,       /**< Receive file start */
171     FILE_EVENT_RECV_PROCESS,     /**< Receiving file */
172     FILE_EVENT_RECV_FINISH,      /**< Receive file end */
173     FILE_EVENT_RECV_ERROR,       /**< Receive file failed */
174     FILE_EVENT_BUTT,
175 } FileEventType;
176 
177 typedef struct {
178     FileEventType type;                  /**< Event data type of the files*/
179     const char **files;                  /**< File path*/
180     uint32_t fileCnt;                    /**< Count of the files*/
181     uint64_t bytesProcessed;             /**< Send or receive bytes of the files*/
182     uint64_t bytesTotal;                 /**< Total bytes of the files*/
183     const char *(*UpdateRecvPath)(void); /**< Update file receiving directory*/
184 } FileEvent;
185 
186 /**
187  * @brief Defines the extended stream data.
188  *
189  * @since 2.0
190  * @version 2.0
191  */
192 typedef struct {
193     int32_t type;  /**< Extended data type {@link TransEnumEventType} */
194     int64_t value; /**< Value of the extended data */
195 } TV;
196 
197 /**
198  * @brief Defines the frame information for stream transmission.
199  *
200  * @since 1.0
201  * @version 1.0
202  */
203 typedef struct {
204     int32_t frameType; /**< Frame type, which can be I-frame or P-frame. */
205     int64_t timeStamp; /**< Timestamp. */
206     int32_t seqNum;    /**< Sequence number. */
207     int32_t seqSubNum; /**< Sequence number of the slice. */
208     int32_t level;     /**< Scalable video coding level. <b>0</b> stands for the base level,
209                         <b>1</b> for level 1, and <b>2</b> for level 2. */
210     int32_t bitMap;    /**< Bitmap, which indicates the start or end slice of a frame. */
211     int32_t tvCount;   /**< Number of scalable tag-values (TVs). */
212     TV *tvList;        /**< Pointer to the TV list. */
213 } StreamFrameInfo;
214 
215 /**
216  * @brief Defines the stream data.
217  *
218  * @since 1.0
219  * @version 1.0
220  */
221 typedef struct {
222     char *buf;  /**< Pointer to the buffer for storing the stream data */
223     int bufLen; /**< Length of the buffer */
224 } StreamData;
225 
226 /**
227  * @brief Enumerates the QoS feedback types.
228  *
229  * @since 2.0
230  * @version 2.0
231  */
232 typedef enum {
233     QOS_SATISFIED,     /**< Feedback on satisfied quality */
234     QOS_NOT_SATISFIED, /**< Feedback on not satisfied quality */
235 } QoSEvent;
236 
237 /**
238  * @brief Defines socket callbacks.
239  *
240  * When a socket is opened or closed, or there is data to process, the related callback is invoked.
241  *
242  * @since 2.0
243  * @version 2.0
244  */
245 typedef struct {
246     /**
247      * @brief Called when a socket is bind.
248      *
249      * This callback is invoked to verify the socket or initialize resources related to the socket.
250      *
251      * @param socket Indicates the unique socket fd; socket fd = <b>0</b> if the bind is failed.
252      * @since 2.0
253      * @version 2.0
254      */
255     void (*OnBind)(int32_t socket, PeerSocketInfo info);
256 
257     /**
258      * @brief Called when a socket is closed.
259      *
260      * This callback is invoked to release resources related to the socket.
261      *
262      * @param socket Indicates the unique socket fd.
263      * @param reason Indicates the reason for closing the socket.
264      * @since 2.0
265      * @version 2.0
266      */
267     void (*OnShutdown)(int32_t socket, ShutdownReason reason);
268 
269     /**
270      * @brief Called when bytes data is received.
271      *
272      * This callback is invoked to notify that data is received.
273      *
274      * @param socket Indicates the unique socket fd.
275      * @param data Indicates the pointer to the bytes data received.
276      * @param dataLen Indicates the length of the bytes data received.
277      * @since 2.0
278      * @version 2.0
279      */
280     void (*OnBytes)(int32_t socket, const void *data, uint32_t dataLen);
281 
282     /**
283      * @brief Called when message data is received.
284      *
285      * This callback is invoked to notify that message data is received.
286      *
287      * @param socket Indicates the unique socket fd.
288      * @param data Indicates the pointer to the message data received.
289      * @param dataLen Indicates the length of the message data received.
290      * @since 2.0
291      * @version 2.0
292      */
293     void (*OnMessage)(int32_t socket, const void *data, uint32_t dataLen);
294 
295     /**
296      * @brief Called when stream data is received.
297      *
298      * This callback is invoked to notify that stream data is received.
299      *
300      * @param socket Indicates the unique socket fd.
301      * @param data Indicates the pointer to the stream data received.
302      * @param ext Indicates the pointer to the extended service data received.
303      * @param param Indicates the pointer to the stream data frame information.
304      * @since 2.0
305      * @version 2.0
306      */
307     void (*OnStream)(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
308 
309     /**
310      * @brief Called when file data is received.
311      *
312      * This callback is invoked to notify that file data is received.
313      *
314      * @param socket Indicates the unique socket fd.
315      * @param event Indicates the file event.
316      * @param data Indicates the pointer to the file data received.
317      * @since 2.0
318      * @version 2.0
319      */
320     void (*OnFile)(int32_t socket, FileEvent *event);
321 
322     /**
323      * @brief Called when QoS state is changed.
324      *
325      * This callback is invoked to notify that QoS state is changed.
326      *
327      * @param socket Indicates the unique socket fd.
328      * @param event Indicates the type of QoS state change.
329      * @param qos[] Indicates the QoS status that we can provide.
330      * @since 2.0
331      * @version 2.0
332      */
333     void (*OnQos)(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount);
334 } ISocketListener;
335 
336 /**
337  * @brief Creates a socket.
338  *
339  * A maximum of 10 socket can be created.
340  *
341  * @param info Indicates the description of the socket structure.
342  * It is the unique identifier of the upper-layer service. The value cannot be empty or exceed 64 characters.
343  *
344  * @return Returns <b>socket fd</b> if the socket creation is successful; returns <b>-1</b> otherwise.
345  * @since 2.0
346  * @version 2.0
347  */
348 int32_t Socket(SocketInfo info);
349 
350 /**
351  * @brief Listens a socket, which is called by server.
352  *
353  * @param socket Indicates the the unique socket fd.
354  * @param qos Indicates the QoS requirements for socket. The value cannot be empty.
355  * @param listener Indicates the pointer to the socket callback.
356  *
357  * @return Returns <b>0</b> if the listen creation is successful; returns <b>-1</b> otherwise.
358  * @since 2.0
359  * @version 2.0
360  */
361 int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
362 
363 /**
364  * @brief Binds a socket, which is called by client.
365  *
366  * {@link OnBind} is invoked to return whether the socket is successfully bind.
367  * Data can be transmitted only after the socket is successfully bind.
368  *
369  * @param socket Indicates the the unique socket fd.
370  * @param qos Indicates the QoS requirements for socket. The value cannot be empty.
371  * @param listener Indicates the pointer to the socket callback.
372  *
373  * @return Returns <b>SOFTBUS_TRANS_INVALID_PARAM</b> if invalid parameters are detected.
374  * @return Returns <b>INVALID_SOCKET</b> if the operation fails.
375  * @return Returns the socket fd (an integer greater than <b>0</b>) if the socket is bind;
376  * returns an error code otherwise.
377  * @since 2.0
378  * @version 2.0
379  */
380 int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
381 
382 /**
383  * @example sendbytes_message_demo.c
384  */
385 
386 /**
387  * @brief Sends bytes data.
388  *
389  * @param socket Indicates the unique socket fd.
390  * @param data Indicates the pointer to the bytes data to send, which cannot be <b>NULL</b>.
391  * @param len Indicates the length of the bytes data to send.
392  *
393  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if invalid parameters are detected.
394  * @return Returns <b>SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT</b> if the bytes data exceeds the maximum limit.
395  * @return Returns <b>SOFTBUS_TRANS_INVALID_SOCKET</b> if <b>socket</b> is invalid.
396  * @return Returns <b>SOFTBUS_TRANS_SOCKET_NO_ENABLE</b> if the socket is not bind.
397  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful; returns an error code otherwise.
398  * @since 2.0
399  * @version 2.0
400  */
401 int32_t SendBytes(int32_t socket, const void *data, uint32_t len);
402 
403 /**
404  * @brief Sends message data.
405  *
406  * @param socket Indicates the unique socket fd.
407  * @param data Indicates the pointer to the message data to send, which cannot be <b>NULL</b>.
408  * @param len Indicates the length of the message data to send.
409  *
410  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>data</b> is <b>NULL</b> or <b>len</b> is zero.
411  * @return Returns <b>SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT</b> if the message data length exceeds the limit.
412  * @return Returns <b>SOFTBUS_INVALID_SOCKET</b> if <b>socket</b> is invalid.
413  * @return Returns <b>SOFTBUS_TRANS_SOCKET_NO_ENABLE</b> if the socket is not bind.
414  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful; returns an error code otherwise.
415  * @since 2.0
416  * @version 2.0
417  */
418 int32_t SendMessage(int32_t socket, const void *data, uint32_t len);
419 
420 /**
421  * @example sendstream_demo.c
422  */
423 
424 /**
425  * @brief Sends stream data.
426  *
427  * @param socket Indicates the unique socket fd.
428  * @param data Indicates the pointer to the stream data to send, which cannot be <b>NULL</b>.
429  * @param ext Indicates the pointer to the extended stream data to send, which cannot be <b>NULL</b>.
430  * @param param Indicates the pointer to the stream frame information, which cannot be <b>NULL</b>.
431  *
432  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if any of the input parameters is <b>NULL</b>.
433  * @return Returns <b>SOFTBUS_INVALID_SOCKET</b> if <b>socket</b> is invalid.
434  * @return Returns <b>SOFTBUS_TRANS_SOCKET_NO_ENABLE</b> if the socket is not bind.
435  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful; returns an error code otherwise.
436  * @since 2.0
437  * @version 2.0
438  */
439 int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
440 
441 /**
442  * @example sendfile_demo.c
443  */
444 
445 /**
446  * @brief Sends files data.
447  *
448  * @param socket Indicates the unique socket fd.
449  * @param sFileList Indicates the pointer to the source files data to send, which cannot be <b>NULL</b>.
450  * @param dFileList Indicates the pointer to the destination files data, which cannot be <b>NULL</b>.
451  * @param fileCnt Indicates the number of files data to send, which cannot be <b>0</b>.
452  *
453  * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>sFileList</b> is <b>NULL</b> or <b>fileCnt</b> is <b>0</b>.
454  * @return Returns <b>SOFTBUS_INVALID_SOCKET</b> if <b>socket</b> is invalid.
455  * @return Returns <b>SOFTBUS_TRANS_SOCKET</b> if the socket is not bind.
456  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful; returns an error code otherwise.
457  * @since 2.0
458  * @version 2.0
459  */
460 int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
461 
462 /**
463  * @brief Get socket based on a socket fd.
464  *
465  * @param socket Indicates the unique socket fd.
466  *
467  * @return Returns no value.
468  * @since 2.0
469  * @version 2.0
470  */
471 void Shutdown(int32_t socket);
472 
473 void DecompressMock(const unsigned char *bytes, const int length);
474 
475 
476 #ifdef __cplusplus
477 }
478 
479 void CompressMock(const std::string &json, const unsigned char *compressedBytes, int &compressedLength);
480 std::string GetUuidMock();
481 bool GetSendMessFlagMock();
482 void ResetSendMessFlagMock();
483 void ResetUuidMock();
484 
485 #endif
486 #endif // SOCKET_H
487