<commit_before>/* uuid.c - Bluetooth UUID handling */

/*
 * Copyright (c) 2015 Intel Corporation
 *
 * 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 <nanokernel.h>
#include <toolchain.h>
#include <string.h>
#include <errno.h>

#include <bluetooth/uuid.h>

#if defined(CONFIG_BLUETOOTH_DEBUG)
#include <stdio.h>
#include <misc/byteorder.h>
#endif /* CONFIG_BLUETOOTH_DEBUG */

/* TODO: Decide wether to continue using BLE format or switch to RFC 4122 */
static const struct bt_uuid uuid128_base = {
	.type = BT_UUID_128,
	.u128 = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10,
		  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },

};

static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid *dst)
{
	switch (src->type) {
	case BT_UUID_16:
		*dst = uuid128_base;
		memcpy(&dst->u128[2], &src->u16, sizeof(src->u16));
		return;
	case BT_UUID_128:
		memcpy(dst, src, sizeof(*dst));
		return;
	}
}

static int uuid128_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2)
{
	struct bt_uuid uuid1, uuid2;

	uuid_to_uuid128(u1, &uuid1);
	uuid_to_uuid128(u2, &uuid2);

	return memcmp(uuid1.u128, uuid2.u128, sizeof(uuid1.u128));
}

int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2)
{
	/* Convert to 128 bit if types don't match */
	if (u1->type != u2->type)
		return uuid128_cmp(u1, u2);

	switch (u1->type) {
	case BT_UUID_16:
		return (int)u1->u16 - (int)u2->u16;
	case BT_UUID_128:<commit_msg>Bluetooth: UUID: Fix conversion to 128 bit format<commit_after>/* uuid.c - Bluetooth UUID handling */

/*
 * Copyright (c) 2015 Intel Corporation
 *
 * 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 <nanokernel.h>
#include <toolchain.h>
#include <string.h>
#include <errno.h>

#include <bluetooth/uuid.h>

#if defined(CONFIG_BLUETOOTH_DEBUG)
#include <stdio.h>
#include <misc/byteorder.h>
#endif /* CONFIG_BLUETOOTH_DEBUG */

#define UUID_16_BASE_OFFSET 12

/* TODO: Decide wether to continue using BLE format or switch to RFC 4122 */
static const struct bt_uuid uuid128_base = {
	.type = BT_UUID_128,
	.u128 = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10,
		  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },

};

static inline void cpu_to_uuid128(void *dst, const void *src, int len)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	int i;

	for (i = 0; i < len; i++) {
		((uint8_t *)dst)[i] = ((uint8_t *)src)[(len - 1) - i];
	}
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
	memcpy(dst, src, len);
#else
#error "Unknown byte order"
#endif
}

static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid *dst)
{
	switch (src->type) {
	case BT_UUID_16:
		*dst = uuid128_base;
		cpu_to_uuid128(&dst->u128[UUID_16_BASE_OFFSET], &src->u16,
			       sizeof(src->u16));
		return;
	case BT_UUID_128:
		memcpy(dst, src, sizeof(*dst));
		return;
	}
}

static int uuid128_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2)
{
	struct bt_uuid uuid1, uuid2;

	uuid_to_uuid128(u1, &uuid1);
	uuid_to_uuid128(u2, &uuid2);

	return memcmp(uuid1.u128, uuid2.u128, sizeof(uuid1.u128));
}

int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2)
{
	/* Convert to 128 bit if types don't match */
	if (u1->type != u2->type)
		return uuid128_cmp(u1, u2);

	switch (u1->type) {
	case BT_UUID_16:
		return (int)u1->u16 - (int)u2->u16;
	case BT_UUID_128:
{'fraction_non_alphanumeric': 0.12245841035120147, 'fraction_numerical': 0.06746765249537892, 'mean_word_length': 4.228260869565218, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2056630', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1642, 'n_words': 492}