Extract binary to hex string conversion
This commit is contained in:
parent
92da055de5
commit
9745ac51bc
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include "aoa_hid.h"
|
#include "aoa_hid.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
#include "util/str.h"
|
||||||
|
|
||||||
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
|
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
|
||||||
#define ACCESSORY_REGISTER_HID 54
|
#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) {
|
sc_hid_event_log(uint16_t accessory_id, const struct sc_hid_event *event) {
|
||||||
// HID Event: [00] FF FF FF FF...
|
// HID Event: [00] FF FF FF FF...
|
||||||
assert(event->size);
|
assert(event->size);
|
||||||
unsigned buffer_size = event->size * 3 + 1;
|
char *hex = sc_str_to_hex_string(event->data, event->size);
|
||||||
char *buffer = malloc(buffer_size);
|
if (!hex) {
|
||||||
if (!buffer) {
|
|
||||||
LOG_OOM();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < event->size; ++i) {
|
LOGV("HID Event: [%d] %s", accessory_id, hex);
|
||||||
snprintf(buffer + i * 3, 4, " %02x", event->data[i]);
|
free(hex);
|
||||||
}
|
|
||||||
LOGV("HID Event: [%d]%s", accessory_id, buffer);
|
|
||||||
free(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -333,3 +334,22 @@ sc_str_remove_trailing_cr(char *s, size_t len) {
|
|||||||
}
|
}
|
||||||
return 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;
|
||||||
|
}
|
||||||
|
@ -138,4 +138,10 @@ sc_str_index_of_column(const char *s, unsigned col, const char *seps);
|
|||||||
size_t
|
size_t
|
||||||
sc_str_remove_trailing_cr(char *s, size_t len);
|
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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user