Skip to content

Commit

Permalink
I believe this is the fix we're looking for :)
Browse files Browse the repository at this point in the history
  • Loading branch information
redaphid committed Dec 5, 2022
1 parent 571d304 commit 7f2da1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion improv-wifi-mod/bleservices/improv.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
}
}
}
}
}
37 changes: 29 additions & 8 deletions improv-wifi-mod/improv-wifi-mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import BLEExports from './consts';
const StateCodes = BLEExports.StateCodes;
const ErrorCodes = BLEExports.ErrorCodes;
const Commands = BLEExports.Commands;

// FYI class definition for BLEServer can be found @ <moddable-project-dir>/modules/network/ble/bleserver.js
export default class ImprovWifi extends BLEServer {
deviceName;
ssid;
Expand All @@ -23,6 +25,7 @@ export default class ImprovWifi extends BLEServer {
this.onCredentialsRecieved = onCredentialsRecieved;
}
startImprov() {
trace("Starting Improv\n");
let advertisingData = {
flags: GAP.ADFlag.LE_GENERAL_DISCOVERABLE_MODE,
completeUUID128List: [uuid `00467768-6228-2272-4663-277478268000`],
Expand All @@ -32,32 +35,37 @@ export default class ImprovWifi extends BLEServer {
this.startAdvertising({ advertisingData });
}
onDisconnected() {
trace("Disconnected\n");
this.state = StateCodes.STATE_AUTHORIZED;
this.error = ErrorCodes.ERROR_NONE;
this.errorCharacteristic = null;
this.stateCharacteristic = null;
this.startImprov();
}
onReady() {
trace("Ready\n");
this.startImprov();
}
onCharacteristicRead(characteristic) {
switch (characteristic.name) {
trace(`Read: ${JSON.stringify(characteristic)}\n`);
switch(characteristic.name){
case "ERROR":
return this.error;
case "STATE":
case "RPC_RESULT":
return this.state;
default:
trace(`I have no idea what ${characteristic.name} is supposed to be`);
return this.error;
return this.error
}
}
onConnected() {
trace("Connected\n");
this.state = StateCodes.STATE_AUTHORIZED;
this.error = ErrorCodes.ERROR_NONE;
}
onCharacteristicNotifyDisabled(characteristic) {
trace('onCharacteristicNotifyDisabled\n');
switch (characteristic.name) {
case 'STATE':
this.stateCharacteristic = null;
Expand All @@ -77,6 +85,7 @@ export default class ImprovWifi extends BLEServer {
}
}
onCharacteristicNotifyEnabled(characteristic) {
trace(`onCharacteristicNotifyEnabled: characteristic: ${characteristic.name} \n`);
this.notify = characteristic;
switch (characteristic.name) {
case 'STATE':
Expand All @@ -102,11 +111,13 @@ export default class ImprovWifi extends BLEServer {
}
}
onCharacteristicWritten(characteristic, value) {
trace(`Written: ${characteristic.name}, in state ${characteristic.state}, with value ${value}, value[0] is ${value?.[0]}, which is a type ${typeof value?.[0]} \n`);
// this is where we go and update state again if necessary
switch (characteristic.name) {
case "RPC_COMMAND":
this.ssid = value;
if (value[0] === Commands.WIFI_SETTINGS) {
trace("Handling wifi settings\n");
this.state = StateCodes.STATE_PROVISIONING;
this.notifyState();
this.handleInboundWifiSettings(value);
Expand All @@ -123,6 +134,7 @@ export default class ImprovWifi extends BLEServer {
}
}
handleInboundWifiSettings(data) {
trace("Handling inbound wifi settings\n");
const ssid_length = data[2];
const ssid_start = 3;
const ssid_end = ssid_start + ssid_length;
Expand All @@ -132,33 +144,42 @@ export default class ImprovWifi extends BLEServer {
const ssid = this.buildValue(data, ssid_start, ssid_end);
const password = this.buildValue(data, pass_start, pass_end);
let result = this.onCredentialsRecieved({ ssid, password });
trace(`Result of onCredentialsRecieved is ${result}\n`);
if (!result) {
this.state = StateCodes.STATE_AUTHORIZED;
this.notifyState();
}
else {
this.state = StateCodes.STATE_PROVISIONED;
this.notifyState();
trace("Credentials weren't authorized :( \n");
this.state = StateCodes.ERROR_UNKNOWN;
this.error = ErrorCodes.ERROR_NOT_AUTHORIZED;
this.notifyError();
return;

}
trace("Credentials were authorized :) \n");
this.state = StateCodes.STATE_PROVISIONED;
this.notifyState();
}
buildValue(data, start, end) {
trace(`Building value from ${start} to ${end}\n`);
let str = '';
for (var i = start; i < end; i++) {
str += String.fromCharCode(data[i]);
}
return str;
}
notifyState() {
trace(`Notifying state: ${this.state}\n`);
if (!this.stateCharacteristic)
return;
this.notifyValue(this.stateCharacteristic, this.state);
}
notifyError() {
trace(`Notifying this characteristic: ${this.errorCharacteristic?.name} this error: ${this.error}\n`);
if (!this.errorCharacteristic)
return;

this.notifyValue(this.errorCharacteristic, this.error);
}
couldNotConnect() {
trace("Could not connect\n");
this.error = ErrorCodes.ERROR_UNABLE_TO_CONNECT;
this.notifyError();
}
Expand Down

1 comment on commit 7f2da1c

@redaphid
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do ya like the reject case? I can get rid of the traces() if you want, but imo in this circumstance it's nice to have them around, bc debugging is so difficult on these things sometimes.

Please sign in to comment.