Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
Channel.h
Go to the documentation of this file.
1 
19 #ifndef SCHEDULER_CHANNEL_H
20 #define SCHEDULER_CHANNEL_H
21 
22 #include <Scheduler.h>
23 
24 class Channel {
25 public:
29  Channel() :
30  m_buf(NULL),
31  m_max(0),
32  m_size(0),
33  m_ticket(0),
34  m_serving(0)
35  {}
36 
46  int send(const void* buf, size_t size)
47  {
48  // Take a ticket and wait for service
49  uint8_t ticket = m_ticket++;
50  await(ticket == m_serving && m_buf != NULL);
51 
52  // Check that the receiver buffer can hold the message
53  if (size > m_max) {
54  m_serving += 1;
55  return (-1);
56  }
57 
58  // Copy message to receiver buffer
59  memcpy(m_buf, buf, size);
60  m_size = size;
61  return (size);
62  }
63 
71  int recv(void* buf, size_t size)
72  {
73  // Wait for the channel to be free for receive
74  await(m_buf == NULL);
75  m_buf = buf;
76  m_max = size;
77  m_size = 0;
78 
79  // Wait for message from sender
80  await(m_size != 0);
81  int res = m_size;
82  m_buf = NULL;
83  m_max = 0;
84  m_size = 0;
85  m_serving += 1;
86 
87  // Return size of received message
88  return (res);
89  }
90 
91 protected:
92  void* m_buf;
93  volatile size_t m_max;
94  volatile size_t m_size;
95  volatile uint8_t m_ticket;
96  volatile uint8_t m_serving;
97 };
98 
99 #endif
void * m_buf
Message buffer.
Definition: Channel.h:92
int recv(void *buf, size_t size)
Definition: Channel.h:71
static char * buf
Definition: ShellTask.h:31
#define await(cond)
Definition: Scheduler.h:162
volatile uint8_t m_ticket
Sender ticket.
Definition: Channel.h:95
Channel()
Definition: Channel.h:29
volatile size_t m_max
Max message size.
Definition: Channel.h:93
volatile uint8_t m_serving
Next sender ticket.
Definition: Channel.h:96
volatile size_t m_size
Actual message size.
Definition: Channel.h:94
int send(const void *buf, size_t size)
Definition: Channel.h:46