Subversion Repositories HomeAutomation

Rev

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

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