-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
157 lines (133 loc) · 4.08 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#![no_main]
#![no_std]
use defmt::Format;
use defmt_rtt as _;
use serde::Serialize;
use heapless::Vec;
// use heapless::String;
// global logger
use nrf9160_hal as _;
// use nrf9160_hal::pac;
// memory layout
// use pac::interrupt;
use alloc_cortex_m::CortexMHeap;
use core::{
mem::MaybeUninit,
sync::atomic::{AtomicBool, Ordering},
};
use coap_lite::{CoapRequest, ContentFormat, RequestType};
use nrf_modem_nal::embedded_nal::{nb, SocketAddr, UdpClientStack};
use nrf_modem_nal::Modem;
use panic_probe as _;
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
/// Terminates the application and makes `probe-run` exit with exit-code = 0
pub fn exit() -> ! {
loop {
cortex_m::asm::bkpt();
}
}
// Error types
#[derive(Format, Clone, Copy)]
pub enum Error {
// ModemError(nrfxlib::Error),
LTEConnectionFailure,
TransmitFailure,
UndefinedTransition,
}
// impl From<nrfxlib::Error> for Error {
// fn from(e: nrfxlib::Error) -> Self {
// Self::ModemError(e)
// }
// }
// Define our possible states
#[derive(Format, Clone, Copy)]
pub enum State {
Initialize,
Sleep,
Ready,
Sample,
Transmit,
Failure(Error),
}
// Define events
#[derive(Format)]
pub enum Event {
SetupComplete,
TimerInterrupt,
SensorPowerOn,
BufferFull,
BufferNotFull,
DataSent,
}
// Define transitions based on current state and event
impl State {
pub fn step(self, event: Event) -> State {
match (self, event) {
(State::Sleep, Event::TimerInterrupt) => State::Ready,
(State::Initialize, Event::SetupComplete) => State::Ready,
(State::Ready, Event::SensorPowerOn) => State::Sample,
(State::Sample, Event::BufferNotFull) => State::Sleep,
(State::Sample, Event::BufferFull) => State::Transmit,
(State::Transmit, Event::DataSent) => State::Sleep,
(_s, _e) => State::Failure(Error::UndefinedTransition), // ( s, e ) => { State::Failure( String::from("Invalid (state,event) combination: ({:?}, {:?})") ) },
}
}
}
// Structure to hold our payload buffer (heapless Vec)
#[derive(Serialize)]
pub struct Payload {
pub data: Vec<i16,3>,
}
impl Payload {
pub fn new() -> Self {
Payload { data: Vec::new() }
}
pub fn transmit_data(&mut self, modem: &mut Modem) {
//TODO: Finish CoAP implementation
let mut request = CoapRequest::new();
request.set_method(RequestType::Post);
request.set_path("data");
request.message.set_content_format(ContentFormat::ApplicationJSON);
request.message.payload = serde_json::to_vec(&self).unwrap();
// Connect LTE and UDP Socket for data transfer
let mut lte = modem.lte_socket().unwrap();
modem.lte_connect(&mut lte).unwrap();
nb::block!(modem.lte_connect(&mut lte)).unwrap();
let mut udp_socket = modem.socket().unwrap();
// TODO: How to use DTLS with this?
nb::block!(modem.connect(
&mut udp_socket,
SocketAddr::V4("65.108.135.161:5672".parse().unwrap())
)).unwrap(); // coap://sandbox.drogue.cloud/v1/foo
nb::block!(modem.send(
&mut udp_socket,
&request.message.to_bytes().unwrap()
)).unwrap();
//clear buffer after upload
self.data.clear();
// Ok(())
}
}
// An allocator is required for the coap-lite lib
#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
static mut HEAP_DATA: [MaybeUninit<u8>; 8192] = [MaybeUninit::uninit(); 8192];
pub fn init() {
static ONCE: AtomicBool = AtomicBool::new(false);
if ONCE
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
unsafe {
ALLOCATOR.init(HEAP_DATA.as_ptr() as usize, HEAP_DATA.len());
}
}
}
// #[link_section = ".spm"]
// #[used]
// static SPM: [u8; 24052] = *include_bytes!("zephyr.bin");