somewhat fix newline

This commit is contained in:
Aurel Feer 2022-06-01 15:07:25 +02:00
parent e28c9dccde
commit e979d98a33
3 changed files with 25 additions and 27 deletions

View File

@ -24,6 +24,10 @@ static struct shelly_st_global * global_state;
#include "shelly_cmd.c"
bool is_newline(char c) {
return c == ASCII_NL || c == ASCII_EOF || c == ASCII_CR;
}
//prints a null terminated string
errval_t shelly_write_str(char * str) {
errval_t err;
@ -38,7 +42,7 @@ errval_t shelly_write_str(char * str) {
void shelly_reset_buffer(void) {
global_state->buffer_i = 0;
global_state->buf[0] = '\0';
memset(global_state->buf, 0, SHELLY_BUF_SIZE);
}
errval_t shelly_buffer_append(char c) {
@ -56,7 +60,6 @@ errval_t shelly_buffer_append(char c) {
__attribute__((__used__))
static void handle_lpuart_interrupt(void * arg) {
errval_t err;
SHELLY_DEBUG("[handle_lpuart_interrupt]\n");
assert(global_state);
char input_char;
@ -71,19 +74,22 @@ static void handle_lpuart_interrupt(void * arg) {
err = lpuart_putchar(global_state->uart_s, input_char);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character");
if (input_char == ASCII_NL || input_char == ASCII_EOF || input_char == ASCII_CR) {
err = lpuart_putchar(global_state->uart_s, '\n');
if (is_newline(input_char)) {
err = lpuart_putchar(global_state->uart_s, ASCII_CR);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character");
global_state->buf[global_state->buffer_i] = '\0';
global_state->buffer_i += 1;
err = shelly_write_str("\n\xd");
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to write string");
err = handle_shelly_cmd(global_state->buf, global_state->buffer_i);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while running command");
shelly_reset_buffer();
err = shelly_write_str("\x3>");
err = shelly_write_str("\n\xd>");
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to write string");
} else {
err = shelly_buffer_append(input_char);
@ -93,7 +99,6 @@ static void handle_lpuart_interrupt(void * arg) {
}
}
}
SHELLY_DEBUG("[handle_lpuart_interrupt] done\n");
}
static void handle_send_completed(void *arg, struct ump_send_queue_entry *entry) {
@ -105,7 +110,7 @@ static void handle_send_completed(void *arg, struct ump_send_queue_entry *entry)
static void send_response(struct shelly_st_local * local_state, errval_t err, size_t payload_size, void *payload) {
assert(local_state);
SHELLY_DEBUG("[send_response]\n");
// SHELLY_DEBUG("[send_response]\n");
struct shelly_header * header = malloc(sizeof(struct shelly_header));
assert(header);
header->type = SHELLY_MSG_RESPONSE;
@ -123,7 +128,7 @@ static void send_response(struct shelly_st_local * local_state, errval_t err, si
handle_send_completed,
NULL
);
SHELLY_DEBUG("[send_response] done\n");
// SHELLY_DEBUG("[send_response] done\n");
}
static void handle_client_request(void *arg, size_t header_size, void *header_buf, size_t payload_size);
@ -132,12 +137,8 @@ static void handle_payload(void *arg, size_t payload_size, void *payload) {
assert(local_state);
assert(global_state);
SHELLY_DEBUG("[handle_payload]\n");
// SHELLY_DEBUG("[handle_payload] client_id=%d\n", local_state->client_id);
// listen for the next request on this channel
ump_recv_header(local_state->recv_chan, handle_client_request, local_state);
SHELLY_DEBUG("[handle_payload] done\n");
}
static void handle_client_request(void *arg, size_t header_size, void *header_buf, size_t payload_size) {
@ -146,8 +147,6 @@ static void handle_client_request(void *arg, size_t header_size, void *header_bu
assert(local_state);
assert(global_state);
SHELLY_DEBUG("[handle_client_request]\n");
struct shelly_header * header = (struct shelly_header *) header_buf;
if (header->type == SHELLY_MSG_GETCHAR) {
@ -157,15 +156,16 @@ static void handle_client_request(void *arg, size_t header_size, void *header_bu
} else if (header->type == SHELLY_MSG_PUTCHAR) {
err = lpuart_putchar(global_state->uart_s, header->character);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character");
if (is_newline(header->character)) {
err = lpuart_putchar(global_state->uart_s, ASCII_CR);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character");
}
}
SHELLY_DEBUG("[handle_client_request] send_response\n");
send_response(local_state, SYS_ERR_OK, 0, NULL);
// skip payload
SHELLY_DEBUG("[handle_client_request] skip payload\n");
ump_recv_payload(local_state->recv_chan, NULL, handle_payload, local_state);
SHELLY_DEBUG("[handle_client_request] done\n");
}
__attribute__((__used__))
@ -199,7 +199,6 @@ static errval_t connection_callback(void *arg, struct capref cap) {
int main(int argc, char *argv[])
{
errval_t err;
debug_printf("welcome to shelly!\n");
global_state = calloc(sizeof(struct shelly_st_global), 1);
assert(global_state);
@ -251,12 +250,7 @@ int main(int argc, char *argv[])
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't alloc dest cap");
SHELLY_DEBUG("created interrupt cap\n");
char buf[256];
debug_print_cap_at_capref(buf, 256, inter_cap);
debug_printf(buf);
// struct waitset * ws = calloc(sizeof(struct waitset), 1);
// waitset_init(ws);
err = inthandler_setup(inter_cap, global_state->ws, MKCLOSURE(handle_lpuart_interrupt, NULL));
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't setup interrupt handler");
@ -272,11 +266,15 @@ int main(int argc, char *argv[])
SHELLY_DEBUG("enabled lpuart interrupt\n");
SHELLY_DEBUG("registering as shelly server\n");
struct ump_binding_server server;
err = ump_binding_register(&server, UMP_SERVER_SHELLY, connection_callback, NULL);
if (err_is_fail(err)) USER_PANIC_ERR(err, "Failed to register UMP server");
SHELLY_DEBUG("registered as shelly server\n");
err = shelly_write_str("welcome to Shelly(TM)(TM)!\n");
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to write welcome message");
while (true) {
err = event_dispatch(global_state->ws);
if (err_is_fail(err)) {

View File

@ -10,7 +10,7 @@
#ifndef SHELLY_H_
#define SHELLY_H_
// #define SHELLY_DEBUG_ON 1
#define SHELLY_DEBUG_ON 1
#if defined(SHELLY_DEBUG_ON)
#define SHELLY_DEBUG(x...) debug_printf("[SHELLY_DEBUG] " x);
@ -50,6 +50,7 @@ struct shelly_st_local {
struct ump_send_chan * send_chan;
};
bool is_newline(char c);
errval_t shelly_write_str(char * str);
void shelly_reset_buffer(void);
errval_t shelly_buffer_append(char c);

View File

@ -14,7 +14,6 @@ static void parse_cmd_line(char * line, int * argc, char ** argv) {
static errval_t shelly_cmd_echo(char * line) {
errval_t err;
SHELLY_DEBUG("[shelly_cmd_echo] argc=%d\n", argc);
err = shelly_write_str(line);
if (err_is_fail(err)) return err;
return SYS_ERR_OK;
@ -52,7 +51,7 @@ static errval_t shelly_cmd_run(char * line) {
}
static errval_t handle_shelly_cmd(char* line, size_t len) {
SHELLY_DEBUG("[handle_shelly_cmd] len=%d\n", len);
// SHELLY_DEBUG("[handle_shelly_cmd] len=%d\n", len);
if (len <= 1) {
return SYS_ERR_OK;
}