Skip to content

Commit

Permalink
Use sc_ prefix for device sender
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Feb 23, 2024
1 parent 9858eff commit 78a7e4f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/src/device_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "util/log.h"

ssize_t
device_msg_deserialize(const uint8_t *buf, size_t len, struct device_msg *msg) {
sc_device_msg_deserialize(const uint8_t *buf, size_t len,
struct sc_device_msg *msg) {
if (!len) {
return 0; // no message
}
Expand Down Expand Up @@ -52,7 +53,7 @@ device_msg_deserialize(const uint8_t *buf, size_t len, struct device_msg *msg) {
}

void
device_msg_destroy(struct device_msg *msg) {
sc_device_msg_destroy(struct sc_device_msg *msg) {
if (msg->type == DEVICE_MSG_TYPE_CLIPBOARD) {
free(msg->clipboard.text);
}
Expand Down
11 changes: 6 additions & 5 deletions app/src/device_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
// type: 1 byte; length: 4 bytes
#define DEVICE_MSG_TEXT_MAX_LENGTH (DEVICE_MSG_MAX_SIZE - 5)

enum device_msg_type {
enum sc_device_msg_type {
DEVICE_MSG_TYPE_CLIPBOARD,
DEVICE_MSG_TYPE_ACK_CLIPBOARD,
};

struct device_msg {
enum device_msg_type type;
struct sc_device_msg {
enum sc_device_msg_type type;
union {
struct {
char *text; // owned, to be freed by free()
Expand All @@ -30,9 +30,10 @@ struct device_msg {

// return the number of bytes consumed (0 for no msg available, -1 on error)
ssize_t
device_msg_deserialize(const uint8_t *buf, size_t len, struct device_msg *msg);
sc_device_msg_deserialize(const uint8_t *buf, size_t len,
struct sc_device_msg *msg);

void
device_msg_destroy(struct device_msg *msg);
sc_device_msg_destroy(struct sc_device_msg *msg);

#endif
8 changes: 4 additions & 4 deletions app/src/receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sc_receiver_destroy(struct sc_receiver *receiver) {
}

static void
process_msg(struct sc_receiver *receiver, struct device_msg *msg) {
process_msg(struct sc_receiver *receiver, struct sc_device_msg *msg) {
switch (msg->type) {
case DEVICE_MSG_TYPE_CLIPBOARD: {
char *current = SDL_GetClipboardText();
Expand Down Expand Up @@ -55,8 +55,8 @@ static ssize_t
process_msgs(struct sc_receiver *receiver, const uint8_t *buf, size_t len) {
size_t head = 0;
for (;;) {
struct device_msg msg;
ssize_t r = device_msg_deserialize(&buf[head], len - head, &msg);
struct sc_device_msg msg;
ssize_t r = sc_device_msg_deserialize(&buf[head], len - head, &msg);
if (r == -1) {
return -1;
}
Expand All @@ -65,7 +65,7 @@ process_msgs(struct sc_receiver *receiver, const uint8_t *buf, size_t len) {
}

process_msg(receiver, &msg);
device_msg_destroy(&msg);
sc_device_msg_destroy(&msg);

head += r;
assert(head <= len);
Expand Down
16 changes: 8 additions & 8 deletions app/tests/test_device_msg_deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ static void test_deserialize_clipboard(void) {
0x41, 0x42, 0x43, // "ABC"
};

struct device_msg msg;
ssize_t r = device_msg_deserialize(input, sizeof(input), &msg);
struct sc_device_msg msg;
ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
assert(r == 8);

assert(msg.type == DEVICE_MSG_TYPE_CLIPBOARD);
assert(msg.clipboard.text);
assert(!strcmp("ABC", msg.clipboard.text));

device_msg_destroy(&msg);
sc_device_msg_destroy(&msg);
}

static void test_deserialize_clipboard_big(void) {
Expand All @@ -35,16 +35,16 @@ static void test_deserialize_clipboard_big(void) {

memset(input + 5, 'a', DEVICE_MSG_TEXT_MAX_LENGTH);

struct device_msg msg;
ssize_t r = device_msg_deserialize(input, sizeof(input), &msg);
struct sc_device_msg msg;
ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
assert(r == DEVICE_MSG_MAX_SIZE);

assert(msg.type == DEVICE_MSG_TYPE_CLIPBOARD);
assert(msg.clipboard.text);
assert(strlen(msg.clipboard.text) == DEVICE_MSG_TEXT_MAX_LENGTH);
assert(msg.clipboard.text[0] == 'a');

device_msg_destroy(&msg);
sc_device_msg_destroy(&msg);
}

static void test_deserialize_ack_set_clipboard(void) {
Expand All @@ -53,8 +53,8 @@ static void test_deserialize_ack_set_clipboard(void) {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
};

struct device_msg msg;
ssize_t r = device_msg_deserialize(input, sizeof(input), &msg);
struct sc_device_msg msg;
ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
assert(r == 9);

assert(msg.type == DEVICE_MSG_TYPE_ACK_CLIPBOARD);
Expand Down

0 comments on commit 78a7e4f

Please sign in to comment.