1 /* 2 * Copyright (c) 2022-2023 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 DEVICESTATUS_MANAGER_H 17 #define DEVICESTATUS_MANAGER_H 18 19 #include <set> 20 #include <map> 21 22 #include "sensor_if.h" 23 #include "devicestatus_data_utils.h" 24 #include "death_recipient.h" 25 #include "idevicestatus_algorithm.h" 26 #include "idevicestatus_callback.h" 27 #include "devicestatus_common.h" 28 #include "devicestatus_msdp_client_impl.h" 29 #include "accesstoken_kit.h" 30 31 namespace OHOS { 32 namespace Msdp { 33 using namespace Security::AccessToken; 34 class DevicestatusService; 35 class DevicestatusManager { 36 public: DevicestatusManager(const wptr<DevicestatusService> & ms)37 explicit DevicestatusManager(const wptr<DevicestatusService>& ms) : ms_(ms) 38 { 39 DEV_HILOGI(SERVICE, "DevicestatusManager instance is created."); 40 } 41 ~DevicestatusManager() = default; 42 43 class DevicestatusCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 44 public: 45 DevicestatusCallbackDeathRecipient() = default; 46 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 47 virtual ~DevicestatusCallbackDeathRecipient() = default; 48 }; 49 50 bool Init(); 51 bool EnableMock(DevicestatusDataUtils::DevicestatusType type); 52 bool InitInterface(DevicestatusDataUtils::DevicestatusType type); 53 bool DisableMock(DevicestatusDataUtils::DevicestatusType type); 54 bool InitDataCallback(); 55 void NotifyDevicestatusChange(const DevicestatusDataUtils::DevicestatusData& devicestatusData); 56 void Subscribe(const DevicestatusDataUtils::DevicestatusType& type, const sptr<IdevicestatusCallback>& callback); 57 void UnSubscribe(const DevicestatusDataUtils::DevicestatusType& type, const sptr<IdevicestatusCallback>& callback); 58 DevicestatusDataUtils::DevicestatusData GetLatestDevicestatusData(const \ 59 DevicestatusDataUtils::DevicestatusType& type); 60 int32_t MsdpDataCallback(const DevicestatusDataUtils::DevicestatusData& data); 61 int32_t LoadAlgorithm(); 62 int32_t UnloadAlgorithm(); 63 void GetPackageName(AccessTokenID tokenId, std::string &packageName); 64 void ProcessDeathObserver(wptr<IRemoteObject> object); 65 private: 66 struct classcomp { operatorclasscomp67 bool operator()(const sptr<IdevicestatusCallback> &l, const sptr<IdevicestatusCallback> &r) const 68 { 69 return l->AsObject() < r->AsObject(); 70 } 71 }; 72 const wptr<DevicestatusService> ms_; 73 std::mutex mutex_; 74 sptr<IRemoteObject::DeathRecipient> devicestatusCBDeathRecipient_; 75 std::unique_ptr<DevicestatusMsdpClientImpl> msdpImpl_; 76 std::map<DevicestatusDataUtils::DevicestatusType, DevicestatusDataUtils::DevicestatusValue> msdpData_; 77 sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr; 78 std::map<DevicestatusDataUtils::DevicestatusType, std::set<const sptr<IdevicestatusCallback>, classcomp>> \ 79 listenerMap_; 80 }; 81 } // namespace Msdp 82 } // namespace OHOS 83 #endif // DEVICESTATUS_MANAGER_H 84