1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/chardev_thread.c

129 lines
3.2 KiB
C
Raw Normal View History

/**
* Character device messaging loop.
*
* Copyright (C) 2013, INRIA.
*
2013-11-22 20:47:05 +01:00
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup chardev
* @{
* @file chardev_thread.c
* @brief Runs an infinite loop in a separate thread to handle access to character devices such as uart.
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @}
*/
2010-09-30 15:10:39 +02:00
#include <stdio.h>
#include <errno.h>
#include "kernel.h"
#include "irq.h"
#include "thread.h"
#include "msg.h"
#include "ringbuffer.h"
#include "posix_io.h"
2010-09-30 15:10:39 +02:00
#define ENABLE_DEBUG (0)
#include "debug.h"
2010-09-30 15:10:39 +02:00
static int min(int a, int b)
{
if (b > a) {
return a;
}
else {
return b;
}
2010-09-30 15:10:39 +02:00
}
void chardev_loop(ringbuffer_t *rb)
{
2011-03-08 10:54:40 +01:00
msg_t m;
2010-09-30 15:10:39 +02:00
int pid = thread_getpid();
int reader_pid = -1;
struct posix_iop_t *r = NULL;
2010-09-30 15:10:39 +02:00
puts("UART0 thread started.");
while (1) {
2010-09-30 15:10:39 +02:00
msg_receive(&m);
if (m.sender_pid != pid) {
2013-12-23 18:18:33 +01:00
DEBUG("Receiving message from another thread: ");
switch(m.type) {
2010-09-30 15:10:39 +02:00
case OPEN:
2013-12-23 18:18:33 +01:00
DEBUG("OPEN\n");
if (reader_pid == -1) {
2010-09-30 15:10:39 +02:00
reader_pid = m.sender_pid;
/* no error */
m.content.value = 0;
}
else {
2010-09-30 15:10:39 +02:00
m.content.value = -EBUSY;
}
msg_reply(&m, &m);
2010-09-30 15:10:39 +02:00
break;
2010-09-30 15:10:39 +02:00
case READ:
2013-12-23 18:18:33 +01:00
DEBUG("READ\n");
if (m.sender_pid != reader_pid) {
2010-09-30 15:10:39 +02:00
m.content.value = -EINVAL;
r = NULL;
2010-09-30 15:10:39 +02:00
msg_reply(&m, &m);
}
else {
r = (struct posix_iop_t *)(void*)m.content.ptr;
2010-09-30 15:10:39 +02:00
}
2010-09-30 15:10:39 +02:00
break;
2010-09-30 15:10:39 +02:00
case CLOSE:
2013-12-23 18:18:33 +01:00
DEBUG("CLOSE\n");
if (m.sender_pid == reader_pid) {
2010-09-30 15:10:39 +02:00
DEBUG("uart0_thread: closing file from %i\n", reader_pid);
reader_pid = -1;
r = NULL;
m.content.value = 0;
}
else {
2010-09-30 15:10:39 +02:00
m.content.value = -EINVAL;
}
msg_reply(&m, &m);
2010-09-30 15:10:39 +02:00
break;
2010-09-30 15:10:39 +02:00
default:
2013-12-23 18:18:33 +01:00
DEBUG("UNKNOWN\n");
2010-09-30 15:10:39 +02:00
m.content.value = -EINVAL;
msg_reply(&m, &m);
}
}
if (rb->avail && (r != NULL)) {
2013-12-23 18:18:33 +01:00
DEBUG("Data is available\n");
2010-09-30 15:10:39 +02:00
int state = disableIRQ();
int nbytes = min(r->nbytes, rb->avail);
DEBUG("uart0_thread [%i]: sending %i bytes received from %i to pid %i\n", pid, nbytes, m.sender_pid, reader_pid);
2010-09-30 15:10:39 +02:00
rb_get_elements(rb, r->buffer, nbytes);
r->nbytes = nbytes;
m.sender_pid = reader_pid;
m.type = OPEN;
m.content.ptr = (char *)r;
2010-09-30 15:10:39 +02:00
msg_reply(&m, &m);
r = NULL;
restoreIRQ(state);
}
}
}