Rev 1598 | Rev 1600 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1598 | Rev 1599 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 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 | ||
| 26 | #include <boost/lexical_cast.hpp> |
26 | #include <boost/lexical_cast.hpp> |
| 27 | 27 | ||
| 28 | namespace atom { |
28 | namespace atom { |
| 29 | namespace can { |
29 | namespace can { |
| 30 | 30 | ||
| 31 | Protocol::Pointer Protocol::instance_ |
31 | Protocol::Pointer Protocol::instance_; |
| 32 | 32 | ||
| 33 | Protocol::Protocol() : LOG("can::Protocol") |
33 | Protocol::Protocol() : LOG("can::Protocol") |
| 34 | { |
34 | { |
| 35 | } |
35 | } |
| 36 | 36 | ||
| 37 | Protocol::~Protocol() |
37 | Protocol::~Protocol() |
| 38 | { |
38 | { |
| - | 39 | } |
|
| - | 40 | ||
| - | 41 | void Protocol::Create() |
|
| - | 42 | { |
|
| - | 43 | Protocol::instance_ = Protocol::Pointer(new Protocol()); |
|
| 39 | } |
44 | } |
| 40 | 45 | ||
| 41 | Protocol::Pointer Protocol::Instance() |
46 | Protocol::Pointer Protocol::Instance() |
| 42 | { |
47 | { |
| 43 | return Protocol::instance_; |
48 | return Protocol::instance_; |
| 44 | } |
49 | } |
| 45 | 50 | ||
| 46 | void Protocol::Delete() |
51 | void Protocol::Delete() |
| 47 | { |
52 | { |
| 48 | Protocol::instance_.reset(); |
53 | Protocol::instance_.reset(); |
| 49 | } |
54 | } |
| 50 | 55 | ||
| 51 | bool Protocol::Load(std::string filename) |
56 | bool Protocol::Load(std::string filename) |
| 52 | { |
57 | { |
| 53 | try |
58 | try |
| 54 | { |
59 | { |
| 55 | this->root_node_.LoadFile(filename); |
60 | this->root_node_.LoadFile(filename); |
| 56 | } |
61 | } |
| 57 | catch (std::runtime_error& e) |
62 | catch (std::runtime_error& e) |
| 58 | { |
63 | { |
| 59 | LOG.Error(e.what()); |
64 | LOG.Error(e.what()); |
| 60 | return false; |
65 | return false; |
| 61 | } |
66 | } |
| 62 | 67 | ||
| 63 | LOG.Info("Protocol loaded successfully."); |
68 | LOG.Info("Protocol loaded successfully."); |
| 64 | return true; |
69 | return true; |
| 65 | } |
70 | } |
| 66 | 71 | ||
| 67 | std::string Protocol::LookupClassName(unsigned int class_id) |
72 | std::string Protocol::LookupClassName(unsigned int class_id) |
| 68 | { |
73 | { |
| 69 | try |
74 | try |
| 70 | { |
75 | { |
| 71 | return this->root_node_.FindChild("classes").SelectChild("id", boost::lexical_cast<std::string>(class_id)).GetAttributeValue("name"); |
76 | return this->root_node_.FindChild("classes").SelectChild("id", boost::lexical_cast<std::string>(class_id)).GetAttributeValue("name"); |
| 72 | } |
77 | } |
| 73 | catch (std::runtime_error& e) |
78 | catch (std::runtime_error& e) |
| 74 | { |
79 | { |
| 75 | LOG.Error(e.what()); |
80 | LOG.Error(e.what()); |
| 76 | throw std::runtime_error("No such class_id found, " + boost::lexical_cast<std::string>(class_id)); |
81 | throw std::runtime_error("No such class_id found, " + boost::lexical_cast<std::string>(class_id)); |
| 77 | } |
82 | } |
| 78 | } |
83 | } |
| 79 | 84 | ||
| 80 | unsigned int Protocol::ResolveClassId(std::string class_name) |
85 | unsigned int Protocol::ResolveClassId(std::string class_name) |
| 81 | { |
86 | { |
| 82 | try |
87 | try |
| 83 | { |
88 | { |
| 84 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("classes").SelectChild("name", class_name).GetAttributeValue("id")); |
89 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("classes").SelectChild("name", class_name).GetAttributeValue("id")); |
| 85 | } |
90 | } |
| 86 | catch (std::runtime_error& e) |
91 | catch (std::runtime_error& e) |
| 87 | { |
92 | { |
| 88 | LOG.Error(e.what()); |
93 | LOG.Error(e.what()); |
| 89 | throw std::runtime_error("No such class_name found, " + class_name); |
94 | throw std::runtime_error("No such class_name found, " + class_name); |
| 90 | } |
95 | } |
| 91 | } |
96 | } |
| 92 | 97 | ||
| 93 | std::string Protocol::LookupCommandName(unsigned int command_id, std::string module_name) |
98 | std::string Protocol::LookupCommandName(unsigned int command_id, std::string module_name) |
| 94 | { |
99 | { |
| 95 | try |
100 | try |
| 96 | { |
101 | { |
| 97 | if (command_id >= 128) |
102 | if (command_id >= 128) |
| 98 | { |
103 | { |
| 99 | return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id), "module", module_name).GetAttributeValue("name"); |
104 | return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id), "module", module_name).GetAttributeValue("name"); |
| 100 | } |
105 | } |
| 101 | 106 | ||
| 102 | return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
107 | return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
| 103 | } |
108 | } |
| 104 | catch (std::runtime_error& e) |
109 | catch (std::runtime_error& e) |
| 105 | { |
110 | { |
| 106 | LOG.Error(e.what()); |
111 | LOG.Error(e.what()); |
| 107 | throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name); |
112 | throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name); |
| 108 | } |
113 | } |
| 109 | } |
114 | } |
| 110 | 115 | ||
| 111 | unsigned int Protocol::ResolveCommandId(std::string command_name, std::string module_name) |
116 | unsigned int Protocol::ResolveCommandId(std::string command_name, std::string module_name) |
| 112 | { |
117 | { |
| 113 | try |
118 | try |
| 114 | { |
119 | { |
| 115 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).GetAttributeValue("id")); |
120 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).GetAttributeValue("id")); |
| 116 | } |
121 | } |
| 117 | catch (std::runtime_error& e) |
122 | catch (std::runtime_error& e) |
| 118 | { |
123 | { |
| 119 | } |
124 | } |
| 120 | 125 | ||
| 121 | try |
126 | try |
| 122 | { |
127 | { |
| 123 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name).GetAttributeValue("id")); |
128 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name).GetAttributeValue("id")); |
| 124 | } |
129 | } |
| 125 | catch (std::runtime_error& e) |
130 | catch (std::runtime_error& e) |
| 126 | { |
131 | { |
| 127 | LOG.Error(e.what()); |
132 | LOG.Error(e.what()); |
| 128 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
133 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
| 129 | } |
134 | } |
| 130 | } |
135 | } |
| 131 | 136 | ||
| 132 | std::string Protocol::LookupNMTCommandName(unsigned int command_id) |
137 | std::string Protocol::LookupNMTCommandName(unsigned int command_id) |
| 133 | { |
138 | { |
| 134 | try |
139 | try |
| 135 | { |
140 | { |
| 136 | return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
141 | return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
| 137 | } |
142 | } |
| 138 | catch (std::runtime_error& e) |
143 | catch (std::runtime_error& e) |
| 139 | { |
144 | { |
| 140 | LOG.Error(e.what()); |
145 | LOG.Error(e.what()); |
| 141 | throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id)); |
146 | throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id)); |
| 142 | } |
147 | } |
| 143 | } |
148 | } |
| 144 | 149 | ||
| 145 | unsigned int Protocol::ResolveNMTCommandId(std::string command_name) |
150 | unsigned int Protocol::ResolveNMTCommandId(std::string command_name) |
| 146 | { |
151 | { |
| 147 | try |
152 | try |
| 148 | { |
153 | { |
| 149 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id")); |
154 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id")); |
| 150 | } |
155 | } |
| 151 | catch (std::runtime_error& e) |
156 | catch (std::runtime_error& e) |
| 152 | { |
157 | { |
| 153 | LOG.Error(e.what()); |
158 | LOG.Error(e.what()); |
| 154 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
159 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
| 155 | } |
160 | } |
| 156 | } |
161 | } |
| 157 | 162 | ||
| 158 | std::string Protocol::GetDefineId(std::string name, std::string group) |
163 | std::string Protocol::GetDefineId(std::string name, std::string group) |
| 159 | { |
164 | { |
| 160 | try |
165 | try |
| 161 | { |
166 | { |
| 162 | return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id"); |
167 | return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id"); |
| 163 | } |
168 | } |
| 164 | catch (std::runtime_error& e) |
169 | catch (std::runtime_error& e) |
| 165 | { |
170 | { |
| 166 | LOG.Error(e.what()); |
171 | LOG.Error(e.what()); |
| 167 | throw std::runtime_error("No such define found, name = " + name + ", group = " + group); |
172 | throw std::runtime_error("No such define found, name = " + name + ", group = " + group); |
| 168 | } |
173 | } |
| 169 | } |
174 | } |
| 170 | 175 | ||
| 171 | std::string Protocol::LookupDirectionFlag(unsigned int direction_flag) |
176 | std::string Protocol::LookupDirectionFlag(unsigned int direction_flag) |
| 172 | { |
177 | { |
| 173 | try |
178 | try |
| 174 | { |
179 | { |
| 175 | return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name"); |
180 | return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name"); |
| 176 | } |
181 | } |
| 177 | catch (std::runtime_error& e) |
182 | catch (std::runtime_error& e) |
| 178 | { |
183 | { |
| 179 | LOG.Error(e.what()); |
184 | LOG.Error(e.what()); |
| 180 | throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag)); |
185 | throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag)); |
| 181 | } |
186 | } |
| 182 | } |
187 | } |
| 183 | 188 | ||
| 184 | unsigned int Protocol::ResolveDirectionFlag(std::string direction_name) |
189 | unsigned int Protocol::ResolveDirectionFlag(std::string direction_name) |
| 185 | { |
190 | { |
| 186 | try |
191 | try |
| 187 | { |
192 | { |
| 188 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id")); |
193 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id")); |
| 189 | } |
194 | } |
| 190 | catch (std::runtime_error& e) |
195 | catch (std::runtime_error& e) |
| 191 | { |
196 | { |
| 192 | LOG.Error(e.what()); |
197 | LOG.Error(e.what()); |
| 193 | throw std::runtime_error("No such direction flag found, direction_name = " + direction_name); |
198 | throw std::runtime_error("No such direction flag found, direction_name = " + direction_name); |
| 194 | } |
199 | } |
| 195 | } |
200 | } |
| 196 | 201 | ||
| 197 | std::string Protocol::LookupModuleName(unsigned int module_id) |
202 | std::string Protocol::LookupModuleName(unsigned int module_id) |
| 198 | { |
203 | { |
| 199 | if (module_id == 0) |
204 | if (module_id == 0) |
| 200 | { |
205 | { |
| 201 | return ""; |
206 | return ""; |
| 202 | } |
207 | } |
| 203 | 208 | ||
| 204 | try |
209 | try |
| 205 | { |
210 | { |
| 206 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
211 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
| 207 | } |
212 | } |
| 208 | catch (std::runtime_error& e) |
213 | catch (std::runtime_error& e) |
| 209 | { |
214 | { |
| 210 | LOG.Error(e.what()); |
215 | LOG.Error(e.what()); |
| 211 | throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id)); |
216 | throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id)); |
| 212 | } |
217 | } |
| 213 | } |
218 | } |
| 214 | 219 | ||
| 215 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
220 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
| 216 | { |
221 | { |
| 217 | if (module_name == "") |
222 | if (module_name == "") |
| 218 | { |
223 | { |
| 219 | return 0; |
224 | return 0; |
| 220 | } |
225 | } |
| 221 | 226 | ||
| 222 | try |
227 | try |
| 223 | { |
228 | { |
| 224 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
229 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
| 225 | } |
230 | } |
| 226 | catch (std::runtime_error& e) |
231 | catch (std::runtime_error& e) |
| 227 | { |
232 | { |
| 228 | LOG.Error(e.what()); |
233 | LOG.Error(e.what()); |
| 229 | throw std::runtime_error("No such module name found, " + module_name); |
234 | throw std::runtime_error("No such module name found, " + module_name); |
| 230 | } |
235 | } |
| 231 | } |
236 | } |
| 232 | 237 | ||
| 233 | xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name) |
238 | xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name) |
| 234 | { |
239 | { |
| 235 | try |
240 | try |
| 236 | { |
241 | { |
| 237 | return this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).FindChild("variables").GetChildren(); |
242 | return this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).FindChild("variables").GetChildren(); |
| 238 | } |
243 | } |
| 239 | catch (std::runtime_error& e) |
244 | catch (std::runtime_error& e) |
| 240 | { |
245 | { |
| 241 | } |
246 | } |
| 242 | 247 | ||
| 243 | try |
248 | try |
| 244 | { |
249 | { |
| 245 | return this->root_node_.FindChild("commands").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
250 | return this->root_node_.FindChild("commands").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
| 246 | } |
251 | } |
| 247 | catch (std::runtime_error& e) |
252 | catch (std::runtime_error& e) |
| 248 | { |
253 | { |
| 249 | LOG.Error(e.what()); |
254 | LOG.Error(e.what()); |
| 250 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
255 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
| 251 | } |
256 | } |
| 252 | } |
257 | } |
| 253 | 258 | ||
| 254 | xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name) |
259 | xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name) |
| 255 | { |
260 | { |
| 256 | try |
261 | try |
| 257 | { |
262 | { |
| 258 | return this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
263 | return this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
| 259 | } |
264 | } |
| 260 | catch (std::runtime_error& e) |
265 | catch (std::runtime_error& e) |
| 261 | { |
266 | { |
| 262 | LOG.Error(e.what()); |
267 | LOG.Error(e.what()); |
| 263 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
268 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
| 264 | } |
269 | } |
| 265 | } |
270 | } |
| 266 | 271 | ||
| 267 | std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
272 | std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 268 | { |
273 | { |
| 269 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
274 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 270 | int raw_value = 0; |
275 | int raw_value = 0; |
| 271 | 276 | ||
| 272 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
277 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
| 273 | 278 | ||
| 274 | return boost::lexical_cast<std::string>(raw_value); |
279 | return boost::lexical_cast<std::string>(raw_value); |
| 275 | } |
280 | } |
| 276 | 281 | ||
| 277 | void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
282 | void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 278 | { |
283 | { |
| 279 | unsigned long raw_bit_value = 0; |
284 | unsigned long raw_bit_value = 0; |
| 280 | int raw_value = boost::lexical_cast<int>(value); |
285 | int raw_value = boost::lexical_cast<int>(value); |
| 281 | 286 | ||
| 282 | memcpy(&raw_bit_value, &raw_value, sizeof(raw_value)); |
287 | memcpy(&raw_bit_value, &raw_value, sizeof(raw_value)); |
| 283 | 288 | ||
| 284 | bitset.Write(start_bit, bit_length, raw_bit_value); |
289 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 285 | } |
290 | } |
| 286 | 291 | ||
| 287 | std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
292 | std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 288 | { |
293 | { |
| 289 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
294 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 290 | 295 | ||
| 291 | return boost::lexical_cast<std::string>((unsigned int)raw_bit_value); |
296 | return boost::lexical_cast<std::string>((unsigned int)raw_bit_value); |
| 292 | } |
297 | } |
| 293 | 298 | ||
| 294 | void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
299 | void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 295 | { |
300 | { |
| 296 | unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value); |
301 | unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value); |
| 297 | 302 | ||
| 298 | bitset.Write(start_bit, bit_length, raw_bit_value); |
303 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 299 | } |
304 | } |
| 300 | 305 | ||
| 301 | std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
306 | std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 302 | { |
307 | { |
| 303 | float raw_value = 0; |
308 | float raw_value = 0; |
| 304 | bool negative = false;; |
309 | bool negative = false;; |
| 305 | 310 | ||
| 306 | if (bitset.Get(start_bit)) |
311 | if (bitset.Get(start_bit)) |
| 307 | { |
312 | { |
| 308 | negative = true; |
313 | negative = true; |
| 309 | } |
314 | } |
| 310 | 315 | ||
| 311 | for (int c = bit_length-1; c > 0; c--) |
316 | for (int c = bit_length - 1; c > 0; c--) |
| 312 | { |
317 | { |
| 313 | if (bitset.Get(start_bit + c) != negative) |
318 | if (bitset.Get(start_bit + c) != negative) |
| 314 | { |
319 | { |
| 315 | raw_value += pow(2.0f, (int)(bit_length-1-c)); |
320 | raw_value += pow(2.0f, (int)(bit_length - 1 - c)); |
| 316 | } |
321 | } |
| 317 | } |
322 | } |
| 318 | 323 | ||
| 319 | raw_value /= 64.0f; |
324 | raw_value /= 64.0f; |
| 320 | 325 | ||
| 321 | if (negative) |
326 | if (negative) |
| 322 | { |
327 | { |
| 323 | raw_value = -raw_value; |
328 | raw_value = -raw_value; |
| 324 | } |
329 | } |
| 325 | 330 | ||
| 326 | return boost::lexical_cast<std::string>(raw_value); |
331 | return boost::lexical_cast<std::string>(raw_value); |
| 327 | } |
332 | } |
| 328 | 333 | ||
| 329 | void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
334 | void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 330 | { |
335 | { |
| 331 | // TODO: |
336 | // TODO: |
| - | 337 | LOG.Error("EncodeFloat() is not implemented yet!"); |
|
| 332 | } |
338 | } |
| 333 | 339 | ||
| 334 | std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
340 | std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 335 | { |
341 | { |
| 336 | std::string value; |
342 | std::string value; |
| 337 | char c = 0; |
343 | char c = 0; |
| 338 | 344 | ||
| 339 | for (unsigned int p = 0; p < bit_length; p += 8) |
345 | for (unsigned int p = 0; p < bit_length; p += 8) |
| 340 | { |
346 | { |
| 341 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 8); |
347 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 8); |
| 342 | memcpy(&c, &raw_bit_value, sizeof(c)); |
348 | memcpy(&c, &raw_bit_value, sizeof(c)); |
| 343 | value += c; |
349 | value += c; |
| 344 | } |
350 | } |
| 345 | 351 | ||
| 346 | return value; |
352 | return value; |
| 347 | } |
353 | } |
| 348 | 354 | ||
| 349 | void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
355 | void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 350 | { |
356 | { |
| 351 | // TODO |
357 | // TODO |
| - | 358 | LOG.Error("EncodeAscii() is not implemented yet!"); |
|
| 352 | } |
359 | } |
| 353 | 360 | ||
| 354 | std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
361 | std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 355 | { |
362 | { |
| 356 | std::string value; |
363 | std::string value; |
| 357 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
364 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 358 | 365 | ||
| 359 | for (unsigned int p = 0; p < bit_length; p += 4) |
366 | for (unsigned int p = 0; p < bit_length; p += 4) |
| 360 | { |
367 | { |
| 361 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 4); |
368 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 4); |
| 362 | 369 | ||
| 363 | if (raw_bit_value >= sizeof(hex)) |
370 | if (raw_bit_value >= sizeof(hex)) |
| 364 | { |
371 | { |
| 365 | value += '-'; |
372 | value += '-'; |
| 366 | } |
373 | } |
| 367 | else |
374 | else |
| 368 | { |
375 | { |
| 369 | value += hex[raw_bit_value]; |
376 | value += hex[raw_bit_value]; |
| 370 | } |
377 | } |
| 371 | } |
378 | } |
| 372 | 379 | ||
| 373 | return value; |
380 | return value; |
| 374 | } |
381 | } |
| 375 | 382 | ||
| 376 | void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
383 | void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 377 | { |
384 | { |
| 378 | // TODO: |
385 | // TODO: |
| - | 386 | LOG.Error("EncodeHexstring() is not implemented yet!"); |
|
| 379 | } |
387 | } |
| 380 | 388 | ||
| 381 | }; // namespace can |
389 | }; // namespace can |
| 382 | }; // namespace atom |
390 | }; // namespace atom |
| 383 | 391 | ||