From 55b6549999248ec28e2faf394706153dc03191fd Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 15 Dec 2017 11:25:23 +0000 Subject: [PATCH 1/3] Implement getSerialNumber (based on BT MAC) --- libs/core/linux.cpp | 4 -- libs/core/pxt.json | 1 + libs/core/serialnumber.cpp | 78 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 libs/core/serialnumber.cpp diff --git a/libs/core/linux.cpp b/libs/core/linux.cpp index 5e7bcfa4..e3f16705 100644 --- a/libs/core/linux.cpp +++ b/libs/core/linux.cpp @@ -221,10 +221,6 @@ int current_time_ms() { return currTime() - startTime; } -int getSerialNumber() { - return 42; // TODO -} - void disposeThread(Thread *t) { if (allThreads == t) { allThreads = t->next; diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 39c6f5e7..716d8e58 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -10,6 +10,7 @@ "linux.cpp", "mmap.cpp", "control.cpp", + "serialnumber.cpp", "buttons.ts", "png.cpp", "screen.cpp", diff --git a/libs/core/serialnumber.cpp b/libs/core/serialnumber.cpp new file mode 100644 index 00000000..7efde89f --- /dev/null +++ b/libs/core/serialnumber.cpp @@ -0,0 +1,78 @@ +#include "pxt.h" + +#include +#include +#include + +#define BTPROTO_HCI 1 +#define HCIGETDEVLIST _IOR('H', 210, int) +#define HCIGETDEVINFO _IOR('H', 211, int) + +struct hci_dev_info { + uint16_t dev_id; + char name[8]; + uint8_t bdaddr[6]; + uint32_t padding[32]; +}; + +struct hci_dev_req { + uint16_t dev_id; + uint32_t dev_opt; +}; + +struct hci_dev_list_req { + uint16_t dev_num; + hci_dev_req dev_req[2]; +}; + + +static uint32_t bt_addr() { + uint32_t res = 0; + + int fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); + if (fd < 0) { + DMESG("Can't open HCI socket"); + return res; + } + + hci_dev_list_req dl; + dl.dev_num = 1; + + if (ioctl(fd, HCIGETDEVLIST, (void *)&dl) < 0) { + DMESG("Failed to get HCI device list"); + goto done; + } + + hci_dev_info di; + di.dev_id = dl.dev_req[0].dev_id; + + if (ioctl(fd, HCIGETDEVINFO, (void *)&di) < 0) { + DMESG("Failed to get HCI device list"); + goto done; + } + + memcpy(&res, di.bdaddr, 4); + res *= 0x1000193; + res += di.bdaddr[4]; + res *= 0x1000193; + res += di.bdaddr[5]; + +done: + close(fd); + return res; +} + +namespace pxt { + +int getSerialNumber() { + static int serial; + + if (serial != 0) + return serial; + + serial = bt_addr() & 0x7fffffff; + + return serial; +} + +} // namespace pxt From 5a9a5e997a25ad493084921837e8684a8ecc98ab Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 15 Dec 2017 11:29:18 +0000 Subject: [PATCH 2/3] Error reporting fixes --- libs/core/serialnumber.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/core/serialnumber.cpp b/libs/core/serialnumber.cpp index 7efde89f..b22daafd 100644 --- a/libs/core/serialnumber.cpp +++ b/libs/core/serialnumber.cpp @@ -27,11 +27,11 @@ struct hci_dev_list_req { static uint32_t bt_addr() { - uint32_t res = 0; + uint32_t res = -1; int fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (fd < 0) { - DMESG("Can't open HCI socket"); + DMESG("BT_ADDR: can't open HCI socket"); return res; } @@ -39,7 +39,7 @@ static uint32_t bt_addr() { dl.dev_num = 1; if (ioctl(fd, HCIGETDEVLIST, (void *)&dl) < 0) { - DMESG("Failed to get HCI device list"); + DMESG("BT_ADDR: can't get HCI device list"); goto done; } @@ -47,7 +47,7 @@ static uint32_t bt_addr() { di.dev_id = dl.dev_req[0].dev_id; if (ioctl(fd, HCIGETDEVINFO, (void *)&di) < 0) { - DMESG("Failed to get HCI device list"); + DMESG("BT_ADDR: can't get HCI device info"); goto done; } From 07ddec343adbf2ee99b7df585e5199b62ab7c395 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 15 Dec 2017 11:30:15 +0000 Subject: [PATCH 3/3] Formatting --- libs/core/serialnumber.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libs/core/serialnumber.cpp b/libs/core/serialnumber.cpp index b22daafd..af448f81 100644 --- a/libs/core/serialnumber.cpp +++ b/libs/core/serialnumber.cpp @@ -4,28 +4,27 @@ #include #include -#define BTPROTO_HCI 1 -#define HCIGETDEVLIST _IOR('H', 210, int) -#define HCIGETDEVINFO _IOR('H', 211, int) +#define BTPROTO_HCI 1 +#define HCIGETDEVLIST _IOR('H', 210, int) +#define HCIGETDEVINFO _IOR('H', 211, int) struct hci_dev_info { - uint16_t dev_id; - char name[8]; - uint8_t bdaddr[6]; + uint16_t dev_id; + char name[8]; + uint8_t bdaddr[6]; uint32_t padding[32]; }; struct hci_dev_req { - uint16_t dev_id; - uint32_t dev_opt; + uint16_t dev_id; + uint32_t dev_opt; }; struct hci_dev_list_req { - uint16_t dev_num; - hci_dev_req dev_req[2]; + uint16_t dev_num; + hci_dev_req dev_req[2]; }; - static uint32_t bt_addr() { uint32_t res = -1;