BTstack for EA LPC4088 QSB example program
Dependencies: LPC4088-USBHost mbed
Fork of KL46Z-BTstack_example by
The usage is the same as KL46Z-BTstack_example.
使い方はKL46Z-BTstack_exampleと同じです。
KL46Z-USBHostBTstack/USBHostBTstack.cpp@3:54f9b5e0d12d, 2014-01-25 (annotated)
- Committer:
- va009039
- Date:
- Sat Jan 25 12:58:21 2014 +0000
- Revision:
- 3:54f9b5e0d12d
- Child:
- 4:cf1b23f4dcd4
for FRDM-KL46Z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 3:54f9b5e0d12d | 1 | // USBHostBTstack.cpp |
va009039 | 3:54f9b5e0d12d | 2 | #include "USBHostBTstack.h" |
va009039 | 3:54f9b5e0d12d | 3 | |
va009039 | 3:54f9b5e0d12d | 4 | //#define BTSTACK_DEBUG |
va009039 | 3:54f9b5e0d12d | 5 | |
va009039 | 3:54f9b5e0d12d | 6 | #ifdef BTSTACK_DEBUG |
va009039 | 3:54f9b5e0d12d | 7 | #define BT_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); |
va009039 | 3:54f9b5e0d12d | 8 | #define BT_DBG_BYTES(STR,BUF,LEN) _debug_bytes(__PRETTY_FUNCTION__,__LINE__,STR,BUF,LEN) |
va009039 | 3:54f9b5e0d12d | 9 | #else |
va009039 | 3:54f9b5e0d12d | 10 | #define BT_DBG(...) while(0); |
va009039 | 3:54f9b5e0d12d | 11 | #define BT_DBG_BYTES(S,BUF,LEN) while(0); |
va009039 | 3:54f9b5e0d12d | 12 | #endif |
va009039 | 3:54f9b5e0d12d | 13 | |
va009039 | 3:54f9b5e0d12d | 14 | #define HCI_COMMAND_DATA_PACKET 0x01 |
va009039 | 3:54f9b5e0d12d | 15 | #define HCI_ACL_DATA_PACKET 0x02 |
va009039 | 3:54f9b5e0d12d | 16 | #define HCI_SCO_DATA_PACKET 0x03 |
va009039 | 3:54f9b5e0d12d | 17 | #define HCI_EVENT_PACKET 0x04 |
va009039 | 3:54f9b5e0d12d | 18 | |
va009039 | 3:54f9b5e0d12d | 19 | USBHostBTstack::USBHostBTstack() |
va009039 | 3:54f9b5e0d12d | 20 | { |
va009039 | 3:54f9b5e0d12d | 21 | host = USBHost::getHostInst(); |
va009039 | 3:54f9b5e0d12d | 22 | m_pCb = NULL; |
va009039 | 3:54f9b5e0d12d | 23 | } |
va009039 | 3:54f9b5e0d12d | 24 | |
va009039 | 3:54f9b5e0d12d | 25 | bool USBHostBTstack::connected() |
va009039 | 3:54f9b5e0d12d | 26 | { |
va009039 | 3:54f9b5e0d12d | 27 | return true; |
va009039 | 3:54f9b5e0d12d | 28 | } |
va009039 | 3:54f9b5e0d12d | 29 | |
va009039 | 3:54f9b5e0d12d | 30 | bool USBHostBTstack::connect() |
va009039 | 3:54f9b5e0d12d | 31 | { |
va009039 | 3:54f9b5e0d12d | 32 | return true; |
va009039 | 3:54f9b5e0d12d | 33 | } |
va009039 | 3:54f9b5e0d12d | 34 | |
va009039 | 3:54f9b5e0d12d | 35 | int USBHostBTstack::open() |
va009039 | 3:54f9b5e0d12d | 36 | { |
va009039 | 3:54f9b5e0d12d | 37 | return 0; |
va009039 | 3:54f9b5e0d12d | 38 | } |
va009039 | 3:54f9b5e0d12d | 39 | |
va009039 | 3:54f9b5e0d12d | 40 | int USBHostBTstack::send_packet(uint8_t packet_type, uint8_t* packet, int size) |
va009039 | 3:54f9b5e0d12d | 41 | { |
va009039 | 3:54f9b5e0d12d | 42 | USB_TYPE res; |
va009039 | 3:54f9b5e0d12d | 43 | switch(packet_type){ |
va009039 | 3:54f9b5e0d12d | 44 | case HCI_COMMAND_DATA_PACKET: |
va009039 | 3:54f9b5e0d12d | 45 | BT_DBG_BYTES("HCI_CMD:", packet, size); |
va009039 | 3:54f9b5e0d12d | 46 | res = host->controlWrite(dev, |
va009039 | 3:54f9b5e0d12d | 47 | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_DEVICE, |
va009039 | 3:54f9b5e0d12d | 48 | 0, 0, 0, packet, size); |
va009039 | 3:54f9b5e0d12d | 49 | TEST_ASSERT(res == USB_TYPE_OK); |
va009039 | 3:54f9b5e0d12d | 50 | break; |
va009039 | 3:54f9b5e0d12d | 51 | case HCI_ACL_DATA_PACKET: |
va009039 | 3:54f9b5e0d12d | 52 | BT_DBG_BYTES("ACL_SND:", packet, size); |
va009039 | 3:54f9b5e0d12d | 53 | res = host->bulkWrite(dev, bulk_out, packet, size); |
va009039 | 3:54f9b5e0d12d | 54 | TEST_ASSERT(res == USB_TYPE_OK); |
va009039 | 3:54f9b5e0d12d | 55 | break; |
va009039 | 3:54f9b5e0d12d | 56 | default: |
va009039 | 3:54f9b5e0d12d | 57 | TEST_ASSERT(0); |
va009039 | 3:54f9b5e0d12d | 58 | } |
va009039 | 3:54f9b5e0d12d | 59 | return 0; |
va009039 | 3:54f9b5e0d12d | 60 | } |
va009039 | 3:54f9b5e0d12d | 61 | |
va009039 | 3:54f9b5e0d12d | 62 | void USBHostBTstack::register_packet_handler(void (*pMethod)(uint8_t, uint8_t*, uint16_t) ) |
va009039 | 3:54f9b5e0d12d | 63 | { |
va009039 | 3:54f9b5e0d12d | 64 | BT_DBG("pMethod: %p", pMethod); |
va009039 | 3:54f9b5e0d12d | 65 | m_pCb = pMethod; |
va009039 | 3:54f9b5e0d12d | 66 | } |
va009039 | 3:54f9b5e0d12d | 67 | |
va009039 | 3:54f9b5e0d12d | 68 | void USBHostBTstack::poll() |
va009039 | 3:54f9b5e0d12d | 69 | { |
va009039 | 3:54f9b5e0d12d | 70 | int result = host->InterruptRead(int_report, sizeof(int_report)); |
va009039 | 3:54f9b5e0d12d | 71 | if (result >= 0) { |
va009039 | 3:54f9b5e0d12d | 72 | BT_DBG_BYTES("HCI_EVT:", int_report, result); |
va009039 | 3:54f9b5e0d12d | 73 | if (m_pCb) { |
va009039 | 3:54f9b5e0d12d | 74 | m_pCb(HCI_EVENT_PACKET, int_report, result); |
va009039 | 3:54f9b5e0d12d | 75 | } |
va009039 | 3:54f9b5e0d12d | 76 | } |
va009039 | 3:54f9b5e0d12d | 77 | result = host->BulkRead(bulk_report, sizeof(bulk_report), 0); |
va009039 | 3:54f9b5e0d12d | 78 | if (result >= 0) { |
va009039 | 3:54f9b5e0d12d | 79 | BT_DBG_BYTES("HCI_ACL_RECV:", bulk_report, result); |
va009039 | 3:54f9b5e0d12d | 80 | if (m_pCb) { |
va009039 | 3:54f9b5e0d12d | 81 | m_pCb(HCI_ACL_DATA_PACKET, bulk_report, result); |
va009039 | 3:54f9b5e0d12d | 82 | } |
va009039 | 3:54f9b5e0d12d | 83 | } |
va009039 | 3:54f9b5e0d12d | 84 | } |
va009039 | 3:54f9b5e0d12d | 85 | |
va009039 | 3:54f9b5e0d12d | 86 | void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len) |
va009039 | 3:54f9b5e0d12d | 87 | { |
va009039 | 3:54f9b5e0d12d | 88 | printf("[%s:%d]\n%s", pretty, line, s); |
va009039 | 3:54f9b5e0d12d | 89 | for(int i = 0; i < len; i++) { |
va009039 | 3:54f9b5e0d12d | 90 | printf(" %02x", buf[i]); |
va009039 | 3:54f9b5e0d12d | 91 | } |
va009039 | 3:54f9b5e0d12d | 92 | printf("\n"); |
va009039 | 3:54f9b5e0d12d | 93 | } |