• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CLIENT_H
17 #define OPP_OBEX_CLIENT_H
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <fstream>
22 #include <memory>
23 #include "../obex/obex_body.h"
24 #include "../obex/obex_client.h"
25 #include "../obex/obex_headers.h"
26 #include "context.h"
27 #include "opp_defines.h"
28 #include "opp_transfer_information.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 class OppSendFileBodyObject : public ObexBodyObject {
33 public:
34     explicit OppSendFileBodyObject(const std::string &file);
35     OppSendFileBodyObject() = default;
36     ~OppSendFileBodyObject() 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     size_t GetFileSize() const;
41     size_t GetFileSendSize() const;
42 
43 private:
44     void OpenFile(const std::string &file);
45     std::ifstream ifs_ {};
46     size_t fileSize_ = 0;
47     size_t fileSendSize_ = 0;
48 };
49 
50 class OppObexClient {
51 public:
52     explicit OppObexClient(const ObexClientConfig &config, utility::Dispatcher &dispatcher);
53     virtual ~OppObexClient();
54     int Connect(uint32_t fileCount) const;
55     int Disconnect(bool withObexReq = true) const;
56     int CancelSendFile();
57     int SendFile(IOppTransferInformation fileInfo);
58 
59     void OnTransportFailed(const ObexClient &client, int errCd);
60     void OnConnected(const ObexClient &client, const ObexHeader &resp);
61     void OnConnectFailed(const ObexClient &client);
62     void OnDisconnected(const ObexClient &client);
63     void OnActionCompleted(const ObexClient &client, const ObexHeader &resp);
64     void OnBusy(ObexClient &client, bool isBusy);
65 
66     class OppObexObserver : public ObexClientObserver {
67     public:
OppObexObserver(OppObexClient * oppObexClient)68         explicit OppObexObserver(OppObexClient *oppObexClient) : oppObexClient_(oppObexClient) {}
~OppObexObserver()69         ~OppObexObserver() override {}
70         void OnTransportFailed(ObexClient &client, int errCd) override;
71         void OnConnected(ObexClient &client, const ObexHeader &resp) override;
72         void OnConnectFailed(ObexClient &client, const ObexHeader &resp) override;
73         void OnDisconnected(ObexClient &client) override;
74         void OnActionCompleted(ObexClient &client, const ObexHeader &resp) override;
75         void OnBusy(ObexClient &client, bool isBusy) override;
76 
77         OppObexClient *oppObexClient_ {nullptr};
78     };
79 
80 private:
81     std::unique_ptr<OppObexObserver> observer_ {nullptr};  // obex observer
82     std::unique_ptr<ObexClient> client_ {nullptr};         // obex client
83     std::shared_ptr<ObexBodyObject> fileObject_ {nullptr};
84     uint32_t connectionId_ = 0;
85     bool isBusy_ = false;
86     bool startSendFile_ = false;
87     bool sendAbort_ = false;
88     int status_ = OPP_OBEX_STATUS_IDLE;
89     std::mutex mutexBusyChanged_;
90     std::condition_variable cvWaitBusyChanged_;
91     std::unique_ptr<utility::Dispatcher> SendFileThread_ {};
92     std::string address_;
93     bool isRegisterL2capLPsm_ = false;
94     uint16_t lpsm_ = 0;
95     bool isSupportSrm_ = false;
96 
97     void SendFileBody();
98     void SendOppDisconnected();
99     void SendOppConnected();
100     void OnTransferStateChangeFaild(int reason);
101     std::u16string StringToU16string(const std::string &str) const;
102 };
103 }  // namespace bluetooth
104 }  // namespace OHOS
105 #endif  // OPP_OBEX_CLIENT_H
106