Skip to content

Commit

Permalink
Added slip chars handling, Added tests (#15)
Browse files Browse the repository at this point in the history
* Added tests, added slip chars handling

* replaced raw payload from test data with bytes
  • Loading branch information
yarsanich committed Jun 5, 2019
1 parent 1d710e0 commit 0f0ad58
Show file tree
Hide file tree
Showing 9 changed files with 9,403 additions and 5,346 deletions.
6 changes: 3 additions & 3 deletions dist/javascripts/WebBridge.bundle.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions jest.config.js
@@ -0,0 +1,6 @@
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
14,615 changes: 9,300 additions & 5,315 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Expand Up @@ -7,7 +7,9 @@
"build:prod": "webpack --mode=production --eisenv=production",
"build": "webpack --mode=development",
"start": "webpack-dev-server --mode=development --hot --inline",
"chrome-without-web-security": "google-chrome --disable-web-security --user-data-dir=/tmp/chrome"
"chrome-without-web-security": "google-chrome --disable-web-security --user-data-dir=/tmp/chrome",
"test": "jest",
"testWithCoverage": "jest --coverage"
},
"dependencies": {
"@ekifvk/jpquery": "github:WinUP/jspath",
Expand Down Expand Up @@ -44,7 +46,6 @@
"tslint-eslint-rules": "^5.4.0",
"typed-web-workers": "^2.1.1",
"typedoc": "^0.14.2",
"typescript": "^3.3.3",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0",
"watchify": "^3.11.0",
Expand All @@ -53,21 +54,25 @@
"worker-nodes": "^1.6.1"
},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.123",
"@types/w3c-web-usb": "^1.0.3",
"commonjs": "0.0.1",
"copy-webpack-plugin": "^5.0.3",
"css-loader": "^2.1.1",
"html-webpack-plugin": "^3.2.0",
"jest": "^24.8.0",
"json-loader": "^0.5.7",
"mini-css-extract-plugin": "^0.7.0",
"pug": "^2.0.0-beta11",
"pug-loader": "^2.4.0",
"style-loader": "^0.23.1",
"ts-jest": "^24.0.2",
"ts-loader": "^6.0.2",
"typescript": "^3.5.1",
"url-loader": "^1.1.2",
"webpack-cli": "^3.3.2",
"webpack": "^4.0.0",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.3.1",
"webpack-livereload-plugin": "^0.11.0"
}
Expand Down
65 changes: 43 additions & 22 deletions src/SerialHandler.ts
Expand Up @@ -40,15 +40,46 @@ export class SerialHandler {
* Adds an event listener to the DAPLink target to listen to serial events and process the data
*/
private async setupSerialHandler() {
this.targetDevice.on(DAPLink.EVENT_SERIAL_DATA, data => {
this.targetDevice.on(DAPLink.EVENT_SERIAL_DATA, async (raw_data) => {
// if serial handling is paused, dont perform any actions
if(this.isPaused)
return;

if (data.search(String.fromCharCode(SlipChar.SLIP_END)) === -1 || data.charCodeAt(0) !== 0) {
if (raw_data.search(String.fromCharCode(SlipChar.SLIP_END)) === -1 || raw_data.charCodeAt(0) !== 0 ) {
return;
}

// DEAL with SLIP Chars
let data = '';

for (let i = 0; i < raw_data.length; i++) {
let c = raw_data.charCodeAt(i);

if (c === SlipChar.SLIP_END){
data += String.fromCharCode(c);
break
}

if (c === SlipChar.SLIP_ESC){
let next = raw_data.charCodeAt(i + 1);

if (next === SlipChar.SLIP_ESC_END) {
data += String.fromCharCode(SlipChar.SLIP_END)
} else if (next === SlipChar.SLIP_ESC_ESC) {
data += String.fromCharCode(SlipChar.SLIP_ESC)
} else {
data += String.fromCharCode(c);
data += String.fromCharCode(next);
}

i += 1;

continue;
}

data += String.fromCharCode(c);
}

this.packetCount++;
debug(`Packet count: ${this.packetCount}`, DebugType.DEBUG);

Expand All @@ -71,26 +102,16 @@ export class SerialHandler {
}

// handle the request and await the promised resolve packet or reason for error
requestHandler.handleRequest(serialPacket)
.then((responsePacket) => {
responsePacket.setRequestBit(RequestStatus.REQUEST_STATUS_OK);
this.write(responsePacket);
})
.catch((reason) => {
debug(`${reason}`, DebugType.ERROR);

// clear all data from input packet and return it as an error packet
let responsePacket = new SerialPacket(serialPacket.getAppID(), serialPacket.getNamespaceID(), serialPacket.getUID(), serialPacket.getReqRes());
responsePacket.clearAndError(reason);
this.write(responsePacket);
});
let responsePacket = await requestHandler.handleRequest(serialPacket)
responsePacket.setRequestBit(RequestStatus.REQUEST_STATUS_OK);
await this.write(responsePacket);
} catch (e) {
console.log(e);

// clear serial packet and try to send an error response
serialPacket.clearAndError("ERROR");
this.write(serialPacket);
}
debug(`${e}`, DebugType.ERROR);
// clear all data from input packet and return it as an error packet
let responsePacket = new SerialPacket(serialPacket.getAppID(), serialPacket.getNamespaceID(), serialPacket.getUID(), serialPacket.getReqRes());
responsePacket.clearAndError(e);
await this.write(responsePacket);
}
});
}

Expand All @@ -109,7 +130,7 @@ export class SerialHandler {
let packet = String.fromCharCode(...serialPacket.getFormattedPacket());

try {
await this.targetDevice.serialWrite(packet);
return await this.targetDevice.serialWrite(packet);
} catch(e) {
console.log(e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/SerialPacket.ts
Expand Up @@ -286,7 +286,7 @@ export class SerialPacket implements Packet {
let offset = 0;

// grab subtype and the remainder of the packet
let subtype = unpack("b", rawPayload);
let subtype = unpack("b", rawPayload, 0);
let remainder = rawPayload.slice(1);

// compare against each subtype and process the data accordingly
Expand Down Expand Up @@ -327,6 +327,7 @@ export class SerialPacket implements Packet {

// unpack header using header structure
header = unpack(HEADER_STRUCTURE, bytes.slice(0, HEADER_LENGTH));

payload = bytes.slice(HEADER_LENGTH);

// create packet using the header bytes and the payload data
Expand Down
2 changes: 1 addition & 1 deletion src/WebBridge.ts
Expand Up @@ -47,7 +47,7 @@ let hub_variables = {
"proxy_requests": true
},
"dapjs": {
"serial_delay": 90,
"serial_delay": 100,
"baud_rate": 115200,
"flash_timeout": 5000,
"reset_pause": 1000
Expand Down
39 changes: 39 additions & 0 deletions tests/serialPacketTests.test.ts
@@ -0,0 +1,39 @@
import { SerialPacket } from '../src/SerialPacket';

describe('decode payload', function () {
it('wrong message decode', function () {
let bytes = [
0, 1, 57, 219, 221, 2, // HEADER
// PAYLOAD
1, 47, 115, 104, 97, 114, 101, 47, 104, 105,
115, 116, 111, 114, 105, 99, 97, 108, 68, 97,
116, 97, 47, 0, 1, 51, 52, 0, 1, 103, 108, 50,
0, 1, 103, 108, 0, 1, 99, 101, 108, 115, 105,
117, 115, 0, 192
];

let rawPayload = '';
for (let i = 0; i < bytes.length; i++) {
rawPayload += (String.fromCharCode(bytes[i]))
}
let result = SerialPacket.dataToSerialPacket(rawPayload);
expect(result.payload).toMatchObject(["/share/historicalData/", "34", "gl2", "gl", "celsius"]);
})
it('correct message decode', function () {
let bytes = [
0, 1, 63, 104, 2, // HEADER correct
// PAYLOAD
1, 47, 115, 104, 97, 114, 101, 47, 104, 105,
115, 116, 111, 114, 105, 99, 97, 108, 68, 97,
116, 97, 47, 0, 1, 51, 53, 0, 1, 103, 108, 49,
0, 1, 103, 108, 0, 1, 99, 101, 108, 115, 105,
117, 115, 0, 192
];
let rawPayload = '';
for (let i = 0; i < bytes.length; i++) {
rawPayload += (String.fromCharCode(bytes[i]))
}
let result = SerialPacket.dataToSerialPacket(rawPayload);
expect(result.payload).toMatchObject(["/share/historicalData/", "35", "gl1", "gl", "celsius"]);
})
});
2 changes: 1 addition & 1 deletion webpack.config.js
Expand Up @@ -18,7 +18,7 @@ var config = {
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.tsx?$/, use: "ts-loader", exclude: /node_modules/ },
{ test: /\.tsx?$/, use: "ts-loader", exclude: ['/node_modules/', '/tests']},
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] },
{ test: /\.pug$/, loader: 'pug-loader' },
{ test: /\.(png|jpg)$/, loader: 'url-loader'},
Expand Down

0 comments on commit 0f0ad58

Please sign in to comment.