Subversion Repositories HomeAutomation

Rev

Rev 2037 | Rev 2164 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 2037 Rev 2039
Line 176... Line 176...
176
   
176
   
177
    Node::Transition::iterator it2 = it1->second.find(event);
177
    Node::Transition::iterator it2 = it1->second.find(event);
178
   
178
   
179
    if (it2 == it1->second.end())
179
    if (it2 == it1->second.end())
180
    {
180
    {
181
        //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));
182
        return STATE_INVALID;
182
        return STATE_INVALID;
183
    }
183
    }
184
   
184
   
185
    return it2->second;
185
    return it2->second;
186
}
186
}
187
 
187
 
188
void Node::Trigger(Node::Event event, common::StringMap variables)
188
void Node::Trigger(Node::Event event, common::StringMap variables)
189
{
189
{
-
 
190
    if (!this->id_.empty())
-
 
191
    {
190
    this->ResetTimeout();
192
        this->ResetTimeout();
191
   
193
       
192
    State target_state = this->GetTransitionTarget(event);
194
        State target_state = this->GetTransitionTarget(event);
193
   
195
       
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]);
196
        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]);
195
   
197
       
196
    if (event == EVENT_BIOS_START)
198
        if (event == EVENT_BIOS_START)
197
    {
199
        {
198
        this->information_.has_application_ = boost::lexical_cast<int>(variables["HasApplication"]) > 0;
200
            this->information_.has_application_ = boost::lexical_cast<int>(variables["HasApplication"]) > 0;
199
        this->information_.bios_version_ = boost::lexical_cast<unsigned int>(variables["BiosVersion"]);
201
            this->information_.bios_version_ = boost::lexical_cast<unsigned int>(variables["BiosVersion"]);
200
        this->information_.device_type_ = variables["DeviceType"];
202
            this->information_.device_type_ = variables["DeviceType"];
201
        this->information_.valid_ = true;
203
            this->information_.valid_ = true;
202
    }
204
        }
203
   
205
       
204
    if (target_state == STATE_INVALID)
206
        if (target_state == STATE_INVALID)
205
    {
207
        {
206
        //this->SendReset();
208
            //this->SendReset();
207
        target_state = STATE_NORM_OFFLINE;
209
            target_state = STATE_NORM_OFFLINE;
208
    }
210
        }
209
    else if (target_state == STATE_NO_CHANGE)
211
        else if (target_state == STATE_NO_CHANGE)
210
    {
212
        {
211
        return;
213
            return;
212
    }
214
        }
213
   
215
       
214
    if ((this->state_ == STATE_APGM_END || this->state_ == STATE_BPGM_END) && event == EVENT_PGM_ACK)
216
        if ((this->state_ == STATE_APGM_END || this->state_ == STATE_BPGM_END) && event == EVENT_PGM_ACK)
215
    {
217
        {
216
        unsigned int checksum = boost::lexical_cast<unsigned int>(variables["Data"]);
218
            unsigned int checksum = boost::lexical_cast<unsigned int>(variables["Data"]);
217
       
219
           
218
        if (this->expected_ack_data_ != checksum)
220
            if (this->expected_ack_data_ != checksum)
219
        {
221
            {
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...");
222
                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...");
221
 
223
 
222
            this->SendReset();
224
                this->SendReset();
223
 
225
 
224
            target_state = STATE_NORM_OFFLINE;
226
                target_state = STATE_NORM_OFFLINE;
225
        }
227
            }
226
        else if (this->state_ == STATE_BPGM_END)
228
            else if (this->state_ == STATE_BPGM_END)
227
        {
229
            {
228
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
230
                float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
229
           
231
               
230
            LOG.Info("Data was successfully transferred to " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
232
                LOG.Info("Data was successfully transferred to " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
231
           
233
               
232
            LOG.Debug("Sending application programming copy to node " + this->id_);
234
                LOG.Debug("Sending application programming copy to node " + this->id_);
233
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Copy");
235
                can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Copy");
234
           
236
               
235
            unsigned int source0 = GET_LOW_BYTE_16(this->start_offset_);
237
                unsigned int source0 = GET_LOW_BYTE_16(this->start_offset_);
236
            unsigned int source1 = GET_HIGH_BYTE_16(this->start_offset_);
238
                unsigned int source1 = GET_HIGH_BYTE_16(this->start_offset_);
237
           
239
               
238
            unsigned int destination0 = GET_LOW_BYTE_16(this->code_->GetAddressLower());
240
                unsigned int destination0 = GET_LOW_BYTE_16(this->code_->GetAddressLower());
239
            unsigned int destination1 = GET_HIGH_BYTE_16(this->code_->GetAddressLower());
241
                unsigned int destination1 = GET_HIGH_BYTE_16(this->code_->GetAddressLower());
240
           
242
               
241
            unsigned int length0 = GET_LOW_BYTE_16(this->code_->GetLength());
243
                unsigned int length0 = GET_LOW_BYTE_16(this->code_->GetLength());
242
            unsigned int length1 = GET_HIGH_BYTE_16(this->code_->GetLength());
244
                unsigned int length1 = GET_HIGH_BYTE_16(this->code_->GetLength());
243
           
245
               
244
            payload->SetVariable("Source0", boost::lexical_cast<std::string>(source0));
246
                payload->SetVariable("Source0", boost::lexical_cast<std::string>(source0));
245
            payload->SetVariable("Source1", boost::lexical_cast<std::string>(source1));
247
                payload->SetVariable("Source1", boost::lexical_cast<std::string>(source1));
246
            payload->SetVariable("Destination0", boost::lexical_cast<std::string>(destination0));
248
                payload->SetVariable("Destination0", boost::lexical_cast<std::string>(destination0));
247
            payload->SetVariable("Destination1", boost::lexical_cast<std::string>(destination1));
249
                payload->SetVariable("Destination1", boost::lexical_cast<std::string>(destination1));
248
            payload->SetVariable("Length0", boost::lexical_cast<std::string>(length0));
250
                payload->SetVariable("Length0", boost::lexical_cast<std::string>(length0));
249
            payload->SetVariable("Length1", boost::lexical_cast<std::string>(length1));
251
                payload->SetVariable("Length1", boost::lexical_cast<std::string>(length1));
250
           
252
               
251
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
253
                broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
252
        }
254
            }
253
        else
255
            else
254
        {
256
            {
255
            float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
257
                float speed = (float)this->code_->GetLength() / (float)(time(NULL) - this->program_start_time_);
256
           
258
               
257
            LOG.Info("Programming was completed successfully on " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
259
                LOG.Info("Programming was completed successfully on " + this->id_ + ", speed was " + boost::lexical_cast<std::string>(speed) + " B/s");
258
        }
260
            }
259
    }
261
        }
260
   
262
       
261
    if (target_state == STATE_NORM_LIST)
263
        if (target_state == STATE_NORM_LIST)
262
    {
264
        {
263
        this->SendListRequest();
265
            this->SendListRequest();
264
    }
266
        }
265
    else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
267
        else if (target_state == STATE_BPGM_OFFLINE || target_state == STATE_APGM_OFFLINE)
266
    {
268
        {
267
        if (event != EVENT_BIOS_START)
269
            if (event != EVENT_BIOS_START)
268
        {
270
            {
269
            this->SendReset();
271
                this->SendReset();
270
        }
272
            }
271
    }
273
        }
272
    else if (target_state == STATE_APGM_START || target_state == STATE_BPGM_START)
274
        else if (target_state == STATE_APGM_START || target_state == STATE_BPGM_START)
273
    {
275
        {
274
        if (!this->code_->IsValid())
276
            if (!this->code_->IsValid())
275
        {
277
            {
276
            LOG.Error("Code is not valid, aborting...");
278
                LOG.Error("Code is not valid, aborting...");
277
            target_state = STATE_NORM_OFFLINE;
279
                target_state = STATE_NORM_OFFLINE;
278
        }
280
            }
279
        else
281
            else
280
        {
282
            {
281
            LOG.Debug("Sending programming start to node " + this->id_);
283
                LOG.Debug("Sending programming start to node " + this->id_);
282
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Start");
284
                can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Start");
283
            payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
285
                payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
284
           
286
               
285
            this->start_offset_ = target_state == STATE_BPGM_START ? 0 : this->code_->GetAddressLower();
287
                this->start_offset_ = target_state == STATE_BPGM_START ? 0 : this->code_->GetAddressLower();
286
           
288
               
287
            unsigned int address0 = GET_LOW_BYTE_16(this->start_offset_);
289
                unsigned int address0 = GET_LOW_BYTE_16(this->start_offset_);
288
            unsigned int address1 = GET_HIGH_BYTE_16(this->start_offset_);
290
                unsigned int address1 = GET_HIGH_BYTE_16(this->start_offset_);
289
           
291
               
290
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_);
292
                this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_);
291
           
293
               
292
            payload->SetVariable("Address0", boost::lexical_cast<std::string>(address0));
294
                payload->SetVariable("Address0", boost::lexical_cast<std::string>(address0));
293
            payload->SetVariable("Address1", boost::lexical_cast<std::string>(address1));
295
                payload->SetVariable("Address1", boost::lexical_cast<std::string>(address1));
294
            payload->SetVariable("Address2", "0"); // Not used?
296
                payload->SetVariable("Address2", "0"); // Not used?
295
            payload->SetVariable("Address3", "0"); // Not used?
297
                payload->SetVariable("Address3", "0"); // Not used?
296
           
298
               
297
            this->current_offset_ = 0;
299
                this->current_offset_ = 0;
298
           
300
               
299
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
301
                broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
300
           
302
               
301
            this->program_start_time_ = time(NULL);
303
                this->program_start_time_ = time(NULL);
302
        }
304
            }
303
    }
305
        }
304
    else if (target_state == STATE_APGM_DATA || target_state == STATE_BPGM_DATA)
306
        else if (target_state == STATE_APGM_DATA || target_state == STATE_BPGM_DATA)
305
    {
307
        {
306
        unsigned int offset = boost::lexical_cast<unsigned int>(variables["Data"]);
308
            unsigned int offset = boost::lexical_cast<unsigned int>(variables["Data"]);
307
       
309
           
308
        if ( this->expected_ack_data_ != offset)
310
            if ( this->expected_ack_data_ != offset)
309
        {
311
            {
310
            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...");
312
                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...");
311
            target_state = STATE_NORM_OFFLINE;
313
                target_state = STATE_NORM_OFFLINE;
312
        }
314
            }
313
        else if (this->current_offset_ >= this->code_->GetLength())
315
            else if (this->current_offset_ >= this->code_->GetLength())
314
        {
316
            {
315
            LOG.Debug("Sending programming end to node " + this->id_);
317
                LOG.Debug("Sending programming end to node " + this->id_);
316
           
318
               
317
            unsigned int checksum = this->code_->GetChecksum();
319
                unsigned int checksum = this->code_->GetChecksum();
318
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(checksum);
320
                this->expected_ack_data_ = SWAP_BYTE_ORDER_16(checksum);
319
           
321
               
320
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_End");
322
                can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_End");
321
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
323
                broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
322
           
324
               
323
            target_state = target_state == STATE_BPGM_DATA ? STATE_BPGM_END : STATE_APGM_END;
325
                target_state = target_state == STATE_BPGM_DATA ? STATE_BPGM_END : STATE_APGM_END;
324
        }
326
            }
325
        else
327
            else
326
        {
328
            {
327
            LOG.Debug("Sending programming data to node " + this->id_);
329
                LOG.Debug("Sending programming data to node " + this->id_);
328
            can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Data_48");
330
                can::Message* payload = new can::Message("nmt", "", "", 0, "Pgm_Data_48");
329
           
331
               
330
            unsigned int offset0 = GET_LOW_BYTE_16(this->start_offset_ + this->current_offset_);
332
                unsigned int offset0 = GET_LOW_BYTE_16(this->start_offset_ + this->current_offset_);
331
            unsigned int offset1 = GET_HIGH_BYTE_16(this->start_offset_ + this->current_offset_);
333
                unsigned int offset1 = GET_HIGH_BYTE_16(this->start_offset_ + this->current_offset_);
332
           
334
               
333
            this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_ + this->current_offset_);
335
                this->expected_ack_data_ = SWAP_BYTE_ORDER_16(this->start_offset_ + this->current_offset_);
334
           
336
               
335
            payload->SetVariable("Offset0", boost::lexical_cast<std::string>(offset0));
337
                payload->SetVariable("Offset0", boost::lexical_cast<std::string>(offset0));
336
            payload->SetVariable("Offset1", boost::lexical_cast<std::string>(offset1));
338
                payload->SetVariable("Offset1", boost::lexical_cast<std::string>(offset1));
337
           
339
               
338
            for (int n = 0; (this->current_offset_ < this->code_->GetLength()) && (n < 6); n++)
340
                for (int n = 0; (this->current_offset_ < this->code_->GetLength()) && (n < 6); n++)
339
            {
341
                {
340
                //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_)));
342
                    //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_)));
341
                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_)));
343
                    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_)));
342
                this->current_offset_++;
344
                    this->current_offset_++;
343
            }
345
                }
344
           
346
               
345
            broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
347
                broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
346
        }
348
            }
347
    }
349
        }
348
    else if (target_state == STATE_BPGM_COPY)
350
        else if (target_state == STATE_BPGM_COPY)
349
    {
351
        {
350
        LOG.Debug("Preparing to send null application to node " + this->id_);
352
            LOG.Debug("Preparing to send null application to node " + this->id_);
351
       
353
           
352
        this->code_->Reset(false);
354
            this->code_->Reset(false);
353
       
355
           
354
        this->code_->AddByte(0xFF);
356
            this->code_->AddByte(0xFF);
355
        this->code_->AddByte(0xFF);
357
            this->code_->AddByte(0xFF);
356
    }
358
        }
357
    else if (target_state == STATE_NORM_OFFLINE)
359
        else if (target_state == STATE_NORM_OFFLINE)
358
    {
360
        {
359
        this->information_.valid_ = false;
361
            this->information_.valid_ = false;
360
        this->SendReset();
362
            this->SendReset();
361
    }
363
        }
362
   
364
       
363
   
365
       
364
    if (this->state_ != target_state)
366
        if (this->state_ != target_state)
365
    {
367
        {
366
        this->signal_on_new_state_(this->id_, this->state_, target_state);
368
            this->signal_on_new_state_(this->id_, this->state_, target_state);
367
    }
369
        }
368
   
370
       
369
    this->state_ = target_state;
371
        this->state_ = target_state;
-
 
372
    }
370
}
373
}
371
 
374
 
372
bool Node::CheckTimeout()
375
bool Node::CheckTimeout()
373
{
376
{
374
    if (this->state_ == STATE_NORM_OFFLINE)
377
    if (this->state_ == STATE_NORM_OFFLINE)
375
    {
378
    {
376
        return true;
379
        return true;
377
    }
380
    }
378
   
381
   
379
    if (this->code_.use_count() == 0 && this->information_.last_active_ + 10 < time(NULL))
382
    if (this->code_.use_count() == 0 && this->information_.last_active_ + 10 < time(NULL))
380
    {
383
    {
Line 384... Line 387...
384
   
387
   
385
    return true;
388
    return true;
386
}
389
}
387
 
390
 
388
void Node::ResetTimeout()
391
void Node::ResetTimeout()
389
{
392
{
390
    this->information_.last_active_ = time(NULL);
393
    this->information_.last_active_ = time(NULL);
391
}
394
}
392
 
395
 
393
void Node::Reset()
396
void Node::Reset()
394
{
397
{
395
    common::StringMap variables;
398
    common::StringMap variables;
396
   
399
   
397
    this->Trigger(EVENT_RESET, variables);
400
    this->Trigger(EVENT_RESET, variables);
398
}
401
}
399
 
402
 
400
void Node::ProgramApplication(Code::Pointer code)
403
void Node::ProgramApplication(Code::Pointer code)
401
{
404
{
402
    this->code_ = code;
405
    this->code_ = code;
403
   
406
   
404
    common::StringMap variables;
407
    common::StringMap variables;
405
   
408
   
406
    this->Trigger(EVENT_PROGRAM_APP, variables);
409
    this->Trigger(EVENT_PROGRAM_APP, variables);
407
}
410
}
408
 
411
 
409
void Node::ProgramBios(Code::Pointer code)
412
void Node::ProgramBios(Code::Pointer code)
410
{
413
{
411
    this->code_ = code;
414
    this->code_ = code;
412
   
415
   
413
    common::StringMap variables;
416
    common::StringMap variables;
414
   
417
   
415
    this->Trigger(EVENT_PROGRAM_BIOS, variables);
418
    this->Trigger(EVENT_PROGRAM_BIOS, variables);
416
}
419
}
417
 
420
 
418
void Node::SendReset()
421
void Node::SendReset()
419
{
422
{
-
 
423
    if (!this->id_.empty())
-
 
424
    {
420
    LOG.Info("Sending node reset to " + this->id_);
425
        LOG.Info("Sending node reset to " + this->id_);
421
    can::Message* payload = new can::Message("nmt", "", "", 0, "Reset");
426
        can::Message* payload = new can::Message("nmt", "", "", 0, "Reset");
422
    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_)));
423
   
428
       
424
    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())));
-
 
430
    }
425
}
431
}
426
 
432
 
427
void Node::SendListRequest()
433
void Node::SendListRequest()
428
{
434
{
-
 
435
    if (!this->id_.empty())
-
 
436
    {
429
    LOG.Debug("Sending module listing on node " + this->id_);
437
        LOG.Debug("Sending module listing on node " + this->id_);
430
    can::Message* payload = new can::Message("mnmt", "To_Owner", "", 0, "List");
438
        can::Message* payload = new can::Message("mnmt", "To_Owner", "", 0, "List");
431
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
439
        payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(common::FromHex(this->id_)));
432
   
440
       
433
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
441
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), Manager::Instance().get())));
-
 
442
    }
434
}
443
}
435
 
444
 
436
}; // namespace control
445
}; // namespace control
437
}; // namespace atom
446
}; // namespace atom