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 403c291624SMatthias Ringwald /* 413c291624SMatthias Ringwald * Dump HCI trace on stdout 423c291624SMatthias Ringwald */ 433c291624SMatthias Ringwald 443c291624SMatthias Ringwald #include "hci_dump.h" 453c291624SMatthias Ringwald #include "btstack_config.h" 463c291624SMatthias Ringwald #include "hci.h" 473c291624SMatthias Ringwald #include "hci_cmd.h" 483c291624SMatthias Ringwald #include <stdio.h> 493c291624SMatthias Ringwald #include <Windows.h> 503c291624SMatthias Ringwald 513c291624SMatthias Ringwald #include "hci_dump_windows_stdout.h" 523c291624SMatthias Ringwald 533c291624SMatthias Ringwald #ifndef ENABLE_PRINTF_HEXDUMP 543c291624SMatthias 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" 553c291624SMatthias Ringwald #endif 563c291624SMatthias Ringwald 573c291624SMatthias Ringwald static char log_message_buffer[HCI_DUMP_MAX_MESSAGE_LEN]; 583c291624SMatthias Ringwald 593c291624SMatthias Ringwald static void hci_dump_windows_stdout_timestamp(void){ 603c291624SMatthias Ringwald SYSTEMTIME lt; 613c291624SMatthias Ringwald GetLocalTime(<); 623c291624SMatthias Ringwald printf("[%04u-%02u-%02u %02u:%02u:%02u.%3u] ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); 633c291624SMatthias Ringwald } 643c291624SMatthias Ringwald 653c291624SMatthias Ringwald static void hci_dump_windows_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 663c291624SMatthias Ringwald switch (packet_type){ 673c291624SMatthias Ringwald case HCI_COMMAND_DATA_PACKET: 683c291624SMatthias Ringwald printf("CMD => "); 693c291624SMatthias Ringwald break; 703c291624SMatthias Ringwald case HCI_EVENT_PACKET: 713c291624SMatthias Ringwald printf("EVT <= "); 723c291624SMatthias Ringwald break; 733c291624SMatthias Ringwald case HCI_ACL_DATA_PACKET: 74*1b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ACL 75*1b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ACL){ 76*1b5f4ae3SMatthias Ringwald printf("LOG -- ACL %s, size %u\n", in ? "in" : "out", len); 77*1b5f4ae3SMatthias Ringwald return; 78*1b5f4ae3SMatthias Ringwald } 79*1b5f4ae3SMatthias Ringwald #endif 803c291624SMatthias Ringwald if (in) { 813c291624SMatthias Ringwald printf("ACL <= "); 823c291624SMatthias Ringwald } else { 833c291624SMatthias Ringwald printf("ACL => "); 843c291624SMatthias Ringwald } 853c291624SMatthias Ringwald break; 863c291624SMatthias Ringwald case HCI_SCO_DATA_PACKET: 87*1b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_SCO 88*1b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_SCO){ 89*1b5f4ae3SMatthias Ringwald printf("LOG -- SCO %s, size %u\n", in ? "in" : "out", len); 90*1b5f4ae3SMatthias Ringwald return; 91*1b5f4ae3SMatthias Ringwald } 92*1b5f4ae3SMatthias Ringwald #endif 933c291624SMatthias Ringwald if (in) { 943c291624SMatthias Ringwald printf("SCO <= "); 953c291624SMatthias Ringwald } else { 963c291624SMatthias Ringwald printf("SCO => "); 973c291624SMatthias Ringwald } 983c291624SMatthias Ringwald break; 993c291624SMatthias Ringwald case HCI_ISO_DATA_PACKET: 100*1b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ISO 101*1b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ISO){ 102*1b5f4ae3SMatthias Ringwald printf("LOG -- ISO %s, size %u\n", in ? "in" : "out", len); 103*1b5f4ae3SMatthias Ringwald return; 104*1b5f4ae3SMatthias Ringwald } 105*1b5f4ae3SMatthias Ringwald #endif 1063c291624SMatthias Ringwald if (in) { 1073c291624SMatthias Ringwald printf("ISO <= "); 1083c291624SMatthias Ringwald } else { 1093c291624SMatthias Ringwald printf("ISO => "); 1103c291624SMatthias Ringwald } 1113c291624SMatthias Ringwald break; 1123c291624SMatthias Ringwald case LOG_MESSAGE_PACKET: 1133c291624SMatthias Ringwald printf("LOG -- %s\n", (char*) packet); 1143c291624SMatthias Ringwald return; 1153c291624SMatthias Ringwald default: 1163c291624SMatthias Ringwald return; 1173c291624SMatthias Ringwald } 1183c291624SMatthias Ringwald printf_hexdump(packet, len); 1193c291624SMatthias Ringwald } 1203c291624SMatthias Ringwald 1213c291624SMatthias Ringwald static void hci_dump_windows_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 1223c291624SMatthias Ringwald hci_dump_windows_stdout_timestamp(); 1233c291624SMatthias Ringwald hci_dump_windows_stdout_packet(packet_type, in, packet, len); 1243c291624SMatthias Ringwald } 1253c291624SMatthias Ringwald 1262d17d4c0SMatthias Ringwald static void hci_dump_windows_stdout_log_message(int log_level, const char * format, va_list argptr){ 1272d17d4c0SMatthias Ringwald UNUSED(log_level); 1283c291624SMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 1293c291624SMatthias Ringwald hci_dump_windows_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 1303c291624SMatthias Ringwald } 1313c291624SMatthias Ringwald 1323c291624SMatthias Ringwald const hci_dump_t * hci_dump_windows_stdout_get_instance(void){ 1333c291624SMatthias Ringwald static const hci_dump_t hci_dump_instance = { 1343c291624SMatthias Ringwald // void (*reset)(void); 1353c291624SMatthias Ringwald NULL, 1363c291624SMatthias Ringwald // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 1373c291624SMatthias Ringwald &hci_dump_windows_stdout_log_packet, 1383c291624SMatthias Ringwald // void (*log_message)(int log_level, const char * format, va_list argptr); 1393c291624SMatthias Ringwald &hci_dump_windows_stdout_log_message, 1403c291624SMatthias Ringwald }; 1413c291624SMatthias Ringwald return &hci_dump_instance; 1423c291624SMatthias Ringwald } 143