From d8d94a9c2f13b1a7c6d0c209f9757e3f1f122e0b Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Mon, 24 Oct 2022 19:55:48 +0200 Subject: added ecp example --- fw/fe310/phone/ecp.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 fw/fe310/phone/ecp.c (limited to 'fw/fe310/phone/ecp.c') diff --git a/fw/fe310/phone/ecp.c b/fw/fe310/phone/ecp.c new file mode 100644 index 0000000..6ce9c8f --- /dev/null +++ b/fw/fe310/phone/ecp.c @@ -0,0 +1,182 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "app/app.h" +#include "app/status.h" + +#include "sms.h" + +#include +#include + +#include "ecp.h" + +#define CTYPE_PHONE 0 +#define MTYPE_VOIP 0 +#define MTYPE_TEXT 1 + +static ECPContext ctx; +static ECPSocket sock; +static ECPConnHandler handler; + +int arc4random_alloc(void **rsp, size_t rsp_size, void **rsxp, size_t rsxp_size) { + *rsp = malloc(rsp_size); + *rsxp = malloc(rsxp_size); + + if ((*rsp == NULL) || (*rsxp == NULL)) { + if (*rsp) free(*rsp); + if (*rsp) free(*rsxp); + return -1; + } + return 0; +} + +static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { + printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); +} + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + APP_LOG(APP_LOG_DEBUG, "CONN OPEN\n"); + + return ECP_OK; +} + +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + switch (mtype) { + case MTYPE_VOIP: { + eos_i2s_spk_write(msg, msg_size); + break; + } + + case MTYPE_TEXT: { + APP_LOG(APP_LOG_DEBUG, "ECP TEXT:%s\n", msg); + break; + } + + } + + return msg_size; +} + +static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) { + ECPConnection *conn; + + conn = malloc(sizeof(ECPConnection)); + if (conn) ecp_conn_init(conn, sock, type); + return conn; +} + +static void conn_free(ECPConnection *conn) { + if (ecp_conn_is_gc(conn)) free(conn); +} + +void ecp_init(void) { + ECPDHKey key_perma; + int rv; + + rv = ecp_ctx_init(&ctx, handle_err, conn_new, conn_free); + if (rv) { + APP_LOG(APP_LOG_ERR, "CTX INIT ERR:%d\n", rv); + return; + } + + ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_PHONE, &handler); + + rv = ecp_dhkey_gen(&key_perma); + if (rv) { + APP_LOG(APP_LOG_ERR, "DHKEY GEN ERR:%d\n", rv); + return; + } + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + if (rv) { + APP_LOG(APP_LOG_ERR, "SOCK CREATE ERR:%d\n", rv); + return; + } + + rv = ecp_sock_open(&sock, NULL); + if (rv) { + APP_LOG(APP_LOG_ERR, "SOCK OPEN ERR:%d\n", rv); + return; + } + +} + +static void connect(void) { + ECPNode node = { + .key_perma.public = { + 0xf0, 0x30, 0xde, 0xb2, 0x82, 0xe8, 0x7c, 0x87, 0x7d, 0x62, 0x15, 0xc3, 0xd9, 0xfd, 0xe2, 0x25, + 0x6c, 0x02, 0xb0, 0x74, 0x35, 0xb3, 0xd6, 0xc2, 0xed, 0xa9, 0x6b, 0xf9, 0x74, 0x53, 0x36, 0x1c, + }, + .key_perma.valid = 1, + .addr = { + .host = {192,168,1,8}, + .port = 3001, + }, + }; + ECPConnection *conn; + int rv; + + conn = malloc(sizeof(ECPConnection)); + if (conn == NULL) { + APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n"); + return; + } + + ecp_conn_init(conn, &sock, CTYPE_PHONE); + rv = ecp_conn_open(conn, &node); + if (rv) { + APP_LOG(APP_LOG_ERR, "CONN OPEN ERR:%d\n", rv); + return; + } +} + +int ecp_app(EVEWindow *window, EVEViewStack *stack) { + EVEFormSpec spec[] = { + APP_SPACERW(APP_SCREEN_W,1), + }; + EVEPage *page = eve_form_create(window, stack, spec, APP_SPEC_SIZE(spec), ecp_uievt, ecp_close); + if (page == NULL) { + APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n"); + return EVE_ERR_NOMEM; + } + + return EVE_OK; +} + +int ecp_uievt(EVEPage *page, uint16_t evt, void *param) { + int ret = 0; + + switch (evt) { + case EVE_UIEVT_GEST_TOUCH: { + connect(); + break; + } + + default: { + ret = eve_form_uievt(page, evt, param); + break; + } + } + + return ret; +} + +void ecp_close(EVEPage *page) { + eve_form_destroy(page); +} -- cgit v1.2.3