• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <iomanip>
20 #include <sstream>
21 
22 #include "cJSON.h"
23 #include "parameter.h"
24 #include "parameters.h"
25 #include "softbus_bus_center.h"
26 #include "softbus_error_code.h"
27 
28 #include "distributed_device_profile_constants.h"
29 #include "distributed_device_profile_log.h"
30 
31 namespace OHOS {
32 namespace DistributedDeviceProfile {
33 namespace {
34     const std::string TAG = "ContentSensorManagerUtils";
35     const char* SYS_SETTINGS_DATA_SYNC = "persist.distributed_scene.sys_settings_data_sync";
36     const char* PRODUCT_ID_KEY = "const.distributed_collaboration.productId";
37     const char* UNDEFINED_VALUE = "undefined";
38     const char* SYNC_TYPE_E2E = "1";
39     const char* MANUFACTURER_KEY = "const.product.manufacturer";
40     constexpr int32_t DEVICE_UUID_LENGTH = 65;
41     constexpr int32_t SYS_SETTINGS_DATA_SYNC_PARAM_LEN = 128;
42     constexpr int32_t PROT_TYPE_WIFI_ONLY = 1;
43     constexpr int32_t PROT_TYPE_MOBILIE_NETWORK_AND_WIFI = 18;
44     constexpr const char* WIFI_ONLY_DEVICE_TREE_PROC_NODE_NAME = "/proc/device-tree/singleap_wifionly/is_wifionly";
45     constexpr const char* WIFI_ONLY_FLAG_VALUE = "1";
46     constexpr int32_t WIFI_ONLY_FLAG_VALUE_MAX_LEN = 8;
47     const char* OHOS_BOOT_BACKCOLOR = "ohos.boot.backcolor";
48     const char* SUB_PROD_ID_MAP = "const.distributed_collaboration.subProdIdMap";
49     const std::string MANU_NAME = "485541574549";
50     const std::string MANU_CODE = "001";
51 }
52 IMPLEMENT_SINGLE_INSTANCE(ContentSensorManagerUtils);
ObtainProductModel()53 std::string ContentSensorManagerUtils::ObtainProductModel()
54 {
55     HILOGI("called!");
56     std::lock_guard<std::mutex> lock(csMutex_);
57     if (!deviceModel_.empty()) {
58         return deviceModel_;
59     }
60     const char* productModelTemp = GetProductModel();
61     if (productModelTemp == nullptr) {
62         HILOGE("get productModel failed!");
63         return "";
64     }
65     deviceModel_ = productModelTemp;
66     free((char*)productModelTemp);
67     return deviceModel_;
68 }
69 
ObtainDeviceType()70 std::string ContentSensorManagerUtils::ObtainDeviceType()
71 {
72     HILOGI("called!");
73     std::lock_guard<std::mutex> lock(csMutex_);
74     if (!deviceType_.empty()) {
75         return deviceType_;
76     }
77     const char* deviceTypeTemp = GetDeviceType();
78     if (deviceTypeTemp == nullptr) {
79         HILOGE("get deviceType failed!");
80         return "";
81     }
82     deviceType_ = deviceTypeTemp;
83     free((char*)deviceTypeTemp);
84     return deviceType_;
85 }
86 
ObtainDeviceTypeId()87 std::string ContentSensorManagerUtils::ObtainDeviceTypeId()
88 {
89     {
90         std::lock_guard<std::mutex> lock(csMutex_);
91         if (!deviceTypeId_.empty()) {
92             return deviceTypeId_;
93         }
94     }
95     NodeBasicInfo nodeBasicInfo;
96     int32_t ret = GetLocalNodeDeviceInfo(DP_PKG_NAME.c_str(), &nodeBasicInfo);
97     if (ret != SOFTBUS_OK) {
98         HILOGE("GetLocalNodeDeviceInfo from dsofbus fail, ret=%{public}d", ret);
99         return "";
100     }
101     uint16_t deviceTypeId = nodeBasicInfo.deviceTypeId;
102     std::stringstream strDeviceTypeId;
103     strDeviceTypeId << std::uppercase << std::setw(NUM_3) << std::setfill('0') << std::hex << deviceTypeId;
104     std::lock_guard<std::mutex> lock(csMutex_);
105     deviceTypeId_ = strDeviceTypeId.str();
106     return deviceTypeId_;
107 }
108 
ObtainManufacture()109 std::string ContentSensorManagerUtils::ObtainManufacture()
110 {
111     HILOGI("called!");
112     std::lock_guard<std::mutex> lock(csMutex_);
113     if (!manufacture_.empty()) {
114         return manufacture_;
115     }
116     std::string manufactureTemp = system::GetParameter(MANUFACTURER_KEY, "");
117     HILOGI("get manufacture from system, manufacture = %{public}s", manufactureTemp.c_str());
118     if (manufactureTemp == DecodeHexStr(MANU_NAME)) {
119         manufactureTemp = MANU_CODE;
120     }
121     if (manufactureTemp.empty()) {
122         HILOGE("get manufacture failed!");
123         return "";
124     }
125     manufacture_ = manufactureTemp;
126     return manufacture_;
127 }
128 
ObtainSerial()129 std::string ContentSensorManagerUtils::ObtainSerial()
130 {
131     HILOGI("called!");
132     std::lock_guard<std::mutex> lock(csMutex_);
133     if (!serial_.empty()) {
134         return serial_;
135     }
136     const char* serialTemp = GetSerial();
137     if (serialTemp == nullptr) {
138         HILOGE("get serial failed!");
139         return "";
140     }
141     serial_ = serialTemp;
142     free((char*)serialTemp);
143     return serial_;
144 }
145 
ObtainMarketName()146 std::string ContentSensorManagerUtils::ObtainMarketName()
147 {
148     HILOGI("called!");
149     std::lock_guard<std::mutex> lock(csMutex_);
150     if (!marketName_.empty()) {
151         return marketName_;
152     }
153     const char* marketNameTemp = GetMarketName();
154     if (marketNameTemp == nullptr) {
155         HILOGE("get marketName failed!");
156         return "";
157     }
158     marketName_ = marketNameTemp;
159     free((char*)marketNameTemp);
160     return marketName_;
161 }
162 
ObtainOsFullName()163 std::string ContentSensorManagerUtils::ObtainOsFullName()
164 {
165     HILOGI("called!");
166     std::lock_guard<std::mutex> lock(csMutex_);
167     if (!osFullName_.empty()) {
168         return osFullName_;
169     }
170     const char* osFullNameTemp = GetOSFullName();
171     if (osFullNameTemp == nullptr) {
172         HILOGE("get osFullName failed!");
173         return "";
174     }
175     osFullName_ = osFullNameTemp;
176     free((char*)osFullNameTemp);
177     return osFullName_;
178 }
179 
ObtainDisplayVersion()180 std::string ContentSensorManagerUtils::ObtainDisplayVersion()
181 {
182     HILOGI("called!");
183     std::lock_guard<std::mutex> lock(csMutex_);
184     if (!displayVersion_.empty()) {
185         return displayVersion_;
186     }
187     const char* displayVersionTemp = GetDisplayVersion();
188     if (displayVersionTemp == nullptr) {
189         HILOGE("get displayVersion failed!");
190         return "";
191     }
192     displayVersion_ = displayVersionTemp;
193     free((char*)displayVersionTemp);
194     return displayVersion_;
195 }
196 
ObtainLocalUdid()197 std::string ContentSensorManagerUtils::ObtainLocalUdid()
198 {
199     std::lock_guard<std::mutex> lock(csMutex_);
200     if (!localUdid_.empty()) {
201         return localUdid_;
202     }
203     HILOGD("GetDevUdid");
204     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
205     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
206     localUdid_ = localUdidTemp;
207     return localUdid_;
208 }
209 
ObtainProductId()210 std::string ContentSensorManagerUtils::ObtainProductId()
211 {
212     HILOGI("called!");
213     std::lock_guard<std::mutex> lock(csMutex_);
214     if (!productId_.empty()) {
215         return productId_;
216     }
217     std::string productIdTemp = system::GetParameter(PRODUCT_ID_KEY, "");
218     if (productIdTemp.empty()) {
219         HILOGE("get productId failed!");
220         return "";
221     }
222     productId_ = productIdTemp;
223     if (productId_.size() > NUM_1) {
224         productId_.pop_back();
225     }
226     return productId_;
227 }
228 
ObtainDeviceDataSyncMode()229 void ContentSensorManagerUtils::ObtainDeviceDataSyncMode()
230 {
231     char isE2EDeviceParam[SYS_SETTINGS_DATA_SYNC_PARAM_LEN + 1] = {0};
232     int ret = GetParameter(SYS_SETTINGS_DATA_SYNC, UNDEFINED_VALUE, isE2EDeviceParam,
233         SYS_SETTINGS_DATA_SYNC_PARAM_LEN);
234     if (ret > 0 && strncmp(isE2EDeviceParam, SYNC_TYPE_E2E, strlen(SYNC_TYPE_E2E)) == 0) {
235         isDeviceE2ESync_.store(true);
236         HILOGI("Determining the e2e device succeeded.");
237         return;
238     }
239     HILOGW("Determining is not e2e device");
240 }
241 
IsDeviceE2ESync()242 bool ContentSensorManagerUtils::IsDeviceE2ESync()
243 {
244     return isDeviceE2ESync_.load();
245 }
246 
GetProtType()247 int32_t ContentSensorManagerUtils::GetProtType()
248 {
249     std::lock_guard<std::mutex> lock(csMutex_);
250     if (protType_ > 0) {
251         return protType_;
252     }
253     if (IsWifiOnly()) {
254         protType_ = PROT_TYPE_WIFI_ONLY;
255     } else {
256         protType_ = PROT_TYPE_MOBILIE_NETWORK_AND_WIFI;
257     }
258     HILOGI("protType:%{public}d", protType_);
259     return protType_;
260 }
261 
IsWifiOnly()262 bool ContentSensorManagerUtils::IsWifiOnly()
263 {
264     char buf[WIFI_ONLY_FLAG_VALUE_MAX_LEN] = {0};
265     FILE *fp = nullptr;
266     if ((fp = fopen(WIFI_ONLY_DEVICE_TREE_PROC_NODE_NAME, "r")) == nullptr) {
267         HILOGE("open wifi only device tree proc node fail");
268         return false;
269     }
270     if (fgets(buf, WIFI_ONLY_FLAG_VALUE_MAX_LEN, fp) == nullptr) {
271         HILOGE("fgets return nullptr");
272         if (fclose(fp) != 0) {
273             HILOGE("Close file failed");
274         }
275         return false;
276     }
277     if (strcmp(buf, WIFI_ONLY_FLAG_VALUE)) {
278         HILOGE("buf not equal WIFI_ONLY_FLAG_VALUE");
279         if (fclose(fp) != 0) {
280             HILOGE("Close file failed");
281         }
282         return false;
283     }
284     if (fclose(fp) != 0) {
285         HILOGE("Close file failed");
286     }
287     return true;
288 }
289 
GetSubProductId()290 std::string ContentSensorManagerUtils::GetSubProductId()
291 {
292     std::string backcolor = GetBackcolor();
293     std::map<std::string, std::string> subProdIdMap = GetSubProdIdMap();
294     if (backcolor.empty() || subProdIdMap.empty() || subProdIdMap.find(backcolor) == subProdIdMap.end()) {
295         return EMPTY_STRING;
296     }
297     return subProdIdMap[backcolor];
298 }
299 
GetBackcolor()300 std::string ContentSensorManagerUtils::GetBackcolor()
301 {
302     std::lock_guard<std::mutex> lock(csMutex_);
303     if (!backcolor_.empty()) {
304         return backcolor_;
305     }
306     std::string temp = system::GetParameter(OHOS_BOOT_BACKCOLOR, "");
307     if (temp.empty()) {
308         HILOGE("get backcolor failed!");
309         return "";
310     }
311     backcolor_ = temp;
312     return backcolor_;
313 }
314 
GetSubProdIdMap()315 std::map<std::string, std::string> ContentSensorManagerUtils::GetSubProdIdMap()
316 {
317     std::lock_guard<std::mutex> lock(csMutex_);
318     if (!subProdIdMap_.empty()) {
319         return subProdIdMap_;
320     }
321     std::string temp = system::GetParameter(SUB_PROD_ID_MAP, "");
322     if (temp.empty()) {
323         HILOGE("get subProdIdMap failed!");
324         return {};
325     }
326     cJSON* json = cJSON_Parse(temp.c_str());
327     if (!cJSON_IsObject(json)) {
328         HILOGW("cJSON_Parse productName fail!");
329         cJSON_Delete(json);
330         return {};
331     }
332     cJSON* item = json->child;
333     while (item != NULL) {
334         if (cJSON_IsString(item)) {
335             subProdIdMap_[item->string] = item->valuestring;
336         }
337         item = item->next;
338     }
339     cJSON_Delete(json);
340     return subProdIdMap_;
341 }
342 
DecodeHexStr(const std::string & str)343 std::string ContentSensorManagerUtils::DecodeHexStr(const std::string &str)
344 {
345     if (str.empty() || str.length() % NUM_2 != 0) {
346         HILOGE("str.length:%{public}zu is not an even number.", str.length());
347         return EMPTY_STRING;
348     }
349     std::vector<uint8_t> bytes;
350     for (size_t i = 0; i < str.length(); i += NUM_2) {
351         std::string byteStr = str.substr(i, NUM_2);
352         long result = strtol(byteStr.c_str(), nullptr, NUM_16);
353         if (result == LONG_MIN || result == LONG_MAX) {
354             HILOGE("decode hexstring error.");
355             return EMPTY_STRING;
356         }
357         uint8_t byte = (uint8_t)result;
358         bytes.push_back(byte);
359     }
360     if (bytes.empty()) {
361         HILOGE("bytes is empty");
362         return EMPTY_STRING;
363     }
364     return std::string(bytes.begin(), bytes.end());
365 }
366 } // namespace DistributedDeviceProfile
367 } // namespace OHOS
368