-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusbctrl_state.c
More file actions
641 lines (574 loc) · 22.4 KB
/
usbctrl_state.c
File metadata and controls
641 lines (574 loc) · 22.4 KB
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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#include "libc/types.h"
#include "libc/stdio.h"
#include "libc/sync.h"
#include "usbctrl.h"
#include "usbctrl_state.h"
#include "usbctrl_requests.h"
#ifdef __FRAMAC__
#ifdef FRAMAC_WITH_META
/* another function is allowed to manipulate the USB state: framac_state_manipulator, from the FramaC emulated entrypoint.
* This function is NOT a part of the library but used to stress out the various error cases and branches of the lib
*/
#include "framac/entrypoint.h"
#endif
#endif
/*
* all allowed transitions and target states for each current state
* empty fields are set as 0xff/0xff for request/state couple, which is
* an inexistent state and request
*
* This table associate each state of the DFU automaton with up to
* 5 potential allowed transitions/next_state couples. This permit to
* easily detect:
* 1) authorized transitions, based on the current state
* 2) next state, based on the current state and current transition
*
* If the next_state for the current transision is keeped to 0xff, this
* means that the current transition for the current state may lead to
* multiple next state depending on other informations. In this case,
* the transition handler has to handle this manually.
*/
#ifdef __FRAMAC__
#ifdef FRAMAC_WITH_META
/*
* This is the USB control plane state automaton conformity proof set against USB 2.0
* specifications.
* The proofs are based on MetaCSL high level specifications that guarantee that
* automaton states and transitions are valid and comply with the specifications.
*
* We wish to thank to Virgile Prevosto case study on the Wookey bootloader on
* which the current automaton metACSL content is based.
*/
/* ==> The automaton state is only writeable by usbctrl_set_state() */
#define VALID_STATE(i) (ctx_list[i].state >= USB_DEVICE_STATE_ATTACHED && ctx_list[i].state < USB_DEVICE_STATE_INVALID)
#define STATE_UNTOUCHED(i) (ctx_list[i].state == \old(ctx_list[i].state))
/*@
meta \prop,
\name(state_only_changed_in_set_state),
\targets(\diff(\ALL, usbctrl_set_state)),
\context(\writing),
\separated(\written, &ctx_list[0 .. CONFIG_USBCTRL_MAX_CTX-1].state);
// only usbctrl_set_state() is allowed to update ctx_list[].state
// in the Frama-C framework context, there is two control plane contexts (i.e. CONFIG_USBCTRL_MAX_CTX == 2)
meta \prop,
\name(ctx0_state_controled_update),
\targets(\diff(\ALL, \union(usbctrl_set_state, \callers(usbctrl_set_state)))),
\context(\postcond),
\flags(proof:deduce),
\fguard(STATE_UNTOUCHED(0));
meta \prop,
\name(ctx1_state_controled_update),
\targets(\diff(\ALL, \union(usbctrl_set_state, \callers(usbctrl_set_state)))),
\context(\postcond),
\flags(proof:deduce),
\fguard(STATE_UNTOUCHED(1));
*/
/* ==> The automaton state is always valid */
/*@ meta \prop,
\name(state_always_valid_small),
\targets(usbctrl_set_state),
\context(\strong_invariant),
\flags(proof:axiom, translate:no),
(VALID_STATE(0) && VALID_STATE(1));
*/
/*@ meta \prop,
\name(state_always_valid_callees),
\targets(\callees(usbctrl_set_state)),
\context(\strong_invariant),
\flags(proof:deduce, translate:no),
(VALID_STATE(0) && VALID_STATE(1));
*/
/*@ meta \prop,
\name(state_always_valid),
\targets(\ALL),
\context(\weak_invariant),
\flags(proof:deduce, translate:yes),
(VALID_STATE(0) && VALID_STATE(1));
*/
/* ==> Restrict transition to transition functions only */
/*
* Definition of the functions set that is responsible of
* automaton transitions
* usbctrl_start_device activate the HW IP and thus set the state automaton
* from 'ATTACHED' (default) to 'POWERED'. Other are USB events.
*/
#define TRANSITION_FUNCTIONS ({ \
usbctrl_start_device, \
usbctrl_stop_device, \
usbctrl_std_req_handle_set_address, \
usbctrl_std_req_handle_set_configuration,\
usbctrl_handle_usbsuspend, \
usbctrl_handle_reset, \
usbctrl_handle_wakeup \
})
/* this proof is to be translated to ACSL for provers, not deduced */
/* ==> only transiion functions (and framac manipulator, out of the library) is allowed to call usbctrl_set_state */
/*@ meta \prop,
\name(state_wrapper_only_called_in_transitions),
\targets(\diff(\ALL,
\union(framac_state_manipulator,
TRANSITION_FUNCTIONS))),
\context(\calling),
\tguard(\called != usbctrl_set_state);
*/
/* ==> for each target state, specify from which state the automaton allows a transition
*
* These successive meta properties specify, for each target state, allowed source states
* the USB 2.0 standard allows in the USB state automaton specifications.
*
* These meta-properties **does not** specify which transition function is responsible of
* the transition, only the origin and the destination of the transition itself.
*/
/*@ meta \prop,
\name(transition_allowed_toward_attached),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_ATTACHED ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_POWERED ||
\at(ctx->state,Before) == USB_DEVICE_STATE_DEFAULT ||
\at(ctx->state,Before) == USB_DEVICE_STATE_ADDRESS ||
\at(ctx->state,Before) == USB_DEVICE_STATE_CONFIGURED ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_DEFAULT ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_ADDRESS ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_CONFIGURED
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_suspended_powered),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_POWERED ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_ATTACHED
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_default),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_DEFAULT ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_POWERED ||
\at(ctx->state,Before) == USB_DEVICE_STATE_DEFAULT ||
\at(ctx->state,Before) == USB_DEVICE_STATE_ADDRESS ||
\at(ctx->state,Before) == USB_DEVICE_STATE_CONFIGURED ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_DEFAULT ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_ADDRESS ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_CONFIGURED
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_address),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_ADDRESS ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_DEFAULT ||
\at(ctx->state,Before) == USB_DEVICE_STATE_CONFIGURED ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_ADDRESS
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_configured),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_CONFIGURED ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_ADDRESS ||
\at(ctx->state,Before) == USB_DEVICE_STATE_SUSPENDED_CONFIGURED
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_suspended_default),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_SUSPENDED_DEFAULT ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_DEFAULT
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_suspended_address),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_SUSPENDED_ADDRESS ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_ADDRESS
));
*/
/*@ meta \prop,
\name(transition_allowed_toward_suspended_configured),
\targets(usbctrl_set_state),
\context(\precond),
\tguard(newstate == USB_DEVICE_STATE_SUSPENDED_CONFIGURED ==>
(
\at(ctx->state,Before) == USB_DEVICE_STATE_CONFIGURED
));
*/
/* ==> Restrict transition functions target states to allowed ones only
*
* Here we associate each transition to dedicated transition function.
* Only thr target state is specified, but with previous meta-properties,
* each target state is associated to a list of allowed source states.
*/
/*
* INFO: the following metaproperties require metACSL v0.1.2 or higher.
* They are not activated by default.
*
*/
#ifdef WITH_META_CALLARG_SUPPORT
/*@
meta \prop,
\name(set_address_can_only_set_allowed_states),
\targets(usbctrl_std_req_handle_set_address),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_DEFAULT ||
\called_arg(newstate) == USB_DEVICE_STATE_ADDRESS));
*/
/*@
meta \prop,
\name(set_configuration_can_only_set_allowed_states),
\targets(usbctrl_std_req_handle_set_configuration),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_CONFIGURED ||
\called_arg(newstate) == USB_DEVICE_STATE_ADDRESS));
*/
/*@
meta \prop,
\name(usbsuspend_can_only_set_allowed_states),
\targets(usbctrl_handle_usbsuspend),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_SUSPENDED_POWER ||
\called_arg(newstate) == USB_DEVICE_STATE_SUSPENDED_DEFAULT ||
\called_arg(newstate) == USB_DEVICE_STATE_SUSPENDED_ADDRESS ||
\called_arg(newstate) == USB_DEVICE_STATE_SUSPENDED_CONFIGURED));
*/
/*@
meta \prop,
\name(usbwakeup_can_only_set_allowed_states),
\targets(usbctrl_handle_wakeup),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_POWERED ||
\called_arg(newstate) == USB_DEVICE_STATE_DEFAULT ||
\called_arg(newstate) == USB_DEVICE_STATE_ADDRESS ||
\called_arg(newstate) == USB_DEVICE_STATE_CONFIGURED));
*/
/*@
meta \prop,
\name(reset_can_only_set_allowed_states),
\targets(usbctrl_handle_reset),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_DEFAULT));
*/
/*@
meta \prop,
\name(start_device_can_only_set_allowed_states),
\targets(usbctrl_start_device),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_POWERED));
*/
/*@
meta \prop,
\name(stop_device_can_only_set_allowed_states),
\targets(usbctrl_stop_device),
\context(\calling),
\tguard(\called == usbctrl_set_state ==> \fguard(
\called_arg(newstate) == USB_DEVICE_STATE_ATTACHED));
*/
#endif/*WITH_META_CALLARG_SUPPORT */
#endif/*!FRAMAC_WITH_META */
#endif/*!__FRAMAC__*/
#ifndef __FRAMAC__
/*
All variables declared here are reported in state.h, so that they can be used in other function specifications (in other files)
*/
/*
* Association between a request and a transition to a next state. This couple
* depend on the current state and is use in the following structure
*/
#define MAX_TRANSITION_STATE 10
typedef struct usb_operation_code_transition {
uint8_t request;
uint8_t target_state;
} usb_request_code_transition_t;
static const struct {
usb_device_state_t state;
usb_request_code_transition_t req_trans[MAX_TRANSITION_STATE];
} usb_automaton[] = {
{USB_DEVICE_STATE_ATTACHED, {
{USB_DEVICE_TRANS_HUB_CONFIGURED, USB_DEVICE_STATE_POWERED},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
}
},
{USB_DEVICE_STATE_POWERED, {
{USB_DEVICE_TRANS_BUS_INACTIVE, USB_DEVICE_STATE_SUSPENDED_POWER},
{USB_DEVICE_TRANS_HUB_RESET, USB_DEVICE_STATE_ATTACHED},
{USB_DEVICE_TRANS_HUB_DECONFIGURED, USB_DEVICE_STATE_ATTACHED},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
}
},
{USB_DEVICE_STATE_SUSPENDED_POWER, {
{USB_DEVICE_TRANS_BUS_ACTIVE, USB_DEVICE_STATE_POWERED},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
}
},
{USB_DEVICE_STATE_SUSPENDED_DEFAULT, {
{USB_DEVICE_TRANS_BUS_ACTIVE, USB_DEVICE_STATE_DEFAULT},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
}
},
{USB_DEVICE_STATE_SUSPENDED_ADDRESS, {
{USB_DEVICE_TRANS_BUS_ACTIVE, USB_DEVICE_STATE_ADDRESS},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
}
},
{USB_DEVICE_STATE_SUSPENDED_CONFIGURED, {
{USB_DEVICE_TRANS_BUS_ACTIVE, USB_DEVICE_STATE_CONFIGURED},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
}
},
{USB_DEVICE_STATE_DEFAULT, {
{USB_DEVICE_TRANS_ADDRESS_ASSIGNED, USB_DEVICE_STATE_ADDRESS},
{USB_DEVICE_TRANS_BUS_INACTIVE, USB_DEVICE_STATE_SUSPENDED_DEFAULT},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
},
},
{USB_DEVICE_STATE_ADDRESS, {
{USB_DEVICE_TRANS_DEV_CONFIGURED, USB_DEVICE_STATE_CONFIGURED},
{USB_DEVICE_TRANS_BUS_INACTIVE, USB_DEVICE_STATE_SUSPENDED_ADDRESS},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
},
},
{USB_DEVICE_STATE_CONFIGURED, {
{USB_DEVICE_TRANS_DEV_DECONFIGURED, USB_DEVICE_STATE_ADDRESS},
{USB_DEVICE_TRANS_BUS_INACTIVE, USB_DEVICE_STATE_SUSPENDED_CONFIGURED},
{USB_DEVICE_TRANS_RESET, USB_DEVICE_STATE_DEFAULT},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
},
},
{USB_DEVICE_STATE_INVALID, { // should never be set
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
{0xff, 0xff},
},
},
};
#endif/*__FRAMAC__*/
/**********************************************
* USB CTRL State automaton getters and setters
*********************************************/
/*@
@ assigns \nothing ;
@ ensures (ctx == \null) ==> \result == USB_DEVICE_STATE_INVALID ;
@ ensures (ctx != \null) ==> \result == ctx->state ;
*/
usb_device_state_t usbctrl_get_state(const usbctrl_context_t *ctx)
{
if (ctx == NULL) {
return USB_DEVICE_STATE_INVALID;
}
return ctx->state;
}
/*
* This function is eligible in both main thread and ISR
* mode (through trigger execution). Please use aprintf only
* here.
*/
/*@
@ assigns ctx->state ;
@ behavior invparam:
@ assumes (newstate >= USB_DEVICE_STATE_INVALID || ctx == \null);
@ ensures ctx->state == \old(ctx->state);
@ ensures \result == MBED_ERROR_INVPARAM ;
@ behavior ok:
@ assumes !(newstate >= USB_DEVICE_STATE_INVALID || ctx == \null);
@ ensures ctx->state == newstate;
@ ensures \result == MBED_ERROR_NONE;
@ disjoint behaviors;
@ complete behaviors;
*/
mbed_error_t usbctrl_set_state(__out usbctrl_context_t *ctx,
__in usb_device_state_t newstate)
{
/* FIXME: transient, maybe we need to lock here. */
if (ctx == NULL) {
return MBED_ERROR_INVPARAM;
}
if (newstate >= USB_DEVICE_STATE_INVALID) {
log_printf("[USBCTRL] invalid state transition !\n");
return MBED_ERROR_INVPARAM;
}
log_printf("[USBCTRL] changing from state %x to %x\n", ctx->state, newstate);
/*@ assert \valid(&ctx->state); */
/* here we do not use set_u8_with_membarrier() because it forbid metACSL from detecting that
* ctx->state is assigned only here. Instead, using request_data_membarrier() */
ctx->state = newstate;
request_data_membarrier();
return MBED_ERROR_NONE;
}
/******************************************************
* USBCTRL automaton management function (transition and
* state check)
*****************************************************/
/*!
* \brief return the next automaton state
*
* The next state is returned depending on the current state
* and the current request. In some case, it can be 0xff if multiple
* next states are possible.
*
* \param current_state the current automaton state
* \param request the current transition request
*
* \return the next state, or 0xff
*/
/*@
@ requires is_valid_state(current_state);
@ requires is_valid_request_code(request);
@ assigns \nothing ;
@ ensures \result == 0xff <==> (\forall integer i; 0 <= i < MAX_TRANSITION_STATE ==> usb_automaton[current_state].req_trans[i].request != request) ;
@ ensures (\result != 0xff) <==> \exists integer i; 0 <= i < MAX_TRANSITION_STATE && usb_automaton[current_state].req_trans[i].request == request
&& \result == usb_automaton[current_state].req_trans[i].target_state ;
*/
uint8_t usbctrl_next_state(usb_device_state_t current_state,
usb_device_trans_t request)
{
/*@
@ loop invariant 0 <= i <= MAX_TRANSITION_STATE ;
@ loop invariant \valid_read(usb_automaton[current_state].req_trans + (0..(MAX_TRANSITION_STATE -1)));
@ loop invariant (\forall integer prei ; 0 <= prei < i ==> usb_automaton[current_state].req_trans[prei].request != request) ;
@ loop assigns i ;
@ loop variant MAX_TRANSITION_STATE -i ;
*/
for (uint8_t i = 0; i < MAX_TRANSITION_STATE; ++i) {
if (usb_automaton[current_state].req_trans[i].request == request) {
return (usb_automaton[current_state].req_trans[i].target_state);
}
}
/* fallback, no corresponding request found for this state */
return 0xff;
}
/*!
* \brief Specify if the current request is valid for the current state
*
* \param current_state the current automaton state
* \param request the current transition request
*
* \return true if the transition request is allowed for this state, or false
*/
/*@
@ requires is_valid_state(current_state) ;
@ requires is_valid_transition(transition);
@ requires \valid_read(usb_automaton[current_state].req_trans + (0..(MAX_TRANSITION_STATE -1)));
@ requires \separated(usb_automaton[current_state].req_trans + (0..(MAX_TRANSITION_STATE -1)));
@ assigns \nothing;
@ ensures \result == \true || \result == \false ;
@ ensures \result == \true <==> (\exists integer i ; 0 <= i < MAX_TRANSITION_STATE && usb_automaton[current_state].req_trans[i].request == transition) ;
@ ensures \result == \false <==> (\forall integer i ; 0 <= i < MAX_TRANSITION_STATE ==> usb_automaton[current_state].req_trans[i].request != transition );
@ ensures \result == \true && transition == USB_DEVICE_TRANS_RESET ==>
!(current_state == USB_DEVICE_STATE_ATTACHED) ;
@ ensures \result == \false && transition == USB_DEVICE_TRANS_RESET ==>
(current_state == USB_DEVICE_STATE_ATTACHED) ;
*/
bool usbctrl_is_valid_transition(usb_device_state_t current_state,
usb_device_trans_t transition)
{
/*@
@ loop invariant 0 <= i <= MAX_TRANSITION_STATE ;
@ loop invariant \valid_read(usb_automaton[current_state].req_trans + (0..(MAX_TRANSITION_STATE -1)));
@ loop invariant (\forall integer prei ; 0 <= prei < i ==> usb_automaton[current_state].req_trans[prei].request != transition) ;
@ loop assigns i ;
@ loop variant MAX_TRANSITION_STATE -i ;
*/
for (uint8_t i = 0; i < MAX_TRANSITION_STATE; ++i) {
if (usb_automaton[current_state].req_trans[i].request == transition) {
return true;
}
}
/*
* Didn't find any request associated to current state. This is not a
* valid transition. We should stall the request.
*/
log_printf("%s: invalid transition from state %d, request %d\n", __func__, current_state, transition);
return false;
}