Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 245 lines (183 sloc) 7.16 kb
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
1 /*
2 * session.c
3 *
4 * Created on: Jul 18, 2012
5 * Author: Tyler
6 */
7
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
8 #include <stdlib.h>
9 #include <stdio.h>
bcdcf060 » triddle
2012-07-21 implemented session watchdog - client must send heartbeat at least ev…
10 #include <util/atomic.h>
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
11 #include <string.h>
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
12
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
13 #include "config.h"
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
14 #include "session.h"
15 #include "command.h"
16 #include "atomq.h"
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
17 #include "fault.h"
18 #include "message.h"
19 #include "timer.h"
20 #include "clock.h"
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
21 #include "processor.h"
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
22 #include "adc.h"
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
23
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
24 bool sessionActive;
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
25 volatile struct atomq *eventQueue;
bcdcf060 » triddle
2012-07-21 implemented session watchdog - client must send heartbeat at least ev…
26 bool gotHeartBeat;
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
27
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
28 static void session_start_broadcast(void);
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
29 static void session_handle_event_sessionEnd(volatile struct session_event *p);
30 static void session_watchdog_timer_cb(volatile struct timer *p);
31 void session_event_deliver_adcSampleReady(uint8_t port, uint8_t timeStamp, uint8_t data);
32 void session_subscription_timer_cb(volatile struct timer *p);
33
34 static void session_event_deliver(volatile struct session_event *event) {
35 if (! atomq_enqueue(eventQueue, false, (void *)event)) {
36 fault_fatal(FAULT_SESSION_EVENT_DELIVER_WOULD_BLOCK);
37 }
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
38 }
39
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
40 void session_event_deliver_sessionStart(void) {
41 static volatile struct session_event event = { session_event_sessionStart, { } };
42 session_event_deliver(&event);
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
43 }
44
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
45 static void session_handle_event_sessionStart(volatile struct session_event *p) {
46 uint8_t timer;
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
47
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
48 timer_delete_all();
49
50 timer = timer_create(SESSION_WATCHDOG_TIMEOUT, 0, true, session_watchdog_timer_cb, NULL);
51 timer_start(timer);
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
52
bcdcf060 » triddle
2012-07-21 implemented session watchdog - client must send heartbeat at least ev…
53 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
54 clock_reset();
55 processor_counter_reset();
56 atomq_reset(eventQueue);
57
58 sessionActive = 1;
59 }
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
60
61 command_send_response(COMMAND_NAME_SESSION_START, NULL, 0);
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
62 }
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
63
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
64 void session_event_deliver_sessionEnd(void) {
65 static volatile struct session_event event = { session_event_sessionEnd, { } };
66 session_event_deliver(&event);
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
67 }
68
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
69 static void session_handle_event_sessionEnd(volatile struct session_event *p) {
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
70 clock_reset();
71
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
72 timer_delete_all();
bcdcf060 » triddle
2012-07-21 implemented session watchdog - client must send heartbeat at least ev…
73
74 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
75 sessionActive = 0;
76 atomq_reset(eventQueue);
77 }
78
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
79 command_send_response(COMMAND_NAME_SESSION_END, NULL, 0);
80 session_start_broadcast();
81 }
82
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
83 void session_event_deliver_clockOverflow(void) {
84 static volatile struct session_event event = { session_event_clockOverflow, { } };
85 session_event_deliver(&event);
86
87 }
88
89 static void session_handle_event_clockOverflow(volatile struct session_event *p) {
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
90 static char buf = 0;
91
92 if (! sessionActive) {
93 return;
94 }
95
96 if (! message_send(31, false, &buf, 1)) {
97 fault_fatal(FAULT_SESSION_CLOCK_OVERFLOW_WOULD_BLOCK);
98 }
99 }
100
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
101 void session_event_deliver_processorCounterOverflow(void) {
102 static volatile struct session_event event = { session_event_processorCounterOverflow, { } };
103 session_event_deliver(&event);
104
105 }
106
107 static void session_handle_event_processorOverflow(volatile struct session_event *p) {
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
108 static char buf = 3;
109
110 if (! sessionActive) {
111 return;
112 }
113
114 if (! message_send(31, false, &buf, 1)) {
115 fault_fatal(FAULT_SESSION_PROCESSOR_OVERFLOW_WOULD_BLOCK);
116 }
117 }
118
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
119 void session_event_deliver_heartBeat(void) {
120 static volatile struct session_event event = { session_event_heartBeat, { } };
121 session_event_deliver(&event);
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
122 }
123
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
124 static void session_handle_event_heartBeat(volatile struct session_event *p) {
bcdcf060 » triddle
2012-07-21 implemented session watchdog - client must send heartbeat at least ev…
125 gotHeartBeat = true;
126 }
127
a16eaa65 » triddle
2012-07-29 Huge set of changes:
128 void session_event_deliver_adcSampleReady(uint8_t channel, uint8_t timeStamp, uint8_t data) {
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
129 static volatile struct session_event event = { session_event_adcSampleReady, { } };
130
57e1c9db » triddle
2012-07-23 ADC operation is reliable but jitter is worse; client tracks differen…
131
a16eaa65 » triddle
2012-07-29 Huge set of changes:
132 //event.data[0] = pin;
133 event.data[1] = timeStamp;
134 event.data[2] = data;
135 event.data[3] = channel;
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
136
137 session_event_deliver(&event);
138 }
139
140 static void session_handle_event_adcSampleReady(volatile struct session_event *currentEvent) {
141
57e1c9db » triddle
2012-07-23 ADC operation is reliable but jitter is worse; client tracks differen…
142 if (! message_send(currentEvent->data[3], false, (void *)&currentEvent->data[1], 2)) {
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
143 fault_fatal(FAULT_SESSION_ADC_SAMPLE_READY_WOULD_BLOCK);
144 }
145 }
146
572a4746 » triddle
2012-07-22 added in timer offset support for adc data subscription
147 void session_event_deliver_subscribe(uint8_t pin, uint8_t channel, uint16_t timerTop, uint16_t timerOffset) {
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
148 static volatile struct session_event event = { session_event_subscribe, { } };
149
150 event.data[0] = pin;
151 event.data[1] = channel;
152 event.data[2] = timerTop & 255;
153 event.data[3] = timerTop >> 8;
572a4746 » triddle
2012-07-22 added in timer offset support for adc data subscription
154 event.data[4] = timerOffset & 255;
155 event.data[5] = timerOffset >> 8;
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
156
157 session_event_deliver(&event);
158 }
159
160 static void session_handle_event_subscribe(volatile struct session_event *currentEvent) {
161 struct session_subscription *subscription = malloc(sizeof(struct session_subscription));
162 uint8_t timer;
163
164 if (subscription == NULL) {
165 fault_fatal(FAULT_SESSION_SUBSCRIPTION_MALLOC_FAILED);
166 }
167
168 subscription->pin = currentEvent->data[0];
169 subscription->channel = currentEvent->data[1];
170
171 subscription->timerTop = currentEvent->data[2] | currentEvent->data[3] << 8;
572a4746 » triddle
2012-07-22 added in timer offset support for adc data subscription
172 subscription->timerOffset = currentEvent->data[3] | currentEvent->data[4] << 8;
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
173
174 command_send_response(COMMAND_NAME_SUBSCRIBE, NULL, 0);
175
572a4746 » triddle
2012-07-22 added in timer offset support for adc data subscription
176 timer = timer_create(subscription->timerTop, subscription->timerOffset, true, session_subscription_timer_cb, subscription);
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
177 timer_start(timer);
178 }
179
180 static void session_handle_event(struct session_event *currentEvent) {
181 switch(currentEvent->id) {
182 case session_event_sessionStart: return session_handle_event_sessionStart(currentEvent);
183 case session_event_sessionEnd: return session_handle_event_sessionEnd(currentEvent);
184 case session_event_clockOverflow: return session_handle_event_clockOverflow(currentEvent);
185 case session_event_processorCounterOverflow: return session_handle_event_processorOverflow(currentEvent);
186 case session_event_heartBeat: return session_handle_event_heartBeat(currentEvent);
187 case session_event_adcSampleReady: return session_handle_event_adcSampleReady(currentEvent);
188 case session_event_subscribe: return session_handle_event_subscribe(currentEvent);
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
189 };
190
191 fault_fatal(FAULT_SESSION_HANDLE_EVENT_EXITED_SWITCH);
192 }
193
194 void session_send_broadcast(void) {
195 static uint8_t message = MESSAGE_SYSTEM_BEACON;
196 printf("Firmdata v%s ", FIRMDATA_VERSION);
197
198 message_send(MESSAGE_SYSTEM_CHANNEL, true, &message, sizeof(uint8_t));
199 }
200
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
201 void session_broadcast_timer_cb(volatile struct timer *p) {
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
202 session_send_broadcast();
203 }
204
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
205 void session_watchdog_timer_cb(volatile struct timer *p) {
206 if (! gotHeartBeat) {
207 session_handle_event_sessionEnd(NULL);
208 }
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
209
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
210 gotHeartBeat = false;
211 }
212
213 void session_subscription_timer_cb(volatile struct timer *p) {
214 struct session_subscription *subscription;
215
216 subscription = p->handlerArg;
217
a16eaa65 » triddle
2012-07-29 Huge set of changes:
218 adc_take_sample(subscription->pin, subscription->channel);
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
219 }
220
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
221 static void session_start_broadcast(void) {
222 uint8_t timer;
223
224 session_send_broadcast();
225
0cdfee5f » triddle
2012-08-03 Refactored client to be Moose based ; added in interim PWM output sup…
226 timer = timer_create(SESSION_BROADCAST_INTERVAL, 0, true, session_broadcast_timer_cb, NULL);
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
227 timer_start(timer);
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
228 }
229
230 void session_update(void) {
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
231 static struct session_event currentEvent;
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
232
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
233 while (atomq_dequeue(eventQueue, false, &currentEvent)) {
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
234 session_handle_event(&currentEvent);
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
235 }
236 }
237
238 void session_init(void) {
4e485671 » triddle
2012-07-21 moved clock overflow message sending into user space ; added support …
239 sessionActive = 0;
bcdcf060 » triddle
2012-07-21 implemented session watchdog - client must send heartbeat at least ev…
240 gotHeartBeat = false;
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
241
1edc70ba » triddle
2012-07-21 added ADC support; added command to subscribe to ADC data - jitter is…
242 eventQueue = atomq_alloc(SESSION_EVENT_QUEUE_SIZE, sizeof(struct session_event));
16a180ab » triddle
2012-07-20 command processing moved out of ISR; sessions can begin and end; star…
243
244 session_start_broadcast();
66963128 » triddle
2012-07-20 moved command processing outside of ISR; started support for a commun…
245 }
Something went wrong with that request. Please try again.