13c291624SMatthias Ringwald /*
23c291624SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
33c291624SMatthias Ringwald *
43c291624SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
53c291624SMatthias Ringwald * modification, are permitted provided that the following conditions
63c291624SMatthias Ringwald * are met:
73c291624SMatthias Ringwald *
83c291624SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
93c291624SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
103c291624SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
113c291624SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
123c291624SMatthias Ringwald * documentation and/or other materials provided with the distribution.
133c291624SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
143c291624SMatthias Ringwald * contributors may be used to endorse or promote products derived
153c291624SMatthias Ringwald * from this software without specific prior written permission.
163c291624SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
173c291624SMatthias Ringwald * personal benefit and not for any commercial purpose or for
183c291624SMatthias Ringwald * monetary gain.
193c291624SMatthias Ringwald *
203c291624SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213c291624SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223c291624SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233c291624SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
243c291624SMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253c291624SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263c291624SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273c291624SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283c291624SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293c291624SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303c291624SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313c291624SMatthias Ringwald * SUCH DAMAGE.
323c291624SMatthias Ringwald *
333c291624SMatthias Ringwald * Please inquire about commercial licensing options at
343c291624SMatthias Ringwald * [email protected]
353c291624SMatthias Ringwald *
363c291624SMatthias Ringwald */
373c291624SMatthias Ringwald
383c291624SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump_windows_stdout.c"
393c291624SMatthias Ringwald
40*b795dcd0SMarcel Wappler
41*b795dcd0SMarcel Wappler #include <Windows.h>
423c291624SMatthias Ringwald /*
433c291624SMatthias Ringwald * Dump HCI trace on stdout
443c291624SMatthias Ringwald */
453c291624SMatthias Ringwald
463c291624SMatthias Ringwald #include "hci_dump.h"
473c291624SMatthias Ringwald #include "btstack_config.h"
483c291624SMatthias Ringwald #include "hci.h"
493c291624SMatthias Ringwald #include "hci_cmd.h"
503c291624SMatthias Ringwald #include <stdio.h>
51*b795dcd0SMarcel Wappler
523c291624SMatthias Ringwald
533c291624SMatthias Ringwald #include "hci_dump_windows_stdout.h"
543c291624SMatthias Ringwald
553c291624SMatthias Ringwald #ifndef ENABLE_PRINTF_HEXDUMP
563c291624SMatthias Ringwald #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"
573c291624SMatthias Ringwald #endif
583c291624SMatthias Ringwald
593c291624SMatthias Ringwald static char log_message_buffer[HCI_DUMP_MAX_MESSAGE_LEN];
603c291624SMatthias Ringwald
hci_dump_windows_stdout_timestamp(void)613c291624SMatthias Ringwald static void hci_dump_windows_stdout_timestamp(void){
623c291624SMatthias Ringwald SYSTEMTIME lt;
633c291624SMatthias Ringwald GetLocalTime(<);
643c291624SMatthias Ringwald printf("[%04u-%02u-%02u %02u:%02u:%02u.%3u] ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
653c291624SMatthias Ringwald }
663c291624SMatthias Ringwald
hci_dump_windows_stdout_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)673c291624SMatthias Ringwald static void hci_dump_windows_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
683c291624SMatthias Ringwald switch (packet_type){
693c291624SMatthias Ringwald case HCI_COMMAND_DATA_PACKET:
703c291624SMatthias Ringwald printf("CMD => ");
713c291624SMatthias Ringwald break;
723c291624SMatthias Ringwald case HCI_EVENT_PACKET:
733c291624SMatthias Ringwald printf("EVT <= ");
743c291624SMatthias Ringwald break;
753c291624SMatthias Ringwald case HCI_ACL_DATA_PACKET:
761b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ACL
771b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ACL){
781b5f4ae3SMatthias Ringwald printf("LOG -- ACL %s, size %u\n", in ? "in" : "out", len);
791b5f4ae3SMatthias Ringwald return;
801b5f4ae3SMatthias Ringwald }
811b5f4ae3SMatthias Ringwald #endif
823c291624SMatthias Ringwald if (in) {
833c291624SMatthias Ringwald printf("ACL <= ");
843c291624SMatthias Ringwald } else {
853c291624SMatthias Ringwald printf("ACL => ");
863c291624SMatthias Ringwald }
873c291624SMatthias Ringwald break;
883c291624SMatthias Ringwald case HCI_SCO_DATA_PACKET:
891b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_SCO
901b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_SCO){
911b5f4ae3SMatthias Ringwald printf("LOG -- SCO %s, size %u\n", in ? "in" : "out", len);
921b5f4ae3SMatthias Ringwald return;
931b5f4ae3SMatthias Ringwald }
941b5f4ae3SMatthias Ringwald #endif
953c291624SMatthias Ringwald if (in) {
963c291624SMatthias Ringwald printf("SCO <= ");
973c291624SMatthias Ringwald } else {
983c291624SMatthias Ringwald printf("SCO => ");
993c291624SMatthias Ringwald }
1003c291624SMatthias Ringwald break;
1013c291624SMatthias Ringwald case HCI_ISO_DATA_PACKET:
1021b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ISO
1031b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ISO){
1041b5f4ae3SMatthias Ringwald printf("LOG -- ISO %s, size %u\n", in ? "in" : "out", len);
1051b5f4ae3SMatthias Ringwald return;
1061b5f4ae3SMatthias Ringwald }
1071b5f4ae3SMatthias Ringwald #endif
1083c291624SMatthias Ringwald if (in) {
1093c291624SMatthias Ringwald printf("ISO <= ");
1103c291624SMatthias Ringwald } else {
1113c291624SMatthias Ringwald printf("ISO => ");
1123c291624SMatthias Ringwald }
1133c291624SMatthias Ringwald break;
1143c291624SMatthias Ringwald case LOG_MESSAGE_PACKET:
1153c291624SMatthias Ringwald printf("LOG -- %s\n", (char*) packet);
1163c291624SMatthias Ringwald return;
1173c291624SMatthias Ringwald default:
1183c291624SMatthias Ringwald return;
1193c291624SMatthias Ringwald }
1203c291624SMatthias Ringwald printf_hexdump(packet, len);
1213c291624SMatthias Ringwald }
1223c291624SMatthias Ringwald
hci_dump_windows_stdout_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)1233c291624SMatthias Ringwald static void hci_dump_windows_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
1243c291624SMatthias Ringwald hci_dump_windows_stdout_timestamp();
1253c291624SMatthias Ringwald hci_dump_windows_stdout_packet(packet_type, in, packet, len);
1263c291624SMatthias Ringwald }
1273c291624SMatthias Ringwald
hci_dump_windows_stdout_log_message(int log_level,const char * format,va_list argptr)1282d17d4c0SMatthias Ringwald static void hci_dump_windows_stdout_log_message(int log_level, const char * format, va_list argptr){
1292d17d4c0SMatthias Ringwald UNUSED(log_level);
1303c291624SMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
1313c291624SMatthias Ringwald hci_dump_windows_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
1323c291624SMatthias Ringwald }
1333c291624SMatthias Ringwald
hci_dump_windows_stdout_get_instance(void)1343c291624SMatthias Ringwald const hci_dump_t * hci_dump_windows_stdout_get_instance(void){
1353c291624SMatthias Ringwald static const hci_dump_t hci_dump_instance = {
1363c291624SMatthias Ringwald // void (*reset)(void);
1373c291624SMatthias Ringwald NULL,
1383c291624SMatthias Ringwald // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
1393c291624SMatthias Ringwald &hci_dump_windows_stdout_log_packet,
1403c291624SMatthias Ringwald // void (*log_message)(int log_level, const char * format, va_list argptr);
1413c291624SMatthias Ringwald &hci_dump_windows_stdout_log_message,
1423c291624SMatthias Ringwald };
1433c291624SMatthias Ringwald return &hci_dump_instance;
1443c291624SMatthias Ringwald }
145