xref: /btstack/platform/windows/hci_dump_windows_stdout.c (revision 2d17d4c0682d450ab238973b9cc335f0518b6547)
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(&lt);
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:
743c291624SMatthias Ringwald             if (in) {
753c291624SMatthias Ringwald                 printf("ACL <= ");
763c291624SMatthias Ringwald             } else {
773c291624SMatthias Ringwald                 printf("ACL => ");
783c291624SMatthias Ringwald             }
793c291624SMatthias Ringwald             break;
803c291624SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
813c291624SMatthias Ringwald             if (in) {
823c291624SMatthias Ringwald                 printf("SCO <= ");
833c291624SMatthias Ringwald             } else {
843c291624SMatthias Ringwald                 printf("SCO => ");
853c291624SMatthias Ringwald             }
863c291624SMatthias Ringwald             break;
873c291624SMatthias Ringwald         case HCI_ISO_DATA_PACKET:
883c291624SMatthias Ringwald             if (in) {
893c291624SMatthias Ringwald                 printf("ISO <= ");
903c291624SMatthias Ringwald             } else {
913c291624SMatthias Ringwald                 printf("ISO => ");
923c291624SMatthias Ringwald             }
933c291624SMatthias Ringwald             break;
943c291624SMatthias Ringwald         case LOG_MESSAGE_PACKET:
953c291624SMatthias Ringwald             printf("LOG -- %s\n", (char*) packet);
963c291624SMatthias Ringwald             return;
973c291624SMatthias Ringwald         default:
983c291624SMatthias Ringwald             return;
993c291624SMatthias Ringwald     }
1003c291624SMatthias Ringwald     printf_hexdump(packet, len);
1013c291624SMatthias Ringwald }
1023c291624SMatthias Ringwald 
1033c291624SMatthias Ringwald static void hci_dump_windows_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
1043c291624SMatthias Ringwald     hci_dump_windows_stdout_timestamp();
1053c291624SMatthias Ringwald     hci_dump_windows_stdout_packet(packet_type, in, packet, len);
1063c291624SMatthias Ringwald }
1073c291624SMatthias Ringwald 
108*2d17d4c0SMatthias Ringwald static void hci_dump_windows_stdout_log_message(int log_level, const char * format, va_list argptr){
109*2d17d4c0SMatthias Ringwald     UNUSED(log_level);
1103c291624SMatthias Ringwald     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
1113c291624SMatthias Ringwald     hci_dump_windows_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
1123c291624SMatthias Ringwald }
1133c291624SMatthias Ringwald 
1143c291624SMatthias Ringwald const hci_dump_t * hci_dump_windows_stdout_get_instance(void){
1153c291624SMatthias Ringwald     static const hci_dump_t hci_dump_instance = {
1163c291624SMatthias Ringwald         // void (*reset)(void);
1173c291624SMatthias Ringwald         NULL,
1183c291624SMatthias Ringwald         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
1193c291624SMatthias Ringwald         &hci_dump_windows_stdout_log_packet,
1203c291624SMatthias Ringwald         // void (*log_message)(int log_level, const char * format, va_list argptr);
1213c291624SMatthias Ringwald         &hci_dump_windows_stdout_log_message,
1223c291624SMatthias Ringwald     };
1233c291624SMatthias Ringwald     return &hci_dump_instance;
1243c291624SMatthias Ringwald }
125