Skip to content

Commit

Permalink
added slip chars handling, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Neznaradko committed Jun 5, 2019
1 parent 1d710e0 commit e96b9e3
Show file tree
Hide file tree
Showing 10 changed files with 9,368 additions and 5,350 deletions.
8 changes: 4 additions & 4 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
5 changes: 3 additions & 2 deletions 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 @@ -322,11 +322,12 @@ export class SerialPacket implements Packet {
let header: any[];

for(let i = 0; i < data.length - 1; i++) {
bytes.push(data.charCodeAt(i));
bytes.push(data.codePointAt(i));
}

// 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
4 changes: 2 additions & 2 deletions src/constants/Config.ts
@@ -1,8 +1,8 @@
// TODO for local development. Request to stage/prod are restricted by CORS
// import translations from '../../translations/translations_local.json';

// export const API_ENDPOINT = 'http://127.0.0.1:4000/api/v1';
export const API_ENDPOINT = 'https://staging.energyinschools.co.uk/api/v1';
export const API_ENDPOINT = 'http://127.0.0.1:4000/api/v1';
// export const API_ENDPOINT = 'https://staging.energyinschools.co.uk/api/v1';
// export const API_ENDPOINT = 'https://energyinschools.co.uk/api/v1';

/* Token Types */
Expand Down
Binary file added tests/serialPacketTests.test.ts
Binary file not shown.
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 e96b9e3

Please sign in to comment.