mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
implement ble connection and tidy up classes
This commit is contained in:
parent
6a3e522106
commit
8f53e9b849
5 changed files with 301 additions and 175 deletions
95
src/connection/ble_connection.js
Normal file
95
src/connection/ble_connection.js
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
import Constants from "../constants.js";
|
||||
import Connection from "./connection.js";
|
||||
|
||||
class BleConnection extends Connection {
|
||||
|
||||
constructor(bleDevice) {
|
||||
super();
|
||||
this.bleDevice = bleDevice;
|
||||
this.gattServer = null;
|
||||
this.rxCharacteristic = null;
|
||||
this.txCharacteristic = null;
|
||||
this.init();
|
||||
}
|
||||
|
||||
static async open() {
|
||||
|
||||
// ensure browser supports web bluetooth
|
||||
if(!navigator.bluetooth){
|
||||
alert("Web Bluetooth is not supported in this browser");
|
||||
return;
|
||||
}
|
||||
|
||||
// ask user to select device
|
||||
const device = await navigator.bluetooth.requestDevice({
|
||||
filters: [
|
||||
{
|
||||
services: [
|
||||
Constants.Ble.ServiceUuid.toLowerCase(),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// make sure user selected a device
|
||||
if(!device){
|
||||
return null;
|
||||
}
|
||||
|
||||
return new BleConnection(device);
|
||||
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
||||
// connect to gatt server
|
||||
this.gattServer = await this.bleDevice.gatt.connect();
|
||||
|
||||
// find service
|
||||
const service = await this.gattServer.getPrimaryService(Constants.Ble.ServiceUuid.toLowerCase());
|
||||
const characteristics = await service.getCharacteristics();
|
||||
|
||||
// find rx characteristic (we write to this one, it's where the radio reads from)
|
||||
this.rxCharacteristic = characteristics.find((characteristic) => {
|
||||
return characteristic.uuid.toLowerCase() === Constants.Ble.CharacteristicUuidRx.toLowerCase();
|
||||
});
|
||||
|
||||
// find tx characteristic (we read this one, it's where the radio writes to)
|
||||
this.txCharacteristic = characteristics.find((characteristic) => {
|
||||
return characteristic.uuid.toLowerCase() === Constants.Ble.CharacteristicUuidTx.toLowerCase();
|
||||
});
|
||||
|
||||
// listen for frames from transmitted to us from the ble device
|
||||
await this.txCharacteristic.startNotifications();
|
||||
this.txCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
|
||||
const frame = new Uint8Array(event.target.value.buffer);
|
||||
this.onFrameReceived(frame);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async close() {
|
||||
try {
|
||||
this.gattServer?.disconnect();
|
||||
this.gattServer = null;
|
||||
} catch(e) {
|
||||
// ignore error when disconnecting
|
||||
}
|
||||
}
|
||||
|
||||
async write(bytes) {
|
||||
try {
|
||||
// we write to the rx characteristic, as that's where the radio reads from
|
||||
await this.rxCharacteristic.writeValue(bytes);
|
||||
} catch(e) {
|
||||
console.log("failed to write to ble device", e);
|
||||
}
|
||||
}
|
||||
|
||||
async sendToRadioFrame(data) {
|
||||
await this.write(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BleConnection;
|
||||
259
src/connection/connection.js
Normal file
259
src/connection/connection.js
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
import BufferWriter from "../buffer_writer.js";
|
||||
import BufferReader from "../buffer_reader.js";
|
||||
import Constants from "../constants.js";
|
||||
|
||||
class Connection {
|
||||
|
||||
async close() {
|
||||
throw new Error("This method must be implemented by the subclass.");
|
||||
}
|
||||
|
||||
async sendToRadioFrame(data) {
|
||||
throw new Error("This method must be implemented by the subclass.");
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
data.writeByte(outPathLen);
|
||||
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());
|
||||
}
|
||||
|
||||
async sendCommandSyncNextMessage() {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.SyncNextMessage);
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
async sendCommandResetPath(pubKey) {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.ResetPath);
|
||||
data.writeBytes(pubKey); // 32 bytes
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
async sendCommandSetAdvertLatLon(lat, lon) {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.SetAdvertLatLon);
|
||||
data.writeUInt32LE(lat);
|
||||
data.writeUInt32LE(lon);
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
async sendCommandRemoveContact(pubKey) {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.RemoveContact);
|
||||
data.writeBytes(pubKey); // 32 bytes
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
onFrameReceived(frame) {
|
||||
|
||||
// console.log("onFrameReceived", frame);
|
||||
|
||||
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);
|
||||
} else if(responseCode === Constants.ResponseCodes.NoMoreMessages){
|
||||
this.onNoMoreMessagesResponse(bufferReader);
|
||||
} 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);
|
||||
} else if(responseCode === Constants.ResponseCodes.Sent){
|
||||
this.onSentResponse(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.Advert){
|
||||
this.onAdvertPush(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.SendConfirmed){
|
||||
this.onSendConfirmedPush(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.MsgWaiting){
|
||||
this.onMsgWaitingPush(bufferReader);
|
||||
} else {
|
||||
console.log("unhandled frame", frame);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onAdvertPush(bufferReader) {
|
||||
console.log("onAdvertPush", {
|
||||
publicKey: bufferReader.readBytes(32),
|
||||
});
|
||||
}
|
||||
|
||||
onSendConfirmedPush(bufferReader) {
|
||||
console.log("onSendConfirmedPush", {
|
||||
ackCode: bufferReader.readBytes(4),
|
||||
roundTrip: bufferReader.readUInt32LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onMsgWaitingPush(bufferReader) {
|
||||
console.log("onMsgWaitingPush", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
onContactsStartResponse(bufferReader) {
|
||||
console.log("onContactsStartResponse", {
|
||||
count: bufferReader.readUInt32LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onContactResponse(bufferReader) {
|
||||
console.log("onContactResponse", {
|
||||
publicKey: bufferReader.readBytes(32),
|
||||
type: bufferReader.readByte(),
|
||||
flags: bufferReader.readByte(),
|
||||
outPathLen: bufferReader.readByte(),
|
||||
outPath: bufferReader.readBytes(64),
|
||||
advName: bufferReader.readCString(32),
|
||||
lastAdvert: bufferReader.readUInt32LE(),
|
||||
advLat: bufferReader.readUInt32LE(),
|
||||
advLon: bufferReader.readUInt32LE(),
|
||||
lastMod: bufferReader.readUInt32LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onEndOfContactsResponse(bufferReader) {
|
||||
console.log("onEndOfContactsResponse", {
|
||||
mostRecentLastmod: bufferReader.readUInt32LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onSentResponse(bufferReader) {
|
||||
console.log("onSentResponse", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
onSelfInfoResponse(bufferReader) {
|
||||
console.log("onSelfInfoResponse", {
|
||||
type: bufferReader.readByte(),
|
||||
txPower: bufferReader.readByte(),
|
||||
maxTxPower: bufferReader.readByte(),
|
||||
publicKey: bufferReader.readBytes(32),
|
||||
advLat: bufferReader.readUInt32LE(),
|
||||
advLon: bufferReader.readUInt32LE(),
|
||||
reserved: bufferReader.readBytes(4),
|
||||
radioFreq: bufferReader.readUInt32LE(),
|
||||
radioBw: bufferReader.readUInt32LE(),
|
||||
radioSf: bufferReader.readByte(),
|
||||
radioCr: bufferReader.readByte(),
|
||||
name: bufferReader.readString(),
|
||||
});
|
||||
}
|
||||
|
||||
onCurrTimeResponse(bufferReader) {
|
||||
console.log("onCurrTimeResponse", {
|
||||
epochSecs: bufferReader.readUInt32LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onNoMoreMessagesResponse(bufferReader) {
|
||||
console.log("onNoMoreMessagesResponse", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
onContactMsgRecvResponse(bufferReader) {
|
||||
console.log("onContactMsgRecvResponse", {
|
||||
pubKeyPrefix: bufferReader.readBytes(6),
|
||||
pathLen: bufferReader.readByte(),
|
||||
txtType: bufferReader.readByte(),
|
||||
senderTimestamp: bufferReader.readUInt32LE(),
|
||||
text: bufferReader.readString(),
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
165
src/connection/serial_connection.js
Normal file
165
src/connection/serial_connection.js
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
import BufferWriter from "../buffer_writer.js";
|
||||
import BufferReader from "../buffer_reader.js";
|
||||
import Constants from "../constants.js";
|
||||
import Connection from "./connection.js";
|
||||
|
||||
class SerialConnection extends Connection {
|
||||
|
||||
constructor(serialPort) {
|
||||
super();
|
||||
this.serialPort = serialPort;
|
||||
this.reader = serialPort.readable.getReader();
|
||||
this.writable = serialPort.writable;
|
||||
this.readBuffer = [];
|
||||
this.readLoop();
|
||||
}
|
||||
|
||||
static async open() {
|
||||
|
||||
// ensure browser supports web serial
|
||||
if(!navigator.serial){
|
||||
alert("Web Serial is not supported in this browser");
|
||||
return null;
|
||||
}
|
||||
|
||||
// ask user to select device
|
||||
const serialPort = await navigator.serial.requestPort({
|
||||
filters: [],
|
||||
});
|
||||
|
||||
// open port
|
||||
await serialPort.open({
|
||||
baudRate: 115200,
|
||||
});
|
||||
|
||||
return new SerialConnection(serialPort);
|
||||
|
||||
}
|
||||
|
||||
async close() {
|
||||
|
||||
// release reader lock
|
||||
try {
|
||||
this.reader.releaseLock();
|
||||
} catch(e) {
|
||||
// console.log("failed to release lock on serial port readable, ignoring...", e);
|
||||
}
|
||||
|
||||
// close serial port
|
||||
try {
|
||||
await this.serialPort.close();
|
||||
} catch(e) {
|
||||
// console.log("failed to close serial port, ignoring...", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async write(bytes) {
|
||||
const writer = this.writable.getWriter();
|
||||
try {
|
||||
await writer.write(new Uint8Array(bytes));
|
||||
} finally {
|
||||
writer.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
async writeFrame(frameType, frameData) {
|
||||
|
||||
// create frame
|
||||
const frame = new BufferWriter();
|
||||
|
||||
// add frame header
|
||||
frame.writeByte(frameType);
|
||||
frame.writeUInt16LE(frameData.length);
|
||||
|
||||
// add frame data
|
||||
frame.writeBytes(frameData);
|
||||
|
||||
// write frame to device
|
||||
await this.write(frame.toBytes());
|
||||
|
||||
}
|
||||
|
||||
async sendToRadioFrame(data) {
|
||||
// write "app to radio" frame 0x3c "<"
|
||||
await this.writeFrame(0x3c, data);
|
||||
}
|
||||
|
||||
async readLoop() {
|
||||
try {
|
||||
while(true){
|
||||
|
||||
// read bytes until reader indicates it's done
|
||||
const { value, done } = await this.reader.read();
|
||||
if(done){
|
||||
break;
|
||||
}
|
||||
|
||||
// append received bytes to read buffer
|
||||
this.readBuffer = [
|
||||
...this.readBuffer,
|
||||
...value,
|
||||
];
|
||||
|
||||
// process read buffer while there is enough bytes for a frame header
|
||||
// 3 bytes frame header = (1 byte frame type) + (2 bytes frame length as unsigned 16-bit little endian)
|
||||
const frameHeaderLength = 3;
|
||||
while(this.readBuffer.length >= frameHeaderLength){
|
||||
try {
|
||||
|
||||
// extract frame header
|
||||
const frameHeader = new BufferReader(this.readBuffer.slice(0, frameHeaderLength));
|
||||
|
||||
// ensure frame type supported
|
||||
const frameType = frameHeader.readByte();
|
||||
if(frameType !== Constants.SerialFrameTypes.Incoming && frameType !== Constants.SerialFrameTypes.Outgoing){
|
||||
// unexpected byte, lets skip it and try again
|
||||
this.readBuffer = this.readBuffer.slice(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// ensure frame length valid
|
||||
const frameLength = frameHeader.readUInt16LE();
|
||||
if(!frameLength){
|
||||
// unexpected byte, lets skip it and try again
|
||||
this.readBuffer = this.readBuffer.slice(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// check if we have received enough bytes for this frame, otherwise wait until more bytes received
|
||||
const requiredLength = frameHeaderLength + frameLength;
|
||||
if(this.readBuffer.length < requiredLength){
|
||||
break;
|
||||
}
|
||||
|
||||
// get frame data, and remove it and its frame header from the read buffer
|
||||
const frameData = this.readBuffer.slice(frameHeaderLength, requiredLength);
|
||||
this.readBuffer = this.readBuffer.slice(requiredLength);
|
||||
|
||||
// handle received frame
|
||||
this.onFrameReceived(frameData);
|
||||
|
||||
} catch(e) {
|
||||
console.error("Failed to process frame", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} catch(error) {
|
||||
|
||||
// ignore error if reader was released
|
||||
if(error instanceof TypeError){
|
||||
return;
|
||||
}
|
||||
|
||||
console.error('Error reading from serial port: ', error);
|
||||
|
||||
} finally {
|
||||
this.reader.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default SerialConnection;
|
||||
Loading…
Add table
Add a link
Reference in a new issue