BTstack Bluetooth stack

Dependencies:   mbed USBHost

USBホストライブラリを変更しました。

  • Bluetoothマウス(VGP-BMS33)での動作を確認しました。mouse_demo.cpp

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Fri Mar 22 22:35:57 2013 +0000
Parent:
1:b657594559be
Commit message:
change to single thread

Changed in this revision

BTstack/hci.c Show annotated file Show diff for this revision Revisions of this file
BTstack/hci_transport_usb.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostBTstack/USBHostBTstack.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostBTstack/USBHostBTstack.h Show annotated file Show diff for this revision Revisions of this file
--- a/BTstack/hci.c	Thu Mar 21 13:53:05 2013 +0000
+++ b/BTstack/hci.c	Fri Mar 22 22:35:57 2013 +0000
@@ -677,9 +677,9 @@
     }
     
     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
-	
-	// execute main loop
-	hci_run();
+    
+    // execute main loop
+    hci_run();
 }
 
 void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
@@ -983,11 +983,11 @@
     }
 
     // create internal event
-	hci_emit_state();
+    hci_emit_state();
     
-	// trigger next/first action
-	hci_run();
-	
+    // trigger next/first action
+    hci_run();
+    
     return 0;
 }
 
@@ -1061,7 +1061,7 @@
         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
             link_key_t link_key;
             log_info("responding to link key request\n");
-            if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
+            if (hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
             } else {
                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
@@ -1109,18 +1109,18 @@
                         log_info("hci_run: init script done\n\r");
                     }
                     // otherwise continue
-					hci_send_cmd(&hci_read_bd_addr);
-					break;
-				case 4:
-					hci_send_cmd(&hci_read_buffer_size);
-					break;
+                    hci_send_cmd(&hci_read_bd_addr);
+                    break;
+                case 4:
+                    hci_send_cmd(&hci_read_buffer_size);
+                    break;
                 case 5:
                     // ca. 15 sec
                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
                     break;
-				case 6:
-					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
-					break;
+                case 6:
+                    hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
+                    break;
                 case 7:
 #ifndef EMBEDDED
                 {
--- a/BTstack/hci_transport_usb.cpp	Thu Mar 21 13:53:05 2013 +0000
+++ b/BTstack/hci_transport_usb.cpp	Fri Mar 22 22:35:57 2013 +0000
@@ -73,8 +73,18 @@
 static hci_transport_t * hci_transport_usb = NULL;
 static USBHostBTstack* bt = NULL;
 
+static int usb_process_ds(struct data_source *ds) {
+    if (bt) {
+        bt->poll();
+    }
+    return 0;
+}
+
 static int usb_open(void *transport_config){
     log_info("usb_open\n");
+    data_source_t *ds = (data_source_t*)malloc(sizeof(data_source_t));
+    ds->process = usb_process_ds;
+    run_loop_add_data_source(ds);
     if (bt) {
         return bt->open();
     }
--- a/USBHostBTstack/USBHostBTstack.cpp	Thu Mar 21 13:53:05 2013 +0000
+++ b/USBHostBTstack/USBHostBTstack.cpp	Fri Mar 22 22:35:57 2013 +0000
@@ -1,12 +1,15 @@
+// USBHostBTstack.cpp
 #include "USBHostBTstack.h"
 #include "dbg.h"
 
-#define BTSTACK_DEBUG 1
+//#define BTSTACK_DEBUG
 
 #ifdef BTSTACK_DEBUG
 #define BT_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
+#define BT_DBG_BYTES(STR,BUF,LEN) _debug_bytes(__PRETTY_FUNCTION__,__LINE__,STR,BUF,LEN)
 #else
 #define BT_DBG(...)  while(0);
+#define BT_DBG_BYTES(S,BUF,LEN) while(0);
 #endif
 
 #define HCI_COMMAND_DATA_PACKET 0x01
@@ -81,30 +84,28 @@
     return false;
 }
 
-void USBHostBTstack::int_rxHandler() {
-    int len_listen = int_in->getSize();
+void USBHostBTstack::int_rxHandler()
+{
     int len = int_in->getLengthTransferred();
-    
-    BT_DBG("len_listen: %d, len: %d", len_listen, len);
-    if (m_pCb) {
-        m_pCb(HCI_EVENT_PACKET, int_report, len);
-    }
-    if (dev) {
-        host->interruptRead(dev, int_in, int_report, len_listen, false);
-    }
+    BT_DBG_BYTES("HCI_EVT:", int_report, len);
+    Packet* pkt = mail_box.alloc();
+    TEST_ASSERT(pkt);
+    pkt->type = HCI_EVENT_PACKET;
+    pkt->buf = int_report;
+    pkt->len = len;
+    mail_box.put(pkt);
 }
 
-void USBHostBTstack::bulk_rxHandler() {
-    int len_listen = bulk_in->getSize();
+void USBHostBTstack::bulk_rxHandler()
+{
     int len = bulk_in->getLengthTransferred();
-    
-    BT_DBG("len_listen: %d, len: %d", len_listen, len);
-    if (m_pCb) {
-        m_pCb(HCI_ACL_DATA_PACKET, bulk_report, len);
-    }
-    if (dev) {
-        host->bulkRead(dev, bulk_in, bulk_report, len_listen, false);
-    }
+    BT_DBG_BYTES("ACL_RCV:", bulk_report, len);
+    Packet* pkt = mail_box.alloc();
+    TEST_ASSERT(pkt);
+    pkt->type = HCI_ACL_DATA_PACKET;
+    pkt->buf = bulk_report;
+    pkt->len = len;
+    mail_box.put(pkt);
 }
 
 /*virtual*/ void USBHostBTstack::setVidPid(uint16_t vid, uint16_t pid)
@@ -117,7 +118,6 @@
     BT_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
     if ((btstack_intf == -1) && intf_class == 0xe0) {
         btstack_intf = intf_nb;
-        btstack_device_found = true;
         return true;
     }
     return false;
@@ -158,22 +158,22 @@
 
 int USBHostBTstack::send_packet(uint8_t packet_type, uint8_t* packet, int size)
 {
-    BT_DBG("packet_type: %d, packet: %p, size: %d", packet_type, packet, size);
     USB_TYPE res;
     switch(packet_type){
         case HCI_COMMAND_DATA_PACKET:
+            BT_DBG_BYTES("HCI_CMD:", packet, size);
             res = host->controlWrite(dev,
                     USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_DEVICE, 
                     0, 0, 0, packet, size);
             TEST_ASSERT(res == USB_TYPE_OK);
             break;
         case HCI_ACL_DATA_PACKET:
+            BT_DBG_BYTES("ACL_SND:", packet, size);
             res = host->bulkWrite(dev, bulk_out, packet, size);
             TEST_ASSERT(res == USB_TYPE_OK);
             break;
         default:
             TEST_ASSERT(0);
-            return -1;
     }
     return 0;
 }
@@ -184,3 +184,32 @@
     m_pCb = pMethod;
 }
 
+void USBHostBTstack::poll()
+{
+    osEvent evt = mail_box.get(0); // non-blocking
+    if (evt.status == osEventMail) {
+        Packet* pkt = reinterpret_cast<Packet*>(evt.value.p);
+        TEST_ASSERT(pkt);
+        //BT_DBG("type: %d, buf: %p, len: %d", pkt->type, pkt->buf, pkt->len);
+        if (m_pCb) {
+            m_pCb(pkt->type, pkt->buf, pkt->len);
+        }
+        if (dev) {
+            if (pkt->type == HCI_EVENT_PACKET) { 
+                host->interruptRead(dev, int_in, int_report, int_in->getSize(), false);
+            } else { // HCI_ACL_DATA_PACKET
+                host->bulkRead(dev, bulk_in, bulk_report, bulk_in->getSize(), false);
+            }
+        }
+        mail_box.free(pkt);
+    }
+}
+
+void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len)
+{
+    printf("[%s:%d]\n%s", pretty, line, s);
+    for(int i = 0; i < len; i++) {
+        printf(" %02x", buf[i]);
+    }
+    printf("\n");
+}
--- a/USBHostBTstack/USBHostBTstack.h	Thu Mar 21 13:53:05 2013 +0000
+++ b/USBHostBTstack/USBHostBTstack.h	Fri Mar 22 22:35:57 2013 +0000
@@ -4,6 +4,12 @@
 
 #define TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
 
+struct Packet {
+    uint8_t type;
+    uint8_t* buf;
+    uint16_t len;
+};
+
 /** 
  * A class to communicate a BTstack
  */
@@ -18,7 +24,7 @@
     /**
     * Check if a BTstack device is connected
     *
-    * @return true if a MSD device is connected
+    * @return true if a BTstack device is connected
     */
     bool connected();
 
@@ -32,7 +38,8 @@
     int open();
     int send_packet(uint8_t packet_type, uint8_t* packet, int size);
     void register_packet_handler( void (*pMethod)(uint8_t, uint8_t*, uint16_t));
-    
+    void poll();
+
 protected:
     //From IUSBEnumerator
     virtual void setVidPid(uint16_t vid, uint16_t pid);
@@ -55,7 +62,10 @@
     bool btstack_device_found;
     int btstack_intf;
     void (*m_pCb)(uint8_t, uint8_t*, uint16_t);
+    Mail<Packet, 2> mail_box;
     void int_rxHandler();
     void bulk_rxHandler();
     void init();
 };
+
+void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len);