• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_errors.h"
19 #include "device_profile_log.h"
20 
21 #include "nlohmann/json.hpp"
22 #include "parameter.h"
23 #include "parameters.h"
24 
25 namespace OHOS {
26 namespace DeviceProfile {
27 namespace {
28 const std::string TAG = "DeviceInfoCollector";
29 
30 const std::string SERVICE_ID = "device";
31 const std::string SERVICE_TYPE = "device";
32 const std::string EMPTY_PARAM = "";
33 const std::string DEVICE_NAME = "deviceName";
34 const std::string DEVICE_MODEL = "model";
35 const std::string DEVICE_UDID = "udid";
36 const std::string DEVICE_TYPE = "devType";
37 const std::string DEVICE_MANU = "manu";
38 const std::string DEVICE_PRODUCT_ID = "prodId";
39 const std::string DEVICE_SN = "sn";
40 const std::string DEVICE_NAME_PARAM = "ro.product.name";
41 const std::string DEVICE_TYPE_PARAM = "ro.build.characteristics";
42 constexpr int32_t DEVICE_UUID_LENGTH = 65;
43 }
44 
ConvertToProfileData(ServiceCharacteristicProfile & profile)45 void DeviceInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile)
46 {
47     profile.SetServiceId(SERVICE_ID);
48     profile.SetServiceType(SERVICE_TYPE);
49     nlohmann::json jsonData;
50     jsonData[DEVICE_NAME] = GetDeviceName();
51     jsonData[DEVICE_MODEL] = GetDeviceModel();
52     jsonData[DEVICE_UDID] = GetDeviceUdid();
53     jsonData[DEVICE_TYPE] = GetDeviceType();
54     jsonData[DEVICE_MANU] = GetDeviceManufacturer();
55     jsonData[DEVICE_PRODUCT_ID] = GetDeviceProductId();
56     jsonData[DEVICE_SN] = GetDeviceSerial();
57     profile.SetCharacteristicProfileJson(jsonData.dump());
58 }
59 
GetDeviceModel()60 std::string DeviceInfoCollector::GetDeviceModel()
61 {
62     return GetProductModel();
63 }
64 
GetDeviceManufacturer()65 std::string DeviceInfoCollector::GetDeviceManufacturer()
66 {
67     return GetManufacture();
68 }
69 
GetDeviceSerial()70 std::string DeviceInfoCollector::GetDeviceSerial()
71 {
72     return GetSerial();
73 }
74 
GetDeviceType()75 std::string DeviceInfoCollector::GetDeviceType()
76 {
77     return system::GetParameter(DEVICE_TYPE_PARAM, EMPTY_PARAM);
78 }
79 
GetDeviceName()80 std::string DeviceInfoCollector::GetDeviceName()
81 {
82     std::string devName = system::GetParameter(DEVICE_NAME_PARAM, EMPTY_PARAM);
83     if (!devName.empty()) {
84         return devName;
85     }
86     return GetProductModel();
87 }
88 
GetDeviceUdid()89 std::string DeviceInfoCollector::GetDeviceUdid()
90 {
91     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
92     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
93     return localDeviceId;
94 }
95 
GetDeviceProductId()96 std::string DeviceInfoCollector::GetDeviceProductId()
97 {
98     return "";
99 }
100 } // namespace DeviceProfile
101 } // namespace OHOS