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 "ability_tree.h"
17 
18 #include "ability_manager_client.h"
19 #include "wukong_define.h"
20 
21 namespace OHOS {
22 namespace WuKong {
23 namespace {
24 const uint8_t ABILITY_BUNDLE_NAME_SIZE_POSION = 56;
25 const uint8_t ABILITY_BUNDLE_NAME_POSION = 32;
26 const uint8_t ABILITY_ABILITY_NAME_SIZE_POSION = 24;
27 const uint8_t ABILITY_ABILITY_NAME_POSION = 0;
28 }  // namespace
29 
30 /**
31  * @brief Ability Node Id specification format
32  * @details
33  * |----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
34  * |BUN SIZ  |BUN STR                      |ABI SIZ  |ABI STR                      |
35  */
SetNodeId()36 bool AbilityTree::SetNodeId()
37 {
38     auto amcPtr = AAFwk::AbilityManagerClient::GetInstance();
39     if (amcPtr == nullptr) {
40         ERROR_LOG("AbilityManagerClient is nullptr");
41         return false;
42     }
43     auto elementName = amcPtr->GetTopAbility();
44     if (elementName.GetBundleName().empty()) {
45         ERROR_LOG("GetTopAbility GetBundleName is nullptr");
46         return false;
47     }
48     const uint8_t nameCount = 3;
49     // make bundle name value
50     bundleName_ = elementName.GetBundleName();
51     uint64_t nameSize = bundleName_.size();
52     nodeId_ |= nameSize << ABILITY_BUNDLE_NAME_SIZE_POSION;
53     nodeId_ |= GetSubName(bundleName_, nameCount) << ABILITY_BUNDLE_NAME_POSION;
54 
55     // make ability name value
56     abilityName_ = elementName.GetAbilityName();
57     nameSize = abilityName_.size();
58     nodeId_ |= nameSize << ABILITY_ABILITY_NAME_SIZE_POSION;
59     nodeId_ |= GetSubName(abilityName_, nameCount) << ABILITY_ABILITY_NAME_POSION;
60 
61     TRACK_LOG_STR("Ability Node ID: (0x%016llX)", nodeId_);
62     return true;
63 }
64 }  // namespace WuKong
65 }  // namespace OHOS
66