hci_dump: full rewrite, provide platform-specific implementations for posix and embedded platforms

This commit is contained in:
Matthias Ringwald 2021-01-12 18:26:44 +01:00
parent 98451c7b10
commit 128d6c999a
12 changed files with 1156 additions and 350 deletions

View File

@ -0,0 +1,134 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hci_dump_embedded_stdout.c"
/*
* Dump HCI trace on stdout
*/
#include "btstack_config.h"
#include "hci_dump_embedded_stdout.h"
#include "btstack_run_loop.h"
#include "hci.h"
#include <stdio.h>
#ifndef ENABLE_PRINTF_HEXDUMP
#error "HCI Dump on stdout requires ENABLE_PRINTF_HEXDUMP to be defined. Use different hci dump implementation or add ENABLE_PRINTF_HEXDUMP to btstack_config.h"
#endif
static char log_message_buffer[256];
static void hci_dump_embedded_stdout_timestamp(void){
uint32_t time_ms = btstack_run_loop_get_time_ms();
int seconds = time_ms / 1000u;
int minutes = seconds / 60;
unsigned int hours = minutes / 60;
uint16_t p_ms = time_ms - (seconds * 1000u);
uint16_t p_seconds = seconds - (minutes * 60);
uint16_t p_minutes = minutes - (hours * 60u);
printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
}
static void hci_dump_embedded_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
switch (packet_type){
case HCI_COMMAND_DATA_PACKET:
printf("CMD => ");
break;
case HCI_EVENT_PACKET:
printf("EVT <= ");
break;
case HCI_ACL_DATA_PACKET:
if (in) {
printf("ACL <= ");
} else {
printf("ACL => ");
}
break;
case HCI_SCO_DATA_PACKET:
if (in) {
printf("SCO <= ");
} else {
printf("SCO => ");
}
break;
case LOG_MESSAGE_PACKET:
printf("LOG -- %s\n", (char*) packet);
return;
default:
return;
}
printf_hexdump(packet, len);
}
static void hci_dump_embedded_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
hci_dump_embedded_stdout_timestamp();
hci_dump_embedded_stdout_packet(packet_type, in, packet, len);
}
static void hci_dump_embedded_stdout_log_message(const char * format, va_list argptr){
int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
hci_dump_embedded_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
}
#ifdef __AVR__
void hci_dump_embedded_stdout_log_message_P(int log_level, PGM_P * format, va_list argptr){
hci_dump_embedded_stdout_timestamp();
printf_P(PSTR("LOG -- "));
vfprintf_P(stdout, format, argptr);
printf_P(PSTR("\n"));
}
#endif
const hci_dump_t * hci_dump_embedded_stdout_get_instance(void){
static const hci_dump_t hci_dump_instance = {
// void (*reset)(void);
NULL,
// void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
&hci_dump_embedded_stdout_log_packet,
// void (*log_message)(int log_level, const char * format, va_list argptr);
&hci_dump_embedded_stdout_log_message,
#ifdef __AVR__ \
// void (*log_message_P)(int log_level, PGM_P * format, va_list argptr);
&hci_dump_embedded_stdout_log_message_P,
#endif
};
return &hci_dump_instance;
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* Dump HCI trace on stdout
*/
#ifndef HCI_DUMP_EMBEDDED_STDOUT_H
#define HCI_DUMP_EMBEDDED_STDOUT_H
#include <stdint.h>
#include <stdarg.h> // for va_list
#include "hci_dump.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Get HCI Dump Embedded STDOUT Instance
* @return hci_dump_impl
*/
const hci_dump_t * hci_dump_embedded_stdout_get_instance(void);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // HCI_DUMP_EMBEDDED_STDOUT_H

View File

@ -0,0 +1,146 @@
/*
* Copyright (C) 2014-2020 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hci_dump_segger_rtt_binary.c"
/**
* Dump HCI trace on SEGGER RTT Logging Channel #1
*/
#include "btstack_config.h"
#include "hci_dump_segger_rtt_binary.h"
#include "btstack_debug.h"
#include "hci.h"
#include "SEGGER_RTT.h"
// allow to configure mode, channel, up buffer size in btstack_config.h
#ifndef SEGGER_RTT_PACKETLOG_MODE
#define SEGGER_RTT_PACKETLOG_MODE SEGGER_RTT_MODE_DEFAULT
#endif
#ifndef SEGGER_RTT_PACKETLOG_BUFFER_SIZE
#define SEGGER_RTT_PACKETLOG_BUFFER_SIZE 1024
#endif
#ifndef SEGGER_RTT_PACKETLOG_CHANNEL
#define SEGGER_RTT_PACKETLOG_CHANNEL 1
#endif
static char segger_rtt_packetlog_buffer[SEGGER_RTT_PACKETLOG_BUFFER_SIZE];
static int dump_format;
static char log_message_buffer[256];
static void hci_dump_segger_rtt_binary_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
if (dump_format == HCI_DUMP_INVALID) return;
static union {
uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
} header;
uint32_t time_ms = btstack_run_loop_get_time_ms();
uint32_t tv_sec = time_ms / 1000u;
uint32_t tv_us = (time_ms - (tv_sec * 1000)) * 1000;
// Saturday, January 1, 2000 12:00:00
tv_sec += 946728000UL;
#if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP)
static const char rtt_warning[] = "RTT buffer full - packet(s) skipped";
static bool rtt_packet_skipped = false;
if (rtt_packet_skipped){
// try to write warning log message
rtt_packet_skipped = false;
packet_type = LOG_MESSAGE_PACKET;
packet = (uint8_t *) &rtt_warning[0];
len = sizeof(rtt_warning)-1;
}
#endif
uint16_t header_len = 0;
switch (dump_format){
case HCI_DUMP_BLUEZ:
hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
break;
case HCI_DUMP_PACKETLOGGER:
hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
break;
default:
return;
}
#if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP)
// check available space in buffer to avoid writing header but not packet
unsigned space_free = SEGGER_RTT_GetAvailWriteSpace(SEGGER_RTT_PACKETLOG_CHANNEL);
if ((header_len + len) > space_free) {
rtt_packet_skipped = true;
return;
}
#endif
// write complete header and payload
SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, &header, header_len);
SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, packet, len);
}
static void hci_dump_segger_rtt_binary_log_message(const char * format, va_list argptr){
if (dump_format == HCI_DUMP_INVALID) return;
int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
hci_dump_segger_rtt_binary_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
}
//
void hci_dump_segger_rtt_binary_open(hci_dump_format_t format){
btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER);
dump_format = format;
SEGGER_RTT_ConfigUpBuffer(SEGGER_RTT_PACKETLOG_CHANNEL, "hci_dump", &segger_rtt_packetlog_buffer[0], SEGGER_RTT_PACKETLOG_BUFFER_SIZE, SEGGER_RTT_PACKETLOG_MODE);
}
const hci_dump_t * hci_dump_segger_rtt_binary_get_instance(void){
static const hci_dump_t hci_dump_instance = {
// void (*reset)(void);
NULL,
// void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
&hci_dump_segger_rtt_binary_log_packet,
// void (*log_message)(int log_level, const char * format, va_list argptr);
&hci_dump_segger_rtt_binary_log_message,
};
return &hci_dump_instance;
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* Dump HCI trace on SEGGER RTT Logging Channel #1
*/
#ifndef HCI_DUMP_SEGGER_RTT_BINARY_H
#define HCI_DUMP_SEGGER_RTT_BINARY_H
#include <stdint.h>
#include <stdarg.h> // for va_list
#include "hci_dump.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Get HCI Dump SEGGER RTT Binary Instance
* @return hci_dump_impl
*/
const hci_dump_t * hci_dump_segger_rtt_binary_get_instance(void);
/**
* @brief Open Log Channel
* @param format
*/
void hci_dump_segger_rtt_binary_open(hci_dump_format_t format);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // HCI_DUMP_SEGGER_RTT_STDOUT_H

View File

@ -0,0 +1,132 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hci_dump_embedded_stdout.c"
/*
* Dump HCI trace on stdout
*/
#include "hci_dump.h"
#include "btstack_config.h"
#include "btstack_util.h"
#include "hci.h"
#include <stdio.h>
#include "SEGGER_RTT.h"
static void hci_dump_segger_rtt_hexdump(const void *data, int size){
char buffer[4];
buffer[2] = ' ';
buffer[3] = 0;
const uint8_t * ptr = (const uint8_t *) data;
while (size > 0){
uint8_t byte = *ptr++;
buffer[0] = char_for_nibble(byte >> 4);
buffer[1] = char_for_nibble(byte & 0x0f);
SEGGER_RTT_Write(0, buffer, 3);
size--;
}
SEGGER_RTT_PutChar(0, '\n');
}
static void hci_dump_segger_rtt_stdout_timestamp(void){
uint32_t time_ms = btstack_run_loop_get_time_ms();
int seconds = time_ms / 1000u;
int minutes = seconds / 60;
unsigned int hours = minutes / 60;
uint16_t p_ms = time_ms - (seconds * 1000u);
uint16_t p_seconds = seconds - (minutes * 60);
uint16_t p_minutes = minutes - (hours * 60u);
SEGGER_RTT_printf(0, "[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
}
static void hci_dump_segger_rtt_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
switch (packet_type){
case HCI_COMMAND_DATA_PACKET:
SEGGER_RTT_printf(0, "CMD => ");
break;
case HCI_EVENT_PACKET:
SEGGER_RTT_printf(0, "EVT <= ");
break;
case HCI_ACL_DATA_PACKET:
if (in) {
SEGGER_RTT_printf(0, "ACL <= ");
} else {
SEGGER_RTT_printf(0, "ACL => ");
}
break;
case HCI_SCO_DATA_PACKET:
if (in) {
SEGGER_RTT_printf(0, "SCO <= ");
} else {
SEGGER_RTT_printf(0, "SCO => ");
}
break;
case LOG_MESSAGE_PACKET:
SEGGER_RTT_printf(0, "LOG -- %s\n", (char*) packet);
return;
default:
return;
}
hci_dump_segger_rtt_hexdump(packet, len);
}
static void hci_dump_segger_rtt_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
hci_dump_segger_rtt_stdout_timestamp();
hci_dump_segger_rtt_stdout_packet(packet_type, in, packet, len);
}
static void hci_dump_segger_rtt_stdout_log_message(const char * format, va_list argptr){
hci_dump_segger_rtt_stdout_timestamp();
SEGGER_RTT_printf(0, "LOG -- ");
SEGGER_RTT_vprintf(0, format, &argptr);
SEGGER_RTT_printf(0, "\n");
}
const hci_dump_t * hci_dump_segger_rtt_stdout_get_instance(void){
static const hci_dump_t hci_dump_instance = {
// void (*reset)(void);
NULL,
// void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
&hci_dump_segger_rtt_stdout_log_packet,
// void (*log_message)(int log_level, const char * format, va_list argptr);
&hci_dump_segger_rtt_stdout_log_message,
};
return &hci_dump_instance;
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* Dump HCI trace on SEGGER RTT Terminal (channel 0)
*/
#ifndef HCI_DUMP_SEGGER_RTT_STDOUT_H
#define HCI_DUMP_SEGGER_RTT_STDOUT_H
#include <stdint.h>
#include <stdarg.h> // for va_list
#include "hci_dump.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Get HCI Dump SEGGER RTT STDOUT Instance
* @return hci_dump_impl
*/
const hci_dump_t * hci_dump_segger_rtt_stdout_get_instance(void);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // HCI_DUMP_SEGGER_RTT_STDOUT_H

View File

@ -0,0 +1,152 @@
/*
* Copyright (C) 2014-2020 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hci_dump_posix_fs.c"
/*
* hci_dump_posix_fs.c
*
* Dump HCI trace in various formats into a file:
*
* - BlueZ's hcidump format
* - Apple's PacketLogger
* - stdout hexdump
*
*/
#include "btstack_config.h"
// enable POSIX functions (needed for -std=c99)
#define _POSIX_C_SOURCE 200809
#include "hci_dump_posix_fs.h"
#include "btstack_debug.h"
#include "hci_cmd.h"
#include <time.h>
#include <stdio.h> // printf
#include <fcntl.h> // open
#include <unistd.h> // write
#include <errno.h> // errno
#include <sys/time.h> // for timestamps
#include <sys/stat.h> // file modes
static int dump_file = -1;
static int dump_format;
static char log_message_buffer[256];
static void hci_dump_posix_fs_reset(void){
btstack_assert(dump_file >= 0);
(void) lseek(dump_file, 0, SEEK_SET);
(void) ftruncate(dump_file, 0);
}
static void hci_dump_posix_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
if (dump_file < 0) return;
static union {
uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
} header;
uint32_t tv_sec = 0;
uint32_t tv_us = 0;
// get time
struct timeval curr_time;
gettimeofday(&curr_time, NULL);
tv_sec = curr_time.tv_sec;
tv_us = curr_time.tv_usec;
uint16_t header_len = 0;
switch (dump_format){
case HCI_DUMP_BLUEZ:
hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
break;
case HCI_DUMP_PACKETLOGGER:
hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
break;
default:
return;
}
(void) write(dump_file, &header, header_len);
(void) write(dump_file, packet, len );
}
static void hci_dump_posix_fs_log_message(const char * format, va_list argptr){
if (dump_file < 0) return;
int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
hci_dump_posix_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
}
// returns system errno
int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format){
btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER);
dump_format = format;
int oflags = O_WRONLY | O_CREAT | O_TRUNC;
#ifdef _WIN32
oflags |= O_BINARY;
#endif
dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
if (dump_file < 0){
printf("failed to open file %s, errno = %d\n", filename, errno);
return errno;
}
return 0;
}
void hci_dump_posix_fs_close(void){
close(dump_file);
dump_file = -1;
}
const hci_dump_t * hci_dump_posix_fs_get_instance(void){
static const hci_dump_t hci_dump_instance = {
// void (*reset)(void);
&hci_dump_posix_fs_reset,
// void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
&hci_dump_posix_fs_log_packet,
// void (*log_message)(int log_level, const char * format, va_list argptr);
&hci_dump_posix_fs_log_message,
};
return &hci_dump_instance;
}

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* Dump HCI trace in binary formats like PacketLogger and BlueZ (hcidump) into file
*/
#ifndef HCI_DUMP_POSIX_FS_H
#define HCI_DUMP_POSIX_FS_H
#include <stdint.h>
#include <stdarg.h> // for va_list
#include "hci_dump.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Get HCI Dump POSIX FS Instance
* @return hci_dump_impl
*/
const hci_dump_t * hci_dump_posix_fs_get_instance(void);
/*
* @brief Open Log file
* @param filename or path
* @param format
* @returns 0 if ok, errno otherwise
*/
int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format);
/*
* @brief Close Log file
*/
void hci_dump_posix_fs_close(void);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // HCI_DUMP_POSIX_FS_H

View File

@ -0,0 +1,127 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hci_dump_posix_stdout.c"
/*
* Dump HCI trace on stdout
*/
#include "hci_dump.h"
#include "btstack_config.h"
#include "hci.h"
#include <time.h>
#include <sys/time.h> // for timestamps
#include "hci_cmd.h"
#include <stdio.h>
#ifndef ENABLE_PRINTF_HEXDUMP
#error "HCI Dump on stdout requires ENABLE_PRINTF_HEXDUMP to be defined. Use different hci dump implementation or add ENABLE_PRINTF_HEXDUMP to btstack_config.h"
#endif
static char time_string[40];
static char log_message_buffer[256];
static void hci_dump_posix_stdout_timestamp(void){
struct tm* ptm;
struct timeval curr_time;
gettimeofday(&curr_time, NULL);
time_t curr_time_secs = curr_time.tv_sec;
/* Obtain the time of day, and convert it to a tm struct. */
ptm = localtime (&curr_time_secs);
/* assert localtime was successful */
if (!ptm) return;
/* Format the date and time, down to a single second. */
strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
/* Compute milliseconds from microseconds. */
uint16_t milliseconds = curr_time.tv_usec / 1000;
/* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */
printf ("%s.%03u] ", time_string, milliseconds);
}
static void hci_dump_posix_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
switch (packet_type){
case HCI_COMMAND_DATA_PACKET:
printf("CMD => ");
break;
case HCI_EVENT_PACKET:
printf("EVT <= ");
break;
case HCI_ACL_DATA_PACKET:
if (in) {
printf("ACL <= ");
} else {
printf("ACL => ");
}
break;
case HCI_SCO_DATA_PACKET:
if (in) {
printf("SCO <= ");
} else {
printf("SCO => ");
}
break;
case LOG_MESSAGE_PACKET:
printf("LOG -- %s\n", (char*) packet);
return;
default:
return;
}
printf_hexdump(packet, len);
}
static void hci_dump_posix_posix_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
hci_dump_posix_stdout_timestamp();
hci_dump_posix_stdout_packet(packet_type, in, packet, len);
}
static void hci_dump_posix_stdout_log_message(const char * format, va_list argptr){
int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
hci_dump_posix_posix_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
}
const hci_dump_t * hci_dump_posix_stdout_get_instance(void){
static const hci_dump_t hci_dump_instance = {
// void (*reset)(void);
NULL,
// void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
&hci_dump_posix_posix_stdout_log_packet,
// void (*log_message)(int log_level, const char * format, va_list argptr);
&hci_dump_posix_stdout_log_message,
};
return &hci_dump_instance;
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* Dump HCI trace on stdout
*/
#ifndef HCI_DUMP_POSIX_STDOUT_H
#define HCI_DUMP_POSIX_STDOUT_H
#include <stdint.h>
#include <stdarg.h> // for va_list
#include "hci_dump.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Get HCI Dump POSIX STDOUT Instance
* @return hci_dump_impl
*/
const hci_dump_t * hci_dump_posix_stdout_get_instance(void);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // HCI_DUMP_POSIX_STDOUT_H

View File

@ -40,134 +40,78 @@
/*
* hci_dump.c
*
* Dump HCI trace in various formats:
*
* - BlueZ's hcidump format
* - Apple's PacketLogger
* - stdout hexdump
*
* Dump HCI trace in various formats based on platform-specific implementation
*/
#include "btstack_config.h"
#include "btstack_debug.h"
#include "btstack_bool.h"
#include "btstack_util.h"
// enable POSIX functions (needed for -std=c99)
#define _POSIX_C_SOURCE 200809
#include "hci_dump.h"
#include "hci.h"
#include "hci_transport.h"
#include "hci_cmd.h"
#include "btstack_run_loop.h"
#include <stdio.h>
#ifdef HAVE_POSIX_FILE_IO
#include <fcntl.h> // open
#include <unistd.h> // write
#include <time.h>
#include <sys/time.h> // for timestamps
#include <sys/stat.h> // for mode flags
#endif
#ifdef ENABLE_SEGGER_RTT
#include "SEGGER_RTT.h"
// allow to configure mode, channel, up buffer size in btstack_config.h for binary HCI formats (PacketLogger/BlueZ)
#ifndef SEGGER_RTT_PACKETLOG_MODE
#define SEGGER_RTT_PACKETLOG_MODE SEGGER_RTT_MODE_DEFAULT
#endif
#ifndef SEGGER_RTT_PACKETLOG_BUFFER_SIZE
#define SEGGER_RTT_PACKETLOG_BUFFER_SIZE 1024
#endif
#ifndef SEGGER_RTT_PACKETLOG_CHANNEL
#define SEGGER_RTT_PACKETLOG_CHANNEL 1
#endif
static char segger_rtt_packetlog_buffer[SEGGER_RTT_PACKETLOG_BUFFER_SIZE];
#endif
// BLUEZ hcidump - struct not used directly, but left here as documentation
typedef struct {
uint16_t len;
uint8_t in;
uint8_t pad;
uint32_t ts_sec;
uint32_t ts_usec;
uint8_t packet_type;
}
hcidump_hdr;
#define HCIDUMP_HDR_SIZE 13
// APPLE PacketLogger - struct not used directly, but left here as documentation
typedef struct {
uint32_t len;
uint32_t ts_sec;
uint32_t ts_usec;
uint8_t type; // 0xfc for note
}
pktlog_hdr;
#define PKTLOG_HDR_SIZE 13
static int dump_file = -1;
static int dump_format;
#ifdef HAVE_POSIX_FILE_IO
static char time_string[40];
static int max_nr_packets = -1;
static int nr_packets = 0;
#endif
#if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT)
static char log_message_buffer[256];
#endif
static const hci_dump_t * hci_dump_impl;
static int max_nr_packets;
static int nr_packets;
// levels: debug, info, error
static int log_level_enabled[3] = { 1, 1, 1};
static bool log_level_enabled[3] = { 1, 1, 1};
void hci_dump_open(const char *filename, hci_dump_format_t format){
dump_format = format;
#ifdef HAVE_POSIX_FILE_IO
if (dump_format == HCI_DUMP_STDOUT) {
dump_file = fileno(stdout);
} else {
int oflags = O_WRONLY | O_CREAT | O_TRUNC;
#ifdef _WIN32
oflags |= O_BINARY;
#endif
dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
if (dump_file < 0){
printf("hci_dump_open: failed to open file %s\n", filename);
}
}
#else
UNUSED(filename);
#ifdef ENABLE_SEGGER_RTT
switch (dump_format){
case HCI_DUMP_PACKETLOGGER:
case HCI_DUMP_BLUEZ:
SEGGER_RTT_ConfigUpBuffer(SEGGER_RTT_PACKETLOG_CHANNEL, "hci_dump", &segger_rtt_packetlog_buffer[0], SEGGER_RTT_PACKETLOG_BUFFER_SIZE, SEGGER_RTT_PACKETLOG_MODE);
break;
default:
break;
}
#endif
dump_file = 1;
#endif
static bool hci_dump_log_level_active(int log_level){
if (hci_dump_impl == NULL) return false;
if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return false;
if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return false;
return log_level_enabled[log_level];
}
void hci_dump_init(const hci_dump_t * impl){
max_nr_packets = -1;
nr_packets = 0;
hci_dump_impl = impl;
}
#ifdef HAVE_POSIX_FILE_IO
void hci_dump_set_max_packets(int packets){
max_nr_packets = packets;
}
void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
if (hci_dump_impl == NULL) return;
if (max_nr_packets > 0){
if ((nr_packets >= max_nr_packets) && (hci_dump_impl->reset != NULL)) {
nr_packets = 0;
(*hci_dump_impl->reset)();
}
nr_packets++;
}
(*hci_dump_impl->log_packet)(packet_type, in, packet, len);
}
void hci_dump_log(int log_level, const char * format, ...){
if (!hci_dump_log_level_active(log_level)) return;
va_list argptr;
va_start(argptr, format);
(*hci_dump_impl->log_message)(format, argptr);
va_end(argptr);
}
#ifdef __AVR__
void hci_dump_log_P(int log_level, PGM_P format, ...){
if (!hci_dump_log_level_active(log_level)) return;
va_list argptr;
va_start(argptr, format);
(*hci_dump_impl->log_packet_P)(format, argptr);
va_end(argptr);
}
#endif
static void hci_dump_packetlogger_setup_header(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
big_endian_store_32( buffer, 0, PKTLOG_HDR_SIZE - 4 + len);
void hci_dump_enable_log_level(int log_level, int enable){
if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return;
if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return;
log_level_enabled[log_level] = enable != 0;
}
void hci_dump_setup_header_packetlogger(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
big_endian_store_32( buffer, 0, HCI_DUMP_HEADER_SIZE_PACKETLOGGER - 4 + len);
big_endian_store_32( buffer, 4, tv_sec);
big_endian_store_32( buffer, 8, tv_us);
uint8_t packet_logger_type = 0;
@ -193,7 +137,7 @@ static void hci_dump_packetlogger_setup_header(uint8_t * buffer, uint32_t tv_sec
buffer[12] = packet_logger_type;
}
static void hci_dump_bluez_setup_header(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
void hci_dump_setup_header_bluez(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
little_endian_store_16( buffer, 0u, 1u + len);
buffer[2] = in;
buffer[3] = 0;
@ -201,218 +145,3 @@ static void hci_dump_bluez_setup_header(uint8_t * buffer, uint32_t tv_sec, uint3
little_endian_store_32( buffer, 8, tv_us);
buffer[12] = packet_type;
}
static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
switch (packet_type){
case HCI_COMMAND_DATA_PACKET:
printf("CMD => ");
break;
case HCI_EVENT_PACKET:
printf("EVT <= ");
break;
case HCI_ACL_DATA_PACKET:
if (in != 0) {
printf("ACL <= ");
} else {
printf("ACL => ");
}
break;
case HCI_SCO_DATA_PACKET:
if (in != 0) {
printf("SCO <= ");
} else {
printf("SCO => ");
}
break;
case LOG_MESSAGE_PACKET:
printf("LOG -- %s\n", (char*) packet);
return;
default:
return;
}
printf_hexdump(packet, len);
}
static void printf_timestamp(void){
#ifdef HAVE_POSIX_FILE_IO
struct tm* ptm;
struct timeval curr_time;
gettimeofday(&curr_time, NULL);
time_t curr_time_secs = curr_time.tv_sec;
/* Obtain the time of day, and convert it to a tm struct. */
ptm = localtime (&curr_time_secs);
/* assert localtime was successful */
if (!ptm) return;
/* Format the date and time, down to a single second. */
strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
/* Compute milliseconds from microseconds. */
uint16_t milliseconds = curr_time.tv_usec / 1000;
/* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */
printf ("%s.%03u] ", time_string, milliseconds);
#else
uint32_t time_ms = btstack_run_loop_get_time_ms();
int seconds = time_ms / 1000u;
int minutes = seconds / 60;
unsigned int hours = minutes / 60;
uint16_t p_ms = time_ms - (seconds * 1000u);
uint16_t p_seconds = seconds - (minutes * 60);
uint16_t p_minutes = minutes - (hours * 60u);
printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
#endif
}
void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
static union {
uint8_t header_bluez[HCIDUMP_HDR_SIZE];
uint8_t header_packetlogger[PKTLOG_HDR_SIZE];
} header;
if (dump_file < 0) return; // not activated yet
#ifdef HAVE_POSIX_FILE_IO
// don't grow bigger than max_nr_packets
if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
if (nr_packets >= max_nr_packets){
lseek(dump_file, 0, SEEK_SET);
// avoid -Wunused-result
int res = ftruncate(dump_file, 0);
UNUSED(res);
nr_packets = 0;
}
nr_packets++;
}
#endif
if (dump_format == HCI_DUMP_STDOUT){
printf_timestamp();
printf_packet(packet_type, in, packet, len);
return;
}
uint32_t tv_sec = 0;
uint32_t tv_us = 0;
// get time
#ifdef HAVE_POSIX_FILE_IO
struct timeval curr_time;
gettimeofday(&curr_time, NULL);
tv_sec = curr_time.tv_sec;
tv_us = curr_time.tv_usec;
#else
uint32_t time_ms = btstack_run_loop_get_time_ms();
tv_sec = time_ms / 1000u;
tv_us = (time_ms - (tv_sec * 1000)) * 1000;
// Saturday, January 1, 2000 12:00:00
tv_sec += 946728000UL;
#endif
#ifdef ENABLE_SEGGER_RTT
#if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP)
static const char rtt_warning[] = "RTT buffer full - packet(s) skipped";
static bool rtt_packet_skipped = false;
if (rtt_packet_skipped){
// try to write warning log message
rtt_packet_skipped = false;
packet_type = LOG_MESSAGE_PACKET;
packet = (uint8_t *) &rtt_warning[0];
len = sizeof(rtt_warning)-1;
}
#endif
#endif
uint16_t header_len = 0;
switch (dump_format){
case HCI_DUMP_BLUEZ:
hci_dump_bluez_setup_header(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
header_len = HCIDUMP_HDR_SIZE;
break;
case HCI_DUMP_PACKETLOGGER:
hci_dump_packetlogger_setup_header(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
header_len = PKTLOG_HDR_SIZE;
break;
default:
return;
}
#ifdef HAVE_POSIX_FILE_IO
// avoid -Wunused-result
int res = 0;
res = write (dump_file, &header, header_len);
res = write (dump_file, packet, len );
UNUSED(res);
#endif
#ifdef ENABLE_SEGGER_RTT
#if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP)
// check available space in buffer to avoid writing header but not packet
unsigned space_free = SEGGER_RTT_GetAvailWriteSpace(SEGGER_RTT_PACKETLOG_CHANNEL);
if ((header_len + len) > space_free) {
rtt_packet_skipped = true;
return;
}
#endif
SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, &header, header_len);
SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, packet, len);
#endif
UNUSED(header_len);
}
static int hci_dump_log_level_active(int log_level){
if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return 0;
if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return 0;
return log_level_enabled[log_level];
}
void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){
if (!hci_dump_log_level_active(log_level)) return;
#if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT)
if (dump_file >= 0){
int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
return;
}
#endif
printf_timestamp();
printf("LOG -- ");
vprintf(format, argptr);
printf("\n");
}
void hci_dump_log(int log_level, const char * format, ...){
va_list argptr;
va_start(argptr, format);
hci_dump_log_va_arg(log_level, format, argptr);
va_end(argptr);
}
#ifdef __AVR__
void hci_dump_log_P(int log_level, PGM_P format, ...){
if (!hci_dump_log_level_active(log_level)) return;
va_list argptr;
va_start(argptr, format);
printf_P(PSTR("LOG -- "));
vfprintf_P(stdout, format, argptr);
printf_P(PSTR("\n"));
va_end(argptr);
}
#endif
void hci_dump_close(void){
#ifdef HAVE_POSIX_FILE_IO
close(dump_file);
#endif
dump_file = -1;
}
void hci_dump_enable_log_level(int log_level, int enable){
if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return;
if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return;
log_level_enabled[log_level] = enable;
}

View File

@ -61,31 +61,54 @@ extern "C" {
#define HCI_DUMP_LOG_LEVEL_INFO 1
#define HCI_DUMP_LOG_LEVEL_ERROR 2
#define HCI_DUMP_HEADER_SIZE_PACKETLOGGER 13
#define HCI_DUMP_HEADER_SIZE_BLUEZ 13
/* API_START */
typedef enum {
HCI_DUMP_BLUEZ = 0,
HCI_DUMP_INVALID = 0,
HCI_DUMP_BLUEZ,
HCI_DUMP_PACKETLOGGER,
HCI_DUMP_STDOUT
} hci_dump_format_t;
/*
* @brief
*/
void hci_dump_open(const char *filename, hci_dump_format_t format);
typedef struct {
// reset output, called if max packets is reached, to limit file size
void (*reset)(void);
// log packet
void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
// log message
void (*log_message)(const char * format, va_list argptr);
#ifdef __AVR__ \
// log message - AVR
void (*log_message_P)(int log_level, PGM_P * format, va_list argptr);
#endif
} hci_dump_t;
/*
* @brief
* @brief Init HCI Dump
* @param hci_dump_impl - platform-specific implementation
*/
void hci_dump_init(const hci_dump_t * hci_dump_impl);
/*
* @brief Set max number of packets - output file might be truncated
*/
void hci_dump_set_max_packets(int packets); // -1 for unlimited
/*
* @brief
* @brief Dump Packet
* @param packet_type
* @param in is 1 for incoming, 0 for outoing
* @param packet
* @param len
*/
void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
/*
* @brief
* @brief Dump Message
* @param log_level
* @param format
*/
void hci_dump_log(int log_level, const char * format, ...)
#ifdef __GNUC__
@ -93,23 +116,46 @@ __attribute__ ((format (__printf__, 2, 3)))
#endif
;
#ifdef __AVR__
/*
* @brief
* @brief Dump Message using format string stored in PGM memory (allows to save RAM)
* @param log_level
* @param format
*/
void hci_dump_log_P(int log_level, PGM_P format, ...);
#ifdef __GNUC__
__attribute__ ((format (__printf__, 2, 3)))
#endif
#endif
/**
* @brief Setup header for PacketLogger format
* @param buffer
* @param tv_sec
* @param tv_us
* @param packet_type
* @param in
* @param len
*/
void hci_dump_setup_header_packetlogger(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len);
/**
* @brief Setup header for BLUEZ (hcidump) format
* @param buffer
* @param tv_sec
* @param tv_us
* @param packet_type
* @param in
* @param len
*/
void hci_dump_setup_header_bluez(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len);
/*
* @brief
*/
void hci_dump_enable_log_level(int log_level, int enable);
/*
* @brief
*/
void hci_dump_close(void);
/* API_END */
void hci_dump_log_va_arg(int log_level, const char * format, va_list argtr);
#ifdef __AVR__
void hci_dump_log_P(int log_level, PGM_P format, ...);
#endif
#if defined __cplusplus
}