1 /*
2  * Copyright (c) 2024 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 OHOS_DISTRIBUTED_HARDWARE_COMM_TOOL_H
17 #define OHOS_DISTRIBUTED_HARDWARE_COMM_TOOL_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22 #include <memory>
23 
24 #include "dh_transport.h"
25 #include "dh_transport_obj.h"
26 #include "event_handler.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 // request remote dh send back full dh capabilities
31 constexpr int32_t DH_COMM_REQ_FULL_CAPS = 1;
32 // send back full dh attributes to the requester
33 constexpr int32_t DH_COMM_RSP_FULL_CAPS = 2;
34 
35 class DHCommTool : public std::enable_shared_from_this<DHCommTool> {
36 public:
37     DHCommTool();
38     virtual ~DHCommTool() = default;
39     static std::shared_ptr<DHCommTool> GetInstance();
40     void Init();
41     void UnInit();
42     /**
43      * @brief trigger request remote dh send back full capatilities.
44      *        payload: {code: DH_COMM_REQ_FULL_CAPS, msg: localNetworkId}.
45      *        msg means the device need the dh capatilities, the remote side should use
46      *        localNetworkId to send dh capatilities msg back.
47      *
48      * @param remoteNetworkId the target device network id
49      */
50     void TriggerReqFullDHCaps(const std::string &remoteNetworkId);
51     void GetAndSendLocalFullCaps(const std::string &reqNetworkId);
52     FullCapsRsp ParseAndSaveRemoteDHCaps(const std::string &remoteCaps);
53 
54     class DHCommToolEventHandler : public AppExecFwk::EventHandler {
55     public:
56         DHCommToolEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,
57             std::shared_ptr<DHCommTool> dhCommToolPtr);
58         ~DHCommToolEventHandler() override = default;
59         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
60     private:
61         void ProcessFullCapsRsp(const FullCapsRsp &capsRsp, const std::shared_ptr<DHCommTool> dhCommToolPtr);
62         std::weak_ptr<DHCommTool> dhCommToolWPtr_;
63     };
64     std::shared_ptr<DHCommTool::DHCommToolEventHandler> GetEventHandler();
65     const std::shared_ptr<DHTransport> GetDHTransportPtr();
66 
67 private:
68     std::shared_ptr<DHTransport> dhTransportPtr_;
69     std::shared_ptr<DHCommTool::DHCommToolEventHandler> eventHandler_;
70 };
71 } // DistributedHardware
72 } // OHOS
73 #endif