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