2020-10-18 22:09:48 +00:00
|
|
|
#include "dispatcher.h"
|
|
|
|
|
|
|
|
#include <flipper.h>
|
2020-10-29 06:27:17 +00:00
|
|
|
#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);
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_check(dispatcher->mqueue);
|
2020-10-18 22:09:48 +00:00
|
|
|
|
|
|
|
dispatcher->lock_mutex = osMutexNew(NULL);
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_check(dispatcher->lock_mutex);
|
2020-10-18 22:09:48 +00:00
|
|
|
dispatcher_lock(dispatcher);
|
|
|
|
|
|
|
|
return dispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispatcher_free(Dispatcher* dispatcher) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(dispatcher);
|
2020-10-18 22:09:48 +00:00
|
|
|
free(dispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispatcher_send(Dispatcher* dispatcher, Message* message) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(dispatcher);
|
|
|
|
furi_assert(message);
|
|
|
|
furi_check(osMessageQueuePut(dispatcher->mqueue, message, 0, osWaitForever) == osOK);
|
2020-10-18 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:19:43 +00:00
|
|
|
// TODO: bad side-effect
|
2020-10-18 22:09:48 +00:00
|
|
|
void dispatcher_recieve(Dispatcher* dispatcher, Message* message) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(dispatcher);
|
|
|
|
furi_assert(message);
|
2020-10-18 22:09:48 +00:00
|
|
|
dispatcher_unlock(dispatcher);
|
2020-10-29 06:27:17 +00:00
|
|
|
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) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(dispatcher);
|
|
|
|
furi_check(osMutexAcquire(dispatcher->lock_mutex, osWaitForever) == osOK);
|
2020-10-18 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dispatcher_unlock(Dispatcher* dispatcher) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(dispatcher);
|
|
|
|
furi_check(osMutexRelease(dispatcher->lock_mutex) == osOK);
|
2020-10-18 22:09:48 +00:00
|
|
|
}
|