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 "syscap_info_collector.h" 17 18 #include "device_profile_errors.h" 19 #include "device_profile_log.h" 20 #include "nlohmann/json.hpp" 21 #include "parameters.h" 22 #include "syscap_interface.h" 23 24 namespace OHOS { 25 namespace DeviceProfile { 26 namespace { 27 const std::string TAG = "SyscapInfoCollector"; 28 29 const std::string SERVICE_ID = "syscap"; 30 const std::string SERVICE_TYPE = "syscap"; 31 const std::string CHARACTER_PRIVATE_SYSCAP = "privatesyscap"; 32 const std::string CHARACTER_OS_SYSCAP = "ossyscap"; 33 constexpr int32_t MAX_DATALEN = 1128; 34 constexpr int32_t INT_BYTES_LEN = 4; 35 } 36 ConvertToProfileData(ServiceCharacteristicProfile & profile)37bool SyscapInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile) 38 { 39 profile.SetServiceId(SERVICE_ID); 40 profile.SetServiceType(SERVICE_TYPE); 41 42 char osBuffer[PCID_MAIN_BYTES]; 43 if (!EncodeOsSyscap(osBuffer, PCID_MAIN_BYTES)) { 44 HILOGE("EncodeOsSyscap failed"); 45 return false; 46 } 47 48 std::vector<int32_t> osSyscapData; 49 for (int i = 0; i < PCID_MAIN_BYTES/INT_BYTES_LEN; i++) { 50 int32_t value = *((int32_t *)osBuffer + i); 51 osSyscapData.push_back(value); 52 } 53 54 nlohmann::json jsonData; 55 nlohmann::json osSyscapJsonData(osSyscapData); 56 jsonData[CHARACTER_OS_SYSCAP] = osSyscapJsonData; 57 58 char* privateBuffer = nullptr; 59 int32_t privateBufferLen; 60 if (!EncodePrivateSyscap(&privateBuffer, &privateBufferLen)) { 61 HILOGE("EncodePrivateSyscap failed"); 62 return false; 63 } 64 if (privateBufferLen + PCID_MAIN_BYTES > MAX_DATALEN) { 65 free(privateBuffer); 66 HILOGI("syscap data length too long"); 67 return false; 68 } 69 jsonData[CHARACTER_PRIVATE_SYSCAP] = privateBuffer; 70 free(privateBuffer); 71 profile.SetCharacteristicProfileJson(jsonData.dump()); 72 return true; 73 } 74 } // namespace DeviceProfile 75 } // namespace OHOS