Advertisement
lukicdarkoo

PLLSP Backup

Jan 30th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const EventEmitter = require('events');
  2.  
  3. /**
  4.  * Packetized Low-Level Secured Protocol
  5.  *
  6.  * <pre>
  7.  * 0                   1                   2                   3
  8.  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  9.  * +--------------+-------+-------+---------------+----------------+
  10.  * |  Start Byte  | Header|Footer |  Packet type  | Payload length |
  11.  * |    (0x3C)    |    Checksum   |               |                |
  12.  * +-------------------------------- - - - - - - - - - - - - - - - +
  13.  * :                     Payload Data continued ...                :
  14.  * + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  15.  * |                     Payload Data continued ...                |
  16.  * +---------------------------------------------------------------+
  17.  * </pre>
  18.  */
  19.  
  20. class PLLSP extends EventEmitter {
  21.     static get STATE_READ_HEADER() { return 0; }
  22.     static get STATE_READ_PAYLOAD() { return 1; }
  23.     static get STATE_WAIT_START() { return 2; }
  24.  
  25.     constructor(name, config) {
  26.         super();
  27.  
  28.         this.name = name;
  29.         this.config = Object.assign({
  30.             bufferSize: 150,
  31.         }, config);
  32.  
  33.         this._buffer = Buffer.alloc(this.config.bufferSize);
  34.         this._bufferSize = 0;
  35.         this._state = PLLSP.STATE_WAIT_START;
  36.  
  37.         this._headerChecksum = 0;
  38.         this._payloadChecksum = 0;
  39.         this._payloadLength = 0;
  40.         this._packetType = 0;
  41.         this._startByteIndex = 0;
  42.     }
  43.  
  44.     generate(buffer) {
  45.         let packet = Buffer.allocUnsafe(buffer.length + 3);
  46.  
  47.         // Copy payload
  48.         buffer.copy(packet, 4, 1);
  49.  
  50.         // Set start byte
  51.         packet.writeUInt8(0x3C, 0);
  52.  
  53.         // Set checksum
  54.         let headerChecksum = buffer.length - 1 + buffer.readUInt8(0);
  55.         let payloadChecksum = 0;
  56.         for (let i = 1; i < buffer.length; i++) {
  57.             payloadChecksum += buffer.readUInt8(i);
  58.         }
  59.         packet.writeUInt8(((headerChecksum & 0x0F) << 4) | (payloadChecksum & 0x0F), 1);
  60.  
  61.         // Set packet type
  62.         packet.writeUInt8(buffer.readUInt8(0), 2);
  63.  
  64.         // Set length
  65.         packet.writeUInt8(buffer.length - 1, 3);
  66.  
  67.         return packet;
  68.     }
  69.  
  70.     push(chunkBuffer) {
  71.         // Append `tempBuffer` to `buffer`
  72.         if (chunkBuffer !== null) {
  73.             chunkBuffer.copy(this._buffer, this._bufferSize);
  74.             this._bufferSize += chunkBuffer.length;
  75.         }
  76.  
  77.         // Try to find start byte and start pumping a packet with data
  78.         if (this._state === PLLSP.STATE_WAIT_START) {
  79.             this._startByteIndex = this._buffer.indexOf(0x3C);
  80.             if (this._startByteIndex >= 0 && this._startByteIndex < this._bufferSize) {
  81.                 this._state = PLLSP.STATE_READ_HEADER;
  82.             }
  83.         }
  84.  
  85.         // Try to read a header
  86.         if (this._state === PLLSP.STATE_READ_HEADER) {
  87.             if (this._bufferSize - this._startByteIndex >= 4) {
  88.                 // Extract header from packet
  89.                 this._headerChecksum = (this._buffer.readUInt8(this._startByteIndex + 1) & 0xF0) >> 4;
  90.                 this._payloadChecksum = this._buffer.readUInt8(this._startByteIndex + 1) & 0x0F;
  91.                 this._packetType = this._buffer.readUInt8(this._startByteIndex + 2);
  92.                 this._payloadLength = this._buffer.readUInt8(this._startByteIndex + 3);
  93.  
  94.                 // Check header checksum
  95.                 if (((this._payloadLength + this._packetType) & 0x0F) !== this._headerChecksum) {
  96.                     this._bufferSize -= 1;
  97.                     this._buffer.copy(this._buffer, 0, this._startByteIndex + 1, this._bufferSize + this._startByteIndex + 1);
  98.                     this._state = PLLSP.STATE_WAIT_START;
  99.                 } else {
  100.                     this._state = PLLSP.STATE_READ_PAYLOAD;
  101.                 }
  102.             }
  103.         }
  104.  
  105.         // Read the payload
  106.         if (this._state === PLLSP.STATE_READ_PAYLOAD) {
  107.             if (this._bufferSize - this._startByteIndex >= this._payloadLength + 4) {
  108.                 // Check payload checksum and if it is OK send event
  109.                 let generatedPayloadChecksum = 0;
  110.                 for (let i = 0; i < this._payloadLength; i++) {
  111.                     generatedPayloadChecksum += this._buffer.readUInt8(i + 4 + this._startByteIndex);
  112.                 }
  113.                 if ((generatedPayloadChecksum & 0x0F) === this._payloadChecksum) {
  114.                     let packetPayload = Buffer.allocUnsafe(this._payloadLength + 1);
  115.                     this._buffer.copy(packetPayload, 1, this._startByteIndex + 4, this._startByteIndex + this._payloadLength + 4);
  116.                     packetPayload.writeUInt8(this._packetType, 0);
  117.  
  118.                     this.emit('data', packetPayload);
  119.                 }
  120.  
  121.                 // Prepare for next packet
  122.                 this._bufferSize -= (this._payloadLength + 4);
  123.                 this._buffer.copy(
  124.                     this._buffer,
  125.                     0,
  126.                     this._startByteIndex + this._payloadLength + 4,
  127.                     this._payloadLength + 4 + this._bufferSize);
  128.                 this._state = PLLSP.STATE_WAIT_START;
  129.                 console.log('start', this._startByteIndex + this._payloadLength + 4);
  130.                 console.log('end', this._payloadLength + 4 + this._bufferSize);
  131.                 console.log(this._buffer);
  132.  
  133.  
  134.                 // If there is two packets in single chunk try to export it
  135.                 this.push(null);
  136.             }
  137.         }
  138.     }
  139. }
  140.  
  141. module.exports = PLLSP;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement