Skip to content

Instantly share code, notes, and snippets.

@bklimt
Created June 14, 2015 01:29
Show Gist options
  • Save bklimt/9f7b290d0a664b7a3567 to your computer and use it in GitHub Desktop.
Save bklimt/9f7b290d0a664b7a3567 to your computer and use it in GitHub Desktop.
How to make the bluez bluetooth stack work with hex pin codes.
/*
* 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