ESP32-PaxCounter/src/rcommand.cpp

278 lines
8.6 KiB
C++
Raw Normal View History

2018-03-21 18:03:14 +01:00
// Basic Config
2018-03-18 19:45:17 +01:00
#include "globals.h"
2018-07-17 11:53:43 +02:00
#include "rcommand.h"
2018-03-18 19:45:17 +01:00
// Local logging tag
2018-06-02 18:28:01 +02:00
static const char TAG[] = "main";
2018-03-18 19:45:17 +01:00
// set of functions that can be triggered by remote commands
2018-07-31 00:00:24 +02:00
void set_reset(uint8_t val[]) {
switch (val[0]) {
case 0: // restart device
ESP_LOGI(TAG, "Remote command: restart device");
2018-07-14 20:07:33 +02:00
sprintf(display_line6, "Reset pending");
2018-08-02 11:33:02 +02:00
vTaskDelay(10000 / portTICK_PERIOD_MS); // wait for LMIC to confirm LoRa
// downlink to server
esp_restart();
break;
case 1: // reset MAC counter
ESP_LOGI(TAG, "Remote command: reset MAC counter");
reset_counters(); // clear macs
reset_salt(); // get new salt
2018-07-14 20:07:33 +02:00
sprintf(display_line6, "Reset counter");
break;
case 2: // reset device to factory settings
ESP_LOGI(TAG, "Remote command: reset device to factory settings");
2018-07-14 20:07:33 +02:00
sprintf(display_line6, "Factory reset");
eraseConfig();
break;
2018-08-05 12:16:54 +02:00
case 3: // reset send queues
ESP_LOGI(TAG, "Remote command: flush send queue");
2018-08-05 17:49:53 +02:00
sprintf(display_line6, "Queue reset");
2018-08-05 12:16:54 +02:00
flushQueues();
break;
2018-08-02 11:33:02 +02:00
default:
ESP_LOGW(TAG, "Remote command: reset called with invalid parameter(s)");
}
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_rssi(uint8_t val[]) {
cfg.rssilimit = val[0] * -1;
ESP_LOGI(TAG, "Remote command: set RSSI limit to %d", cfg.rssilimit);
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_sendcycle(uint8_t val[]) {
cfg.sendcycle = val[0];
2018-07-14 23:38:43 +02:00
// update send cycle interrupt
timerAlarmWrite(sendCycle, cfg.sendcycle * 2 * 10000, true);
// reload interrupt after each trigger of channel switch cycle
2018-08-02 11:33:02 +02:00
ESP_LOGI(TAG, "Remote command: set send cycle to %d seconds",
cfg.sendcycle * 2);
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_wifichancycle(uint8_t val[]) {
cfg.wifichancycle = val[0];
2018-07-14 23:38:43 +02:00
// update channel rotation interrupt
timerAlarmWrite(channelSwitch, cfg.wifichancycle * 10000, true);
ESP_LOGI(TAG,
"Remote command: set Wifi channel switch interval to %.1f seconds",
cfg.wifichancycle / float(100));
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_blescantime(uint8_t val[]) {
cfg.blescantime = val[0];
ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds",
cfg.blescantime / float(100));
#ifdef BLECOUNTER
// stop & restart BLE scan task to apply new parameter
if (cfg.blescan) {
stop_BLEscan();
start_BLEscan();
}
#endif
2018-08-07 22:39:49 +02:00
}
2018-07-31 00:00:24 +02:00
void set_countmode(uint8_t val[]) {
switch (val[0]) {
case 0: // cyclic unconfirmed
cfg.countermode = 0;
ESP_LOGI(TAG, "Remote command: set counter mode to cyclic unconfirmed");
break;
case 1: // cumulative
cfg.countermode = 1;
ESP_LOGI(TAG, "Remote command: set counter mode to cumulative");
break;
2018-08-02 11:33:02 +02:00
case 2: // cyclic confirmed
cfg.countermode = 2;
ESP_LOGI(TAG, "Remote command: set counter mode to cyclic confirmed");
break;
2018-08-02 11:33:02 +02:00
default: // invalid parameter
ESP_LOGW(
TAG,
"Remote command: set counter mode called with invalid parameter(s)");
}
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_screensaver(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set screen saver to %s ",
val[0] ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.screensaver = val[0] ? 1 : 0;
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_display(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set screen to %s", val[0] ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.screenon = val[0] ? 1 : 0;
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_gps(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set GPS mode to %s", val[0] ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.gpsmode = val[0] ? 1 : 0;
2018-08-02 11:33:02 +02:00
}
2018-06-08 22:41:37 +02:00
2018-07-31 00:00:24 +02:00
void set_beacon(uint8_t val[]) {
2018-08-02 11:33:02 +02:00
uint8_t id = val[0]; // use first parameter as beacon storage id
memmove(val, val + 1, 6); // strip off storage id
beacons[id] = macConvert(val); // store beacon MAC in array
ESP_LOGI(TAG, "Remote command: set beacon ID#%d", id);
printKey("MAC", val, 6, false); // show beacon MAC
2018-08-07 22:39:49 +02:00
}
2018-07-31 00:00:24 +02:00
void set_monitor(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set beacon monitor mode to %s",
val ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.monitormode = val[0] ? 1 : 0;
}
2018-07-31 00:00:24 +02:00
void set_lorasf(uint8_t val[]) {
2018-07-14 20:07:33 +02:00
#ifdef HAS_LORA
2018-07-31 00:00:24 +02:00
ESP_LOGI(TAG, "Remote command: set LoRa SF to %d", val[0]);
switch_lora(val[0], cfg.txpower);
2018-07-14 20:07:33 +02:00
#else
ESP_LOGW(TAG, "Remote command: LoRa not implemented");
#endif // HAS_LORA
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_loraadr(uint8_t val[]) {
2018-07-14 20:07:33 +02:00
#ifdef HAS_LORA
2018-07-31 00:00:24 +02:00
ESP_LOGI(TAG, "Remote command: set LoRa ADR mode to %s",
val[0] ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.adrmode = val[0] ? 1 : 0;
LMIC_setAdrMode(cfg.adrmode);
2018-07-14 20:07:33 +02:00
#else
ESP_LOGW(TAG, "Remote command: LoRa not implemented");
#endif // HAS_LORA
2018-08-07 22:39:49 +02:00
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_blescan(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.blescan = val[0] ? 1 : 0;
#ifdef BLECOUNTER
2018-08-07 22:39:49 +02:00
if (cfg.blescan)
start_BLEscan();
2018-08-07 22:39:49 +02:00
else {
macs_ble = 0; // clear BLE counter
stop_BLEscan();
}
2018-08-07 22:39:49 +02:00
#endif
}
2018-03-18 19:45:17 +01:00
2018-07-31 00:00:24 +02:00
void set_wifiant(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set Wifi antenna to %s",
2018-07-31 00:00:24 +02:00
val[0] ? "external" : "internal");
2018-08-07 22:39:49 +02:00
cfg.wifiant = val[0] ? 1 : 0;
#ifdef HAS_ANTENNA_SWITCH
antenna_select(cfg.wifiant);
#endif
2018-08-07 22:39:49 +02:00
}
2018-07-31 00:00:24 +02:00
void set_vendorfilter(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: set vendorfilter mode to %s",
2018-07-31 00:00:24 +02:00
val[0] ? "on" : "off");
2018-08-07 22:39:49 +02:00
cfg.vendorfilter = val[0] ? 1 : 0;
}
2018-04-15 12:12:06 +02:00
2018-07-31 00:00:24 +02:00
void set_rgblum(uint8_t val[]) {
// Avoid wrong parameters
2018-07-31 00:00:24 +02:00
cfg.rgblum = (val[0] >= 0 && val[0] <= 100) ? (uint8_t)val[0] : RGBLUMINOSITY;
ESP_LOGI(TAG, "Remote command: set RGB Led luminosity %d", cfg.rgblum);
2018-04-02 01:33:49 +02:00
};
2018-07-31 00:00:24 +02:00
void set_lorapower(uint8_t val[]) {
2018-07-14 20:07:33 +02:00
#ifdef HAS_LORA
2018-07-31 00:00:24 +02:00
ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %d", val[0]);
switch_lora(cfg.lorasf, val[0]);
2018-07-14 20:07:33 +02:00
#else
2018-07-14 23:38:43 +02:00
ESP_LOGW(TAG, "Remote command: LoRa not implemented");
2018-07-14 20:07:33 +02:00
#endif // HAS_LORA
2018-03-18 19:45:17 +01:00
};
2018-07-31 00:00:24 +02:00
void get_config(uint8_t val[]) {
2018-06-16 19:50:36 +02:00
ESP_LOGI(TAG, "Remote command: get device configuration");
payload.reset();
payload.addConfig(cfg);
2018-08-04 15:27:58 +02:00
SendData(CONFIGPORT);
2018-03-18 19:45:17 +01:00
};
2018-07-31 00:00:24 +02:00
void get_status(uint8_t val[]) {
2018-06-16 19:50:36 +02:00
ESP_LOGI(TAG, "Remote command: get device status");
#ifdef HAS_BATTERY_PROBE
uint16_t voltage = read_voltage();
#else
2018-07-14 23:38:43 +02:00
uint16_t voltage = 0;
#endif
2018-06-16 19:50:36 +02:00
payload.reset();
2018-08-05 12:16:54 +02:00
payload.addStatus(voltage, uptime() / 1000, temperatureRead(),
ESP.getFreeHeap());
2018-08-04 15:27:58 +02:00
SendData(STATUSPORT);
2018-06-08 22:41:37 +02:00
};
2018-07-31 00:00:24 +02:00
void get_gps(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get gps status");
#ifdef HAS_GPS
gps_read();
2018-06-16 19:50:36 +02:00
payload.reset();
payload.addGPS(gps_status);
2018-08-04 15:27:58 +02:00
SendData(GPSPORT);
#else
2018-07-14 23:38:43 +02:00
ESP_LOGW(TAG, "GPS function not supported");
#endif
};
2018-03-18 19:45:17 +01:00
// assign previously defined functions to set of numeric remote commands
2018-08-02 11:33:02 +02:00
// format: opcode, function, #bytes params,
// flag (1 = do make settings persistent / 0 = don't)
//
2018-08-02 11:33:02 +02:00
cmd_t table[] = {
{0x01, set_rssi, 1, true}, {0x02, set_countmode, 1, true},
{0x03, set_gps, 1, true}, {0x04, set_display, 1, true},
{0x05, set_lorasf, 1, true}, {0x06, set_lorapower, 1, true},
{0x07, set_loraadr, 1, true}, {0x08, set_screensaver, 1, true},
{0x09, set_reset, 1, false}, {0x0a, set_sendcycle, 1, true},
{0x0b, set_wifichancycle, 1, true}, {0x0c, set_blescantime, 1, true},
{0x0d, set_vendorfilter, 1, false}, {0x0e, set_blescan, 1, true},
{0x0f, set_wifiant, 1, true}, {0x10, set_rgblum, 1, true},
{0x11, set_monitor, 1, true}, {0x12, set_beacon, 7, false},
{0x80, get_config, 0, false}, {0x81, get_status, 0, false},
{0x84, get_gps, 0, false}};
2018-03-18 19:45:17 +01:00
2018-08-04 18:09:25 +02:00
const uint8_t cmdtablesize =
sizeof(table) / sizeof(table[0]); // number of commands in command table
// check and execute remote command
void rcommand(uint8_t cmd[], uint8_t cmdlength) {
if (cmdlength == 0)
return;
uint8_t foundcmd[cmdlength], cursor = 0;
bool storeflag = false;
while (cursor < cmdlength) {
2018-08-04 18:09:25 +02:00
int i = cmdtablesize;
while (i--) {
if (cmd[cursor] == table[i].opcode) { // lookup command in opcode table
cursor++; // strip 1 byte opcode
if ((cursor + table[i].params) <= cmdlength) {
memmove(foundcmd, cmd + cursor,
table[i].params); // strip opcode from cmd array
cursor += table[i].params;
if (table[i].store) // ceck if function needs to store configuration
storeflag = true;
table[i].func(
foundcmd); // execute assigned function with given parameters
} else
ESP_LOGI(
TAG,
"Remote command x%02X called with missing parameter(s), skipped",
table[i].opcode);
break; // exit table lookup loop, command was found
} // command validation
} // command table lookup loop
} // command parsing loop
2018-08-04 18:09:25 +02:00
if (storeflag)
saveConfig();
} // rcommand()