mbed base bard check program for BlueTooth USB dongle module (3 switches, 6 leds, I2C LCD, A/D)
Fork of BTstack by
BTstack/hci_transport_usb.cpp@3:7b7d1273e2d5, 2016-10-17 (annotated)
- Committer:
- tamaki
- Date:
- Mon Oct 17 00:25:18 2016 +0000
- Revision:
- 3:7b7d1273e2d5
- Parent:
- 2:871b41f4789e
mbed base bard check program
; for BlueTooth USB dongle module
; (3 switches, 6 leds, I2C LCD, A/D)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:1ed23ab1345f | 1 | /* |
va009039 | 0:1ed23ab1345f | 2 | * Copyright (C) 2009-2012 by Matthias Ringwald |
va009039 | 0:1ed23ab1345f | 3 | * |
va009039 | 0:1ed23ab1345f | 4 | * Redistribution and use in source and binary forms, with or without |
va009039 | 0:1ed23ab1345f | 5 | * modification, are permitted provided that the following conditions |
va009039 | 0:1ed23ab1345f | 6 | * are met: |
va009039 | 0:1ed23ab1345f | 7 | * |
va009039 | 0:1ed23ab1345f | 8 | * 1. Redistributions of source code must retain the above copyright |
va009039 | 0:1ed23ab1345f | 9 | * notice, this list of conditions and the following disclaimer. |
va009039 | 0:1ed23ab1345f | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
va009039 | 0:1ed23ab1345f | 11 | * notice, this list of conditions and the following disclaimer in the |
va009039 | 0:1ed23ab1345f | 12 | * documentation and/or other materials provided with the distribution. |
va009039 | 0:1ed23ab1345f | 13 | * 3. Neither the name of the copyright holders nor the names of |
va009039 | 0:1ed23ab1345f | 14 | * contributors may be used to endorse or promote products derived |
va009039 | 0:1ed23ab1345f | 15 | * from this software without specific prior written permission. |
va009039 | 0:1ed23ab1345f | 16 | * 4. Any redistribution, use, or modification is done solely for |
va009039 | 0:1ed23ab1345f | 17 | * personal benefit and not for any commercial purpose or for |
va009039 | 0:1ed23ab1345f | 18 | * monetary gain. |
va009039 | 0:1ed23ab1345f | 19 | * |
va009039 | 0:1ed23ab1345f | 20 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
va009039 | 0:1ed23ab1345f | 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
va009039 | 0:1ed23ab1345f | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
va009039 | 0:1ed23ab1345f | 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
va009039 | 0:1ed23ab1345f | 24 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
va009039 | 0:1ed23ab1345f | 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
va009039 | 0:1ed23ab1345f | 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
va009039 | 0:1ed23ab1345f | 27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
va009039 | 0:1ed23ab1345f | 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
va009039 | 0:1ed23ab1345f | 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
va009039 | 0:1ed23ab1345f | 30 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
va009039 | 0:1ed23ab1345f | 31 | * SUCH DAMAGE. |
va009039 | 0:1ed23ab1345f | 32 | * |
va009039 | 0:1ed23ab1345f | 33 | * Please inquire about commercial licensing options at btstack@ringwald.ch |
va009039 | 0:1ed23ab1345f | 34 | * |
va009039 | 0:1ed23ab1345f | 35 | */ |
va009039 | 0:1ed23ab1345f | 36 | |
va009039 | 0:1ed23ab1345f | 37 | /* |
va009039 | 0:1ed23ab1345f | 38 | * hci_transport_usb.cpp |
va009039 | 0:1ed23ab1345f | 39 | * |
va009039 | 0:1ed23ab1345f | 40 | * HCI Transport API implementation for USB |
va009039 | 0:1ed23ab1345f | 41 | * |
va009039 | 0:1ed23ab1345f | 42 | * Created by Matthias Ringwald on 7/5/09. |
va009039 | 0:1ed23ab1345f | 43 | */ |
va009039 | 0:1ed23ab1345f | 44 | |
va009039 | 0:1ed23ab1345f | 45 | // delock bt class 2 - csr |
va009039 | 0:1ed23ab1345f | 46 | // 0a12:0001 (bus 27, device 2) |
va009039 | 0:1ed23ab1345f | 47 | |
va009039 | 0:1ed23ab1345f | 48 | // Interface Number - Alternate Setting - suggested Endpoint Address - Endpoint Type - Suggested Max Packet Size |
va009039 | 0:1ed23ab1345f | 49 | // HCI Commands 0 0 0x00 Control 8/16/32/64 |
va009039 | 0:1ed23ab1345f | 50 | // HCI Events 0 0 0x81 Interrupt (IN) 16 |
va009039 | 0:1ed23ab1345f | 51 | // ACL Data 0 0 0x82 Bulk (IN) 32/64 |
va009039 | 0:1ed23ab1345f | 52 | // ACL Data 0 0 0x02 Bulk (OUT) 32/64 |
va009039 | 0:1ed23ab1345f | 53 | |
va009039 | 0:1ed23ab1345f | 54 | #include <stdio.h> |
va009039 | 0:1ed23ab1345f | 55 | #include <string.h> |
va009039 | 0:1ed23ab1345f | 56 | #include "config.h" |
va009039 | 0:1ed23ab1345f | 57 | #include "debug.h" |
va009039 | 0:1ed23ab1345f | 58 | #include "hci.h" |
va009039 | 0:1ed23ab1345f | 59 | #include "hci_transport.h" |
va009039 | 0:1ed23ab1345f | 60 | #include "hci_dump.h" |
va009039 | 1:b657594559be | 61 | #include "USBHostBTstack.h" |
va009039 | 1:b657594559be | 62 | |
va009039 | 0:1ed23ab1345f | 63 | enum { |
va009039 | 0:1ed23ab1345f | 64 | LIB_USB_CLOSED = 0, |
va009039 | 0:1ed23ab1345f | 65 | LIB_USB_OPENED, |
va009039 | 0:1ed23ab1345f | 66 | LIB_USB_DEVICE_OPENDED, |
va009039 | 0:1ed23ab1345f | 67 | LIB_USB_KERNEL_DETACHED, |
va009039 | 0:1ed23ab1345f | 68 | LIB_USB_INTERFACE_CLAIMED, |
va009039 | 0:1ed23ab1345f | 69 | LIB_USB_TRANSFERS_ALLOCATED |
va009039 | 0:1ed23ab1345f | 70 | } libusb_state = LIB_USB_CLOSED; |
va009039 | 0:1ed23ab1345f | 71 | |
va009039 | 0:1ed23ab1345f | 72 | // single instance |
va009039 | 0:1ed23ab1345f | 73 | static hci_transport_t * hci_transport_usb = NULL; |
va009039 | 1:b657594559be | 74 | static USBHostBTstack* bt = NULL; |
va009039 | 0:1ed23ab1345f | 75 | |
va009039 | 2:871b41f4789e | 76 | static int usb_process_ds(struct data_source *ds) { |
va009039 | 2:871b41f4789e | 77 | if (bt) { |
va009039 | 2:871b41f4789e | 78 | bt->poll(); |
va009039 | 2:871b41f4789e | 79 | } |
va009039 | 2:871b41f4789e | 80 | return 0; |
va009039 | 2:871b41f4789e | 81 | } |
va009039 | 2:871b41f4789e | 82 | |
va009039 | 0:1ed23ab1345f | 83 | static int usb_open(void *transport_config){ |
va009039 | 0:1ed23ab1345f | 84 | log_info("usb_open\n"); |
va009039 | 2:871b41f4789e | 85 | data_source_t *ds = (data_source_t*)malloc(sizeof(data_source_t)); |
va009039 | 2:871b41f4789e | 86 | ds->process = usb_process_ds; |
va009039 | 2:871b41f4789e | 87 | run_loop_add_data_source(ds); |
va009039 | 1:b657594559be | 88 | if (bt) { |
va009039 | 1:b657594559be | 89 | return bt->open(); |
va009039 | 1:b657594559be | 90 | } |
va009039 | 0:1ed23ab1345f | 91 | return 0; |
va009039 | 0:1ed23ab1345f | 92 | } |
va009039 | 0:1ed23ab1345f | 93 | static int usb_close(void *transport_config){ |
va009039 | 0:1ed23ab1345f | 94 | |
va009039 | 0:1ed23ab1345f | 95 | return 0; |
va009039 | 0:1ed23ab1345f | 96 | } |
va009039 | 0:1ed23ab1345f | 97 | |
va009039 | 0:1ed23ab1345f | 98 | static int usb_send_packet(uint8_t packet_type, uint8_t * packet, int size){ |
va009039 | 0:1ed23ab1345f | 99 | //log_info("usb_send_packet\n"); |
va009039 | 0:1ed23ab1345f | 100 | if (bt) { |
va009039 | 0:1ed23ab1345f | 101 | bt->send_packet(packet_type, packet, size); |
va009039 | 0:1ed23ab1345f | 102 | } |
va009039 | 0:1ed23ab1345f | 103 | return 0; |
va009039 | 0:1ed23ab1345f | 104 | } |
va009039 | 0:1ed23ab1345f | 105 | |
va009039 | 0:1ed23ab1345f | 106 | static void usb_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ |
va009039 | 0:1ed23ab1345f | 107 | log_info("registering packet handler\n"); |
va009039 | 0:1ed23ab1345f | 108 | if (bt) { |
va009039 | 1:b657594559be | 109 | bt->register_packet_handler(handler); |
va009039 | 0:1ed23ab1345f | 110 | } |
va009039 | 0:1ed23ab1345f | 111 | } |
va009039 | 0:1ed23ab1345f | 112 | |
va009039 | 0:1ed23ab1345f | 113 | static const char * usb_get_transport_name(void){ |
va009039 | 0:1ed23ab1345f | 114 | return "USB"; |
va009039 | 0:1ed23ab1345f | 115 | } |
va009039 | 0:1ed23ab1345f | 116 | |
va009039 | 0:1ed23ab1345f | 117 | // get usb singleton |
va009039 | 0:1ed23ab1345f | 118 | hci_transport_t * hci_transport_usb_instance() { |
va009039 | 0:1ed23ab1345f | 119 | if (!bt) { |
va009039 | 1:b657594559be | 120 | bt = new USBHostBTstack; |
va009039 | 0:1ed23ab1345f | 121 | } |
va009039 | 0:1ed23ab1345f | 122 | if (!hci_transport_usb) { |
va009039 | 0:1ed23ab1345f | 123 | hci_transport_usb = (hci_transport_t*)malloc( sizeof(hci_transport_t)); |
va009039 | 0:1ed23ab1345f | 124 | hci_transport_usb->open = usb_open; |
va009039 | 0:1ed23ab1345f | 125 | hci_transport_usb->close = usb_close; |
va009039 | 0:1ed23ab1345f | 126 | hci_transport_usb->send_packet = usb_send_packet; |
va009039 | 0:1ed23ab1345f | 127 | hci_transport_usb->register_packet_handler = usb_register_packet_handler; |
va009039 | 0:1ed23ab1345f | 128 | hci_transport_usb->get_transport_name = usb_get_transport_name; |
va009039 | 0:1ed23ab1345f | 129 | hci_transport_usb->set_baudrate = NULL; |
va009039 | 0:1ed23ab1345f | 130 | hci_transport_usb->can_send_packet_now = NULL; |
va009039 | 0:1ed23ab1345f | 131 | } |
va009039 | 0:1ed23ab1345f | 132 | return hci_transport_usb; |
va009039 | 0:1ed23ab1345f | 133 | } |