UNPKG

@iotize/device-client.js

Version:

IoTize Device client for Javascript

70 lines (69 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var invalid_adpu_error_1 = require("../invalid-adpu-error"); var apdu_command_1 = require("./apdu-command"); /** * Created by IoTize on 09/03/ * * Model representing an application protocol data unit */ var APDUCommandConverter = /** @class */ (function () { function APDUCommandConverter() { this.buffer = new Uint8Array(APDUCommandConverter.APDU_BUFFER_SIZE); } /** * [(4) 0:3 HEADER][(1) 4:4 LENGTH][5:n PAYLOAD][ (4) STATUS CODE] * * @return * @throws Exception */ APDUCommandConverter.prototype.encode = function (command) { var offset = 0; this.buffer[offset++] = command.getCLA(); this.buffer[offset++] = command.getInstructionCode(); this.buffer.set(command.getParameters(), offset); offset += apdu_command_1.APDUCommand.P1P2_LENGTH; this.buffer.set(command.getSizeOfCommandInBytes(), offset); offset += command.getSizeOfCommandInBytes().length; this.buffer.set(command.getData(), offset); offset += command.getData().length; if (command.hasExpectedResponseSize()) { this.buffer.set(command.getExpectedResponseSize(), offset); offset += command.getExpectedResponseSize().length; } return this.buffer.subarray(0, offset); }; /** * [(4) 0:3 HEADER][(1) 4:4 LENGTH][5: PAYLOAD][ (4) STATUS CODE] * * @param buffer the input encoded data * @return the APDUCommand model * @throws InvalidAPDUException if it's not a valid APDU encoded data */ APDUCommandConverter.prototype.decode = function (buffer) { var command = new apdu_command_1.APDUCommand(); if (!buffer || buffer.length < APDUCommandConverter.MIN_APDU_SIZE) { throw invalid_adpu_error_1.APDUError.frameSizeTooSmall(buffer || new Uint8Array()); } command.setCLA(buffer[0]) .setInstructionCode(buffer[1]) .setInstructionParameter(buffer[2], buffer[3]); // TODO here we suppose that Le = 1 byte. It's not always true var lenData = ((buffer[4] & 0xFF)); command.setSizeOfCommandInBytes(lenData); var apduHeaderSize = 5; // TODO constant if (buffer.length < lenData + apduHeaderSize) { throw invalid_adpu_error_1.APDUError .frameSizePayloadTooSmall(lenData + apduHeaderSize, buffer); } command.setData(buffer.subarray(apduHeaderSize, apduHeaderSize + lenData)); // TODO no response size // command.setExpectedResponseSize(ByteArrayHelper.subarray(buffer, -1)); return command; }; APDUCommandConverter.HEADER_LENGTH = 4; APDUCommandConverter.MIN_APDU_SIZE = 5; APDUCommandConverter.APDU_BUFFER_SIZE = 255; return APDUCommandConverter; }()); exports.APDUCommandConverter = APDUCommandConverter;