Skip to content

Commit

Permalink
Add missing message_helpers.h
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Bettio <[email protected]>
  • Loading branch information
bettio committed Mar 3, 2024
1 parent b20b547 commit c80265a
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions message_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* This file is part of AtomGL.
*
* Copyright 2022 Davide Bettio <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include <esp_heap_caps.h>

#include <atom.h>
#include <bif.h>
#include <context.h>
#include <debug.h>
#include <defaultatoms.h>
#include <globalcontext.h>
#include <interop.h>
#include <mailbox.h>
#include <module.h>
#include <port.h>
#include <sys.h>
#include <term.h>
#include <utils.h>

#include <esp32_sys.h>

#include <trace.h>

#include <math.h>

struct PendingReply
{
uint64_t pending_call_ref_ticks;
term pending_call_pid;
};

static xQueueHandle display_messages_queue;

static NativeHandlerResult display_driver_consume_mailbox(Context *ctx);

static void send_message(term pid, term message, GlobalContext *global);

static void process_message(Message *message, Context *ctx)
{
GenMessage gen_message;
if (UNLIKELY(port_parse_gen_message(message->message, &gen_message) != GenCallMessage)) {
fprintf(stderr, "Received invalid message.");
AVM_ABORT();
}

term req = gen_message.req;
if (UNLIKELY(!term_is_tuple(req) || term_get_tuple_arity(req) < 1)) {
AVM_ABORT();
}
term cmd = term_get_tuple_element(req, 0);

if (cmd == context_make_atom(ctx, "\x6"
"update")) {
term display_list = term_get_tuple_element(req, 1);
do_update(ctx, display_list);

} else {
#if REPORT_UNEXPECTED_MSGS
fprintf(stderr, "display: ");
term_display(stderr, req, ctx);
fprintf(stderr, "\n");
#endif
}

BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2) + REF_SIZE, heap);
term return_tuple = term_alloc_tuple(2, &heap);
term_put_tuple_element(return_tuple, 0, gen_message.ref);
term_put_tuple_element(return_tuple, 1, OK_ATOM);

send_message(gen_message.pid, return_tuple, ctx->global);
END_WITH_STACK_HEAP(heap, ctx->global);
}

static void process_messages(void *arg)
{
struct SPI *args = arg;

while (true) {
Message *message;
xQueueReceive(display_messages_queue, &message, portMAX_DELAY);
process_message(message, args->ctx);

BEGIN_WITH_STACK_HEAP(1, temp_heap);
mailbox_message_dispose(&message->base, &temp_heap);
END_WITH_STACK_HEAP(temp_heap, args->ctx->global);
}
}

static NativeHandlerResult display_driver_consume_mailbox(Context *ctx)
{
MailboxMessage *mbox_msg = mailbox_take_message(&ctx->mailbox);
Message *msg = CONTAINER_OF(mbox_msg, Message, base);

xQueueSend(display_messages_queue, &msg, 1);

return NativeContinue;
}

static void send_message(term pid, term message, GlobalContext *global)
{
int local_process_id = term_to_local_process_id(pid);
globalcontext_send_message(global, local_process_id, message);
}

0 comments on commit c80265a

Please sign in to comment.