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