From a2cc409287fdbbe8541abea8e6300c4fa694414f Mon Sep 17 00:00:00 2001 From: Davide Bettio Date: Sat, 23 Dec 2023 18:58:42 +0100 Subject: [PATCH] port: use same message format that is used for gen_server This will allow future changes, such as casts, etc... Signed-off-by: Davide Bettio --- libs/eavmlib/src/port.erl | 2 +- src/platforms/esp32/components/avm_builtins/gpio_driver.c | 7 +++++-- src/platforms/esp32/components/avm_builtins/i2c_driver.c | 6 ++++-- src/platforms/esp32/components/avm_builtins/spi_driver.c | 7 ++++--- src/platforms/esp32/components/avm_builtins/uart_driver.c | 7 +++++-- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libs/eavmlib/src/port.erl b/libs/eavmlib/src/port.erl index 934208cc90..ec5c032e06 100644 --- a/libs/eavmlib/src/port.erl +++ b/libs/eavmlib/src/port.erl @@ -68,7 +68,7 @@ call(Port, Message) -> -spec call(pid(), Message :: term(), Timeout :: timeout()) -> term() | {error, timeout}. call(Port, Message, Timeout) -> MonitorRef = monitor(port, Port), - Port ! {self(), MonitorRef, Message}, + Port ! {'$call', {self(), MonitorRef}, Request}, Result = receive {'DOWN', MonitorRef, port, Port, normal} -> diff --git a/src/platforms/esp32/components/avm_builtins/gpio_driver.c b/src/platforms/esp32/components/avm_builtins/gpio_driver.c index 7c9b9a63a5..8ce3d64b59 100644 --- a/src/platforms/esp32/components/avm_builtins/gpio_driver.c +++ b/src/platforms/esp32/components/avm_builtins/gpio_driver.c @@ -479,8 +479,12 @@ static NativeHandlerResult consume_gpio_mailbox(Context *ctx) { Message *message = mailbox_first(&ctx->mailbox); term msg = message->message; - term pid = term_get_tuple_element(msg, 0); + term from = term_get_tuple_element(msg, 1); term req = term_get_tuple_element(msg, 2); + + term pid = term_get_tuple_element(from, 0); + term ref = term_get_tuple_element(from, 1); + term cmd_term = term_get_tuple_element(req, 0); int local_process_id = term_to_local_process_id(pid); @@ -522,7 +526,6 @@ static NativeHandlerResult consume_gpio_mailbox(Context *ctx) if (UNLIKELY(memory_ensure_free_with_roots(ctx, 3, 1, &ret, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) { ret_msg = OUT_OF_MEMORY_ATOM; } else { - term ref = term_get_tuple_element(msg, 1); ret_msg = create_pair(ctx, ref, ret); } diff --git a/src/platforms/esp32/components/avm_builtins/i2c_driver.c b/src/platforms/esp32/components/avm_builtins/i2c_driver.c index 066c7216a9..39a53a80c7 100644 --- a/src/platforms/esp32/components/avm_builtins/i2c_driver.c +++ b/src/platforms/esp32/components/avm_builtins/i2c_driver.c @@ -547,9 +547,12 @@ static NativeHandlerResult i2cdriver_consume_mailbox(Context *ctx) { Message *message = mailbox_first(&ctx->mailbox); term msg = message->message; - term pid = term_get_tuple_element(msg, 0); + term from = term_get_tuple_element(msg, 1); term req = term_get_tuple_element(msg, 2); + term pid = term_get_tuple_element(from, 0); + term ref = term_get_tuple_element(from, 1); + #ifdef ENABLE_TRACE TRACE("message: "); term_display(stdout, msg, ctx); @@ -601,7 +604,6 @@ static NativeHandlerResult i2cdriver_consume_mailbox(Context *ctx) if (UNLIKELY(memory_ensure_free_with_roots(ctx, 3, 1, &ret, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) { ret_msg = OUT_OF_MEMORY_ATOM; } else { - term ref = term_get_tuple_element(msg, 1); ret_msg = create_pair(ctx, ref, ret); } diff --git a/src/platforms/esp32/components/avm_builtins/spi_driver.c b/src/platforms/esp32/components/avm_builtins/spi_driver.c index 89d979a4a5..87f7d44b9d 100644 --- a/src/platforms/esp32/components/avm_builtins/spi_driver.c +++ b/src/platforms/esp32/components/avm_builtins/spi_driver.c @@ -602,10 +602,12 @@ static NativeHandlerResult spidriver_consume_mailbox(Context *ctx) { Message *message = mailbox_first(&ctx->mailbox); term msg = message->message; - term pid = term_get_tuple_element(msg, 0); - term ref = term_get_tuple_element(msg, 1); + term from = term_get_tuple_element(msg, 1); term req = term_get_tuple_element(msg, 2); + term pid = term_get_tuple_element(from, 0); + term ref = term_get_tuple_element(from, 1); + term cmd_term = term_get_tuple_element(req, 0); int local_process_id = term_to_local_process_id(pid); @@ -652,7 +654,6 @@ static NativeHandlerResult spidriver_consume_mailbox(Context *ctx) if (UNLIKELY(memory_ensure_free_with_roots(ctx, 3, 1, &ret, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) { ret_msg = OUT_OF_MEMORY_ATOM; } else { - ref = term_get_tuple_element(msg, 1); ret_msg = create_pair(ctx, ref, ret); } diff --git a/src/platforms/esp32/components/avm_builtins/uart_driver.c b/src/platforms/esp32/components/avm_builtins/uart_driver.c index 12a9b617ae..f1f2b3aaa2 100644 --- a/src/platforms/esp32/components/avm_builtins/uart_driver.c +++ b/src/platforms/esp32/components/avm_builtins/uart_driver.c @@ -452,9 +452,12 @@ static NativeHandlerResult uart_driver_consume_mailbox(Context *ctx) while (mailbox_has_next(&ctx->mailbox)) { Message *message = mailbox_first(&ctx->mailbox); term msg = message->message; - term pid = term_get_tuple_element(msg, 0); - term ref = term_get_tuple_element(msg, 1); + term from = term_get_tuple_element(msg, 1); term req = term_get_tuple_element(msg, 2); + + term pid = term_get_tuple_element(from, 0); + term ref = term_get_tuple_element(from, 1); + uint64_t ref_ticks = term_to_ref_ticks(ref); int local_pid = term_to_local_process_id(pid);