mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
implement additional packet types
This commit is contained in:
parent
f2e56f3924
commit
581e159f0d
4 changed files with 159 additions and 13 deletions
52
index.html
52
index.html
|
|
@ -30,16 +30,19 @@
|
|||
Disconnect
|
||||
</button>
|
||||
<button @click="sendCommandAppStart" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
Send AppStart
|
||||
AppStart
|
||||
</button>
|
||||
<button @click="sendSendSelfAdvert(0)" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
Send Advert (Zero Hop)
|
||||
SendSelfAdvert(ZeroHop)
|
||||
</button>
|
||||
<button @click="sendSendSelfAdvert(1)" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
Send Advert (Flood)
|
||||
SendSelfAdvert(Flood)
|
||||
</button>
|
||||
<button @click="sendCommandSetAdvertName" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
Set Advert Name
|
||||
SetAdvertName
|
||||
</button>
|
||||
<button @click="sendCommandAddUpdateContact" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
AddUpdateContact
|
||||
</button>
|
||||
<button @click="sendCommandSyncNextMessage" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
SyncNextMessage
|
||||
|
|
@ -53,6 +56,15 @@
|
|||
<button @click="sendCommandSetDeviceTime" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
SetDeviceTime
|
||||
</button>
|
||||
<button @click="sendCommandSetRadioParams" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
SetRadioParams
|
||||
</button>
|
||||
<button @click="sendCommandSetTxPower" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
SetTxPower
|
||||
</button>
|
||||
<button @click="sendCommandSendTxtMsg" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
SendTxtMsg
|
||||
</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
|
@ -61,6 +73,7 @@
|
|||
|
||||
<script type="module">
|
||||
import Device from "./src/device.js";
|
||||
import Constants from "./src/constants.js";
|
||||
Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
|
|
@ -95,6 +108,14 @@
|
|||
async sendCommandAppStart() {
|
||||
await this.device.sendCommandAppStart();
|
||||
},
|
||||
async sendCommandSendTxtMsg() {
|
||||
const txtType = Constants.TxtTypes.Plain;
|
||||
const attempt = 0;
|
||||
const senderTimestamp = Math.floor(Date.now() / 1000);
|
||||
const pubKeyPrefix = new Uint8Array([148, 63, 175, 162, 88, 212, 192, 40, 214, 185, 213, 140, 42, 145, 194, 186, 70, 71, 112, 68, 0, 192, 65, 4, 105, 143, 230, 50, 162, 79, 247, 192]);
|
||||
const text = `Test Message: ${senderTimestamp}`;
|
||||
await this.device.sendCommandSendTxtMsg(txtType, attempt, senderTimestamp, pubKeyPrefix, text);
|
||||
},
|
||||
async sendSendSelfAdvert(type) {
|
||||
await this.device.sendCommandSendSelfAdvert(type);
|
||||
},
|
||||
|
|
@ -108,6 +129,17 @@
|
|||
const timestamp = Math.floor(Date.now() / 1000);
|
||||
await this.device.sendCommandSetDeviceTime(timestamp);
|
||||
},
|
||||
async sendCommandSetTxPower() {
|
||||
const txPower = 22;
|
||||
await this.device.sendCommandSetTxPower(txPower);
|
||||
},
|
||||
async sendCommandSetRadioParams() {
|
||||
const radioFreq = 917375;
|
||||
const radioBw = 250000;
|
||||
const radioSf = 7;
|
||||
const radioCr = 5;
|
||||
await this.device.sendCommandSetRadioParams(radioFreq, radioBw, radioSf, radioCr);
|
||||
},
|
||||
async sendCommandSetAdvertName() {
|
||||
|
||||
// ask user for name
|
||||
|
|
@ -120,6 +152,18 @@
|
|||
await this.device.sendCommandSetAdvertName(name);
|
||||
|
||||
},
|
||||
async sendCommandAddUpdateContact() {
|
||||
const publicKey = new Uint8Array([148, 63, 175, 162, 88, 212, 192, 40, 214, 185, 213, 140, 42, 145, 194, 186, 70, 71, 112, 68, 0, 192, 65, 4, 105, 143, 230, 50, 162, 79, 247, 192]);
|
||||
const type = Constants.AdvType.Chat;
|
||||
const flags = 0;
|
||||
const outPathLen = 0;
|
||||
const outPath = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
const advName = "Boaty";
|
||||
const lastAdvert = 1739244825;
|
||||
const advLat = 0;
|
||||
const advLon = 0;
|
||||
await this.device.sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon);
|
||||
},
|
||||
async sendCommandSyncNextMessage() {
|
||||
await this.device.sendCommandSyncNextMessage();
|
||||
},
|
||||
|
|
|
|||
|
|
@ -39,6 +39,27 @@ class BufferWriter {
|
|||
this.writeBytes(new TextEncoder().encode(string));
|
||||
}
|
||||
|
||||
writeCString(string, maxLength) {
|
||||
|
||||
// create buffer of max length
|
||||
const bytes = new Uint8Array(new ArrayBuffer(maxLength));
|
||||
|
||||
// encode string to bytes
|
||||
const encodedString = new TextEncoder().encode(string);
|
||||
|
||||
// copy in string until we hit the max length, or we run out of string bytes
|
||||
for(var i = 0; i < maxLength && i < encodedString.length; i++){
|
||||
bytes[i] = encodedString[i];
|
||||
}
|
||||
|
||||
// ensure the last byte is always a null terminator
|
||||
bytes[bytes.length - 1] = 0;
|
||||
|
||||
// write to buffer
|
||||
this.writeBytes(bytes);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BufferWriter;
|
||||
|
|
|
|||
|
|
@ -7,17 +7,17 @@ class Constants {
|
|||
|
||||
static CommandCodes = {
|
||||
AppStart: 1, // done
|
||||
SendTxtMsg: 2,
|
||||
SendTxtMsg: 2, // done
|
||||
SendChannelTxtMsg: 3,
|
||||
GetContacts: 4, // done
|
||||
GetDeviceTime: 5, // done
|
||||
SetDeviceTime: 6, // done
|
||||
SendSelfAdvert: 7, // done
|
||||
SetAdvertName: 8, // done
|
||||
AddUpdateContact: 9,
|
||||
AddUpdateContact: 9, // done
|
||||
SyncNextMessage: 10, // done
|
||||
SetRadioParams: 11,
|
||||
SetTxPower: 12,
|
||||
SetRadioParams: 11, // done
|
||||
SetTxPower: 12, // done
|
||||
}
|
||||
|
||||
static ResponseCodes = {
|
||||
|
|
@ -27,18 +27,25 @@ class Constants {
|
|||
Contact: 3, // done
|
||||
EndOfContacts: 4, // done
|
||||
SelfInfo: 5, // done
|
||||
Sent: 6,
|
||||
Sent: 6, // done
|
||||
ContactMsgRecv: 7, // done
|
||||
ChannelMsgRecv: 8,
|
||||
CurrTime: 9, // done
|
||||
NoMoreMessages: 10,
|
||||
NoMoreMessages: 10, // done
|
||||
}
|
||||
|
||||
static PushCodes = {
|
||||
Advert: 0x80,
|
||||
Advert: 0x80, // done
|
||||
PathUpdated: 0x81,
|
||||
SendConfirmed: 0x82,
|
||||
MsgWaiting: 0x83,
|
||||
SendConfirmed: 0x82, // done
|
||||
MsgWaiting: 0x83, // done
|
||||
}
|
||||
|
||||
static AdvType = {
|
||||
None: 0,
|
||||
Chat: 1,
|
||||
Repeater: 2,
|
||||
Room: 3,
|
||||
}
|
||||
|
||||
static SelfAdvertTypes = {
|
||||
|
|
@ -46,6 +53,12 @@ class Constants {
|
|||
Flood: 1,
|
||||
}
|
||||
|
||||
static TxtTypes = {
|
||||
Plain: 0,
|
||||
CliData: 1,
|
||||
SignedPlain: 2,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Constants;
|
||||
|
|
|
|||
|
|
@ -81,6 +81,17 @@ class Device {
|
|||
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);
|
||||
|
|
@ -117,12 +128,44 @@ class Device {
|
|||
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 readLoop() {
|
||||
try {
|
||||
while(true){
|
||||
|
|
@ -209,6 +252,8 @@ class Device {
|
|||
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){
|
||||
|
|
@ -217,8 +262,12 @@ class Device {
|
|||
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 {
|
||||
|
|
@ -233,6 +282,13 @@ class Device {
|
|||
});
|
||||
}
|
||||
|
||||
onSendConfirmedPush(bufferReader) {
|
||||
console.log("onSendConfirmedPush", {
|
||||
ackCode: bufferReader.readBytes(4),
|
||||
roundTrip: bufferReader.readUInt32LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onMsgWaitingPush(bufferReader) {
|
||||
console.log("onMsgWaitingPush", {
|
||||
|
||||
|
|
@ -266,6 +322,12 @@ class Device {
|
|||
});
|
||||
}
|
||||
|
||||
onSentResponse(bufferReader) {
|
||||
console.log("onSentResponse", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
onSelfInfoResponse(bufferReader) {
|
||||
console.log("onSelfInfoResponse", {
|
||||
type: bufferReader.readByte(),
|
||||
|
|
@ -287,6 +349,12 @@ class Device {
|
|||
});
|
||||
}
|
||||
|
||||
onNoMoreMessagesResponse(bufferReader) {
|
||||
console.log("onNoMoreMessagesResponse", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
onContactMsgRecvResponse(bufferReader) {
|
||||
console.log("onContactMsgRecvResponse", {
|
||||
pubKeyPrefix: bufferReader.readBytes(6),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue