Procházet zdrojové kódy

* new Packet::writeTo(), readFrom() methods. (for saving snapshots of packets)

Scott Powell před 1 rokem
rodič
revize
78e307c495
2 změnil soubory, kde provedl 34 přidání a 0 odebrání
  1. 20 0
      src/Packet.cpp
  2. 14 0
      src/Packet.h

+ 20 - 0
src/Packet.cpp

@@ -19,5 +19,25 @@ void Packet::calculatePacketHash(uint8_t* hash) const {
   sha.finalize(hash, MAX_HASH_SIZE);
 }
 
+uint8_t Packet::writeTo(uint8_t dest[]) const {
+  uint8_t i = 0;
+  dest[i++] = header;
+  dest[i++] = path_len;
+  memcpy(&dest[i], path, path_len); i += path_len;
+  memcpy(&dest[i], payload, payload_len); i += payload_len;
+  return i;
+}
+
+bool Packet::readFrom(const uint8_t src[], uint8_t len) {
+  uint8_t i = 0;
+  header = src[i++];
+  path_len = src[i++];
+  if (path_len > sizeof(path)) return false;   // bad encoding
+  memcpy(path, &src[i], path_len); i += path_len;
+  if (i >= len) return false;   // bad encoding
+  payload_len = len - i;
+  memcpy(payload, &src[i], payload_len); //i += payload_len;
+  return true;   // success
+}
 
 }

+ 14 - 0
src/Packet.h

@@ -71,6 +71,20 @@ public:
 
   void markDoNotRetransmit() { header = 0xFF; }
   bool isMarkedDoNotRetransmit() const { return header == 0xFF; }
+
+  /**
+   * \brief  save entire packet as a blob
+   * \param dest  (OUT) destination buffer (assumed to be MAX_MTU_SIZE)
+   * \returns  the packet length
+   */
+  uint8_t writeTo(uint8_t dest[]) const;
+
+  /**
+   * \brief  restore this packet from a blob (as created using writeTo())
+   * \param  src  (IN) buffer containing blob
+   * \param  len  the packet length (as returned by writeTo())
+   */
+  bool readFrom(const uint8_t src[], uint8_t len);
 };
 
 }