1*3c291624SMatthias Ringwald /* 2*3c291624SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*3c291624SMatthias Ringwald * 4*3c291624SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*3c291624SMatthias Ringwald * modification, are permitted provided that the following conditions 6*3c291624SMatthias Ringwald * are met: 7*3c291624SMatthias Ringwald * 8*3c291624SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*3c291624SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*3c291624SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*3c291624SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*3c291624SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*3c291624SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*3c291624SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*3c291624SMatthias Ringwald * from this software without specific prior written permission. 16*3c291624SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*3c291624SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*3c291624SMatthias Ringwald * monetary gain. 19*3c291624SMatthias Ringwald * 20*3c291624SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*3c291624SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*3c291624SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*3c291624SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24*3c291624SMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*3c291624SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*3c291624SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*3c291624SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*3c291624SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*3c291624SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*3c291624SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*3c291624SMatthias Ringwald * SUCH DAMAGE. 32*3c291624SMatthias Ringwald * 33*3c291624SMatthias Ringwald * Please inquire about commercial licensing options at 34*3c291624SMatthias Ringwald * [email protected] 35*3c291624SMatthias Ringwald * 36*3c291624SMatthias Ringwald */ 37*3c291624SMatthias Ringwald 38*3c291624SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump_windows_stdout.c" 39*3c291624SMatthias Ringwald 40*3c291624SMatthias Ringwald /* 41*3c291624SMatthias Ringwald * Dump HCI trace on stdout 42*3c291624SMatthias Ringwald */ 43*3c291624SMatthias Ringwald 44*3c291624SMatthias Ringwald #include "hci_dump.h" 45*3c291624SMatthias Ringwald #include "btstack_config.h" 46*3c291624SMatthias Ringwald #include "hci.h" 47*3c291624SMatthias Ringwald #include "hci_cmd.h" 48*3c291624SMatthias Ringwald #include <stdio.h> 49*3c291624SMatthias Ringwald #include <Windows.h> 50*3c291624SMatthias Ringwald 51*3c291624SMatthias Ringwald #include "hci_dump_windows_stdout.h" 52*3c291624SMatthias Ringwald 53*3c291624SMatthias Ringwald #ifndef ENABLE_PRINTF_HEXDUMP 54*3c291624SMatthias 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" 55*3c291624SMatthias Ringwald #endif 56*3c291624SMatthias Ringwald 57*3c291624SMatthias Ringwald static char log_message_buffer[HCI_DUMP_MAX_MESSAGE_LEN]; 58*3c291624SMatthias Ringwald 59*3c291624SMatthias Ringwald static void hci_dump_windows_stdout_timestamp(void){ 60*3c291624SMatthias Ringwald SYSTEMTIME lt; 61*3c291624SMatthias Ringwald GetLocalTime(<); 62*3c291624SMatthias Ringwald printf("[%04u-%02u-%02u %02u:%02u:%02u.%3u] ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); 63*3c291624SMatthias Ringwald } 64*3c291624SMatthias Ringwald 65*3c291624SMatthias Ringwald static void hci_dump_windows_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 66*3c291624SMatthias Ringwald switch (packet_type){ 67*3c291624SMatthias Ringwald case HCI_COMMAND_DATA_PACKET: 68*3c291624SMatthias Ringwald printf("CMD => "); 69*3c291624SMatthias Ringwald break; 70*3c291624SMatthias Ringwald case HCI_EVENT_PACKET: 71*3c291624SMatthias Ringwald printf("EVT <= "); 72*3c291624SMatthias Ringwald break; 73*3c291624SMatthias Ringwald case HCI_ACL_DATA_PACKET: 74*3c291624SMatthias Ringwald if (in) { 75*3c291624SMatthias Ringwald printf("ACL <= "); 76*3c291624SMatthias Ringwald } else { 77*3c291624SMatthias Ringwald printf("ACL => "); 78*3c291624SMatthias Ringwald } 79*3c291624SMatthias Ringwald break; 80*3c291624SMatthias Ringwald case HCI_SCO_DATA_PACKET: 81*3c291624SMatthias Ringwald if (in) { 82*3c291624SMatthias Ringwald printf("SCO <= "); 83*3c291624SMatthias Ringwald } else { 84*3c291624SMatthias Ringwald printf("SCO => "); 85*3c291624SMatthias Ringwald } 86*3c291624SMatthias Ringwald break; 87*3c291624SMatthias Ringwald case HCI_ISO_DATA_PACKET: 88*3c291624SMatthias Ringwald if (in) { 89*3c291624SMatthias Ringwald printf("ISO <= "); 90*3c291624SMatthias Ringwald } else { 91*3c291624SMatthias Ringwald printf("ISO => "); 92*3c291624SMatthias Ringwald } 93*3c291624SMatthias Ringwald break; 94*3c291624SMatthias Ringwald case LOG_MESSAGE_PACKET: 95*3c291624SMatthias Ringwald printf("LOG -- %s\n", (char*) packet); 96*3c291624SMatthias Ringwald return; 97*3c291624SMatthias Ringwald default: 98*3c291624SMatthias Ringwald return; 99*3c291624SMatthias Ringwald } 100*3c291624SMatthias Ringwald printf_hexdump(packet, len); 101*3c291624SMatthias Ringwald } 102*3c291624SMatthias Ringwald 103*3c291624SMatthias Ringwald static void hci_dump_windows_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 104*3c291624SMatthias Ringwald hci_dump_windows_stdout_timestamp(); 105*3c291624SMatthias Ringwald hci_dump_windows_stdout_packet(packet_type, in, packet, len); 106*3c291624SMatthias Ringwald } 107*3c291624SMatthias Ringwald 108*3c291624SMatthias Ringwald static void hci_dump_windows_stdout_log_message(const char * format, va_list argptr){ 109*3c291624SMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 110*3c291624SMatthias Ringwald hci_dump_windows_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 111*3c291624SMatthias Ringwald } 112*3c291624SMatthias Ringwald 113*3c291624SMatthias Ringwald const hci_dump_t * hci_dump_windows_stdout_get_instance(void){ 114*3c291624SMatthias Ringwald static const hci_dump_t hci_dump_instance = { 115*3c291624SMatthias Ringwald // void (*reset)(void); 116*3c291624SMatthias Ringwald NULL, 117*3c291624SMatthias Ringwald // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 118*3c291624SMatthias Ringwald &hci_dump_windows_stdout_log_packet, 119*3c291624SMatthias Ringwald // void (*log_message)(int log_level, const char * format, va_list argptr); 120*3c291624SMatthias Ringwald &hci_dump_windows_stdout_log_message, 121*3c291624SMatthias Ringwald }; 122*3c291624SMatthias Ringwald return &hci_dump_instance; 123*3c291624SMatthias Ringwald } 124