Skip to content

Commit

Permalink
Merge pull request #24 from SlimeVR/feat/tracker-emulation/user-actions
Browse files Browse the repository at this point in the history
fix(firmware-protocol): Add missing user action packet
  • Loading branch information
TheDevMinerTV authored Jun 3, 2024
2 parents 4129f62 + 8b9f905 commit 8c3f7e4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-cherries-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/firmware-protocol': patch
---

added missing UserAction (21) packet
5 changes: 5 additions & 0 deletions .changeset/wise-hornets-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/tracker-emulation': patch
---

add the ability to send the UserAction (21) packet
4 changes: 4 additions & 0 deletions packages/firmware-protocol/src/packets/PacketParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ServerBoundSensorInfoPacket } from './ServerBoundSensorInfoPacket';
import { ServerBoundSignalStrengthPacket } from './ServerBoundSignalStrengthPacket';
import { ServerBoundTapPacket } from './ServerBoundTapPacket';
import { ServerBoundTemperaturePacket } from './ServerBoundTemperaturePacket';
import { ServerBoundUserActionPacket } from './ServerBoundUserActionPacket';
import { InspectionPacketParser } from './inspection/PacketParser';

const bundle = (num: bigint, packet: Packet | null) => [num, packet] as const;
Expand Down Expand Up @@ -118,6 +119,9 @@ export const parse = (data: Buffer, isDeviceBound: boolean, isInBundle = false)
case ServerBoundTemperaturePacket.type:
return bundle(num, ServerBoundTemperaturePacket.fromBuffer(data));

case ServerBoundUserActionPacket.type:
return bundle(num, ServerBoundUserActionPacket.fromBuffer(data));

case InspectionPacketParser.type:
return bundle(num, InspectionPacketParser.parseRawDataPacket(data));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Packet } from './Packet';

export const UserAction = {
RESET_FULL: 2,
RESET_YAW: 3,
RESET_MOUNTING: 4,
PAUSE_TRACKING: 5
} as const;
export type UserAction = (typeof UserAction)[keyof typeof UserAction];

export class ServerBoundUserActionPacket extends Packet {
constructor(readonly action: number) {
super(ServerBoundUserActionPacket.type);
}

static fromBuffer(data: Buffer) {
const action = data.readUIntBE(0, 1) & (0xff as UserAction);

return new ServerBoundUserActionPacket(action);
}

static get type() {
return 21;
}

override toString() {
return `ServerBoundUserActionPacket{action: ${this.action}}`;
}

encode(num: bigint): Buffer {
const buf = Buffer.alloc(4 + 8 + 1);

buf.writeInt32BE(ServerBoundUserActionPacket.type, 0);
buf.writeBigInt64BE(num, 4);

buf.writeInt8(this.action, 12);

return buf;
}
}
3 changes: 2 additions & 1 deletion packages/firmware-protocol/src/packets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './DeviceBoundHandshakePacket';
export * from './DeviceBoundHeartbeatPacket';
export * from './DeviceBoundPingPacket';
export * from './DeviceBoundSensorInfoPacket';
export * from './inspection';
export * from './Packet';
export * from './PacketParser';
export * from './ServerBoundAccelPacket';
Expand All @@ -23,4 +24,4 @@ export * from './ServerBoundSensorInfoPacket';
export * from './ServerBoundSignalStrengthPacket';
export * from './ServerBoundTapPacket';
export * from './ServerBoundTemperaturePacket';
export * from './inspection';
export * from './ServerBoundUserActionPacket';
8 changes: 7 additions & 1 deletion packages/tracker-emulation/src/EmulatedTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
ServerBoundRotationDataPacket,
ServerBoundSignalStrengthPacket,
ServerBoundTemperaturePacket,
ServerFeatureFlags
ServerBoundUserActionPacket,
ServerFeatureFlags,
UserAction
} from '@slimevr/firmware-protocol';
import { createSocket, RemoteInfo, Socket } from 'dgram';
import EventEmitter from 'events';
Expand Down Expand Up @@ -166,6 +168,10 @@ export class EmulatedTracker extends (EventEmitter as {
await this.sendPacketToServer(new ServerBoundSignalStrengthPacket(sensorId, signalStrength));
}

async sendUserAction(action: UserAction) {
await this.sendPacketToServer(new ServerBoundUserActionPacket(action));
}

async sendPacketToServer(packet: Packet) {
if (this.state.status !== 'connected-to-server') return;

Expand Down

0 comments on commit 8c3f7e4

Please sign in to comment.