Skip to content

Commit

Permalink
defined the blockwise option with properties
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Dec 18, 2017
1 parent 7a45de2 commit b5caf75
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 4 deletions.
3 changes: 3 additions & 0 deletions build/BlockOption.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { NumericOption } from "./Option";
export declare class Block1Option extends NumericOption {
}
6 changes: 6 additions & 0 deletions build/BlockOption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Option_1 = require("./Option");
class Block1Option extends Option_1.NumericOption {
}
exports.Block1Option = Block1Option;
3 changes: 3 additions & 0 deletions build/BlockwiseOptions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { NumericOption } from "./Option";
export declare class Block1Option extends NumericOption {
}
6 changes: 6 additions & 0 deletions build/BlockwiseOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Option_1 = require("./Option");
class Block1Option extends Option_1.NumericOption {
}
exports.Block1Option = Block1Option;
8 changes: 8 additions & 0 deletions build/Option.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export declare class NumericOption extends Option {
value: number;
static create(code: number, name: string, repeatable: boolean, maxLength: number, rawValue: Buffer): NumericOption;
}
/**
* Specialized Message optionis for blockwise transfer
*/
export declare class BlockOption extends NumericOption {
sizeExponent: number;
isLastBlock: boolean;
blockNumber: number;
}
/**
* Specialized Message options for binary (and empty) content.
*/
Expand Down
36 changes: 34 additions & 2 deletions build/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,38 @@ class NumericOption extends Option {
}
}
exports.NumericOption = NumericOption;
/**
* Specialized Message optionis for blockwise transfer
*/
class BlockOption extends NumericOption {
get sizeExponent() {
return this.value & 0b111;
}
set sizeExponent(value) {
if (value < 0 || value > 6) {
throw new Error("the size exponent must be in the range of 0..6");
}
// overwrite the last 3 bits
this.value = (this.value & ~0b111) | value;
}
get isLastBlock() {
const moreBlocks = (this.value & 0b1000) === 0b1000;
return !moreBlocks;
}
set isLastBlock(value) {
const moreBlocks = !value;
// overwrite the 4th bit
this.value = (this.value & ~0b1000) | (moreBlocks ? 0b1000 : 0);
}
get blockNumber() {
return this.value >>> 4;
}
set blockNumber(value) {
// TODO: check if we need to update the value length
this.value = (value << 4) | (this.value & 0b1111);
}
}
exports.BlockOption = BlockOption;
/**
* Specialized Message options for binary (and empty) content.
*/
Expand Down Expand Up @@ -254,8 +286,8 @@ defineOptionConstructor(NumericOption, 7, "Uri-Port", false, 2);
defineOptionConstructor(NumericOption, 12, "Content-Format", false, 2);
defineOptionConstructor(NumericOption, 14, "Max-Age", false, 4);
defineOptionConstructor(NumericOption, 17, "Accept", false, 2);
defineOptionConstructor(NumericOption, 23, "Block2", false, 3);
defineOptionConstructor(NumericOption, 27, "Block1", false, 3);
defineOptionConstructor(BlockOption, 23, "Block2", false, 3);
defineOptionConstructor(BlockOption, 27, "Block1", false, 3);
defineOptionConstructor(NumericOption, 28, "Size2", false, 4);
defineOptionConstructor(NumericOption, 60, "Size1", false, 4);
defineOptionConstructor(BinaryOption, 1, "If-Match", true, 0, 8);
Expand Down
Empty file.
Empty file.
40 changes: 38 additions & 2 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,42 @@ export class NumericOption extends Option {

}

/**
* Specialized Message optionis for blockwise transfer
*/
export class BlockOption extends NumericOption {

public get sizeExponent(): number {
return this.value & 0b111;
}
public set sizeExponent(value: number) {
if (value < 0 || value > 6) {
throw new Error("the size exponent must be in the range of 0..6");
}
// overwrite the last 3 bits
this.value = (this.value & ~0b111) | value;
}

public get isLastBlock(): boolean {
const moreBlocks = (this.value & 0b1000) === 0b1000;
return !moreBlocks;
}
public set isLastBlock(value: boolean) {
const moreBlocks = !value;
// overwrite the 4th bit
this.value = (this.value & ~0b1000) | (moreBlocks ? 0b1000 : 0);
}

public get blockNumber(): number {
return this.value >>> 4;
}
public set blockNumber(value: number) {
// TODO: check if we need to update the value length
this.value = (value << 4) | (this.value & 0b1111);
}

}

/**
* Specialized Message options for binary (and empty) content.
*/
Expand Down Expand Up @@ -312,8 +348,8 @@ defineOptionConstructor(NumericOption, 7, "Uri-Port", false, 2);
defineOptionConstructor(NumericOption, 12, "Content-Format", false, 2);
defineOptionConstructor(NumericOption, 14, "Max-Age", false, 4);
defineOptionConstructor(NumericOption, 17, "Accept", false, 2);
defineOptionConstructor(NumericOption, 23, "Block2", false, 3);
defineOptionConstructor(NumericOption, 27, "Block1", false, 3);
defineOptionConstructor(BlockOption, 23, "Block2", false, 3);
defineOptionConstructor(BlockOption, 27, "Block1", false, 3);
defineOptionConstructor(NumericOption, 28, "Size2", false, 4);
defineOptionConstructor(NumericOption, 60, "Size1", false, 4);
defineOptionConstructor(BinaryOption, 1, "If-Match", true, 0, 8);
Expand Down

0 comments on commit b5caf75

Please sign in to comment.