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

Revision:
3:54f9b5e0d12d
Child:
4:cf1b23f4dcd4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KL46Z-USBHostBTstack/USBHostBTstack.cpp	Sat Jan 25 12:58:21 2014 +0000
@@ -0,0 +1,93 @@
+// USBHostBTstack.cpp
+#include "USBHostBTstack.h"
+
+//#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
+#define HCI_ACL_DATA_PACKET     0x02
+#define HCI_SCO_DATA_PACKET     0x03
+#define HCI_EVENT_PACKET        0x04
+
+USBHostBTstack::USBHostBTstack()
+{
+    host = USBHost::getHostInst();
+    m_pCb = NULL;
+}
+
+bool USBHostBTstack::connected()
+{
+    return true;
+}
+
+bool USBHostBTstack::connect()
+{
+    return true;
+}
+
+int USBHostBTstack::open()
+{
+    return 0;
+}
+
+int USBHostBTstack::send_packet(uint8_t packet_type, uint8_t* packet, int 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 0;
+}
+
+void USBHostBTstack::register_packet_handler(void (*pMethod)(uint8_t, uint8_t*, uint16_t) )
+{
+    BT_DBG("pMethod: %p", pMethod);
+    m_pCb = pMethod;
+}
+
+void USBHostBTstack::poll()
+{
+    int result = host->InterruptRead(int_report, sizeof(int_report));
+    if (result >= 0) {
+        BT_DBG_BYTES("HCI_EVT:", int_report, result);
+        if (m_pCb) {
+            m_pCb(HCI_EVENT_PACKET, int_report, result);
+        }    
+    }
+    result = host->BulkRead(bulk_report, sizeof(bulk_report), 0);
+    if (result >= 0) {
+        BT_DBG_BYTES("HCI_ACL_RECV:", bulk_report, result);
+        if (m_pCb) {
+            m_pCb(HCI_ACL_DATA_PACKET, bulk_report, result);
+        }    
+    }
+}
+
+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");
+}