|
| 1 | +'use strict'; |
| 2 | +var constants = require('../lifx').constants; |
| 3 | + |
| 4 | +// Package headers 36 bit in total consisting of |
| 5 | +// |
| 6 | +// size - 2 bit |
| 7 | +// description - 2 bit |
| 8 | +// |
| 9 | +// source - 4 bit |
| 10 | +// target - 6 bit |
| 11 | +// 00 00 |
| 12 | +// site - 6 bit |
| 13 | +// |
| 14 | +// 00 |
| 15 | +// sequence - 1 bit |
| 16 | +// time - 8 bit |
| 17 | +// type - 2 bit |
| 18 | +// 00 00 |
| 19 | +var Packet = {}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Parses a lifx packet header |
| 23 | + * @param {Buffer} buf Buffer containg lifx packet |
| 24 | + * @return {Object} parsed packet object |
| 25 | + */ |
| 26 | +Packet.toObject = function(buf) { |
| 27 | + var obj = {}; |
| 28 | + var offset = 0; |
| 29 | + |
| 30 | + obj.size = buf.readUInt16LE(offset); |
| 31 | + offset += 2; |
| 32 | + |
| 33 | + obj.description = buf.readUInt16LE(offset); |
| 34 | + offset += 2; |
| 35 | + obj.addressable = (obj.description & constants.ADDRESSABLE_BIT) !== 0; |
| 36 | + obj.tagged = (obj.description & constants.TAGGED_BIT) !== 0; |
| 37 | + obj.origin = ((obj.description & constants.ORIGIN_BITS) >> 14) !== 0; |
| 38 | + obj.protocolVersion = (obj.description & constants.PROTOCOL_VERSION_BITS); |
| 39 | + |
| 40 | + obj.source = buf.toString('hex', offset, offset + 4); |
| 41 | + offset += 4; |
| 42 | + |
| 43 | + obj.target = buf.toString('hex', offset, offset + 6); |
| 44 | + offset += 6; |
| 45 | + |
| 46 | + obj.reserved1 = buf.slice(offset, offset + 2); |
| 47 | + offset += 2; |
| 48 | + |
| 49 | + obj.site = buf.toString('utf8', offset, offset + 6); |
| 50 | + obj.site = obj.site.replace(/\0/g, ''); |
| 51 | + offset += 6; |
| 52 | + |
| 53 | + obj.reserved2 = buf.slice(offset, offset + 1); |
| 54 | + offset += 1; |
| 55 | + |
| 56 | + obj.sequence = buf.readUInt8(offset); |
| 57 | + offset += 1; |
| 58 | + |
| 59 | + // buf.readUInt64LE |
| 60 | + obj.time = (Math.pow(2, 32) * buf.readUInt32LE(offset + 4)) + buf.readUInt32LE(offset); |
| 61 | + offset += 8; |
| 62 | + if (obj.time !== 0) { |
| 63 | + obj.time = obj.time.toString(); |
| 64 | + obj.time = obj.time.substring(0, 13); |
| 65 | + var time = new Date(); |
| 66 | + time.setTime(obj.time); |
| 67 | + obj.time = time; |
| 68 | + } else { |
| 69 | + obj.time = null; |
| 70 | + } |
| 71 | + |
| 72 | + obj.type = buf.readUInt16LE(offset); |
| 73 | + offset += 2; |
| 74 | + |
| 75 | + obj.reserved3 = buf.slice(offset, offset + 2); |
| 76 | + offset += 2; |
| 77 | + |
| 78 | + return obj; |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * Creates a buffer for a lifx header from an object |
| 83 | + * @param {Object} obj Object containg header data |
| 84 | + * @return {Buffer} header buffer |
| 85 | + */ |
| 86 | +Packet.toBuffer = function(obj) { |
| 87 | + var buf = new Buffer(36); |
| 88 | + buf.fill(0); |
| 89 | + var offset = 0; |
| 90 | + |
| 91 | + buf.writeUInt16LE(obj.size, offset); |
| 92 | + offset += 2; |
| 93 | + |
| 94 | + if (obj.description === undefined || obj.description === null) { |
| 95 | + if(obj.protocolVersion === undefined) { |
| 96 | + obj.protocolVersion = constants.PROTOCOL_VERSION_CURRENT; |
| 97 | + } |
| 98 | + |
| 99 | + obj.description = obj.protocolVersion; |
| 100 | + |
| 101 | + if(obj.addressable !== undefined && obj.addressable === true) { |
| 102 | + obj.description |= constants.ADDRESSABLE_BIT; |
| 103 | + } |
| 104 | + |
| 105 | + if(obj.tagged !== undefined && obj.tagged === true) { |
| 106 | + obj.description |= constants.TAGGED_BIT; |
| 107 | + } |
| 108 | + |
| 109 | + if(obj.origin !== undefined && obj.origin === true) { |
| 110 | + // 0 or 1 to the 14 bit |
| 111 | + obj.description |= (1 << 14); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + buf.writeUInt16LE(obj.description, offset); |
| 116 | + offset += 2; |
| 117 | + |
| 118 | + if (obj.source !== undefined && obj.source.length > 0) { |
| 119 | + buf.write(obj.source, offset, 4, 'hex'); |
| 120 | + } |
| 121 | + offset += 4; |
| 122 | + |
| 123 | + buf.write(obj.target, offset, 6, 'hex'); |
| 124 | + offset += 6; |
| 125 | + |
| 126 | + // reserved1 |
| 127 | + offset += 2; |
| 128 | + |
| 129 | + buf.write(obj.site, offset, 6, 'utf8'); |
| 130 | + offset += 6; |
| 131 | + |
| 132 | + // reserved2 |
| 133 | + offset += 1; |
| 134 | + |
| 135 | + buf.writeUInt8(obj.sequence, offset); |
| 136 | + offset += 1; |
| 137 | + |
| 138 | + // @TODO buf.writeUInt64LE |
| 139 | + buf.writeUInt32LE(0, offset); |
| 140 | + offset += 4; |
| 141 | + buf.writeUInt32LE(0, offset); |
| 142 | + offset += 4; |
| 143 | + // obj.time = (Math.pow(2, 32) * buf.readUInt32LE(offset + 4)) + buf.readUInt32LE(offset); |
| 144 | + |
| 145 | + buf.writeUInt16LE(obj.type, offset); |
| 146 | + offset += 2; |
| 147 | + |
| 148 | + // reserved3 |
| 149 | + offset += 2; |
| 150 | + |
| 151 | + return buf; |
| 152 | +}; |
| 153 | + |
| 154 | +module.exports = Packet; |
0 commit comments