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 <stdint.h>
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51/**
52 * @brief business type of session
53 *
54 * @since 1.0
55 * @version 1.0
56 */
57typedef enum {
58    TYPE_MESSAGE = 1,
59    TYPE_BYTES,
60    TYPE_FILE,
61    TYPE_STREAM,
62    TYPE_BUTT,
63} SessionType;
64
65typedef 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
85typedef 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
93typedef 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
105typedef struct {
106    char *buf;
107    int bufLen;
108} StreamData;
109
110typedef struct {
111    int type;
112    int64_t value;
113} TV;
114
115typedef 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
126typedef struct {
127    int (*OnSessionOpened)(int sessionId, int result);
128    void (*OnSessionClosed)(int sessionId);
129    void (*OnBytesReceived)(int sessionId, const void *data, unsigned int dataLen);
130    void (*OnMessageReceived)(int sessionId, const void *data, unsigned int dataLen);
131    void (*OnStreamReceived)(int sessionId, const StreamData *data, const StreamData *ext, const FrameInfo *param);
132} ISessionListener;
133
134typedef struct {
135    int (*OnReceiveFileStarted)(int sessionId, const char *files, int fileCnt);
136    int (*OnReceiveFileProcess)(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal);
137    void (*OnReceiveFileFinished)(int sessionId, const char *files, int fileCnt);
138    void (*OnFileTransError)(int sessionId);
139} IFileReceiveListener;
140
141typedef struct {
142    int (*OnSendFileProcess)(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal);
143    int (*OnSendFileFinished)(int sessionId, const char *firstFile);
144    void (*OnFileTransError)(int sessionId);
145} IFileSendListener;
146
147int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener);
148
149int RemoveSessionServer(const char *pkgName, const char *sessionName);
150
151int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId, const char *groupId,
152                const SessionAttribute *attr);
153
154void CloseSession(int sessionId);
155
156int SendBytes(int sessionId, const void *data, unsigned int len);
157
158int SendMessage(int sessionId, const void *data, unsigned int len);
159
160int SendStream(int sessionId, const StreamData *data, const StreamData *ext, const FrameInfo *param);
161
162int GetMySessionName(int sessionId, char *sessionName, unsigned int len);
163
164int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len);
165
166int GetPeerDeviceId(int sessionId, char *devId, unsigned int len);
167
168int GetSessionSide(int sessionId);
169
170int SetFileReceiveListener(const char *pkgName, const char *sessionName, const IFileReceiveListener *recvListener,
171                           const char *rootDir);
172
173int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener);
174
175int SendFile(int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
176#ifdef __cplusplus
177}
178#endif
179#endif // SESSION_H
180