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