1 /*
2 * Copyright (c) 2021 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 "dm_ability_manager.h"
17
18 #include "auth_manager.h"
19 #include "ability_manager_client.h"
20 #include "ability_record.h"
21 #include "ability_manager_service.h"
22 #include "parameter.h"
23 #include "semaphore.h"
24
25 #include "constants.h"
26 #include "device_manager_log.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 namespace {
31 const int32_t ABILITY_START_TIMEOUT = 3; // 3 second
32 }
33 IMPLEMENT_SINGLE_INSTANCE(DmAbilityManager);
34
GetAbilityRole()35 AbilityRole DmAbilityManager::GetAbilityRole()
36 {
37 return mAbilityStatus_;
38 }
39
StartAbility(AbilityRole role)40 AbilityStatus DmAbilityManager::StartAbility(AbilityRole role)
41 {
42 std::string roleStr;
43 if (role == AbilityRole::ABILITY_ROLE_INITIATIVE) {
44 roleStr = "initiative";
45 } else if (role == AbilityRole::ABILITY_ROLE_PASSIVE) {
46 roleStr = "passive";
47 } else {
48 DMLOG(DM_LOG_ERROR, "StartAbility, failed, role unknown");
49 return AbilityStatus::ABILITY_STATUS_FAILED;
50 }
51
52 DMLOG(DM_LOG_ERROR, "StartAbility, role %s", roleStr.c_str());
53 mAbilityStatus_ = role;
54
55 char localDeviceId[DEVICE_UUID_LENGTH] = {0};
56 GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
57 std::string deviceId = localDeviceId;
58 std::string bundleName = "com.ohos.devicemanagerui";
59 std::string abilityName = "com.ohos.devicemanagerui.MainAbility";
60 int32_t displayOwner = (role == AbilityRole::ABILITY_ROLE_INITIATIVE) ?
61 AuthManager::GetInstance().GetDisplayOwner() : DISPLAY_OWNER_SYSTEM;
62
63 mStatus_ = AbilityStatus::ABILITY_STATUS_START;
64 AAFwk::Want want;
65 AppExecFwk::ElementName element(deviceId, bundleName, abilityName);
66 want.SetElement(element);
67 if (displayOwner == DISPLAY_OWNER_OTHER) {
68 return AbilityStatus::ABILITY_STATUS_SUCCESS;
69 }
70 AAFwk::AbilityManagerClient::GetInstance()->Connect();
71 ErrCode result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
72 if (result == OHOS::ERR_OK) {
73 DMLOG(DM_LOG_INFO, "Start Ability succeed");
74 } else {
75 DMLOG(DM_LOG_INFO, "Start Ability faild");
76 mStatus_ = AbilityStatus::ABILITY_STATUS_FAILED;
77 return mStatus_;
78 }
79 waitForTimeout(ABILITY_START_TIMEOUT);
80 return mStatus_;
81 }
82
waitForTimeout(uint32_t timeout_s)83 void DmAbilityManager::waitForTimeout(uint32_t timeout_s)
84 {
85 struct timespec ts;
86 clock_gettime(CLOCK_REALTIME, &ts);
87 ts.tv_sec += timeout_s;
88 sem_timedwait(&mSem_, &ts);
89 }
90
StartAbilityDone()91 void DmAbilityManager::StartAbilityDone()
92 {
93 mStatus_ = AbilityStatus::ABILITY_STATUS_SUCCESS;
94 sem_post(&mSem_);
95 }
96 }
97 }
98