-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathexample-codelock.c
369 lines (310 loc) · 10.1 KB
/
example-codelock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
/*
* Copyright (c) 2004-2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the async.h library.
*
* Author: Adam Dunkels <[email protected]>, Sandro Magi <[email protected]>
*
* $Id: example-codelock.c,v 1.5 2005/10/06 07:57:08 adam Exp $
*/
/*
*
* This example shows how to implement a simple code lock. The code
* lock waits for key presses from a numeric keyboard and if the
* correct code is entered, the lock is unlocked. There is a maximum
* time of one second between each key press, and after the correct
* code has been entered, no more keys must be pressed for 0.5 seconds
* before the lock is opened.
*
* This is an example that shows two things:
* - how to implement a code lock key input mechanism, and
* - how to implement a sequential timed routine.
*
* The program consists of two async functions, one that implements the
* code lock reader and one that implements simulated keyboard input.
*
*
*/
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/time.h>
#endif
#include <stdio.h>
#include "async.h"
#include "async-time.h"
/*---------------------------------------------------------------------------*/
/*
* This example uses two timers: one for the code lock async and
* one for the simulated key input async.
*/
static struct timer codelock_timer, input_timer;
/*---------------------------------------------------------------------------*/
/*
* This is the code that has to be entered.
*/
static const char code[4] = {'1', '4', '2', '3'};
/*---------------------------------------------------------------------------*/
/*
* This example has two async and therefore has two async
* control structures of type struct async. These are initialized with
* async_init() in the entry function below.
*/
static struct async codelock_pt, input_pt;
/*---------------------------------------------------------------------------*/
/*
* The following code implements a simple key input. Input is made
* with the press_key() function, and the function key_pressed()
* checks if a key has been pressed. The variable "key" holds the
* latest key that was pressed. The variable "key_pressed_flag" is set
* when a key is pressed and cleared when a key press is checked.
*/
static char key, key_pressed_flag;
static void
press_key(char k)
{
printf("--- Key '%c' pressed\n", k);
key = k;
key_pressed_flag = 1;
}
static int
key_pressed(void)
{
if(key_pressed_flag != 0) {
key_pressed_flag = 0;
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/*
* Declaration of the async function implementing the code lock
* logic. The async function is declared using the `async` return type.
* The function is declared with the "static" keyword since it
* is local to this file. The name of the function is codelock_thread
* and it takes one argument, pt, of the type struct async.
*
*/
static async
codelock_thread(struct async *pt)
{
/* This is a local variable that holds the number of keys that have
* been pressed. Note that it is declared with the "static" keyword
* to make sure that the variable is *not* allocated on the stack.
*/
static int keys;
/*
* Declare the beginning of the async.
*/
async_begin(pt);
/*
* We'll let the async loop until the async is
* explicitly exited with async_exit.
*/
while(1) {
/*
* We'll be reading key presses until we get the right amount of
* correct keys.
*/
for(keys = 0; keys < sizeof(code); ++keys) {
/*
* If we haven't gotten any keypresses, we'll simply wait for one.
*/
if(keys == 0) {
/*
* The await() function will block until the condition
* key_pressed() is true.
*/
await(key_pressed());
} else {
/*
* If the "key" variable was larger than zero, we have already
* gotten at least one correct key press. If so, we'll not
* only wait for the next key, but we'll also set a timer that
* expires in one second. This gives the person pressing the
* keys one second to press the next key in the code.
*/
timer_set(&codelock_timer, 1000);
/*
* The following statement shows how complex blocking
* conditions can be easily expressed with asyncs and
* the await() function.
*/
await(key_pressed() || timer_expired(&codelock_timer));
/*
* If the timer expired, we should break out of the for() loop
* and start reading keys from the beginning of the while(1)
* loop instead.
*/
if(timer_expired(&codelock_timer)) {
printf("Code lock timer expired.\n");
/*
* Break out from the for() loop and start from the
* beginning of the while(1) loop.
*/
break;
}
}
/*
* Check if the pressed key was correct.
*/
if(key != code[keys]) {
printf("Incorrect key '%c' found\n", key);
/*
* Break out of the for() loop since the key was incorrect.
*/
break;
} else {
printf("Correct key '%c' found\n", key);
}
}
/*
* Check if we have gotten all keys.
*/
if(keys == sizeof(code)) {
printf("Correct code entered, waiting for 500 ms before unlocking.\n");
/*
* Ok, we got the correct code. But to make sure that the code
* was not just a fluke of luck by an intruder, but the correct
* code entered by a person that knows the correct code, we'll
* wait for half a second before opening the lock. If another
* key is pressed during this time, we'll assume that it was a
* fluke of luck that the correct code was entered the first
* time.
*/
timer_set(&codelock_timer, 500);
await(key_pressed() || timer_expired(&codelock_timer));
/*
* If we continued from the await() statement without
* the timer expired, we don't open the lock.
*/
if(!timer_expired(&codelock_timer)) {
printf("Key pressed during final wait, code lock locked again.\n");
} else {
/*
* If the timer expired, we'll open the lock and exit from the
* async.
*/
printf("Code lock unlocked.\n");
async_exit;
}
}
}
/*
* Finally, we'll mark the end of the async.
*/
async_end;
}
/*---------------------------------------------------------------------------*/
/*
* This is the second async in this example. It implements a
* simulated user pressing the keys. This illustrates how a linear
* sequence of timed instructions can be implemented with
* asyncs.
*/
static async
input_thread(struct async *pt)
{
async_begin(pt);
printf("Waiting 1 second before entering first key.\n");
timer_set(&input_timer, 1000);
await(timer_expired(&input_timer));
press_key('1');
timer_set(&input_timer, 100);
await(timer_expired(&input_timer));
press_key('2');
timer_set(&input_timer, 100);
await(timer_expired(&input_timer));
press_key('3');
timer_set(&input_timer, 2000);
await(timer_expired(&input_timer));
press_key('1');
timer_set(&input_timer, 200);
await(timer_expired(&input_timer));
press_key('4');
timer_set(&input_timer, 200);
await(timer_expired(&input_timer));
press_key('2');
timer_set(&input_timer, 2000);
await(timer_expired(&input_timer));
press_key('3');
timer_set(&input_timer, 200);
await(timer_expired(&input_timer));
press_key('1');
timer_set(&input_timer, 200);
await(timer_expired(&input_timer));
press_key('4');
timer_set(&input_timer, 200);
await(timer_expired(&input_timer));
press_key('2');
timer_set(&input_timer, 100);
await(timer_expired(&input_timer));
press_key('3');
timer_set(&input_timer, 100);
await(timer_expired(&input_timer));
press_key('4');
timer_set(&input_timer, 1500);
await(timer_expired(&input_timer));
press_key('1');
timer_set(&input_timer, 300);
await(timer_expired(&input_timer));
press_key('4');
timer_set(&input_timer, 400);
await(timer_expired(&input_timer));
press_key('2');
timer_set(&input_timer, 500);
await(timer_expired(&input_timer));
press_key('3');
timer_set(&input_timer, 2000);
await(timer_expired(&input_timer));
async_end;
}
/*---------------------------------------------------------------------------*/
/*
* This is the main function. It initializes the two async
* control structures and schedules the two asyncs. The main
* function returns when the async the runs the code lock exits.
*/
int
example_codelock(void)
{
/*
* Initialize the two async control structures.
*/
async_init(&input_pt);
async_init(&codelock_pt);
/*
* Schedule the two asyncs until the codelock_thread() exits.
*/
async_run(
codelock_thread(&codelock_pt) &
input_thread(&input_pt)
);
return 0;
}