BTstack for EA LPC4088 QSB example program

Dependencies:   LPC4088-USBHost mbed

Fork of KL46Z-BTstack_example by Norimasa Okamoto

The usage is the same as KL46Z-BTstack_example.
使い方はKL46Z-BTstack_exampleと同じです。
/media/uploads/va009039/lpc4088-btstack-480x360.jpg

Committer:
va009039
Date:
Sat Jan 25 12:58:21 2014 +0000
Revision:
3:54f9b5e0d12d
Child:
6:d946cb517371
for FRDM-KL46Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 3:54f9b5e0d12d 1 #include "mbed.h"
va009039 3:54f9b5e0d12d 2 #include <btstack/run_loop.h>
va009039 3:54f9b5e0d12d 3 #include <btstack/hci_cmds.h>
va009039 3:54f9b5e0d12d 4 #include "hci.h"
va009039 3:54f9b5e0d12d 5 #include "l2cap.h"
va009039 3:54f9b5e0d12d 6 #include "debug.h"
va009039 3:54f9b5e0d12d 7 #include "bd_addr.h" // class bd_addr
va009039 3:54f9b5e0d12d 8 Serial pc(USBTX, USBRX);
va009039 3:54f9b5e0d12d 9 DigitalOut led1(LED1), led2(LED2);
va009039 3:54f9b5e0d12d 10
va009039 3:54f9b5e0d12d 11 #if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)
va009039 3:54f9b5e0d12d 12 #define LED_ON 0
va009039 3:54f9b5e0d12d 13 #define LED_OFF 1
va009039 3:54f9b5e0d12d 14 #else
va009039 3:54f9b5e0d12d 15 #error
va009039 3:54f9b5e0d12d 16 #endif
va009039 3:54f9b5e0d12d 17
va009039 3:54f9b5e0d12d 18 #define INQUIRY_INTERVAL 15
va009039 3:54f9b5e0d12d 19
va009039 3:54f9b5e0d12d 20 bd_addr addr;
va009039 3:54f9b5e0d12d 21
va009039 3:54f9b5e0d12d 22 static void hid_process_packet(uint8_t* report, int size)
va009039 3:54f9b5e0d12d 23 {
va009039 3:54f9b5e0d12d 24 if (report[0] == 0xa1 && report[1] == 0x02) {
va009039 3:54f9b5e0d12d 25 led1 = (report[2] & 0x01) ? LED_ON : LED_OFF; // left
va009039 3:54f9b5e0d12d 26 led2 = (report[2] & 0x02) ? LED_ON : LED_OFF; // right
va009039 3:54f9b5e0d12d 27 //led2 = (report[2] & 0x04) ? LED_ON : LED_OFF; // center
va009039 3:54f9b5e0d12d 28 }
va009039 3:54f9b5e0d12d 29 hexdump(report, size);
va009039 3:54f9b5e0d12d 30 }
va009039 3:54f9b5e0d12d 31
va009039 3:54f9b5e0d12d 32 static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
va009039 3:54f9b5e0d12d 33
va009039 3:54f9b5e0d12d 34 if (packet_type == HCI_EVENT_PACKET && packet[0] == L2CAP_EVENT_CHANNEL_OPENED){
va009039 3:54f9b5e0d12d 35 if (packet[2]) {
va009039 3:54f9b5e0d12d 36 log_info("Connection failed\n");
va009039 3:54f9b5e0d12d 37 return;
va009039 3:54f9b5e0d12d 38 }
va009039 3:54f9b5e0d12d 39 log_info("Connected\n");
va009039 3:54f9b5e0d12d 40 }
va009039 3:54f9b5e0d12d 41 if (packet_type == L2CAP_DATA_PACKET){
va009039 3:54f9b5e0d12d 42 // handle input
va009039 3:54f9b5e0d12d 43 log_info("HID report, size %u\n", size);
va009039 3:54f9b5e0d12d 44 hid_process_packet(packet, size);
va009039 3:54f9b5e0d12d 45 }
va009039 3:54f9b5e0d12d 46 }
va009039 3:54f9b5e0d12d 47
va009039 3:54f9b5e0d12d 48 static void packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
va009039 3:54f9b5e0d12d 49 if (packet_type == HCI_EVENT_PACKET) {
va009039 3:54f9b5e0d12d 50 switch (packet[0]) {
va009039 3:54f9b5e0d12d 51 case BTSTACK_EVENT_STATE:
va009039 3:54f9b5e0d12d 52 // bt stack activated, get started - set local name
va009039 3:54f9b5e0d12d 53 if (packet[2] == HCI_STATE_WORKING) {
va009039 3:54f9b5e0d12d 54 hci_send_cmd(&hci_write_authentication_enable, 1);
va009039 3:54f9b5e0d12d 55 }
va009039 3:54f9b5e0d12d 56 break;
va009039 3:54f9b5e0d12d 57
va009039 3:54f9b5e0d12d 58 case HCI_EVENT_INQUIRY_RESULT:
va009039 3:54f9b5e0d12d 59 // ignore none mouses
va009039 3:54f9b5e0d12d 60 if ((packet[12] & 0x80) != 0x80 || packet[13] != 0x25) break;
va009039 3:54f9b5e0d12d 61 addr.data(&packet[3]);
va009039 3:54f9b5e0d12d 62 log_info("Mouse addr: %s\n", addr.to_str());
va009039 3:54f9b5e0d12d 63 hci_send_cmd(&hci_inquiry_cancel);
va009039 3:54f9b5e0d12d 64 break;
va009039 3:54f9b5e0d12d 65
va009039 3:54f9b5e0d12d 66 case HCI_EVENT_INQUIRY_COMPLETE:
va009039 3:54f9b5e0d12d 67 log_info("No mouse found :(\n");
va009039 3:54f9b5e0d12d 68 break;
va009039 3:54f9b5e0d12d 69
va009039 3:54f9b5e0d12d 70 case HCI_EVENT_LINK_KEY_REQUEST:
va009039 3:54f9b5e0d12d 71 // deny link key request
va009039 3:54f9b5e0d12d 72 hci_send_cmd(&hci_link_key_request_negative_reply, addr.data());
va009039 3:54f9b5e0d12d 73 break;
va009039 3:54f9b5e0d12d 74
va009039 3:54f9b5e0d12d 75 case HCI_EVENT_PIN_CODE_REQUEST:
va009039 3:54f9b5e0d12d 76 // inform about pin code request
va009039 3:54f9b5e0d12d 77 log_info("Enter 0000\n");
va009039 3:54f9b5e0d12d 78 hci_send_cmd(&hci_pin_code_request_reply, addr.data(), 4, "0000");
va009039 3:54f9b5e0d12d 79 break;
va009039 3:54f9b5e0d12d 80
va009039 3:54f9b5e0d12d 81 case HCI_EVENT_COMMAND_COMPLETE:
va009039 3:54f9b5e0d12d 82 if (COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable)){
va009039 3:54f9b5e0d12d 83 log_info("Inquiry\n");
va009039 3:54f9b5e0d12d 84 hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
va009039 3:54f9b5e0d12d 85 }
va009039 3:54f9b5e0d12d 86 if (COMMAND_COMPLETE_EVENT(packet, hci_inquiry_cancel) ) {
va009039 3:54f9b5e0d12d 87 // inq successfully cancelled
va009039 3:54f9b5e0d12d 88 log_info("Connecting\n");
va009039 3:54f9b5e0d12d 89 l2cap_create_channel_internal(NULL, l2cap_packet_handler, addr.data(), PSM_HID_INTERRUPT, 150);
va009039 3:54f9b5e0d12d 90 }
va009039 3:54f9b5e0d12d 91 break;
va009039 3:54f9b5e0d12d 92 }
va009039 3:54f9b5e0d12d 93 }
va009039 3:54f9b5e0d12d 94 }
va009039 3:54f9b5e0d12d 95
va009039 3:54f9b5e0d12d 96 int main(void){
va009039 3:54f9b5e0d12d 97 //pc.baud(921600);
va009039 3:54f9b5e0d12d 98 log_info("%s\n", __FILE__);
va009039 3:54f9b5e0d12d 99
va009039 3:54f9b5e0d12d 100 // init LEDs
va009039 3:54f9b5e0d12d 101 led1 = LED_OFF;
va009039 3:54f9b5e0d12d 102 led2 = LED_OFF;
va009039 3:54f9b5e0d12d 103
va009039 3:54f9b5e0d12d 104 // GET STARTED
va009039 3:54f9b5e0d12d 105 run_loop_init(RUN_LOOP_EMBEDDED);
va009039 3:54f9b5e0d12d 106
va009039 3:54f9b5e0d12d 107 // init HCI
va009039 3:54f9b5e0d12d 108 hci_transport_t* transport = hci_transport_usb_instance();
va009039 3:54f9b5e0d12d 109 remote_device_db_t* remote_db = (remote_device_db_t *)&remote_device_db_memory;
va009039 3:54f9b5e0d12d 110 hci_init(transport, NULL, NULL, remote_db);
va009039 3:54f9b5e0d12d 111
va009039 3:54f9b5e0d12d 112 // init L2CAP
va009039 3:54f9b5e0d12d 113 l2cap_init();
va009039 3:54f9b5e0d12d 114 l2cap_register_packet_handler(packet_handler);
va009039 3:54f9b5e0d12d 115
va009039 3:54f9b5e0d12d 116 // turn on!, send RESET command
va009039 3:54f9b5e0d12d 117 hci_power_control(HCI_POWER_ON);
va009039 3:54f9b5e0d12d 118
va009039 3:54f9b5e0d12d 119 // go!
va009039 3:54f9b5e0d12d 120 run_loop_execute();
va009039 3:54f9b5e0d12d 121
va009039 3:54f9b5e0d12d 122 return 0;
va009039 3:54f9b5e0d12d 123 }