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 "pasteboard_info_collector.h"
17 #include "cJSON.h"
18 #include "parameter.h"
19 #include "content_sensor_manager_utils.h"
20 #include "distributed_device_profile_log.h"
21 
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 namespace {
25 const std::string TAG = "PasteboardInfoCollector";
26 constexpr const char *SERVICE_ID = "pasteboardService";
27 constexpr const char *CHARACTER_ID = "supportDistributedPasteboard";
28 constexpr const char *VERSION_ID = "PasteboardVersionId";
29 constexpr uint32_t FIRST_VERSION = 4;
30 
31 const char *DISTRIBUTED_PASTEBOARD_ENABLED_KEY = "persist.pasteboard.distributedPasteboardEnabled";
32 const std::string ENABLED_STATUS = "true";
33 const char* UNDEFINED_VALUE = "undefined";
34 constexpr int CONFIG_LEN = 10;
35 constexpr uint32_t NOT_SUPPORT = 0;
36 constexpr uint32_t SUPPORT = 1;
37 }
38 
ConvertToProfile(std::vector<ServiceProfile> & svrProfileList)39 bool PasteboardInfoCollector::ConvertToProfile(std::vector<ServiceProfile>& svrProfileList)
40 {
41     HILOGI("called!");
42     ServiceProfile svrProfile;
43     svrProfile.SetDeviceId(ContentSensorManagerUtils::GetInstance().ObtainLocalUdid());
44     svrProfile.SetServiceName(SERVICE_ID);
45     svrProfile.SetServiceType(SERVICE_ID);
46     svrProfileList.push_back(svrProfile);
47     return true;
48 }
49 
ConvertToProfile(std::vector<CharacteristicProfile> & charProfileList)50 bool PasteboardInfoCollector::ConvertToProfile(std::vector<CharacteristicProfile>& charProfileList)
51 {
52     HILOGI("called!");
53     CharacteristicProfile charProfile;
54     charProfile.SetDeviceId(ContentSensorManagerUtils::GetInstance().ObtainLocalUdid());
55     charProfile.SetServiceName(SERVICE_ID);
56     charProfile.SetCharacteristicKey(CHARACTER_ID);
57 
58     cJSON* jsonData = cJSON_CreateObject();
59     if (!cJSON_IsObject(jsonData)) {
60         HILOGE("Create cJSON failed!");
61         cJSON_Delete(jsonData);
62         return false;
63     }
64     uint32_t supportDistributedPasteboard = GetSupportDistributedPasteboard();
65     cJSON* item = cJSON_AddNumberToObject(jsonData, CHARACTER_ID, supportDistributedPasteboard);
66     if (!cJSON_IsNumber(item)) {
67         HILOGE("Add CHARACTER_ID to cJSON failed! supportDistributedPasteboard=%{public}u",
68             supportDistributedPasteboard);
69         cJSON_Delete(jsonData);
70         return false;
71     }
72     item = cJSON_AddNumberToObject(jsonData, VERSION_ID, FIRST_VERSION);
73     if (!cJSON_IsNumber(item)) {
74         HILOGE("Add VERSION_ID to cJSON failed!");
75         cJSON_Delete(jsonData);
76         return false;
77     }
78     char* jsonChars = cJSON_PrintUnformatted(jsonData);
79     if (jsonChars == NULL) {
80         cJSON_Delete(jsonData);
81         HILOGE("cJSON formatted to string failed!");
82         return false;
83     }
84     std::string jsonStr = jsonChars;
85     charProfile.SetCharacteristicValue(jsonStr);
86     cJSON_Delete(jsonData);
87     cJSON_free(jsonChars);
88     charProfileList.push_back(charProfile);
89     return true;
90 }
91 
GetSupportDistributedPasteboard()92 uint32_t PasteboardInfoCollector::GetSupportDistributedPasteboard()
93 {
94     char value[CONFIG_LEN + 1] = { 0 };
95     GetParameter(DISTRIBUTED_PASTEBOARD_ENABLED_KEY, UNDEFINED_VALUE, value, CONFIG_LEN);
96     HILOGI("GetParameter, value = %{public}s.", value);
97     std::string enabledStatus(value);
98     return enabledStatus == ENABLED_STATUS ? SUPPORT : NOT_SUPPORT;
99 }
100 } // namespace DeviceProfile
101 } // OHOS