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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use cstr_core::CStr;
use cty::*;
use crate::{
sys::console,
encoding::{
tinycbor::CborEncoder,
},
libs::mynewt_rust,
hw::sensor::SensorValueType,
fill_zero, Strn, StrnRep,
};
pub static mut COAP_CONTEXT: CoapContext = fill_zero!(CoapContext);
#[derive(Default)]
pub struct CoapContext {
key_buffer: [u8; COAP_KEY_SIZE],
value_buffer: [u8; COAP_VALUE_SIZE],
}
const COAP_KEY_SIZE: usize = 32;
const COAP_VALUE_SIZE: usize = 32;
static mut cbor_encoder0: CborEncoder = fill_zero!(CborEncoder);
static mut cbor_encoder1: CborEncoder = fill_zero!(CborEncoder);
impl CoapContext {
#[cfg(feature = "use_float")]
pub fn json_set_geolocation(&mut self, key: &Strn, lat_key: &Strn, long_key: &Strn, geo: SensorValueType) {
if let SensorValueType::Geolocation { latitude, longitude, .. } = geo {
let notused = self.to_void_ptr();
let encoder = unsafe { &mut sensor_coap::coap_json_encoder };
let key_cstr = self.key_strn_to_cstr(key);
let rc = unsafe { json::json_encode_object_key(encoder, key_cstr as *mut u8) };
assert!(rc == 0);
let rc = unsafe { json::json_encode_object_start(encoder) };
assert!(rc == 0);
let key_cstr = self.key_strn_to_cstr(lat_key);
unsafe { mynewt_rust::json_helper_set_float(notused, key_cstr, latitude as f32) };
let key_cstr = self.key_strn_to_cstr(long_key);
unsafe { mynewt_rust::json_helper_set_float(notused, key_cstr, longitude as f32) };
let rc = unsafe { json::json_encode_object_finish(encoder) };
assert!(rc == 0);
};
}
#[cfg(not(feature = "use_float"))]
pub fn json_set_geolocation(&mut self, _key: &Strn, _lat_key: &Strn, _long_key: &Strn, _geo: SensorValueType) {}
pub fn json_set_text_string(&mut self, key: &Strn, value: &Strn) {
let notused = self.to_void_ptr();
let key_cstr: *const u8 = self.key_strn_to_cstr(key);
let value_cstr: *const u8 = self.value_strn_to_cstr(value);
unsafe {
mynewt_rust::json_helper_set_text_string(
notused,
key_cstr as *const c_char,
value_cstr as *const c_char
)
};
}
fn key_strn_to_cstr(&mut self, key: &Strn) -> *const u8 {
match key.rep {
StrnRep::ByteStr(bs) => { self.key_to_cstr(bs) }
StrnRep::CStr(cstr) => { cstr }
}
}
fn value_strn_to_cstr(&mut self, value: &Strn) -> *const u8 {
match value.rep {
StrnRep::ByteStr(bs) => { self.value_to_cstr(bs) }
StrnRep::CStr(cstr) => { cstr }
}
}
pub fn key_to_cstr(&mut self, s: &[u8]) -> *const u8 {
if s.last() == Some(&0) { return s.as_ptr() as *const u8; }
assert!(s.len() < COAP_KEY_SIZE, "big key");
self.key_buffer[..s.len()].copy_from_slice(s);
self.key_buffer[s.len()] = 0;
self.key_buffer.as_ptr() as *const u8
}
pub fn value_to_cstr(&mut self, s: &[u8]) -> *const u8 {
if s.last() == Some(&0) { return s.as_ptr() as *const u8; }
assert!(s.len() < COAP_VALUE_SIZE, "big value");
self.value_buffer[..s.len()].copy_from_slice(s);
self.value_buffer[s.len()] = 0;
self.value_buffer.as_ptr() as *const u8
}
pub fn cstr_len(&self, s: &[u8]) -> usize {
if s.last() == Some(&0) { return s.len() - 1; }
s.len()
}
pub fn global_encoder(&self) -> *mut super::tinycbor::CborEncoder {
unsafe { &mut super::g_encoder }
}
pub fn new_encoder(&self, key: &str, suffix: &str) -> *mut super::tinycbor::CborEncoder {
console::print("new_encoder: "); console::print(key); console::print(suffix); console::print("\n");
if (key, suffix) == ("values", _ARRAY) { unsafe { &mut cbor_encoder0 } }
else if (key, suffix) == ("values", _MAP) { unsafe { &mut cbor_encoder1 } }
else {
assert!(false, "new_encoder fail");
unsafe { &mut super::root_map }
}
}
pub fn encoder(&self, key: &str, suffix: &str) -> *mut super::tinycbor::CborEncoder {
console::print("encoder: "); console::print(key); console::print(suffix); console::print("\n");
if (key, suffix) == ("root", "_map") { unsafe { &mut super::root_map } }
else if (key, suffix) == ("values", _ARRAY) { unsafe { &mut cbor_encoder0 } }
else if (key, suffix) == ("values", _MAP) { unsafe { &mut cbor_encoder1 } }
else {
assert!(false, "encoder fail");
unsafe { &mut super::root_map }
}
}
pub fn check_result(&self, res: u32) {
assert_eq!(res, 0, "enc fail");
}
pub fn fail(&mut self, err: CoapError) {
assert_eq!(err, CoapError::OK, "enc fail");
}
pub fn to_void_ptr(&mut self) -> *mut c_void {
let ptr: *mut CoapContext = self;
ptr as *mut c_void
}
}
#[derive(PartialEq)]
pub enum CoapError {
OK = 0,
VALUE_NOT_UINT = 1,
}
impl ::core::fmt::Debug for CoapError {
fn fmt(&self, _fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
Ok(())
}
}
pub trait ToBytesOptionalNull {
fn to_bytes_optional_nul(&self) -> &[u8];
}
impl ToBytesOptionalNull for [u8] {
fn to_bytes_optional_nul(&self) -> &[u8] {
self
}
}
impl ToBytesOptionalNull for str {
fn to_bytes_optional_nul(&self) -> &[u8] {
self.as_bytes()
}
}
impl ToBytesOptionalNull for &str {
fn to_bytes_optional_nul(&self) -> &[u8] {
self.as_bytes()
}
}
impl ToBytesOptionalNull for CStr {
fn to_bytes_optional_nul(&self) -> &[u8] {
self.to_bytes_with_nul()
}
}
impl ToBytesOptionalNull for crate::Strn {
fn to_bytes_optional_nul(&self) -> &[u8] {
match self.rep {
StrnRep::ByteStr(bs) => { bs }
StrnRep::CStr(_cstr) => { assert!(false, "strn bytes"); &[] }
}
}
}
pub const _ROOT: &str = "root";
pub const _MAP: &str = "_map";
pub const _ARRAY: &str = "_array";