From b08dada6c1f0d93392886b0e874e0e486e5fb447 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 29 May 2019 08:35:28 +0200 Subject: [PATCH] Prefix control event constants by namespace This will avoid conflicts with future device events. --- app/src/control_event.c | 4 ++-- app/src/control_event.h | 6 +++--- app/src/controller.c | 2 +- app/tests/test_control_event_serialize.c | 20 ++++++++++---------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/control_event.c b/app/src/control_event.c index bdd044a0..e836f506 100644 --- a/app/src/control_event.c +++ b/app/src/control_event.c @@ -25,9 +25,9 @@ control_event_serialize(const struct control_event *event, unsigned char *buf) { case CONTROL_EVENT_TYPE_TEXT: { // write length (2 bytes) + string (non nul-terminated) size_t len = strlen(event->text_event.text); - if (len > TEXT_MAX_LENGTH) { + if (len > CONTROL_EVENT_TEXT_MAX_LENGTH) { // injecting a text takes time, so limit the text length - len = TEXT_MAX_LENGTH; + len = CONTROL_EVENT_TEXT_MAX_LENGTH; } buffer_write16be(&buf[1], (uint16_t) len); memcpy(&buf[3], event->text_event.text, len); diff --git a/app/src/control_event.h b/app/src/control_event.h index ac698065..3fc1a54f 100644 --- a/app/src/control_event.h +++ b/app/src/control_event.h @@ -9,8 +9,8 @@ #include "common.h" #define CONTROL_EVENT_QUEUE_SIZE 64 -#define TEXT_MAX_LENGTH 300 -#define SERIALIZED_EVENT_MAX_SIZE (3 + TEXT_MAX_LENGTH) +#define CONTROL_EVENT_TEXT_MAX_LENGTH 300 +#define CONTROL_EVENT_SERIALIZED_MAX_SIZE (3 + CONTROL_EVENT_TEXT_MAX_LENGTH) enum control_event_type { CONTROL_EVENT_TYPE_KEYCODE, @@ -59,7 +59,7 @@ struct control_event_queue { int tail; }; -// buf size must be at least SERIALIZED_EVENT_MAX_SIZE +// buf size must be at least CONTROL_EVENT_SERIALIZED_MAX_SIZE int control_event_serialize(const struct control_event *event, unsigned char *buf); diff --git a/app/src/controller.c b/app/src/controller.c index 8b95e88c..8a342503 100644 --- a/app/src/controller.c +++ b/app/src/controller.c @@ -51,7 +51,7 @@ controller_push_event(struct controller *controller, static bool process_event(struct controller *controller, const struct control_event *event) { - unsigned char serialized_event[SERIALIZED_EVENT_MAX_SIZE]; + unsigned char serialized_event[CONTROL_EVENT_SERIALIZED_MAX_SIZE]; int length = control_event_serialize(event, serialized_event); if (!length) { return false; diff --git a/app/tests/test_control_event_serialize.c b/app/tests/test_control_event_serialize.c index 90b75fff..9fae75dc 100644 --- a/app/tests/test_control_event_serialize.c +++ b/app/tests/test_control_event_serialize.c @@ -13,7 +13,7 @@ static void test_serialize_keycode_event(void) { }, }; - unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE]; int size = control_event_serialize(&event, buf); assert(size == 10); @@ -34,7 +34,7 @@ static void test_serialize_text_event(void) { }, }; - unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE]; int size = control_event_serialize(&event, buf); assert(size == 16); @@ -49,20 +49,20 @@ static void test_serialize_text_event(void) { static void test_serialize_long_text_event(void) { struct control_event event; event.type = CONTROL_EVENT_TYPE_TEXT; - char text[TEXT_MAX_LENGTH + 1]; + char text[CONTROL_EVENT_TEXT_MAX_LENGTH + 1]; memset(text, 'a', sizeof(text)); - text[TEXT_MAX_LENGTH] = '\0'; + text[CONTROL_EVENT_TEXT_MAX_LENGTH] = '\0'; event.text_event.text = text; - unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE]; int size = control_event_serialize(&event, buf); - assert(size == 3 + TEXT_MAX_LENGTH); + assert(size == 3 + CONTROL_EVENT_TEXT_MAX_LENGTH); - unsigned char expected[3 + TEXT_MAX_LENGTH]; + unsigned char expected[3 + CONTROL_EVENT_TEXT_MAX_LENGTH]; expected[0] = 0x01; // CONTROL_EVENT_TYPE_KEYCODE expected[1] = 0x01; expected[2] = 0x2c; // text length (16 bits) - memset(&expected[3], 'a', TEXT_MAX_LENGTH); + memset(&expected[3], 'a', CONTROL_EVENT_TEXT_MAX_LENGTH); assert(!memcmp(buf, expected, sizeof(expected))); } @@ -86,7 +86,7 @@ static void test_serialize_mouse_event(void) { }, }; - unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE]; int size = control_event_serialize(&event, buf); assert(size == 18); @@ -119,7 +119,7 @@ static void test_serialize_scroll_event(void) { }, }; - unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE]; int size = control_event_serialize(&event, buf); assert(size == 21);