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