summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/serial/serialPort_readable-manual.https.html
blob: 4e49ef4061774bf6d48f9fec7d7f09f45414fb11 (plain)
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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="resources/common.js"></script>
    <script src="resources/manual.js"></script>
  </head>
  <body>
    <p>
      These tests require a device configured with the following Arduino sketch:

      <pre>
uint32_t seed = 0;
uint32_t bytesToSend = 0;

uint8_t nextByte() {
  seed = (1103515245 * seed + 12345) % 0x8000000;
  return (seed >> 16) & 0xFF;
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (!Serial) {
    return;
  }

  if (bytesToSend == 0) {
    // Read the seed and number of bytes to send from the host as 32-bit
    // little-endian values.
    if (Serial.available() &lt 8) {
      return;
    }

    uint8_t buf[8];
    Serial.readBytes((char*)buf, sizeof buf);
    seed = (uint32_t)buf[0] |
            ((uint32_t)buf[1] &lt;&lt; 8) |
            ((uint32_t)buf[2] &lt;&lt; 16) |
            ((uint32_t)buf[3] &lt;&lt; 24);
    bytesToSend = (uint32_t)buf[4] |
                  ((uint32_t)buf[5] &lt;&lt; 8) |
                  ((uint32_t)buf[6] &lt;&lt; 16) |
                  ((uint32_t)buf[7] &lt;&lt; 24);
  } else {
    uint8_t buf[64];
    uint32_t count = min(sizeof buf, bytesToSend);
    for (uint32_t i = 0; i &lt; count; ++i) {
      buf[i] = nextByte();
    }
    bytesToSend -= count;
    Serial.write((char*)buf, count);
  }
}
      </pre>
    </p>
    <p>
      <progress id='progress'></progress>
    </p>
    <script>
      let seed = 10;
      const length = 1024 * 1024 * 10;

      function next_byte() {
        seed = (Math.imul(1103515245, seed) + 12345) % (1 << 31);
        return (seed >> 16) & 0xFF;
      }

      manual_serial_test(async (t, port) => {
        await port.open({baudRate: 115200, bufferSize: 1024});

        const config = new DataView(new ArrayBuffer(8));
        config.setUint32(0, seed, /*littleEndian=*/true);
        config.setUint32(4, length, /*littleEndian=*/true);

        const writer = port.writable.getWriter();
        writer.write(config);

        const progress = document.getElementById('progress');
        progress.max = length;
        progress.value = 0;

        const reader = port.readable.getReader();
        let bytesRead = 0;
        while (bytesRead < length) {
          const { value, done } = await reader.read();
          assert_false(done);
          for (let i = 0; i < value.byteLength; ++i) {
            assert_equals(value[i], next_byte(),
                          `mismatch at byte ${bytesRead + i}`);
          }
          bytesRead += value.byteLength;
          progress.value = bytesRead;
        }

        writer.releaseLock();
        reader.releaseLock();
        await port.close();
      }, `Reading ${length} bytes from the device succeeds.`);
    </script>
  </body>
</html>