• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_MECH_CONNECT_MANAGER_H
17 #define OHOS_MECH_CONNECT_MANAGER_H
18 
19 #include <mutex>
20 #include <set>
21 #include <string>
22 
23 #include "event_handler.h"
24 #include "mechbody_controller_log.h"
25 #include "mechbody_controller_types.h"
26 
27 namespace OHOS {
28 namespace MechBodyController {
29 
30 class IMechConnectListener {
31 public:
32     IMechConnectListener() = default;
33     virtual ~IMechConnectListener() = default;
34 
35     virtual void OnMechConnect(const MechInfo& mechInfo) = 0;
36     virtual void OnMechDisconnect(const MechInfo& mechInfo) = 0;
37 };
38 
39 class MechConnectManager {
40 public:
41     static MechConnectManager& GetInstance();
42 private:
43     MechConnectManager() = default;
44     ~MechConnectManager() = default;
45     MechConnectManager(const MechConnectManager&) = delete;
46     MechConnectManager& operator= (const MechConnectManager&) = delete;
47     MechConnectManager(MechConnectManager&&) = delete;
48     MechConnectManager& operator= (MechConnectManager&&) = delete;
49 
50 public:
51     void Init();
52     void UnInit();
53     int32_t AddDeviceChangeListener(const std::shared_ptr<IMechConnectListener>& listener);
54     int32_t RemoveDeviceChangeListener(const std::shared_ptr<IMechConnectListener>& listener);
55     int32_t NotifyMechConnect(MechInfo& mechInfo);
56     int32_t NotifyMechDisconnect(const MechInfo& mechInfo);
57     bool GetMechBasicInfo(int32_t mechId, MechInfo& mechInfo);
58     bool GetConnectMechList(std::set<MechInfo>& mechInfos);
59     bool NotifyMechState(int32_t mechId, bool isAttached);
60     bool GetMechState(int32_t mechId);
61     bool UpdateBleStatus(bool isBLEActive);
62     bool GetLocalDeviceBleStatus();
63     bool IsConnect();
64 
65     int32_t AddMechInfo(MechInfo &mechInfo);
66     int32_t GetMechInfo(std::string &mac, MechInfo &mechInfo);
67     int32_t SetMechanicGattState(std::string &mac, bool state);
68     int32_t GetMechanicGattState(std::string &mac, bool &state);
69     int32_t SetMechanicPairState(std::string &mac, bool state);
70     int32_t GetMechanicPairState(std::string &mac, bool &state);
71     int32_t SetMechanicHidState(std::string &mac, bool state);
72     int32_t GetMechanicHidState(std::string &mac, bool &state);
73 
74 private:
75     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
76     std::mutex listenerSetMutex_;
77     std::mutex mechInfosMutex_;
78     std::set<std::shared_ptr<IMechConnectListener>> listenerSet_;
79     std::set<MechInfo> mechInfos_;
80     std::atomic<bool> isBLEActive_ = true;
81 };
82 } // namespace MechBodyController
83 } // namespace OHOS
84 #endif // OHOS_MECH_CONNECT_MANAGER_H
85