Skip to content

Commit

Permalink
port: use same message format that is used for gen_server
Browse files Browse the repository at this point in the history
This will allow future changes, such as casts, etc...

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Dec 23, 2023
1 parent 9839e23 commit a2cc409
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion libs/eavmlib/src/port.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down
7 changes: 5 additions & 2 deletions src/platforms/esp32/components/avm_builtins/gpio_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions src/platforms/esp32/components/avm_builtins/i2c_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 4 additions & 3 deletions src/platforms/esp32/components/avm_builtins/spi_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 5 additions & 2 deletions src/platforms/esp32/components/avm_builtins/uart_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a2cc409

Please sign in to comment.