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 #ifndef OPP_SERVICE_H 17 #define OPP_SERVICE_H 18 19 #include "base_observer_list.h" 20 #include "context.h" 21 #include "interface_profile.h" 22 #include "interface_profile_opp.h" 23 #include "opp_gap_server.h" 24 #include "opp_message.h" 25 #include "opp_obex_server.h" 26 #include "opp_sdp_server.h" 27 #include "opp_statemachine.h" 28 #include "opp_transfer.h" 29 #include "opp_transfer_information.h" 30 31 namespace OHOS { 32 namespace bluetooth { 33 class OppService : public IProfileOpp, public utility::Context { 34 public: 35 /** 36 * @brief Get the instance of the OppService object. 37 * 38 * @return Returns the instance of the OppService object. 39 */ 40 static OppService *GetService(); 41 /** 42 * @brief Construct a new Opp Service object 43 * 44 */ 45 OppService(); 46 /** 47 * @brief Destroy the Opp Service object 48 * 49 */ 50 virtual ~OppService(); 51 utility::Context *GetContext() override; 52 void Enable(void) override; 53 void Disable(void) override; 54 int Connect(const RawAddress &device) override; 55 std::list<RawAddress> GetConnectDevices() override; 56 int GetConnectState(void) override; 57 int GetMaxConnectNum(void) override; 58 std::vector<RawAddress> GetDevicesByStates(std::vector<int> states) override; 59 int GetDeviceState(const RawAddress &device) override; 60 int Disconnect(const RawAddress &device) override; 61 void RegisterObserver(IOppObserver &oppObserver) override; 62 void DeregisterObserver(IOppObserver &oppObserver) override; 63 int SendFile(const RawAddress &device, const std::vector<std::string> filePaths, 64 const std::vector<std::string> mimeTypes) override; 65 int SetIncomingFileConfirmation(const bool accept) override; 66 IOppTransferInformation GetCurrentTransferInformation() override; 67 int CancelTransfer() override; 68 int CancelTransfer(const std::string &device); 69 OppConfig &GetOppConfig(); 70 71 void ShutDownDone(bool isAllDisconnected); 72 void ProcessEvent(const OppMessage &event); 73 void PostEvent(const OppMessage &event); 74 void RemoveStateMachine(const std::string &device); 75 void ConnectObex(const std::string &device, const ObexClientConfig &config); 76 void DisconnectObex(const std::string &device); 77 void OnReceiveIncomingConnect(ObexServerSession &session, uint32_t connectId); 78 void OnReceiveIncomingFile(IOppTransferInformation info); 79 void OnObexConnected(const std::string &device); 80 void OnObexDisconnected(const std::string &device); 81 void OnTransferStateChange(const std::string &device, int state, int reason); 82 void OnTransferPositionChange(const std::string &device, size_t position); 83 void NotifyReceiveIncomingFile(IOppTransferInformation info); 84 void NotifyTransferStateChanged(IOppTransferInformation info); 85 void NotifyStateChanged(const RawAddress &device, int state); 86 87 private: 88 /** 89 * @brief Service startup. 90 * 91 */ 92 void StartUp(); 93 94 /** 95 * @brief Service shutdown. 96 * 97 */ 98 void ShutDown(); 99 bool IsConnected(const std::string &address) const; 100 void ProcessConnectEvent(const OppMessage &event); 101 void ProcessDefaultEvent(const OppMessage &event) const; 102 void ProcessRemoveStateMachine(const std::string &address); 103 void StartNextTransfer(); 104 void LoadOppConfig(); 105 // service status 106 bool isStarted_ {false}; 107 // service status 108 bool isShuttingDown_ {false}; 109 // the mutex variable 110 std::recursive_mutex mutex_ {}; 111 112 BaseObserverList<IOppObserver> oppObservers_ {}; 113 // the map of the device and sate machine 114 std::map<const std::string, std::unique_ptr<OppStateMachine>> stateMachines_ {}; 115 std::list<std::shared_ptr<OppTransfer>> oppTransferList_ {}; 116 std::unique_ptr<OppSdpServer> oppSdpServer_ {nullptr}; 117 std::unique_ptr<OppGapServer> oppGapServer_ {nullptr}; 118 std::unique_ptr<OppObexServer> oppObexServer_ {nullptr}; 119 OppConfig oppConfig_ {}; 120 // const state map 121 const std::map<const int, const int> stateMap_ = { 122 {OPP_STATE_DISCONNECTED, static_cast<int>(BTConnectState::DISCONNECTED)}, 123 {OPP_STATE_CONNECTING, static_cast<int>(BTConnectState::CONNECTING)}, 124 {OPP_STATE_DISCONNECTING, static_cast<int>(BTConnectState::DISCONNECTING)}, 125 {OPP_STATE_CONNECTED, static_cast<int>(BTConnectState::CONNECTED)} 126 }; 127 }; 128 } // namespace bluetooth 129 } // namespace OHOS 130 #endif // OPP_SERVICE_H 131