Rev 986 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 986 | Rev 991 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | * Copyright (C) November 28, 2008 by Mattias Runge * |
2 | * Copyright (C) November 28, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * canmessage.cpp * |
4 | * canmessage.cpp * |
| 5 | * * |
5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
9 | * (at your option) any later version. * |
| 10 | * * |
10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
14 | * GNU General Public License for more details. * |
| 15 | * * |
15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | 21 | ||
| 22 | #include <string> |
22 | #include <string> |
| 23 | #include <iostream> |
23 | #include <iostream> |
| 24 | #include "canidtranslator.h" |
24 | #include "canidtranslator.h" |
| 25 | #include "canmessage.h" |
25 | #include "canmessage.h" |
| 26 | 26 | ||
| 27 | void CanMessage::setRaw(string rawHex) |
27 | void CanMessage::setRaw(string rawHex) |
| 28 | { |
28 | { |
| 29 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
29 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
| 30 | //SyslogStream &slog = SyslogStream::getInstance(); |
30 | //SyslogStream &slog = SyslogStream::getInstance(); |
| 31 | 31 | ||
| 32 | //PKT 00060102 1 0 00 00 c4 |
32 | //PKT 00060102 1 0 00 00 c4 |
| 33 | 33 | ||
| 34 | rawHex = trim(rawHex, '\n'); |
34 | rawHex = trim(rawHex, '\n'); |
| 35 | rawHex = trim(rawHex); |
35 | rawHex = trim(rawHex); |
| 36 | 36 | ||
| 37 | vector<string> parts = explode(" ", rawHex); |
37 | vector<string> parts = explode(" ", rawHex); |
| 38 | 38 | ||
| 39 | if (parts.size() < 2 || parts[0].compare("PKT") != 0) |
39 | if (parts.size() < 2 || parts[0].compare("PKT") != 0) |
| 40 | { |
40 | { |
| 41 | //slog << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n"; |
41 | //slog << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n"; |
| 42 | throw new CanMessageException("Malformed can message received from canDaemon"); |
42 | throw new CanMessageException("Malformed can message received from canDaemon"); |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | string rawHeaderBin = hex2bin(parts[1]); |
45 | string rawHeaderBin = hex2bin(parts[1]); |
| 46 | 46 | ||
| 47 | myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4))); |
47 | myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4))); |
| 48 | 48 | ||
| 49 | if (myClassName == "") |
49 | if (myClassName == "") |
| 50 | { |
50 | { |
| 51 | myIsUnknown = true; |
51 | myIsUnknown = true; |
| 52 | return; |
52 | return; |
| 53 | } |
53 | } |
| 54 | 54 | ||
| 55 | string rawHexData = ""; |
55 | string rawHexData = ""; |
| 56 | for (int n = 4; n < parts.size(); n++) |
56 | for (int n = 4; n < parts.size(); n++) |
| 57 | { |
57 | { |
| 58 | rawHexData += parts[n]; |
58 | rawHexData += parts[n]; |
| 59 | } |
59 | } |
| 60 | 60 | ||
| 61 | if (myClassName == "nmt") |
61 | if (myClassName == "nmt") |
| 62 | { |
62 | { |
| 63 | int commandId = bin2uint(rawHeaderBin.substr(8, 8)); |
63 | int commandId = bin2uint(rawHeaderBin.substr(8, 8)); |
| 64 | myCommandName = translator.lookupNMTCommandName(commandId); |
64 | myCommandName = translator.lookupNMTCommandName(commandId); |
| 65 | 65 | ||
| 66 | myData = translator.translateNMTData(commandId, rawHexData); |
66 | myData = translator.translateNMTData(commandId, rawHexData); |
| 67 | } |
67 | } |
| 68 | else |
68 | else |
| 69 | { |
69 | { |
| 70 | myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1))); |
70 | myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1))); |
| 71 | myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8))); |
71 | myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8))); |
| 72 | 72 | ||
| 73 | myModuleId = bin2uint(rawHeaderBin.substr(16, 8)); |
73 | myModuleId = bin2uint(rawHeaderBin.substr(16, 8)); |
| 74 | 74 | ||
| 75 | int commandId = bin2uint(rawHeaderBin.substr(24, 8)); |
75 | int commandId = bin2uint(rawHeaderBin.substr(24, 8)); |
| 76 | myCommandName = translator.lookupCommandName(commandId, myModuleName); |
76 | myCommandName = translator.lookupCommandName(commandId, myModuleName); |
| 77 | 77 | ||
| 78 | myData = translator.translateData(commandId, myModuleName, rawHexData); |
78 | myData = translator.translateData(commandId, myModuleName, rawHexData); |
| 79 | } |
79 | } |
| 80 | } |
80 | } |
| 81 | 81 | ||
| 82 | string CanMessage::getRaw() |
82 | string CanMessage::getRaw() |
| 83 | { |
83 | { |
| 84 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
84 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
| 85 | 85 | ||
| 86 | string dataHex; |
86 | string dataHex; |
| 87 | 87 | ||
| 88 | string rawHex = "PKT "; |
88 | string rawHex = "PKT "; |
| 89 | 89 | ||
| 90 | string bin = "000"; |
90 | string bin = "000"; |
| 91 | 91 | ||
| 92 | int classId = translator.resolveClassId(myClassName); |
92 | int classId = translator.resolveClassId(myClassName); |
| 93 | if (classId == -1) |
93 | if (classId == -1) |
| 94 | bin += "0000"; |
94 | bin += "0000"; |
| 95 | else |
95 | else |
| 96 | bin += uint2bin(classId, 4); |
96 | bin += uint2bin(classId, 4); |
| 97 | 97 | ||
| 98 | if (myClassName == "nmt") |
98 | if (myClassName == "nmt") |
| 99 | { |
99 | { |
| 100 | bin +="0"; |
100 | bin +="0"; |
| 101 | 101 | ||
| 102 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
102 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
| 103 | if (commandNameId == -1) |
103 | if (commandNameId == -1) |
| 104 | bin += "00000000"; |
104 | bin += "00000000"; |
| 105 | else |
105 | else |
| 106 | bin += uint2bin(commandNameId, 8); |
106 | bin += uint2bin(commandNameId, 8); |
| 107 | 107 | ||
| 108 | bin += "0000000000000000"; |
108 | bin += "0000000000000000"; |
| 109 | 109 | ||
| 110 | dataHex = translator.translateNMTDataToHex(commandNameId, myData); |
110 | dataHex = translator.translateNMTDataToHex(commandNameId, myData); |
| 111 | } |
111 | } |
| 112 | else |
112 | else |
| 113 | { |
113 | { |
| 114 | bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1); |
114 | bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1); |
| 115 | 115 | ||
| 116 | 116 | ||
| 117 | int moduleTypeId = translator.resolveModuleId(myModuleName); |
117 | int moduleTypeId = translator.resolveModuleId(myModuleName); |
| 118 | if (moduleTypeId == -1) |
118 | if (moduleTypeId == -1) |
| 119 | bin += "00000000"; |
119 | bin += "00000000"; |
| 120 | else |
120 | else |
| 121 | bin += uint2bin(moduleTypeId, 8); |
121 | bin += uint2bin(moduleTypeId, 8); |
| 122 | 122 | ||
| 123 | 123 | ||
| 124 | int moduleId = myModuleId; |
124 | int moduleId = myModuleId; |
| 125 | bin += uint2bin(moduleId, 8); |
125 | bin += uint2bin(moduleId, 8); |
| 126 | 126 | ||
| 127 | 127 | ||
| 128 | int commandNameId = translator.resolveCommandId(myCommandName, myModuleName); |
128 | int commandNameId = translator.resolveCommandId(myCommandName, myModuleName); |
| 129 | if (commandNameId == -1) |
129 | if (commandNameId == -1) |
| 130 | bin += "00000000"; |
130 | bin += "00000000"; |
| 131 | else |
131 | else |
| 132 | bin += uint2bin(commandNameId, 8); |
132 | bin += uint2bin(commandNameId, 8); |
| 133 | 133 | ||
| 134 | dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData); |
134 | dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData); |
| 135 | } |
135 | } |
| 136 | 136 | ||
| 137 | rawHex += bin2hex(bin); |
137 | rawHex += bin2hex(bin); |
| 138 | 138 | ||
| 139 | rawHex += " 1 0"; |
139 | rawHex += " 1 0"; |
| 140 | 140 | ||
| 141 | while (dataHex.size() > 0) |
141 | while (dataHex.size() > 0) |
| 142 | { |
142 | { |
| 143 | string hex = dataHex.substr(0, 2); |
143 | string hex = dataHex.substr(0, 2); |
| 144 | dataHex.erase(0, 2); |
144 | dataHex.erase(0, 2); |
| 145 | rawHex += " " + hex; |
145 | rawHex += " " + hex; |
| 146 | } |
146 | } |
| 147 | 147 | ||
| 148 | rawHex += "\n"; |
148 | rawHex += "\n"; |
| 149 | 149 | ||
| 150 | return rawHex; |
150 | return rawHex; |
| - | 151 | } |
|
| - | 152 | ||
| - | 153 | void CanMessage::setData(map<string, CanVariable> data) |
|
| - | 154 | { |
|
| - | 155 | myData = data; |
|
| - | 156 | ||
| - | 157 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
|
| - | 158 | ||
| - | 159 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
|
| - | 160 | ||
| - | 161 | if (myClassName == "nmt") |
|
| - | 162 | { |
|
| - | 163 | translator.makeNMTDataValid(commandNameId, data); |
|
| - | 164 | } |
|
| - | 165 | else |
|
| - | 166 | { |
|
| - | 167 | translator.makeDataValid(commandNameId, myModuleName, data); |
|
| - | 168 | } |
|
| 151 | } |
169 | } |
| 152 | 170 | ||
| 153 | string CanMessage::getJSONData() |
171 | string CanMessage::getJSONData() |
| 154 | { |
172 | { |
| 155 | string json = "{"; |
173 | string json = "{"; |
| 156 | 174 | ||
| 157 | map<string, CanVariable>::iterator iter; |
175 | map<string, CanVariable>::iterator iter; |
| 158 | 176 | ||
| 159 | for (iter = myData.begin(); iter != myData.end(); iter++) |
177 | for (iter = myData.begin(); iter != myData.end(); iter++) |
| 160 | { |
178 | { |
| 161 | json += " " + iter->first + " : "; |
179 | json += " " + iter->first + " : "; |
| 162 | 180 | ||
| 163 | if (iter->second.getType() == "uint") |
181 | if (iter->second.getType() == "uint") |
| 164 | { |
182 | { |
| 165 | json += iter->second.getValue(); |
183 | json += iter->second.getValue(); |
| 166 | } |
184 | } |
| 167 | else if (iter->second.getType() == "float") |
185 | else if (iter->second.getType() == "float") |
| 168 | { |
186 | { |
| 169 | json += iter->second.getValue(); |
187 | json += iter->second.getValue(); |
| 170 | } |
188 | } |
| 171 | else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring") |
189 | else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring") |
| 172 | { |
190 | { |
| 173 | json += "'" + escape(iter->second.getValue()) + "'"; |
191 | json += "'" + escape(iter->second.getValue()) + "'"; |
| 174 | } |
192 | } |
| 175 | 193 | ||
| 176 | json += ","; |
194 | json += ","; |
| 177 | } |
195 | } |
| 178 | 196 | ||
| 179 | json = trim(json, ','); |
197 | json = trim(json, ','); |
| 180 | 198 | ||
| 181 | return json + " }"; |
199 | return json + " }"; |
| 182 | } |
200 | } |
| 183 | 201 | ||