alp-tools

1.3.0 • Public • Published

This library is built on alp-lib and implements higher level methods to parse and build DASH7 ALP packets.

Parsing

alp-lib overlay

parse(payload): any[]

Shortcut for alp-lib parse. Parses a binary ALP payload into an array of ALP actions.

  • payload (Buffer | number[]) : The payload to parse.

parsex(payload): any[]

Same as parse, except it takes an hexadecimal string input.

  • payload (Buffer | number[]) : The payload to parse as an hexadecimal string.

Human readable feedback

xplain(packets): any[]

Turns an ALP parsed packet array into a more human readable version.

  • packets (any[]): Array of ALP packets (output of parse).

parse.xplain(payload): any[]

Parses a ALP payload directly into a human readable decoded structure.

  • payload (Buffer | number[]) : The payload to parse.

Building

class PacketBuilder

Creates a packet builder capable of creating Packet(s).

Constructor()

set_default_params(partial_default_params)

  • partial_default_params: Hash map containing: { "action_name": <default_action_parameters> } When this function is called, the PacketBuilder completes its default actions parameters by saving the specified actions parameters as the new defaults. This doesn't erase the default parameters of missing actions in the partial_default_params hash map. For each action specified, the defaults parameters are completely replaced by the new default parameter specification.

Any PacketBuilder.Packet or PacketBuilder.LazyPacket created with this PacketBuilder will use those default actions parameters.

new(): PacketBuilder.Packet

Creates a new Packet.

lazy(): PacketBuilder.LazyPacket

Creates a new Lazy Packet.

Packet "like"

The PacketBuilder implements all the action methods as a shortcut for: builder.new().<action>().

class PacketBuilder.Packet

Byte array representing an ALP packet containing a list of ALP actions. Each of its ALP action related method creates a new Packet containing this packet's ALP actions followed by the method's ALP action.

Standard methods

buffer(): Buffer

Builds the ALP packet as a NodeJS Buffer.

packet_builder(): PacketBuilder

Creates a PacketBuilder that uses this packet as header for its produced packets.

xplain(): any[]

Gives an xplain representation of this packet.

Specification dependent methods

All those methods return the Packet object therefore allowing building a packet by chaining the methods.

nop(params): Packet

or NOP(params): Packet

  • group (boolean)
  • resp (boolean)
read_file(params): Packet

or rd_f_data(params): Packet

or RD_F_DATA(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • file_offset (number)
  • length (number)
read_file_properties(params): Packet

or rd_f_prop(params): Packet

or RD_F_PROP(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
write_file(params): Packet

or wr_f_data(params): Packet

or WR_F_DATA(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • file_offset (number)
  • length (number)
  • data (string|buffer|[bytes])
write_file_flush(params): Packet

or wr_f_flush(params): Packet

or WR_F_FLUSH(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • file_offset (number)
  • length (number)
  • data (string|buffer|[bytes])
write_file_properties(params): Packet

or wr_f_prop(params): Packet

or WR_F_PROP(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • file_offset (number)
  • length (number)
  • data (string|buffer|[bytes])
action_query(params): Packet

or act_query(params): Packet

or ACT_QUERY(params): Packet

  • group (boolean)
  • resp (boolean)
  • type (number)
  • masked (boolean)
  • data_type (number)
  • comp_type (number)
  • comp_length (number)
  • comp_mask (string|buffer|[bytes])
  • comp_value (string|buffer|[bytes])
  • file1 (object):
    • file_id (number)
    • file_offset (number)
  • file2 (object):
    • file_id (number)
    • file_offset (number)
break_query(params): Packet

or BREAK_QUERY(params): Packet

  • group (boolean)
  • resp (boolean)
  • type (number)
  • masked (boolean)
  • data_type (number)
  • comp_type (number)
  • comp_length (number)
  • comp_mask (string|buffer|[bytes])
  • comp_value (string|buffer|[bytes])
  • file1 (object):
    • file_id (number)
    • file_offset (number)
  • file2 (object):
    • file_id (number)
    • file_offset (number)
permission_request(params): Packet

or perm_rq(params): Packet

or PERM_RQ(params): Packet

  • group (boolean)
  • resp (boolean)
  • perm_lvl (number)
  • auth_id (number)
  • auth_token (string|buffer|[bytes])
checksum(params): Packet

or cksum(params): Packet

or CKSUM(params): Packet

  • group (boolean)
  • resp (boolean)
  • type (number)
  • masked (boolean)
  • data_type (number)
  • comp_type (number)
  • comp_length (number)
  • comp_mask (string|buffer|[bytes])
  • comp_value (string|buffer|[bytes])
  • file1 (object):
    • file_id (number)
    • file_offset (number)
  • file2 (object):
    • file_id (number)
    • file_offset (number)
file_exists(params): Packet

or f_exist(params): Packet

or F_EXIST(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
file_create(params): Packet

or f_create(params): Packet

or F_CREATE(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • encrypted (boolean)
  • executable (boolean)
  • user_read (boolean)
  • user_write (boolean)
  • user_run (boolean)
  • guest_read (boolean)
  • guest_write (boolean)
  • guest_run (boolean)
  • act_en (boolean)
  • act_cond (number)
  • storage_class (number)
  • alp_cmd_fid (object):
    • file_id (number)
  • itf_fid (object):
    • file_id (number)
  • file_size (number)
  • allocated_size (number)
file_delete(params): Packet

or f_delete(params): Packet

or F_DELETE(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
file_restore(params): Packet

or f_restore(params): Packet

or F_RESTORE(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
file_flush(params): Packet

or f_flush(params): Packet

or F_FLUSH(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
file_copy(params): Packet

or f_copy(params): Packet

or F_COPY(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
file_execute(params): Packet

or f_exec(params): Packet

or F_EXEC(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
read_file_response(params): Packet

or rsp_f_data(params): Packet

or RSP_F_DATA(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • file_offset (number)
  • length (number)
  • data (string|buffer|[bytes])
read_file_properties_response(params): Packet

or rsp_f_prop(params): Packet

or RSP_F_PROP(params): Packet

  • group (boolean)
  • resp (boolean)
  • file_id (number)
  • encrypted (boolean)
  • executable (boolean)
  • user_read (boolean)
  • user_write (boolean)
  • user_run (boolean)
  • guest_read (boolean)
  • guest_write (boolean)
  • guest_run (boolean)
  • act_en (boolean)
  • act_cond (number)
  • storage_class (number)
  • alp_cmd_fid (object):
    • file_id (number)
  • itf_fid (object):
    • file_id (number)
  • file_size (number)
  • allocated_size (number)
status(params): Packet

or STATUS(params): Packet

####### Variant 0

  • ext = 0
  • action_id (number)
  • status (number)

####### Variant 1

  • ext = 1
  • itf_id = 16
  • length (number)
  • payload (string|buffer|[bytes])
  • ch_header (number)
  • ch_idx (number)
  • rxlev (number)
  • lb (number)
  • snr (number)
  • missed (boolean)
  • retry (boolean)
  • id_type (number)
  • token (number)
  • seq (number)
  • resp_to (number)
  • addr (object):
    • addr_type (number)
    • nls_method (number)
    • access_class (number)
    • id (string|buffer|[bytes])
    • estimated_reached (number)
  • nls_state (string|buffer|[bytes])

####### Variant 2

  • ext = 1
  • itf_id = 215
  • length (number)
  • payload (string|buffer|[bytes])
  • ch_header (number)
  • ch_idx (number)
  • rxlev (number)
  • lb (number)
  • snr (number)
  • missed (boolean)
  • retry (boolean)
  • id_type (number)
  • token (number)
  • seq (number)
  • resp_to (number)
  • addr (object):
    • addr_type (number)
    • nls_method (number)
    • access_class (number)
    • id (string|buffer|[bytes])
    • estimated_reached (number)
  • nls_state (string|buffer|[bytes])

####### Variant 3

  • ext = 3
  • itf_id = 215
  • length (number)
  • payload (string|buffer|[bytes])
  • status (number)
response_tag(params): Packet

or rsp_tag(params): Packet

or RSP_TAG(params): Packet

  • eop (boolean)
  • err (boolean)
  • tag_id (number)
chunk(params): Packet

or CHUNK(params): Packet

  • chunk (number)
logic(params): Packet

or LOGIC(params): Packet

  • logic (number)
forward(params): Packet

or fwd(params): Packet

or FWD(params): Packet

  • resp (boolean)
  • itf_id = 215
  • stop_on_err (boolean)
  • record (boolean)
  • retry_mode (number)
  • resp_mode (number)
  • dorm_to (number)
  • te (number)
  • addr (object):
    • addr_type (number)
    • nls_method (number)
    • access_class (number)
    • id (string|buffer|[bytes])
    • estimated_reached (number)
iforward(params): Packet

or ifwd(params): Packet

or IFWD(params): Packet

  • ovld (boolean)
  • resp (boolean)
  • itf_f_id (number)
  • addr_type (number)
  • nls_method (number)
  • access_class (number)
  • id (string|buffer|[bytes])
  • estimated_reached (number)
tag(params): Packet

or TAG(params): Packet

  • eop (boolean)
  • tag_id (number)

class PacketBuilder.LazyPacket

Unlike the PacketBuilder.Packet class, this class ALP action methods do not trigger a binary packet build right away. Instead they add the ALP packet corresponding to the call to an internal representation that can be built later.

Therefore, the ALP action methods return the original packet itself instead of a new packet (as opposed to the PacketBuilder.Packet methods behavior).

See examples for more details on the different behaviors between the packet classes.

Standard methods

build(): PacketBuilder.Packet

Builds an ALP packet.

build_buffer(): Buffer

Builds the current ALP packet into a buffer.

xplain(): any[]

Gives an xplain representation of this packet.

list(): any[]

Return an Object representation of the contained ALP actions that can be passed the alp-lib.build to build the corresponding binary packet.

Specification dependent methods

Implements all the specification dependent PacketBuilder.Packet methods but in a "lazy" way.

Examples

Parsing action

const AT = require("../alp_tools");

const read_command_packet = Buffer.from("B45641420007")
console.log("parse", AT.parse(binary));
console.log("parse.xplain", AT.parse.xplain(binary));

let complex_payload = new Buffer("B400F3640004000841010142004142000941420001", "hex");
console.log("Complex payload explained:", AT.parse.xplain(complex_payload));

Common ALP actions building

const AT = require("./alp_tools");

// Print helper
function bin_pkt_print(first_line, binary, no_xplain) {
    console.log(first_line);
    console.log(binary.buffer());
    // Comparing the standard parse and xplain parse versions
    console.log("PARSE", AT.parse(binary));
    console.log("PARSE.XPLAIN", AT.parse.xplain(binary));
    console.log();
}

// Creating a packet builder
let alp_builder = new AT.PacketBuilder();

// Building a status
let status = alp_builder.status({
    ext: 0,
    action_id: 1,
    status: 2,
});
bin_pkt_print("STATUS:", status, true);

// Building a local read file
let local_read_file = alp_builder
    .tag({
        eop: true,
        tag_id: 4,
    }).read_file({
        resp: true,
        group: false,

        file_id: 0, // UID file
        file_offset: 0,
        length: 8,
    });
bin_pkt_print("LOCAL_READ_FILE:", local_read_file, true);

// Setting some common default parameters for the ALP builder for ergonomy
alp_builder.set_default_params({
    tag: {eop: true, tag_id: 0},
    read_file: {resp: true, group: false, file_offset: 0},
    write_file: {resp: true, group: false, file_offset: 0},
    fwd: {
        resp: true,
        group: false,

        itf_id: 0xD7,

        stop_on_err: false,
        record: false,
        retry_mode: 0,
        resp_mode: 1,
        dorm_to: 0,
        te: 0,

        addr: {
            addr_type: 2,
            nls_method: 6,
            estimated_reached: 1,
        },
    },
});

// Building the local read more easily
let easy_local_read_file = alp_builder
    .tag()
    .read_file({
        file_id: 0, // UID file
        length: 8,
    });
bin_pkt_print("EASY_LOCAL_READ_FILE:", easy_local_read_file, true);

// Building a distant write file
let distant_write_file = alp_builder
    .tag()
    .fwd({
        addr: {
            id: Buffer.from("001BC50C70000D11", "hex"),
            access_class: 0x01,
        },
    })
    .write_file({
        file_id: 0x42, // scratch file on devices modem
        data: Buffer.from("FEDCBA", "hex"),
    });

// Unfortunately, as can be seen in the next print, AT.parse.xplain still have trouble explaining some action
    // fields.
bin_pkt_print("DISTANT_WRITE_FILE:", distant_write_file, true);

Difference between Packet and LazyPacket

const AT = require("../alp_tools");

// Creating a packet builder
let alp_builder = new AT.PacketBuilder();

// Setting some common default parameters for the ALP builder for ergonomy
alp_builder.set_default_params({
    tag: {eop: true, tag_id: 0},
    read_file: {resp: true, group: false, file_offset: 0},
    write_file: {resp: true, group: false, file_offset: 0},
    fwd: {
        resp: true,
        group: false,

        itf_id: 0xD7,

        stop_on_err: false,
        record: false,
        retry_mode: 0,
        resp_mode: 1,
        dorm_to: 0,
        te: 0,

        addr: {
            addr_type: 2,
            nls_method: 6,
            estimated_reached: 1,
        },
    },
});

// Test function
function test_packet(packet) {
    let tag = packet.tag();
    let tagged_read = tag.read_file({file_id: 0, length: 5});

    console.log("Packet tag: ", tag.toString());
    console.log("Packet tagged_read", tagged_read.toString());
}

// Test Packet
console.log("Testing Packet");
test_packet(alp_builder.new());
console.log();

// Test LazyPacket
console.log("Testing LazyPacket");
test_packet(alp_builder.lazy());
console.log("Oh no! Packet tag contains a read!");
console.log();

// Correct LazyPacket usage
let packet = alp_builder.lazy();
packet.tag();
for(let i = 0; i < 10; ++i) {
    packet.read_file({file_id: i, length: i});
}
console.log("LazyPacket many reads:", packet.toString());

Readme

Keywords

Package Sidebar

Install

npm i alp-tools

Weekly Downloads

2

Version

1.3.0

License

BSD-2-Clause

Unpacked Size

62.7 kB

Total Files

9

Last publish

Collaborators

  • wizzilab