Norimasa Okamoto
/
BTstack
BTstack Bluetooth stack
Embed:
(wiki syntax)
Show/hide line numbers
mouse_demo.cpp
00001 #if 1 00002 /* mouse_demo.cpp */ 00003 #include "mbed.h" 00004 #include <btstack/run_loop.h> 00005 #include <btstack/hci_cmds.h> 00006 #include "hci.h" 00007 #include "l2cap.h" 00008 #include "debug.h" 00009 #include "bd_addr.h" // class bd_addr 00010 Serial pc(USBTX, USBRX); 00011 DigitalOut led1(LED1), led2(LED2), led3(LED3); 00012 00013 #define INQUIRY_INTERVAL 15 00014 00015 bd_addr addr; 00016 00017 static void hid_process_packet(uint8_t* report, int size) 00018 { 00019 if (report[0] == 0xa1 && report[1] == 0x02) { 00020 led1 = report[2] & 0x01; // left 00021 led2 = report[2] & 0x02; // right 00022 led3 = report[2] & 0x04; // center 00023 } 00024 hexdump(report, size); 00025 } 00026 00027 static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 00028 00029 if (packet_type == HCI_EVENT_PACKET && packet[0] == L2CAP_EVENT_CHANNEL_OPENED){ 00030 if (packet[2]) { 00031 log_info("Connection failed\n"); 00032 return; 00033 } 00034 log_info("Connected\n"); 00035 } 00036 if (packet_type == L2CAP_DATA_PACKET){ 00037 // handle input 00038 log_info("HID report, size %u\n", size); 00039 hid_process_packet(packet, size); 00040 } 00041 } 00042 00043 static void packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 00044 if (packet_type == HCI_EVENT_PACKET) { 00045 switch (packet[0]) { 00046 case BTSTACK_EVENT_STATE: 00047 // bt stack activated, get started - set local name 00048 if (packet[2] == HCI_STATE_WORKING) { 00049 hci_send_cmd(&hci_write_authentication_enable, 1); 00050 } 00051 break; 00052 00053 case HCI_EVENT_INQUIRY_RESULT: 00054 // ignore none mouses 00055 if ((packet[12] & 0x80) != 0x80 || packet[13] != 0x25) break; 00056 addr.data(&packet[3]); 00057 log_info("Mouse addr: %s\n", addr.to_str()); 00058 hci_send_cmd(&hci_inquiry_cancel); 00059 break; 00060 00061 case HCI_EVENT_INQUIRY_COMPLETE: 00062 log_info("No mouse found :(\n"); 00063 break; 00064 00065 case HCI_EVENT_LINK_KEY_REQUEST: 00066 // deny link key request 00067 hci_send_cmd(&hci_link_key_request_negative_reply, addr.data()); 00068 break; 00069 00070 case HCI_EVENT_PIN_CODE_REQUEST: 00071 // inform about pin code request 00072 log_info("Enter 0000\n"); 00073 hci_send_cmd(&hci_pin_code_request_reply, addr.data(), 4, "0000"); 00074 break; 00075 00076 case HCI_EVENT_COMMAND_COMPLETE: 00077 if (COMMAND_COMPLETE_EVENT(packet, hci_write_authentication_enable)){ 00078 log_info("Inquiry\n"); 00079 hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); 00080 } 00081 if (COMMAND_COMPLETE_EVENT(packet, hci_inquiry_cancel) ) { 00082 // inq successfully cancelled 00083 log_info("Connecting\n"); 00084 l2cap_create_channel_internal(NULL, l2cap_packet_handler, addr.data(), PSM_HID_INTERRUPT, 150); 00085 } 00086 break; 00087 } 00088 } 00089 } 00090 00091 int main(void){ 00092 pc.baud(921600); 00093 log_info("%s\n", __FILE__); 00094 00095 // init LEDs 00096 led1 = led2 = led3 = 1; 00097 00098 // GET STARTED 00099 run_loop_init(RUN_LOOP_EMBEDDED); 00100 00101 // init HCI 00102 hci_transport_t* transport = hci_transport_usb_instance(); 00103 remote_device_db_t* remote_db = (remote_device_db_t *)&remote_device_db_memory; 00104 hci_init(transport, NULL, NULL, remote_db); 00105 00106 // init L2CAP 00107 l2cap_init(); 00108 l2cap_register_packet_handler(packet_handler); 00109 00110 // turn on!, send RESET command 00111 hci_power_control(HCI_POWER_ON); 00112 00113 // go! 00114 run_loop_execute(); 00115 00116 return 0; 00117 } 00118 #endif
Generated on Tue Jul 12 2022 15:55:29 by 1.7.2