1 /*
2  * Copyright (C) 2021-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 #ifndef OBEX_SERVER_H
17 #define OBEX_SERVER_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <memory>
22 #include <unordered_map>
23 #include "dispatcher.h"
24 #include "obex_headers.h"
25 #include "obex_session.h"
26 #include "obex_socket_transport.h"
27 #include "obex_types.h"
28 
29 namespace OHOS {
30 namespace bluetooth {
31 /**
32  * @brief ObexServerConfig
33  */
34 struct ObexServerConfig {
35     bool useRfcomm_ = false;                 // If using Rfcomm, set to true
36     uint8_t rfcommScn_ = 0;                  // The server's number
37     uint16_t rfcommMtu_ = 0;                 // The maximum size of data received at a time with rfcomm
38     bool useL2cap_ = false;                  // If using L2Cap, set to true
39     uint16_t l2capPsm_ = 0;                  // l2cap psm
40     uint16_t l2capMtu_ = 0;                  // The maximum size of data received at a time with l2cap
41     bool isSupportSrm_ = false;              // Is Support Single Request Mode
42     bool isSupportReliableSession_ = false;  // Is Use Reliable Session
43 };
44 class ObexServerSession;
45 class ObexIncomingConnect;
46 class ObexServerObserver {
47 public:
48     virtual ~ObexServerObserver() = default;
49     virtual void OnTransportConnect(ObexIncomingConnect &incomingConnect);
50     virtual void OnTransportConnected(const std::string &btAddr);
51     virtual void OnTransportDisconnected(const std::string &btAddr);
52     virtual void OnTransportError(const std::string &btAddr, int errCd, const std::string &msg);
53 
54     virtual void OnError(const int errCd, const std::string &msg);
55     virtual void OnConnect(ObexServerSession &session, const ObexHeader &req) = 0;
56     virtual void OnDisconnect(ObexServerSession &session, const ObexHeader &req) = 0;
57     virtual void OnPut(ObexServerSession &session, const ObexHeader &req);
58     virtual void OnGet(ObexServerSession &session, const ObexHeader &req);
59     virtual void OnAbort(ObexServerSession &session, const ObexHeader &req);
60     virtual void OnSetPath(ObexServerSession &session, const ObexHeader &req);
61     virtual void OnAction(ObexServerSession &session, const ObexHeader &req);
62     virtual void OnSession(ObexServerSession &session, const ObexHeader &req);
63 
64     virtual void OnBusy(ObexServerSession &session, bool isBusy) const;
65 };
66 class ObexPrivateServer {
67 public:
68     /**
69      * @brief ObexPrivateServerConfig
70      */
71     struct ObexPrivateServerConfig {
72         uint16_t scn_ = 0;                       // The server's channel number
73         uint16_t mtu_ = 0;                       // The maximum size of data received at a time with rfcomm
74         bool isGoepL2capPSM_ = false;            // l2cap : true , rfcomm : false
75         bool isSupportSrm_ = false;              // Is Support Single Request Mode
76         bool isSupportReliableSession_ = false;  // Is Use Reliable Session
77     };
78     explicit ObexPrivateServer(
79         const ObexPrivateServerConfig &config, ObexServerObserver &observer, utility::Dispatcher &dispatcher);
80     virtual ~ObexPrivateServer() = default;
81     int Startup() const;
82     void Shutdown() const;
83     int RemoveSession(ObexServerSession &session) const;
84 
85 protected:
86     class ObexServerTransportObserver : public ObexTransportObserver {
87     public:
88         explicit ObexServerTransportObserver(ObexPrivateServer &obexServer);
89         ~ObexServerTransportObserver() override = default;
90         // Transport callback
91         void OnTransportConnectIncoming(ObexIncomingConnect &incomingConnect) override;
92         void OnTransportIncomingDisconnected(const std::string &btAddr) override;
93         void OnTransportConnected(ObexTransport &transport) override;
94         void OnTransportDisconnected(ObexTransport &transport) override;
95         void OnTransportDataAvailable(ObexTransport &transport, ObexPacket &obexPacket) override;
96         void OnTransportDataBusy(ObexTransport &transport, uint8_t isBusy) override;
97         void OnTransportError(ObexTransport &transport, int errCd) override;
98 
99     private:
100         void HandleDataAvailableConnect(ObexServerSession &serverSession, const ObexHeader &req);
101         ObexPrivateServer &obexServer_;
102         std::unique_ptr<bluetooth::ObexHeader> GetObexHeaderFromPacket(ObexPacket &obexPacket) const;
103         BT_DISALLOW_COPY_AND_ASSIGN(ObexServerTransportObserver);
104     };
105     virtual void HandleTransportDataBusy(ObexServerSession &serverSession, uint8_t isBusy);
106     virtual void HandlePutRequest(ObexServerSession &session, ObexHeader &req) const;
107     virtual void HandleGetRequest(ObexServerSession &session, ObexHeader &req);
108     virtual void HandleSetPathRequest(ObexServerSession &session, ObexHeader &req);
109     virtual void HandleAbortRequest(ObexServerSession &session, ObexHeader &req);
110     virtual void SetBusy(ObexServerSession &session, bool isBusy) const;
111     void RemoveSessionByTransport(ObexTransport &transport);
112 
113     ObexPrivateServer();
114     uint16_t initMtu_ = 0;
115     bool isSupportSrm_ = false;              // Is Support Single Request Mode
116     bool isSupportReliableSession_ = false;  // Is Use Reliable Session
117     ObexServerObserver &observer_;
118     std::unique_ptr<ObexServerTransportObserver> transportObserver_ = nullptr;
119     std::unique_ptr<ObexServerTransport> serverTransport_ = nullptr;
120 
121     std::unordered_map<ObexTransport *, std::unique_ptr<ObexServerSession>> serverSessionsMap_ {};
122     std::list<std::unique_ptr<ObexServerSession>> invalidSessions_ {};
123     utility::Dispatcher &dispatcher_;
124     static const uint16_t MAX_TRASH_SESSION_COUNT;
125     BT_DISALLOW_COPY_AND_ASSIGN(ObexPrivateServer);
126 };
127 class ObexServer {
128 public:
129     explicit ObexServer(const std::string &serviceName, const ObexServerConfig &config, ObexServerObserver &observer,
130         utility::Dispatcher &dispatcher);
131     virtual ~ObexServer() = default;
132     int Startup() const;
133     void Shutdown() const;
134 
135 protected:
136     ObexServer() = default;
137 
138     std::string serviceName_ = "";
139     std::unique_ptr<ObexPrivateServer> rfcommServer_ = nullptr;
140     std::unique_ptr<ObexPrivateServer> l2capServer_ = nullptr;
141 };
142 }  // namespace bluetooth
143 }  // namespace OHOS
144 #endif  // OBEX_SERVER_H
145