Skip to content

Commit

Permalink
Do sanity check on mesh control data to prevent unexpected behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed May 3, 2017
1 parent bc923dd commit 58298af
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
12 changes: 9 additions & 3 deletions pyaci/aci/AciEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __init__(self,pkt):
class HeartbeatMsg(object):
def __init__(self, data):
if len(data) != 18:
print(str.format("Error: expected 16 bytes, got %s" %(data)))
print(str.format("Error: expected 18 bytes, got %s" %(data)))
(self.rssi, self.received_at, self.received_at_ms, self.local_clock_version, self.sensor_id, self.epoch_seconds, self.epoch_ms, self.clock_version) = unpack('<BiHHBiHH', bytearray(data))

def __repr__(self):
Expand All @@ -174,7 +174,13 @@ def __init__(self,pkt):
super(AciEventAppEvt, self).__init__(pkt)
self.app_evt_opcode = self.Data[0]

def is_heartbeat(self):
return self.app_evt_opcode == AciEventAppEvt.APP_EVENT_OPCODE_HEARTBEAT

def heartbeat_msg(self):
return HeartbeatMsg(self.Data[1:])

def __repr__(self):
if self.app_evt_opcode == AciEventAppEvt.APP_EVENT_OPCODE_HEARTBEAT:
return repr(HeartbeatMsg(self.Data[1:]))
if self.is_heartbeat():
return repr(self.heartbeat_msg())
#if self.app_opcode == APP_EVENT_OPCODE_HEARTBEAT:
15 changes: 13 additions & 2 deletions pyaci/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ def __init__(self, sensei_config):
self.aci = AciUart.AciUart(port=sensei_config['mesh_network']['serial_path'], baudrate=115200)
self.classroom_id = sensei_config["classroom_id"]

def handle_heartbeat(self, hb):
print(str.format("handling heartbeat: %s" %(hb)))
if hb.epoch_seconds != hb.received_at:
print(str.format("Sensor %d clock offset detected; issuing sync_time." %(hb.sensor_id)))
self.sync_time()

def get_sensor_updates(self):
updates = []
while True:
try:
evt = self.aci.events_queue.get_nowait()
print(str.format("evt = %s" %(evt)))
if isinstance(evt, AciEvent.AciEventNew) and evt.is_sensor_update():
updates.append(evt.sensor_values())
elif isinstance(evt, AciEvent.AciEventAppEvt) and evt.is_heartbeat():
self.handle_heartbeat(evt.heartbeat_msg())
except Empty:
break
return updates
Expand Down Expand Up @@ -80,6 +89,7 @@ def run(self):
while True:
updates = self.get_sensor_updates()
if len(updates) > 0:
continue
obs = [self.radio_obs_from_update(update) for update in updates]
flattened_obs = [ob for sublist in obs for ob in sublist]
if len(flattened_obs) > 0:
Expand All @@ -91,11 +101,12 @@ def run(self):
accelerometer_obs.append(ob)
if len(accelerometer_obs) > 0:
self.api.upload_accelerometer_observations(accelerometer_obs)
elif time.time() - self.last_time_sync > Uploader.TIME_SYNC_INTERVAL:
self.sync_time()
else:
time.sleep(0.5)

if time.time() - self.last_time_sync > Uploader.TIME_SYNC_INTERVAL:
self.sync_time()


if __name__ == '__main__':

Expand Down
7 changes: 6 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void rbc_mesh_event_handler(rbc_mesh_event_t* p_evt)
case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL:
case RBC_MESH_EVENT_TYPE_NEW_VAL:
case RBC_MESH_EVENT_TYPE_UPDATE_VAL:
if (p_evt->params.rx.value_handle == MESH_CONTROL_HANDLE) {
if (p_evt->params.rx.value_handle == MESH_CONTROL_HANDLE && p_evt->params.rx.data_len == sizeof(mesh_control_t)) {
mesh_control_update_config((mesh_control_t*)p_evt->params.rx.p_data);
}
break;
Expand Down Expand Up @@ -123,6 +123,11 @@ int main(void)

mesh_control_init();

nrf_gpio_cfg_output(LED_START + LED_GREEN);
nrf_gpio_cfg_output(LED_START + LED_RED);
nrf_gpio_cfg_output(LED_START + LED_BLUE);


/* Enable Softdevice (including sd_ble before framework */
SOFTDEVICE_HANDLER_INIT(MESH_CLOCK_SRC, NULL);
softdevice_ble_evt_handler_set(rbc_mesh_ble_evt_handler);
Expand Down
25 changes: 25 additions & 0 deletions src/mesh_control.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "mesh_control.h"
#include "transport_control.h"
#include "leds.h"

static mesh_control_t m_config;

Expand All @@ -19,5 +20,29 @@ uint8_t mesh_control_get_hb_tx_power() {
}

void mesh_control_update_config(mesh_control_t *new_config) {
rbc_mesh_txpower_t allowed_tx_power[] = {
RBC_MESH_TXPOWER_0dBm,
RBC_MESH_TXPOWER_Pos4dBm,
RBC_MESH_TXPOWER_Neg12dBm,
RBC_MESH_TXPOWER_Neg8dBm,
RBC_MESH_TXPOWER_Neg4dBm
};

// Sanity checks
if (new_config->wake_interval == 0 || new_config->wake_interval > 1000) {
return;
}

bool tx_allowed = false;
for (int i=0; i<sizeof(allowed_tx_power)/sizeof(rbc_mesh_txpower_t); i++) {
if (allowed_tx_power[i] == new_config->hb_tx_power) {
tx_allowed = true;
}
}
if (!tx_allowed) {
return;
}

// Ok, looks good!
m_config = *new_config;
}
1 change: 0 additions & 1 deletion src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static void periodic_timer_cb(void * p_context)
DBG_TICK_PIN(6);

if (m_current_time % mesh_control_get_wake_interval() == 0) {
//report_debug_register();
SET_LED(LED_GREEN);
app_timer_cnt_get(&m_clock_second_start_counter_value);
m_scheduler_state = SCHEDULER_STATE_BEFORE_HB;
Expand Down

0 comments on commit 58298af

Please sign in to comment.