diff --git a/example/rfcomm.c b/example/rfcomm.c index ba2ab48af..8ed761a78 100644 --- a/example/rfcomm.c +++ b/example/rfcomm.c @@ -309,6 +309,12 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint } break; + case HCI_EVENT_LINK_KEY_REQUEST: + // link key request + bt_flip_addr(event_addr, &packet[2]); + bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr); + break; + case HCI_EVENT_PIN_CODE_REQUEST: // inform about pin code request bt_flip_addr(event_addr, &packet[2]); @@ -370,40 +376,47 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint } } -void usage(void){ - fprintf(stderr, "Usage : RFComm [-a|--address aa:bb:cc:dd:ee:ff] [-c|--channel n] [-p|--pin nnnn]\n"); +void usage(const char *name){ + fprintf(stderr, "Usage : %s [-a|--address aa:bb:cc:dd:ee:ff] [-c|--channel n] [-p|--pin nnnn]\n", name); } #define FIFO_NAME "/tmp/rfcomm0" int main (int argc, const char * argv[]){ + int arg = 1; + + if (argc == 1){ + usage(argv[0]); + return 1; } + while (arg < argc) { if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ arg++; if(arg >= argc || !sscan_bd_addr((uint8_t *)argv[arg], addr)){ - usage(); + usage(argv[0]); return 1; } } else if (!strcmp(argv[arg], "-c") || !strcmp(argv[arg], "--channel")) { arg++; if(arg >= argc || !sscanf(argv[arg], "%d", &RFCOMM_CHANNEL_ID)){ - usage(); + usage(argv[0]); return 1; } } else if (!strcmp(argv[arg], "-p") || !strcmp(argv[arg], "--pin")) { arg++; int pin1,pin2,pin3,pin4; if(arg >= argc || sscanf(argv[arg], "%1d%1d%1d%1d", &pin1, &pin2, &pin3, &pin4) != 4){ - usage(); + usage(argv[0]); return 1; } snprintf(PIN, 5, "%01d%01d%01d%01d", pin1, pin2, pin3, pin4); } else { - usage(); + usage(argv[0]); return 1; } arg++; } + printf("Waiting for client to open %s...\n", FIFO_NAME); int err = mknod(FIFO_NAME, S_IFIFO | 0666, 0); if(err >= 0 || errno == EEXIST){ fifo_fd = open(FIFO_NAME, O_WRONLY); diff --git a/example/test.c b/example/test.c index 53d9b753c..fa4fa8952 100644 --- a/example/test.c +++ b/example/test.c @@ -43,12 +43,17 @@ #include // bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard -bd_addr_t addr = {0x00, 0x19, 0x1d, 0x90, 0x44, 0x68 }; // WiiMote +// bd_addr_t addr = {0x00, 0x19, 0x1d, 0x90, 0x44, 0x68 }; // WiiMote +bd_addr_t addr = {0xd4, 0x9a, 0x20, 0x7e, 0x69, 0xcc }; // Magic Mouse hci_con_handle_t con_handle; uint16_t source_cid_interrupt; uint16_t source_cid_control; +float xCoord = 0; +float yCoord = 0; +float speed = 0.2; + void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ bd_addr_t event_addr; @@ -57,17 +62,27 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint case L2CAP_DATA_PACKET: // just dump data for now - printf("source cid %x -- ", channel); - hexdump( packet, size ); + // printf("source cid %x -- ", channel); + // hexdump( packet, size ); // HOME => disconnect - if (packet[0] == 0xA1) { // Status report - if (packet[1] == 0x30 || packet[1] == 0x31) { // type 0x30 or 0x31 - if (packet[3] & 0x080) { // homne button pressed - printf("Disconnect baseband\n"); - bt_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection - } - } + if (packet[0] == 0xA1 && packet[1] == 0x10) { + int16_t moveX = READ_BT_16(packet, 3); + int16_t moveY = READ_BT_16(packet, 5); + + xCoord+=moveX * speed; + yCoord+=moveY * speed; + + xCoord = xCoord<0 ? 0:xCoord; + xCoord = xCoord>320 ? 320:xCoord; + + yCoord = yCoord<0 ? 0:yCoord; + yCoord = yCoord>480 ? 480:yCoord; + + printf("%3d %3d (delta %d %d) ", (int)xCoord, (int)yCoord, moveX, moveY); + if (packet[2] & 1) printf("Left "); + if (packet[2] & 2) printf("Right "); + printf("\n"); } break; @@ -89,9 +104,17 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint case HCI_EVENT_PIN_CODE_REQUEST: // inform about pin code request - printf("Please enter PIN 1234 on remote device\n"); + printf("HCI_EVENT_PIN_CODE_REQUEST\n"); + bt_flip_addr(event_addr, &packet[2]); + bt_send_cmd(&hci_pin_code_request_reply, event_addr, 4, "0000"); break; + case HCI_EVENT_LINK_KEY_REQUEST: + // link key request + bt_flip_addr(event_addr, &packet[2]); + bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr); + break; + case L2CAP_EVENT_CHANNEL_OPENED: // inform about new l2cap connection // inform about new l2cap connection @@ -113,10 +136,10 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint source_cid_control = source_cid; // request acceleration data.. uint8_t setMode31[] = { 0x52, 0x12, 0x00, 0x31 }; - bt_send_l2cap( source_cid, setMode31, sizeof(setMode31)); + // bt_send_l2cap( source_cid, setMode31, sizeof(setMode31)); // stop blinking uint8_t setLEDs[] = { 0x52, 0x11, 0x10 }; - bt_send_l2cap( source_cid, setLEDs, sizeof(setLEDs)); + // bt_send_l2cap( source_cid, setLEDs, sizeof(setLEDs)); } } else { printf("L2CAP connection to device ");