Subversion Repositories HomeAutomation

Rev

Rev 1788 | Rev 1911 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1788 Rev 1876
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
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
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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
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.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include "Node.h"
21
#include "Node.h"
22
 
22
 
23
#include <boost/date_time/posix_time/posix_time.hpp>
23
#include <boost/date_time/posix_time/posix_time.hpp>
24
#include <boost/lexical_cast.hpp>
24
#include <boost/lexical_cast.hpp>
25
 
25
 
26
#include "Manager.h"
26
#include "Manager.h"
27
#include "can/Message.h"
27
#include "can/Message.h"
28
#include "broker/Manager.h"
28
#include "broker/Manager.h"
29
 
29
 
30
namespace atom {
30
namespace atom {
31
namespace control {
31
namespace control {
32
 
32
 
33
Node::TransitionList Node::transitions_;
33
Node::TransitionList Node::transitions_;
34
std::map<Node::State, std::string> Node::state_names_;
34
std::map<Node::State, std::string> Node::state_names_;
35
std::map<Node::Event, std::string> Node::event_names_;
35
std::map<Node::Event, std::string> Node::event_names_;
36
   
36
   
37
Node::Node(Node::Id id) : LOG("control::Node")
37
Node::Node(Node::Id id) : LOG("control::Node")
38
{
38
{
39
    this->state_ = STATE_NORM_OFFLINE;
39
    this->state_ = STATE_NORM_OFFLINE;
40
    this->id_ = boost::algorithm::to_lower_copy((std::string)id);
40
    //this->id_ = boost::algorithm::to_lower_copy((std::string)id);
-
 
41
    //this->id_ = boost::algorithm::to_upper_copy((std::string)id);
-
 
42
    this->id_ = (std::string)id;
41
    this->information_.has_application_ = false;
43
    this->information_.has_application_ = false;
42
    this->information_.bios_version_ = 0;
44
    this->information_.bios_version_ = 0;
43
    this->information_.device_type_ = "N/A";
45
    this->information_.device_type_ = "N/A";
44
    this->information_.last_active_ = 0;
46
    this->information_.last_active_ = 0;
45
    this->information_.valid_ = false;
47
    this->information_.valid_ = false;
46
   
48
   
47
    this->current_offset_ = 0;
49
    this->current_offset_ = 0;
48
    this->expected_ack_data_ = 0;
50
    this->expected_ack_data_ = 0;
49
    this->program_start_time_ = 0;
51
    this->program_start_time_ = 0;
50
}
52
}
51
 
53
 
52
Node::~Node()
54
Node::~Node()
53
{
55
{
54
 
56
 
55
}
57
}
56
 
58
 
57
void Node::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state)
59
void Node::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state)
58
{
60
{
59
    this->signal_on_new_state_.connect(slot_on_new_state);
61
    this->signal_on_new_state_.connect(slot_on_new_state);
60
}
62
}
61
 
63
 
62
Node::Id Node::GetId()
64
Node::Id Node::GetId()
63
{
65
{
64
    return this->id_;
66
    return this->id_;
65
}
67
}
66
 
68
 
67
Node::Information Node::GetInformation()
69
Node::Information Node::GetInformation()
68
{
70
{
69
    return this->information_;
71
    return this->information_;
70
}
72
}
71
 
73
 
72
void Node::SetupStateMachine()
74
void Node::SetupStateMachine()
73
{
75
{
74
    // Normal flow
76
    // Normal flow
75
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_BIOS_START,        STATE_NORM_ONLINE);
77
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_BIOS_START,        STATE_NORM_ONLINE);
76
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_APP_START,         STATE_NORM_LIST);
78
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_APP_START,         STATE_NORM_LIST);
77
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_HEARTBEAT,         STATE_NORM_LIST);
79
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_HEARTBEAT,         STATE_NORM_LIST);
78
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
80
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
79
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
81
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
80
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_RESET,             STATE_NO_CHANGE);
82
    Node::AddTransition(STATE_NORM_OFFLINE,          EVENT_RESET,             STATE_NO_CHANGE);
81
   
83
   
82
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_BIOS_START,        STATE_NORM_ONLINE);
84
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_BIOS_START,        STATE_NORM_ONLINE);
83
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_APP_START,         STATE_NORM_LIST);
85
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_APP_START,         STATE_NORM_LIST);
84
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_HEARTBEAT,         STATE_NORM_LIST);
86
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_HEARTBEAT,         STATE_NORM_LIST);
85
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
87
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
86
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
88
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
87
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_RESET,             STATE_NORM_OFFLINE);
89
    Node::AddTransition(STATE_NORM_ONLINE,           EVENT_RESET,             STATE_NORM_OFFLINE);
88
   
90
   
89
    Node::AddTransition(STATE_NORM_LIST,             EVENT_BIOS_START,        STATE_NORM_ONLINE);
91
    Node::AddTransition(STATE_NORM_LIST,             EVENT_BIOS_START,        STATE_NORM_ONLINE);
90
    Node::AddTransition(STATE_NORM_LIST,             EVENT_APP_START,         STATE_NORM_LIST);
92
    Node::AddTransition(STATE_NORM_LIST,             EVENT_APP_START,         STATE_NORM_LIST);
91
    Node::AddTransition(STATE_NORM_LIST,             EVENT_HEARTBEAT,         STATE_NO_CHANGE);
93
    Node::AddTransition(STATE_NORM_LIST,             EVENT_HEARTBEAT,         STATE_NO_CHANGE);
92
    Node::AddTransition(STATE_NORM_LIST,             EVENT_LIST_DONE,         STATE_NORM_INITIALIZED);
94
    Node::AddTransition(STATE_NORM_LIST,             EVENT_LIST_DONE,         STATE_NORM_INITIALIZED);
93
    Node::AddTransition(STATE_NORM_LIST,             EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
95
    Node::AddTransition(STATE_NORM_LIST,             EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
94
    Node::AddTransition(STATE_NORM_LIST,             EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
96
    Node::AddTransition(STATE_NORM_LIST,             EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
95
    Node::AddTransition(STATE_NORM_LIST,             EVENT_RESET,             STATE_NORM_OFFLINE);
97
    Node::AddTransition(STATE_NORM_LIST,             EVENT_RESET,             STATE_NORM_OFFLINE);
96
   
98
   
97
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_BIOS_START,        STATE_NORM_ONLINE);
99
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_BIOS_START,        STATE_NORM_ONLINE);
98
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_APP_START,         STATE_NORM_LIST);
100
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_APP_START,         STATE_NORM_LIST);
99
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_HEARTBEAT,         STATE_NO_CHANGE);
101
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_HEARTBEAT,         STATE_NO_CHANGE);
100
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
102
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_PROGRAM_BIOS,      STATE_BPGM_OFFLINE);
101
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
103
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_PROGRAM_APP,       STATE_APGM_OFFLINE);
102
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_RESET,             STATE_NORM_OFFLINE);
104
    Node::AddTransition(STATE_NORM_INITIALIZED,      EVENT_RESET,             STATE_NORM_OFFLINE);
103
   
105
   
104
   
106
   
105
    // Program bios flow
107
    // Program bios flow
106
    Node::AddTransition(STATE_BPGM_OFFLINE,          EVENT_BIOS_START,        STATE_BPGM_START);
108
    Node::AddTransition(STATE_BPGM_OFFLINE,          EVENT_BIOS_START,        STATE_BPGM_START);
107
   
109
   
108
    Node::AddTransition(STATE_BPGM_START,            EVENT_PGM_ACK,           STATE_BPGM_DATA);
110
    Node::AddTransition(STATE_BPGM_START,            EVENT_PGM_ACK,           STATE_BPGM_DATA);
109
    Node::AddTransition(STATE_BPGM_START,            EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
111
    Node::AddTransition(STATE_BPGM_START,            EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
110
 
112
 
111
    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
113
    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
112
    Node::AddTransition(STATE_BPGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
114
    Node::AddTransition(STATE_BPGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
113
   
115
   
114
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_ACK,           STATE_BPGM_COPY);
116
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_ACK,           STATE_BPGM_COPY);
115
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
117
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
116
   
118
   
117
    Node::AddTransition(STATE_BPGM_COPY,             EVENT_BIOS_START,        STATE_APGM_OFFLINE); // Remove application after bios upgrade
119
    Node::AddTransition(STATE_BPGM_COPY,             EVENT_BIOS_START,        STATE_APGM_OFFLINE); // Remove application after bios upgrade
118
       
120
       
119
   
121
   
120
    // Program application flow
122
    // Program application flow
121
    Node::AddTransition(STATE_APGM_OFFLINE,          EVENT_BIOS_START,        STATE_APGM_START);
123
    Node::AddTransition(STATE_APGM_OFFLINE,          EVENT_BIOS_START,        STATE_APGM_START);
122
   
124
   
123
    Node::AddTransition(STATE_APGM_START,            EVENT_PGM_ACK,           STATE_APGM_DATA);
125
    Node::AddTransition(STATE_APGM_START,            EVENT_PGM_ACK,           STATE_APGM_DATA);
124
    Node::AddTransition(STATE_APGM_START,            EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
126
    Node::AddTransition(STATE_APGM_START,            EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
125
   
127
   
126
    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
128
    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
127
    Node::AddTransition(STATE_APGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
129
    Node::AddTransition(STATE_APGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
128
   
130
   
129
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_ACK,           STATE_NORM_OFFLINE); // Return to normal flow
131
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_ACK,           STATE_NORM_OFFLINE); // Return to normal flow
130
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
132
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
131
   
133
   
132
   
134
   
133
    Node::state_names_[STATE_INVALID] = "STATE_INVALID";
135
    Node::state_names_[STATE_INVALID] = "STATE_INVALID";
134
    Node::state_names_[STATE_NO_CHANGE] = "STATE_NO_CHANGE";
136
    Node::state_names_[STATE_NO_CHANGE] = "STATE_NO_CHANGE";
135
    Node::state_names_[STATE_NORM_OFFLINE] = "STATE_NORM_OFFLINE";
137
    Node::state_names_[STATE_NORM_OFFLINE] = "STATE_NORM_OFFLINE";
136
    Node::state_names_[STATE_NORM_ONLINE] = "STATE_NORM_ONLINE";
138
    Node::state_names_[STATE_NORM_ONLINE] = "STATE_NORM_ONLINE";
137
    Node::state_names_[STATE_NORM_LIST] = "STATE_NORM_LIST";
139
    Node::state_names_[STATE_NORM_LIST] = "STATE_NORM_LIST";
138
    Node::state_names_[STATE_NORM_INITIALIZED] = "STATE_NORM_INITIALIZED";
140
    Node::state_names_[STATE_NORM_INITIALIZED] = "STATE_NORM_INITIALIZED";
139
    Node::state_names_[STATE_BPGM_OFFLINE] = "STATE_BPGM_OFFLINE";
141
    Node::state_names_[STATE_BPGM_OFFLINE] = "STATE_BPGM_OFFLINE";
140
    Node::state_names_[STATE_BPGM_START] = "STATE_BPGM_START";
142
    Node::state_names_[STATE_BPGM_START] = "STATE_BPGM_START";
141
    Node::state_names_[STATE_BPGM_DATA] = "STATE_BPGM_DATA";
143
    Node::state_names_[STATE_BPGM_DATA] = "STATE_BPGM_DATA";
142
    Node::state_names_[STATE_BPGM_END] = "STATE_BPGM_END";
144
    Node::state_names_[STATE_BPGM_END] = "STATE_BPGM_END";
143
    Node::state_names_[STATE_BPGM_COPY] = "STATE_BPGM_COPY";
145
    Node::state_names_[STATE_BPGM_COPY] = "STATE_BPGM_COPY";
144
    Node::state_names_[STATE_APGM_OFFLINE] = "STATE_APGM_OFFLINE";
146
    Node::state_names_[STATE_APGM_OFFLINE] = "STATE_APGM_OFFLINE";
145
    Node::state_names_[STATE_APGM_START] = "STATE_APGM_START";
147
    Node::state_names_[STATE_APGM_START] = "STATE_APGM_START";
146
    Node::state_names_[STATE_APGM_DATA] = "STATE_APGM_DATA";
148
    Node::state_names_[STATE_APGM_DATA] = "STATE_APGM_DATA";
147
    Node::state_names_[STATE_APGM_END] = "STATE_APGM_END";
149
    Node::state_names_[STATE_APGM_END] = "STATE_APGM_END";
148
   
150
   
149
    Node::event_names_[EVENT_BIOS_START] = "EVENT_BIOS_START";
151
    Node::event_names_[EVENT_BIOS_START] = "EVENT_BIOS_START";
150
    Node::event_names_[EVENT_APP_START] = "EVENT_APP_START";
152
    Node::event_names_[EVENT_APP_START] = "EVENT_APP_START";
151
    Node::event_names_[EVENT_HEARTBEAT] = "EVENT_HEARTBEAT";
153
    Node::event_names_[EVENT_HEARTBEAT] = "EVENT_HEARTBEAT";
152
    Node::event_names_[EVENT_PGM_ACK] = "EVENT_PGM_ACK";
154
    Node::event_names_[EVENT_PGM_ACK] = "EVENT_PGM_ACK";
153
    Node::event_names_[EVENT_PGM_NACK] = "EVENT_PGM_NACK";
155
    Node::event_names_[EVENT_PGM_NACK] = "EVENT_PGM_NACK";
154
    Node::event_names_[EVENT_LIST_DONE] = "EVENT_LIST_DONE";
156
    Node::event_names_[EVENT_LIST_DONE] = "EVENT_LIST_DONE";
155
    Node::event_names_[EVENT_PROGRAM_BIOS] = "EVENT_PROGRAM_BIOS";
157
    Node::event_names_[EVENT_PROGRAM_BIOS] = "EVENT_PROGRAM_BIOS";
156
    Node::event_names_[EVENT_PROGRAM_APP] = "EVENT_PROGRAM_APP";
158
    Node::event_names_[EVENT_PROGRAM_APP] = "EVENT_PROGRAM_APP";
157
    Node::event_names_[EVENT_RESET] = "EVENT_RESET";
159
    Node::event_names_[EVENT_RESET] = "EVENT_RESET";
158
}
160
}
159
 
161
 
160
void Node::AddTransition(Node::State current_state, Node::Event event, State target_state)
162
void Node::AddTransition(Node::State current_state, Node::Event event, State target_state)
161
{
163
{
162
    Node::transitions_[current_state][event] = target_state;
164
    Node::transitions_[current_state][event] = target_state;
163
}
165
}
164
 
166
 
165
Node::State Node::GetTransitionTarget(Node::Event event)
167
Node::State Node::GetTransitionTarget(Node::Event event)
166
{
168
{
167
    Node::TransitionList::iterator it1 = Node::transitions_.find(this->state_);
169
    Node::TransitionList::iterator it1 = Node::transitions_.find(this->state_);
168
   
170
   
169
    if (it1 == Node::transitions_.end())
171
    if (it1 == Node::transitions_.end())
170
    {
172
    {
171
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_));
173
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_));
172
        return STATE_INVALID;
174
        return STATE_INVALID;
173
    }
175
    }
174
   
176
   
175
    Node::Transition::iterator it2 = it1->second.find(event);
177
    Node::Transition::iterator it2 = it1->second.find(event);
176
   
178
   
177
    if (it2 == it1->second.end())
179
    if (it2 == it1->second.end())
178
    {
180
    {
179
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_) + " for event " + boost::lexical_cast<std::string>(event));
181
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_) + " for event " + boost::lexical_cast<std::string>(event));
180
        return STATE_INVALID;
182
        return STATE_INVALID;
181
    }
183
    }
182
   
184
   
183
    return it2->second;
185
    return it2->second;
184
}
186
}
185
 
187
 
186
void Node::Trigger(Node::Event event, common::StringMap variables)
188
void Node::Trigger(Node::Event event, common::StringMap variables)
187
{
189
{
188
    this->ResetTimeout();
190
    this->ResetTimeout();
189
   
191
   
190
    State target_state = this->GetTransitionTarget(event);
192
    State target_state = this->GetTransitionTarget(event);
191
   
193
   
192
    LOG.Debug("Trigger called on " + this->id_ + ", current state: " + Node::state_names_[this->state_] + ", event: " + Node::event_names_[event] + ", target state: "  + Node::state_names_[target_state]);
194
    LOG.Debug("Trigger called on " + this->id_ + ", current state: " + Node::state_names_[this->state_] + ", event: " + Node::event_names_[event] + ", target state: "  + Node::state_names_[target_state]);
193
   
195
   
194
    if (event == EVENT_BIOS_START)
196
    if (event == EVENT_BIOS_START)
195
    {
197
    {
196
        this->information_.has_application_ = boost::lexical_cast<int>(variables["HasApplication"]) > 0;
198
        this->information_.has_application_ = boost::lexical_cast<int>(variables["HasApplication"]) > 0;
197
        this->information_.bios_version_ = boost::lexical_cast<unsigned int>(variables["BiosVersion"]);
199
        this->information_.bios_version_ = boost::lexical_cast<unsigned int>(variables["BiosVersion"]);
198
        this->information_.device_type_ = variables["DeviceType"];
200
        this->information_.device_type_ = variables["DeviceType"];
199
        this->information_.valid_ = true;
201
        this->information_.valid_ = true;
200
    }
202
    }
201
   
203
   
202
    if (target_state == STATE_INVALID)
204
    if (target_state == STATE_INVALID)
203
    {
205
    {
204
        this->SendReset();
206
        this->SendReset();
205
        target_state = STATE_NORM_OFFLINE;
207
        target_state = STATE_NORM_OFFLINE;
206
    }
208
    }
207
    else if (target_state == STATE_NO_CHANGE)
209
    else if (target_state == STATE_NO_CHANGE)
208
    {
210
    {
209
        return;
211
        return;
210
    }
212
    }
211
   
213
   
212
    if ((this->state_ == STATE_APGM_END || this->state_ == STATE_BPGM_END) && event == EVENT_PGM_ACK)
214
    if ((this->state_ == STATE_APGM_END || this->state_ == STATE_BPGM_END) && event == EVENT_PGM_ACK)
213
    {
215
    {
214
        unsigned int checksum = boost::lexical_cast<unsigned int>(variables["Data"]);
216
        unsigned int checksum = boost::lexical_cast<unsigned int>(variables["Data"]);
215
       
217
       
216
        if (this->expected_ack_data_ != checksum)
218
        if (this->expected_ack_data_ != checksum)
217
        {
219
        {
218
            LOG.Warning("Checksum received in ACK was incorrect got " + boost::lexical_cast<std::string>(checksum) + " expected " + boost::lexical_cast<std::string>(this->expected_ack_data_) + ", aborting...");
220
            LOG.Warning("Checksum received in ACK was incorrect got " + boost::lexical_cast<std::string>(checksum) + " expected " + boost::lexical_cast<std::string>(this->expected_ack_data_) + ", aborting...");
219
        }
221
        }
220
        else if (this->state_ == STATE_BPGM_END)
222
        else if (this->state_ == STATE_BPGM_END)
221
        {
223
        {
222
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
224
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
223
           
225
           
224
            LOG.Info("Data was successfully transferred to " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
226
            LOG.Info("Data was successfully transferred to " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
225
           
227
           
226
            LOG.Debug("Sending application programming copy to node " + this->id_);
228
            LOG.Debug("Sending application programming copy to node " + this->id_);
227
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Copy");
229
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Copy");
228
           
230
           
229
            unsigned int source0 = GET_LOW_BYTE_16(this->start_offset_);
231
            unsigned int source0 = GET_LOW_BYTE_16(this->start_offset_);
230
            unsigned int source1 = GET_HIGH_BYTE_16(this->start_offset_);
232
            unsigned int source1 = GET_HIGH_BYTE_16(this->start_offset_);
231
           
233
           
232
            unsigned int destination0 = GET_LOW_BYTE_16(this->code_->GetAddressLower());
234
            unsigned int destination0 = GET_LOW_BYTE_16(this->code_->GetAddressLower());
233
            unsigned int destination1 = GET_HIGH_BYTE_16(this->code_->GetAddressLower());
235
            unsigned int destination1 = GET_HIGH_BYTE_16(this->code_->GetAddressLower());
234
           
236
           
235
            unsigned int length0 = GET_LOW_BYTE_16(this->code_->GetLength());
237
            unsigned int length0 = GET_LOW_BYTE_16(this->code_->GetLength());
236
            unsigned int length1 = GET_HIGH_BYTE_16(this->code_->GetLength());
238
            unsigned int length1 = GET_HIGH_BYTE_16(this->code_->GetLength());
237
           
239
           
238
            payload->SetVariable("Source0", boost::lexical_cast<std::string>(source0));
240
            payload->SetVariable("Source0", boost::lexical_cast<std::string>(source0));
239
            payload->SetVariable("Source1", boost::lexical_cast<std::string>(source1));
241
            payload->SetVariable("Source1", boost::lexical_cast<std::string>(source1));
240
            payload->SetVariable("Destination0", boost::lexical_cast<std::string>(destination0));
242
            payload->SetVariable("Destination0", boost::lexical_cast<std::string>(destination0));
241
            payload->SetVariable("Destination1", boost::lexical_cast<std::string>(destination1));
243
            payload->SetVariable("Destination1", boost::lexical_cast<std::string>(destination1));
242
            payload->SetVariable("Length0", boost::lexical_cast<std::string>(length0));
244
            payload->SetVariable("Length0", boost::lexical_cast<std::string>(length0));
243
            payload->SetVariable("Length1", boost::lexical_cast<std::string>(length1));
245
            payload->SetVariable("Length1", boost::lexical_cast<std::string>(length1));
244
           
246
           
245
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
247
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
246
        }
248
        }
247
        else
249
        else
248
        {
250
        {
249
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
251
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
250
           
252
           
251
            LOG.Info("Programming was completed successfully on " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
253
            LOG.Info("Programming was completed successfully on " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
252
        }
254
        }
253
    }
255
    }
254
   
256
   
255
    if (target_state == STATE_NORM_LIST)
257
    if (target_state == STATE_NORM_LIST)
256
    {
258
    {
257
        this->SendListRequest();
259
        this->SendListRequest();
258
    }
260
    }
259
    else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
261
    else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
260
    {
262
    {
261
        if (event != EVENT_BIOS_START)
263
        if (event != EVENT_BIOS_START)
262
        {
264
        {
263
            this->SendReset();
265
            this->SendReset();
264
        }
266
        }
265
    }
267
    }
266
    else if (target_state == STATE_APGM_START || target_state == STATE_BPGM_START)
268
    else if (target_state == STATE_APGM_START || target_state == STATE_BPGM_START)
267
    {
269
    {
268
        if (!this->code_->IsValid())
270
        if (!this->code_->IsValid())
269
        {
271
        {
270
            LOG.Error("Code is not valid, aborting...");
272
            LOG.Error("Code is not valid, aborting...");
271
            target_state = STATE_NORM_OFFLINE;
273
            target_state = STATE_NORM_OFFLINE;
272
        }
274
        }
273
        else
275
        else
274
        {
276
        {
275
            LOG.Debug("Sending programming start to node " + this->id_);
277
            LOG.Debug("Sending programming start to node " + this->id_);
276
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Start");
278
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Start");
277
            payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
279
            payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
278
           
280
           
279
            this->start_offset_ = target_state == STATE_BPGM_START ? 0 : this->code_->GetAddressLower();
281
            this->start_offset_ = target_state == STATE_BPGM_START ? 0 : this->code_->GetAddressLower();
280
           
282
           
281
            unsigned int address0 = GET_LOW_BYTE_16(this->start_offset_);
283
            unsigned int address0 = GET_LOW_BYTE_16(this->start_offset_);
282
            unsigned int address1 = GET_HIGH_BYTE_16(this->start_offset_);
284
            unsigned int address1 = GET_HIGH_BYTE_16(this->start_offset_);
283
           
285
           
284
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_);
286
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_);
285
           
287
           
286
            payload->SetVariable("Address0", boost::lexical_cast<std::string>(address0));
288
            payload->SetVariable("Address0", boost::lexical_cast<std::string>(address0));
287
            payload->SetVariable("Address1", boost::lexical_cast<std::string>(address1));
289
            payload->SetVariable("Address1", boost::lexical_cast<std::string>(address1));
288
            payload->SetVariable("Address2", "0"); // Not used?
290
            payload->SetVariable("Address2", "0"); // Not used?
289
            payload->SetVariable("Address3", "0"); // Not used?
291
            payload->SetVariable("Address3", "0"); // Not used?
290
           
292
           
291
            this->current_offset_ = 0;
293
            this->current_offset_ = 0;
292
           
294
           
293
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
295
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
294
           
296
           
295
            this->program_start_time_ = time(NULL);
297
            this->program_start_time_ = time(NULL);
296
        }
298
        }
297
    }
299
    }
298
    else if (target_state == STATE_APGM_DATA || target_state == STATE_BPGM_DATA)
300
    else if (target_state == STATE_APGM_DATA || target_state == STATE_BPGM_DATA)
299
    {
301
    {
300
        unsigned int offset = boost::lexical_cast<unsigned int>(variables["Data"]);
302
        unsigned int offset = boost::lexical_cast<unsigned int>(variables["Data"]);
301
       
303
       
302
        if ( this->expected_ack_data_ != offset)
304
        if ( this->expected_ack_data_ != offset)
303
        {
305
        {
304
            LOG.Warning("Offset received in ACK was incorrect got " + boost::lexical_cast<std::string>(offset) + " expected " + boost::lexical_cast<std::string>(this->expected_ack_data_) + ", aborting...");
306
            LOG.Warning("Offset received in ACK was incorrect got " + boost::lexical_cast<std::string>(offset) + " expected " + boost::lexical_cast<std::string>(this->expected_ack_data_) + ", aborting...");
305
            target_state = STATE_NORM_OFFLINE;
307
            target_state = STATE_NORM_OFFLINE;
306
        }
308
        }
307
        else if (this->current_offset_ >= this->code_->GetLength())
309
        else if (this->current_offset_ >= this->code_->GetLength())
308
        {
310
        {
309
            LOG.Debug("Sending programming end to node " + this->id_);
311
            LOG.Debug("Sending programming end to node " + this->id_);
310
           
312
           
311
            unsigned int checksum = this->code_->GetChecksum();
313
            unsigned int checksum = this->code_->GetChecksum();
312
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(checksum);
314
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(checksum);
313
           
315
           
314
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_End");
316
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_End");
315
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
317
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
316
           
318
           
317
            target_state = target_state == STATE_BPGM_DATA ? STATE_BPGM_END : STATE_APGM_END;
319
            target_state = target_state == STATE_BPGM_DATA ? STATE_BPGM_END : STATE_APGM_END;
318
        }
320
        }
319
        else
321
        else
320
        {
322
        {
321
            LOG.Debug("Sending programming data to node " + this->id_);
323
            LOG.Debug("Sending programming data to node " + this->id_);
322
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Data_48");
324
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Data_48");
323
           
325
           
324
            unsigned int offset0 = GET_LOW_BYTE_16(this->start_offset_ + this->current_offset_);
326
            unsigned int offset0 = GET_LOW_BYTE_16(this->start_offset_ + this->current_offset_);
325
            unsigned int offset1 = GET_HIGH_BYTE_16(this->start_offset_ + this->current_offset_);
327
            unsigned int offset1 = GET_HIGH_BYTE_16(this->start_offset_ + this->current_offset_);
326
           
328
           
327
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_ + this->current_offset_);
329
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_ + this->current_offset_);
328
           
330
           
329
            payload->SetVariable("Offset0", boost::lexical_cast<std::string>(offset0));
331
            payload->SetVariable("Offset0", boost::lexical_cast<std::string>(offset0));
330
            payload->SetVariable("Offset1", boost::lexical_cast<std::string>(offset1));
332
            payload->SetVariable("Offset1", boost::lexical_cast<std::string>(offset1));
331
           
333
           
332
            for (int n = 0; (this->current_offset_ < this->code_->GetLength()) && (n < 6); n++)
334
            for (int n = 0; (this->current_offset_ < this->code_->GetLength()) && (n < 6); n++)
333
            {
335
            {
334
                //LOG.Debug("Data" + boost::lexical_cast<std::string>(n) + "=" +  boost::lexical_cast<std::string>((unsigned int)this->code_->GetByte(this->code_->GetAddressLower() + this->current_offset_)));
336
                //LOG.Debug("Data" + boost::lexical_cast<std::string>(n) + "=" +  boost::lexical_cast<std::string>((unsigned int)this->code_->GetByte(this->code_->GetAddressLower() + this->current_offset_)));
335
                payload->SetVariable("Data" + boost::lexical_cast<std::string>(n), boost::lexical_cast<std::string>((unsigned int)this->code_->GetByte(this->code_->GetAddressLower() + this->current_offset_)));
337
                payload->SetVariable("Data" + boost::lexical_cast<std::string>(n), boost::lexical_cast<std::string>((unsigned int)this->code_->GetByte(this->code_->GetAddressLower() + this->current_offset_)));
336
                this->current_offset_++;
338
                this->current_offset_++;
337
            }
339
            }
338
           
340
           
339
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
341
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
340
        }
342
        }
341
    }
343
    }
342
    else if (target_state == STATE_BPGM_COPY)
344
    else if (target_state == STATE_BPGM_COPY)
343
    {
345
    {
344
        LOG.Debug("Preparing to send null application to node " + this->id_);
346
        LOG.Debug("Preparing to send null application to node " + this->id_);
345
       
347
       
346
        this->code_->Reset();
348
        this->code_->Reset();
347
       
349
       
348
        this->code_->AddByte(0xFF);
350
        this->code_->AddByte(0xFF);
349
        this->code_->AddByte(0xFF);
351
        this->code_->AddByte(0xFF);
350
    }
352
    }
351
    else if (target_state == STATE_NORM_OFFLINE)
353
    else if (target_state == STATE_NORM_OFFLINE)
352
    {
354
    {
353
        this->information_.valid_ = false;
355
        this->information_.valid_ = false;
354
        this->SendReset();
356
        this->SendReset();
355
    }
357
    }
356
   
358
   
357
   
359
   
358
    if (this->state_ != target_state)
360
    if (this->state_ != target_state)
359
    {
361
    {
360
        this->signal_on_new_state_(this->id_, this->state_, target_state);
362
        this->signal_on_new_state_(this->id_, this->state_, target_state);
361
    }
363
    }
362
   
364
   
363
    this->state_ = target_state;
365
    this->state_ = target_state;
364
}
366
}
365
 
367
 
366
bool Node::CheckTimeout()
368
bool Node::CheckTimeout()
367
{
369
{
368
    if (this->state_ == STATE_NORM_OFFLINE)
370
    if (this->state_ == STATE_NORM_OFFLINE)
369
    {
371
    {
370
        return true;
372
        return true;
371
    }
373
    }
372
   
374
   
373
    if (this->code_.use_count() == 0 && this->information_.last_active_ + 10 < time(NULL))
375
    if (this->code_.use_count() == 0 && this->information_.last_active_ + 10 < time(NULL))
374
    {
376
    {
375
        this->state_ = STATE_NORM_OFFLINE;
377
        this->state_ = STATE_NORM_OFFLINE;
376
        return false;
378
        return false;
377
    }
379
    }
378
   
380
   
379
    return true;
381
    return true;
380
}
382
}
381
 
383
 
382
void Node::ResetTimeout()
384
void Node::ResetTimeout()
383
{
385
{
384
    this->information_.last_active_ = time(NULL);
386
    this->information_.last_active_ = time(NULL);
385
}
387
}
386
 
388
 
387
void Node::Reset()
389
void Node::Reset()
388
{
390
{
389
    common::StringMap variables;
391
    common::StringMap variables;
390
   
392
   
391
    this->Trigger(EVENT_RESET, variables);
393
    this->Trigger(EVENT_RESET, variables);
392
}
394
}
393
 
395
 
394
void Node::ProgramApplication(Code::Pointer code)
396
void Node::ProgramApplication(Code::Pointer code)
395
{
397
{
396
    this->code_ = code;
398
    this->code_ = code;
397
   
399
   
398
    common::StringMap variables;
400
    common::StringMap variables;
399
   
401
   
400
    this->Trigger(EVENT_PROGRAM_APP, variables);
402
    this->Trigger(EVENT_PROGRAM_APP, variables);
401
}
403
}
402
 
404
 
403
void Node::ProgramBios(Code::Pointer code)
405
void Node::ProgramBios(Code::Pointer code)
404
{
406
{
405
    this->code_ = code;
407
    this->code_ = code;
406
   
408
   
407
    common::StringMap variables;
409
    common::StringMap variables;
408
   
410
   
409
    this->Trigger(EVENT_PROGRAM_BIOS, variables);
411
    this->Trigger(EVENT_PROGRAM_BIOS, variables);
410
}
412
}
411
 
413
 
412
void Node::SendReset()
414
void Node::SendReset()
413
{
415
{
414
    LOG.Info("Sending node reset to " + this->id_);
416
    LOG.Info("Sending node reset to " + this->id_);
415
    can::Message* payload = new can::Message("nmt", "", "", 0, "Reset");
417
    can::Message* payload = new can::Message("nmt", "", "", 0, "Reset");
416
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
418
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
417
   
419
   
418
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
420
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
419
}
421
}
420
 
422
 
421
void Node::SendListRequest()
423
void Node::SendListRequest()
422
{
424
{
423
    LOG.Debug("Sending module listing on node " + this->id_);
425
    LOG.Debug("Sending module listing on node " + this->id_);
424
    can::Message* payload = new can::Message("mnmt", "To_Owner", "", 0, "List");
426
    can::Message* payload = new can::Message("mnmt", "To_Owner", "", 0, "List");
425
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
427
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
426
   
428
   
427
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
429
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
428
}
430
}
429
 
431
 
430
}; // namespace control
432
}; // namespace control
431
}; // namespace atom
433
}; // namespace atom
432
 
434