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 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 <cstdint> 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 typedef struct { 94 /** @brief dataType{@link SessionType} */ 95 int dataType; 96 int linkTypeNum; 97 LinkType linkType[LINK_TYPE_MAX]; 98 union { 99 struct StreamAttr { 100 int streamType; 101 } streamAttr; 102 } attr; 103 } SessionAttribute; 104 105 typedef struct { 106 char *buf; 107 int bufLen; 108 } StreamData; 109 110 typedef struct { 111 int type; 112 int64_t value; 113 } TV; 114 115 typedef struct { 116 int frameType; 117 int64_t timeStamp; 118 int seqNum; 119 int seqSubNum; 120 int level; 121 int bitMap; 122 int tvCount; 123 TV *tvList; 124 } FrameInfo; 125 126 enum FrameType { 127 NONE, 128 VIDEO_I, 129 VIDEO_P, 130 VIDEO_MAX = 50, 131 RADIO = VIDEO_MAX + 1, 132 RADIO_MAX = 100, 133 }; 134 // APP should update StreamFrameInfo each time. 135 struct StreamFrameInfo { 136 uint32_t streamId = 0; 137 uint32_t seqNum = 0; 138 uint32_t level = 0; 139 FrameType frameType = NONE; 140 uint32_t timestamp = 0; 141 uint32_t bitrate = 0; 142 }; 143 144 constexpr uint32_t DEVICE_ID_SIZE_MAX = 65; 145 constexpr uint32_t CHAR_ARRAY_SIZE = 100; 146 147 typedef struct { 148 int (*OnSessionOpened)(int sessionId, int result); 149 void (*OnSessionClosed)(int sessionId); 150 void (*OnBytesReceived)(int sessionId, const void *data, unsigned int dataLen); 151 void (*OnMessageReceived)(int sessionId, const void *data, unsigned int dataLen); 152 void (*OnStreamReceived)(int sessionId, const StreamData *data, const StreamData *ext, 153 const StreamFrameInfo *param); 154 } ISessionListener; 155 156 typedef struct { 157 int (*OnReceiveFileStarted)(int sessionId, const char *files, int fileCnt); 158 int (*OnReceiveFileProcess)(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal); 159 void (*OnReceiveFileFinished)(int sessionId, const char *files, int fileCnt); 160 void (*OnFileTransError)(int sessionId); 161 } IFileReceiveListener; 162 163 typedef struct { 164 int (*OnSendFileProcess)(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal); 165 int (*OnSendFileFinished)(int sessionId, const char *firstFile); 166 void (*OnFileTransError)(int sessionId); 167 } IFileSendListener; 168 169 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener); 170 171 int RemoveSessionServer(const char *pkgName, const char *sessionName); 172 173 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId, const char *groupId, 174 const SessionAttribute *attr); 175 void OpenSessionResult(void); 176 177 void CloseSession(int sessionId); 178 179 int SendBytes(int sessionId, const void *data, unsigned int len); 180 181 int SendMessage(int sessionId, const void *data, unsigned int len); 182 183 int SendStream(int sessionId, const StreamData *data, const StreamData *ext, const FrameInfo *param); 184 185 int GetMySessionName(int sessionId, char *sessionName, unsigned int len); 186 187 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len); 188 189 int GetPeerDeviceId(int sessionId, char *devId, unsigned int len); 190 191 int GetSessionSide(int sessionId); 192 193 int SetFileReceiveListener(const char *pkgName, const char *sessionName, const IFileReceiveListener *recvListener, 194 const char *rootDir); 195 196 int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener); 197 198 int SendFile(int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt); 199 #ifdef __cplusplus 200 } 201 #endif 202 #endif // SESSION_H