Skip to content

Commit

Permalink
fix: some stuff, bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 23, 2024
1 parent 4cf6159 commit ccbb262
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 263 deletions.
1 change: 1 addition & 0 deletions packages/cc/src/cc/NotificationCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,7 @@ export class NotificationCCReport extends NotificationCC {
// Convert CommandClass instances to a standardized object representation
const cc = CommandClass.parse(this.eventParameters, {
...ctx,
frameType: "singlecast",
sourceNodeId: this.nodeId as number,
// Security encapsulation is handled outside of this CC,
// so it is not needed here:
Expand Down
10 changes: 0 additions & 10 deletions packages/cc/src/lib/CommandClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,6 @@ export class CCRaw {
public withPayload(payload: Buffer): CCRaw {
return new CCRaw(this.ccId, this.ccCommand, payload);
}

// FIXME: Use this?
public serialize(): Buffer {
const ccIdLength = this.ccId >= 0xf100 ? 2 : 1;
const data = Buffer.allocUnsafe(ccIdLength + 1 + this.payload.length);
data.writeUIntBE(this.ccId, 0, ccIdLength);
data[ccIdLength] = this.ccCommand ?? 0;
this.payload.copy(data, ccIdLength + 1);
return data;
}
}

// @publicAPI
Expand Down
12 changes: 1 addition & 11 deletions packages/host/src/ZWaveHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ export interface HostIDs {
homeId: number;
}

// FIXME: This should not be needed. Instead have the driver set callback IDs during sendMessage
/** Allows generating a new callback ID */
export interface GetNextCallbackId {
/**
* Returns the next callback ID. Callback IDs are used to correlate requests
* to the controller/nodes with its response
*/
getNextCallbackId(): number;
}

/** Allows querying device configuration for a node */
export interface GetDeviceConfig {
getDeviceConfig(nodeId: number): DeviceConfig | undefined;
Expand Down Expand Up @@ -71,7 +61,7 @@ export interface CCParsingContext
__internalIsMockNode?: boolean;

/** If known, the frame type of the containing message */
frameType?: FrameType;
frameType: FrameType;

getHighestSecurityClass(nodeId: number): MaybeNotKnown<SecurityClass>;

Expand Down
5 changes: 2 additions & 3 deletions packages/maintenance/src/generateTypedDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ async function processCCDocFile(
if (!APIClass) return;

const ccId = getCommandClassFromClassDeclaration(
// FIXME: there seems to be some discrepancy between ts-morph's bundled typescript and our typescript
file.compilerNode as any,
APIClass.compilerNode as any,
file.compilerNode,
APIClass.compilerNode,
);
if (ccId == undefined) return;
const ccName = getCCName(ccId);
Expand Down
148 changes: 0 additions & 148 deletions packages/serial/src/message/Message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,154 +100,6 @@ test("should throw the correct error when parsing a faulty message", (t) => {
}
});

test("isComplete() should work correctly", (t) => {
// actual messages from OZW
const okayMessages = [
Buffer.from([
0x01,
0x09,
0x00,
0x13,
0x03,
0x02,
0x00,
0x00,
0x25,
0x0b,
0xca,
]),
Buffer.from([0x01, 0x05, 0x00, 0x47, 0x04, 0x20, 0x99]),
Buffer.from([0x01, 0x06, 0x00, 0x46, 0x0c, 0x0d, 0x32, 0x8c]),
Buffer.from([
0x01,
0x0a,
0x00,
0x13,
0x03,
0x03,
0x8e,
0x02,
0x04,
0x25,
0x40,
0x0b,
]),
];
for (const msg of okayMessages) {
t.is(Message.isComplete(msg), true); // `${msg.toString("hex")} should be detected as complete`
}

// truncated messages
const truncatedMessages = [
undefined,
Buffer.from([]),
Buffer.from([0x01]),
Buffer.from([0x01, 0x09]),
Buffer.from([0x01, 0x09, 0x00]),
Buffer.from([
0x01,
0x09,
0x00,
0x13,
0x03,
0x02,
0x00,
0x00,
0x25,
0x0b,
]),
];
for (const msg of truncatedMessages) {
t.is(Message.isComplete(msg), false); // `${msg ? msg.toString("hex") : "null"} should be detected as incomplete`
}

// faulty but non-truncated messages should be detected as complete
const faultyMessages = [
Buffer.from([
0x01,
0x09,
0x00,
0x13,
0x03,
0x02,
0x00,
0x00,
0x25,
0x0b,
0xca,
]),
Buffer.from([0x01, 0x05, 0x00, 0x47, 0x04, 0x20, 0x99]),
Buffer.from([0x01, 0x06, 0x00, 0x46, 0x0c, 0x0d, 0x32, 0x8c]),
Buffer.from([
0x01,
0x0a,
0x00,
0x13,
0x03,
0x03,
0x8e,
0x02,
0x04,
0x25,
0x40,
0x0b,
]),
];
for (const msg of faultyMessages) {
t.is(Message.isComplete(msg), true); // `${msg.toString("hex")} should be detected as complete`
}

// actual messages from OZW, appended with some random data
const tooLongMessages = [
Buffer.from([
0x01,
0x09,
0x00,
0x13,
0x03,
0x02,
0x00,
0x00,
0x25,
0x0b,
0xca,
0x00,
]),
Buffer.from([0x01, 0x05, 0x00, 0x47, 0x04, 0x20, 0x99, 0x01, 0x02]),
Buffer.from([
0x01,
0x06,
0x00,
0x46,
0x0c,
0x0d,
0x32,
0x8c,
0xab,
0xcd,
0xef,
]),
Buffer.from([
0x01,
0x0a,
0x00,
0x13,
0x03,
0x03,
0x8e,
0x02,
0x04,
0x25,
0x40,
0x0b,
0x12,
]),
];
for (const msg of tooLongMessages) {
t.is(Message.isComplete(msg), true); // `${msg.toString("hex")} should be detected as complete`
}
});

test("toJSON() should return a semi-readable JSON representation", (t) => {
const msg1 = new Message({
type: MessageType.Request,
Expand Down
47 changes: 2 additions & 45 deletions packages/serial/src/message/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,23 @@ export enum MessageOrigin {
export interface MessageParsingContext extends HostIDs, GetDeviceConfig {
/** How many bytes a node ID occupies in serial API commands */
nodeIdType: NodeIDType;

sdkVersion: string | undefined;

requestStorage: Map<FunctionType, Record<string, unknown>> | undefined;

origin?: MessageOrigin;
}

export interface MessageBaseOptions {
callbackId?: number;
}

export interface MessageCreationOptions extends MessageBaseOptions {
export interface MessageOptions extends MessageBaseOptions {
type?: MessageType;
functionType?: FunctionType;
expectedResponse?: FunctionType | typeof Message | ResponsePredicate;
expectedCallback?: FunctionType | typeof Message | ResponsePredicate;
payload?: Buffer;
}

export type MessageOptions = MessageCreationOptions;

export interface MessageEncodingContext
extends
Readonly<SecurityManagers>,
Expand Down Expand Up @@ -141,29 +136,14 @@ export class MessageRaw {
public withPayload(payload: Buffer): MessageRaw {
return new MessageRaw(this.type, this.functionType, payload);
}

// FIXME: Use this?
public serialize(): Buffer {
const ret = Buffer.allocUnsafe(this.payload.length + 5);
ret[0] = MessageHeaders.SOF;
// length of the following data, including the checksum
ret[1] = this.payload.length + 3;
// write the remaining data
ret[2] = this.type;
ret[3] = this.functionType;
this.payload.copy(ret, 4);
// followed by the checksum
ret[ret.length - 1] = computeChecksum(ret);
return ret;
}
}

/**
* Represents a Z-Wave message for communication with the serial interface
*/
export class Message {
public constructor(
options: MessageCreationOptions = {},
options: MessageOptions = {},
) {
const {
// Try to determine the message type if none is given
Expand Down Expand Up @@ -201,23 +181,12 @@ export class Message {
public static parse(
data: Buffer,
ctx: MessageParsingContext,
// contextStore?: Map<FunctionType, Record<string, unknown>>,
): Message {
const raw = MessageRaw.parse(data);

const Constructor = getMessageConstructor(raw.type, raw.functionType)
?? Message;

// Take the context out of the context store if it exists
// FIXME: Is there a better way to do this?
// if (contextStore) {
// const functionType = getFunctionTypeStatic(Constructor)!;
// if (contextStore.has(functionType)) {
// options.context = contextStore.get(functionType)!;
// contextStore.delete(functionType);
// }
// }

return Constructor.from(raw, ctx);
}

Expand Down Expand Up @@ -302,18 +271,6 @@ export class Message {
return ret;
}

/**
* Checks if there's enough data in the buffer to deserialize
*/
public static isComplete(data?: Buffer): boolean {
if (!data || !data.length || data.length < 5) return false; // not yet

const messageLength = getMessageLength(data);
if (data.length < messageLength) return false; // not yet

return true; // probably, but the checksum may be wrong
}

/** Generates a representation of this Message for the log */
public toLogEntry(): MessageOrCCLogEntry {
const tags = [
Expand Down
1 change: 0 additions & 1 deletion packages/testing/src/MockController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export class MockController {
msg = Message.parse(data, {
...this.parsingContext,
origin: MessageOrigin.Host,
// FIXME: parseCCs: false should be used everywhere
});
this._receivedHostMessages.push(msg);
if (this.autoAckHostMessages) {
Expand Down
Loading

0 comments on commit ccbb262

Please sign in to comment.