1 /* 2 * Copyright (c) 2021-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 HKS_SA_H 17 #define HKS_SA_H 18 19 #include "huks_service_ipc_interface_code.h" 20 #include "hks_sa_interface.h" 21 22 #include <atomic> 23 #include "iremote_broker.h" 24 #include "iremote_stub.h" 25 #include "nocopyable.h" 26 #include "system_ability.h" 27 28 namespace OHOS { 29 namespace Security { 30 namespace Hks { 31 enum ServiceRunningState { 32 STATE_NOT_START, 33 STATE_RUNNING 34 }; 35 enum ResponseCode { 36 HW_NO_ERROR = 0, 37 HW_SYSTEM_ERROR = -1, 38 HW_PERMISSION_DENIED = -2, 39 }; 40 41 constexpr int SA_ID_KEYSTORE_SERVICE = 3510; 42 43 class HksService : public SystemAbility, public HksStub { 44 DECLEAR_SYSTEM_ABILITY(HksService) 45 46 public: 47 DISALLOW_COPY_AND_MOVE(HksService); 48 HksService(int saId, bool runOnCreate); 49 virtual ~HksService(); 50 51 int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 52 53 static sptr<HksService> GetInstance(); 54 55 protected: 56 void OnStart() override; 57 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 58 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 59 void OnStop() override; 60 61 private: 62 HksService(); 63 bool Init(); 64 65 bool registerToService_; 66 volatile std::atomic_int runningState_; 67 std::mutex runningStateLock; 68 static std::mutex instanceLock; 69 static sptr<HksService> instance; 70 int OnRemotePluginRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); 71 }; 72 73 class HksDeathRecipient : public IRemoteObject::DeathRecipient { 74 public: HksDeathRecipient()75 HksDeathRecipient() {} 76 explicit HksDeathRecipient(int32_t callingPid); 77 ~HksDeathRecipient() override = default; 78 void OnRemoteDied(const wptr<IRemoteObject>& remoteObject) override; 79 private: 80 int32_t callingPid_; 81 }; 82 83 } // namespace Hks 84 } // namespace Security 85 } // namespace OHOS 86 87 #endif // HKS_SA_H 88