Rev 2066 | Rev 2124 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 2066 | Rev 2118 | ||
|---|---|---|---|
| Line 20... | Line 20... | ||
| 20 | 20 | ||
| 21 | #include "Protocol.h" |
21 | #include "Protocol.h" |
| 22 | 22 | ||
| 23 | #include <stdexcept> |
23 | #include <stdexcept> |
| 24 | #include <string.h> |
24 | #include <string.h> |
| 25 | - | ||
| - | 25 | #include <stdint.h> |
|
| 26 | #include <boost/lexical_cast.hpp> |
26 | #include <boost/lexical_cast.hpp> |
| 27 | 27 | ||
| 28 | #include "common/log.h" |
28 | #include "common/log.h" |
| 29 | 29 | ||
| 30 | namespace atom { |
30 | namespace atom { |
| Line 141... | Line 141... | ||
| 141 | std::string Protocol::LookupNMTCommandName(unsigned int command_id) |
141 | std::string Protocol::LookupNMTCommandName(unsigned int command_id) |
| 142 | { |
142 | { |
| 143 | try |
143 | try |
| 144 | { |
144 | { |
| 145 | return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
145 | return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
| 146 | } |
146 | } |
| 147 | catch (std::runtime_error& e) |
147 | catch (std::runtime_error& e) |
| 148 | { |
148 | { |
| 149 | LOG.Error(e.what()); |
149 | LOG.Error(e.what()); |
| 150 | throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id)); |
150 | throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id)); |
| 151 | } |
151 | } |
| 152 | } |
152 | } |
| 153 | 153 | ||
| 154 | unsigned int Protocol::ResolveNMTCommandId(std::string command_name) |
154 | unsigned int Protocol::ResolveNMTCommandId(std::string command_name) |
| 155 | { |
155 | { |
| 156 | try |
156 | try |
| 157 | { |
157 | { |
| 158 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id")); |
158 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id")); |
| 159 | } |
159 | } |
| 160 | catch (std::runtime_error& e) |
160 | catch (std::runtime_error& e) |
| 161 | { |
161 | { |
| 162 | LOG.Error(e.what()); |
162 | LOG.Error(e.what()); |
| 163 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
163 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
| 164 | } |
164 | } |
| 165 | } |
165 | } |
| 166 | 166 | ||
| 167 | std::string Protocol::GetDefineId(std::string name, std::string group) |
167 | std::string Protocol::GetDefineId(std::string name, std::string group) |
| 168 | { |
168 | { |
| 169 | try |
169 | try |
| 170 | { |
170 | { |
| 171 | return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id"); |
171 | return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id"); |
| 172 | } |
172 | } |
| 173 | catch (std::runtime_error& e) |
173 | catch (std::runtime_error& e) |
| 174 | { |
174 | { |
| 175 | LOG.Error(e.what()); |
175 | LOG.Error(e.what()); |
| 176 | throw std::runtime_error("No such define found, name = " + name + ", group = " + group); |
176 | throw std::runtime_error("No such define found, name = " + name + ", group = " + group); |
| 177 | } |
177 | } |
| 178 | } |
178 | } |
| 179 | 179 | ||
| 180 | std::string Protocol::LookupDirectionFlag(unsigned int direction_flag) |
180 | std::string Protocol::LookupDirectionFlag(unsigned int direction_flag) |
| Line 185... | Line 185... | ||
| 185 | } |
185 | } |
| 186 | catch (std::runtime_error& e) |
186 | catch (std::runtime_error& e) |
| 187 | { |
187 | { |
| 188 | LOG.Error(e.what()); |
188 | LOG.Error(e.what()); |
| 189 | throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag)); |
189 | throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag)); |
| 190 | } |
190 | } |
| 191 | } |
191 | } |
| 192 | 192 | ||
| 193 | unsigned int Protocol::ResolveDirectionFlag(std::string direction_name) |
193 | unsigned int Protocol::ResolveDirectionFlag(std::string direction_name) |
| 194 | { |
194 | { |
| 195 | try |
195 | try |
| 196 | { |
196 | { |
| 197 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id")); |
197 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id")); |
| 198 | } |
198 | } |
| 199 | catch (std::runtime_error& e) |
199 | catch (std::runtime_error& e) |
| 200 | { |
200 | { |
| 201 | LOG.Error(e.what()); |
201 | LOG.Error(e.what()); |
| 202 | throw std::runtime_error("No such direction flag found, direction_name = " + direction_name); |
202 | throw std::runtime_error("No such direction flag found, direction_name = " + direction_name); |
| 203 | } |
203 | } |
| 204 | } |
204 | } |
| 205 | 205 | ||
| 206 | std::string Protocol::LookupModuleName(unsigned int module_id) |
206 | std::string Protocol::LookupModuleName(unsigned int module_id) |
| 207 | { |
207 | { |
| 208 | try |
208 | try |
| 209 | { |
209 | { |
| 210 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
210 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
| 211 | } |
211 | } |
| 212 | catch (std::runtime_error& e) |
212 | catch (std::runtime_error& e) |
| 213 | { |
213 | { |
| 214 | LOG.Error(e.what()); |
214 | LOG.Error(e.what()); |
| Line 219... | Line 219... | ||
| 219 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
219 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
| 220 | { |
220 | { |
| 221 | if (module_name == "") |
221 | if (module_name == "") |
| 222 | { |
222 | { |
| 223 | return 0; |
223 | return 0; |
| 224 | } |
224 | } |
| 225 | 225 | ||
| 226 | try |
226 | try |
| 227 | { |
227 | { |
| 228 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
228 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
| 229 | } |
229 | } |
| 230 | catch (std::runtime_error& e) |
230 | catch (std::runtime_error& e) |
| 231 | { |
231 | { |
| 232 | LOG.Error(e.what()); |
232 | LOG.Error(e.what()); |
| 233 | throw std::runtime_error("No such module name found, " + module_name); |
233 | throw std::runtime_error("No such module name found, " + module_name); |
| 234 | } |
234 | } |
| 235 | } |
235 | } |
| 236 | 236 | ||
| Line 303... | Line 303... | ||
| 303 | 303 | ||
| 304 | return xml::Node::NodeList(); |
304 | return xml::Node::NodeList(); |
| 305 | } |
305 | } |
| 306 | 306 | ||
| 307 | std::string Protocol::DecodeInt(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
307 | std::string Protocol::DecodeInt(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 308 | { |
308 | { |
| 309 | bool sign = bitset.Get(start_bit) == 1; |
309 | bool sign = bitset.Get(start_bit) == 1; |
| 310 | 310 | ||
| 311 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
311 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 312 | 312 | ||
| 313 | long raw_value = raw_bit_value; |
313 | long raw_value = raw_bit_value; |
| 314 | 314 | ||
| 315 | if (sign) |
315 | if (sign) |
| 316 | { |
316 | { |
| 317 | long mask = 0; |
317 | long mask = 0; |
| 318 | 318 | ||
| 319 | for (unsigned int n = 0; n < bit_length; n++) |
319 | for (unsigned int n = 0; n < bit_length; n++) |
| 320 | { |
320 | { |
| 321 | mask += (1 << n); |
321 | mask += (1 << n); |
| 322 | } |
322 | } |
| 323 | 323 | ||
| 324 | raw_value = -((~raw_bit_value) & mask); |
324 | raw_value = -((~raw_bit_value) & mask); |
| 325 | } |
325 | } |
| 326 | 326 | ||
| 327 | return boost::lexical_cast<std::string>(raw_value); |
327 | return boost::lexical_cast<std::string>(raw_value); |
| 328 | } |
328 | } |
| 329 | 329 | ||
| 330 | void Protocol::EncodeInt(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
330 | void Protocol::EncodeInt(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 331 | { |
331 | { |
| 332 | unsigned long raw_bit_value = 0; |
332 | unsigned long raw_bit_value = 0; |
| 333 | long raw_value = boost::lexical_cast<long>(value); |
333 | long raw_value = boost::lexical_cast<long>(value); |
| 334 | //LOG.Info("Float as int: " + raw_value); |
334 | //LOG.Info("Float as int: " + raw_value); |
| 335 | 335 | ||
| 336 | raw_bit_value = raw_value; |
336 | raw_bit_value = raw_value; |
| 337 | 337 | ||
| 338 | if (raw_value < 0) |
338 | if (raw_value < 0) |
| 339 | { |
339 | { |
| Line 344... | Line 344... | ||
| 344 | mask += (1 << n); |
344 | mask += (1 << n); |
| 345 | } |
345 | } |
| 346 | 346 | ||
| 347 | raw_bit_value = (~(-raw_value)) & mask; |
347 | raw_bit_value = (~(-raw_value)) & mask; |
| 348 | } |
348 | } |
| 349 | 349 | ||
| 350 | bitset.Write(start_bit, bit_length, raw_bit_value); |
350 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 351 | } |
351 | } |
| 352 | 352 | ||
| 353 | std::string Protocol::DecodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
353 | std::string Protocol::DecodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 354 | { |
354 | { |
| 355 |
|
355 | uint64_t raw_bit_value = bitset.Read(start_bit, bit_length); |
| 356 | 356 | ||
| 357 | //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value)); |
357 | //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value)); |
| 358 | 358 | ||
| 359 | return boost::lexical_cast<std::string>(( |
359 | return boost::lexical_cast<std::string>((uint64_t)raw_bit_value); |
| 360 | } |
360 | } |
| 361 | 361 | ||
| 362 | void Protocol::EncodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
362 | void Protocol::EncodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 363 | { |
363 | { |
| 364 |
|
364 | uint64_t raw_bit_value = boost::lexical_cast<uint64_t>(value); |
| 365 | 365 | ||
| 366 | bitset.Write(start_bit, bit_length, raw_bit_value); |
366 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 367 | } |
367 | } |
| 368 | 368 | ||
| 369 | std::string Protocol::DecodeFloat(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
369 | std::string Protocol::DecodeFloat(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |