Skip to content

Display and control your Android device

Extract binary to hex string conversion

Romain Vimont107f7a83abe6
diff --git a/app/src/usb/aoa_hid.c b/app/src/usb/aoa_hid.c
index d6b418a0..50bc33fe 100644
--- a/app/src/usb/aoa_hid.c
+++ b/app/src/usb/aoa_hid.c
@@ -5,6 +5,7 @@
#include "aoa_hid.h"
#include "util/log.h"
+#include "util/str.h"
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
#define ACCESSORY_REGISTER_HID 54
@@ -20,17 +21,12 @@ static void
sc_hid_event_log(uint16_t accessory_id, const struct sc_hid_event *event) {
// HID Event: [00] FF FF FF FF...
assert(event->size);
- unsigned buffer_size = event->size * 3 + 1;
- char *buffer = malloc(buffer_size);
- if (!buffer) {
- LOG_OOM();
+ char *hex = sc_str_to_hex_string(event->data, event->size);
+ if (!hex) {
return;
}
- for (unsigned i = 0; i < event->size; ++i) {
- snprintf(buffer + i * 3, 4, " %02x", event->data[i]);
- }
- LOGV("HID Event: [%d]%s", accessory_id, buffer);
- free(buffer);
+ LOGV("HID Event: [%d] %s", accessory_id, hex);
+ free(hex);
}
bool
diff --git a/app/src/util/str.c b/app/src/util/str.c
index d78aa9d7..755369d8 100644
--- a/app/src/util/str.c
+++ b/app/src/util/str.c
@@ -3,6 +3,7 @@
#include <assert.h>
#include <errno.h>
#include <limits.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -333,3 +334,22 @@ sc_str_remove_trailing_cr(char *s, size_t len) {
}
return len;
}
+
+char *
+sc_str_to_hex_string(const uint8_t *data, size_t size) {
+ size_t buffer_size = size * 3 + 1;
+ char *buffer = malloc(buffer_size);
+ if (!buffer) {
+ LOG_OOM();
+ return NULL;
+ }
+
+ for (size_t i = 0; i < size; ++i) {
+ snprintf(buffer + i * 3, 4, "%02X ", data[i]);
+ }
+
+ // Remove the final space
+ buffer[size * 3] = '\0';
+
+ return buffer;
+}
diff --git a/app/src/util/str.h b/app/src/util/str.h
index 4f7eeeda..20da26f0 100644
--- a/app/src/util/str.h
+++ b/app/src/util/str.h
@@ -138,4 +138,10 @@ sc_str_index_of_column(const char *s, unsigned col, const char *seps);
size_t
sc_str_remove_trailing_cr(char *s, size_t len);
+/**
+ * Convert binary data to hexadecimal string
+ */
+char *
+sc_str_to_hex_string(const uint8_t *data, size_t len);
+
#endif