All files / static / packet_conv / lane_deleted.js

100.00% Branches 16/16
100.00% Lines 108/108
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x6
x6
x7
x7
x1
x10
x6
x8
x8
x1
x8
x6
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x2
x1
x2
x2
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x13
x13
x13
x15
x15
x1
x23
x13
x15
x15
x1
x13
x15
x15
x1
x13
x15
x15
x13
x15
x15
x13
x23
x28
x28
x28
x28
x23
x1
x15
x13









































































































/**
 * @file Encoding/decoding functions for `lane_deleted` packet
 *
 * More detail of packet protocol, see `packet-protocol.md`
 *
 * @author aKuad
 */

import { typeof_detail } from "../util/typeof_detail.js";


/**
 * Packet type ID of gain modify packet
 */
export const LANE_DELETED_PACKET_TYPE_ID = 0x33;


/**
 * Create lane_deleted packet
 *
 * @param {number} lane_id Lane ID of deleted
 * @returns {Uint8Array} Encoded packet
 *
 * @throws {TypeError} If `lane_id` is not `number`
 * @throws {RangeError} If `lane_id` is not in -80~80
 */
export function packet_lane_deleted_encode(lane_id) {
  // Arguments type checking
  if(typeof lane_id !== "number") {
    throw new TypeError(`lane_id must be number, but got ${typeof_detail(lane_id)}`);
  }

  // Arguments range checking
  if(lane_id < 0 || lane_id > 255) {
    throw new RangeError(`lane_id must be in 0~255, but got ${lane_id}`);
  }

  return Uint8Array.of(LANE_DELETED_PACKET_TYPE_ID, lane_id);
}


/**
 * Data structure of decoded lane_deleted packet
 *
 * @typedef {Object} GainModify
 * @property {number} lane_id Lane ID of deleted
 */

/**
 * Unpack lane_deleted packet
 *
 * Note: About raises, see reference of `is_lane_deleted_packet`.
 *
 * @param {Uint8Array} raw_packet Encoded packet
 * @returns {number} Lane ID of deleted
 */
export function packet_lane_deleted_decode(raw_packet) {
  is_lane_deleted_packet(raw_packet, true);

  return raw_packet[1];
}


/**
 * Verify the packet is lane_deleted packet
 *
 * @param {Uint8Array} raw_packet Packet to verify
 * @param {boolean} throw_on_invalid Toggle behavior when packet is invalid, true: raise exception, false: return false
 * @returns {boolean} It is a lane_deleted packet: true, otherwise: false (if throw_on_invalid === true, error will be thrown)
 *
 * @throws {TypeError} If `raw_packet` is not `Uint8Array`
 * @throws {RangeError} If `raw_packet` is an empty array
 * @throws {RangeError} If `raw_packet` has not an lane_deleted packet type ID
 * @throws {RangeError} If `raw_packet` is too long bytes as lane_deleted packet
 */
export function is_lane_deleted_packet(raw_packet, throw_on_invalid = false) {
  try {
    // Arguments type checking
    if(!(raw_packet instanceof Uint8Array)) {
      throw new TypeError(`raw_packet must be Uint8Array, but got ${typeof_detail(raw_packet)}`);
    }

    // Packet content availability checking
    if(raw_packet.length === 0) {
      throw new RangeError("Empty array passed");
    }

    if(raw_packet[0] !== LANE_DELETED_PACKET_TYPE_ID) {
      throw new RangeError(`It has not a lane_deleted packet type ID - should be ${LANE_DELETED_PACKET_TYPE_ID}, but got ${raw_packet[0]}`);
    }

    if(raw_packet.length < 2) {
      throw new RangeError(`Too short bytes as lane deleted packet - expected 2, but got ${raw_packet.length}`);
    }
    if(raw_packet.length > 2) {
      throw new RangeError(`Too long bytes as lane deleted packet - expected 2, but got ${raw_packet.length}`);
    }
  } catch(e) {
    if(throw_on_invalid) {
      throw e;
    } else {
      return false;
    }
  }

  return true;
}