BTstack Bluetooth stack

Dependencies:   mbed USBHost

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hci_transport_usb.cpp Source File

hci_transport_usb.cpp

00001 /*
00002  * Copyright (C) 2009-2012 by Matthias Ringwald
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  * 4. Any redistribution, use, or modification is done solely for
00017  *    personal benefit and not for any commercial purpose or for
00018  *    monetary gain.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
00024  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * Please inquire about commercial licensing options at btstack@ringwald.ch
00034  *
00035  */
00036 
00037 /*
00038  *  hci_transport_usb.cpp
00039  *
00040  *  HCI Transport API implementation for USB
00041  *
00042  *  Created by Matthias Ringwald on 7/5/09.
00043  */
00044 
00045 // delock bt class 2 - csr
00046 // 0a12:0001 (bus 27, device 2)
00047 
00048 // Interface Number - Alternate Setting - suggested Endpoint Address - Endpoint Type - Suggested Max Packet Size 
00049 // HCI Commands 0 0 0x00 Control 8/16/32/64 
00050 // HCI Events   0 0 0x81 Interrupt (IN) 16 
00051 // ACL Data     0 0 0x82 Bulk (IN) 32/64 
00052 // ACL Data     0 0 0x02 Bulk (OUT) 32/64 
00053 
00054 #include <stdio.h>
00055 #include <string.h>
00056 #include "config.h"
00057 #include "debug.h"
00058 #include "hci.h"
00059 #include "hci_transport.h"
00060 #include "hci_dump.h"
00061 #include "USBHostBTstack.h"
00062 
00063 enum {
00064     LIB_USB_CLOSED = 0,
00065     LIB_USB_OPENED,
00066     LIB_USB_DEVICE_OPENDED,
00067     LIB_USB_KERNEL_DETACHED,
00068     LIB_USB_INTERFACE_CLAIMED,
00069     LIB_USB_TRANSFERS_ALLOCATED
00070 } libusb_state = LIB_USB_CLOSED;
00071 
00072 // single instance
00073 static hci_transport_t * hci_transport_usb = NULL;
00074 static USBHostBTstack* bt = NULL;
00075 
00076 static int usb_process_ds(struct data_source *ds) {
00077     if (bt) {
00078         bt->poll();
00079     }
00080     return 0;
00081 }
00082 
00083 static int usb_open(void *transport_config){
00084     log_info("usb_open\n");
00085     data_source_t *ds = (data_source_t*)malloc(sizeof(data_source_t));
00086     ds->process = usb_process_ds;
00087     run_loop_add_data_source(ds);
00088     if (bt) {
00089         return bt->open();
00090     }
00091     return 0;
00092 }
00093 static int usb_close(void *transport_config){
00094 
00095     return 0;
00096 }
00097 
00098 static int usb_send_packet(uint8_t packet_type, uint8_t * packet, int size){
00099     //log_info("usb_send_packet\n");
00100     if (bt) {
00101         bt->send_packet(packet_type, packet, size);
00102     }
00103     return 0;
00104 }
00105 
00106 static void usb_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
00107     log_info("registering packet handler\n");
00108     if (bt) {
00109         bt->register_packet_handler(handler);
00110     }
00111 }
00112 
00113 static const char * usb_get_transport_name(void){
00114     return "USB";
00115 }
00116 
00117 // get usb singleton
00118 hci_transport_t * hci_transport_usb_instance() {
00119     if (!bt) {
00120         bt = new USBHostBTstack;
00121     }
00122     if (!hci_transport_usb) {
00123         hci_transport_usb = (hci_transport_t*)malloc( sizeof(hci_transport_t));
00124         hci_transport_usb->open                          = usb_open;
00125         hci_transport_usb->close                         = usb_close;
00126         hci_transport_usb->send_packet                   = usb_send_packet;
00127         hci_transport_usb->register_packet_handler       = usb_register_packet_handler;
00128         hci_transport_usb->get_transport_name            = usb_get_transport_name;
00129         hci_transport_usb->set_baudrate                  = NULL;
00130         hci_transport_usb->can_send_packet_now           = NULL;
00131     }
00132     return hci_transport_usb;
00133 }