-
Notifications
You must be signed in to change notification settings - Fork 60
/
esphap_debug.c
41 lines (33 loc) · 1.03 KB
/
esphap_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdlib.h>
#include <stdio.h>
#include "esphap_debug.h"
char *binary_to_string(const byte *data, size_t size) {
int i;
size_t buffer_size = 1; // 1 char for eos
for (i=0; i<size; i++)
buffer_size += (data[i] == '\\') ? 2 : ((data[i] >= 32 && data[i] < 128) ? 1 : 4);
char *buffer = malloc(buffer_size);
int pos = 0;
for (i=0; i<size; i++) {
if (data[i] == '\\') {
buffer[pos++] = '\\';
buffer[pos++] = '\\';
} else if (data[i] < 32 || data[i] >= 128) {
size_t printed = snprintf(&buffer[pos], buffer_size - pos, "\\x%02X", data[i]);
if (printed > 0) {
pos += printed;
}
} else {
buffer[pos++] = data[i];
}
}
buffer[pos] = 0;
return buffer;
}
void print_binary(const char *prompt, const byte *data, size_t size) {
#ifdef HOMEKIT_DEBUG
char *buffer = binary_to_string(data, size);
printf("%s (%d bytes): \"%s\"\n", prompt, (int)size, buffer);
free(buffer);
#endif
}