• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "syscap_info_collector.h"
17 
18 #include "parameters.h"
19 #include "syscap_interface.h"
20 
21 #include "distributed_device_profile_errors.h"
22 #include "distributed_device_profile_log.h"
23 
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 namespace {
27     const std::string TAG = "SyscapInfoCollector";
28     const std::string SERVICE_ID = "syscap";
29     const std::string SERVICE_TYPE = "syscap";
30     const std::string CHARACTER_PRIVATE_SYSCAP = "privatesyscap";
31     const std::string CHARACTER_OS_SYSCAP = "ossyscap";
32     constexpr int32_t MAX_DATALEN = 1128;
33     constexpr int32_t INT_BYTES_LEN = 4;
34 }
35 
ConvertToProfile(DeviceProfile & profile)36 bool SyscapInfoCollector::ConvertToProfile(DeviceProfile &profile)
37 {
38     char osBuffer[PCID_MAIN_BYTES];
39     if (!EncodeOsSyscap(osBuffer, PCID_MAIN_BYTES)) {
40         HILOGE("EncodeOsSyscap failed");
41         return false;
42     }
43 
44     std::vector<int32_t> osSyscapData;
45     for (int32_t i = 0; i < PCID_MAIN_BYTES/INT_BYTES_LEN; i++) {
46         int32_t value = *((int32_t *)osBuffer + i);
47         osSyscapData.push_back(value);
48     }
49     cJSON* jsonData = cJSON_CreateObject();
50     if (!cJSON_IsObject(jsonData)) {
51         HILOGE("Create cJSON failed!");
52         cJSON_Delete(jsonData);
53         return false;
54     }
55     if (!AddOsSyscapToJson(jsonData, osSyscapData)) {
56         cJSON_Delete(jsonData);
57         return false;
58     }
59 
60     char* privateBuffer = nullptr;
61     int32_t privateBufferLen;
62     if (!EncodePrivateSyscap(&privateBuffer, &privateBufferLen)) {
63         cJSON_Delete(jsonData);
64         HILOGE("EncodePrivateSyscap failed");
65         return false;
66     }
67     if (privateBufferLen + PCID_MAIN_BYTES > MAX_DATALEN) {
68         free(privateBuffer);
69         cJSON_Delete(jsonData);
70         HILOGI("syscap data length too long");
71         return false;
72     }
73     if (!AddPrivateSyscapToJson(jsonData, privateBuffer)) {
74         free(privateBuffer);
75         cJSON_Delete(jsonData);
76         return false;
77     }
78     free(privateBuffer);
79     std::string jsonStr;
80     if (!GenJsonStr(jsonData, jsonStr)) {
81         cJSON_Delete(jsonData);
82         return false;
83     }
84     cJSON_Delete(jsonData);
85     profile.SetOsSysCap(jsonStr);
86     return true;
87 }
88 
AddOsSyscapToJson(cJSON * const jsonData,const std::vector<int32_t> & osSyscapData)89 bool SyscapInfoCollector::AddOsSyscapToJson(cJSON* const jsonData, const std::vector<int32_t>& osSyscapData)
90 {
91     cJSON* osSyscapJsonData = cJSON_CreateArray();
92     if (!cJSON_IsArray(osSyscapJsonData)) {
93         cJSON_Delete(osSyscapJsonData);
94         HILOGE("Create JSON_ARRAY failed!");
95         return false;
96     }
97     for (const auto& value : osSyscapData) {
98         cJSON* jsonArrItem = cJSON_CreateNumber(value);
99         if (jsonArrItem == NULL) {
100             continue;
101         }
102         if (!cJSON_AddItemToArray(osSyscapJsonData, jsonArrItem)) {
103             cJSON_Delete(jsonArrItem);
104             continue;
105         }
106     }
107     int32_t size = static_cast<int32_t>(osSyscapData.size());
108     int32_t jsonArraySize = static_cast<int32_t>(cJSON_GetArraySize(osSyscapJsonData));
109     if (jsonArraySize != size) {
110         cJSON_Delete(osSyscapJsonData);
111         HILOGE("size not equal!size=%{public}d, jsonArraySize=%{public}d", size, jsonArraySize);
112         return false;
113     }
114     if (!cJSON_AddItemToObject(jsonData, CHARACTER_OS_SYSCAP.c_str(), osSyscapJsonData)) {
115         cJSON_Delete(osSyscapJsonData);
116         HILOGE("Add json array to Object failed!");
117         return false;
118     }
119     return true;
120 }
121 
AddPrivateSyscapToJson(cJSON * const jsonData,const char * const privateBuffer)122 bool SyscapInfoCollector::AddPrivateSyscapToJson(cJSON* const jsonData, const char* const privateBuffer)
123 {
124     cJSON* item = cJSON_AddStringToObject(jsonData, CHARACTER_PRIVATE_SYSCAP.c_str(), privateBuffer);
125     if (!cJSON_IsString(item)) {
126         HILOGE("Add CHARACTER_PRIVATE_SYSCAP to cJSON failed!");
127         return false;
128     }
129     return true;
130 }
131 
GenJsonStr(const cJSON * const jsonData,std::string & jsonStr)132 bool SyscapInfoCollector::GenJsonStr(const cJSON* const jsonData, std::string& jsonStr)
133 {
134     char* jsonChars = cJSON_PrintUnformatted(jsonData);
135     if (jsonChars == NULL) {
136         HILOGE("cJSON formatted to string failed!");
137         return false;
138     }
139     jsonStr = jsonChars;
140     cJSON_free(jsonChars);
141     return true;
142 }
143 } // namespace DeviceProfile
144 } // namespace OHOS