-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage_helpers.h
125 lines (101 loc) · 3.36 KB
/
message_helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 QueueHandle_t 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);
}