1 /*
2 * Copyright (C) 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 #include "devattest_system_ability_listener.h"
17
18 #include "iservice_registry.h"
19 #include "devattest_log.h"
20 #include "devattest_errno.h"
21 #include "devattest_network_manager.h"
22
23 namespace OHOS {
24 namespace DevAttest {
25 constexpr std::int32_t COMM_NET_CONN_MANAGER_SA_ID = 1151;
26 constexpr std::int32_t FIRST_SA_ID = 0x00000001;
27 constexpr std::int32_t LAST_SA_ID = 0x00ffffff;
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)28 void DevAttestSystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
29 {
30 if (systemAbilityId == COMM_NET_CONN_MANAGER_SA_ID) {
31 (void)DelayedSingleton<DevAttestNetworkManager>::GetInstance()->RegisterNetConnCallback();
32 } else {
33 HILOGW("[OnAddSystemAbility] Do Nothing");
34 }
35
36 if (RemoveDevAttestSystemAbilityListener(systemAbilityId)) {
37 HILOGI("[OnAddSystemAbility] RemoveSystemAbilityListener success.");
38 }
39 return;
40 }
41
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)42 void DevAttestSystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
43 {
44 HILOGD("[OnRemoveSystemAbility] SA:%{public}d removed", systemAbilityId);
45 }
46
AddDevAttestSystemAbilityListener(int32_t systemAbilityId)47 bool DevAttestSystemAbilityListener::AddDevAttestSystemAbilityListener(int32_t systemAbilityId)
48 {
49 HILOGD("[AddDevAttestSystemAbilityListener] start");
50 if (!CheckInputSysAbilityId(systemAbilityId)) {
51 HILOGE("[AddDevAttestSystemAbilityListener] systemAbilityId invalid %{public}d", systemAbilityId);
52 return false;
53 }
54 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55 if (samgrProxy == nullptr) {
56 HILOGE("[AddDevAttestSystemAbilityListener] failed to get samgrProxy");
57 return false;
58 }
59 int32_t ret = samgrProxy->SubscribeSystemAbility(systemAbilityId, this);
60 if (ret != DEVATTEST_SUCCESS) {
61 HILOGE("[AddDevAttestSystemAbilityListener] failed to subscribe sa: %{public}d", systemAbilityId);
62 return false;
63 }
64 return true;
65 }
66
RemoveDevAttestSystemAbilityListener(int32_t systemAbilityId)67 bool DevAttestSystemAbilityListener::RemoveDevAttestSystemAbilityListener(int32_t systemAbilityId)
68 {
69 if (!CheckInputSysAbilityId(systemAbilityId)) {
70 HILOGE("[RemoveDevAttestSystemAbilityListener] systemAbilityId invalid %{public}d", systemAbilityId);
71 return false;
72 }
73 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
74 if (samgrProxy == nullptr) {
75 HILOGE("[RemoveDevAttestSystemAbilityListener] failed to get samgrProxy");
76 return false;
77 }
78 int32_t ret = samgrProxy->UnSubscribeSystemAbility(systemAbilityId, this);
79 if (ret != DEVATTEST_SUCCESS) {
80 HILOGE("[RemoveDevAttestSystemAbilityListener] failed to unsubscribe sa: %{public}d", systemAbilityId);
81 return false;
82 }
83 return true;
84 }
85
CheckInputSysAbilityId(int32_t systemAbilityId)86 bool DevAttestSystemAbilityListener::CheckInputSysAbilityId(int32_t systemAbilityId)
87 {
88 return (systemAbilityId >= FIRST_SA_ID) && (systemAbilityId <= LAST_SA_ID);
89 }
90 } // DevAttest
91 } // OHOS