-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FreeRTOS-Plus-TCP port #239
Merged
Merged
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
3d954cd
FreeRTOS-Plus-TCP port: system and UDP unicast
bjsowa 2d9ed40
Fix clock_elapsed_s method
bjsowa d3e8cc1
Avoid double free
bjsowa f949eb3
Add TCP transport
bjsowa 8e2d99d
Fix build when only TCP is enabled
bjsowa ba1c4c0
Initial FreeRTOS-Plus-TCP example
bjsowa d502492
Initial CI workflow
bjsowa c8c8b3d
Fix newline character in printf
bjsowa b86ce98
Fix network driver checksum options
bjsowa 46bde55
Fix printf newline
bjsowa 6314ac9
Make vAppTask static
bjsowa 649fed5
Resolve MAC address of gateway before running Zenoh app
bjsowa c7cb1ef
Set zenoh debug level
bjsowa 9965500
Increase socket timeout
bjsowa 98be97f
Fix segfault when failed to create endpoint
bjsowa 28fe14f
Fix warning about overflow in int conversion when using 64-bit system
bjsowa 1390425
Use ai_family returned by getaddrinfo
bjsowa 933324f
Fix tcp and udp read methods
bjsowa 8633333
Retain original contributor in z_pub example
bjsowa 1c0d0b3
Add z_scout example
bjsowa 663724b
Base the z_pub example on unix example
bjsowa fe5d30e
Add more examples
bjsowa 1581f37
Change FreeRTOS-Plus-TCP repository
bjsowa 66e5d1c
Fix building in Release mode
bjsowa 7fc8da0
Multi-threading initial
bjsowa 841924c
Mark UDP Multicast as unsupported
bjsowa 8148874
Fix _z_task_free
bjsowa 070b5a6
Use multi-threading in examples
bjsowa daa800d
Add sleep to z_sub example
bjsowa 424421d
Allow passing task attributes to read and lease tasks
bjsowa 62556f9
Use static tasks in z_pub example
bjsowa a364a6e
Remove redundant print
bjsowa 683a4d8
Add more examples
bjsowa 5d92775
Change char* to const char*
bjsowa 63c089b
Update license headers
bjsowa 2291ddd
Update README
bjsowa a547d02
Merge branch 'master' into freertos
bjsowa 4e81de9
Fix config keys
bjsowa d6d757f
Fix fprintzid
bjsowa 376ed99
Ignore freertos_plus_tcp port sources when using PlatformIO
bjsowa 7f9c2cf
Merge remote-tracking branch 'zenoh/master' into freertos
bjsowa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef ZENOH_PICO_SYSTEM_FREERTOS_PLUS_TCP_TYPES_H | ||
#define ZENOH_PICO_SYSTEM_FREERTOS_PLUS_TCP_TYPES_H | ||
|
||
#include "FreeRTOS.h" | ||
#include "FreeRTOS_IP.h" | ||
|
||
typedef TickType_t z_clock_t; | ||
typedef TickType_t z_time_t; | ||
|
||
typedef struct { | ||
union { | ||
#if Z_LINK_UDP_MULTICAST == 1 || Z_LINK_UDP_UNICAST == 1 | ||
Socket_t _socket; | ||
#endif | ||
}; | ||
} _z_sys_net_socket_t; | ||
|
||
typedef struct { | ||
union { | ||
#if Z_LINK_TCP == 1 || Z_LINK_UDP_MULTICAST == 1 || Z_LINK_UDP_UNICAST == 1 | ||
struct freertos_addrinfo *_iptcp; | ||
#endif | ||
}; | ||
} _z_sys_net_endpoint_t; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <stdlib.h> | ||
|
||
#include "zenoh-pico/system/platform.h" | ||
#include "zenoh-pico/utils/pointers.h" | ||
#include "zenoh-pico/utils/result.h" | ||
|
||
// FreeRTOS includes | ||
#include "FreeRTOS.h" | ||
#include "FreeRTOS_IP.h" | ||
|
||
#if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 | ||
/*------------------ UDP sockets ------------------*/ | ||
int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { | ||
int8_t ret = _Z_RES_OK; | ||
|
||
if (FreeRTOS_getaddrinfo(s_address, NULL, NULL, &ep->_iptcp) < 0) { | ||
ret = _Z_ERR_GENERIC; | ||
} | ||
|
||
ep->_iptcp->ai_addr->sin_family = FREERTOS_AF_INET4; | ||
|
||
// Parse and check the validity of the port | ||
uint32_t port = strtoul(s_port, NULL, 10); | ||
if ((port > (uint32_t)0) && (port <= (uint32_t)65355)) { // Port numbers should range from 1 to 65355 | ||
ep->_iptcp->ai_addr->sin_port = FreeRTOS_htons((uint16_t)port); | ||
} else { | ||
ret = _Z_ERR_GENERIC; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
void _z_free_endpoint_udp(_z_sys_net_endpoint_t *ep) { FreeRTOS_freeaddrinfo(ep->_iptcp); } | ||
#endif | ||
|
||
#if Z_LINK_UDP_UNICAST == 1 | ||
int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout) { | ||
int8_t ret = _Z_RES_OK; | ||
|
||
sock->_socket = FreeRTOS_socket(rep._iptcp->ai_family, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP); | ||
if (sock->_socket != FREERTOS_INVALID_SOCKET) { | ||
TickType_t receive_timeout = pdMS_TO_TICKS(tout); | ||
|
||
if (FreeRTOS_setsockopt(sock->_socket, 0, FREERTOS_SO_RCVTIMEO, &receive_timeout, 0) != 0) { | ||
ret = _Z_ERR_GENERIC; | ||
} | ||
|
||
if (ret != _Z_RES_OK) { | ||
FreeRTOS_closesocket(sock->_socket); | ||
} | ||
} else { | ||
ret = _Z_ERR_GENERIC; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int8_t _z_listen_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout) { | ||
int8_t ret = _Z_RES_OK; | ||
(void)sock; | ||
(void)rep; | ||
(void)tout; | ||
|
||
// @TODO: To be implemented | ||
ret = _Z_ERR_GENERIC; | ||
|
||
return ret; | ||
} | ||
|
||
void _z_close_udp_unicast(_z_sys_net_socket_t *sock) { FreeRTOS_closesocket(sock->_socket); } | ||
|
||
size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { | ||
struct freertos_sockaddr raddr; | ||
uint32_t addrlen = sizeof(struct freertos_sockaddr); | ||
|
||
int32_t rb = FreeRTOS_recvfrom(sock._socket, ptr, len, 0, &raddr, &addrlen); | ||
if (rb < 0) { | ||
rb = SIZE_MAX; | ||
} | ||
|
||
return rb; | ||
} | ||
|
||
size_t _z_read_exact_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { | ||
size_t n = 0; | ||
uint8_t *pos = &ptr[0]; | ||
|
||
do { | ||
size_t rb = _z_read_udp_unicast(sock, pos, len - n); | ||
if (rb == SIZE_MAX) { | ||
n = rb; | ||
break; | ||
} | ||
|
||
n = n + rb; | ||
pos = _z_ptr_u8_offset(pos, n); | ||
} while (n != len); | ||
|
||
return n; | ||
} | ||
|
||
size_t _z_send_udp_unicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len, | ||
const _z_sys_net_endpoint_t rep) { | ||
return FreeRTOS_sendto(sock._socket, ptr, len, 0, rep._iptcp->ai_addr, sizeof(struct freertos_sockaddr)); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "FreeRTOS_IP.h" | ||
#include "zenoh-pico/config.h" | ||
#include "zenoh-pico/system/platform.h" | ||
|
||
/*------------------ Random ------------------*/ | ||
uint8_t z_random_u8(void) { return z_random_u32(); } | ||
|
||
uint16_t z_random_u16(void) { return z_random_u32(); } | ||
|
||
uint32_t z_random_u32(void) { | ||
uint32_t ret = 0; | ||
xApplicationGetRandomNumber(&ret); | ||
return ret; | ||
} | ||
|
||
uint64_t z_random_u64(void) { | ||
uint64_t ret = 0; | ||
ret |= z_random_u32(); | ||
ret = ret << 32; | ||
ret |= z_random_u32(); | ||
return ret; | ||
} | ||
|
||
void z_random_fill(void *buf, size_t len) { | ||
for (size_t i = 0; i < len; i++) { | ||
*((uint8_t *)buf) = z_random_u8(); | ||
} | ||
} | ||
|
||
/*------------------ Memory ------------------*/ | ||
void *z_malloc(size_t size) { return pvPortMalloc(size); } | ||
|
||
void *z_realloc(void *ptr, size_t size) { | ||
// realloc not implemented in FreeRTOS | ||
return NULL; | ||
} | ||
|
||
void z_free(void *ptr) { vPortFree(ptr); } | ||
|
||
/*------------------ Sleep ------------------*/ | ||
int z_sleep_us(size_t time) { | ||
vTaskDelay(pdMS_TO_TICKS(time / 1000)); | ||
return 0; | ||
} | ||
|
||
int z_sleep_ms(size_t time) { | ||
vTaskDelay(pdMS_TO_TICKS(time)); | ||
return 0; | ||
} | ||
|
||
int z_sleep_s(size_t time) { | ||
vTaskDelay(pdMS_TO_TICKS(time * 1000)); | ||
return 0; | ||
} | ||
|
||
/*------------------ Clock ------------------*/ | ||
z_clock_t z_clock_now(void) { return z_time_now(); } | ||
|
||
unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms(instant) * 1000; } | ||
bjsowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
unsigned long z_clock_elapsed_ms(z_clock_t *instant) { return z_time_elapsed_ms(instant); } | ||
|
||
unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_time_elapsed_ms(instant) * 1000; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will return seconds but useconds instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed it. It is worth noting that the emscripten port, which I used for reference, has the same issue. |
||
|
||
/*------------------ Time ------------------*/ | ||
z_time_t z_time_now(void) { return xTaskGetTickCount(); } | ||
|
||
const char *z_time_now_as_str(char *const buf, unsigned long buflen) { | ||
snprintf(buf, buflen, "%u", z_time_now()); | ||
return buf; | ||
} | ||
|
||
unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } | ||
|
||
unsigned long z_time_elapsed_ms(z_time_t *time) { | ||
z_time_t now = z_time_now(); | ||
|
||
unsigned long elapsed = (now - *time) * portTICK_PERIOD_MS; | ||
return elapsed; | ||
} | ||
|
||
unsigned long z_time_elapsed_s(z_time_t *time) { return z_time_elapsed_ms(time) / 1000; } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this value set by
getaddrinfo
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not,
getaddrinfo
only setsai_family
but does not write it toai_addr->sin_family
. I changed it so it at least uses the value returned by getaddrinfo.