1 /*
2 * Copyright (c) 2022-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 "softbus_adapter_hiview.h"
17
18 #include <cstdlib>
19
20 #include "hiview.h"
21 #include "comm_log.h"
22 #include "parameter.h"
23 #include "securec.h"
24 #include "softbus_adapter_mem.h"
25
26 #define PROP_USER_TYPE "ro.logsystem.usertype"
27 #define PROP_USER_TYPE_VALUE_LEN 8
28 #define PROP_USER_TYPE_CHANA_BETA 3
29
SoftBusGetLogSysType(void)30 SoftBusLogSysType SoftBusGetLogSysType(void)
31 {
32 static SoftBusLogSysType userType = SOFTBUS_LOG_SYS_UNKNOWN;
33 static bool isUserTypeObtained = false;
34
35 if (isUserTypeObtained) {
36 return userType;
37 }
38
39 char value[PROP_USER_TYPE_VALUE_LEN] = { 0 };
40 int32_t ret = GetParameter(PROP_USER_TYPE, "", value, sizeof(value));
41 if (ret < 0) {
42 COMM_LOGE(COMM_ADAPTER, "GetProp fail. userType=%{public}s, ret=%{public}d", PROP_USER_TYPE, ret);
43 return SOFTBUS_LOG_SYS_UNKNOWN;
44 }
45 isUserTypeObtained = true;
46 COMM_LOGI(COMM_ADAPTER, "userType=%{public}s, value=%{public}s", PROP_USER_TYPE, value);
47
48 // is beta or not: 1 china release, 3 china beta, 5 oversea beta, 6 oversea release
49 if (atoi(value) == PROP_USER_TYPE_CHANA_BETA) {
50 userType = SOFTBUS_LOG_SYS_BETA;
51 return userType;
52 }
53 userType = SOFTBUS_LOG_SYS_RELEASE;
54 return userType;
55 }
56
SoftBusGenHiviewHash(const char * deviceId,char * buf,uint32_t size)57 void SoftBusGenHiviewHash(const char *deviceId, char *buf, uint32_t size)
58 {
59 if (deviceId == nullptr || buf == NULL || size < HiView::DEFAULT_TRUNCATED_LENGTH) {
60 COMM_LOGE(COMM_ADAPTER, "invalid param");
61 return;
62 }
63 std::string input(deviceId);
64 std::string hash = HiView::GenTruncatedHash(input, HiView::ALGORITHM_SHA_256, HiView::DEFAULT_TRUNCATED_LENGTH);
65 if (hash.empty()) {
66 COMM_LOGE(COMM_ADAPTER, "HiView::genTruncatedHash return empty hash");
67 return;
68 }
69 if (strcpy_s(buf, size, hash.c_str()) != EOK) {
70 COMM_LOGE(COMM_ADAPTER, "HiView::genTruncatedHash strcpy_s fail");
71 return;
72 }
73 }