Skip to content

Commit 234bba8

Browse files
committed
Send sensor data after aggregating
1 parent f769b11 commit 234bba8

File tree

10 files changed

+9071
-4888
lines changed

10 files changed

+9071
-4888
lines changed

logs/libapp-demangle.S

Lines changed: 1417 additions & 552 deletions
Large diffs are not rendered by default.

logs/libapp-expanded.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ mod app_network {
105105
sys::console, encoding::coap_context::*,
106106
libs::{sensor_network}, coap, d, Strn};
107107
use mynewt_macros::strn;
108+
/// Aggregate the sensor value with other sensor data before transmitting to server.
109+
/// If the sensor value is a GPS geolocation, we remember it and attach it to other sensor data for transmission.
110+
pub fn aggregate_sensor_data(sensor_value: &SensorValue)
111+
-> MynewtResult<()> {
112+
if let SensorValueType::Geolocation { .. } = sensor_value.value {
113+
unsafe { CURRENT_GEOLOCATION = sensor_value.value };
114+
Ok(())
115+
} else {
116+
let transmit_value =
117+
SensorValue{key: sensor_value.key,
118+
value: sensor_value.value,
119+
geo: unsafe { CURRENT_GEOLOCATION },};
120+
send_sensor_data(&transmit_value)
121+
}
122+
}
108123
/// Compose a CoAP JSON message with the Sensor Key (field name) and Value in `val`
109124
/// and send to the CoAP server. The message will be enqueued for transmission by the CoAP / OIC
110125
/// Background Task so this function will return without waiting for the message to be transmitted.
@@ -116,7 +131,7 @@ mod app_network {
116131
/// {"key":"t", "value":1715}
117132
/// ]}
118133
/// ```
119-
pub fn send_sensor_data(val: &SensorValue) -> MynewtResult<()> {
134+
fn send_sensor_data(val: &SensorValue) -> MynewtResult<()> {
120135
console::print("Rust send_sensor_data\n");
121136
let device_id = sensor_network::get_device_id()?;
122137
let rc = sensor_network::init_server_post(&Strn::new(b"\0"))?;
@@ -295,6 +310,8 @@ mod app_network {
295310
console::print("\n");
296311
Ok(())
297312
}
313+
/// Current geolocation recorded from GPS
314+
static mut CURRENT_GEOLOCATION: SensorValueType = SensorValueType::None;
298315
}
299316
mod app_sensor {
300317
//! Poll the temperature sensor every 10 seconds. Transmit the sensor data to the CoAP server after polling.
@@ -305,7 +322,7 @@ mod app_sensor {
305322
sensor_type_t, SensorValue, SensorValueType},
306323
sys::console, fill_zero, Strn};
307324
use mynewt_macros::{init_strn};
308-
use crate::app_network::send_sensor_data;
325+
use crate::app_network;
309326
/// Sensor to be polled: `temp_stm32_0` is the internal temperature sensor
310327
static SENSOR_DEVICE: Strn =
311328
Strn{rep: mynewt::StrnRep::ByteStr(b"temp_stm32_0\x00"),};
@@ -376,7 +393,7 @@ mod app_sensor {
376393
}
377394
};
378395
}
379-
let res = send_sensor_data(&sensor_value);
396+
let res = app_network::aggregate_sensor_data(&sensor_value);
380397
if let Err(err) = res {
381398
if err == MynewtError::SYS_EAGAIN {
382399
console::print("TMP network not ready\n");
@@ -502,6 +519,7 @@ mod gps_sensor {
502519
sensor_type_t, SensorValue, SensorValueType},
503520
sys::console, fill_zero, Strn};
504521
use mynewt_macros::{init_strn};
522+
use crate::app_network;
505523
/// Sensor to be polled: `gps_l70r_0` is the Quectel L70-R GPS module
506524
static GPS_DEVICE: Strn =
507525
Strn{rep: mynewt::StrnRep::ByteStr(b"gps_l70r_0\x00"),};
@@ -570,7 +588,7 @@ mod gps_sensor {
570588
console::print("\n");
571589
console::flush();
572590
}
573-
aggregate_sensor_data(sensor_value);
591+
app_network::aggregate_sensor_data(&sensor_value);
574592
MynewtError::SYS_EOK
575593
}
576594
/// Convert the geolocation value received from Mynewt into a Geolocation `SensorValue` for transmission.
@@ -647,18 +665,6 @@ mod gps_sensor {
647665
}
648666
},}
649667
}
650-
static mut current_geolocation: SensorValueType = SensorValueType::None;
651-
/// Aggregate the sensor value with other sensor data before transmitting to server.
652-
fn aggregate_sensor_data(sensor_value: SensorValue) {
653-
if let SensorValueType::Geolocation { .. } = sensor_value.value {
654-
unsafe { current_geolocation = sensor_value.value };
655-
} else {
656-
let transmit_value =
657-
SensorValue{key: sensor_value.key,
658-
value: sensor_value.value,
659-
geo: unsafe { current_geolocation },};
660-
}
661-
}
662668
}
663669
use core::panic::PanicInfo;
664670
use cortex_m::asm::bkpt;

logs/libapp.S

Lines changed: 1417 additions & 552 deletions
Large diffs are not rendered by default.

logs/libapp.elf

Lines changed: 3348 additions & 2633 deletions
Large diffs are not rendered by default.

logs/libapp.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

logs/rustlib-demangle.S

Lines changed: 1419 additions & 555 deletions
Large diffs are not rendered by default.

logs/rustlib.S

Lines changed: 1419 additions & 555 deletions
Large diffs are not rendered by default.

rust/app/src/app_network.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ use mynewt::{
3636
};
3737
use mynewt_macros::strn; // Import Mynewt procedural macros
3838

39+
/// Aggregate the sensor value with other sensor data before transmitting to server.
40+
/// If the sensor value is a GPS geolocation, we remember it and attach it to other sensor data for transmission.
41+
pub fn aggregate_sensor_data(sensor_value: &SensorValue) -> MynewtResult<()> { // Returns an error code upon error.
42+
if let SensorValueType::Geolocation {..} = sensor_value.value {
43+
// Save the geolocation for later transmission.
44+
unsafe { CURRENT_GEOLOCATION = sensor_value.value };
45+
Ok(())
46+
} else {
47+
// Attach the current geolocation to the sensor data for transmission.
48+
let transmit_value = SensorValue {
49+
key: sensor_value.key,
50+
value: sensor_value.value,
51+
geo: unsafe { CURRENT_GEOLOCATION }
52+
};
53+
// Transmit sensor value with geolocation.
54+
send_sensor_data(&transmit_value)
55+
}
56+
}
57+
3958
/// Compose a CoAP JSON message with the Sensor Key (field name) and Value in `val`
4059
/// and send to the CoAP server. The message will be enqueued for transmission by the CoAP / OIC
4160
/// Background Task so this function will return without waiting for the message to be transmitted.
@@ -47,7 +66,7 @@ use mynewt_macros::strn; // Import Mynewt procedural macros
4766
/// {"key":"t", "value":1715}
4867
/// ]}
4968
/// ```
50-
pub fn send_sensor_data(val: &SensorValue) -> MynewtResult<()> { // Returns an error code upon error.
69+
fn send_sensor_data(val: &SensorValue) -> MynewtResult<()> { // Returns an error code upon error.
5170
console::print("Rust send_sensor_data\n");
5271
// Get a randomly-generated device ID that changes each time we restart the device.
5372
let device_id = sensor_network::get_device_id() ? ;
@@ -85,4 +104,7 @@ pub fn send_sensor_data(val: &SensorValue) -> MynewtResult<()> { // Returns a
85104

86105
// The CoAP Background Task will transmit the message in the background.
87106
Ok(())
88-
}
107+
}
108+
109+
/// Current geolocation recorded from GPS
110+
static mut CURRENT_GEOLOCATION: SensorValueType = SensorValueType::None;

rust/app/src/app_sensor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use mynewt::{
3333
fill_zero, Strn, // Import Mynewt macros
3434
};
3535
use mynewt_macros::{ init_strn }; // Import Mynewt procedural macros
36-
use crate::app_network::send_sensor_data; // Import `app_network.rs` for sending sensor data
36+
use crate::app_network; // Import `app_network.rs` for sending sensor data
3737

3838
/// Sensor to be polled: `temp_stm32_0` is the internal temperature sensor
3939
static SENSOR_DEVICE: Strn = init_strn!("temp_stm32_0");
@@ -99,7 +99,7 @@ extern fn handle_sensor_data(sensor: sensor_ptr, _arg: sensor_arg,
9999
// CoAP server. The message will be enqueued for transmission by the OIC
100100
// background task so this function will return without waiting for the message
101101
// to be transmitted.
102-
let res = send_sensor_data(&sensor_value);
102+
let res = app_network::aggregate_sensor_data(&sensor_value);
103103

104104
// `SYS_EAGAIN` means that the Network Task is still starting up the network.
105105
// We drop the sensor data and send at the next poll.

rust/app/src/gps_sensor.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use mynewt::{
3232
fill_zero, Strn, // Import Mynewt macros
3333
};
3434
use mynewt_macros::{ init_strn }; // Import Mynewt procedural macros
35-
////use crate::app_network::send_sensor_data; // Import `app_network.rs` for sending sensor data
35+
use crate::app_network; // Import `app_network.rs` for sending sensor data
3636

3737
/// Sensor to be polled: `gps_l70r_0` is the Quectel L70-R GPS module
3838
static GPS_DEVICE: Strn = init_strn!("gps_l70r_0");
@@ -95,7 +95,7 @@ extern fn handle_gps_data(sensor: sensor_ptr, _arg: sensor_arg,
9595
}
9696

9797
// Aggregate the GPS geolocation with other sensor data before transmitting to server.
98-
aggregate_sensor_data(sensor_value);
98+
app_network::aggregate_sensor_data(&sensor_value);
9999

100100
// Return 0 to Mynewt to indicate no error. Should not end with a semicolon (;).
101101
MynewtError::SYS_EOK
@@ -132,22 +132,4 @@ fn convert_gps_data(sensor_data: sensor_data_ptr, sensor_type: sensor_type_t) ->
132132
// _ => { assert!(false, "sensor type"); SensorValueType::None }
133133
}
134134
}
135-
}
136-
137-
static mut current_geolocation: SensorValueType = SensorValueType::None;
138-
139-
/// Aggregate the sensor value with other sensor data before transmitting to server.
140-
fn aggregate_sensor_data(sensor_value: SensorValue) {
141-
if let SensorValueType::Geolocation {..} = sensor_value.value {
142-
// Save the geolocation for later transmission.
143-
unsafe { current_geolocation = sensor_value.value };
144-
} else {
145-
// Attach the current geolocation to the sensor data for transmission.
146-
let transmit_value = SensorValue {
147-
key: sensor_value.key,
148-
value: sensor_value.value,
149-
geo: unsafe { current_geolocation }
150-
};
151-
// TODO: Transmit sensor value with geolocation.
152-
}
153135
}

0 commit comments

Comments
 (0)