1 /*
2  * Copyright (c) 2023-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 "content_sensor_manager_utils.h"
17 
18 #include <cstring>
19 
20 #include "parameter.h"
21 
22 #include "distributed_device_profile_log.h"
23 
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 namespace {
27     const std::string TAG = "ContentSensorManagerUtils";
28     const char* SYS_SETTINGS_DATA_SYNC = "persist.distributed_scene.sys_settings_data_sync";
29     const char* UNDEFINED_VALUE = "undefined";
30     const char* SYNC_TYPE_E2E = "1";
31     constexpr int32_t DEVICE_UUID_LENGTH = 65;
32     constexpr int32_t SYS_SETTINGS_DATA_SYNC_PARAM_LEN = 128;
33 }
34 IMPLEMENT_SINGLE_INSTANCE(ContentSensorManagerUtils);
ObtainProductModel()35 std::string ContentSensorManagerUtils::ObtainProductModel()
36 {
37     HILOGI("called!");
38     std::lock_guard<std::mutex> lock(csMutex_);
39     if (!deviceModel_.empty()) {
40         return deviceModel_;
41     }
42     const char* productModelTemp = GetProductModel();
43     if (productModelTemp == nullptr) {
44         HILOGE("get productModel failed!");
45         return "";
46     }
47     deviceModel_ = productModelTemp;
48     free((char*)productModelTemp);
49     return deviceModel_;
50 }
51 
ObtainDeviceType()52 std::string ContentSensorManagerUtils::ObtainDeviceType()
53 {
54     HILOGI("called!");
55     std::lock_guard<std::mutex> lock(csMutex_);
56     if (!deviceType_.empty()) {
57         return deviceType_;
58     }
59     const char* deviceTypeTemp = GetDeviceType();
60     if (deviceTypeTemp == nullptr) {
61         HILOGE("get deviceType failed!");
62         return "";
63     }
64     deviceType_ = deviceTypeTemp;
65     free((char*)deviceTypeTemp);
66     return deviceType_;
67 }
68 
ObtainManufacture()69 std::string ContentSensorManagerUtils::ObtainManufacture()
70 {
71     HILOGI("called!");
72     std::lock_guard<std::mutex> lock(csMutex_);
73     if (!manufacture_.empty()) {
74         return manufacture_;
75     }
76     const char* manufactureTemp = GetManufacture();
77     if (manufactureTemp == nullptr) {
78         HILOGE("get manufacture failed!");
79         return "";
80     }
81     manufacture_ = manufactureTemp;
82     free((char*)manufactureTemp);
83     return manufacture_;
84 }
85 
ObtainSerial()86 std::string ContentSensorManagerUtils::ObtainSerial()
87 {
88     HILOGI("called!");
89     std::lock_guard<std::mutex> lock(csMutex_);
90     if (!serial_.empty()) {
91         return serial_;
92     }
93     const char* serialTemp = GetSerial();
94     if (serialTemp == nullptr) {
95         HILOGE("get serial failed!");
96         return "";
97     }
98     serial_ = serialTemp;
99     free((char*)serialTemp);
100     return serial_;
101 }
102 
ObtainMarketName()103 std::string ContentSensorManagerUtils::ObtainMarketName()
104 {
105     HILOGI("called!");
106     std::lock_guard<std::mutex> lock(csMutex_);
107     if (!marketName_.empty()) {
108         return marketName_;
109     }
110     const char* marketNameTemp = GetMarketName();
111     if (marketNameTemp == nullptr) {
112         HILOGE("get marketName failed!");
113         return "";
114     }
115     marketName_ = marketNameTemp;
116     free((char*)marketNameTemp);
117     return marketName_;
118 }
119 
ObtainOsFullName()120 std::string ContentSensorManagerUtils::ObtainOsFullName()
121 {
122     HILOGI("called!");
123     std::lock_guard<std::mutex> lock(csMutex_);
124     if (!osFullName_.empty()) {
125         return osFullName_;
126     }
127     const char* osFullNameTemp = GetOSFullName();
128     if (osFullNameTemp == nullptr) {
129         HILOGE("get osFullName failed!");
130         return "";
131     }
132     osFullName_ = osFullNameTemp;
133     free((char*)osFullNameTemp);
134     return osFullName_;
135 }
136 
ObtainDisplayVersion()137 std::string ContentSensorManagerUtils::ObtainDisplayVersion()
138 {
139     HILOGI("called!");
140     std::lock_guard<std::mutex> lock(csMutex_);
141     if (!displayVersion_.empty()) {
142         return displayVersion_;
143     }
144     const char* displayVersionTemp = GetDisplayVersion();
145     if (displayVersionTemp == nullptr) {
146         HILOGE("get displayVersion failed!");
147         return "";
148     }
149     displayVersion_ = displayVersionTemp;
150     free((char*)displayVersionTemp);
151     return displayVersion_;
152 }
153 
ObtainLocalUdid()154 std::string ContentSensorManagerUtils::ObtainLocalUdid()
155 {
156     std::lock_guard<std::mutex> lock(csMutex_);
157     if (!localUdid_.empty()) {
158         return localUdid_;
159     }
160     HILOGD("GetDevUdid");
161     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
162     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
163     localUdid_ = localUdidTemp;
164     return localUdid_;
165 }
166 
ObtainDeviceDataSyncMode()167 void ContentSensorManagerUtils::ObtainDeviceDataSyncMode()
168 {
169     char isE2EDeviceParam[SYS_SETTINGS_DATA_SYNC_PARAM_LEN + 1] = {0};
170     int ret = GetParameter(SYS_SETTINGS_DATA_SYNC, UNDEFINED_VALUE, isE2EDeviceParam,
171         SYS_SETTINGS_DATA_SYNC_PARAM_LEN);
172     if (ret > 0 && strncmp(isE2EDeviceParam, SYNC_TYPE_E2E, strlen(SYNC_TYPE_E2E)) == 0) {
173         isDeviceE2ESync_.store(true);
174         HILOGI("Determining the e2e device succeeded.");
175         return;
176     }
177     HILOGW("Determining is not e2e device");
178 }
179 
IsDeviceE2ESync()180 bool ContentSensorManagerUtils::IsDeviceE2ESync()
181 {
182     return isDeviceE2ESync_.load();
183 }
184 } // namespace DistributedDeviceProfile
185 } // namespace OHOS
186