Skip to content

Commit

Permalink
Updated demo code to show handling of different packet types
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhoff committed May 9, 2023
1 parent 753d9ec commit c04163b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
13 changes: 13 additions & 0 deletions include/UDP_data.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#ifndef __UDP_DATA_H__
#define __UDP_DATA_H__

#define PACKET_TYPE_CONFIG 0x0
#define PACKET_TYPE_DATA 0x1

// The packed attribute here is important if you want to parse
// your structs manually, ie. struct pointer + so many bytes.
// Packing the structs will ensure the compiler doesn't add in
// padding for memory allignment.
struct __attribute__((packed)) config_packet {
char packet_type;
char config_byte;
};

struct __attribute__((packed)) data_packet {
char packet_type;
int my_int;
char my_string[20];
};
Expand Down
49 changes: 42 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ void vDrawIP(unsigned char IP[4], unsigned int port)
sprintf(buff, "%u", port);
gfxDrawCenteredText(buff, PORT_X, OCTET_Y, Black);


// sprintf(buff, " ip port");
// gfxDrawCenteredText(buff, SCREEN_WIDTH / 2, OCTET_Y + 20, Black);
}
Expand Down Expand Up @@ -394,7 +393,15 @@ void vSendTask(void *pvParameters)
xSemaphoreGive(ip_and_port.lock);
}

struct data_packet my_packet = {.my_int = 555, .my_string = "Hello via UDP"};
struct config_packet my_config_packet = { .packet_type =
PACKET_TYPE_CONFIG,
.config_byte = 0b10101010
};

struct data_packet my_data_packet = { .packet_type = PACKET_TYPE_DATA,
.my_int = 555,
.my_string = "Hello via UDP"
};

while (1) {
gfxEventFetchEvents(
Expand All @@ -407,17 +414,46 @@ void vSendTask(void *pvParameters)

gfxDrawUpdateScreen(); // Refresh the screen to draw string

aIOSocketPut(UDP, addr, port, (char *)&my_packet, sizeof(struct data_packet));
printf("Sending config packet...");
aIOSocketPut(UDP, addr, port, (char *)&my_config_packet,
sizeof(struct config_packet));
printf("Packet sent\n");

vTaskDelay(1000);

printf("Sending data packet...");
aIOSocketPut(UDP, addr, port, (char *)&my_data_packet,
sizeof(struct data_packet));
printf("Packet sent\n");

vTaskDelay(1000);
}
}

void UDPHandler(size_t read_size, char *buffer, void *args)
{
struct data_packet *recv_packet = (struct data_packet *)buffer;
printf("Packet received\n");

// Get first byte to check packet type
char packet_type = *buffer;

printf("Recv int: %d and string: %s\n", recv_packet->my_int, recv_packet->my_string);
// Handle packet depending on type
switch (packet_type) {
case PACKET_TYPE_CONFIG: {
struct config_packet *config_packet =
(struct config_packet *)buffer;

printf("Recv config byte: %x\n", config_packet->config_byte);
} break;
case PACKET_TYPE_DATA: {
struct data_packet *data_packet = (struct data_packet *)buffer;

printf("Recv int: %d and string: %s\n", data_packet->my_int,
data_packet->my_string);
} break;
default:
break;
}
}

void vRecvTask(void *pvParameters)
Expand All @@ -429,8 +465,7 @@ void vRecvTask(void *pvParameters)
xSemaphoreGive(ip_and_port.lock);
}

aIOOpenUDPSocket(NULL, port, UDP_BUFFER_SIZE,
UDPHandler, NULL);
aIOOpenUDPSocket(NULL, port, UDP_BUFFER_SIZE, UDPHandler, NULL);

gfxDrawBindThread();

Expand Down

0 comments on commit c04163b

Please sign in to comment.