Subversion Repositories HomeAutomation

Rev

Rev 1627 | Rev 1657 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1627 Rev 1642
Line 22... Line 22...
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 "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 can {
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_;
-
 
35
std::map<Node::Event, std::string> Node::event_names_;
34
   
36
   
35
Node::Node(Node::Id id) : LOG("can::Node")
37
Node::Node(Node::Id id) : LOG("control::Node")
36
{
38
{
37
    this->state_ = STATE_NORM_OFFLINE;
39
    this->state_ = STATE_NORM_OFFLINE;
38
    this->id_ = id;
40
    this->id_ = id;
-
 
41
    this->current_offset_ = 0;
-
 
42
    this->expected_ack_data_ = 0;
-
 
43
    this->program_start_time_ = 0;
39
}
44
}
40
 
45
 
41
Node::~Node()
46
Node::~Node()
42
{
47
{
43
 
48
 
Line 101... Line 106...
101
    Node::AddTransition(STATE_BPGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
106
    Node::AddTransition(STATE_BPGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
102
   
107
   
103
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_ACK,           STATE_BPGM_COPY);
108
    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
109
    Node::AddTransition(STATE_BPGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
105
   
110
   
106
    Node::AddTransition(STATE_BPGM_COPY,             EVENT_BIOS_START,        STATE_NORM_ONLINE); // Return to normal flow
111
    Node::AddTransition(STATE_BPGM_COPY,             EVENT_BIOS_START,        STATE_APGM_OFFLINE); // Remove application after bios upgrade
107
   
112
       
108
   
113
   
109
    // Program application flow
114
    // Program application flow
110
    Node::AddTransition(STATE_APGM_OFFLINE,          EVENT_BIOS_START,        STATE_APGM_START);
115
    Node::AddTransition(STATE_APGM_OFFLINE,          EVENT_BIOS_START,        STATE_APGM_START);
111
   
116
   
Line 115... Line 120...
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
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
116
    Node::AddTransition(STATE_APGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
121
    Node::AddTransition(STATE_APGM_DATA,             EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
117
   
122
   
118
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_ACK,           STATE_NORM_OFFLINE); // Return to normal flow
123
    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
124
    Node::AddTransition(STATE_APGM_END,              EVENT_PGM_NACK,          STATE_NORM_OFFLINE); // Return to normal flow
-
 
125
   
-
 
126
   
-
 
127
    Node::state_names_[STATE_INVALID] = "STATE_INVALID";
-
 
128
    Node::state_names_[STATE_NO_CHANGE] = "STATE_NO_CHANGE";
-
 
129
    Node::state_names_[STATE_NORM_OFFLINE] = "STATE_NORM_OFFLINE";
-
 
130
    Node::state_names_[STATE_NORM_ONLINE] = "STATE_NORM_ONLINE";
-
 
131
    Node::state_names_[STATE_NORM_LIST] = "STATE_NORM_LIST";
-
 
132
    Node::state_names_[STATE_NORM_INITIALIZED] = "STATE_NORM_INITIALIZED";
-
 
133
    Node::state_names_[STATE_BPGM_OFFLINE] = "STATE_BPGM_OFFLINE";
-
 
134
    Node::state_names_[STATE_BPGM_START] = "STATE_BPGM_START";
-
 
135
    Node::state_names_[STATE_BPGM_DATA] = "STATE_BPGM_DATA";
-
 
136
    Node::state_names_[STATE_BPGM_END] = "STATE_BPGM_END";
-
 
137
    Node::state_names_[STATE_BPGM_COPY] = "STATE_BPGM_COPY";
-
 
138
    Node::state_names_[STATE_APGM_OFFLINE] = "STATE_APGM_OFFLINE";
-
 
139
    Node::state_names_[STATE_APGM_START] = "STATE_APGM_START";
-
 
140
    Node::state_names_[STATE_APGM_DATA] = "STATE_APGM_DATA";
-
 
141
    Node::state_names_[STATE_APGM_END] = "STATE_APGM_END";
-
 
142
   
-
 
143
    Node::event_names_[EVENT_BIOS_START] = "EVENT_BIOS_START";
-
 
144
    Node::event_names_[EVENT_APP_START] = "EVENT_APP_START";
-
 
145
    Node::event_names_[EVENT_HEARTBEAT] = "EVENT_HEARTBEAT";
-
 
146
    Node::event_names_[EVENT_PGM_ACK] = "EVENT_PGM_ACK";
-
 
147
    Node::event_names_[EVENT_PGM_NACK] = "EVENT_PGM_NACK";
-
 
148
    Node::event_names_[EVENT_LIST_DONE] = "EVENT_LIST_DONE";
-
 
149
    Node::event_names_[EVENT_PROGRAM_BIOS] = "EVENT_PROGRAM_BIOS";
-
 
150
    Node::event_names_[EVENT_PROGRAM_APP] = "EVENT_PROGRAM_APP";
-
 
151
    Node::event_names_[EVENT_RESET] = "EVENT_RESET";
120
}
152
}
121
 
153
 
122
void Node::AddTransition(Node::State current_state, Node::Event event, State target_state)
154
void Node::AddTransition(Node::State current_state, Node::Event event, State target_state)
123
{
155
{
124
    Node::transitions_[current_state][event] = target_state;
156
    Node::transitions_[current_state][event] = target_state;
125
}
157
}
126
 
158
 
127
Node::State Node::GetTransitionTarget(Node::Event event)
159
Node::State Node::GetTransitionTarget(Node::Event event)
128
{
160
{
129
    Node::TransitionList::iterator it1 = Node::transitions_.find(this->state_);
161
    Node::TransitionList::iterator it1 = Node::transitions_.find(this->state_);
130
   
162
   
131
    if (it1 == Node::transitions_.end())
163
    if (it1 == Node::transitions_.end())
132
    {
164
    {
133
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_));
165
        LOG.Warning("No transitions found for current state " + boost::lexical_cast<std::string>(this->state_));
134
        return STATE_INVALID;
166
        return STATE_INVALID;
135
    }
167
    }
Line 143... Line 175...
143
    }
175
    }
144
   
176
   
145
    return it2->second;
177
    return it2->second;
146
}
178
}
147
 
179
 
148
void Node::Trigger(Node::Event event, type::StringMap variables)
180
void Node::Trigger(Node::Event event, common::StringMap variables)
149
{
181
{
150
    this->ResetTimeout();
182
    this->ResetTimeout();
151
   
183
   
152
    State target_state = this->GetTransitionTarget(event);
184
    State target_state = this->GetTransitionTarget(event);
153
   
185
   
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));
186
    LOG.Debug("Trigger called on " + common::ToHex(this->id_) + ", current state: " + Node::state_names_[this->state_] + ", event: " + Node::event_names_[event] + ", target state: "  + Node::state_names_[target_state]);
155
   
187
   
156
    if (target_state == STATE_INVALID)
188
    if (target_state == STATE_INVALID)
157
    {
189
    {
158
        this->SendReset();
190
        this->SendReset();
159
        target_state = STATE_NORM_OFFLINE;
191
        target_state = STATE_NORM_OFFLINE;
160
    }
192
    }
161
    else if (target_state == STATE_NO_CHANGE)
193
    else if (target_state == STATE_NO_CHANGE)
162
    {
194
    {
163
        return;
195
        return;
-
 
196
    }
-
 
197
   
-
 
198
    if ((this->state_ == STATE_APGM_END || this->state_ == STATE_BPGM_END) && event == EVENT_PGM_ACK)
-
 
199
    {
-
 
200
        unsigned int checksum = boost::lexical_cast<unsigned int>(variables["Data"]);
-
 
201
       
-
 
202
        if (this->expected_ack_data_ != checksum)
-
 
203
        {
-
 
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...");
-
 
205
        }
-
 
206
        else if (this->state_ == STATE_BPGM_END)
-
 
207
        {
-
 
208
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
-
 
209
           
-
 
210
            LOG.Info("Data was successfully transferred to " + common::ToHex(this->id_) + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
-
 
211
           
-
 
212
            LOG.Debug("Sending application programming copy to node " + common::ToHex(this->id_));
-
 
213
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Copy");
-
 
214
           
-
 
215
            unsigned int source0 = GET_LOW_BYTE_16(this->start_offset_);
-
 
216
            unsigned int source1 = GET_HIGH_BYTE_16(this->start_offset_);
-
 
217
           
-
 
218
            unsigned int destination0 = GET_LOW_BYTE_16(this->code_->GetAddressLower());
-
 
219
            unsigned int destination1 = GET_HIGH_BYTE_16(this->code_->GetAddressLower());
-
 
220
           
-
 
221
            unsigned int length0 = GET_LOW_BYTE_16(this->code_->GetLength());
-
 
222
            unsigned int length1 = GET_HIGH_BYTE_16(this->code_->GetLength());
-
 
223
           
-
 
224
            payload->SetVariable("Source0", boost::lexical_cast<std::string>(source0));
-
 
225
            payload->SetVariable("Source1", boost::lexical_cast<std::string>(source1));
-
 
226
            payload->SetVariable("Destination0", boost::lexical_cast<std::string>(destination0));
-
 
227
            payload->SetVariable("Destination1", boost::lexical_cast<std::string>(destination1));
-
 
228
            payload->SetVariable("Length0", boost::lexical_cast<std::string>(length0));
-
 
229
            payload->SetVariable("Length1", boost::lexical_cast<std::string>(length1));
-
 
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
        else
-
 
234
        {
-
 
235
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
-
 
236
           
-
 
237
            LOG.Info("Programming was completed successfully on " + common::ToHex(this->id_) + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
-
 
238
        }
164
    }
239
    }
-
 
240
   
165
    else if (target_state == STATE_NORM_LIST)
241
    if (target_state == STATE_NORM_LIST)
166
    {
242
    {
167
        this->SendListRequest();
243
        this->SendListRequest();
168
    }
-
 
169
    else if (target_state == STATE_NORM_OFFLINE)
-
 
170
    {
-
 
171
        this->SendReset();
-
 
172
    }
244
    }
173
    else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
245
    else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
174
    {
246
    {
-
 
247
        if (event != EVENT_BIOS_START)
-
 
248
        {
175
        this->SendReset();
249
            this->SendReset();
-
 
250
        }
176
    }
251
    }
177
    else if (target_state == STATE_BPGM_START || target_state == STATE_APGM_START)
252
    else if (target_state == STATE_APGM_START || target_state == STATE_BPGM_START)
178
    {
253
    {
-
 
254
        if (!this->code_->IsValid())
-
 
255
        {
-
 
256
            LOG.Error("Code is not valid, aborting...");
-
 
257
            target_state = STATE_NORM_OFFLINE;
-
 
258
        }
-
 
259
        else
-
 
260
        {
-
 
261
            LOG.Debug("Sending programming start to node " + common::ToHex(this->id_));
-
 
262
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Start");
-
 
263
            payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
-
 
264
           
-
 
265
            this->start_offset_ = target_state == STATE_BPGM_START ? 0 : this->code_->GetAddressLower();
-
 
266
           
-
 
267
            unsigned int address0 = GET_LOW_BYTE_16(this->start_offset_);
-
 
268
            unsigned int address1 = GET_HIGH_BYTE_16(this->start_offset_);
-
 
269
           
-
 
270
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_);
-
 
271
           
-
 
272
            payload->SetVariable("Address0", boost::lexical_cast<std::string>(address0));
-
 
273
            payload->SetVariable("Address1", boost::lexical_cast<std::string>(address1));
-
 
274
            payload->SetVariable("Address2", "0"); // Not used?
-
 
275
            payload->SetVariable("Address3", "0"); // Not used?
-
 
276
           
179
        // TODO: Send out pgm start
277
            this->current_offset_ = 0;
-
 
278
           
-
 
279
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
-
 
280
           
-
 
281
            this->program_start_time_ = time(NULL);
-
 
282
        }
180
    }
283
    }
181
    else if (target_state == STATE_BPGM_DATA || target_state == STATE_APGM_DATA)
284
    else if (target_state == STATE_APGM_DATA || target_state == STATE_BPGM_DATA)
182
    {
285
    {
-
 
286
        unsigned int offset = boost::lexical_cast<unsigned int>(variables["Data"]);
-
 
287
       
183
        // TODO: if more data send it
288
        if ( this->expected_ack_data_ != offset)
-
 
289
        {
-
 
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...");
-
 
291
            target_state = STATE_NORM_OFFLINE;
-
 
292
        }
-
 
293
        else if (this->current_offset_ >= this->code_->GetLength())
-
 
294
        {
-
 
295
            LOG.Debug("Sending programming end to node " + common::ToHex(this->id_));
-
 
296
           
-
 
297
            unsigned int checksum = this->code_->GetChecksum();
-
 
298
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(checksum);
-
 
299
           
-
 
300
            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())));
-
 
302
           
184
        // TODO: if no more data send pgm end and set target_state = STATE_APGM_END or target_state = STATE_BPGM_END
303
            target_state = target_state == STATE_BPGM_DATA ? STATE_BPGM_END : STATE_APGM_END;
-
 
304
        }
-
 
305
        else
-
 
306
        {
-
 
307
            LOG.Debug("Sending programming data to node " + common::ToHex(this->id_));
-
 
308
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Data_48");
-
 
309
           
-
 
310
            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_);
-
 
312
           
-
 
313
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_ + this->current_offset_);
-
 
314
           
-
 
315
            payload->SetVariable("Offset0", boost::lexical_cast<std::string>(offset0));
-
 
316
            payload->SetVariable("Offset1", boost::lexical_cast<std::string>(offset1));
-
 
317
           
-
 
318
            for (int n = 0; (this->current_offset_ < this->code_->GetLength()) && (n < 6); n++)
-
 
319
            {
-
 
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_)));
-
 
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_)));
-
 
322
                this->current_offset_++;
-
 
323
            }
-
 
324
           
-
 
325
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
-
 
326
        }
-
 
327
    }
-
 
328
    else if (target_state == STATE_BPGM_COPY)
-
 
329
    {
-
 
330
        LOG.Debug("Preparing to send null application to node " + common::ToHex(this->id_));
-
 
331
       
-
 
332
        this->code_->Reset();
-
 
333
       
-
 
334
        this->code_->AddByte(0xFF);
-
 
335
        this->code_->AddByte(0xFF);
-
 
336
    }
-
 
337
    else if (target_state == STATE_NORM_OFFLINE)
-
 
338
    {
-
 
339
        this->SendReset();
185
    }
340
    }
-
 
341
   
186
   
342
   
187
    if (this->state_ != target_state)
343
    if (this->state_ != target_state)
188
    {
344
    {
189
        this->signal_on_new_state_(this->id_, this->state_, target_state);
345
        this->signal_on_new_state_(this->id_, this->state_, target_state);
190
    }
346
    }
191
   
347
   
192
    this->state_ = target_state;
348
    this->state_ = target_state;
193
}
349
}
194
 
350
 
195
bool Node::CheckTimeout()
351
bool Node::CheckTimeout()
196
{
352
{
197
    if (this->state_ == STATE_NORM_OFFLINE)
353
    if (this->state_ == STATE_NORM_OFFLINE)
198
    {
354
    {
199
        return true;
355
        return true;
200
    }
356
    }
201
   
357
   
202
    if (this->last_active_ + 10 < time(NULL))
358
    if (this->code_.use_count() == 0 && this->last_active_ + 10 < time(NULL))
203
    {
359
    {
204
        this->state_ = STATE_NORM_OFFLINE;
360
        this->state_ = STATE_NORM_OFFLINE;
205
        return false;
361
        return false;
206
    }
362
    }
207
   
363
   
208
    return true;
364
    return true;
209
}
365
}
210
 
366
 
211
void Node::ResetTimeout()
367
void Node::ResetTimeout()
212
{
368
{
213
    this->last_active_ = time(NULL);
369
    this->last_active_ = time(NULL);
-
 
370
}
-
 
371
 
-
 
372
void Node::ProgramApplication(Code::Pointer code)
-
 
373
{
-
 
374
    this->code_ = code;
-
 
375
   
-
 
376
    common::StringMap variables;
-
 
377
   
-
 
378
    this->Trigger(EVENT_PROGRAM_APP, variables);
-
 
379
}
-
 
380
 
-
 
381
void Node::ProgramBios(Code::Pointer code)
-
 
382
{
-
 
383
    this->code_ = code;
-
 
384
   
-
 
385
    common::StringMap variables;
-
 
386
   
-
 
387
    this->Trigger(EVENT_PROGRAM_BIOS, variables);
214
}
388
}
215
 
389
 
216
void Node::SendReset()
390
void Node::SendReset()
217
{
391
{
218
    LOG.Debug("Sending node reset to " + type::ToHex(this->id_));
392
    LOG.Info("Sending node reset to " + common::ToHex(this->id_));
219
    Message* payload = new Message("nmt", "", "", 0, "Reset");
393
    can::Message* payload = new can::Message("nmt", "", "", 0, "Reset");
220
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
394
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
221
   
395
   
222
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
396
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
223
}
397
}
224
 
398
 
225
void Node::SendListRequest()
399
void Node::SendListRequest()
226
{
400
{
227
    LOG.Debug("Sending module listing on node " + type::ToHex(this->id_));
401
    LOG.Debug("Sending module listing on node " + common::ToHex(this->id_));
228
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
402
    can::Message* payload = new can::Message("mnmt", "To_Owner", "", 0, "List");
229
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
403
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(this->id_));
230
   
404
   
231
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
405
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
232
}
406
}
233
 
407
 
234
}; // namespace can
408
}; // namespace control
235
}; // namespace atom
409
}; // namespace atom