Line data Source code
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 = { { // 0000FFF6-0000-1000-8000-00805F9B34FB 32 : 0x00, 0x00, 0xFF, 0xF6, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 33 : 0xFB } }; 34 : 35 163 : inline static uint8_t HexDigitToInt(const char c) 36 : { 37 163 : return static_cast<uint8_t>(c >= '0' && c <= '9' ? c - '0' : tolower(c) - 'a' + 10); 38 : } 39 : 40 3 : bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo) 41 : { 42 3 : if ((idOne == nullptr) || (idTwo == nullptr)) 43 : { 44 0 : return false; 45 : } 46 3 : return (memcmp(idOne->bytes, idTwo->bytes, 16) == 0); 47 : } 48 : 49 : // Convert a string like "0000FFF6-0000-1000-8000-00805F9B34FB" to binary UUID 50 6 : bool StringToUUID(const char * str, ChipBleUUID & uuid) 51 : { 52 6 : constexpr size_t NUM_UUID_NIBBLES = sizeof(uuid.bytes) * 2; 53 6 : size_t nibbleId = 0; 54 : 55 177 : for (; *str; ++str) 56 : { 57 173 : if (*str == '-') // skip separators 58 8 : continue; 59 : 60 165 : if (!isxdigit(*str)) // invalid character! 61 1 : return false; 62 : 63 164 : if (nibbleId >= NUM_UUID_NIBBLES) // too long string! 64 1 : return false; 65 : 66 163 : uint8_t & byte = uuid.bytes[nibbleId / 2]; 67 163 : if (nibbleId % 2 == 0) 68 82 : byte = static_cast<uint8_t>(HexDigitToInt(*str) << 4); 69 : else 70 81 : byte = static_cast<uint8_t>(byte | HexDigitToInt(*str)); 71 : 72 163 : ++nibbleId; 73 : } 74 : 75 : // All bytes were initialized? 76 4 : return nibbleId == NUM_UUID_NIBBLES; 77 : } 78 : 79 : } /* namespace Ble */ 80 : } /* namespace chip */ 81 : 82 : #endif /* CONFIG_NETWORK_LAYER_BLE */