Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / ble / BleUUID.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2014-2017 Nest Labs, Inc.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18 #include <ble/BleConfig.h>
19
20 #if CONFIG_NETWORK_LAYER_BLE
21
22 #include <cctype>
23 #include <cstdint>
24 #include <cstring>
25
26 #include "BleUUID.h"
27
28 namespace chip {
29 namespace Ble {
30
31 const ChipBleUUID CHIP_BLE_SVC_ID = { { // 0000FEAF-0000-1000-8000-00805F9B34FB
32                                         0x00, 0x00, 0xFE, 0xAF, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34,
33                                         0xFB } };
34
35 inline static uint8_t HexDigitToInt(const char c)
36 {
37     return static_cast<uint8_t>(c >= '0' && c <= '9' ? c - '0' : tolower(c) - 'a' + 10);
38 }
39
40 bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo)
41 {
42     if ((idOne == nullptr) || (idTwo == nullptr))
43     {
44         return false;
45     }
46     return (memcmp(idOne->bytes, idTwo->bytes, 16) == 0);
47 }
48
49 // Convert a string like "0000FEAF-0000-1000-8000-00805F9B34FB" to binary UUID
50 bool StringToUUID(const char * str, ChipBleUUID & uuid)
51 {
52     constexpr size_t NUM_UUID_NIBBLES = sizeof(uuid.bytes) * 2;
53     size_t nibbleId                   = 0;
54
55     for (; *str; ++str)
56     {
57         if (*str == '-') // skip separators
58             continue;
59
60         if (!isxdigit(*str)) // invalid character!
61             return false;
62
63         if (nibbleId >= NUM_UUID_NIBBLES) // too long string!
64             return false;
65
66         uint8_t & byte = uuid.bytes[nibbleId / 2];
67         if (nibbleId % 2 == 0)
68             byte = static_cast<uint8_t>(HexDigitToInt(*str) << 4);
69         else
70             byte = static_cast<uint8_t>(byte | HexDigitToInt(*str));
71
72         ++nibbleId;
73     }
74
75     // All bytes were initialized?
76     return nibbleId == NUM_UUID_NIBBLES;
77 }
78
79 } /* namespace Ble */
80 } /* namespace chip */
81
82 #endif /* CONFIG_NETWORK_LAYER_BLE */