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 <thread>
19 #include "cstdint"
20
21 #include "net_conn_client.h"
22 #include "net_conn_constants.h"
23
24 #include "system_ability_definition.h"
25 #include "system_ability_status_change_stub.h"
26 #include "iservice_registry.h"
27
28 #include "devattest_log.h"
29 #include "devattest_network_callback.h"
30
31 namespace OHOS {
32 namespace DevAttest {
33 using namespace NetManagerStandard;
34 constexpr std::int32_t WAIT_FOR_KVSTORE = 1000;
35 constexpr std::int32_t RETRY_REGISTER_NET_CALLBACK_TIME = 5;
36
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)37 void DevAttestSystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
38 {
39 HILOGI("SA:%{public}d added", systemAbilityId);
40
41 std::shared_ptr<NetManagerStandard::NetConnClient> netManager = DelayedSingleton<NetConnClient>::GetInstance();
42 if (netManager == nullptr) {
43 HILOGE("Failed to init NetConnClient.");
44 return;
45 }
46
47 sptr<DevAttestNetworkCallback> callback = (std::make_unique<DevAttestNetworkCallback>()).release();
48 int32_t ret = 0;
49 for (size_t i = 0; i < RETRY_REGISTER_NET_CALLBACK_TIME; i++) {
50 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_KVSTORE));
51 ret = netManager->RegisterNetConnCallback(callback);
52 if (ret == NETMANAGER_SUCCESS) {
53 break;
54 }
55 }
56
57 if (ret == NETMANAGER_SUCCESS) {
58 HILOGE("RegisterNetConnCallback success.");
59 } else {
60 HILOGE("RegisterNetConnCallback failed.");
61 }
62
63 if (RemoveDevAttestSystemAbilityListener(systemAbilityId)) {
64 HILOGE("RemoveSystemAbilityListener success.");
65 }
66 }
67
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)68 void DevAttestSystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
69 {
70 HILOGI("SA:%{public}d removed", systemAbilityId);
71 }
72
AddDevAttestSystemAbilityListener(int32_t systemAbilityId)73 bool DevAttestSystemAbilityListener::AddDevAttestSystemAbilityListener(int32_t systemAbilityId)
74 {
75 HILOGI("AddDevAttestSystemAbilityListener start");
76 if (!CheckInputSysAbilityId(systemAbilityId)) {
77 HILOGI("systemAbilityId invalid %{public}d", systemAbilityId);
78 return false;
79 }
80 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
81 if (samgrProxy == nullptr) {
82 HILOGI("failed to get samgrProxy");
83 return false;
84 }
85 int32_t ret = samgrProxy->SubscribeSystemAbility(systemAbilityId, this);
86 HILOGI("SubscribeSystemAbility ret: %{public}d", ret);
87 if (ret) {
88 HILOGI("failed to subscribe sa: %{public}d", systemAbilityId);
89 return false;
90 }
91 return true;
92 }
93
RemoveDevAttestSystemAbilityListener(int32_t systemAbilityId)94 bool DevAttestSystemAbilityListener::RemoveDevAttestSystemAbilityListener(int32_t systemAbilityId)
95 {
96 if (!CheckInputSysAbilityId(systemAbilityId)) {
97 HILOGI("systemAbilityId invalid %{public}d", systemAbilityId);
98 return false;
99 }
100 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101 if (samgrProxy == nullptr) {
102 HILOGI("failed to get samgrProxy");
103 return false;
104 }
105 int32_t ret = samgrProxy->UnSubscribeSystemAbility(systemAbilityId, this);
106 HILOGI("UnSubscribeSystemAbility ret: %{public}d", ret);
107 if (ret) {
108 HILOGI("failed to unsubscribe sa: %{public}d", systemAbilityId);
109 return false;
110 }
111 return true;
112 }
113
CheckInputSysAbilityId(int32_t systemAbilityId)114 bool DevAttestSystemAbilityListener::CheckInputSysAbilityId(int32_t systemAbilityId)
115 {
116 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
117 }
118 } // DevAttest
119 } // OHOS