Line data Source code
1 : #include "Utils.h"
2 : #include "../Hal.h"
3 :
4 : #include <stdarg.h>
5 : #include <stdio.h>
6 : #include <stdlib.h>
7 : #include <string.h>
8 : #include <inttypes.h>
9 :
10 0 : uint32_t rlb_reflect(uint32_t in, uint8_t bits) {
11 0 : uint32_t res = 0;
12 0 : for(uint8_t i = 0; i < bits; i++) {
13 0 : res |= (((in & ((uint32_t)1 << i)) >> i) << (bits - i - 1));
14 : }
15 0 : return(res);
16 : }
17 :
18 0 : void rlb_hexdump(const char* level, const uint8_t* data, size_t len, uint32_t offset, uint8_t width, bool be) {
19 : #if RADIOLIB_DEBUG
20 : size_t rem_len = len;
21 : for(size_t i = 0; i < len; i+=16) {
22 : char str[120];
23 : sprintf(str, "%08" PRIx32 ": ", (uint32_t)i+offset);
24 : size_t line_len = 16;
25 : if(rem_len < line_len) {
26 : line_len = rem_len;
27 : }
28 : for(size_t j = 0; j < line_len; j+=width) {
29 : if(width > 1) {
30 : int m = 0;
31 : int step = width/2;
32 : if(be) {
33 : step *= -1;
34 : }
35 : for(int32_t k = width - 1; k >= -width + 1; k+=step) {
36 : sprintf(&str[10 + (j+m)*3], "%02x ", data[i+j+k+m]);
37 : m++;
38 : }
39 : } else {
40 : sprintf(&str[10 + (j)*3], "%02x ", data[i+j]);
41 : }
42 : }
43 : for(size_t j = line_len; j < 16; j++) {
44 : sprintf(&str[10 + j*3], " ");
45 : }
46 : str[58] = ' ';
47 :
48 : // at this point we need to start escaping "%" characters
49 : char* strPtr = &str[59];
50 : for(size_t j = 0; j < line_len; j++) {
51 : char c = data[i+j];
52 : if((c < ' ') || (c > '~')) {
53 : c = '.';
54 : } else if(c == '%') {
55 : *strPtr++ = '%';
56 : }
57 : sprintf(strPtr++, "%c", c);
58 :
59 : }
60 : for(size_t j = line_len; j < 16; j++) {
61 : sprintf(strPtr++, " ");
62 : }
63 : if(level) {
64 : RADIOLIB_DEBUG_PRINT_LVL("", "%s", level);
65 : }
66 : RADIOLIB_DEBUG_PRINT("%s", str);
67 : RADIOLIB_DEBUG_PRINTLN();
68 : rem_len -= 16;
69 : }
70 :
71 : #else
72 : // outside of debug, this does nothing
73 : (void)level;
74 : (void)data;
75 : (void)len;
76 : (void)offset;
77 : (void)width;
78 : (void)be;
79 :
80 : #endif
81 0 : }
82 :
83 : #if RADIOLIB_DEBUG
84 : // https://github.com/esp8266/Arduino/blob/65579d29081cb8501e4d7f786747bf12e7b37da2/cores/esp8266/Print.cpp#L50
85 : size_t rlb_printf(bool ts, const char* format, ...) {
86 : va_list arg;
87 : va_start(arg, format);
88 : char temp[64];
89 : char* buffer = temp;
90 : RadioLibTime_t timestamp = rlb_time_us();
91 : unsigned long sec = timestamp/1000000UL;
92 : unsigned long usec = timestamp%1000000UL;
93 : size_t len_ts = 0;
94 : if(ts) { len_ts = snprintf(temp, sizeof(temp), "[%lu.%06lu] ", sec, usec); }
95 : size_t len_str = vsnprintf(&temp[len_ts], sizeof(temp) - len_ts, format, arg);
96 : size_t len = len_ts + len_str;
97 : va_end(arg);
98 : if (len > sizeof(temp) - 1) {
99 : buffer = new char[len + 1];
100 : if (!buffer) {
101 : return 0;
102 : }
103 : va_start(arg, format);
104 : if(ts) { len_ts = snprintf(buffer, len_ts + 1, "[%lu.%06lu] ", sec, usec); }
105 : vsnprintf(buffer + len_ts, len_str + 1, format, arg);
106 : va_end(arg);
107 : }
108 : #if defined(RADIOLIB_BUILD_ARDUINO)
109 : len = RADIOLIB_DEBUG_PORT.write(reinterpret_cast<const uint8_t*>(buffer), len);
110 : #else
111 : len = fwrite(buffer, sizeof(temp[0]), len, RADIOLIB_DEBUG_PORT);
112 : #endif
113 : if (buffer != temp) {
114 : delete[] buffer;
115 : }
116 : return len;
117 : }
118 : #endif
|