1 /*
2 * Copyright (c) 2021-2023 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 <stddef.h>
17 #include <string.h>
18
19 #include <securec.h>
20
21 #include "anonymizer.h"
22 #include "bus_center_adapter.h"
23 #include "bus_center_info_key.h"
24 #include "parameter.h"
25 #include "lnn_log.h"
26 #include "lnn_settingdata_event_monitor.h"
27 #include "softbus_adapter_bt_common.h"
28 #include "softbus_adapter_mem.h"
29 #include "softbus_bus_center.h"
30 #include "softbus_common.h"
31 #include "softbus_def.h"
32 #include "softbus_errcode.h"
33 #include "softbus_feature_config.h"
34 #include "softbus_utils.h"
35
36 #define OHOS_API_VERSION "const.ohos.apiversion"
37 #define OHOS_BOOT_SN "ohos.boot.sn"
38 #define OS_VERSION "const.ohos.fullname" /* Read osversion by the string */
39 #define DEVICE_VERSION "const.build.ver.physical" /* Read deviceversion by the string */
40 #define VERSION_SDK "ro.build.version.sdk"
41 #define UNDEFINED_VALUE "undefined"
42 #define OHOS_DEVICE_SECURITY_LEVEL "const.security.device_security_level"
43 #define OHOS_TYPE_UNKNOWN (-1)
44 #define API_VERSION_LEN 10
45 #define VERSION_SDK_LEN 10
46 #define SN_LEN 32
47
48 typedef struct {
49 const char *inBuf;
50 const char *outBuf;
51 } TypeInfo;
52
53 static TypeInfo g_typeConvertMap[] = {
54 {GET_TYPE_UNKNOWN, TYPE_UNKNOWN},
55 {GET_TYPE_PHONE, TYPE_PHONE},
56 {GET_TYPE_PAD, TYPE_PAD},
57 {GET_TYPE_TV, TYPE_TV},
58 {GET_TYPE_CAR, TYPE_CAR},
59 {GET_TYPE_WATCH, TYPE_WATCH},
60 {GET_TYPE_IPCAMERA, TYPE_IPCAMERA},
61 {GET_TYPE_2IN1, TYPE_2IN1},
62 };
63
SoftBusGetBleMacAddr(char * macStr,uint32_t len)64 static int32_t SoftBusGetBleMacAddr(char *macStr, uint32_t len)
65 {
66 int32_t bleMacRefreshSwitch;
67 if (SoftbusGetConfig(SOFTBUS_INT_BLE_MAC_AUTO_REFRESH_SWITCH,
68 (unsigned char *)(&bleMacRefreshSwitch), sizeof(bleMacRefreshSwitch)) != SOFTBUS_OK) {
69 LNN_LOGE(LNN_STATE, "get ble mac refresh switch from config file fail");
70 return SOFTBUS_ERR;
71 }
72 /* ble mac not periodic refresh, return error if get ble mac fail */
73 if (bleMacRefreshSwitch == 0) {
74 int32_t ret;
75 SoftBusBtAddr mac = {0};
76
77 if (len != BT_MAC_LEN) {
78 return SOFTBUS_INVALID_PARAM;
79 }
80 ret = SoftBusGetBtMacAddr(&mac);
81 if (ret != SOFTBUS_OK) {
82 LNN_LOGE(LNN_STATE, "get ble mac addr fail");
83 return ret;
84 }
85 ret = ConvertReverseBtMacToStr(macStr, len, mac.addr, sizeof(mac.addr));
86 if (ret != SOFTBUS_OK) {
87 LNN_LOGE(LNN_STATE, "convert bt mac to str fail");
88 return ret;
89 }
90 return SOFTBUS_OK;
91 }
92 /* ble mac periodic refresh, return SOFTBUS_OK */
93 (void)memset_s(macStr, len, 0, len);
94 return SOFTBUS_OK;
95 }
96
SoftBusConvertDeviceType(const char * inBuf,char * outBuf,uint32_t outLen)97 static int32_t SoftBusConvertDeviceType(const char *inBuf, char *outBuf, uint32_t outLen)
98 {
99 uint32_t id;
100 for (id = 0; id < sizeof(g_typeConvertMap) / sizeof(TypeInfo); id++) {
101 if (strcmp(g_typeConvertMap[id].inBuf, inBuf) == EOK) {
102 if (strcpy_s(outBuf, outLen, g_typeConvertMap[id].outBuf) != EOK) {
103 LNN_LOGE(LNN_STATE, "strcpy_s fail");
104 return SOFTBUS_ERR;
105 }
106 return SOFTBUS_OK;
107 }
108 }
109 return SOFTBUS_ERR;
110 }
111
SoftBusGetOsType(void)112 static int32_t SoftBusGetOsType(void)
113 {
114 char apiVersion[API_VERSION_LEN + 1];
115 (void)memset_s(apiVersion, API_VERSION_LEN + 1, 0, API_VERSION_LEN + 1);
116 GetParameter(OHOS_API_VERSION, UNDEFINED_VALUE, apiVersion, API_VERSION_LEN);
117 char bootSN[SN_LEN + 1];
118 (void)memset_s(bootSN, SN_LEN + 1, 0, SN_LEN + 1);
119 GetParameter(OHOS_BOOT_SN, UNDEFINED_VALUE, bootSN, SN_LEN);
120 char osVersion[OS_VERSION_BUF_LEN];
121 (void)memset_s(osVersion, OS_VERSION_BUF_LEN, 0, OS_VERSION_BUF_LEN);
122 GetParameter(OS_VERSION, UNDEFINED_VALUE, osVersion, OS_VERSION_BUF_LEN);
123 if (strcmp(apiVersion, UNDEFINED_VALUE) != 0 || strcmp(bootSN, UNDEFINED_VALUE) != 0 ||
124 strcmp(osVersion, UNDEFINED_VALUE) != 0) {
125 char *anonyBootSN = NULL;
126 Anonymize(bootSN, &anonyBootSN);
127 LNN_LOGI(LNN_STATE, "apiVersion: %{public}s bootSN: %{public}s osVersion: %{public}s", apiVersion, anonyBootSN,
128 osVersion);
129 AnonymizeFree(anonyBootSN);
130 return OH_OS_TYPE;
131 }
132 char versionSDK[VERSION_SDK_LEN + 1];
133 (void)memset_s(versionSDK, VERSION_SDK_LEN + 1, 0, VERSION_SDK_LEN + 1);
134 GetParameter(VERSION_SDK, UNDEFINED_VALUE, versionSDK, VERSION_SDK_LEN);
135 if (strcmp(versionSDK, UNDEFINED_VALUE) != 0) {
136 LNN_LOGI(LNN_STATE, "versionSDK: %{public}s", versionSDK);
137 return HO_OS_TYPE;
138 }
139 LNN_LOGE(LNN_STATE, "GetOsType fail!");
140 return OHOS_TYPE_UNKNOWN;
141 }
142
GetCommonDevInfo(CommonDeviceKey key,char * value,uint32_t len)143 int32_t GetCommonDevInfo(CommonDeviceKey key, char *value, uint32_t len)
144 {
145 if (value == NULL) {
146 LNN_LOGE(LNN_STATE, "para error");
147 return SOFTBUS_INVALID_PARAM;
148 }
149 char localUdid[UDID_BUF_LEN] = {0};
150 const char *devType = NULL;
151 switch (key) {
152 case COMM_DEVICE_KEY_DEVNAME:
153 /* set real value when device name init */
154 break;
155 case COMM_DEVICE_KEY_UDID:
156 if (GetDevUdid(localUdid, UDID_BUF_LEN) != 0) {
157 LNN_LOGE(LNN_STATE, "GetDevUdid failed");
158 return SOFTBUS_ERR;
159 }
160 if (strncpy_s(value, len, localUdid, UDID_BUF_LEN) != EOK) {
161 return SOFTBUS_ERR;
162 }
163 break;
164 case COMM_DEVICE_KEY_DEVTYPE:
165 devType = GetDeviceType();
166 LNN_LOGI(LNN_STATE, "get device from GetDeviceType, GetDeviceType=%{public}s", devType);
167 if (devType != NULL) {
168 char softBusDevType[DEVICE_TYPE_BUF_LEN] = {0};
169 if (SoftBusConvertDeviceType(devType, softBusDevType, DEVICE_TYPE_BUF_LEN) != SOFTBUS_OK) {
170 LNN_LOGE(LNN_STATE, "convert device type fail");
171 return SOFTBUS_ERR;
172 }
173 if (strcpy_s(value, len, softBusDevType) != EOK) {
174 return SOFTBUS_ERR;
175 }
176 } else {
177 LNN_LOGE(LNN_STATE, "GetDeviceType failed");
178 return SOFTBUS_ERR;
179 }
180 break;
181 case COMM_DEVICE_KEY_BLE_MAC:
182 if (SoftBusGetBleMacAddr(value, len) != SOFTBUS_OK) {
183 LNN_LOGE(LNN_STATE, "get ble mac addr failed!");
184 return SOFTBUS_ERR;
185 }
186 break;
187 default:
188 break;
189 }
190 return SOFTBUS_OK;
191 }
192
GetCommonOsType(int32_t * value)193 int32_t GetCommonOsType(int32_t *value)
194 {
195 int32_t ret = SoftBusGetOsType();
196 *value = ret;
197 if (*value == OHOS_TYPE_UNKNOWN) {
198 LNN_LOGE(LNN_STATE, "get invalid os type, osType = %{public}d", *value);
199 return SOFTBUS_ERR;
200 }
201 return SOFTBUS_OK;
202 }
203
GetCommonOsVersion(char * value,uint32_t len)204 int32_t GetCommonOsVersion(char *value, uint32_t len)
205 {
206 if (value == NULL) {
207 LNN_LOGE(LNN_STATE, "para error");
208 return SOFTBUS_INVALID_PARAM;
209 }
210 char *osVersion = (char *)SoftBusCalloc(OS_VERSION_BUF_LEN);
211 if (osVersion == NULL) {
212 LNN_LOGE(LNN_STATE, "calloc osVersion failed!");
213 return SOFTBUS_MEM_ERR;
214 }
215 GetParameter(OS_VERSION, UNDEFINED_VALUE, osVersion, OS_VERSION_BUF_LEN);
216 if (strcmp(osVersion, UNDEFINED_VALUE) != 0) {
217 if (strcpy_s(value, len, osVersion) != EOK) {
218 LNN_LOGE(LNN_STATE, "strcpy_s osVersion failed.");
219 SoftBusFree(osVersion);
220 return SOFTBUS_MEM_ERR;
221 }
222 } else {
223 LNN_LOGE(LNN_STATE, "get invalid osVersion, osVersion= %{public}s", UNDEFINED_VALUE);
224 SoftBusFree(osVersion);
225 return SOFTBUS_ERR;
226 }
227 SoftBusFree(osVersion);
228 return SOFTBUS_OK;
229 }
230
GetCommonDeviceVersion(char * value,uint32_t len)231 int32_t GetCommonDeviceVersion(char *value, uint32_t len)
232 {
233 if (value == NULL) {
234 LNN_LOGE(LNN_STATE, "para error");
235 return SOFTBUS_INVALID_PARAM;
236 }
237 char *deviceVersion = (char *)SoftBusCalloc(DEVICE_VERSION_SIZE_MAX);
238 if (deviceVersion == NULL) {
239 LNN_LOGE(LNN_STATE, "calloc deviceVersion failed!");
240 return SOFTBUS_MEM_ERR;
241 }
242 GetParameter(DEVICE_VERSION, UNDEFINED_VALUE, deviceVersion, DEVICE_VERSION_SIZE_MAX);
243 if (strcmp(deviceVersion, UNDEFINED_VALUE) != 0) {
244 if (strcpy_s(value, len, deviceVersion) != EOK) {
245 LNN_LOGE(LNN_STATE, "strcpy_s deviceVersion failed.");
246 SoftBusFree(deviceVersion);
247 return SOFTBUS_MEM_ERR;
248 }
249 } else {
250 LNN_LOGE(LNN_STATE, "get invalid deviceVersion, deviceVersion= %{public}s", UNDEFINED_VALUE);
251 SoftBusFree(deviceVersion);
252 return SOFTBUS_ERR;
253 }
254 SoftBusFree(deviceVersion);
255 return SOFTBUS_OK;
256 }
257
GetWlanIpv4Addr(char * ip,uint32_t size)258 int32_t GetWlanIpv4Addr(char *ip, uint32_t size)
259 {
260 (void)ip;
261 (void)size;
262 return SOFTBUS_ERR;
263 }
264
GetDeviceSecurityLevel(int32_t * level)265 int32_t GetDeviceSecurityLevel(int32_t *level)
266 {
267 if (level == NULL) {
268 LNN_LOGE(LNN_STATE, "param error");
269 return SOFTBUS_INVALID_PARAM;
270 }
271 *level = GetIntParameter(OHOS_DEVICE_SECURITY_LEVEL, 0);
272 LNN_LOGI(LNN_STATE, "level=%{public}d", *level);
273 if (*level <= 0) {
274 LNN_LOGE(LNN_STATE, "getIntParamenter fail.");
275 return SOFTBUS_ERR;
276 }
277 return SOFTBUS_OK;
278 }