Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 99 lines (72 sloc) 1.944 kb
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
1 /*
2 * message.c
3 *
4 * Created on: Jul 13, 2012
5 * Author: Tyler
6 */
7
8 #include <util/atomic.h>
9 #include <stdlib.h>
10 #include <stdio.h>
e3b97fb4 »
2012-07-13 start of command support ; stderr and stdout are functional as messag…
11 #include <stdbool.h>
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
12
13 #include "message.h"
14 #include "fault.h"
15
16 volatile struct atomq *messageBuf;
17
18 #define MESSAGE_HEADER_LEN 1
19 #define MESSAGE_MAX_SIZE 7
20
21 void message_set_buffer(volatile struct atomq *queue) {
22 messageBuf = queue;
23 }
24
25 static bool message_send_nb(uint8_t port, void *src, uint8_t len) {
26 static uint8_t i, header;
27
28 if (len > MESSAGE_MAX_SIZE) {
29 fault_fatal(FAULT_MESSAGE_MAX_LENGTH_EXCEEDED);
30 }
31
66963128 »
2012-07-20 moved command processing outside of ISR; started support for a commun…
32 header = port << 3;
33 header |= len;
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
34
35 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
36 if (messageBuf == NULL) {
37 fault_fatal(FAULT_MESSAGE_BUFFER_WAS_NULL);
38 }
39
59ab516a »
2012-07-13 clock and timers added
40 if (atomq_slots_available(messageBuf) < len + MESSAGE_HEADER_LEN) {
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
41 return false;
42 }
43
44 if (! atomq_enqueue(messageBuf, false, &header)) {
45 fault_fatal(FAULT_MESSAGE_HEADER_SEND_WOULD_BLOCK_ON_ENQUEUE);
46 }
47
48 for(i = 0; i < len; i++) {
49 if (! atomq_enqueue(messageBuf, false, src)) {
50 fault_fatal(FAULT_MESSAGE_HEADER_BODY_WOULD_BLOCK_ON_ENQUEUE);
51 }
52
53 src++;
54 }
55 }
56
57 return true;
58 }
59
60 bool message_send(uint8_t port, bool shouldBlock, void *src, uint8_t len) {
61 static bool retval;
62
63 while(1) {
64 retval = message_send_nb(port, src, len);
65
66 if (retval || ! shouldBlock) {
67 return retval;
68 }
69 }
70
71 return true;
72 }
73
e3b97fb4 »
2012-07-13 start of command support ; stderr and stdout are functional as messag…
74 bool message_send_stdio(uint8_t fd, bool shouldBlock, char c) {
75 static char buf[2];
76
77 buf[0] = fd;
78 buf[1] = c;
79
80 return message_send(0, shouldBlock, buf, 2);
81 }
82
83 static int message_putchar_stdout(char c, FILE *fh) {
84 message_send_stdio(1, true, c);
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
85 return 0;
86 }
87
e3b97fb4 »
2012-07-13 start of command support ; stderr and stdout are functional as messag…
88 static int message_putchar_stderr(char c, FILE *fh) {
89 message_send_stdio(2, true, c);
90 return 0;
91 }
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
92
93 void message_init(void) {
e3b97fb4 »
2012-07-13 start of command support ; stderr and stdout are functional as messag…
94 stdout = fdevopen(message_putchar_stdout, NULL);
95 stderr = fdevopen(message_putchar_stderr, NULL);
2374a209 »
2012-07-13 added in message sending support; stdout is being sent via messages
96 }
97
98
Something went wrong with that request. Please try again.