1 /*
2  * Copyright (c) 2024 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 "content_sensor_manager_utils.h"
17 #include "distributed_device_profile_errors.h"
18 #include "profile_cache.h"
19 #include "static_capability_collector.h"
20 #include "static_capability_loader.h"
21 #include "static_profile_manager.h"
22 
23 namespace OHOS {
24 namespace DistributedDeviceProfile {
25 IMPLEMENT_SINGLE_INSTANCE(StaticCapabilityCollector);
26 namespace {
27     const std::string TAG = "StaticCapabilityCollector";
28 }
Init()29 int32_t StaticCapabilityCollector::Init()
30 {
31     HILOGI("call!");
32     StaticCapabilityLoader::GetInstance().Init();
33     CollectStaticCapability();
34     return DP_SUCCESS;
35 }
36 
UnInit()37 int32_t StaticCapabilityCollector::UnInit()
38 {
39     StaticCapabilityLoader::GetInstance().UnInit();
40     HILOGI("call!");
41     return DP_SUCCESS;
42 }
43 
CollectStaticCapability()44 int32_t StaticCapabilityCollector::CollectStaticCapability()
45 {
46     HILOGI("call!");
47     std::string staticCapability = "";
48     if (StaticCapabilityLoader::GetInstance().LoadStaticCapability(staticCapability) != DP_SUCCESS ||
49         staticCapability.empty()) {
50         HILOGE("staticCapability is invalid!");
51         return DP_STATIC_COLLECT_FAIL;
52     }
53     std::string staticVersion = "";
54     std::unordered_map<std::string, CharacteristicProfile> staticProfileMap;
55     StaticCapabilityLoader::GetInstance().LoadStaticInfo(staticCapability, staticVersion, staticProfileMap);
56     if (staticVersion.empty() || staticProfileMap.empty()) {
57         HILOGE("staticVersion or staticProfileMap is invalid!");
58         return DP_STATIC_COLLECT_FAIL;
59     }
60     AddStaticInfoToCache(staticProfileMap);
61     AddStaticCapabilityToDB(staticVersion, staticCapability);
62     return DP_SUCCESS;
63 }
64 
AddStaticInfoToCache(const std::unordered_map<std::string,CharacteristicProfile> & staticProfileMap)65 int32_t StaticCapabilityCollector::AddStaticInfoToCache(
66     const std::unordered_map<std::string, CharacteristicProfile>& staticProfileMap)
67 {
68     HILOGI("call!");
69     for (const auto& item : staticProfileMap) {
70         HILOGI("AddStaticInfoToCache key: %{public}s, value: %{public}s!",
71             ProfileUtils::GetDbKeyAnonyString(item.first).c_str(), item.second.dump().c_str());
72         ProfileCache::GetInstance().AddStaticCharProfile(item.second);
73     }
74     return DP_SUCCESS;
75 }
76 
AddStaticCapabilityToDB(const std::string & staticVersion,const std::string & staticCapability)77 int32_t StaticCapabilityCollector::AddStaticCapabilityToDB(const std::string& staticVersion,
78     const std::string& staticCapability)
79 {
80     HILOGI("call!");
81     if (staticVersion.empty() || staticVersion.size() >= MAX_STRING_LEN) {
82         HILOGE("StaticVersion is invalid!");
83         return DP_INVALID_PARAM;
84     }
85     if (staticCapability.empty() || staticCapability.size() >= MAX_STRING_LEN) {
86         HILOGE("StaticCapability is invalid!");
87         return DP_INVALID_PARAM;
88     }
89     cJSON* characteristicValueJson = cJSON_CreateObject();
90     if (!cJSON_IsObject(characteristicValueJson)) {
91         HILOGE("Create cJSON failed!");
92         cJSON_Delete(characteristicValueJson);
93         return DP_STATIC_COLLECT_FAIL;
94     }
95     cJSON_AddStringToObject(characteristicValueJson, STATIC_CAPABILITY_VERSION.c_str(), staticVersion.c_str());
96     cJSON_AddStringToObject(characteristicValueJson, STATIC_CAPABILITY_VALUE.c_str(), staticCapability.c_str());
97     char* characteristicValueStr = cJSON_Print(characteristicValueJson);
98     if (characteristicValueStr == NULL) {
99         HILOGE("characteristicValueStr is null!");
100         cJSON_Delete(characteristicValueJson);
101         return DP_STATIC_COLLECT_FAIL;
102     }
103     std::string characteristicValue = characteristicValueStr;
104     std::string localDeviceId = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid();
105     std::string serviceId = STATIC_CAPABILITY_SVR_ID;
106     std::string characteristicId = STATIC_CAPABILITY_CHAR_ID;
107     CharacteristicProfile staticCapabilityProfile(localDeviceId, serviceId, characteristicId, characteristicValue);
108     StaticProfileManager::GetInstance().PutCharacteristicProfile(staticCapabilityProfile);
109     cJSON_free(characteristicValueStr);
110     cJSON_Delete(characteristicValueJson);
111     return DP_SUCCESS;
112 }
113 } // namespace DistributedDeviceProfile
114 } // namespace OHOS