Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Darwin / UUIDHelperImpl.mm
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2015-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
19 #import "UUIDHelper.h"
20
21 @implementation UUIDHelper
22
23 + (CBUUID *)GetShortestServiceUUID:(const chip::Ble::ChipBleUUID *)svcId
24 {
25     // shorten the 16-byte UUID reported by BLE Layer to shortest possible, 2 or 4 bytes
26     // this is the BLE Service UUID Base. If a 16-byte service UUID partially matches with these 12 bytes,
27     // it can be shortened to 2 or 4 bytes.
28     static const uint8_t bleBaseUUID[12] = { 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
29     if (0 == memcmp(svcId->bytes + 4, bleBaseUUID, sizeof(bleBaseUUID))) {
30         // okay, let's try to shorten it
31         if ((0 == svcId->bytes[0]) && (0 == svcId->bytes[1])) {
32             // the highest 2 bytes are both 0, so we just need 2 bytes
33             return [CBUUID UUIDWithData:[NSData dataWithBytes:(svcId->bytes + 2) length:2]];
34         } else {
35             // we need to use 4 bytes
36             return [CBUUID UUIDWithData:[NSData dataWithBytes:svcId->bytes length:4]];
37         }
38     } else {
39         // it cannot be shortened as it doesn't match with the BLE Service UUID Base
40         return [CBUUID UUIDWithData:[NSData dataWithBytes:svcId->bytes length:16]];
41     }
42 }
43 @end