-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwipMailbox.cpp
102 lines (84 loc) · 3.43 KB
/
lwipMailbox.cpp
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
/**
* \file
* \brief Definitions of mailbox-related functions for lwIP
*
* \author Copyright (C) 2019-2022 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
*
* \par License
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "lwip/sys.h"
#include "distortos/DynamicRawFifoQueue.hpp"
#include <cassert>
#include <cstring>
static_assert(sizeof(sys_mbox_t) == sizeof(distortos::DynamicRawFifoQueue) &&
alignof(sys_mbox_t) == alignof(distortos::DynamicRawFifoQueue),
"sys_mbox_t doesn't match distortos::DynamicRawFifoQueue!");
namespace
{
/*---------------------------------------------------------------------------------------------------------------------+
| local objects
+---------------------------------------------------------------------------------------------------------------------*/
/// distortos_DynamicRawFifoQueue in an invalid state, real object will never match this one
const distortos_DynamicRawFifoQueue invalidMailbox {};
} // namespace
/*---------------------------------------------------------------------------------------------------------------------+
| global functions
+---------------------------------------------------------------------------------------------------------------------*/
u32_t sys_arch_mbox_fetch(sys_mbox_t* const mailbox, void** message, const u32_t timeoutMs)
{
void* dummyMessage;
if (message == nullptr)
message = &dummyMessage;
auto& dynamicRawFifoQueue = *reinterpret_cast<distortos::DynamicRawFifoQueue*>(mailbox);
const auto ret = timeoutMs == 0 ? dynamicRawFifoQueue.pop(*message) :
dynamicRawFifoQueue.tryPopFor(std::chrono::milliseconds{timeoutMs}, *message);
assert(ret == 0 || ret == ETIMEDOUT);
static_assert(SYS_ARCH_TIMEOUT != 0);
return ret == 0 ? 0 : SYS_ARCH_TIMEOUT;
}
u32_t sys_arch_mbox_tryfetch(sys_mbox_t* const mailbox, void** message)
{
void* dummyMessage;
if (message == nullptr)
message = &dummyMessage;
auto& dynamicRawFifoQueue = *reinterpret_cast<distortos::DynamicRawFifoQueue*>(mailbox);
const auto ret = dynamicRawFifoQueue.tryPop(*message);
return ret == 0 ? 0 : SYS_MBOX_EMPTY;
}
void sys_mbox_free(sys_mbox_t* const mailbox)
{
auto& dynamicRawFifoQueue = *reinterpret_cast<distortos::DynamicRawFifoQueue*>(mailbox);
dynamicRawFifoQueue.~DynamicRawFifoQueue();
}
err_t sys_mbox_new(sys_mbox_t* const mailbox, const int size)
{
new (mailbox) distortos::DynamicRawFifoQueue {sizeof(void*), static_cast<size_t>(size)};
return ERR_OK;
}
void sys_mbox_post(sys_mbox_t* const mailbox, void* const message)
{
auto& dynamicRawFifoQueue = *reinterpret_cast<distortos::DynamicRawFifoQueue*>(mailbox);
const auto ret = dynamicRawFifoQueue.push(message);
assert(ret == 0);
}
void sys_mbox_set_invalid(sys_mbox_t* const mailbox)
{
memcpy(mailbox, &invalidMailbox, sizeof(*mailbox));
}
err_t sys_mbox_trypost(sys_mbox_t* const mailbox, void* const message)
{
auto& dynamicRawFifoQueue = *reinterpret_cast<distortos::DynamicRawFifoQueue*>(mailbox);
const auto ret = dynamicRawFifoQueue.tryPush(message);
return ret == 0 ? ERR_OK : ERR_MEM;
}
err_t sys_mbox_trypost_fromisr(sys_mbox_t* const mailbox, void* message)
{
return sys_mbox_trypost(mailbox, message);
}
int sys_mbox_valid(sys_mbox_t* const mailbox)
{
const auto ret = memcmp(mailbox, &invalidMailbox, sizeof(*mailbox));
return ret != 0;
}