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_PUBLISH_COMMON_EVENT_H 17 #define OHOS_PUBLISH_COMMON_EVENT_H 18 19 #include <functional> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 #include "common_event_data.h" 25 #include "common_event_manager.h" 26 #include "common_event_subscribe_info.h" 27 #include "common_event_subscriber.h" 28 #include "matching_skills.h" 29 #include "system_ability_status_change_stub.h" 30 31 #include "dm_constants.h" 32 33 namespace OHOS { 34 namespace DistributedHardware { 35 using OHOS::EventFwk::CommonEventData; 36 using OHOS::EventFwk::CommonEventSubscriber; 37 using OHOS::EventFwk::CommonEventSubscribeInfo; 38 using PublishEventCallback = std::function<void(int32_t, int32_t, int32_t)>; 39 40 class DmPublishEventSubscriber : public CommonEventSubscriber { 41 public: DmPublishEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo,const PublishEventCallback & callback,const std::vector<std::string> & eventNameVec)42 DmPublishEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const PublishEventCallback &callback, 43 const std::vector<std::string> &eventNameVec) : CommonEventSubscriber(subscribeInfo), 44 eventNameVec_(eventNameVec), callback_(callback) {} 45 ~DmPublishEventSubscriber() override = default; 46 std::vector<std::string> GetSubscriberEventNameVec() const; 47 void OnReceiveEvent(const CommonEventData &data) override; 48 void SetWifiState(const int32_t wifiState); 49 int32_t GetWifiState(); 50 void SetBluetoothState(const int32_t bluetoothState); 51 int32_t GetBluetoothState(); 52 void SetScreenState(const int32_t screenState); 53 int32_t GetScreenState(); 54 55 private: 56 void SetScreenEventState(const std::string &receiveEvent); 57 std::vector<std::string> eventNameVec_; 58 int32_t wifiState_ { -1 }; 59 int32_t bluetoothState_ { -1 }; 60 int32_t screenState_ = DM_SCREEN_UNKNOWN; 61 PublishEventCallback callback_; 62 std::mutex wifiStateMutex_; 63 std::mutex bluetoothStateMutex_; 64 std::mutex screenStateMutex_; 65 }; 66 67 class DmPublishCommonEventManager { 68 public: 69 DmPublishCommonEventManager() = default; 70 ~DmPublishCommonEventManager(); 71 bool SubscribePublishCommonEvent(const std::vector<std::string> &eventNameVec, 72 const PublishEventCallback &callback); 73 bool UnsubscribePublishCommonEvent(); 74 void SetSubscriber(std::shared_ptr<DmPublishEventSubscriber> subscriber); 75 std::shared_ptr<DmPublishEventSubscriber> GetSubscriber(); 76 77 private: 78 std::vector<std::string> eventNameVec_; 79 bool eventValidFlag_ = false; 80 std::mutex evenSubscriberMutex_; 81 std::mutex subscriberMutex_; 82 std::shared_ptr<DmPublishEventSubscriber> subscriber_ = nullptr; 83 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 84 int32_t counter_ = 0; 85 86 private: 87 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 88 public: SystemAbilityStatusChangeListener(std::shared_ptr<DmPublishEventSubscriber> publishSubscriber)89 explicit SystemAbilityStatusChangeListener(std::shared_ptr<DmPublishEventSubscriber> publishSubscriber) 90 : changeSubscriber_(publishSubscriber) {} 91 ~SystemAbilityStatusChangeListener() = default; 92 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 93 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 94 95 private: 96 std::shared_ptr<DmPublishEventSubscriber> changeSubscriber_; 97 }; 98 }; 99 } // namespace DistributedHardware 100 } // namespace OHOS 101 #endif // OHOS_PUBLISH_COMMON_EVENT_H 102