flipperzero-firmware/applications/nfc/dispatcher.c

58 lines
1.6 KiB
C
Raw Normal View History

2020-10-18 22:09:48 +00:00
#include "dispatcher.h"
#include <flipper.h>
#include <flipper_v2.h>
2020-10-18 22:09:48 +00:00
struct Dispatcher {
void* message;
size_t message_size;
osMessageQueueId_t mqueue;
osMutexId_t lock_mutex;
};
Dispatcher* dispatcher_alloc(size_t queue_size, size_t message_size) {
Dispatcher* dispatcher = furi_alloc(sizeof(Dispatcher));
dispatcher->message = furi_alloc(message_size);
dispatcher->message_size = message_size;
dispatcher->mqueue = osMessageQueueNew(queue_size, message_size, NULL);
furi_check(dispatcher->mqueue);
2020-10-18 22:09:48 +00:00
dispatcher->lock_mutex = osMutexNew(NULL);
furi_check(dispatcher->lock_mutex);
2020-10-18 22:09:48 +00:00
dispatcher_lock(dispatcher);
return dispatcher;
}
void dispatcher_free(Dispatcher* dispatcher) {
furi_assert(dispatcher);
2020-10-18 22:09:48 +00:00
free(dispatcher);
}
void dispatcher_send(Dispatcher* dispatcher, Message* message) {
furi_assert(dispatcher);
furi_assert(message);
furi_check(osMessageQueuePut(dispatcher->mqueue, message, 0, osWaitForever) == osOK);
2020-10-18 22:09:48 +00:00
}
// TODO: bad side-effect
2020-10-18 22:09:48 +00:00
void dispatcher_recieve(Dispatcher* dispatcher, Message* message) {
furi_assert(dispatcher);
furi_assert(message);
2020-10-18 22:09:48 +00:00
dispatcher_unlock(dispatcher);
furi_check(osMessageQueueGet(dispatcher->mqueue, message, NULL, osWaitForever) == osOK);
2020-10-18 22:09:48 +00:00
dispatcher_lock(dispatcher);
}
void dispatcher_lock(Dispatcher* dispatcher) {
furi_assert(dispatcher);
furi_check(osMutexAcquire(dispatcher->lock_mutex, osWaitForever) == osOK);
2020-10-18 22:09:48 +00:00
}
void dispatcher_unlock(Dispatcher* dispatcher) {
furi_assert(dispatcher);
furi_check(osMutexRelease(dispatcher->lock_mutex) == osOK);
2020-10-18 22:09:48 +00:00
}