BLE demo for mbed Ported RunningElectronics's SBDBT firmware for BLE. It can communicate with iOS

Dependencies:   FatFileSystem mbed

Fork of BTstack by Norimasa Okamoto

Committer:
todotani
Date:
Wed Feb 20 14:18:38 2013 +0000
Revision:
6:cf06ba884429
Parent:
0:1ed23ab1345f
Change tick timer to 1ms. Change attribute 0xFFF1 as read of DigitalIn p5

Who changed what in which revision?

UserRevisionLine numberNew 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 0:1ed23ab1345f 61 #include "usbbt.h"
va009039 0:1ed23ab1345f 62 // prototypes
va009039 0:1ed23ab1345f 63 static int usb_close(void *transport_config);
va009039 0:1ed23ab1345f 64
va009039 0:1ed23ab1345f 65 enum {
va009039 0:1ed23ab1345f 66 LIB_USB_CLOSED = 0,
va009039 0:1ed23ab1345f 67 LIB_USB_OPENED,
va009039 0:1ed23ab1345f 68 LIB_USB_DEVICE_OPENDED,
va009039 0:1ed23ab1345f 69 LIB_USB_KERNEL_DETACHED,
va009039 0:1ed23ab1345f 70 LIB_USB_INTERFACE_CLAIMED,
va009039 0:1ed23ab1345f 71 LIB_USB_TRANSFERS_ALLOCATED
va009039 0:1ed23ab1345f 72 } libusb_state = LIB_USB_CLOSED;
va009039 0:1ed23ab1345f 73
va009039 0:1ed23ab1345f 74 // single instance
va009039 0:1ed23ab1345f 75 static hci_transport_t * hci_transport_usb = NULL;
va009039 0:1ed23ab1345f 76 static usbbt* bt = NULL;
va009039 0:1ed23ab1345f 77 static int usb_process_ds(struct data_source *ds) {
va009039 0:1ed23ab1345f 78 if (bt) {
va009039 0:1ed23ab1345f 79 bt->poll();
va009039 0:1ed23ab1345f 80 }
va009039 0:1ed23ab1345f 81 return 0;
va009039 0:1ed23ab1345f 82 }
va009039 0:1ed23ab1345f 83
va009039 0:1ed23ab1345f 84 static int usb_open(void *transport_config){
va009039 0:1ed23ab1345f 85 log_info("usb_open\n");
va009039 0:1ed23ab1345f 86 data_source_t *ds = (data_source_t*)malloc(sizeof(data_source_t));
va009039 0:1ed23ab1345f 87 ds->process = usb_process_ds;
va009039 0:1ed23ab1345f 88 run_loop_add_data_source(ds);
va009039 0:1ed23ab1345f 89 return 0;
va009039 0:1ed23ab1345f 90 }
va009039 0:1ed23ab1345f 91 static int usb_close(void *transport_config){
va009039 0:1ed23ab1345f 92
va009039 0:1ed23ab1345f 93 return 0;
va009039 0:1ed23ab1345f 94 }
va009039 0:1ed23ab1345f 95
va009039 0:1ed23ab1345f 96 static int usb_send_packet(uint8_t packet_type, uint8_t * packet, int size){
va009039 0:1ed23ab1345f 97 //log_info("usb_send_packet\n");
va009039 0:1ed23ab1345f 98 if (bt) {
va009039 0:1ed23ab1345f 99 bt->send_packet(packet_type, packet, size);
va009039 0:1ed23ab1345f 100 }
va009039 0:1ed23ab1345f 101 return 0;
va009039 0:1ed23ab1345f 102 }
va009039 0:1ed23ab1345f 103
va009039 0:1ed23ab1345f 104 static void usb_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
va009039 0:1ed23ab1345f 105 log_info("registering packet handler\n");
va009039 0:1ed23ab1345f 106 if (bt) {
va009039 0:1ed23ab1345f 107 bt->setOnPacket(handler);
va009039 0:1ed23ab1345f 108 }
va009039 0:1ed23ab1345f 109 }
va009039 0:1ed23ab1345f 110
va009039 0:1ed23ab1345f 111 static const char * usb_get_transport_name(void){
va009039 0:1ed23ab1345f 112 return "USB";
va009039 0:1ed23ab1345f 113 }
va009039 0:1ed23ab1345f 114
va009039 0:1ed23ab1345f 115 // get usb singleton
va009039 0:1ed23ab1345f 116 hci_transport_t * hci_transport_usb_instance() {
va009039 0:1ed23ab1345f 117 if (!bt) {
va009039 0:1ed23ab1345f 118 bt = new usbbt;
va009039 0:1ed23ab1345f 119 bt->setup();
va009039 0:1ed23ab1345f 120 }
va009039 0:1ed23ab1345f 121 if (!hci_transport_usb) {
va009039 0:1ed23ab1345f 122 hci_transport_usb = (hci_transport_t*)malloc( sizeof(hci_transport_t));
va009039 0:1ed23ab1345f 123 hci_transport_usb->open = usb_open;
va009039 0:1ed23ab1345f 124 hci_transport_usb->close = usb_close;
va009039 0:1ed23ab1345f 125 hci_transport_usb->send_packet = usb_send_packet;
va009039 0:1ed23ab1345f 126 hci_transport_usb->register_packet_handler = usb_register_packet_handler;
va009039 0:1ed23ab1345f 127 hci_transport_usb->get_transport_name = usb_get_transport_name;
va009039 0:1ed23ab1345f 128 hci_transport_usb->set_baudrate = NULL;
va009039 0:1ed23ab1345f 129 hci_transport_usb->can_send_packet_now = NULL;
va009039 0:1ed23ab1345f 130 }
va009039 0:1ed23ab1345f 131 return hci_transport_usb;
va009039 0:1ed23ab1345f 132 }