• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <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_error_code.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 PRODUCT_ID                 "const.distributed_collaboration.productId" /* Read product id by the string */
41 #define MODEL_NAME                 "const.product.model" /* Read product model name by the string */
42 #define VERSION_SDK                "ro.build.version.sdk"
43 #define UNDEFINED_VALUE            "undefined"
44 #define OHOS_DEVICE_SECURITY_LEVEL "const.security.device_security_level"
45 #define OHOS_TYPE_UNKNOWN          (-1)
46 #define API_VERSION_LEN            10
47 #define VERSION_SDK_LEN            10
48 #define SN_LEN                     32
49 
50 typedef struct {
51     const char *inBuf;
52     const char *outBuf;
53 } TypeInfo;
54 
55 static TypeInfo g_typeConvertMap[] = {
56     {GET_TYPE_UNKNOWN, TYPE_UNKNOWN},
57     {GET_TYPE_PHONE, TYPE_PHONE},
58     {GET_TYPE_PAD, TYPE_PAD},
59     {GET_TYPE_TV, TYPE_TV},
60     {GET_TYPE_CAR, TYPE_CAR},
61     {GET_TYPE_WATCH, TYPE_WATCH},
62     {GET_TYPE_IPCAMERA, TYPE_IPCAMERA},
63     {GET_TYPE_2IN1, TYPE_2IN1},
64     {GET_TYPE_GLASS, TYPE_GLASS},
65 };
66 
SoftBusGetBleMacAddr(char * macStr,uint32_t len)67 static int32_t SoftBusGetBleMacAddr(char *macStr, uint32_t len)
68 {
69     int32_t bleMacRefreshSwitch;
70     if (SoftbusGetConfig(SOFTBUS_INT_BLE_MAC_AUTO_REFRESH_SWITCH,
71         (unsigned char *)(&bleMacRefreshSwitch), sizeof(bleMacRefreshSwitch)) != SOFTBUS_OK) {
72         LNN_LOGE(LNN_STATE, "get ble mac refresh switch from config file fail");
73         return SOFTBUS_GET_CONFIG_VAL_ERR;
74     }
75     /* ble mac not periodic refresh, return error if get ble mac fail */
76     if (bleMacRefreshSwitch == 0) {
77         int32_t ret;
78         SoftBusBtAddr mac = {0};
79 
80         if (len != BT_MAC_LEN) {
81             return SOFTBUS_INVALID_PARAM;
82         }
83         ret = SoftBusGetBtMacAddr(&mac);
84         if (ret != SOFTBUS_OK) {
85             LNN_LOGE(LNN_STATE, "get ble mac addr fail");
86             return ret;
87         }
88         ret = ConvertReverseBtMacToStr(macStr, len, mac.addr, sizeof(mac.addr));
89         if (ret != SOFTBUS_OK) {
90             LNN_LOGE(LNN_STATE, "convert bt mac to str fail");
91             return ret;
92         }
93         return SOFTBUS_OK;
94     }
95     /* ble mac periodic refresh, return SOFTBUS_OK */
96     (void)memset_s(macStr, len, 0, len);
97     return SOFTBUS_OK;
98 }
99 
SoftBusConvertDeviceType(const char * inBuf,char * outBuf,uint32_t outLen)100 static int32_t SoftBusConvertDeviceType(const char *inBuf, char *outBuf, uint32_t outLen)
101 {
102     uint32_t id;
103     for (id = 0; id < sizeof(g_typeConvertMap) / sizeof(TypeInfo); id++) {
104         if (strcmp(g_typeConvertMap[id].inBuf, inBuf) == EOK) {
105             if (strcpy_s(outBuf, outLen, g_typeConvertMap[id].outBuf) != EOK) {
106                 LNN_LOGE(LNN_STATE, "strcpy_s fail");
107                 return SOFTBUS_STRCPY_ERR;
108             }
109             return SOFTBUS_OK;
110         }
111     }
112     return SOFTBUS_NOT_FIND;
113 }
114 
SoftBusGetOsType(void)115 static int32_t SoftBusGetOsType(void)
116 {
117     char apiVersion[API_VERSION_LEN + 1];
118     (void)memset_s(apiVersion, API_VERSION_LEN + 1, 0, API_VERSION_LEN + 1);
119     GetParameter(OHOS_API_VERSION, UNDEFINED_VALUE, apiVersion, API_VERSION_LEN);
120     char bootSN[SN_LEN + 1];
121     (void)memset_s(bootSN, SN_LEN + 1, 0, SN_LEN + 1);
122     GetParameter(OHOS_BOOT_SN, UNDEFINED_VALUE, bootSN, SN_LEN);
123     char osVersion[OS_VERSION_BUF_LEN];
124     (void)memset_s(osVersion, OS_VERSION_BUF_LEN, 0, OS_VERSION_BUF_LEN);
125     GetParameter(OS_VERSION, UNDEFINED_VALUE, osVersion, OS_VERSION_BUF_LEN);
126     if (strcmp(apiVersion, UNDEFINED_VALUE) != 0 || strcmp(bootSN, UNDEFINED_VALUE) != 0 ||
127         strcmp(osVersion, UNDEFINED_VALUE) != 0) {
128         char *anonyBootSN = NULL;
129         Anonymize(bootSN, &anonyBootSN);
130         LNN_LOGI(LNN_STATE, "apiVersion: %{public}s bootSN: %{public}s osVersion: %{public}s",
131             apiVersion, AnonymizeWrapper(anonyBootSN), osVersion);
132         AnonymizeFree(anonyBootSN);
133         return OH_OS_TYPE;
134     }
135     char versionSDK[VERSION_SDK_LEN + 1];
136     (void)memset_s(versionSDK, VERSION_SDK_LEN + 1, 0, VERSION_SDK_LEN + 1);
137     GetParameter(VERSION_SDK, UNDEFINED_VALUE, versionSDK, VERSION_SDK_LEN);
138     if (strcmp(versionSDK, UNDEFINED_VALUE) != 0) {
139         LNN_LOGI(LNN_STATE, "versionSDK: %{public}s", versionSDK);
140         return HO_OS_TYPE;
141     }
142     LNN_LOGE(LNN_STATE, "GetOsType fail!");
143     return OHOS_TYPE_UNKNOWN;
144 }
145 
GetCommonDevInfo(CommonDeviceKey key,char * value,uint32_t len)146 int32_t GetCommonDevInfo(CommonDeviceKey key, char *value, uint32_t len)
147 {
148     if (value == NULL) {
149         LNN_LOGE(LNN_STATE, "para error");
150         return SOFTBUS_INVALID_PARAM;
151     }
152     char localUdid[UDID_BUF_LEN] = {0};
153     const char *devType = NULL;
154     switch (key) {
155         case COMM_DEVICE_KEY_DEVNAME:
156             /* set real value when device name init */
157             break;
158         case COMM_DEVICE_KEY_UDID:
159             if (GetDevUdid(localUdid, UDID_BUF_LEN) != 0) {
160                 LNN_LOGE(LNN_STATE, "GetDevUdid failed");
161                 return SOFTBUS_NETWORK_GET_DEVICE_INFO_ERR;
162             }
163             if (strncpy_s(value, len, localUdid, UDID_BUF_LEN) != EOK) {
164                 return SOFTBUS_STRCPY_ERR;
165             }
166             break;
167         case COMM_DEVICE_KEY_DEVTYPE:
168             devType = GetDeviceType();
169             LNN_LOGI(LNN_STATE, "get device from GetDeviceType, GetDeviceType=%{public}s", devType);
170             if (devType != NULL) {
171                 char softBusDevType[DEVICE_TYPE_BUF_LEN] = {0};
172                 int32_t ret = SoftBusConvertDeviceType(devType, softBusDevType, DEVICE_TYPE_BUF_LEN);
173                 if (ret != SOFTBUS_OK) {
174                     LNN_LOGE(LNN_STATE, "convert device type fail");
175                     return ret;
176                 }
177                 if (strcpy_s(value, len, softBusDevType) != EOK) {
178                     return SOFTBUS_STRCPY_ERR;
179                 }
180             } else {
181                 LNN_LOGE(LNN_STATE, "GetDeviceType failed");
182                 return SOFTBUS_NETWORK_GET_DEVICE_INFO_ERR;
183             }
184             break;
185         case COMM_DEVICE_KEY_BLE_MAC:
186             if (SoftBusGetBleMacAddr(value, len) != SOFTBUS_OK) {
187                 LNN_LOGE(LNN_STATE, "get ble mac addr failed!");
188                 return SOFTBUS_NETWORK_GET_DEVICE_INFO_ERR;
189             }
190             break;
191         default:
192             break;
193     }
194     return SOFTBUS_OK;
195 }
196 
GetCommonOsType(int32_t * value)197 int32_t GetCommonOsType(int32_t *value)
198 {
199     int32_t ret = SoftBusGetOsType();
200     *value = ret;
201     if (*value == OHOS_TYPE_UNKNOWN) {
202         LNN_LOGE(LNN_STATE, "get invalid os type, osType=%{public}d", *value);
203         return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
204     }
205     return SOFTBUS_OK;
206 }
207 
GetCommonOsVersion(char * value,uint32_t len)208 int32_t GetCommonOsVersion(char *value, uint32_t len)
209 {
210     if (value == NULL) {
211         LNN_LOGE(LNN_STATE, "para error");
212         return SOFTBUS_INVALID_PARAM;
213     }
214     char *osVersion = (char *)SoftBusCalloc(OS_VERSION_BUF_LEN);
215     if (osVersion == NULL) {
216         LNN_LOGE(LNN_STATE, "calloc osVersion failed!");
217         return SOFTBUS_MEM_ERR;
218     }
219     GetParameter(OS_VERSION, UNDEFINED_VALUE, osVersion, OS_VERSION_BUF_LEN);
220     if (strcmp(osVersion, UNDEFINED_VALUE) != 0) {
221         if (strcpy_s(value, len, osVersion) != EOK) {
222             LNN_LOGE(LNN_STATE, "strcpy_s osVersion failed.");
223             SoftBusFree(osVersion);
224             return SOFTBUS_MEM_ERR;
225         }
226     } else {
227         LNN_LOGE(LNN_STATE, "get invalid osVersion, osVersion=%{public}s", UNDEFINED_VALUE);
228         SoftBusFree(osVersion);
229         return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
230     }
231     SoftBusFree(osVersion);
232     return SOFTBUS_OK;
233 }
234 
GetCommonDeviceVersion(char * value,uint32_t len)235 int32_t GetCommonDeviceVersion(char *value, uint32_t len)
236 {
237     if (value == NULL) {
238         LNN_LOGE(LNN_STATE, "para error");
239         return SOFTBUS_INVALID_PARAM;
240     }
241     char *deviceVersion = (char *)SoftBusCalloc(DEVICE_VERSION_SIZE_MAX);
242     if (deviceVersion == NULL) {
243         LNN_LOGE(LNN_STATE, "calloc deviceVersion failed!");
244         return SOFTBUS_MEM_ERR;
245     }
246     GetParameter(DEVICE_VERSION, UNDEFINED_VALUE, deviceVersion, DEVICE_VERSION_SIZE_MAX);
247     if (strcmp(deviceVersion, UNDEFINED_VALUE) != 0) {
248         if (strcpy_s(value, len, deviceVersion) != EOK) {
249             LNN_LOGE(LNN_STATE, "strcpy_s deviceVersion failed.");
250             SoftBusFree(deviceVersion);
251             return SOFTBUS_MEM_ERR;
252         }
253     } else {
254         LNN_LOGE(LNN_STATE, "get invalid deviceVersion, deviceVersion=%{public}s", UNDEFINED_VALUE);
255         SoftBusFree(deviceVersion);
256         return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
257     }
258     SoftBusFree(deviceVersion);
259     return SOFTBUS_OK;
260 }
261 
GetCommonDeviceProductId(char * value,uint32_t len)262 int32_t GetCommonDeviceProductId(char *value, uint32_t len)
263 {
264     if (value == NULL) {
265         LNN_LOGE(LNN_STATE, "para error");
266         return SOFTBUS_INVALID_PARAM;
267     }
268     char *productId = (char *)SoftBusCalloc(PRODUCT_ID_SIZE_MAX);
269     if (productId == NULL) {
270         LNN_LOGE(LNN_STATE, "calloc productId failed!");
271         return SOFTBUS_MEM_ERR;
272     }
273     GetParameter(PRODUCT_ID, UNDEFINED_VALUE, productId, PRODUCT_ID_SIZE_MAX);
274     if (strcmp(productId, UNDEFINED_VALUE) != 0) {
275         if (strcpy_s(value, len, productId) != EOK) {
276             LNN_LOGE(LNN_STATE, "strcpy_s productId failed.");
277             SoftBusFree(productId);
278             return SOFTBUS_MEM_ERR;
279         }
280     } else {
281         LNN_LOGE(LNN_STATE, "get invalid productId, productId=%{public}s", UNDEFINED_VALUE);
282         SoftBusFree(productId);
283         return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
284     }
285     char *anonyProductId = NULL;
286     Anonymize(productId, &anonyProductId);
287     LNN_LOGI(LNN_STATE, "get productId=%{public}s", AnonymizeWrapper(anonyProductId));
288     AnonymizeFree(anonyProductId);
289     SoftBusFree(productId);
290     return SOFTBUS_OK;
291 }
292 
GetCommonDeviceModelName(char * value,uint32_t len)293 int32_t GetCommonDeviceModelName(char *value, uint32_t len)
294 {
295     if (value == NULL) {
296         LNN_LOGE(LNN_STATE, "para error");
297         return SOFTBUS_INVALID_PARAM;
298     }
299     char *modelName = (char *)SoftBusCalloc(MODEL_NAME_SIZE_MAX);
300     if (modelName == NULL) {
301         LNN_LOGE(LNN_STATE, "calloc modelName failed!");
302         return SOFTBUS_MEM_ERR;
303     }
304     GetParameter(MODEL_NAME, UNDEFINED_VALUE, modelName, MODEL_NAME_SIZE_MAX);
305     if (strcmp(modelName, UNDEFINED_VALUE) != 0) {
306         if (strcpy_s(value, len, modelName) != EOK) {
307             LNN_LOGE(LNN_STATE, "strcpy_s modelName failed.");
308             SoftBusFree(modelName);
309             return SOFTBUS_MEM_ERR;
310         }
311     } else {
312         LNN_LOGE(LNN_STATE, "get invalid modelName, modelName=%{public}s", UNDEFINED_VALUE);
313         SoftBusFree(modelName);
314         return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
315     }
316     char *anonyModelName = NULL;
317     Anonymize(modelName, &anonyModelName);
318     LNN_LOGI(LNN_STATE, "get modelName=%{public}s", AnonymizeWrapper(anonyModelName));
319     AnonymizeFree(anonyModelName);
320     SoftBusFree(modelName);
321     return SOFTBUS_OK;
322 }
323 
GetWlanIpv4Addr(char * ip,uint32_t size)324 int32_t GetWlanIpv4Addr(char *ip, uint32_t size)
325 {
326     (void)ip;
327     (void)size;
328     return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
329 }
330 
GetDeviceSecurityLevel(int32_t * level)331 int32_t GetDeviceSecurityLevel(int32_t *level)
332 {
333     if (level == NULL) {
334         LNN_LOGE(LNN_STATE, "param error");
335         return SOFTBUS_INVALID_PARAM;
336     }
337     *level = GetIntParameter(OHOS_DEVICE_SECURITY_LEVEL, 0);
338     LNN_LOGI(LNN_STATE, "level=%{public}d", *level);
339     if (*level <= 0) {
340         LNN_LOGE(LNN_STATE, "getIntParamenter fail.");
341         return SOFTBUS_NETWORK_GET_INVALID_DEVICE_INFO;
342     }
343     return SOFTBUS_OK;
344 }
345