summaryrefslogtreecommitdiff
path: root/fw/fe310/phone/modem.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-09-04 18:37:42 +0200
committerUros Majstorovic <majstor@majstor.org>2022-09-04 18:37:42 +0200
commitfc98d3809e0db36d634f290417b9152f87f83e3e (patch)
treed7c0cbb883571dccfcd4028d8b7b2a2144fc2d2b /fw/fe310/phone/modem.c
parent07e6abe5d5a1813298805bea2bf9d62ad895aaaa (diff)
new phone firmware
Diffstat (limited to 'fw/fe310/phone/modem.c')
-rw-r--r--fw/fe310/phone/modem.c197
1 files changed, 197 insertions, 0 deletions
diff --git a/fw/fe310/phone/modem.c b/fw/fe310/phone/modem.c
new file mode 100644
index 0000000..4c8d4e5
--- /dev/null
+++ b/fw/fe310/phone/modem.c
@@ -0,0 +1,197 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <eos.h>
+#include <soc/uart.h>
+#include <dev/net.h>
+#include <net/cell.h>
+
+#include <eve/eve.h>
+#include <eve/eve_kbd.h>
+#include <eve/eve_text.h>
+#include <eve/eve_font.h>
+
+#include <eve/screen/window.h>
+#include <eve/screen/page.h>
+#include <eve/screen/form.h>
+
+#include "app/app.h"
+
+#include "cell.h"
+#include "modem.h"
+
+typedef struct {
+ uint32_t mem;
+ EVEText text;
+ EVEViewStack *stack;
+} VParam;
+
+static void key_down(void *p, int c) {
+ EVEView *view = p;
+ VParam *param = view->param;
+ EVEText *text= &param->text;
+ unsigned char *buf;
+ uint16_t offset;
+ int i;
+
+ if (c == 0x11) {
+ modem_close(view);
+ return;
+ }
+
+ buf = eos_cell_uart_data_buffer(&offset);
+ i = 1;
+ if (c == '\n') {
+ buf[0] = '\r';
+ buf[1] = '\n';
+ i++;
+ } else {
+ buf[0] = c;
+ }
+ eos_cell_send_buffer(buf, i, offset, 0);
+
+ eve_text_scroll0(text);
+}
+
+static void handle_uart(unsigned char type) {
+ int i, c;
+ unsigned char *buf;
+ uint16_t offset;
+
+ c = eos_uart_getc(0);
+ if (c == EOS_ERR_EMPTY) return;
+
+ if (c == 0x11) {
+ EVEView *view = app_search_view("main");
+
+ eve_select();
+ modem_close(view);
+ eve_touch_clear_opt();
+ eve_window_root_draw(view->window->root);
+ eve_deselect();
+ return;
+ }
+
+ buf = eos_cell_uart_data_buffer(&offset);
+ buf[0] = c;
+ i = 1;
+ while ((c = eos_uart_getc(0)) != EOS_ERR_EMPTY) {
+ buf[i++] = c;
+ if (i == EOS_NET_SIZE_BUF - offset) break;
+ }
+ eos_cell_send_buffer(buf, i, offset, 0);
+
+ eos_uart_rxwm_set(0);
+}
+
+static void handle_cell_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
+ EVEView *view;
+ VParam *param;
+
+ view = app_search_view("main");
+ param = view->param;
+
+ if (type == EOS_CELL_MTYPE_UART_DATA) {
+ EVEText *text = &param->text;
+ int i;
+
+ eve_select();
+ for (i=1; i<len; i++) {
+ if (buffer[i] != '\r') {
+ eve_text_putc(text, buffer[i]);
+ putchar(buffer[i]);
+ }
+ }
+ if (text->dirty) {
+ text->dirty = 0;
+ eve_window_root_draw(view->window->root);
+ }
+ eve_deselect();
+ eos_net_free(buffer, 0);
+ } else {
+ cell_msg_handler(type, buffer, len);
+ }
+}
+
+static uint8_t modem_draw(EVEView *view, uint8_t tag0) {
+ VParam *param = view->param;
+ EVEText *text = &param->text;
+
+ tag0 = eve_view_clear(view, tag0, 0);
+ return eve_text_draw(text, tag0);
+}
+
+static int modem_touch(EVEView *view, EVETouch *touch, uint16_t evt, uint8_t tag0) {
+ VParam *param = view->param;
+ EVEText *text = &param->text;
+
+ return eve_text_touch(text, touch, evt, tag0);
+}
+
+int modem_app(EVEWindow *window, EVEViewStack *stack) {
+ unsigned char *buf;
+ EVEWindowRoot *root = window->root;
+ EVEKbd *kbd = eve_window_kbd(window);
+ EVERect g = {0, 60, 480, 512};
+ EVEView *view;
+ VParam *param;
+ int rv;
+
+ view = eve_malloc(sizeof(EVEView));
+ if (view == NULL) {
+ rv = EVE_ERR_NOMEM;
+ APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n");
+ return rv;
+ }
+ param = eve_malloc(sizeof(VParam));
+ if (param == NULL) {
+ rv = EVE_ERR_NOMEM;
+ eve_free(view);
+ APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n");
+ return rv;
+ }
+ param->mem = root->mem_next;
+ param->stack = stack;
+ eve_text_init(&param->text, &g, 30, 16, 200, root->mem_next, &root->mem_next);
+ eve_view_init(view, window, modem_draw, modem_touch, NULL, param);
+
+ eve_kbd_set_handler(kbd, key_down, view);
+ eve_window_kbd_attach(window);
+
+ eos_uart_flush_rx();
+
+ eos_uart_set_handler(EOS_UART_ETYPE_RX, handle_uart);
+ eos_net_acquire_for_evt(EOS_EVT_UART | EOS_UART_ETYPE_RX, 1);
+ eos_uart_rxwm_set(0);
+
+ eos_cell_set_handler(EOS_CELL_MTYPE_DEV, handle_cell_msg);
+ eos_cell_uart_take(NULL, 0);
+
+ return EVE_OK;
+}
+
+void modem_close(EVEView *view) {
+ VParam *param = view->param;
+ EVEWindow *window = view->window;
+ EVEWindowRoot *root = window->root;
+ EVEKbd *kbd = eve_window_kbd(window);
+ EVEViewStack *stack = param->stack;
+
+ eos_cell_uart_give(NULL, 0);
+
+ eos_uart_rxwm_clear();
+ eos_uart_set_handler(EOS_UART_ETYPE_RX, NULL);
+ eos_net_acquire_for_evt(EOS_EVT_UART | EOS_UART_ETYPE_RX, 0);
+
+ eos_cell_set_handler(EOS_CELL_MTYPE_DEV, cell_msg_handler);
+
+ root->mem_next = param->mem;
+ eve_window_kbd_detach(window);
+ eve_kbd_set_handler(kbd, NULL, NULL);
+
+ eve_free(param);
+ eve_free(view);
+ eve_stack_back(stack, window);
+}