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 Bluetooth
18  * @{
19  *
20  * @brief Defines a bluetooth system that provides basic blurtooth connection and profile functions,
21  *        including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc.
22  */
23 
24 /**
25  * @file transport.h
26  *
27  * @brief Declares TRANSPORT service functions, including basic and observer functions.
28  */
29 
30 #ifndef TRANSPORT_H
31 #define TRANSPORT_H
32 
33 #include <stdint.h>
34 #include "packet.h"
35 #include "raw_address.h"
36 
37 namespace OHOS {
38 namespace bluetooth {
39 class DataTransport;
40 
41 /**
42  * @brief This DataTransportObserver class provides a set of methods that from
43  *        RFCOMM or L2CAP's callback.
44  */
45 class DataTransportObserver {
46 public:
47     /**
48      * @brief The event is triggered when server accept a new connection.
49      *
50      * @param addr remote device address.
51      * @param port rfcomm scn or l2cap psm.
52      */
53     virtual void OnConnectIncoming(const RawAddress &addr, uint16_t port) = 0;
54 
55     /**
56      * @brief The event is triggered when server accept a new connection timeout.
57      *
58      * @param addr remote device address.
59      */
60     virtual void OnIncomingDisconnected(const RawAddress &addr) = 0;
61 
62     /**
63      * @brief The event is triggered when connection complete successfully.
64      *
65      * @param transport the pointer of the transport.
66      */
67     virtual void OnConnected(DataTransport *transport, uint16_t sendMTU, uint16_t recvMTU) = 0;
68 
69     /**
70      * @brief The event is triggered when the connection is disconnected.
71      *
72      * @param transport the pointer of the transport.
73      */
74     virtual void OnDisconnected(DataTransport *transport) = 0;
75 
76     /**
77      * @brief The event is triggered when the disconnection process is successful.
78      *
79      * @param transport the pointer of the transport.
80      */
81     virtual void OnDisconnectSuccess(DataTransport *transport) = 0;
82 
83     /**
84      * @brief The event is triggered when data is received from stack.
85      *
86      * @param transport the pointer of the transport.
87      */
88     virtual void OnDataAvailable(DataTransport *transport) = 0;
89 
90     /**
91      * @brief The event is triggered when data is received from stack.
92      *
93      * @param transport the pointer of the transport.
94      */
95     virtual void OnDataAvailable(DataTransport *transport, Packet *pkt) = 0;
96 
97     /**
98      * @brief The event is triggered when peer or RFCOMM/L2CAP is not available to receive data.
99      *
100      * @param transport the pointer of the transport.
101      */
OnDataBusy(DataTransport * transport,uint8_t isBusy)102     virtual void OnDataBusy(DataTransport *transport, uint8_t isBusy) {};
103 
104     /**
105      * @brief The event is triggered when process is failed.
106      *
107      * @param transport the pointer of the transport.
108      * @param errType connection failed, disconnection failed.
109      */
110     virtual void OnTransportError(DataTransport *transport, int errType) = 0;
111 
112     /**
113      * @brief Destructor.
114      */
~DataTransportObserver()115     virtual ~DataTransportObserver()
116     {}
117 };
118 
119 /**
120  * @brief This DataTransport class provides a set of methods that
121  *        the upper layer to interact with rfcomm or L2CAP
122  */
123 class DataTransport {
124 public:
125     /**
126      * @brief Client connect rfcomm channel.
127      *
128      * @return int
129      */
130     virtual int Connect() = 0;
131 
132     /**
133      * @brief Client disconnect rfcomm connection.
134      *
135      * @return int
136      */
137     virtual int Disconnect() = 0;
138 
139     /**
140      * @brief Server register to RFCOMM and wait for the client to connect.
141      *
142      * @return int
143      */
144     virtual int RegisterServer() = 0;
145 
146     /**
147      * @brief Server close and disconnect all channels connected to the server.
148      *
149      * @return int
150      */
151     virtual int RemoveServer(bool isDisable) = 0;
152 
153     /**
154      * @brief Server accept the connection request.
155      *
156      * @param addr remote device address.
157      * @param port rfcomm's scn or l2cap's psm.
158      * @return int
159      */
160     virtual int AcceptConnection(const RawAddress &addr, uint16_t port) = 0;
161 
162     /**
163      * @brief Server reject the connection request
164      *
165      * @param addr remote device address.
166      * @param port rfcomm's scn or l2cap's psm.
167      * @return int
168      */
169     virtual int RejectConnection(const RawAddress &addr, uint16_t port) = 0;
170 
171     /**
172      * @brief This function is used to get the data sent by the peer from RFCOMM/L2cap.
173      *
174      * @param pkt receive data from RFCOMM/L2cap.
175      * @return int
176      */
177     virtual int Read(Packet **pkt) = 0;
178 
179     /**
180      * @brief This function is used to write the data to RFCOMM/L2cap.
181      *
182      * @param pkt send data to RFCOMM/L2cap.
183      * @return int
184      */
185     virtual int Write(Packet *pkt) = 0;
186 
187     /**
188      * @brief This function is used to get the Bluetooth address of the peer of the connected channel.
189      *
190      * @return RawAddress
191      */
192     virtual RawAddress GetRemoteAddress() = 0;
193 
194     /**
195      * @brief Destructor.
196      */
~DataTransport()197     virtual ~DataTransport()
198     {}
199 };
200 }  // namespace bluetooth
201 }  // namespace OHOS
202 #endif  // TRANSPORT_H
203