Created
June 14, 2015 01:29
-
-
Save bklimt/9f7b290d0a664b7a3567 to your computer and use it in GitHub Desktop.
How to make the bluez bluetooth stack work with hex pin codes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* An excerpt from https://github.com/pcacjr/bluez/blob/c70a4afe4328053f462d8e30a0a787245aa7b948/src/event.c#L132 | |
*/ | |
static size_t decode_hex(const char *pin, char *out) | |
{ | |
size_t i; | |
for (i = 0; i < 16 && pin[i * 2] && pin[i * 2 + 1]; i++) | |
sscanf(&pin[i * 2], "%02hhX", &out[i]); | |
return i; | |
} | |
static size_t decode_pin(const char *pin, char *out) | |
{ | |
size_t len; | |
if (!pin) | |
return 0; | |
if (pin[0] == '$') { | |
len = decode_hex(&pin[1], out); | |
} else { | |
len = strnlen(pin, 16); | |
memcpy(out, pin, len); | |
} | |
return len; | |
} | |
static void pincode_cb(struct agent *agent, DBusError *derr, | |
const char *pincode, struct btd_device *device) | |
{ | |
struct btd_adapter *adapter = device_get_adapter(device); | |
bdaddr_t dba; | |
int err; | |
size_t len; | |
char rawpin[16]; | |
device_get_address(device, &dba); | |
len = decode_pin(pincode, rawpin); | |
if (derr || !len) { | |
err = btd_adapter_pincode_reply(adapter, &dba, NULL, 0); | |
if (err < 0) | |
goto fail; | |
return; | |
} | |
err = btd_adapter_pincode_reply(adapter, &dba, rawpin, len); | |
if (err < 0) | |
goto fail; | |
return; | |
fail: | |
error("Sending PIN code reply failed: %s (%d)", strerror(-err), -err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment