2025-02-11 21:39:18 +13:00
|
|
|
import BufferWriter from "../buffer_writer.js";
|
|
|
|
|
import BufferReader from "../buffer_reader.js";
|
|
|
|
|
import Constants from "../constants.js";
|
2025-02-11 22:07:10 +13:00
|
|
|
import EventEmitter from "../events.js";
|
2025-02-11 07:27:31 +13:00
|
|
|
|
2025-02-11 22:07:10 +13:00
|
|
|
class Connection extends EventEmitter {
|
2025-02-11 07:27:31 +13:00
|
|
|
|
2025-02-11 23:30:54 +13:00
|
|
|
onConnected() {
|
|
|
|
|
this.emit("connected");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 23:42:58 +13:00
|
|
|
onDisconnected() {
|
|
|
|
|
this.emit("disconnected");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
async close() {
|
2025-02-11 21:39:18 +13:00
|
|
|
throw new Error("This method must be implemented by the subclass.");
|
2025-02-11 07:27:31 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendToRadioFrame(data) {
|
2025-02-11 21:39:18 +13:00
|
|
|
throw new Error("This method must be implemented by the subclass.");
|
2025-02-11 07:27:31 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendCommandAppStart() {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.AppStart);
|
|
|
|
|
data.writeByte(1); // appVer
|
|
|
|
|
data.writeBytes(new Uint8Array(6)); // reserved
|
|
|
|
|
data.writeString("test"); // appName
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:31:28 +13:00
|
|
|
async sendCommandSendTxtMsg(txtType, attempt, senderTimestamp, pubKeyPrefix, text) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SendTxtMsg);
|
|
|
|
|
data.writeByte(txtType);
|
|
|
|
|
data.writeByte(attempt);
|
|
|
|
|
data.writeUInt32LE(senderTimestamp);
|
|
|
|
|
data.writeBytes(pubKeyPrefix.slice(0, 6)); // only the first 6 bytes of pubKey are sent
|
|
|
|
|
data.writeString(text);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
async sendCommandGetContacts(since) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.GetContacts);
|
|
|
|
|
if(since){
|
|
|
|
|
data.writeUInt32LE(since);
|
|
|
|
|
}
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendCommandGetDeviceTime() {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.GetDeviceTime);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendCommandSetDeviceTime(epochSecs) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SetDeviceTime);
|
|
|
|
|
data.writeUInt32LE(epochSecs);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendCommandSendSelfAdvert(type) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SendSelfAdvert);
|
|
|
|
|
data.writeByte(type);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendCommandSetAdvertName(name) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SetAdvertName);
|
|
|
|
|
data.writeString(name);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:31:28 +13:00
|
|
|
async sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.AddUpdateContact);
|
|
|
|
|
data.writeBytes(publicKey);
|
|
|
|
|
data.writeByte(type);
|
|
|
|
|
data.writeByte(flags);
|
2025-02-12 01:10:17 +13:00
|
|
|
data.writeByte(outPathLen); // todo writeInt8
|
2025-02-11 17:31:28 +13:00
|
|
|
data.writeBytes(outPath); // 64 bytes
|
|
|
|
|
data.writeCString(advName, 32); // 32 bytes
|
|
|
|
|
data.writeUInt32LE(lastAdvert);
|
|
|
|
|
data.writeUInt32LE(advLat);
|
|
|
|
|
data.writeUInt32LE(advLon);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
async sendCommandSyncNextMessage() {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SyncNextMessage);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:31:28 +13:00
|
|
|
async sendCommandSetRadioParams(radioFreq, radioBw, radioSf, radioCr) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SetRadioParams);
|
|
|
|
|
data.writeUInt32LE(radioFreq);
|
|
|
|
|
data.writeUInt32LE(radioBw);
|
|
|
|
|
data.writeByte(radioSf);
|
|
|
|
|
data.writeByte(radioCr);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendCommandSetTxPower(txPower) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SetTxPower);
|
|
|
|
|
data.writeByte(txPower);
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 18:01:22 +13:00
|
|
|
async sendCommandResetPath(pubKey) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.ResetPath);
|
|
|
|
|
data.writeBytes(pubKey); // 32 bytes
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:45:53 +13:00
|
|
|
async sendCommandSetAdvertLatLon(lat, lon) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.SetAdvertLatLon);
|
2025-02-11 22:51:18 +13:00
|
|
|
data.writeInt32LE(lat);
|
|
|
|
|
data.writeInt32LE(lon);
|
2025-02-11 17:45:53 +13:00
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:51:18 +13:00
|
|
|
async sendCommandRemoveContact(pubKey) {
|
|
|
|
|
const data = new BufferWriter();
|
|
|
|
|
data.writeByte(Constants.CommandCodes.RemoveContact);
|
|
|
|
|
data.writeBytes(pubKey); // 32 bytes
|
|
|
|
|
await this.sendToRadioFrame(data.toBytes());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
onFrameReceived(frame) {
|
|
|
|
|
|
2025-02-11 22:07:10 +13:00
|
|
|
// emit received frame
|
|
|
|
|
this.emit("rx", frame);
|
2025-02-11 07:27:31 +13:00
|
|
|
|
|
|
|
|
const bufferReader = new BufferReader(frame);
|
|
|
|
|
const responseCode = bufferReader.readByte();
|
|
|
|
|
|
|
|
|
|
if(responseCode === Constants.ResponseCodes.SelfInfo){
|
|
|
|
|
this.onSelfInfoResponse(bufferReader);
|
|
|
|
|
} else if(responseCode === Constants.ResponseCodes.CurrTime){
|
|
|
|
|
this.onCurrTimeResponse(bufferReader);
|
2025-02-11 17:31:28 +13:00
|
|
|
} else if(responseCode === Constants.ResponseCodes.NoMoreMessages){
|
|
|
|
|
this.onNoMoreMessagesResponse(bufferReader);
|
2025-02-11 07:27:31 +13:00
|
|
|
} else if(responseCode === Constants.ResponseCodes.ContactMsgRecv){
|
|
|
|
|
this.onContactMsgRecvResponse(bufferReader);
|
|
|
|
|
} else if(responseCode === Constants.ResponseCodes.ContactsStart){
|
|
|
|
|
this.onContactsStartResponse(bufferReader);
|
|
|
|
|
} else if(responseCode === Constants.ResponseCodes.Contact){
|
|
|
|
|
this.onContactResponse(bufferReader);
|
|
|
|
|
} else if(responseCode === Constants.ResponseCodes.EndOfContacts){
|
|
|
|
|
this.onEndOfContactsResponse(bufferReader);
|
2025-02-11 17:31:28 +13:00
|
|
|
} else if(responseCode === Constants.ResponseCodes.Sent){
|
|
|
|
|
this.onSentResponse(bufferReader);
|
2025-02-11 07:27:31 +13:00
|
|
|
} else if(responseCode === Constants.PushCodes.Advert){
|
|
|
|
|
this.onAdvertPush(bufferReader);
|
2025-02-12 00:48:02 +13:00
|
|
|
} else if(responseCode === Constants.PushCodes.PathUpdated){
|
|
|
|
|
this.onPathUpdatedPush(bufferReader);
|
2025-02-11 17:31:28 +13:00
|
|
|
} else if(responseCode === Constants.PushCodes.SendConfirmed){
|
|
|
|
|
this.onSendConfirmedPush(bufferReader);
|
2025-02-11 07:27:31 +13:00
|
|
|
} else if(responseCode === Constants.PushCodes.MsgWaiting){
|
|
|
|
|
this.onMsgWaitingPush(bufferReader);
|
|
|
|
|
} else {
|
|
|
|
|
console.log("unhandled frame", frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onAdvertPush(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.PushCodes.Advert, {
|
2025-02-11 07:27:31 +13:00
|
|
|
publicKey: bufferReader.readBytes(32),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 00:48:02 +13:00
|
|
|
onPathUpdatedPush(bufferReader) {
|
|
|
|
|
this.emit(Constants.PushCodes.PathUpdated, {
|
|
|
|
|
publicKey: bufferReader.readBytes(32),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:31:28 +13:00
|
|
|
onSendConfirmedPush(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.PushCodes.SendConfirmed, {
|
2025-02-11 17:31:28 +13:00
|
|
|
ackCode: bufferReader.readBytes(4),
|
|
|
|
|
roundTrip: bufferReader.readUInt32LE(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
onMsgWaitingPush(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.PushCodes.MsgWaiting, {
|
2025-02-11 07:27:31 +13:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onContactsStartResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.ContactsStart, {
|
2025-02-11 07:27:31 +13:00
|
|
|
count: bufferReader.readUInt32LE(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onContactResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.Contact, {
|
2025-02-11 07:27:31 +13:00
|
|
|
publicKey: bufferReader.readBytes(32),
|
|
|
|
|
type: bufferReader.readByte(),
|
|
|
|
|
flags: bufferReader.readByte(),
|
2025-02-12 01:10:17 +13:00
|
|
|
outPathLen: bufferReader.readInt8(),
|
2025-02-11 07:27:31 +13:00
|
|
|
outPath: bufferReader.readBytes(64),
|
|
|
|
|
advName: bufferReader.readCString(32),
|
|
|
|
|
lastAdvert: bufferReader.readUInt32LE(),
|
|
|
|
|
advLat: bufferReader.readUInt32LE(),
|
|
|
|
|
advLon: bufferReader.readUInt32LE(),
|
|
|
|
|
lastMod: bufferReader.readUInt32LE(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onEndOfContactsResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.EndOfContacts, {
|
2025-02-11 07:27:31 +13:00
|
|
|
mostRecentLastmod: bufferReader.readUInt32LE(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:31:28 +13:00
|
|
|
onSentResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.Sent, {
|
2025-02-12 23:00:07 +13:00
|
|
|
expectedAckCrc: bufferReader.readUInt32LE(),
|
|
|
|
|
estTimeout: bufferReader.readUInt32LE(),
|
2025-02-11 17:31:28 +13:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
onSelfInfoResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.SelfInfo, {
|
2025-02-11 07:27:31 +13:00
|
|
|
type: bufferReader.readByte(),
|
|
|
|
|
txPower: bufferReader.readByte(),
|
|
|
|
|
maxTxPower: bufferReader.readByte(),
|
|
|
|
|
publicKey: bufferReader.readBytes(32),
|
2025-02-11 22:51:18 +13:00
|
|
|
advLat: bufferReader.readInt32LE(),
|
|
|
|
|
advLon: bufferReader.readInt32LE(),
|
2025-02-11 18:09:38 +13:00
|
|
|
reserved: bufferReader.readBytes(4),
|
2025-02-11 07:27:31 +13:00
|
|
|
radioFreq: bufferReader.readUInt32LE(),
|
|
|
|
|
radioBw: bufferReader.readUInt32LE(),
|
|
|
|
|
radioSf: bufferReader.readByte(),
|
|
|
|
|
radioCr: bufferReader.readByte(),
|
|
|
|
|
name: bufferReader.readString(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onCurrTimeResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.CurrTime, {
|
2025-02-11 07:27:31 +13:00
|
|
|
epochSecs: bufferReader.readUInt32LE(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:31:28 +13:00
|
|
|
onNoMoreMessagesResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.NoMoreMessages, {
|
2025-02-11 17:31:28 +13:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
onContactMsgRecvResponse(bufferReader) {
|
2025-02-11 23:30:54 +13:00
|
|
|
this.emit(Constants.ResponseCodes.ContactMsgRecv, {
|
2025-02-11 07:27:31 +13:00
|
|
|
pubKeyPrefix: bufferReader.readBytes(6),
|
|
|
|
|
pathLen: bufferReader.readByte(),
|
|
|
|
|
txtType: bufferReader.readByte(),
|
|
|
|
|
senderTimestamp: bufferReader.readUInt32LE(),
|
|
|
|
|
text: bufferReader.readString(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 23:30:54 +13:00
|
|
|
getSelfInfo() {
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
|
|
|
|
|
// listen for response
|
|
|
|
|
this.once(Constants.ResponseCodes.SelfInfo, (selfInfo) => {
|
|
|
|
|
resolve(selfInfo);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// request self info
|
|
|
|
|
await this.sendCommandAppStart();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getContacts() {
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
|
|
|
|
|
// add contacts we receive to a list
|
|
|
|
|
const contacts = [];
|
|
|
|
|
const onContactReceived = (contact) => {
|
|
|
|
|
contacts.push(contact);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// listen for contacts
|
|
|
|
|
this.on(Constants.ResponseCodes.Contact, onContactReceived);
|
|
|
|
|
|
|
|
|
|
// there's no more contacts to receive, stop listening and resolve the promise
|
|
|
|
|
this.once(Constants.ResponseCodes.EndOfContacts, () => {
|
|
|
|
|
this.off(Constants.ResponseCodes.Contact, onContactReceived);
|
|
|
|
|
resolve(contacts);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// request contacts from device
|
|
|
|
|
await this.sendCommandGetContacts();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 23:00:07 +13:00
|
|
|
sendTextMessage(contactPublicKey, text) {
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// resolve with the first sent response
|
|
|
|
|
this.once(Constants.ResponseCodes.Sent, (response) => {
|
|
|
|
|
resolve(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// compose message
|
|
|
|
|
const txtType = Constants.TxtTypes.Plain;
|
|
|
|
|
const attempt = 0;
|
|
|
|
|
const senderTimestamp = Math.floor(Date.now() / 1000);
|
|
|
|
|
|
|
|
|
|
// send message
|
|
|
|
|
await this.sendCommandSendTxtMsg(txtType, attempt, senderTimestamp, contactPublicKey, text);
|
|
|
|
|
|
|
|
|
|
} catch(e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 07:27:31 +13:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 21:39:18 +13:00
|
|
|
export default Connection;
|