Subversion Repositories HomeAutomation

Rev

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

Rev 1986 Rev 1987
Line 32... Line 32...
32
#include "Protocol.h"
32
#include "Protocol.h"
33
#include "Message.h"
33
#include "Message.h"
34
 
34
 
35
#include "common/Bitset.h"
35
#include "common/Bitset.h"
36
#include "common/common.h"
36
#include "common/common.h"
-
 
37
#include "common/log.h"
37
 
38
 
38
namespace atom {
39
namespace atom {
39
namespace can {
40
namespace can {
40
 
41
 
41
enum
42
enum
Line 43... Line 44...
43
    PACKET_START = 253,
44
    PACKET_START = 253,
44
    PACKET_END   = 250,
45
    PACKET_END   = 250,
45
    PACKET_PING  = 251
46
    PACKET_PING  = 251
46
};
47
};
47
   
48
 
-
 
49
static const std::string log_module_ = "can::network";
-
 
50
 
48
Network::Network(std::string address): broker::Subscriber(false), buffer_(2048), LOG("can::Network")
51
Network::Network(std::string address): broker::Subscriber(false), LOG("can::Network")
49
{
52
{
50
    this->address_ = address;
53
    this->address_ = address;
51
    this->client_id_ = 0;
54
    this->client_id_ = 0;
52
       
55
       
53
    // Examples of address
56
    // Examples of address
54
    // udp:192.168.1.250:1100
57
    // udp:192.168.1.250:1100
55
    // serial:/dev/ttyUSB0:38400
58
    // serial:/dev/ttyUSB0:38400
56
   
59
   
57
    common::StringList parts;
60
    common::StringList parts;
58
    boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off);
61
    boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off);
59
   
62
   
60
    if (parts.size() < 3)
63
    if (parts.size() < 3)
61
    {
64
    {
62
        LOG.Error("Malformed address string: " + address);
65
        LOG.Error("Malformed address string: " + address);
63
        return;
66
        return;
64
    }
67
    }
65
   
68
   
66
    boost::algorithm::to_lower(parts[0]);
69
    boost::algorithm::to_lower(parts[0]);
67
   
70
   
68
    if (parts[0] == "udp")
71
    if (parts[0] == "udp")
Line 72... Line 75...
72
    else if (parts[0] == "serial")
75
    else if (parts[0] == "serial")
73
    {
76
    {
74
        this->protocol_ = net::PROTOCOL_SERIAL;
77
        this->protocol_ = net::PROTOCOL_SERIAL;
75
    }
78
    }
76
    else
79
    else
77
    {
80
    {
78
        LOG.Error("Unknown protocol, only support udp and serial, got " + parts[0]);
81
        LOG.Error("Unknown protocol, only support udp and serial, got " + parts[0]);
79
        return;
82
        return;
80
    }
83
    }
81
   
84
   
82
    this->address_ = parts[1];
85
    this->address_ = parts[1];
Line 101... Line 104...
101
}
104
}
102
 
105
 
103
Network::~Network()
106
Network::~Network()
104
{
107
{
105
    net::Manager::Instance()->Disconnect(this->client_id_);
108
    net::Manager::Instance()->Disconnect(this->client_id_);
106
}
109
}
107
 
110
 
108
void Network::SlotOnTimeout(timer::TimerId timer_id)
111
void Network::SlotOnTimeout(timer::TimerId timer_id)
109
{
112
{
110
    if (timer_id == this->timer_id_)
113
    if (timer_id == this->timer_id_)
111
    {
114
    {
112
        this->io_service_.post(boost::bind(&Network::SlotOnTimeoutHandler, this, timer_id));
115
        this->io_service_.post(boost::bind(&Network::SlotOnTimeoutHandler, this, timer_id));
113
    }
116
    }
114
}
117
}
115
 
118
 
116
void Network::SlotOnMessageHandler(broker::Message::Pointer message)
119
void Network::SlotOnMessageHandler(broker::Message::Pointer message)
117
{
120
{
118
    if (message->GetType() == broker::Message::CAN_MESSAGE)
121
    if (message->GetType() == broker::Message::CAN_MESSAGE)
119
    {
122
    {
Line 122... Line 125...
122
       
125
       
123
        data[0] = PACKET_START;
126
        data[0] = PACKET_START;
124
       
127
       
125
        unsigned int class_id = Protocol::Instance()->ResolveClassId(payload->GetClassName());
128
        unsigned int class_id = Protocol::Instance()->ResolveClassId(payload->GetClassName());
126
        data[4] = class_id << 1;
129
        data[4] = class_id << 1;
127
 
130
 
128
        if (payload->GetClassName() == "nmt")
131
        if (payload->GetClassName() == "nmt")
129
        {
132
        {
130
            unsigned int command_id = Protocol::Instance()->ResolveNMTCommandId(payload->GetCommandName());
133
            unsigned int command_id = Protocol::Instance()->ResolveNMTCommandId(payload->GetCommandName());
131
            data[3] = command_id;
134
            data[3] = command_id;
132
        }
135
        }
133
        else
136
        else
Line 137... Line 140...
137
           
140
           
138
            unsigned int module_id = Protocol::Instance()->ResolveModuleId(payload->GetModuleName());
141
            unsigned int module_id = Protocol::Instance()->ResolveModuleId(payload->GetModuleName());
139
            data[3] = module_id;
142
            data[3] = module_id;
140
           
143
           
141
            data[2] = payload->GetId();
144
            data[2] = payload->GetId();
142
           
145
           
143
            unsigned int command_id = Protocol::Instance()->ResolveCommandId(payload->GetCommandName(), payload->GetModuleName());
146
            unsigned int command_id = Protocol::Instance()->ResolveCommandId(payload->GetCommandName(), payload->GetModuleName());
144
            data[1] = command_id;
147
            data[1] = command_id;
145
        }
148
        }
146
       
149
       
147
        unsigned int highest_bit = 0;
150
        unsigned int highest_bit = 0;
148
        common::Bitset databits(64);
151
        common::Bitset databits(64);
149
       
152
       
150
        xml::Node::NodeList variable_nodes;
153
        xml::Node::NodeList variable_nodes;
151
        unsigned int start_bit;
154
        unsigned int start_bit;
152
        unsigned int bit_length;
155
        unsigned int bit_length;
153
        std::string type;
156
        std::string type;
154
        std::string value;
157
        std::string value;
Line 158... Line 161...
158
            variable_nodes = Protocol::Instance()->GetNMTCommandVariables(payload->GetCommandName());
161
            variable_nodes = Protocol::Instance()->GetNMTCommandVariables(payload->GetCommandName());
159
        }
162
        }
160
        else
163
        else
161
        {
164
        {
162
            variable_nodes = Protocol::Instance()->GetCommandVariables(payload->GetCommandName(), payload->GetModuleName());
165
            variable_nodes = Protocol::Instance()->GetCommandVariables(payload->GetCommandName(), payload->GetModuleName());
163
        }
166
        }
164
       
167
       
165
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
168
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
166
        {
169
        {
167
            value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name"));
170
            value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name"));
168
            //LOG.Debug(variable_nodes[n].GetAttributeValue("name") + "=" + value); 
171
            //LOG.Debug(variable_nodes[n].GetAttributeValue("name") + "=" + value); 
Line 222... Line 225...
222
        {
225
        {
223
            data[8 + n] = databits.GetBytes()[n];
226
            data[8 + n] = databits.GetBytes()[n];
224
        }
227
        }
225
       
228
       
226
        data[16] = PACKET_END;
229
        data[16] = PACKET_END;
227
       
-
 
228
        data.SetSize(17);
-
 
229
       
230
       
230
        //LOG.Debug("Bytes: " + data.ToDebugString());
231
        //LOG.Debug("Bytes: " + data.ToDebugString());
231
        net::Manager::Instance()->SendTo(this->client_id_, data);
232
        net::Manager::Instance()->SendTo(this->client_id_, data);
232
    } else if (message->GetType() == broker::Message::CAN_RAW_MESSAGE)
233
    } else if (message->GetType() == broker::Message::CAN_RAW_MESSAGE)
233
    {
234
    {
Line 273... Line 274...
273
            value = line.substr(index+17,2);
274
            value = line.substr(index+17,2);
274
        data[8+length] = common::FromHex(value);
275
        data[8+length] = common::FromHex(value);
275
        //LOG.Info("data: " + value + " data: ");//+ data[6]);
276
        //LOG.Info("data: " + value + " data: ");//+ data[6]);
276
        index += 3;
277
        index += 3;
277
        length++;
278
        length++;
278
        }
279
        }
279
       
280
       
280
        data[7] = length;
281
        data[7] = length;
281
        data[16] = PACKET_END;
282
        data[16] = PACKET_END;
282
       
283
 
283
        data.SetSize(17);
-
 
284
       
284
       
285
        //LOG.Info("Bytes: " + data.ToDebugString());
285
        //LOG.Info("Bytes: " + data.ToDebugString());
286
        net::Manager::Instance()->SendTo(this->client_id_, data);
286
        net::Manager::Instance()->SendTo(this->client_id_, data);
287
    }
287
    }
288
}
288
}
289
 
289
 
290
void Network::SlotOnNewDataHandler(net::SocketId client_id, common::Byteset data)
290
void Network::SlotOnNewDataHandler(net::SocketId client_id, common::Byteset data)
291
{
291
{
292
    if (client_id != this->client_id_)
292
    if (client_id != this->client_id_)
293
    {
293
    {
294
      return;
294
      return;
295
    }
295
    }
296
   
296
   
297
    static bool have_start = false;
297
    static bool have_start = false;
298
   
298
   
299
    for (unsigned int n = 0; n < data.GetSize(); n++)
299
    for (unsigned int n = 0; n < data.size(); n++)
300
    {
300
    {
301
        if (have_start)
301
      log::Debug(log_module_, "data[%u]=%u", n, (unsigned int)data[n]);
302
        {
302
     
303
            if (data[n] == PACKET_END && this->buffer_.GetSize() == 15)
303
        if (have_start)
304
            {
304
        {
-
 
305
          log::Debug(log_module_, "have_start, this->buffer_.size() = %u", this->buffer_.size());
-
 
306
         
-
 
307
            if (data[n] == PACKET_END && this->buffer_.size() == 15)
-
 
308
            {
-
 
309
              log::Debug(log_module_, "PACKET_END");
-
 
310
             
305
                this->ProcessBuffer();
311
                this->ProcessBuffer();
306
                have_start = false;
312
                have_start = false;
307
            }
313
            }
308
            else
314
            else
309
            {
315
            {
310
                this->buffer_.Append(data[n]);
316
                this->buffer_.push_back(data[n]);
311
            }
317
            }
312
        }
318
        }
313
        else if (data[n] == PACKET_START)
319
        else if (data[n] == PACKET_START)
314
        {
320
        {
-
 
321
          common::Byteset empty_vector;
315
            this->buffer_.Clear();
322
            this->buffer_.swap(empty_vector);
-
 
323
 
316
            have_start = true;
324
            have_start = true;
317
        }
325
        }
318
        else if (data[n] == PACKET_PING)
326
        else if (data[n] == PACKET_PING)
319
        {
327
        {
320
            LOG.Info("Received pong.");
328
            LOG.Info("Received pong.");
Line 364... Line 372...
364
    }
372
    }
365
}
373
}
366
 
374
 
367
void Network::ProcessBuffer()
375
void Network::ProcessBuffer()
368
{
376
{
-
 
377
  LOG_DEBUG_ENTER;
-
 
378
 
369
    try
379
    try
370
    {
380
    {
371
        std::string class_name = "";
381
        std::string class_name = "";
372
        std::string direction_name = "";
382
        std::string direction_name = "";
373
        std::string module_name = "";
383
        std::string module_name = "";
Line 377... Line 387...
377
    std::string PKTstring = "PKT " +
387
    std::string PKTstring = "PKT " +
378
                atom::common::ToHex8bit((unsigned int)this->buffer_[3]) +
388
                atom::common::ToHex8bit((unsigned int)this->buffer_[3]) +
379
                atom::common::ToHex8bit((unsigned int)this->buffer_[2]) +
389
                atom::common::ToHex8bit((unsigned int)this->buffer_[2]) +
380
                atom::common::ToHex8bit((unsigned int)this->buffer_[1]) +
390
                atom::common::ToHex8bit((unsigned int)this->buffer_[1]) +
381
                atom::common::ToHex8bit((unsigned int)this->buffer_[0]) +
391
                atom::common::ToHex8bit((unsigned int)this->buffer_[0]) +
382
                " " +
392
                " " +
383
                atom::common::ToHex4bit((unsigned int)this->buffer_[4]) +
393
                atom::common::ToHex4bit((unsigned int)this->buffer_[4]) +
384
                " " +
394
                " " +
385
                atom::common::ToHex4bit((unsigned int)this->buffer_[5]);
395
                atom::common::ToHex4bit((unsigned int)this->buffer_[5]);
386
   
396
   
387
    for (unsigned int index = 7; index < 7 + (unsigned int)this->buffer_[6]; index++)
397
    for (unsigned int index = 7; index < 7 + (unsigned int)this->buffer_[6]; index++)
388
    {
398
    {
389
        PKTstring += " " + atom::common::ToHex8bit((unsigned int)this->buffer_[index]) ;
399
        PKTstring += " " + atom::common::ToHex8bit((unsigned int)this->buffer_[index]) ;
390
    }
400
    }
391
   
401
   
Line 393... Line 403...
393
   
403
   
394
    //LOG.Info(PKTstring);
404
    //LOG.Info(PKTstring);
395
   
405
   
396
    std::string* payload_str = new std::string(PKTstring);
406
    std::string* payload_str = new std::string(PKTstring);
397
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_MESSAGE, broker::Message::PayloadPointer(payload_str), this)));
407
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_MESSAGE, broker::Message::PayloadPointer(payload_str), this)));
398
 
408
 
399
   
409
   
400
        unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F;
410
        unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F;
401
        //LOG.Debug("class_id=" + boost::lexical_cast<std::string>(class_id));
411
        //LOG.Debug("class_id=" + boost::lexical_cast<std::string>(class_id));
402
        class_name = Protocol::Instance()->LookupClassName(class_id);
412
        class_name = Protocol::Instance()->LookupClassName(class_id);
403
       
413
       
404
        if (class_name == "nmt")
414
        if (class_name == "nmt")
Line 431... Line 441...
431
       
441
       
432
        common::Byteset data_set(length);
442
        common::Byteset data_set(length);
433
       
443
       
434
        for (unsigned int n = 0; n < length; n++)
444
        for (unsigned int n = 0; n < length; n++)
435
        {
445
        {
436
            data_set.Append(this->buffer_[n + 7]);
446
            data_set.push_back(this->buffer_[n + 7]);
437
        }
447
        }
438
 
448
 
439
        common::Bitset databits(data_set);
449
        common::Bitset databits(data_set);
440
        xml::Node::NodeList variable_nodes;
450
        xml::Node::NodeList variable_nodes;
441
        unsigned int start_bit;
451
        unsigned int start_bit;
Line 510... Line 520...
510
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
520
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
511
    }
521
    }
512
    catch (std::runtime_error& e)
522
    catch (std::runtime_error& e)
513
    {
523
    {
514
        LOG.Error("Malformed message received, " + std::string(e.what()));
524
        LOG.Error("Malformed message received, " + std::string(e.what()));
515
        LOG.Debug("Bytes: " + this->buffer_.ToDebugString());
525
        LOG.Debug("Bytes: " + std::string(this->buffer_.begin(), this->buffer_.end()));
516
    }
526
    }
517
   
527
   
-
 
528
    common::Byteset empty_vector;
518
    this->buffer_.Clear();
529
    this->buffer_.swap(empty_vector);
-
 
530
   
-
 
531
   
-
 
532
    LOG_DEBUG_EXIT;
519
}
533
}
520
   
534
   
521
}; // namespace can
535
}; // namespace can
522
}; // namespace atom
536
}; // namespace atom