-
Notifications
You must be signed in to change notification settings - Fork 117
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
Added support for ESP32 with Husarnet TCPv6 transport #708
Open
DominikN
wants to merge
6
commits into
micro-ROS:galactic
Choose a base branch
from
DominikN:galactic
base: galactic
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8626e39
initial husarnet support based on https://github.com/micro-ROS/micro_…
DominikN 6c49c50
fix esp32 build prefix
DominikN 9c83c2e
waiting for micro-ros-agent host
DominikN 6bad822
Update src/husarnet_transport.cpp
DominikN 83213a4
Remove printfs on Serial1 interface
DominikN 68b42b3
Remove printfs on Serial1 in Husarnet Transport definition
DominikN 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#if defined(ESP32) && defined(HUSARNET) | ||
#include <Arduino.h> | ||
#include <Husarnet.h> | ||
#include <HusarnetClient.h> | ||
#include <micro_ros_arduino.h> | ||
|
||
extern "C" { | ||
|
||
static HusarnetClient client; | ||
|
||
bool arduino_husarnet_transport_open(struct uxrCustomTransport *transport) { | ||
struct micro_ros_agent_locator *locator = | ||
(struct micro_ros_agent_locator *)transport->args; | ||
|
||
Serial1.printf("Connecting to \"%s:%d\"... ", locator->hostname, | ||
locator->port); | ||
DominikN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* Try to connect to a server on port 8888 on your laptop */ | ||
if (!client.connect(locator->hostname, locator->port)) { | ||
Serial1.printf("failed\r\n"); | ||
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. Also here |
||
return false; | ||
} | ||
|
||
Serial1.printf("done\r\n"); | ||
|
||
return true; | ||
} | ||
|
||
bool arduino_husarnet_transport_close(struct uxrCustomTransport *transport) { | ||
client.stop(); | ||
return true; | ||
} | ||
|
||
size_t arduino_husarnet_transport_write(struct uxrCustomTransport *transport, | ||
const uint8_t *buf, size_t len, | ||
uint8_t *errcode) { | ||
(void)errcode; | ||
|
||
// As we are using a TCP stream connection we should indicate the size of the message with the first two bytes of the stream. | ||
static uint8_t buffer_size[2]; | ||
buffer_size[0] = (uint8_t)(0x00FF & len); | ||
buffer_size[1] = (uint8_t)((0xFF00 & len) >> 8); | ||
size_t sent = client.write(buffer_size, 2); | ||
|
||
// Then we send the payload | ||
if (sent == 2) { | ||
sent = client.write(buf, len); | ||
} else { | ||
sent = 0; | ||
} | ||
|
||
return sent; | ||
} | ||
|
||
// Sample state machine for receiving data | ||
typedef enum { | ||
STATE_WAIT_FOR_SIZE = 0, | ||
STATE_WAIT_FOR_DATA, | ||
STATE_MESSAGE_AVAILABLE | ||
} husarnet_tcp_states_t; | ||
|
||
typedef struct { | ||
uint8_t buffer[UXR_CONFIG_CUSTOM_TRANSPORT_MTU]; | ||
|
||
uint8_t length_buffer[2]; | ||
|
||
uint16_t message_size; | ||
uint16_t message_size_received; | ||
|
||
husarnet_tcp_states_t state; | ||
} husarnet_tcp_receiver_t; | ||
|
||
static husarnet_tcp_receiver_t receiver = {}; | ||
|
||
void read_tcp_data(husarnet_tcp_receiver_t & r) { | ||
switch(r.state) { | ||
case STATE_WAIT_FOR_SIZE: | ||
if (client.available() >= 2) | ||
{ | ||
client.read(r.length_buffer, 2); | ||
r.message_size = (r.length_buffer[0] | (r.length_buffer[1] << 8)); | ||
r.message_size_received = 0; | ||
r.state = STATE_WAIT_FOR_DATA; | ||
} | ||
break; | ||
case STATE_WAIT_FOR_DATA: | ||
if(client.available()) | ||
{ | ||
size_t to_read = (r.message_size - r.message_size_received) < client.available() ? r.message_size - r.message_size_received : client.available(); | ||
size_t readed = client.read(&r.buffer[r.message_size_received], to_read); | ||
r.message_size_received += readed; | ||
if(r.message_size_received == r.message_size){ | ||
r.state = STATE_MESSAGE_AVAILABLE; | ||
} | ||
} | ||
break; | ||
case STATE_MESSAGE_AVAILABLE: | ||
break; | ||
} | ||
} | ||
|
||
size_t arduino_husarnet_transport_read(struct uxrCustomTransport *transport, | ||
uint8_t *buf, size_t len, int timeout, | ||
uint8_t *errcode) { | ||
(void)errcode; | ||
|
||
client.setTimeout(timeout); | ||
|
||
do | ||
{ | ||
int64_t time_init = uxr_millis(); | ||
read_tcp_data(receiver); | ||
timeout -= (int)(uxr_millis() - time_init); | ||
} | ||
while ((STATE_MESSAGE_AVAILABLE != receiver.state) && (0 < timeout)); | ||
|
||
if (STATE_MESSAGE_AVAILABLE == receiver.state) | ||
{ | ||
size_t readed = receiver.message_size; | ||
memcpy(buf, receiver.buffer, readed); | ||
receiver.state = STATE_WAIT_FOR_SIZE; | ||
return readed; | ||
} | ||
else | ||
{ | ||
return 0; | ||
} | ||
} | ||
|
||
} | ||
|
||
#endif |
Oops, something went wrong.
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.
Will Arduino IDE look in this path for the library?
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.
Only with additional flags:
There is still
esp32
folder that will be handled by default by Arduino IDE