summaryrefslogtreecommitdiffstats
path: root/assets/js/xymini.js
blob: a1fd1ca9f7e75fc519c358db5dbcceb231abb22b (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* XYMini Sender - Minimal implementation of file transfer through serial
 * Copyright (C) Ernesto Castellotti <mail@ernestocastellotti.it>
 * SPDX-License-Identifier: MPL-2.0-no-copyleft-exception
 *
 * Warning: This does not comply with XMODEM and YMODEM standards
*/

const STX = 0x02;
const ACK = 0x06;
const NAK = 0x15;
const EOF = 0x04;
const XYMINI_1K_MAGIC = 0x43;
const PAYLOAD_LEN = 1024;
const BLOCK_LEN = PAYLOAD_LEN + 5;
const CRC_POLY = 0x1021;

function uint16 (n) {
  return n & 0xFFFF;
}

function updateCrc(crcIn, incr) {
    const xor = uint16(crcIn >> 15);
    let result = uint16(crcIn << 1);

    if (incr) {
        result = uint16(result + 1);
    }

    if (xor) {
        result = uint16(result ^= CRC_POLY);
    }

    return result;
}

function crc16(data) {
    let crc;

    for (let i = 0; i < data.length; i++) {
        for (let j = 0x80; j; j >>= 1) {
            crc = updateCrc(crc, data[i] & j);
        }
    }

    for (let n = 0; n < 16; n++) {
        crc = updateCrc(crc, 0);
    }

    return crc;
}

async function detectXYMini(reader) {
    const textDecoder = new TextDecoder();

    while (true) {
        const { value, done } = await reader.read();

        if (value[0] == XYMINI_1K_MAGIC) {
            console.log("XYMini: detected");
            break;
        }
    }
}

function generateXYMiniBlock(blockId, payload) {
    let buf = new Uint8Array(BLOCK_LEN);
    let i = 0;

    buf[i++] = STX;
    buf[i++] = blockId;
    buf[i++] = 0xFF - blockId;

    if (payload.length > PAYLOAD_LEN) {
        throw new Error("Payload too large to be transmitted in one block");
    }

    for (let j = 0; j < payload.length; j++) {
        buf[i++] = payload[j];
    }

    while (i < BLOCK_LEN - 2) {
        buf[i++] = 0xFF;
    }

    let crcBuf = buf.slice(3, PAYLOAD_LEN + 3)
    let crc = crc16(crcBuf);

    buf[i++] = (crc >> 8) & 0xFF;
    buf[i++] = crc & 0xFF;

    return buf;
}

async function sendXYMini(portReader, portWriter, data, baudRate = 115200, progressCallback) {
    let blockId = 1;
    let size = data.length;
    let i = 0;
    let nakN = 0;
    let wrongCharN = 0;

    await detectXYMini(portReader);

    while(true) {
        const payloadSize = Math.min(PAYLOAD_LEN, size);

        if (size) {
            const payload = data.slice(i, payloadSize + i);

            const block = generateXYMiniBlock(blockId, payload);
            await portWriter.write(block);
        } else {
            portWriter.write(new Uint8Array([EOF]));
        }

        const { value, done } = await portReader.read();

        if (value[0] == ACK) {
            if (!size) {
                console.log("XYMini: End of transmission");

                return;
            }

            blockId++;
            size -= payloadSize;
            i += payloadSize;
            nakN = 0;
            wrongCharN = 0;
            progressCallback(data.length - size);
        } else if (value[0] == NAK) {
            if (nakN >= 10) {
                throw new Error("Received 10 NAK, receiver is rejecting file transmission");
            }

            console.log("XYMini: NAK");
            nakN++;
        } else {
            if (wrongCharN >= 30) {
                throw new Error("Received 30 wrong characters, the receiver is rejecting the transmission or the connection is too noisy");
            }

            console.log("XYMini: wrong character");
            console.log(value);
            wrongCharN++;
        }
    }
}