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
//! COAP encoder state used by CoAP encoding macros

use cstr_core::CStr;      //  Import string utilities from `cstr_core` library: https://crates.io/crates/cstr_core
use cty::*;               //  Import C types from cty library: https://crates.io/crates/cty
use crate::{
    sys::console,
    encoding::tinycbor::CborEncoder,
    fill_zero, Strn, StrnRep,
};

/// Global instance that contains the current state of the CoAP encoder. Only 1 encoding task is supported at a time.
pub static mut COAP_CONTEXT: CoapContext = fill_zero!(CoapContext);

/// CoAP encoder state. Buffers the next key and value to be encoded.
#[derive(Default)]
pub struct CoapContext {
    /// Static buffer for the key to be encoded. Will be passed to Mynewt COAP encoder API.  Always null-terminated.
    key_buffer: [u8; COAP_KEY_SIZE],
    /// Static buffer for the string value to be encoded. Will be passed to Mynewt COAP encoder API.  Always null-terminated.
    value_buffer: [u8; COAP_VALUE_SIZE],
}

/// Size of the static key buffer
const COAP_KEY_SIZE: usize = 32;
/// Size of the static value buffer
const COAP_VALUE_SIZE: usize = 32;

/// Global CBOR root map
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
            )
        };
    }

    /// Given a key `s`, return a `*char` pointer that is null-terminated. Used for encoding COAP keys.
    /// If `s` is null-terminated, return it as a pointer. Else copy `s` to the static buffer,
    /// append null and return the buffer as a pointer.
    pub fn key_to_cstr(&mut self, s: &[u8]) -> *const u8 {                
        //  If null-terminated, return as pointer.
        if s.last() == Some(&0) { return s.as_ptr() as *const u8; }
        //  Else copy into static key buffer and return pointer to buffer.
        assert!(s.len() < COAP_KEY_SIZE, "big key");  //  Key too long
        self.key_buffer[..s.len()].copy_from_slice(s);
        self.key_buffer[s.len()] = 0;
        self.key_buffer.as_ptr() as *const u8
    }

    /// Given a value `s`, return a `*char` pointer that is null-terminated. Used for encoding COAP values.
    /// If `s` is null-terminated, return it as a pointer. Else copy `s` to the static buffer,
    /// append null and return the buffer as a pointer.
    pub fn value_to_cstr(&mut self, s: &[u8]) -> *const u8 {
        //  If null-terminated, return as pointer.
        if s.last() == Some(&0) { return s.as_ptr() as *const u8; }
        //  Else copy into static value buffer and return pointer to buffer.
        assert!(s.len() < COAP_VALUE_SIZE, "big value");  //  Value too long
        self.value_buffer[..s.len()].copy_from_slice(s);
        self.value_buffer[s.len()] = 0;
        self.value_buffer.as_ptr() as *const u8
    }

    /// Compute the byte length of the string in `s`.
    /// If `s` is null-terminated, return length of `s` - 1. Else return length of `s`.
    pub fn cstr_len(&self, s: &[u8]) -> usize {
        //  If null-terminated, return length - 1.
        if s.last() == Some(&0) { return s.len() - 1; }
        s.len()
    }

    /// Return the global CBOR encoder
    pub fn global_encoder(&self) -> *mut super::tinycbor::CborEncoder {
        unsafe { &mut super::g_encoder }
    }

    /// Create a new CBOR encoder for the current map or array, e.g. `key=root, suffix=_map` 
    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");
        //  TODO: Allow multiple keys
        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");  //  TODO: No such encoder.
            unsafe { &mut super::root_map }
        }        
    }

    /// Return the CBOR encoder for the current map or array, e.g. `key=root, suffix=_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");
        //  TODO: Allow multiple keys
        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");  //  TODO: No such encoder.
            unsafe { &mut super::root_map }
        }        
    }

    /// Fail the encoding with an error if `res` is non-zero.
    pub fn check_result(&self, res: u32) {
        assert_eq!(res, 0, "enc fail");
    }

    /// Fail the encoding with an error
    pub fn fail(&mut self, err: CoapError) {
        assert_eq!(err, CoapError::OK, "enc fail");
    }

    /// Cast itself as a `*mut c_void`
    pub fn to_void_ptr(&mut self) -> *mut c_void {
        let ptr: *mut CoapContext = self;
        ptr as *mut c_void
    }
}

/// Error codes for COAP encoding failure
#[derive(Debug, PartialEq)]
pub enum CoapError {
    /// No error
    OK = 0,
    /// Encoded value is not unsigned integer
    VALUE_NOT_UINT = 1,
}

/// Convert the type to array of bytes that may or may not end with null
pub trait ToBytesOptionalNull {
    /// Convert the type to array of bytes that may or may not end with null
    fn to_bytes_optional_nul(&self) -> &[u8];
}

/// Convert the type to array of bytes that may or may not end with null
impl ToBytesOptionalNull for [u8] {
    /// Convert the type to array of bytes that may or may not end with null
    fn to_bytes_optional_nul(&self) -> &[u8] {
        self
    }
}

/// Convert the type to array of bytes that may or may not end with null
impl ToBytesOptionalNull for str {
    /// Convert the type to array of bytes that may or may not end with null
    fn to_bytes_optional_nul(&self) -> &[u8] {
        self.as_bytes()
    }
}

/// Convert the type to array of bytes that may or may not end with null
impl ToBytesOptionalNull for &str {
    /// Convert the type to array of bytes that may or may not end with null
    fn to_bytes_optional_nul(&self) -> &[u8] {
        self.as_bytes()
    }
}

/// Convert the type to array of bytes that may or may not end with null. CStr always ends with null.
impl ToBytesOptionalNull for CStr {
    /// Convert the type to array of bytes that may or may not end with null. CStr always ends with null.
    fn to_bytes_optional_nul(&self) -> &[u8] {
        self.to_bytes_with_nul()
    }
}

/*
/// Convert the type to array of bytes that may or may not end with null. Strn always ends with null.
impl ToBytesOptionalNull for crate::Strn {
    /// Convert the type to array of bytes that may or may not end with null. Strn always ends with null.
    fn to_bytes_optional_nul(&self) -> &[u8] {
        if self.cstr != 0 as *const u8 { 
            return unsafe { ::core::mem::transmute::<*const u8, &[u8]>(self.cstr) };
            //return self.cstr as &[u8]; 
        }
        self.bytestr
    }
}
*/

/// Root of CoAP document
pub const _ROOT: &str = "root";

/// Map element of CoAP document
pub const _MAP: &str = "_map";

/// Array element of CoAP document
pub const _ARRAY: &str = "_array";