hci_dump_rtthread_stdout.c 4.5 KB

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