• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "lnn_device_info.h"
17 
18 #include <stddef.h>
19 #include <string.h>
20 
21 #include <securec.h>
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24 #include "softbus_log.h"
25 
26 #define DEVICE_TYPE_MAX_LENGTH 3
27 #define LEFT_SHIFT_DEVICE_TYPE_LENGTH  (DEVICE_TYPE_MAX_LENGTH * 4)
28 #define HEX_OF_BINARY_BITS 4
29 #define LAST_FOUR_BINARY_DIGITS 16
30 #define DIVIDE_NUMBER_AND_LETTERS 10
31 #define ONE_BIT_MAX_HEX 15
32 
33 typedef struct {
34     char *type;
35     uint16_t id;
36 } TypeToId;
37 
38 static TypeToId g_typeToIdMap[] = {
39     {TYPE_UNKNOWN, TYPE_UNKNOW_ID},
40     {TYPE_PHONE, TYPE_PHONE_ID},
41     {TYPE_PAD, TYPE_PAD_ID},
42     {TYPE_TV, TYPE_TV_ID},
43     {TYPE_CAR, TYPE_CAR_ID},
44     {TYPE_WATCH, TYPE_WATCH_ID},
45     {TYPE_IPCAMERA, TYPE_IPCAMERA_ID},
46     {TYPE_PC, TYPE_PC_ID},
47     {TYPE_SMART_DISPLAY, TYPE_SMART_DISPLAY_ID},
48 };
49 
50 static char g_stringTypeId[DEVICE_TYPE_MAX_LENGTH + 1] = {0};
51 
LnnGetDeviceName(const DeviceBasicInfo * info)52 NO_SANITIZE("cfi") const char *LnnGetDeviceName(const DeviceBasicInfo *info)
53 {
54     if (info == NULL) {
55         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnGetDeviceName para error.");
56         return NULL;
57     }
58     return info->deviceName;
59 }
60 
LnnSetDeviceName(DeviceBasicInfo * info,const char * name)61 NO_SANITIZE("cfi") int32_t LnnSetDeviceName(DeviceBasicInfo *info, const char *name)
62 {
63     if (info == NULL || name == NULL || strlen(name) > DEVICE_NAME_BUF_LEN - 1) {
64         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnSetDeviceName para error.");
65         return SOFTBUS_INVALID_PARAM;
66     }
67     if (strncpy_s(info->deviceName, DEVICE_NAME_BUF_LEN, name, strlen(name)) != EOK) {
68         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s fail:strncpy_s fail!", __func__);
69         return SOFTBUS_MEM_ERR;
70     }
71     return SOFTBUS_OK;
72 }
73 
LnnGetDeviceTypeId(const DeviceBasicInfo * info,uint16_t * typeId)74 NO_SANITIZE("cfi") int32_t LnnGetDeviceTypeId(const DeviceBasicInfo *info, uint16_t *typeId)
75 {
76     if (info == NULL || typeId == NULL) {
77         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnGetDeviceTypeId para error.");
78         return SOFTBUS_INVALID_PARAM;
79     }
80     *typeId = info->deviceTypeId;
81     return SOFTBUS_OK;
82 }
83 
ConvertStringToInt(const char * deviceType,uint16_t * typeId)84 static uint16_t ConvertStringToInt(const char *deviceType, uint16_t *typeId)
85 {
86     *typeId = 0;
87     uint16_t tmp;
88     uint32_t len = strlen(deviceType);
89     for (uint32_t i = 0; i < len; i++) {
90         if ((*(deviceType + i) <= '9') && (*(deviceType + i) >= '0')) {
91             *typeId |= (uint16_t)(*(deviceType + i) - '0');
92             *typeId = (*typeId << HEX_OF_BINARY_BITS);
93             continue;
94         } else if ((*(deviceType + i) <= 'F') && (*(deviceType + i) >= 'A')) {
95             tmp = (*(deviceType + i) - 'A' + DIVIDE_NUMBER_AND_LETTERS);
96             *typeId |= tmp;
97             *typeId = (*typeId << HEX_OF_BINARY_BITS);
98             continue;
99         } else if ((*(deviceType + i) <= 'f') && (*(deviceType + i) >= 'a')) {
100             tmp = (*(deviceType + i) - 'a' + DIVIDE_NUMBER_AND_LETTERS);
101             *typeId |= tmp;
102             *typeId = (*typeId << HEX_OF_BINARY_BITS);
103             continue;
104         } else {
105             *typeId = TYPE_UNKNOW_ID;
106             return *typeId;
107         }
108     }
109     *typeId = (*typeId >> HEX_OF_BINARY_BITS);
110     return *typeId;
111 }
112 
InterceptTypeId(uint16_t typeId,uint32_t i)113 static char InterceptTypeId(uint16_t typeId, uint32_t i)
114 {
115     return (char)((typeId >> (HEX_OF_BINARY_BITS * i)) % LAST_FOUR_BINARY_DIGITS);
116 }
117 
ConvertIntToHexString(uint16_t typeId)118 static char *ConvertIntToHexString(uint16_t typeId)
119 {
120     uint32_t j = 0;
121     for (int32_t i = DEVICE_TYPE_MAX_LENGTH - 1; i >= 0; i--) {
122         if (InterceptTypeId(typeId, i) == 0) {
123             g_stringTypeId[j] = '0';
124         } else if (InterceptTypeId(typeId, i) < DIVIDE_NUMBER_AND_LETTERS) {
125             g_stringTypeId[j] = InterceptTypeId(typeId, i) + '0';
126         } else if (InterceptTypeId(typeId, i) >= DIVIDE_NUMBER_AND_LETTERS) {
127             g_stringTypeId[j] = InterceptTypeId(typeId, i) - DIVIDE_NUMBER_AND_LETTERS + 'A';
128         }
129         j++;
130     }
131     g_stringTypeId[j] = '\0';
132     return g_stringTypeId;
133 }
134 
LnnConvertDeviceTypeToId(const char * deviceType,uint16_t * typeId)135 NO_SANITIZE("cfi") int32_t LnnConvertDeviceTypeToId(const char *deviceType, uint16_t *typeId)
136 {
137     int mstRet;
138     if (deviceType == NULL || typeId == NULL) {
139         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnConvertDeviceTypeToId para error.");
140         return SOFTBUS_INVALID_PARAM;
141     }
142     uint32_t count = sizeof(g_typeToIdMap) / sizeof(TypeToId);
143     for (uint32_t i = 0; i < count; i++) {
144         if (strcmp(g_typeToIdMap[i].type, deviceType) == 0) {
145             *typeId = g_typeToIdMap[i].id;
146             return SOFTBUS_OK;
147         }
148     }
149     if (strlen(deviceType) <= DEVICE_TYPE_MAX_LENGTH) {
150         mstRet = memset_s(g_stringTypeId, sizeof(g_stringTypeId), 0, DEVICE_TYPE_MAX_LENGTH);
151         if (mstRet != EOK) {
152             *typeId = TYPE_UNKNOW_ID;
153             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnConvertDeviceTypeToId memset_s fail.");
154             return SOFTBUS_ERR;
155         }
156         *typeId = ConvertStringToInt(deviceType, typeId);
157         if (*typeId != TYPE_UNKNOW_ID) {
158             return SOFTBUS_OK;
159         }
160         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "convert string to int fail.");
161     }
162     *typeId = TYPE_UNKNOW_ID;
163     return SOFTBUS_ERR;
164 }
165 
LnnConvertIdToDeviceType(uint16_t typeId)166 NO_SANITIZE("cfi") char *LnnConvertIdToDeviceType(uint16_t typeId)
167 {
168     uint32_t count = sizeof(g_typeToIdMap) / sizeof(TypeToId);
169     for (uint32_t i = 0; i < count; i++) {
170         if (g_typeToIdMap[i].id == typeId) {
171             return g_typeToIdMap[i].type;
172         }
173     }
174     if ((typeId <= ONE_BIT_MAX_HEX << LEFT_SHIFT_DEVICE_TYPE_LENGTH) && (typeId > 0)) {
175         return ConvertIntToHexString(typeId);
176     }
177     SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "typeId not exist");
178     return NULL;
179 }
180