Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1598 runge 1
/*
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
 */
20
 
21
#include "Node.h"
22
 
1601 runge 23
#include <boost/date_time/posix_time/posix_time.hpp>
1627 runge 24
#include <boost/lexical_cast.hpp>
1601 runge 25
 
1627 runge 26
#include "Manager.h"
1598 runge 27
#include "Message.h"
28
#include "broker/Manager.h"
29
 
30
namespace atom {
31
namespace can {
1627 runge 32
 
33
Node::TransitionList Node::transitions_;
1598 runge 34
 
1627 runge 35
Node::Node(Node::Id id) : LOG("can::Node")
1598 runge 36
{
1627 runge 37
    this->state_ = STATE_NORM_OFFLINE;
1598 runge 38
    this->id_ = id;
39
}
40
 
41
Node::~Node()
42
{
43
 
44
}
45
 
1627 runge 46
void Node::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state)
47
{
48
    this->signal_on_new_state_.connect(slot_on_new_state);
49
}
50
 
1598 runge 51
Node::Id Node::GetId()
52
{
53
    return this->id_;
54
}
55
 
56
Node::State Node::GetState()
57
{
58
    return this->state_;
59
}
60
 
1627 runge 61
void Node::SetupStateMachine()
1598 runge 62
{
1627 runge 63
    // Normal flow
64
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_BIOS_START,        STATE_NORM_ONLINE);
65
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_APP_START,         STATE_NORM_LIST);
66
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_HEARTBEAT,         STATE_NORM_LIST);
67
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
68
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
69
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_RESET,             STATE_NO_CHANGE);
70
 
71
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_BIOS_START,        STATE_NORM_ONLINE);
72
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_APP_START,         STATE_NORM_LIST);
73
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_HEARTBEAT,         STATE_NORM_LIST);
74
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
75
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
76
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_RESET,             STATE_NORM_OFFLINE);
77
 
78
    Node::AddTransition(STATE_NORM_LIST,             EVENT_BIOS_START,        STATE_NORM_ONLINE);
79
    Node::AddTransition(STATE_NORM_LIST,             EVENT_APP_START,         STATE_NORM_LIST);
80
    Node::AddTransition(STATE_NORM_LIST,             EVENT_HEARTBEAT,         STATE_NO_CHANGE);
81
    Node::AddTransition(STATE_NORM_LIST,             EVENT_LIST_DONE,         STATE_NORM_INITIALIZED);
82
    Node::AddTransition(STATE_NORM_LIST,             EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
83
    Node::AddTransition(STATE_NORM_LIST,             EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
84
    Node::AddTransition(STATE_NORM_LIST,             EVENT_RESET,             STATE_NORM_OFFLINE);
85
 
86
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_BIOS_START,        STATE_NORM_ONLINE);
87
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_APP_START,         STATE_NORM_LIST);
88
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_HEARTBEAT,         STATE_NO_CHANGE);
89
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
90
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
91
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_RESET,             STATE_NORM_OFFLINE);
92
 
93
 
94
    // Program bios flow
95
    Node::AddTransition(STATE_BPGM_OFFLINE,          EVENT_BIOS_START,        STATE_BPGM_START);
96
 
97
    Node::AddTransition(STATE_BPGM_START,            EVENT_PGM_ACK,           STATE_BPGM_DATA);
98
    Node::AddTransition(STATE_BPGM_START,            EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
99
 
100
    Node::AddTransition(STATE_BPGM_DATA,             EVENT_PGM_ACK,           STATE_BPGM_DATA); // If there is no more data to send STATE_PGM_END will be set instead
101
    Node::AddTransition(STATE_BPGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
102
 
103
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_ACK,           STATE_BPGM_COPY);
104
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
105
 
106
    Node::AddTransition(STATE_BPGM_COPY,             EVENT_BIOS_START,        STATE_NORM_ONLINE); // Return to normal flow
107
 
108
 
109
    // Program application flow
110
    Node::AddTransition(STATE_APGM_OFFLINE,          EVENT_BIOS_START,        STATE_APGM_START);
111
 
112
    Node::AddTransition(STATE_APGM_START,            EVENT_PGM_ACK,           STATE_APGM_DATA);
113
    Node::AddTransition(STATE_APGM_START,            EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
114
 
115
    Node::AddTransition(STATE_APGM_DATA,             EVENT_PGM_ACK,           STATE_APGM_DATA); // If there is no more data to send STATE_PGM_END will be set instead
116
    Node::AddTransition(STATE_APGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
117
 
118
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_ACK,           STATE_NORM_OFFLINE); // Return to normal flow
119
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
1598 runge 120
}
1601 runge 121
 
1627 runge 122
void Node::AddTransition(Node::State current_state, Node::Event event, State target_state)
123
{
124
    Node::transitions_[current_state][event] = target_state;
125
}
126
 
127
Node::State Node::GetTransitionTarget(Node::Event event)
128
{
129
    Node::TransitionList::iterator it1 = Node::transitions_.find(this->state_);
130
 
131
    if (it1 == Node::transitions_.end())
132
    {
133
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_));
134
        return STATE_INVALID;
135
    }
136
 
137
    Node::Transition::iterator it2 = it1->second.find(event);
138
 
139
    if (it2 == it1->second.end())
140
    {
141
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_) + " for event " + boost::lexical_cast<std::string>(event));
142
        return STATE_INVALID;
143
    }
144
 
145
    return it2->second;
146
}
147
 
148
void Node::Trigger(Node::Event event, type::StringMap variables)
149
{
150
    this->ResetTimeout();
151
 
152
    State target_state = this->GetTransitionTarget(event);
153
 
154
    LOG.Debug("Trigger called: current state: " + boost::lexical_cast<std::string>(this->state_) + ", event: " + boost::lexical_cast<std::string>(event) + ", target state:"  + boost::lexical_cast<std::string>(target_state));
155
 
156
    if (target_state == STATE_INVALID)
157
    {
158
        this->SendReset();
159
        target_state = STATE_NORM_OFFLINE;
160
    }
161
    else if (target_state == STATE_NO_CHANGE)
162
    {
163
        return;
164
    }
165
    else if (target_state == STATE_NORM_LIST)
166
    {
167
        this->SendListRequest();
168
    }
169
    else if (target_state == STATE_NORM_OFFLINE)
170
    {
171
        this->SendReset();
172
    }
173
    else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
174
    {
175
        this->SendReset();
176
    }
177
    else if (target_state == STATE_BPGM_START || target_state == STATE_APGM_START)
178
    {
179
        // TODO: Send out pgm start
180
    }
181
    else if (target_state == STATE_BPGM_DATA || target_state == STATE_APGM_DATA)
182
    {
183
        // TODO: if more data send it
184
        // TODO: if no more data send pgm end and set target_state = STATE_APGM_END or target_state = STATE_BPGM_END
185
    }
186
 
187
    if (this->state_ != target_state)
188
    {
189
        this->signal_on_new_state_(this->id_, this->state_, target_state);
190
    }
191
 
192
    this->state_ = target_state;
193
}
194
 
1601 runge 195
bool Node::CheckTimeout()
196
{
1627 runge 197
    if (this->state_ == STATE_NORM_OFFLINE)
1606 runge 198
    {
199
        return true;
200
    }
201
 
1601 runge 202
    if (this->last_active_ + 10 < time(NULL))
203
    {
1627 runge 204
        this->state_ = STATE_NORM_OFFLINE;
1601 runge 205
        return false;
206
    }
1598 runge 207
 
1601 runge 208
    return true;
209
}
210
 
211
void Node::ResetTimeout()
212
{
213
    this->last_active_ = time(NULL);
214
}
1627 runge 215
 
216
void Node::SendReset()
217
{
218
    LOG.Debug("Sending node reset to " + type::ToHex(this->id_));
219
    Message* payload = new Message("nmt", "", "", 0, "Reset");
220
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
1601 runge 221
 
1627 runge 222
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
223
}
224
 
225
void Node::SendListRequest()
226
{
227
    LOG.Debug("Sending module listing on node " + type::ToHex(this->id_));
228
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
229
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
230
 
231
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
232
}
233
 
1598 runge 234
}; // namespace can
235
}; // namespace atom