Rev 1328 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1328 | Rev 1403 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | * Created on: Apr 27, 2009 |
4 | * Created on: Apr 27, 2009 |
| 5 | * Author: Mattias Runge |
5 | * Author: Mattias Runge |
| 6 | */ |
6 | */ |
| 7 | 7 | ||
| 8 | #include "CanNet.h" |
8 | #include "CanNet.h" |
| 9 | #include <iostream> |
- | |
| 10 | 9 | ||
| 11 |
|
10 | #include <iostream> |
| 12 | 11 | ||
| 13 | namespace atom { |
12 | namespace atom { |
| 14 | namespace subscribers { |
13 | namespace subscribers { |
| 15 | 14 | ||
| 16 | // TODO Fix input on the form of a URL: |
15 | // TODO Fix input on the form of a URL: |
| 17 | // ex. udp://192.168.1.250:1100 |
16 | // ex. udp://192.168.1.250:1100 |
| 18 | // ex. dev://dev/ttyUSB0:57000 |
17 | // ex. dev://dev/ttyUSB0:57000 |
| 19 | CanNet::CanNet( |
18 | CanNet::CanNet(string address, unsigned int port) : Subscriber(false) |
| 20 | { |
19 | { |
| 21 | LOG.setName("CanNetSubscriber"); |
20 | LOG.setName("CanNetSubscriber"); |
| 22 | 21 | ||
| 23 | this-> |
22 | this->udp_server = net::UdpServer::getInstance(port); |
| 24 | this-> |
23 | this->udp_server->connect(boost::bind(&CanNet::Slot_OnNewData, this, _1, _2)); |
| 25 | 24 | ||
| 26 | this-> |
25 | this->endpoint_ = this->udp_server->getEndpoint(address); |
| 27 | 26 | ||
| 28 | this-> |
27 | this->SendPing(); |
| 29 | 28 | ||
| 30 | this-> |
29 | this->Start(); |
| 31 | } |
30 | } |
| 32 | 31 | ||
| 33 | CanNet::~CanNet() |
32 | CanNet::~CanNet() |
| 34 | { |
33 | { |
| 35 | } |
34 | } |
| 36 | 35 | ||
| 37 |
|
36 | ByteList CanNet::BuildPacket(unsigned int id, ByteList data) |
| 38 | { |
37 | { |
| 39 |
|
38 | ByteList packet; |
| 40 | 39 | ||
| 41 | packet.push_back((unsigned char) PACKET_START); |
40 | packet.push_back((unsigned char) PACKET_START); |
| 42 | 41 | ||
| 43 | packet.push_back((unsigned char) (id & 0xFF)); |
42 | packet.push_back((unsigned char) (id & 0xFF)); |
| 44 | packet.push_back((unsigned char) ((id >> 8) & 0xFF)); |
43 | packet.push_back((unsigned char) ((id >> 8) & 0xFF)); |
| Line 57... | Line 56... | ||
| 57 | packet.push_back(data[n]); |
56 | packet.push_back(data[n]); |
| 58 | } |
57 | } |
| 59 | else |
58 | else |
| 60 | { |
59 | { |
| 61 | packet.push_back(' '); |
60 | packet.push_back(' '); |
| 62 | } |
61 | } |
| - | 62 | } |
|
| - | 63 | ||
| - | 64 | packet.push_back((unsigned char) PACKET_END); |
|
| - | 65 | ||
| - | 66 | return packet; |
|
| 63 | } |
67 | } |
| 64 | 68 | ||
| - | 69 | void CanNet::SendPing() |
|
| - | 70 | { |
|
| 65 |
|
71 | ByteList data(1, (unsigned char) PACKET_PING); |
| 66 | 72 | ||
| 67 |
|
73 | LOG.info("Sending ping packet..."); |
| - | 74 | this->myUdpServer->sendTo(this->myEndpoint, data); |
|
| 68 | } |
75 | } |
| 69 | 76 | ||
| 70 | void CanNet:: |
77 | void CanNet::ProcessBuffer() |
| 71 | { |
78 | { |
| 72 | if (this-> |
79 | if (this->buffer_.size() != 15) |
| 73 | { |
80 | { |
| 74 | LOG.warn("Received packet of length " + convert::int2string(this->myBuffer.size() + 2) + ", should be 17."); |
81 | LOG.warn("Received packet of length " + convert::int2string(this->myBuffer.size() + 2) + ", should be 17."); |
| 75 | this->myBuffer.clear(); |
82 | this->myBuffer.clear(); |
| 76 | return; |
83 | return; |
| 77 | } |
84 | } |
| Line 84... | Line 91... | ||
| 84 | data.push_back(this->myBuffer[3]); |
91 | data.push_back(this->myBuffer[3]); |
| 85 | 92 | ||
| 86 | unsigned char length = this->myBuffer[4]; |
93 | unsigned char length = this->myBuffer[4]; |
| 87 | 94 | ||
| 88 | for (unsigned char n = 0; n < length; n++) |
95 | for (unsigned char n = 0; n < length; n++) |
| 89 | { |
96 | { |
| 90 | data.push_back(this->myBuffer[5 + n]); |
97 | data.push_back(this->myBuffer[5 + n]); |
| 91 | } |
98 | } |
| 92 | 99 | ||
| 93 | BitBuffer buffer(data); |
100 | BitBuffer buffer(data); |
| 94 | 101 | ||
| 95 | Header header(buffer); |
102 | Header header(buffer); |
| 96 | 103 | ||
| 97 | Message::pointer message = boost::make_shared<Message>(header); |
104 | Message::pointer message = boost::make_shared<Message>(header); |
| 98 | 105 | ||
| 99 | message->readBits(buffer); |
106 | message->readBits(buffer); |
| 100 | 107 | ||
| 101 | this->put(message); |
108 | this->put(message); |
| 102 | 109 | ||
| 103 | this->myBuffer.clear(); |
110 | this->myBuffer.clear(); |
| 104 | } |
- | |
| 105 | - | ||
| 106 | void CanNet::sendPing() |
- | |
| 107 | { |
- | |
| 108 | byte_list data(1, (unsigned char) PACKET_PING); |
- | |
| 109 | - | ||
| 110 | LOG.info("Sending ping packet..."); |
- | |
| 111 | this->myUdpServer->sendTo(this->myEndpoint, data); |
- | |
| 112 | } |
111 | } |
| 113 | 112 | ||
| 114 | void CanNet:: |
113 | void CanNet::OnMessage(Message::Pointer message) |
| 115 | { |
114 | { |
| 116 | LOG.debug("update called"); |
115 | LOG.debug("update called"); |
| 117 | 116 | ||
| 118 | BitBuffer buffer; |
117 | BitBuffer buffer; |
| 119 | 118 | ||
| 120 | message->getHeader().writeBits(buffer); |
119 | message->getHeader().writeBits(buffer); |
| 121 | 120 | ||
| 122 | unsigned long id; |
121 | unsigned long id; |
| 123 | buffer.read(32, id); |
122 | buffer.read(32, id); |
| 124 | 123 | ||
| 125 | buffer.clear(); |
124 | buffer.clear(); |
| 126 | 125 | ||
| 127 | message->writeBits(buffer); |
126 | message->writeBits(buffer); |
| 128 | 127 | ||
| 129 | byte_list data = buffer.getAsBytes(); |
128 | byte_list data = buffer.getAsBytes(); |
| 130 | 129 | ||
| 131 | this->myUdpServer->sendTo(this->myEndpoint, buildPacket(id, data)); |
130 | this->myUdpServer->sendTo(this->myEndpoint, buildPacket(id, data)); |
| 132 | } |
131 | } |
| 133 | 132 | ||
| 134 | void CanNet:: |
133 | void CanNet::Slot_OnNewData(const udp::endpoint &sender, const ByteList &bytes) |
| 135 | { |
134 | { |
| 136 | //LOG.debug(udp::endpoint(sender).address().to_string() + "==" + this->myEndpoint.address().to_string()); |
135 | //LOG.debug(udp::endpoint(sender).address().to_string() + "==" + this->myEndpoint.address().to_string()); |
| 137 | //LOG.debug(itos(udp::endpoint(sender).port()) + "==" + itos(this->myEndpoint.port())); |
136 | //LOG.debug(itos(udp::endpoint(sender).port()) + "==" + itos(this->myEndpoint.port())); |
| 138 | if (sender.address() == this-> |
137 | if (sender.address() == this->enporint_.address()) // Port is wrong sometimes, therefor we only compare address |
| 139 | { |
138 | { |
| 140 | LOG.debug("Got data."); |
139 | LOG.debug("Got data."); |
| 141 | 140 | ||
| 142 | for (unsigned int n = 0; n < bytes.size(); n++) |
141 | for (unsigned int n = 0; n < bytes.size(); n++) |
| 143 | { |
142 | { |
| 144 | if (bytes[n] == PACKET_START) |
143 | if (bytes[n] == PACKET_START) |
| 145 | { |
144 | { |
| 146 | this-> |
145 | this->buffer_.clear(); |
| 147 | } |
146 | } |
| 148 | else if (bytes[n] == PACKET_END) |
147 | else if (bytes[n] == PACKET_END) |
| 149 | { |
148 | { |
| 150 | this-> |
149 | this->ProcessBuffer(); |
| 151 | } |
150 | } |
| 152 | else if (bytes[n] == PACKET_PING) |
151 | else if (bytes[n] == PACKET_PING) |
| 153 | { |
152 | { |
| 154 | LOG.info("Received pong."); |
153 | LOG.info("Received pong."); |
| 155 | } |
154 | } |
| 156 | else |
155 | else |
| 157 | { |
156 | { |
| 158 | //LOG.info("Received byte. " + itos(bytes[n])); |
157 | //LOG.info("Received byte. " + itos(bytes[n])); |
| 159 | this-> |
158 | this->buffer_.push_back(bytes[n]); |
| 160 | } |
159 | } |
| 161 | } |
160 | } |
| 162 | } |
161 | } |
| 163 | else |
162 | else |
| 164 | { |
163 | { |