{"size": 2766, "ext": "js", "max_stars_repo_path": "packet.js", "max_stars_repo_name": "bjorning/three-wire-parser", "max_stars_repo_stars_event_min_datetime": "2020-11-11T08:23:45.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-11T08:23:45.000Z", "max_issues_repo_path": "packet.js", "max_issues_repo_name": "bjorning/three-wire-parser", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packet.js", "max_forks_repo_name": "bjorning/three-wire-parser", "max_forks_repo_forks_event_min_datetime": "2018-07-22T10:39:38.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-22T10:39:38.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.8125, "max_line_length": 96, "alphanum_fraction": 0.6200289226}
const ThreeWireHeader = require('./header');
const LinkControlPacket = require('./packetTypes/linkControlPacket');

const HEADER_LENGTH = 4;

/**
 * @class ThreeWirePacket
 * 
 * Represents a complete Three Wire UART Protocol packet
 * 
 * @example
 * import packet1 = new ThreeWirePacket(packetBytesArray);
 */
class ThreeWirePacket {
    /**
     * @param {Array} packetBytesArray The raw packet
     */
    constructor(packetBytesArray) {
        this._pktBytes = packetBytesArray;

        if (!packetBytesArray) {
            throw new Error('Invalid input, undefined');
        } else if (packetBytesArray.length < HEADER_LENGTH) {
            throw new Error(`Invalid input, length must be >= ${HEADER_LENGTH}`);
        }

        this.parseHeader();
        this.parsePayload();
        //this.parseCRC();
    }

    parseHeader() {
        this._header = new ThreeWireHeader(this._pktBytes);
        
        if (!this._header) {
            throw new Error('Invalid header, could not parse');
        }
    }

    parsePayload() {
        const payloadStart = HEADER_LENGTH;

        if ((this._pktBytes.length - HEADER_LENGTH) < this._header.payloadLength) {
            throw new Error('Invalid length, payload array shorter than header payload length');
        }

        const payloadEnd = payloadStart + this._header.payloadLength;
        this._payloadBytesArray = this._pktBytes.slice(payloadStart, payloadEnd);

        switch (this._header.packetType) {
            case this._header.packetTypeEnum.ACI_PACKET:
            case this._header.packetTypeEnum.HCI_COMMAND_PACKET:
            case this._header.packetTypeEnum.HCI_ACL_DATA_PACKET:
            case this._header.packetTypeEnum.HCI_SYNCHRONOUS_DATA_PACKET:
            case this._header.packetTypeEnum.HCI_EVENT_PACKET:
            case this._header.packetTypeEnum.RESET_PACKET:
            case this._header.packetTypeEnum.VENDOR_SPECIFIC_PACKET:
                break;
            case this._header.packetTypeEnum.LINK_CONTROL_PACKET:
                const linkCtrlPkt = new LinkControlPacket(this._payloadBytesArray);
                this._parsedPayload = linkCtrlPkt.parsedPacket;
                break;

        }
    }

    /**
     * @type {ThreeWireHeader} Packet header
     */
    get header() {
        return this._header;
    }

    /**
     * @type {object} Packet payload
     */
    get payload() {
        return this._payloadBytesArray;
    }

    /**
     * @type {string} Parsed payload as string
     */
    get parsedPayload() {
        return this._parsedPayload;
    }

    toString() {
        let str = '';
        str += this._header.toString();
        str += `, payload: ${this.parsedPayload}`;
        return str;
    }
}

module.exports = ThreeWirePacket;