Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: speed up int_to_bluetooth_address C implementation #52

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/bluetooth_data_tools/_utils_impl.pyx
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import cython
# cython: language_level=3, c_string_type=str, c_string_encoding=ascii

from libc.stdlib cimport malloc


cdef extern from "stdint.h":
ctypedef unsigned long long uint64_t
from libc.stdint cimport uint64_t

cdef extern from "utils_wrapper.h":
void _uint64_to_bdaddr(uint64_t addr, char *bdaddr)

cdef char* uint64_to_bdaddr(uint64_t address):
cdef char* bdaddr = <char *> malloc((18) * sizeof(char))
if not bdaddr:
return NULL # malloc failed
_uint64_to_bdaddr(address, bdaddr)
return bdaddr

void _uint64_to_bdaddr(uint64_t address, char bdaddr[17]) nogil


def _int_to_bluetooth_address(addr: int) -> str:
return uint64_to_bdaddr(addr).decode('ascii')
cdef char bdaddr[17]
_uint64_to_bdaddr(<uint64_t>addr, bdaddr)
return <str>bdaddr[:17]
44 changes: 22 additions & 22 deletions src/bluetooth_data_tools/utils_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@


#include <stdint.h>
#include <stdio.h>

void _uint64_to_bdaddr(uint64_t address, char *bdaddr) {
/**
* Convert the given integer bluetooth address to its hexadecimal string representation.
* The buffer passed in must accept at least 17 bytes. It will NOT be null-terminated.
*/
void _uint64_to_bdaddr(uint64_t address, char bdaddr[17]) {
static const char hex_table[] = "0123456789ABCDEF";
*bdaddr++ = hex_table[(address >> 44) & 0x0F];
*bdaddr++ = hex_table[(address >> 40) & 0x0F];
*bdaddr++ = ':';
*bdaddr++ = hex_table[(address >> 36) & 0x0F];
*bdaddr++ = hex_table[(address >> 32) & 0x0F];
*bdaddr++ = ':';
*bdaddr++ = hex_table[(address >> 28) & 0x0F];
*bdaddr++ = hex_table[(address >> 24) & 0x0F];
*bdaddr++ = ':';
*bdaddr++ = hex_table[(address >> 20) & 0x0F];
*bdaddr++ = hex_table[(address >> 16) & 0x0F];
*bdaddr++ = ':';
*bdaddr++ = hex_table[(address >> 12) & 0x0F];
*bdaddr++ = hex_table[(address >> 8) & 0x0F];
*bdaddr++ = ':';
*bdaddr++ = hex_table[(address >> 4) & 0x0F];
*bdaddr++ = hex_table[address & 0x0F];
*bdaddr++ = '\0';
bdaddr[0] = hex_table[(address >> 44) & 0x0F];
bdaddr[1] = hex_table[(address >> 40) & 0x0F];
bdaddr[2] = ':';
bdaddr[3] = hex_table[(address >> 36) & 0x0F];
bdaddr[4] = hex_table[(address >> 32) & 0x0F];
bdaddr[5] = ':';
bdaddr[6] = hex_table[(address >> 28) & 0x0F];
bdaddr[7] = hex_table[(address >> 24) & 0x0F];
bdaddr[8] = ':';
bdaddr[9] = hex_table[(address >> 20) & 0x0F];
bdaddr[10] = hex_table[(address >> 16) & 0x0F];
bdaddr[11] = ':';
bdaddr[12] = hex_table[(address >> 12) & 0x0F];
bdaddr[13] = hex_table[(address >> 8) & 0x0F];
bdaddr[14] = ':';
bdaddr[15] = hex_table[(address >> 4) & 0x0F];
bdaddr[16] = hex_table[address & 0x0F];
}