OOSMOS Async Example
1 Introduction
2 Async on Various Platforms
2.1 Async on PIC32
2.2 Async on Windows
1 Introduction
2 Async on Various Platf...
2.1 Async on PIC32
2.2 Async on Windows
1 Introduction
2 Async on Various Pla...
2.1 Async on PIC32
2.2 Async on Windows
1 Introduction
2 Async on Various P...
2.1 Async on PIC32
2.2 Async on Windows

1 Introduction

This is a comprehensive example of OOSMOS's Async call capabilities. In order to include all the different Async call types, we use the asynctest class which tests them all.
Because Async calls must be made from within an oosmos_INSTATE event and because the tests are granular, we test a little bit in each state. See Figure 1 that shows the state machine — a test per state.
Figure 1. asynctest State Chart
This is the asynctest source code:
1
22
23
24
25
26
27
28
29
30
[GPLv2]
 
#ifndef _asynctest_h
#define _asynctest_h
 
typedef struct asynctestTag asynctest;
 
extern asynctest * asynctestNew(void);
 
#endif
asynctest.h
1
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
[GPLv2]
 
#include <stdlib.h>
#include "oosmos.h"
#include "prt.h"
#include "asynctest.h"
 
//
// Change the following #undef to #define to enable debug output.
//
#undef asynctestDEBUG
 
//
// Adjust this in order to preallocate all 'asynctest' objects.
// Use 1 for a memory constrained environment.
//
#ifndef MAX_ASYNCTEST
#define MAX_ASYNCTEST 1
#endif
 
static int TotalTests = 11;
static int AsyncTestObjects = 0;
static int Successes = 0;
static int Failures = 0;
 
enum {
eventDone = 1,
eventTimedOut,
eventPrint,
};
 
#ifdef asynctestDEBUG
#define NameCase(Name) case Name: return #Name;
 
static const char * EventNames(int EventCode)
{
switch(EventCode) {
NameCase(eventDone)
NameCase(eventTimedOut)
NameCase(eventPrint)
default: return "--No Event Name--";
}
}
#endif
 
struct asynctestTag
{
oosmos_sStateMachine (StateMachine, oosmos_sEvent, 10);
oosmos_sOrtho Ortho_State;
oosmos_sOrthoRegion Ortho_RegionA_State;
oosmos_sLeaf Ortho_RegionA_TestA_State;
oosmos_sLeaf Ortho_RegionA_TestB_State;
oosmos_sLeaf Ortho_RegionA_TestC_State;
oosmos_sLeaf Ortho_RegionA_TestD_State;
oosmos_sLeaf Ortho_RegionA_TestE_State;
oosmos_sLeaf Ortho_RegionA_TestF_State;
oosmos_sLeaf Ortho_RegionA_TestG_State;
oosmos_sLeaf Ortho_RegionA_Done_State;
oosmos_sOrthoRegion Ortho_RegionB_State;
oosmos_sLeaf Ortho_RegionB_Idle_State;
};
 
static int ConditionRandom(int Range)
{
return rand() % Range == 0;
}
 
static bool ConditionTrue(void)
{
return true;
}
 
static bool ConditionFalse(void)
{
return false;
}
 
 
//
// Test oosmos_AsyncDelayMS.
//
static bool Ortho_RegionA_TestA_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncDelayMS...\n");
oosmos_AsyncDelayMS(pRegion, 100);
oosmos_AsyncEnd(pRegion);
 
return oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_TestB_State);
}
 
return false;
}
 
//
// oosmos_AsyncWaitCond
// oosmos_AsyncWaitCond_TimeoutMS
//
static bool Ortho_RegionA_TestB_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
bool TimedOut;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncWaitCond...\n");
 
oosmos_AsyncWaitCond(pRegion, ConditionRandom(2));
 
prtFormatted("Test AsyncWaitCond_TimeoutMS...\n");
 
oosmos_AsyncWaitCond_TimeoutMS(pRegion, 100, &TimedOut,
ConditionTrue()
);
Successes += (TimedOut == false);
 
oosmos_AsyncWaitCond_TimeoutMS(pRegion, 100, &TimedOut,
ConditionFalse()
);
Successes += (TimedOut == true);
oosmos_AsyncEnd(pRegion);
 
return oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_TestC_State);
}
 
return false;
}
 
//
// oosmos_AsyncWaitCond_TimeoutMS_Event
//
static bool Ortho_RegionA_TestC_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncWaitCond_TimeoutMS_Event...\n");
 
oosmos_AsyncWaitCond_TimeoutMS_Event(pRegion, 100, eventTimedOut,
ConditionTrue()
);
Successes += 1;
 
oosmos_AsyncWaitCond_TimeoutMS_Event(pRegion, 100, eventTimedOut,
ConditionFalse()
);
Failures += 1;
oosmos_AsyncEnd(pRegion);
return oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_TestD_State);
 
case eventTimedOut:
Successes += 1;
return true;
}
 
return false;
}
 
//
// oosmos_AsyncWaitCond_TimeoutMS_Exit
//
static bool Ortho_RegionA_TestD_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncWaitCond_TimeoutMS_Exit...\n");
 
oosmos_AsyncWaitCond_TimeoutMS_Exit(pRegion, 100,
ConditionTrue()
);
Successes += 1;
 
oosmos_AsyncWaitCond_TimeoutMS_Exit(pRegion, 100,
ConditionFalse()
);
Failures += 1;
 
oosmos_AsyncFinally(pRegion);
Successes += 1;
oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_TestE_State);
 
oosmos_AsyncEnd(pRegion);
return true;
}
 
return false;
}
 
//
// oosmos_AsyncWaitEvent
// oosmos_AsyncWaitEvent_TimeoutMS
//
static bool Ortho_RegionA_TestE_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
bool TimedOut;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncWaitEvent...\n");
 
oosmos_SendEvent(pAsyncTest, eventPrint);
oosmos_AsyncWaitEvent(pRegion, pEvent, eventDone);
 
prtFormatted("Test AsyncWaitEvent_TimeoutMS...\n");
 
oosmos_SendEvent(pAsyncTest, eventPrint);
oosmos_AsyncWaitEvent_TimeoutMS(pRegion, pEvent, 100, &TimedOut,
eventDone
);
Successes += (TimedOut == false);
 
oosmos_AsyncWaitEvent_TimeoutMS(pRegion, pEvent, 100, &TimedOut,
eventDone
);
Successes += (TimedOut == true);
oosmos_AsyncEnd(pRegion);
 
return oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_TestF_State);
}
 
return false;
}
 
//
// oosmos_AsyncWaitEvent_TimeoutMS_Event
//
static bool Ortho_RegionA_TestF_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncWaitEvent_TimeoutMS_Event...\n");
 
//
// Enqueue 'eventPrint' to the orthogonal state. We expect to get and 'eventDone' well
// before the timeout.
//
oosmos_SendEvent(pAsyncTest, eventPrint);
oosmos_AsyncWaitEvent_TimeoutMS_Event(pRegion, pEvent, 100, eventTimedOut,
eventDone
);
Successes += 1;
 
//
// We don't enqueue 'eventPrint' so we don't expect an 'eventDone' therefore it should time out.
//
oosmos_AsyncWaitEvent_TimeoutMS_Event(pRegion, pEvent, 100, eventTimedOut,
eventDone
);
Failures += 1;
oosmos_AsyncEnd(pRegion);
return oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_TestG_State);
 
case eventTimedOut:
Successes += 1;
return true;
}
 
return false;
}
 
//
// oosmos_AsyncWaitEvent_TimeoutMS_Exit
//
static bool Ortho_RegionA_TestG_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
 
switch (pEvent->Code) {
case oosmos_INSTATE:
oosmos_AsyncBegin(pRegion);
prtFormatted("Test AsyncWaitEvent_TimeoutMS_Exit...\n");
 
//
// Enqueue 'eventPrint' to the orthogonal state. We expect to get and 'eventDone' well
// before the timeout.
//
oosmos_SendEvent(pAsyncTest, eventPrint);
oosmos_AsyncWaitEvent_TimeoutMS_Exit(pRegion, pEvent, 100,
eventDone
);
Successes += 1;
 
//
// We don't enqueue 'eventPrint' so we don't expect an 'eventDone' therefore it should time out.
//
oosmos_AsyncWaitEvent_TimeoutMS_Exit(pRegion, pEvent, 100,
eventDone
);
Failures += 1;
 
oosmos_AsyncFinally(pRegion);
oosmos_Transition(pRegion, &pAsyncTest->Ortho_RegionA_Done_State);
 
oosmos_AsyncEnd(pRegion);
return true;
}
 
return false;
}
 
static bool Ortho_RegionA_Done_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
switch (pEvent->Code) {
case oosmos_ENTER:
prtFormatted("TotalTests: %d, Successes: %d, Failures: %d\n", TotalTests, Successes, Failures);
 
if (Failures > 0) {
prtFormatted("FAILURE.\n");
oosmos_EndProgram(0);
}
 
if (Successes == TotalTests * AsyncTestObjects) {
prtFormatted("SUCCESS.\n");
oosmos_EndProgram(0);
}
 
return true;
}
 
return false;
}
 
static bool Ortho_RegionB_Idle_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
asynctest * pAsyncTest = (asynctest *) pObject;
 
switch (pEvent->Code) {
case eventPrint:
return oosmos_SendEvent(pAsyncTest, eventDone);
}
 
return false;
}
 
extern asynctest * asynctestNew(void)
{
oosmos_Allocate(pAsyncTest, asynctest, MAX_ASYNCTEST, NULL);
 
// StateName Parent Default
// ========================================================================================
oosmos_StateMachineInit (pAsyncTest, StateMachine, NULL, Ortho_State );
oosmos_OrthoInitNoCode (pAsyncTest, Ortho_State, StateMachine );
oosmos_OrthoRegionInitNoCode(pAsyncTest, Ortho_RegionA_State, Ortho_State, Ortho_RegionA_TestA_State);
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestA_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestB_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestC_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestD_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestE_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestF_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_TestG_State, Ortho_RegionA_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionA_Done_State, Ortho_RegionA_State );
oosmos_OrthoRegionInitNoCode(pAsyncTest, Ortho_RegionB_State, Ortho_State, Ortho_RegionB_Idle_State );
oosmos_LeafInit (pAsyncTest, Ortho_RegionB_Idle_State, Ortho_RegionB_State );
 
#ifdef asynctestDEBUG
oosmos_Debug(&pAsyncTest->StateMachine, true, EventNames);
#endif
 
AsyncTestObjects += 1;
 
return pAsyncTest;
}
asynctest.c

2 Async on Various Platforms

The Async example will run on any platform but we have only packaged the Examples directory with PIC32 and Windows implementations.

2.1 Async on PIC32

1
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[GPLv2]
 
#include "oosmos.h"
#include "asynctest.h"
 
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
 
#define ASYNCTESTS 1
 
extern int main(void)
{
oosmos_ClockSpeedInMHz(80);
 
for (int I = 1; I <= ASYNCTESTS; I++)
asynctestNew();
 
while (true) {
oosmos_RunStateMachines();
oosmos_DelayMS(25);
}
 
return 0;
}
main.c - Async for PIC32

2.2 Async on Windows

On Windows, we create all the asynctest objects and then drop into the conventional Windows oosmos_RunStateMachines loop.
Change ASYNCTESTS to whatever you like but it's easier to step through with a source level debugger when it is set to 1.
1
22
23
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[GPLv2]
 
#includes...
 
#define ASYNCTESTS 1
 
extern int main(void)
{
int I;
 
for (I = 1; I <= ASYNCTESTS; I++)
asynctestNew();
 
while (true) {
oosmos_RunStateMachines();
oosmos_DelayMS(25);
}
 
return 0;
}
main.c - Async for Windows
Copyright © 2014-2016  OOSMOS, LLC