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