Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/wendy_com/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ idf_component_register(
"src/wendy_com_agent.c"
"src/wendy_com_cmd.c"
"src/wendy_com_link.c"
"src/wendy_com_uart.c"
"src/wendy_com_msg.pb.c"
INCLUDE_DIRS
"include"
REQUIRES
esp-tls mbedtls freertos vfs log
nanopb
)
idf_component_get_property(nanopb_dir nanopb COMPONENT_DIR)
target_include_directories(${COMPONENT_LIB} PUBLIC "${nanopb_dir}")
1 change: 1 addition & 0 deletions components/wendy_com/include/wendy_com_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ WendyComResult wcom_cmd_app_push_data(int client_id, size_t offset, const uint8_
WendyComResult wcom_cmd_app_push_end(int client_id);
WendyComResult wcom_cmd_app_start(int client_id);
WendyComResult wcom_cmd_app_stop(int client_id);
WendyComResult wcom_cmd_get_device_identity(WendyComDeviceIdentity *out);

void wcom_cmd_client_disconnected(int client_id);

Expand Down
1 change: 1 addition & 0 deletions components/wendy_com/include/wendy_com_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct wcom_app_delegate {
WendyComResult (*on_app_start)(void);
WendyComResult (*on_app_stop)(void);
WendyComResult (*on_reboot)(void);
void (*on_get_device_identity)(const char **id, const char **name, const char **display_name);
};

struct wcom_operation {
Expand Down
7 changes: 5 additions & 2 deletions components/wendy_com/include/wendy_com_link.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef WENDY_COM_LINK_H
#define WENDY_COM_LINK_H

#include "esp_tls.h"
#include "wendy_com_uart.h"
#include "wendy_com_common.h"

// literals
Expand All @@ -10,6 +10,8 @@

// types

typedef struct esp_tls esp_tls_t;

enum wcom_link_state {
WCOM_LINK_STATE_UNDEFINED = 0,
WCOM_LINK_STATE_CONNECTED,
Expand Down Expand Up @@ -58,7 +60,8 @@ void wcom_close(int link_id);

// connection provider interface, ESP-IDF-specific

int wcom_add_link(esp_tls_t *tls);
int wcom_add_tls_link(esp_tls_t *tls);
int wcom_add_uart_link(wendy_com_uart_t *uart);
void wcom_remove_link(int link_id);

#endif
53 changes: 49 additions & 4 deletions components/wendy_com/include/wendy_com_msg.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ typedef struct _WendyComAppStopParams {
char dummy_field;
} WendyComAppStopParams;

typedef struct _WendyComGetDeviceIdentityParams {
char dummy_field;
} WendyComGetDeviceIdentityParams;

typedef struct _WendyComDeviceIdentity {
pb_callback_t id;
pb_callback_t name;
pb_callback_t display_name;
} WendyComDeviceIdentity;

/* Top-level command message — the oneof variant identifies the command. */
typedef struct _WendyComCommand {
uint32_t request_id;
Expand All @@ -75,6 +85,7 @@ typedef struct _WendyComCommand {
WendyComAppPushEndParams app_push_end;
WendyComAppStartParams app_start;
WendyComAppStopParams app_stop;
WendyComGetDeviceIdentityParams get_device_identity;
} params;
} WendyComCommand;

Expand All @@ -85,6 +96,7 @@ typedef struct _WendyComResponse {
pb_size_t which_data;
union {
WendyComProtocolVersion protocol_version;
WendyComDeviceIdentity device_identity;
} data;
} WendyComResponse;

Expand All @@ -108,6 +120,8 @@ extern "C" {





#define WendyComResponse_result_ENUMTYPE WendyComResult


Expand All @@ -121,6 +135,8 @@ extern "C" {
#define WendyComAppPushEndParams_init_default {0}
#define WendyComAppStartParams_init_default {0}
#define WendyComAppStopParams_init_default {0}
#define WendyComGetDeviceIdentityParams_init_default {0}
#define WendyComDeviceIdentity_init_default {{{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}}
#define WendyComCommand_init_default {0, 0, {WendyComProtocolVersionParams_init_default}}
#define WendyComResponse_init_default {0, _WendyComResult_MIN, 0, {WendyComProtocolVersion_init_default}}
#define WendyComProtocolVersion_init_zero {0, 0}
Expand All @@ -132,6 +148,8 @@ extern "C" {
#define WendyComAppPushEndParams_init_zero {0}
#define WendyComAppStartParams_init_zero {0}
#define WendyComAppStopParams_init_zero {0}
#define WendyComGetDeviceIdentityParams_init_zero {0}
#define WendyComDeviceIdentity_init_zero {{{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}}
#define WendyComCommand_init_zero {0, 0, {WendyComProtocolVersionParams_init_zero}}
#define WendyComResponse_init_zero {0, _WendyComResult_MIN, 0, {WendyComProtocolVersion_init_zero}}

Expand All @@ -143,6 +161,9 @@ extern "C" {
#define WendyComAppPushBeginParams_size_tag 1
#define WendyComAppPushDataParams_offset_tag 1
#define WendyComAppPushDataParams_data_tag 2
#define WendyComDeviceIdentity_id_tag 1
#define WendyComDeviceIdentity_name_tag 2
#define WendyComDeviceIdentity_display_name_tag 3
#define WendyComCommand_request_id_tag 1
#define WendyComCommand_protocol_version_tag 2
#define WendyComCommand_ping_tag 3
Expand All @@ -152,9 +173,11 @@ extern "C" {
#define WendyComCommand_app_push_end_tag 7
#define WendyComCommand_app_start_tag 8
#define WendyComCommand_app_stop_tag 9
#define WendyComCommand_get_device_identity_tag 10
#define WendyComResponse_request_id_tag 1
#define WendyComResponse_result_tag 2
#define WendyComResponse_protocol_version_tag 3
#define WendyComResponse_device_identity_tag 4

/* Struct field encoding specification for nanopb */
#define WendyComProtocolVersion_FIELDLIST(X, a) \
Expand Down Expand Up @@ -205,6 +228,18 @@ X(a, CALLBACK, SINGULAR, BYTES, data, 2)
#define WendyComAppStopParams_CALLBACK NULL
#define WendyComAppStopParams_DEFAULT NULL

#define WendyComGetDeviceIdentityParams_FIELDLIST(X, a) \

#define WendyComGetDeviceIdentityParams_CALLBACK NULL
#define WendyComGetDeviceIdentityParams_DEFAULT NULL

#define WendyComDeviceIdentity_FIELDLIST(X, a) \
X(a, CALLBACK, SINGULAR, STRING, id, 1) \
X(a, CALLBACK, SINGULAR, STRING, name, 2) \
X(a, CALLBACK, SINGULAR, STRING, display_name, 3)
#define WendyComDeviceIdentity_CALLBACK pb_default_field_callback
#define WendyComDeviceIdentity_DEFAULT NULL

#define WendyComCommand_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, request_id, 1) \
X(a, STATIC, ONEOF, MESSAGE, (params,protocol_version,params.protocol_version), 2) \
Expand All @@ -214,7 +249,8 @@ X(a, STATIC, ONEOF, MESSAGE, (params,app_push_begin,params.app_push_begin)
X(a, STATIC, ONEOF, MESSAGE, (params,app_push_data,params.app_push_data), 6) \
X(a, STATIC, ONEOF, MESSAGE, (params,app_push_end,params.app_push_end), 7) \
X(a, STATIC, ONEOF, MESSAGE, (params,app_start,params.app_start), 8) \
X(a, STATIC, ONEOF, MESSAGE, (params,app_stop,params.app_stop), 9)
X(a, STATIC, ONEOF, MESSAGE, (params,app_stop,params.app_stop), 9) \
X(a, STATIC, ONEOF, MESSAGE, (params,get_device_identity,params.get_device_identity), 10)
#define WendyComCommand_CALLBACK NULL
#define WendyComCommand_DEFAULT NULL
#define WendyComCommand_params_protocol_version_MSGTYPE WendyComProtocolVersionParams
Expand All @@ -225,14 +261,17 @@ X(a, STATIC, ONEOF, MESSAGE, (params,app_stop,params.app_stop), 9)
#define WendyComCommand_params_app_push_end_MSGTYPE WendyComAppPushEndParams
#define WendyComCommand_params_app_start_MSGTYPE WendyComAppStartParams
#define WendyComCommand_params_app_stop_MSGTYPE WendyComAppStopParams
#define WendyComCommand_params_get_device_identity_MSGTYPE WendyComGetDeviceIdentityParams

#define WendyComResponse_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, request_id, 1) \
X(a, STATIC, SINGULAR, UENUM, result, 2) \
X(a, STATIC, ONEOF, MESSAGE, (data,protocol_version,data.protocol_version), 3)
X(a, STATIC, ONEOF, MESSAGE, (data,protocol_version,data.protocol_version), 3) \
X(a, STATIC, ONEOF, MESSAGE, (data,device_identity,data.device_identity), 4)
#define WendyComResponse_CALLBACK NULL
#define WendyComResponse_DEFAULT NULL
#define WendyComResponse_data_protocol_version_MSGTYPE WendyComProtocolVersion
#define WendyComResponse_data_device_identity_MSGTYPE WendyComDeviceIdentity

extern const pb_msgdesc_t WendyComProtocolVersion_msg;
extern const pb_msgdesc_t WendyComProtocolVersionParams_msg;
Expand All @@ -243,6 +282,8 @@ extern const pb_msgdesc_t WendyComAppPushDataParams_msg;
extern const pb_msgdesc_t WendyComAppPushEndParams_msg;
extern const pb_msgdesc_t WendyComAppStartParams_msg;
extern const pb_msgdesc_t WendyComAppStopParams_msg;
extern const pb_msgdesc_t WendyComGetDeviceIdentityParams_msg;
extern const pb_msgdesc_t WendyComDeviceIdentity_msg;
extern const pb_msgdesc_t WendyComCommand_msg;
extern const pb_msgdesc_t WendyComResponse_msg;

Expand All @@ -256,22 +297,26 @@ extern const pb_msgdesc_t WendyComResponse_msg;
#define WendyComAppPushEndParams_fields &WendyComAppPushEndParams_msg
#define WendyComAppStartParams_fields &WendyComAppStartParams_msg
#define WendyComAppStopParams_fields &WendyComAppStopParams_msg
#define WendyComGetDeviceIdentityParams_fields &WendyComGetDeviceIdentityParams_msg
#define WendyComDeviceIdentity_fields &WendyComDeviceIdentity_msg
#define WendyComCommand_fields &WendyComCommand_msg
#define WendyComResponse_fields &WendyComResponse_msg

/* Maximum encoded size of messages (where known) */
/* WendyComAppPushDataParams_size depends on runtime parameters */
/* WendyComDeviceIdentity_size depends on runtime parameters */
/* WendyComCommand_size depends on runtime parameters */
#define WENDY_COM_MSG_PB_H_MAX_SIZE WendyComResponse_size
/* WendyComResponse_size depends on runtime parameters */
#define WENDY_COM_MSG_PB_H_MAX_SIZE WendyComProtocolVersion_size
#define WendyComAppPushBeginParams_size 6
#define WendyComAppPushEndParams_size 0
#define WendyComAppStartParams_size 0
#define WendyComAppStopParams_size 0
#define WendyComGetDeviceIdentityParams_size 0
#define WendyComPingParams_size 0
#define WendyComProtocolVersionParams_size 12
#define WendyComProtocolVersion_size 12
#define WendyComRebootParams_size 0
#define WendyComResponse_size 22

#ifdef __cplusplus
} /* extern "C" */
Expand Down
39 changes: 39 additions & 0 deletions components/wendy_com/include/wendy_com_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef WENDY_COM_UART_H
#define WENDY_COM_UART_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>

#define WENDY_COM_UART_ERR_UNKNOWN -1
#define WENDY_COM_UART_ERR_WANT_READ -2
#define WENDY_COM_UART_ERR_WANT_WRITE -3

#define WENDY_COM_UART_RAW_BUF_SIZE 256

typedef enum wendy_com_uart_esc_cmd {
WENDY_COM_UART_ESC_CMD_CONSOLE = 'c',
WENDY_COM_UART_ESC_CMD_ECHO = 'e',
WENDY_COM_UART_ESC_CMD_KEEP_ALIVE = 'k',
WENDY_COM_UART_ESC_CMD_COM = 'm',
WENDY_COM_UART_ESC_CMD_OFF = 'o',
WENDY_COM_UART_ESC_CMD_ESC = '_',
} wendy_com_uart_esc_cmd_t;

typedef struct wendy_com_uart {
int fd;
uint8_t raw_buf[WENDY_COM_UART_RAW_BUF_SIZE];
int raw_pos;
int raw_len;
bool pending_esc;
bool eof_pending;
uint8_t last_esc_cmd;
} wendy_com_uart_t;

void wendy_com_uart_init(wendy_com_uart_t *uart, int fd);
ssize_t wendy_com_uart_read(wendy_com_uart_t *uart, void *data, size_t datalen);
ssize_t wendy_com_uart_write(wendy_com_uart_t *uart, const void *data, size_t datalen);
int wendy_com_uart_get_fd(wendy_com_uart_t *uart);

#endif
28 changes: 19 additions & 9 deletions components/wendy_com/proto/wendy_com_msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@ message WendyComAppPushEndParams {}
message WendyComAppStartParams {}
message WendyComAppStopParams {}

message WendyComGetDeviceIdentityParams {}

message WendyComDeviceIdentity {
string id = 1;
string name = 2;
string display_name = 3;
}

// Top-level command message — the oneof variant identifies the command.
message WendyComCommand {
uint32 request_id = 1;
oneof params {
WendyComProtocolVersionParams protocol_version = 2;
WendyComPingParams ping = 3;
WendyComRebootParams reboot = 4;
WendyComAppPushBeginParams app_push_begin = 5;
WendyComAppPushDataParams app_push_data = 6;
WendyComAppPushEndParams app_push_end = 7;
WendyComAppStartParams app_start = 8;
WendyComAppStopParams app_stop = 9;
WendyComProtocolVersionParams protocol_version = 2;
WendyComPingParams ping = 3;
WendyComRebootParams reboot = 4;
WendyComAppPushBeginParams app_push_begin = 5;
WendyComAppPushDataParams app_push_data = 6;
WendyComAppPushEndParams app_push_end = 7;
WendyComAppStartParams app_start = 8;
WendyComAppStopParams app_stop = 9;
WendyComGetDeviceIdentityParams get_device_identity = 10;
}
}

Expand All @@ -58,6 +67,7 @@ message WendyComResponse {
uint32 request_id = 1;
WendyComResult result = 2;
oneof data {
WendyComProtocolVersion protocol_version = 3;
WendyComProtocolVersion protocol_version = 3;
WendyComDeviceIdentity device_identity = 4;
}
}
16 changes: 14 additions & 2 deletions components/wendy_com/src/wendy_com_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ static void _process_command(struct _agent_link *link, const uint8_t *body, size

WendyComResponse resp = { .request_id = cmd.request_id, .result = WendyComResult_WENDY_COM_RESULT_UNKNOWN_ERROR };

ESP_LOGI(TAG, "ch%d received cmd id %d", link->link_id, cmd.request_id);
switch (cmd.which_params) {
case WendyComCommand_protocol_version_tag:
resp.which_data = WendyComResponse_protocol_version_tag;
Expand Down Expand Up @@ -175,14 +176,25 @@ static void _process_command(struct _agent_link *link, const uint8_t *body, size
case WendyComCommand_app_stop_tag:
resp.result = wcom_cmd_app_stop(link->link_id);
break;
case WendyComCommand_get_device_identity_tag:
resp.which_data = WendyComResponse_device_identity_tag;
resp.result = wcom_cmd_get_device_identity(&resp.data.device_identity);
break;
default:
ESP_LOGW(TAG, "ch%d unknown cmd (which_params=%d)", link->link_id, cmd.which_params);
resp.result = WendyComResult_WENDY_COM_RESULT_UNKNOWN_ERROR;
break;
}

void *buf = _get_buffer(link, WendyComResponse_size);
pb_ostream_t out_stream = pb_ostream_from_buffer(buf, WendyComResponse_size);
pb_ostream_t sizing = PB_OSTREAM_SIZING;
if (!pb_encode(&sizing, WendyComResponse_fields, &resp)) {
ESP_LOGE(TAG, "ch%d pb_encode sizing: %s", link->link_id, PB_GET_ERROR(&sizing));
wcom_close(link->link_id);
return;
}

void *buf = _get_buffer(link, sizing.bytes_written);
pb_ostream_t out_stream = pb_ostream_from_buffer(buf, sizing.bytes_written);
if (!pb_encode(&out_stream, WendyComResponse_fields, &resp)) {
ESP_LOGE(TAG, "ch%d pb_encode response: %s", link->link_id, PB_GET_ERROR(&out_stream));
wcom_close(link->link_id);
Expand Down
24 changes: 24 additions & 0 deletions components/wendy_com/src/wendy_com_cmd.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <string.h>
#include "wendy_com_cmd.h"
#include "wendy_com_msg.pb.h"
#include "esp_log.h"
#include "esp_system.h"
#include "wendy_com_common.h"
#include <pb_encode.h>


static const char *TAG = "wcom_cmd";
Expand Down Expand Up @@ -89,6 +91,28 @@ WendyComResult wcom_cmd_app_stop(int client_id)
return WendyComResult_WENDY_COM_RESULT_OK;
}

static bool _encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
const char *str = *arg;
return pb_encode_tag_for_field(stream, field) &&
pb_encode_string(stream, (const uint8_t *)str, strlen(str));
}

WendyComResult wcom_cmd_get_device_identity(WendyComDeviceIdentity *out)
{
ESP_LOGI(TAG, "GET_DEVICE_IDENTITY");
const char *id = "???", *name = "???", *display_name = "???";
if (_app_delegate && _app_delegate->on_get_device_identity)
_app_delegate->on_get_device_identity(&id, &name, &display_name);
out->id.funcs.encode = _encode_string;
out->id.arg = (void *)id;
out->name.funcs.encode = _encode_string;
out->name.arg = (void *)name;
out->display_name.funcs.encode = _encode_string;
out->display_name.arg = (void *)display_name;
return WendyComResult_WENDY_COM_RESULT_OK;
}

void wcom_cmd_client_disconnected(int client_id)
{
if (client_id == _pushing_client_id) {
Expand Down
Loading
Loading