From: David Herrmann <dh.herrmann@googlemail.com>
To: linux-bluetooth@vger.kernel.org
Cc: johan.hedberg@gmail.com, hadess@hadess.net, dforsi@gmail.com,
David Herrmann <dh.herrmann@googlemail.com>
Subject: [PATCH 3/4] Parse pin codes starting with '$' as hexadecimal encoded strings
Date: Sun, 10 Apr 2011 19:11:16 +0200 [thread overview]
Message-ID: <1302455477-27664-4-git-send-email-dh.herrmann@googlemail.com> (raw)
In-Reply-To: <1302455477-27664-1-git-send-email-dh.herrmann@googlemail.com>
If a pin code is retrieved from an agent and the first character is
a dollar sign '$', then the pin is decoded as following:
- The first character (dollar sign) is stripped from the pin
- The rest is parsed as hexadecimal numbers, where each two characters
will be converted into a one byte integer. If an odd number of
characters follows, then the last character is stripped. Empty
pins are valid. Parser is case insensitive.
Pins not starting with '$' are parsed as usual.
For instance:
pin: $0A3e005067
is decoded into a 5 byte pin:
decoded: 0x0a 0x3e 0x00 0x50 0x67
---
src/event.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 42 insertions(+), 1 deletions(-)
diff --git a/src/event.c b/src/event.c
index 248fb78..f2fc688 100644
--- a/src/event.c
+++ b/src/event.c
@@ -101,12 +101,52 @@ static gboolean get_adapter_and_device(bdaddr_t *src, bdaddr_t *dst,
*
*****************************************************************/
+static char hex2dec(char c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ else if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ else if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ else
+ return 0;
+}
+
+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)
+ out[i] = hex2dec(pin[i * 2]) * 16 + hex2dec(pin[i * 2 + 1]);
+ return i;
+}
+
+static size_t decode_pin(const char *pin, char *out)
+{
+ size_t len;
+
+ if (!pin) {
+ return 0;
+ }
+ else 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 sba, dba;
int err;
+ size_t len;
+ char rawpin[16];
device_get_address(device, &dba);
@@ -117,7 +157,8 @@ static void pincode_cb(struct agent *agent, DBusError *derr,
return;
}
- err = btd_adapter_pincode_reply(adapter, &dba, pincode, pincode ? strlen(pincode) : 0);
+ len = decode_pin(pincode, rawpin);
+ err = btd_adapter_pincode_reply(adapter, &dba, rawpin, len);
if (err < 0)
goto fail;
--
1.7.4.4
next prev parent reply other threads:[~2011-04-10 17:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-10 17:11 [RFC][PATCH 0/4] Allow hexadecimal encoded pins David Herrmann
2011-04-10 17:11 ` [PATCH 1/4] Add length argument to hci pincode reply David Herrmann
2011-05-05 17:34 ` Gustavo F. Padovan
2011-04-10 17:11 ` [PATCH 2/4] Make adapter API accept binary pincodes David Herrmann
2011-05-05 17:38 ` Gustavo F. Padovan
2011-04-10 17:11 ` David Herrmann [this message]
2011-04-10 17:11 ` [PATCH 4/4] Remove 16 byte limit for PIN codes returned by agents David Herrmann
2011-05-02 23:32 ` [RFC][PATCH 0/4] Allow hexadecimal encoded pins Bastien Nocera
2011-05-03 11:51 ` David Herrmann
2011-05-03 12:27 ` Daniele Forsi
2011-05-03 18:28 ` David Herrmann
2011-05-06 1:37 ` Bastien Nocera
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1302455477-27664-4-git-send-email-dh.herrmann@googlemail.com \
--to=dh.herrmann@googlemail.com \
--cc=dforsi@gmail.com \
--cc=hadess@hadess.net \
--cc=johan.hedberg@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).