• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "device_info_collector.h"
17 
18 #include "device_profile_log.h"
19 #include "nlohmann/json.hpp"
20 #include "parameter.h"
21 
22 namespace OHOS {
23 namespace DeviceProfile {
24 namespace {
25 const std::string TAG = "DeviceInfoCollector";
26 
27 const std::string SERVICE_ID = "device";
28 const std::string SERVICE_TYPE = "device";
29 const std::string EMPTY_PARAM = "";
30 const std::string DEVICE_NAME = "deviceName";
31 const std::string DEVICE_MODEL = "model";
32 const std::string DEVICE_UDID = "udid";
33 const std::string DEVICE_TYPE = "devType";
34 const std::string DEVICE_MANU = "manu";
35 const std::string DEVICE_SN = "sn";
36 constexpr int32_t DEVICE_UUID_LENGTH = 65;
37 }
38 
ConvertToProfileData(ServiceCharacteristicProfile & profile)39 bool DeviceInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile)
40 {
41     profile.SetServiceId(SERVICE_ID);
42     profile.SetServiceType(SERVICE_TYPE);
43     nlohmann::json jsonData;
44     jsonData[DEVICE_NAME] = GetDeviceName();
45     jsonData[DEVICE_MODEL] = GetDeviceModel();
46     jsonData[DEVICE_UDID] = GetDeviceUdid();
47     jsonData[DEVICE_TYPE] = GetDevType();
48     jsonData[DEVICE_MANU] = GetDeviceManufacturer();
49     jsonData[DEVICE_SN] = GetDeviceSerial();
50     profile.SetCharacteristicProfileJson(jsonData.dump());
51     return true;
52 }
53 
GetDeviceModel()54 std::string DeviceInfoCollector::GetDeviceModel()
55 {
56     // The pointer is const in the interface. So no longer free char* productModelTempl.
57     const char* productModelTempl = GetProductModel();
58     if (productModelTempl == nullptr) {
59         HILOGE("get failed");
60         return EMPTY_PARAM;
61     }
62     std::string deviceModel = productModelTempl;
63     return deviceModel;
64 }
65 
GetDevType()66 std::string DeviceInfoCollector::GetDevType()
67 {
68     // The pointer is const in the interface. So no longer free char* deviceTypeTempl.
69     const char* deviceTypeTempl = GetDeviceType();
70     if (deviceTypeTempl == nullptr) {
71         HILOGE("get failed");
72         return EMPTY_PARAM;
73     }
74     std::string deviceType = deviceTypeTempl;
75     return deviceType;
76 }
77 
GetDeviceManufacturer()78 std::string DeviceInfoCollector::GetDeviceManufacturer()
79 {
80     // The pointer is const in the interface. So no longer free char* manuFactureTempl.
81     const char* manuFactureTempl = GetManufacture();
82     if (manuFactureTempl == nullptr) {
83         HILOGE("get failed");
84         return EMPTY_PARAM;
85     }
86     std::string manuFacture = manuFactureTempl;
87     return manuFacture;
88 }
89 
GetDeviceSerial()90 std::string DeviceInfoCollector::GetDeviceSerial()
91 {
92     // The pointer is const in the interface. So no longer free char* serialTempl.
93     const char* serialTempl = GetSerial();
94     if (serialTempl == nullptr) {
95         HILOGE("get failed");
96         return EMPTY_PARAM;
97     }
98     std::string deviceSerial = serialTempl;
99     return deviceSerial;
100 }
101 
GetDeviceName()102 std::string DeviceInfoCollector::GetDeviceName()
103 {
104     // The pointer is const in the interface. So no longer free char* marketNameTempl.
105     const char* marketNameTempl = GetMarketName();
106     if (marketNameTempl == nullptr) {
107         return GetDeviceModel();
108     }
109     std::string devName = marketNameTempl;
110     if (!devName.empty()) {
111         return devName;
112     }
113     return GetDeviceModel();
114 }
115 
GetDeviceUdid()116 std::string DeviceInfoCollector::GetDeviceUdid()
117 {
118     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
119     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
120     return localDeviceId;
121 }
122 } // namespace DeviceProfile
123 } // namespace OHOS