2020-07-23 19:37:48 +00:00
|
|
|
#include "io.h"
|
|
|
|
#include "bt.h"
|
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
#define MAX_MSG_LEN 50
|
|
|
|
#define MAX_READ_RUN 100
|
2020-07-23 19:37:48 +00:00
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
unsigned char data_buf[MAX_MSG_LEN];
|
|
|
|
unsigned int data_len;
|
|
|
|
unsigned int messages_received = 0;
|
|
|
|
unsigned int poll_state = 0;
|
2020-07-23 19:37:48 +00:00
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
enum {
|
|
|
|
LE_EVENT_CODE = 0x3e,
|
|
|
|
LE_ADREPORT_CODE = 0x02,
|
|
|
|
HCI_EVENT_PKT = 0x04
|
|
|
|
};
|
2020-07-23 19:37:48 +00:00
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
void poll2(unsigned char byte)
|
2020-07-23 19:37:48 +00:00
|
|
|
{
|
2020-07-26 18:28:44 +00:00
|
|
|
switch (poll_state) {
|
|
|
|
case 0:
|
|
|
|
if (byte != HCI_EVENT_PKT) poll_state = 0;
|
|
|
|
else poll_state = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (byte != LE_EVENT_CODE) poll_state = 0;
|
|
|
|
else poll_state = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (byte > MAX_MSG_LEN) poll_state = 0;
|
|
|
|
else {
|
|
|
|
poll_state = 3;
|
|
|
|
data_len = byte;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data_buf[poll_state - 3] = byte;
|
|
|
|
if (poll_state == data_len + 3 - 1) {
|
|
|
|
messages_received++;
|
|
|
|
poll_state = 0;
|
|
|
|
} else poll_state++;
|
|
|
|
}
|
2020-07-23 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
unsigned char *poll()
|
2020-07-23 19:37:48 +00:00
|
|
|
{
|
2020-07-26 18:28:44 +00:00
|
|
|
unsigned int goal = messages_received + 1;
|
|
|
|
|
|
|
|
if (bt_isReadByteReady()) {
|
|
|
|
unsigned int run = 0;
|
|
|
|
|
|
|
|
while (run < MAX_READ_RUN && messages_received < goal && bt_isReadByteReady()) {
|
|
|
|
unsigned char byte = bt_readByte();
|
|
|
|
poll2(byte);
|
|
|
|
run++;
|
|
|
|
}
|
|
|
|
if (run == MAX_READ_RUN) return 0;
|
|
|
|
else return data_buf;
|
2020-07-23 19:37:48 +00:00
|
|
|
}
|
2020-07-26 18:28:44 +00:00
|
|
|
return 0;
|
2020-07-23 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
void bt_update()
|
2020-07-23 19:37:48 +00:00
|
|
|
{
|
2020-07-26 18:28:44 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
|
|
|
|
while ( (buf = poll()) ) {
|
|
|
|
if (data_len >= 2 && buf[0] == LE_ADREPORT_CODE) {
|
|
|
|
unsigned char numreports = buf[1];
|
2020-07-30 14:56:39 +00:00
|
|
|
unsigned int c=0;
|
2020-07-26 18:28:44 +00:00
|
|
|
|
2020-07-30 14:56:39 +00:00
|
|
|
if (numreports > 0 && data_len >= 11) {
|
2020-07-30 20:47:18 +00:00
|
|
|
uart_writeText("\ra(");
|
2020-07-26 18:28:44 +00:00
|
|
|
for (int c=9;c>=4;c--) {
|
|
|
|
if (c != 9) uart_writeText(":");
|
|
|
|
uart_hex(buf[c]);
|
|
|
|
}
|
|
|
|
uart_writeText(")");
|
|
|
|
|
|
|
|
unsigned char buf_len = buf[10];
|
|
|
|
|
2020-07-30 14:56:39 +00:00
|
|
|
if ((buf_len + 11) <= data_len - 1) {
|
2020-07-26 18:28:44 +00:00
|
|
|
buf += 11;
|
2020-07-30 14:56:39 +00:00
|
|
|
|
|
|
|
while (c < numreports) {
|
|
|
|
unsigned char ad_len = buf[0];
|
|
|
|
unsigned char ad_type = buf[1];
|
|
|
|
buf += 2;
|
|
|
|
|
|
|
|
if (ad_len > 2) {
|
|
|
|
if (ad_type == 0x09 || ad_type == 0x08) {
|
|
|
|
unsigned int d=0;
|
|
|
|
|
2020-07-30 20:47:18 +00:00
|
|
|
uart_writeText(" -> adtype("); uart_hex(ad_type); uart_writeText(":"); uart_hex(ad_len); uart_writeText(")");
|
2020-07-30 14:56:39 +00:00
|
|
|
uart_writeText(" -> ");
|
|
|
|
while (d<ad_len - 1) {
|
|
|
|
uart_writeByteBlockingActual(buf[d]);
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
uart_writeText("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf += ad_len - 1;
|
|
|
|
c++;
|
|
|
|
}
|
2020-07-26 18:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
uart_init();
|
|
|
|
bt_init();
|
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
uart_writeText("bt_reset()\n");
|
|
|
|
bt_reset();
|
2020-07-23 19:37:48 +00:00
|
|
|
|
2020-07-26 18:28:44 +00:00
|
|
|
uart_writeText("bt_loadfirmware()\n");
|
|
|
|
bt_loadfirmware();
|
2020-07-23 19:37:48 +00:00
|
|
|
|
2020-08-05 20:30:54 +00:00
|
|
|
uart_writeText("bt_setbaud()\n");
|
|
|
|
bt_setbaud();
|
|
|
|
|
|
|
|
uart_writeText("bt_setbdaddr()\n");
|
|
|
|
bt_setbdaddr();
|
|
|
|
|
|
|
|
/*
|
|
|
|
uart_writeText("startActiveScanning()\n");
|
2020-07-23 19:37:48 +00:00
|
|
|
setLEeventmask(0xff);
|
|
|
|
startActiveScanning();
|
2020-08-05 20:30:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char local_addr[6];
|
|
|
|
uart_writeText("bt_getbdaddr()\n");
|
|
|
|
bt_getbdaddr(local_addr);
|
|
|
|
uart_writeText("\nBD_ADDR is ");
|
|
|
|
for (int c=5;c>=0;c--) uart_byte(local_addr[c]);
|
|
|
|
uart_writeText("\n");
|
|
|
|
|
|
|
|
uart_writeText("startActiveAdvertising()\n");
|
|
|
|
startActiveAdvertising();
|
2020-07-23 19:37:48 +00:00
|
|
|
|
|
|
|
uart_writeText("Waiting for input...\n");
|
2020-07-26 18:28:44 +00:00
|
|
|
while (1) {
|
|
|
|
uart_update();
|
|
|
|
bt_update();
|
|
|
|
}
|
2020-07-23 19:37:48 +00:00
|
|
|
}
|