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
use cstr_core::CStr;
use cty::*;
use crate::{
sys::console,
encoding::tinycbor::CborEncoder,
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 {
pub fn json_set_text_string(&mut self, key: &Strn, value: &Strn) {
let key_cstr: *const u8 =
match key.rep {
StrnRep::ByteStr(bs) => { self.key_to_cstr(bs) }
StrnRep::CStr(cstr) => { cstr }
};
let value_cstr: *const u8 =
match value.rep {
StrnRep::ByteStr(bs) => { self.value_to_cstr(bs) }
StrnRep::CStr(cstr) => { cstr }
};
unsafe {
crate::libs::mynewt_rust::json_helper_set_text_string(
self.to_void_ptr(),
key_cstr as *const c_char,
value_cstr as *const c_char
)
};
}
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(Debug, PartialEq)]
pub enum CoapError {
OK = 0,
VALUE_NOT_UINT = 1,
}
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()
}
}
pub const _ROOT: &str = "root";
pub const _MAP: &str = "_map";
pub const _ARRAY: &str = "_array";