* feat: kboot package * feat: kboot package * fix: wait 1 sec after device is available * test: fix unit test * refactor: clean unused codes * doc: improve readme.md * doc: improve readme.md * test: fix unit test * chore: fix lint settings * style: fix linting issues
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { isNullOrUndefined } from 'util';
|
|
import { pack } from 'byte-data';
|
|
|
|
import { CommandOption } from '../../models';
|
|
|
|
/**
|
|
* Encode the USB Command.
|
|
* @param option
|
|
*/
|
|
export const encodeCommandOption = (option: CommandOption): number[] => {
|
|
const payload = [
|
|
option.command,
|
|
option.hasDataPhase ? 1 : 0,
|
|
0, // Reserved. Should be 0
|
|
option.params ? option.params.length / 4 >> 0 : 0 // number of parameters
|
|
];
|
|
|
|
if (option.params) {
|
|
payload.push(...option.params);
|
|
}
|
|
|
|
const header = [
|
|
1, // Communication channel
|
|
0, // TODO: What is it?
|
|
...pack(payload.length, { bits: 16 }) // payload length in 2 byte
|
|
];
|
|
|
|
const placeholders = new Array(32 - payload.length)
|
|
.fill(0);
|
|
|
|
return [...header, ...payload, ...placeholders];
|
|
};
|
|
|
|
export const validateCommandParams = (params: any[]): void => {
|
|
if (isNullOrUndefined(params)) {
|
|
return;
|
|
}
|
|
|
|
if (!Array.isArray(params)) {
|
|
throw new Error('Command parameters must be an array!');
|
|
}
|
|
|
|
if (params.length > 28) {
|
|
throw new Error('Maximum 7 (28 bytes) command parameters allowed!');
|
|
}
|
|
};
|