-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFsmTest.cpp
More file actions
130 lines (104 loc) · 3.08 KB
/
CFsmTest.cpp
File metadata and controls
130 lines (104 loc) · 3.08 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
/**
* CFsmTest.c
*
* This file is part of IoT u-Micro Finite State Machine library
*
* Copyright (C) 2012, Hernan Monserrat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include <cstdio>
#include <cassert>
#include <list>
#include "uEventHandler.hpp"
#include "uFsm.hpp"
#include <iostream>
#define MAXNUMTRANSITIONS 26
// States
#define IDLE 0x0001
#define WAIT_COM 0x0002
#define WAIT_DEV 0x0003
#define WAIT_CRL 0x0004
#define WAIT_END 0x0005
#define ERROR 0xFFFF
// Define list of possible events
// COM Events to CEN
#define EV_RESET 0x0003 // R
#define EV_TRANSFER 0x0004 //
// Event handlers indexes
enum Handler_indexes {
Ind_InternalError,
// COM Events to CEN
Ind_reset,
Ind_transfer,
};
class EvHandler : public uEventHandler
{
public:
EvHandler( uint32_t inNumberTransitions);
private:
void FillHandlersArray( void ) override;
bool reset( void* );
bool transfer( void*);
bool handleInternalError( void* );
};
EvHandler::EvHandler( uint32_t inNumberTransitions )
: uEventHandler(inNumberTransitions)
{
FillHandlersArray();
}
bool EvHandler::handleInternalError( void* )
{
std::cout << "handleInternalError\n";
return false;
}
bool EvHandler::reset( void* )
{
std::cout << "reset\n";
return true;
}
bool EvHandler::transfer( void* )
{
std::cout << "transfer\n";
return true;
}
void EvHandler::FillHandlersArray( void ) {
functions_[Ind_InternalError]
= (TransitionFunc) &EvHandler::handleInternalError;
// COM Events to DEV
functions_[Ind_reset]
= (TransitionFunc) &EvHandler::reset;
functions_[Ind_transfer]
= (TransitionFunc) &EvHandler::transfer;
}
int main(int argc, const char * argv[]) {
EvHandler *evHandler;
uFsm *eFsm;
std::cout << "Starting tests!\n";
evHandler = new EvHandler( MAXNUMTRANSITIONS);
assert(evHandler!=NULL);
eFsm = new uFsm(evHandler, MAXNUMTRANSITIONS, IDLE );
assert(eFsm!=NULL);
eFsm->defineTransition( IDLE, IDLE, INTERNAL_ERROR ,Ind_InternalError);
eFsm->defineTransition( IDLE, WAIT_DEV, EV_RESET, Ind_reset);
eFsm->defineTransition( IDLE, WAIT_DEV, EV_TRANSFER, Ind_transfer);
eFsm->control(INTERNAL_ERROR, NULL);
eFsm->control(EV_RESET, NULL);
// eFsm->control(EV_TRANSFER, NULL);
delete eFsm;
delete evHandler;
std::cout << "bye\n";
return EXIT_SUCCESS;
}