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_dump.c
va009039 0:1ed23ab1345f 39 *
va009039 0:1ed23ab1345f 40 * Dump HCI trace in various formats:
va009039 0:1ed23ab1345f 41 *
va009039 0:1ed23ab1345f 42 * - BlueZ's hcidump format
va009039 0:1ed23ab1345f 43 * - Apple's PacketLogger
va009039 0:1ed23ab1345f 44 * - stdout hexdump
va009039 0:1ed23ab1345f 45 *
va009039 0:1ed23ab1345f 46 * Created by Matthias Ringwald on 5/26/09.
va009039 0:1ed23ab1345f 47 */
va009039 0:1ed23ab1345f 48
va009039 0:1ed23ab1345f 49 #include "config.h"
va009039 0:1ed23ab1345f 50
va009039 0:1ed23ab1345f 51 #include "hci_dump.h"
va009039 0:1ed23ab1345f 52 #include "hci.h"
va009039 0:1ed23ab1345f 53 #include "hci_transport.h"
va009039 0:1ed23ab1345f 54 #include <btstack/hci_cmds.h>
va009039 0:1ed23ab1345f 55
va009039 0:1ed23ab1345f 56 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 57 #include <fcntl.h> // open
va009039 0:1ed23ab1345f 58 #include <arpa/inet.h> // hton..
va009039 0:1ed23ab1345f 59 #include <unistd.h> // write
va009039 0:1ed23ab1345f 60 #include <stdio.h>
va009039 0:1ed23ab1345f 61 #include <time.h>
va009039 0:1ed23ab1345f 62 #include <sys/time.h> // for timestamps
va009039 0:1ed23ab1345f 63 #include <sys/stat.h> // for mode flags
va009039 0:1ed23ab1345f 64 #include <stdarg.h> // for va_list
va009039 0:1ed23ab1345f 65 #endif
va009039 0:1ed23ab1345f 66
va009039 0:1ed23ab1345f 67 // BLUEZ hcidump
va009039 0:1ed23ab1345f 68 typedef struct {
va009039 0:1ed23ab1345f 69 uint16_t len;
va009039 0:1ed23ab1345f 70 uint8_t in;
va009039 0:1ed23ab1345f 71 uint8_t pad;
va009039 0:1ed23ab1345f 72 uint32_t ts_sec;
va009039 0:1ed23ab1345f 73 uint32_t ts_usec;
va009039 0:1ed23ab1345f 74 uint8_t packet_type;
va009039 0:1ed23ab1345f 75 }
va009039 0:1ed23ab1345f 76 #ifdef __GNUC__
va009039 0:1ed23ab1345f 77 __attribute__ ((packed))
va009039 0:1ed23ab1345f 78 #endif
va009039 0:1ed23ab1345f 79 hcidump_hdr;
va009039 0:1ed23ab1345f 80
va009039 0:1ed23ab1345f 81 // APPLE PacketLogger
va009039 0:1ed23ab1345f 82 typedef struct {
va009039 0:1ed23ab1345f 83 uint32_t len;
va009039 0:1ed23ab1345f 84 uint32_t ts_sec;
va009039 0:1ed23ab1345f 85 uint32_t ts_usec;
va009039 0:1ed23ab1345f 86 uint8_t type; // 0xfc for note
va009039 0:1ed23ab1345f 87 }
va009039 0:1ed23ab1345f 88 #ifdef __GNUC__
va009039 0:1ed23ab1345f 89 __attribute__ ((packed))
va009039 0:1ed23ab1345f 90 #endif
va009039 0:1ed23ab1345f 91 pktlog_hdr;
va009039 0:1ed23ab1345f 92
va009039 0:1ed23ab1345f 93 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 94 static int dump_file = -1;
va009039 0:1ed23ab1345f 95 static int dump_format;
va009039 0:1ed23ab1345f 96 static hcidump_hdr header_bluez;
va009039 0:1ed23ab1345f 97 static pktlog_hdr header_packetlogger;
va009039 0:1ed23ab1345f 98 static char time_string[40];
va009039 0:1ed23ab1345f 99 static int max_nr_packets = -1;
va009039 0:1ed23ab1345f 100 static int nr_packets = 0;
va009039 0:1ed23ab1345f 101 static char log_message_buffer[256];
va009039 0:1ed23ab1345f 102 #endif
va009039 0:1ed23ab1345f 103
va009039 0:1ed23ab1345f 104 void hci_dump_open(char *filename, hci_dump_format_t format){
va009039 0:1ed23ab1345f 105 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 106 dump_format = format;
va009039 0:1ed23ab1345f 107 if (dump_format == HCI_DUMP_STDOUT) {
va009039 0:1ed23ab1345f 108 dump_file = fileno(stdout);
va009039 0:1ed23ab1345f 109 } else {
va009039 0:1ed23ab1345f 110 dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
va009039 0:1ed23ab1345f 111 }
va009039 0:1ed23ab1345f 112 #endif
va009039 0:1ed23ab1345f 113 }
va009039 0:1ed23ab1345f 114
va009039 0:1ed23ab1345f 115 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 116 void hci_dump_set_max_packets(int packets){
va009039 0:1ed23ab1345f 117 max_nr_packets = packets;
va009039 0:1ed23ab1345f 118 }
va009039 0:1ed23ab1345f 119 #endif
va009039 0:1ed23ab1345f 120
va009039 0:1ed23ab1345f 121 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
va009039 0:1ed23ab1345f 122 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 123
va009039 0:1ed23ab1345f 124 if (dump_file < 0) return; // not activated yet
va009039 0:1ed23ab1345f 125
va009039 0:1ed23ab1345f 126 // don't grow bigger than max_nr_packets
va009039 0:1ed23ab1345f 127 if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
va009039 0:1ed23ab1345f 128 if (nr_packets >= max_nr_packets){
va009039 0:1ed23ab1345f 129 lseek(dump_file, 0, SEEK_SET);
va009039 0:1ed23ab1345f 130 ftruncate(dump_file, 0);
va009039 0:1ed23ab1345f 131 nr_packets = 0;
va009039 0:1ed23ab1345f 132 }
va009039 0:1ed23ab1345f 133 nr_packets++;
va009039 0:1ed23ab1345f 134 }
va009039 0:1ed23ab1345f 135
va009039 0:1ed23ab1345f 136 // get time
va009039 0:1ed23ab1345f 137 struct timeval curr_time;
va009039 0:1ed23ab1345f 138 struct tm* ptm;
va009039 0:1ed23ab1345f 139 gettimeofday(&curr_time, NULL);
va009039 0:1ed23ab1345f 140
va009039 0:1ed23ab1345f 141 switch (dump_format){
va009039 0:1ed23ab1345f 142 case HCI_DUMP_STDOUT: {
va009039 0:1ed23ab1345f 143 /* Obtain the time of day, and convert it to a tm struct. */
va009039 0:1ed23ab1345f 144 ptm = localtime (&curr_time.tv_sec);
va009039 0:1ed23ab1345f 145 /* Format the date and time, down to a single second. */
va009039 0:1ed23ab1345f 146 strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
va009039 0:1ed23ab1345f 147 /* Compute milliseconds from microseconds. */
va009039 0:1ed23ab1345f 148 uint16_t milliseconds = curr_time.tv_usec / 1000;
va009039 0:1ed23ab1345f 149 /* Print the formatted time, in seconds, followed by a decimal point
va009039 0:1ed23ab1345f 150 and the milliseconds. */
va009039 0:1ed23ab1345f 151 printf ("%s.%03u] ", time_string, milliseconds);
va009039 0:1ed23ab1345f 152 switch (packet_type){
va009039 0:1ed23ab1345f 153 case HCI_COMMAND_DATA_PACKET:
va009039 0:1ed23ab1345f 154 printf("CMD => ");
va009039 0:1ed23ab1345f 155 break;
va009039 0:1ed23ab1345f 156 case HCI_EVENT_PACKET:
va009039 0:1ed23ab1345f 157 printf("EVT <= ");
va009039 0:1ed23ab1345f 158 break;
va009039 0:1ed23ab1345f 159 case HCI_ACL_DATA_PACKET:
va009039 0:1ed23ab1345f 160 if (in) {
va009039 0:1ed23ab1345f 161 printf("ACL <= ");
va009039 0:1ed23ab1345f 162 } else {
va009039 0:1ed23ab1345f 163 printf("ACL => ");
va009039 0:1ed23ab1345f 164 }
va009039 0:1ed23ab1345f 165 break;
va009039 0:1ed23ab1345f 166 case LOG_MESSAGE_PACKET:
va009039 0:1ed23ab1345f 167 // assume buffer is big enough
va009039 0:1ed23ab1345f 168 packet[len] = 0;
va009039 0:1ed23ab1345f 169 printf("LOG -- %s\n", (char*) packet);
va009039 0:1ed23ab1345f 170 return;
va009039 0:1ed23ab1345f 171 default:
va009039 0:1ed23ab1345f 172 return;
va009039 0:1ed23ab1345f 173 }
va009039 0:1ed23ab1345f 174 hexdump(packet, len);
va009039 0:1ed23ab1345f 175 break;
va009039 0:1ed23ab1345f 176 }
va009039 0:1ed23ab1345f 177
va009039 0:1ed23ab1345f 178 case HCI_DUMP_BLUEZ:
va009039 0:1ed23ab1345f 179 bt_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len);
va009039 0:1ed23ab1345f 180 header_bluez.in = in;
va009039 0:1ed23ab1345f 181 header_bluez.pad = 0;
va009039 0:1ed23ab1345f 182 bt_store_32( (uint8_t *) &header_bluez.ts_sec, 0, curr_time.tv_sec);
va009039 0:1ed23ab1345f 183 bt_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec);
va009039 0:1ed23ab1345f 184 header_bluez.packet_type = packet_type;
va009039 0:1ed23ab1345f 185 write (dump_file, &header_bluez, sizeof(hcidump_hdr) );
va009039 0:1ed23ab1345f 186 write (dump_file, packet, len );
va009039 0:1ed23ab1345f 187 break;
va009039 0:1ed23ab1345f 188
va009039 0:1ed23ab1345f 189 case HCI_DUMP_PACKETLOGGER:
va009039 0:1ed23ab1345f 190 header_packetlogger.len = htonl( sizeof(pktlog_hdr) - 4 + len);
va009039 0:1ed23ab1345f 191 header_packetlogger.ts_sec = htonl(curr_time.tv_sec);
va009039 0:1ed23ab1345f 192 header_packetlogger.ts_usec = htonl(curr_time.tv_usec);
va009039 0:1ed23ab1345f 193 switch (packet_type){
va009039 0:1ed23ab1345f 194 case HCI_COMMAND_DATA_PACKET:
va009039 0:1ed23ab1345f 195 header_packetlogger.type = 0x00;
va009039 0:1ed23ab1345f 196 break;
va009039 0:1ed23ab1345f 197 case HCI_ACL_DATA_PACKET:
va009039 0:1ed23ab1345f 198 if (in) {
va009039 0:1ed23ab1345f 199 header_packetlogger.type = 0x03;
va009039 0:1ed23ab1345f 200 } else {
va009039 0:1ed23ab1345f 201 header_packetlogger.type = 0x02;
va009039 0:1ed23ab1345f 202 }
va009039 0:1ed23ab1345f 203 break;
va009039 0:1ed23ab1345f 204 case HCI_EVENT_PACKET:
va009039 0:1ed23ab1345f 205 header_packetlogger.type = 0x01;
va009039 0:1ed23ab1345f 206 break;
va009039 0:1ed23ab1345f 207 case LOG_MESSAGE_PACKET:
va009039 0:1ed23ab1345f 208 header_packetlogger.type = 0xfc;
va009039 0:1ed23ab1345f 209 break;
va009039 0:1ed23ab1345f 210 default:
va009039 0:1ed23ab1345f 211 return;
va009039 0:1ed23ab1345f 212 }
va009039 0:1ed23ab1345f 213 write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) );
va009039 0:1ed23ab1345f 214 write (dump_file, packet, len );
va009039 0:1ed23ab1345f 215 break;
va009039 0:1ed23ab1345f 216
va009039 0:1ed23ab1345f 217 default:
va009039 0:1ed23ab1345f 218 break;
va009039 0:1ed23ab1345f 219 }
va009039 0:1ed23ab1345f 220 #endif
va009039 0:1ed23ab1345f 221 }
va009039 0:1ed23ab1345f 222
va009039 0:1ed23ab1345f 223 void hci_dump_log(const char * format, ...){
va009039 0:1ed23ab1345f 224 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 225 va_list argptr;
va009039 0:1ed23ab1345f 226 va_start(argptr, format);
va009039 0:1ed23ab1345f 227 int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
va009039 0:1ed23ab1345f 228 hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
va009039 0:1ed23ab1345f 229 va_end(argptr);
va009039 0:1ed23ab1345f 230 #endif
va009039 0:1ed23ab1345f 231 }
va009039 0:1ed23ab1345f 232
va009039 0:1ed23ab1345f 233 void hci_dump_close(){
va009039 0:1ed23ab1345f 234 #ifndef EMBEDDED
va009039 0:1ed23ab1345f 235 close(dump_file);
va009039 0:1ed23ab1345f 236 dump_file = -1;
va009039 0:1ed23ab1345f 237 #endif
va009039 0:1ed23ab1345f 238 }
va009039 0:1ed23ab1345f 239