Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

72
src/libbtutil/bt_device.c Normal file
View file

@ -0,0 +1,72 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bt_device.h"
#include <bluetooth/bluetooth_types.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
BTDevice bt_device_init_with_address(BTDeviceAddress address, bool is_random) {
BTDeviceInternal device = {
.address = address,
.is_classic = false,
.is_random_address = is_random,
};
return device.opaque;
}
BTDeviceAddress bt_device_get_address(BTDevice device) {
return ((BTDeviceInternal *) &device)->address;
}
bool bt_device_address_equal(const BTDeviceAddress *addr1,
const BTDeviceAddress *addr2) {
if (addr1 == NULL || addr2 == NULL) {
return false;
}
return memcmp(addr1, addr2, sizeof(BTDeviceAddress)) == 0;
}
bool bt_device_address_is_invalid(const BTDeviceAddress *addr) {
if (!addr) {
return true;
}
BTDeviceAddress invalid = {};
return bt_device_address_equal(addr, &invalid);
}
bool bt_device_internal_equal(const BTDeviceInternal *device1_int,
const BTDeviceInternal *device2_int) {
if (device1_int == NULL || device2_int == NULL) {
return false;
}
return (device1_int->is_classic == device2_int->is_classic &&
device1_int->is_random_address == device2_int->is_random_address &&
bt_device_address_equal(&device1_int->address, &device2_int->address));
}
bool bt_device_equal(const BTDevice *device1, const BTDevice *device2) {
const BTDeviceInternal *device1_int = (const BTDeviceInternal *) device1;
const BTDeviceInternal *device2_int = (const BTDeviceInternal *) device2;
return bt_device_internal_equal(device1_int, device2_int);
}
bool bt_device_is_invalid(const BTDevice *device) {
const BTDevice invalid_device = BT_DEVICE_INVALID;
return bt_device_equal(device, &invalid_device);
}

28
src/libbtutil/bt_uuid.c Normal file
View file

@ -0,0 +1,28 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bt_uuid.h"
#include <bluetooth/bluetooth_types.h>
#include <stdint.h>
Uuid bt_uuid_expand_16bit(uint16_t uuid16) {
return bt_uuid_expand_32bit(uuid16);
}
Uuid bt_uuid_expand_32bit(uint32_t uuid32) {
return (const Uuid) { BT_UUID_EXPAND(uuid32) };
}

View file

@ -0,0 +1,60 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <bluetooth/bluetooth_types.h>
#include <stdbool.h>
//! Creates a BTDevice given its address.
//! @param address The address to use
//! @param is_random Specify true if the address is a random address, or false
//! if it is the real BD_ADDR of the device.
//! @return The created BTDevice
BTDevice bt_device_init_with_address(BTDeviceAddress address, bool is_random);
//! Gets the address of the device.
//! @param device The device for which to get the address.
//! @return The address of the device.
BTDeviceAddress bt_device_get_address(BTDevice device);
//! Compares two Bluetooth device addresses.
//! @return true if the addresses are equal, false if they are not or if one
//! or both addresses were NULL.
bool bt_device_address_equal(const BTDeviceAddress *addr1,
const BTDeviceAddress *addr2);
//! Compares the address with an all-zero (invalid) address.
//! @return true if the address is NULL or all-zeroes.
bool bt_device_address_is_invalid(const BTDeviceAddress *addr);
//! Compares two BTDeviceInternal structs.
//! @return true if the devices refer to the same device, false if they refer
//! to different devices or if one or both devices were NULL.
bool bt_device_internal_equal(const BTDeviceInternal *device1_int,
const BTDeviceInternal *device2_int);
//! Compares two Bluetooth devices.
//! @return true if the devices refer to the same device, false if they refer
//! to different devices or if one or both devices were NULL.
bool bt_device_equal(const BTDevice *device1, const BTDevice *device2);
//! Tests whether the device is a valid device.
//! This function is meant to be used together with APIs that return a BTDevice,
//! for example ble_service_get_device().
//! @return true if the device appears to be invalid, false if it does not.
bool bt_device_is_invalid(const BTDevice *device);

View file

@ -0,0 +1,33 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <util/uuid.h>
#include <stdint.h>
//! Expands a 16-bit UUID to its 128-bit equivalent, using the Bluetooth base
//! UUID (0000xxxx-0000-1000-8000-00805F9B34FB).
//! @param uuid16 The 16-bit value from which to derive the 128-bit UUID.
//! @return Uuid structure containing the final UUID.
Uuid bt_uuid_expand_16bit(uint16_t uuid16);
//! Expands a 32-bit UUID to its 128-bit equivalent, using the Bluetooth base
//! UUID (xxxxxxxx-0000-1000-8000-00805F9B34FB).
//! @param uuid32 The 32-bit value from which to derive the 128-bit UUID.
//! @return Uuid structure containing the final UUID.
Uuid bt_uuid_expand_32bit(uint32_t uuid32);

View file

@ -0,0 +1,26 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
typedef struct SMPairingInfo SMPairingInfo;
typedef struct SM128BitKey SM128BitKey;
bool sm_is_pairing_info_equal_identity(const SMPairingInfo *a, const SMPairingInfo *b);
bool sm_is_pairing_info_empty(const SMPairingInfo *p);
bool sm_is_pairing_info_irk_not_used(const SM128BitKey *irk_key);

48
src/libbtutil/sm_util.c Normal file
View file

@ -0,0 +1,48 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sm_util.h"
#include "bt_device.h"
#include <bluetooth/sm_types.h>
#include <stdbool.h>
#include <string.h>
// -------------------------------------------------------------------------------------------------
bool sm_is_pairing_info_equal_identity(const SMPairingInfo *a, const SMPairingInfo *b) {
return (a->is_remote_identity_info_valid &&
b->is_remote_identity_info_valid &&
bt_device_equal(&a->identity.opaque, &b->identity.opaque) &&
memcmp(&a->irk, &b->irk, sizeof(SMIdentityResolvingKey)) == 0);
}
// -------------------------------------------------------------------------------------------------
bool sm_is_pairing_info_empty(const SMPairingInfo *p) {
return (!p->is_local_encryption_info_valid &&
!p->is_remote_encryption_info_valid &&
!p->is_remote_identity_info_valid &&
!p->is_remote_signing_info_valid);
}
bool sm_is_pairing_info_irk_not_used(const SMIdentityResolvingKey *irk_key) {
// Per BLE spec v4.2 section 10.7 "Privacy Feature":
//
// "The local or peers IRK shall be an all-zero key, if not applicable for the particular
// device identity."
const SMIdentityResolvingKey empty_key = { };
return (memcmp(irk_key, &empty_key, sizeof(empty_key)) == 0);
}

24
src/libbtutil/wscript Normal file
View file

@ -0,0 +1,24 @@
import waftools
def build(bld):
sources = bld.path.ant_glob('**/*.c')
def build_libutil(target, env):
# Build the libbtutil directory using firmware environment
bld.stlib(source=sources,
target=target,
includes=['.', 'include/btutil'],
use=['pblibc_includes', 'libutil_includes', 'root_includes'],
env=env.derive())
bld(export_includes=['include'], name='libbtutil_includes')
if (bld.variant != 'test'):
build_libutil('libbtutil-cm0', bld.all_envs['cortex-m0'])
build_libutil('libbtutil', bld.env)
build_libutil('libbtutil-32bit', bld.all_envs['32bit'])
# vim:filetype=python