Subversion Repositories HomeAutomation

Rev

Rev 1642 | Rev 1788 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1598 runge 1
/*
1642 runge 2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
1598 runge 20
 
1642 runge 21
#ifndef CONTROL_NODE_H
22
#define CONTROL_NODE_H
1598 runge 23
 
24
#include <string>
1627 runge 25
#include <map>
1598 runge 26
 
27
#include <boost/shared_ptr.hpp>
1627 runge 28
#include <boost/signals2.hpp>
1598 runge 29
 
1601 runge 30
#include <time.h>
31
 
1642 runge 32
#include "common/common.h"
1627 runge 33
#include "logging/Logger.h"
34
 
1642 runge 35
#include "Code.h"
36
#include <boost/concept_check.hpp>
37
 
1598 runge 38
namespace atom {
1642 runge 39
namespace control {
1598 runge 40
 
41
class Node
42
{
43
public:
44
    typedef boost::shared_ptr<Node> Pointer;
1657 runge 45
    typedef std::string Id;
1598 runge 46
 
47
    typedef enum
48
    {
1627 runge 49
        STATE_INVALID,
50
        STATE_NO_CHANGE,
51
        STATE_NORM_OFFLINE,
52
        STATE_NORM_ONLINE,
53
        STATE_NORM_LIST,
54
        STATE_NORM_INITIALIZED,
55
        STATE_BPGM_OFFLINE,
56
        STATE_BPGM_START,
57
        STATE_BPGM_DATA,
58
        STATE_BPGM_END,
59
        STATE_BPGM_COPY,
60
        STATE_APGM_OFFLINE,
61
        STATE_APGM_START,
62
        STATE_APGM_DATA,
63
        STATE_APGM_END,
1598 runge 64
    } State;
1627 runge 65
 
66
    typedef enum
67
    {
68
        EVENT_BIOS_START,
69
        EVENT_APP_START,
70
        EVENT_HEARTBEAT,
71
        EVENT_PGM_ACK,
72
        EVENT_PGM_NACK,
73
        EVENT_LIST_DONE,
74
        EVENT_PROGRAM_BIOS,
75
        EVENT_PROGRAM_APP,
76
        EVENT_RESET
77
    } Event;
1598 runge 78
 
1627 runge 79
    typedef boost::signals2::signal<void(Id, State, State)> SignalOnNewState;
80
 
81
 
1598 runge 82
    Node(Id id);
83
    virtual ~Node();
84
 
1627 runge 85
    static void SetupStateMachine();
86
 
87
    void ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state);
88
 
1598 runge 89
    Id GetId();
90
    State GetState();
1627 runge 91
 
1642 runge 92
    void Trigger(Event event, common::StringMap variables);
1598 runge 93
 
1601 runge 94
    bool CheckTimeout();
95
    void ResetTimeout();
96
 
1642 runge 97
    void ProgramApplication(Code::Pointer code);
98
    void ProgramBios(Code::Pointer code);
1657 runge 99
    void Reset();
1642 runge 100
 
1598 runge 101
private:
1627 runge 102
    typedef std::map<Event, State> Transition;
103
    typedef std::map<State, Transition> TransitionList;
104
 
1598 runge 105
    Id id_;
106
    State state_;
1601 runge 107
    time_t last_active_;
1627 runge 108
    SignalOnNewState signal_on_new_state_;
109
    logging::Logger LOG;
1642 runge 110
    Code::Pointer code_;
111
    unsigned int start_offset_;
112
    unsigned int current_offset_;
113
    unsigned int expected_ack_data_;
114
    time_t program_start_time_;
1627 runge 115
 
1642 runge 116
 
1627 runge 117
    static TransitionList transitions_;
1642 runge 118
    static std::map<State, std::string> state_names_;
119
    static std::map<Event, std::string> event_names_;
1627 runge 120
 
121
    static void AddTransition(State current_state, Event event, State target_state);
122
 
123
    State GetTransitionTarget(Event event);
124
    void SendReset();
125
    void SendListRequest();
1598 runge 126
};
127
 
1642 runge 128
}; // namespace control
1598 runge 129
}; // namespace atom
130
 
1642 runge 131
#endif // CONTROL_NODE_H