| Kevin Rocard | ef8d727 | 2014-04-23 20:34:14 +0200 | [diff] [blame] | 1 | /* |
| David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 2 | * Copyright (c) 2011-2014, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | * list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation and/or |
| 13 | * other materials provided with the distribution. |
| 14 | * |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software without |
| 17 | * specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "Message.h" |
| 31 | #include <assert.h> |
| 32 | #include "Socket.h" |
| 33 | #include "RemoteProcessorProtocol.h" |
| 34 | #include <string.h> |
| 35 | #include <assert.h> |
| Kevin Rocard | ef8d727 | 2014-04-23 20:34:14 +0200 | [diff] [blame] | 36 | #include <errno.h> |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 37 | |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 38 | using std::string; |
| 39 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 40 | CMessage::CMessage(uint8_t ucMsgId) : _ucMsgId(ucMsgId), _pucData(NULL), _uiDataSize(0), _uiIndex(0) |
| 41 | { |
| 42 | } |
| 43 | |
| Patrick Benavoli | 930075c | 2011-08-31 17:09:06 +0200 | [diff] [blame] | 44 | CMessage::CMessage() : _ucMsgId((uint8_t)-1), _pucData(NULL), _uiDataSize(0), _uiIndex(0) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | CMessage::~CMessage() |
| 49 | { |
| 50 | delete [] _pucData; |
| 51 | } |
| 52 | |
| 53 | // Msg Id |
| 54 | uint8_t CMessage::getMsgId() const |
| 55 | { |
| 56 | return _ucMsgId; |
| 57 | } |
| 58 | |
| 59 | // Data |
| 60 | void CMessage::writeData(const void* pvData, uint32_t uiSize) |
| 61 | { |
| 62 | assert(_uiIndex + uiSize <= _uiDataSize); |
| 63 | |
| 64 | // Copy |
| 65 | memcpy(&_pucData[_uiIndex], pvData, uiSize); |
| 66 | |
| 67 | // Index |
| 68 | _uiIndex += uiSize; |
| 69 | } |
| 70 | |
| 71 | void CMessage::readData(void* pvData, uint32_t uiSize) |
| 72 | { |
| 73 | assert(_uiIndex + uiSize <= _uiDataSize); |
| 74 | |
| 75 | // Copy |
| 76 | memcpy(pvData, &_pucData[_uiIndex], uiSize); |
| 77 | |
| 78 | // Index |
| 79 | _uiIndex += uiSize; |
| 80 | } |
| 81 | |
| 82 | void CMessage::writeString(const string& strData) |
| 83 | { |
| 84 | // Size |
| 85 | uint32_t uiSize = strData.length(); |
| 86 | |
| 87 | writeData(&uiSize, sizeof(uiSize)); |
| 88 | |
| 89 | // Content |
| 90 | writeData(strData.c_str(), uiSize); |
| 91 | } |
| 92 | |
| 93 | void CMessage::readString(string& strData) |
| 94 | { |
| 95 | // Size |
| 96 | uint32_t uiSize; |
| 97 | |
| 98 | readData(&uiSize, sizeof(uiSize)); |
| 99 | |
| 100 | // Data |
| Kevin Rocard | 0a7b18e | 2014-12-19 15:11:21 +0100 | [diff] [blame] | 101 | char pcData[uiSize + 1]; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 102 | |
| 103 | // Content |
| 104 | readData(pcData, uiSize); |
| 105 | |
| 106 | // NULL-terminate string |
| 107 | pcData[uiSize] = '\0'; |
| 108 | |
| 109 | // Output |
| 110 | strData = pcData; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | uint32_t CMessage::getStringSize(const string& strData) const |
| 114 | { |
| 115 | // Return string length plus room to store its length |
| 116 | return strData.length() + sizeof(uint32_t); |
| 117 | } |
| 118 | |
| 119 | // Remaining data size |
| 120 | uint32_t CMessage::getRemainingDataSize() const |
| 121 | { |
| 122 | return _uiDataSize - _uiIndex; |
| 123 | } |
| 124 | |
| 125 | // Send/Receive |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 126 | CMessage::Result CMessage::serialize(CSocket* pSocket, bool bOut, string& strError) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 127 | { |
| 128 | if (bOut) { |
| 129 | |
| 130 | // Make room for data to send |
| 131 | allocateData(getDataSize()); |
| 132 | |
| 133 | // Get data from derived |
| 134 | fillDataToSend(); |
| 135 | |
| 136 | // Finished providing data? |
| 137 | assert(_uiIndex == _uiDataSize); |
| 138 | |
| 139 | // First send sync word |
| 140 | uint16_t uiSyncWord = SYNC_WORD; |
| 141 | |
| 142 | if (!pSocket->write(&uiSyncWord, sizeof(uiSyncWord))) { |
| 143 | |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 144 | if (pSocket->hasPeerDisconnected()) { |
| 145 | return peerDisconnected; |
| 146 | } |
| 147 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Size |
| 151 | uint32_t uiSize = sizeof(_ucMsgId) + _uiDataSize; |
| 152 | |
| 153 | if (!pSocket->write(&uiSize, sizeof(uiSize))) { |
| 154 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 155 | strError += string("Size write failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 156 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Msg Id |
| 160 | if (!pSocket->write(&_ucMsgId, sizeof(_ucMsgId))) { |
| 161 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 162 | strError += string("Msg write failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 163 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Data |
| 167 | if (!pSocket->write(_pucData, _uiDataSize)) { |
| 168 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 169 | strError = string("Data write failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 170 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Checksum |
| 174 | uint8_t ucChecksum = computeChecksum(); |
| 175 | |
| 176 | if (!pSocket->write(&ucChecksum, sizeof(ucChecksum))) { |
| 177 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 178 | strError = string("Checksum write failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 179 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | } else { |
| 183 | // First read sync word |
| 184 | uint16_t uiSyncWord; |
| 185 | |
| 186 | if (!pSocket->read(&uiSyncWord, sizeof(uiSyncWord))) { |
| 187 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 188 | strError = string("Sync read failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 189 | if (pSocket->hasPeerDisconnected()) { |
| 190 | return peerDisconnected; |
| 191 | } |
| 192 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Check Sync word |
| 196 | if (uiSyncWord != SYNC_WORD) { |
| 197 | |
| Kevin Rocard | ef8d727 | 2014-04-23 20:34:14 +0200 | [diff] [blame] | 198 | strError = "Sync word incorrect"; |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 199 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Size |
| 203 | uint32_t uiSize; |
| 204 | |
| 205 | if (!pSocket->read(&uiSize, sizeof(uiSize))) { |
| 206 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 207 | strError = string("Size read failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 208 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Msg Id |
| 212 | if (!pSocket->read(&_ucMsgId, sizeof(_ucMsgId))) { |
| 213 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 214 | strError = string("Msg id read failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 215 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Data |
| 219 | |
| 220 | // Allocate |
| 221 | allocateData(uiSize - sizeof(_ucMsgId)); |
| 222 | |
| 223 | // Data receive |
| 224 | if (!pSocket->read(_pucData, _uiDataSize)) { |
| 225 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 226 | strError = string("Data read failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 227 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Checksum |
| 231 | uint8_t ucChecksum; |
| 232 | |
| 233 | if (!pSocket->read(&ucChecksum, sizeof(ucChecksum))) { |
| 234 | |
| Kevin Rocard | e3a06ba | 2014-10-01 10:41:24 +0200 | [diff] [blame] | 235 | strError = string("Checksum read failed: ") + strerror(errno); |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 236 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 237 | } |
| 238 | // Compare |
| 239 | if (ucChecksum != computeChecksum()) { |
| 240 | |
| Kevin Rocard | ef8d727 | 2014-04-23 20:34:14 +0200 | [diff] [blame] | 241 | strError = "Received checksum != computed checksum"; |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 242 | return error; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Collect data in derived |
| 246 | collectReceivedData(); |
| 247 | } |
| 248 | |
| Kevin Rocard | e874c25 | 2014-04-25 15:11:12 +0200 | [diff] [blame] | 249 | return success; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Checksum |
| 253 | uint8_t CMessage::computeChecksum() const |
| 254 | { |
| Patrick Benavoli | 1387bda | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 255 | uint8_t uiChecksum = _ucMsgId; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 256 | |
| 257 | uint32_t uiIndex; |
| 258 | |
| 259 | for (uiIndex = 0; uiIndex < _uiDataSize; uiIndex++) { |
| 260 | |
| 261 | uiChecksum += _pucData[uiIndex]; |
| 262 | } |
| 263 | |
| 264 | return uiChecksum; |
| 265 | } |
| 266 | |
| 267 | // Data allocation |
| 268 | void CMessage::allocateData(uint32_t uiSize) |
| 269 | { |
| 270 | // Remove previous one |
| 271 | if (_pucData) { |
| 272 | |
| 273 | delete [] _pucData; |
| 274 | } |
| 275 | // Do allocate |
| 276 | _pucData = new uint8_t[uiSize]; |
| 277 | |
| 278 | // Record size |
| 279 | _uiDataSize = uiSize; |
| 280 | |
| 281 | // Reset Index |
| 282 | _uiIndex = 0; |
| 283 | } |