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_OBEX_SERVER_H 17 #define OPP_OBEX_SERVER_H 18 19 #include <cstring> 20 #include <fstream> 21 #include <memory> 22 #include "../obex/obex_headers.h" 23 #include "../obex/obex_server.h" 24 #include "../obex/obex_session.h" 25 #include "../obex/obex_transport.h" 26 #include "context.h" 27 #include "base_def.h" 28 #include "raw_address.h" 29 30 namespace OHOS { 31 namespace bluetooth { 32 class OppReceiveFileBodyObject : public ObexBodyObject { 33 public: 34 explicit OppReceiveFileBodyObject(const std::string &file, const std::string address); 35 OppReceiveFileBodyObject() = default; 36 ~OppReceiveFileBodyObject() override; 37 size_t Read(uint8_t *buf, size_t bufLen) override; 38 size_t Write(const uint8_t *buf, size_t bufLen) override; 39 int Close() override; 40 void SetFileReceiveSuccess(); 41 42 private: 43 void OpenFile(const std::string &file); 44 std::ofstream ofs_ {}; 45 size_t fileReceiveSize_ = 0; 46 std::string address_; 47 std::string file_; 48 bool fileReceiveSuccess_ = false; 49 }; 50 51 class OppObexServer { 52 public: 53 OppObexServer(const ObexServerConfig &config, utility::Dispatcher &dispatcher); 54 virtual ~OppObexServer() = default; 55 int StartUp() const; 56 int ShutDown() const; 57 58 private: 59 class OppObexObserver : public ObexServerObserver { 60 public: 61 void OnTransportConnect(ObexIncomingConnect &incomingConnect) override; 62 void OnConnect(ObexServerSession &session, const ObexHeader &req) override; 63 void OnDisconnect(ObexServerSession &session, const ObexHeader &req) override; 64 void OnPut(ObexServerSession &session, const ObexHeader &req) override; 65 void OnTransportDisconnected(const std::string &btAddr) override; 66 void OnTransportError(const std::string &btAddr, int errCd, const std::string &msg) override; 67 void OnBusy(ObexServerSession &session, bool isBusy) const override; 68 void OnAbort(ObexServerSession &session, const ObexHeader &req) override; 69 70 private: 71 void SendOppDisconnected(const std::string &btAddr) const; 72 int ReceiveFileHeader(ObexServerSession &session, const ObexHeader &req); 73 void ReceiveFileBody(ObexServerSession &session, const ObexHeader &req, bool isHead) const; 74 std::string U16stringToString(const std::u16string &u16str) const; 75 std::string RenameFile(std::string fileName) const; 76 bool HasSameName(std::string path, std::string name) const; 77 bool NeedRejectFileForPts(std::string fileName) const; 78 79 uint32_t connectionId_ = 1; 80 }; 81 std::unique_ptr<ObexServer> obexServer_ = nullptr; 82 std::unique_ptr<OppObexObserver> oppObexObserver_ = nullptr; 83 84 BT_DISALLOW_COPY_AND_ASSIGN(OppObexServer); 85 }; 86 } // namespace bluetooth 87 } // namespace OHOS 88 #endif // OPP_OBEX_SERVER_H 89