From 24d330b18925108ce7ad06d296651b277ee3c767 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sun, 13 Apr 2025 13:43:52 +1200 Subject: [PATCH] add prefs toggle for auto adding or manual adding of new contacts --- index.html | 23 +++++++++++++++++ src/connection/connection.js | 49 +++++++++++++++++++++++++++++++++++- src/constants.js | 5 +++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 9fb08d3..fb178ef 100644 --- a/index.html +++ b/index.html @@ -122,6 +122,12 @@ + + @@ -274,6 +280,7 @@ }, async loadSelfInfo() { this.selfInfo = await this.connection.getSelfInfo(); + console.log(this.selfInfo); }, async loadContacts() { this.contacts = await this.connection.getContacts(); @@ -647,6 +654,22 @@ } }, + async setAutoAddContacts() { + try { + await this.connection.setAutoAddContacts(); + } catch(e) { + console.log(e); + alert("failed to set auto add contacts"); + } + }, + async setManualAddContacts() { + try { + await this.connection.setManualAddContacts(); + } catch(e) { + console.log(e); + alert("failed to set manual add contacts"); + } + }, showCommandLine(contact) { // hide cli if clicked same contact diff --git a/src/connection/connection.js b/src/connection/connection.js index 3ccd588..30937f3 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -268,6 +268,13 @@ class Connection extends EventEmitter { await this.sendToRadioFrame(data.toBytes()); } + async sendCommandSetOtherParams(manualAddContacts) { + const data = new BufferWriter(); + data.writeByte(Constants.CommandCodes.SetOtherParams); + data.writeByte(manualAddContacts); // 0 or 1 + await this.sendToRadioFrame(data.toBytes()); + } + onFrameReceived(frame) { // emit received frame @@ -513,7 +520,8 @@ class Connection extends EventEmitter { publicKey: bufferReader.readBytes(32), advLat: bufferReader.readInt32LE(), advLon: bufferReader.readInt32LE(), - reserved: bufferReader.readBytes(4), + reserved: bufferReader.readBytes(3), + manualAddContacts: bufferReader.readByte(), radioFreq: bufferReader.readUInt32LE(), radioBw: bufferReader.readUInt32LE(), radioSf: bufferReader.readByte(), @@ -1760,6 +1768,45 @@ class Connection extends EventEmitter { }); } + setOtherParams(manualAddContacts) { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive ok + const onOk = () => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(); + } + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // listen for events + this.once(Constants.ResponseCodes.Ok, onOk); + this.once(Constants.ResponseCodes.Err, onErr); + + // set other params + await this.sendCommandSetOtherParams(manualAddContacts); + + } catch(e) { + reject(e); + } + }); + } + + async setAutoAddContacts() { + return await this.setOtherParams(false); + } + + async setManualAddContacts() { + return await this.setOtherParams(true); + } + } export default Connection; diff --git a/src/constants.js b/src/constants.js index ea8b1a0..46121e1 100644 --- a/src/constants.js +++ b/src/constants.js @@ -45,6 +45,8 @@ class Constants { SetChannel: 32, // todo sign commands SendTracePath: 36, + // todo set device pin command + SetOtherParams: 38, } static ResponseCodes = { @@ -68,7 +70,7 @@ class Constants { } static PushCodes = { - Advert: 0x80, + Advert: 0x80, // when companion is set to auto add contacts PathUpdated: 0x81, SendConfirmed: 0x82, MsgWaiting: 0x83, @@ -78,6 +80,7 @@ class Constants { StatusResponse: 0x87, LogRxData: 0x88, TraceData: 0x89, + NewAdvert: 0x8A, // when companion is set to manually add contacts } static AdvType = {