1 /*
2   Copyright (C) 2006-2013 Werner Dittmann
3 
4   This program is free software: you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published by
6   the Free Software Foundation, either version 3 of the License, or
7   (at your option) any later version.
8 
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 /*
19  * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
20  */
21 
22 #ifndef _ZRTPSTATES_H_
23 #define _ZRTPSTATES_H_
24 
25 /**
26  * @file ZrtpStates.h
27  * @brief The ZRTP state switching class
28  *
29  * @ingroup GNU_ZRTP
30  * @{
31  */
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <assert.h>
36 #include <stdint.h>
37 
38 class __EXPORT ZrtpStateClass;
39 /**
40  * This structure hold the state name as enum (int) number and the pointer to
41  * the functions that handles the various triggers that can occur in a state.
42  */
43 typedef struct  {
44     int32_t stateName;                      ///< The state number
45     void (ZrtpStateClass::* handler)(void); ///< The state handler
46 } state_t;
47 
48 /**
49  * Implement a simple state switching.
50  *
51  * This class provides functions that manage the states and the event handler
52  * functions. Its a very simple implementation.
53  *
54  * @author Werner Dittmann <Werner.Dittmann@t-online.de>
55  */
56 
57 class __EXPORT ZrtpStates {
58  public:
59 
60     /// Create an initialize state switching
61     ZrtpStates(state_t* const zstates,
62            const int32_t numStates,
63            const int32_t initialState):
64     numStates(numStates), states(zstates), state(initialState) {}
65 
66     /// Call a state handler
67     int32_t processEvent(ZrtpStateClass& zsc) {
68         (zsc.*states[state].handler)();
69         return 0;
70     }
71 
72     /// Check if in specified state
73     bool inState(const int32_t s) { return ((s == state)); }
74 
75     /// Set the next state
76     void nextState(int32_t s)        { state = s; }
77 
78  private:
79     const int32_t numStates;
80     const state_t* states;
81     int32_t  state;
82 
83     ZrtpStates();
84 };
85 
86 /**
87  * @}
88  */
89 #endif  //ZRTPSTATES
90 
91