Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ce_tlv.h Source File

ce_tlv.h

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2018 ARM Ltd.
00003 //  
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //  
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //  
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 
00017 #ifndef __CE_TLV_H__
00018 #define __CE_TLV_H__
00019 
00020 #ifdef __cplusplus
00021 extern "C" {
00022 #endif
00023 #include <stdint.h>
00024 #include <stdbool.h>
00025 #include <stddef.h>
00026 
00027 typedef enum  {
00028     CE_TLV_STATUS_SUCCESS,
00029     CE_TLV_STATUS_END,
00030     CE_TLV_STATUS_INVALID_ARG,
00031     CE_TLV_STATUS_TEXT_NOT_TERMINATED,
00032     CE_TLV_STATUS_MALFORMED_TLV,
00033     CE_TLV_STATUS_ERROR,
00034     CE_TLV_STATUS_ENCODER_INSUFFICIENT_BUFFER
00035 } ce_tlv_status_e;
00036 
00037 typedef enum {
00038     CE_TLV_TYPE_CERT_NAME = 1
00039 } ce_tlv_type_e;
00040 
00041 typedef struct ce_tlv_element_ {
00042     const uint8_t *_current; // 4 bytes
00043     const uint8_t *_end; // 4 bytes
00044     union { 
00045         const uint8_t *bytes;
00046         const char *text;
00047         int integer;
00048     } val; // 4 bytes
00049     bool is_required;
00050     uint16_t type; // 2 bytes
00051     uint16_t len; // 2 bytes
00052 } ce_tlv_element_s;
00053 
00054 ce_tlv_status_e ce_tlv_parser_init(const uint8_t *tlv_buf, size_t tlv_buf_len, ce_tlv_element_s *element_out);
00055 ce_tlv_status_e ce_tlv_parse_next(ce_tlv_element_s *element);
00056 
00057 /* Checks if the given element is required or optional by testing 
00058 * the "type" MSB bit. [0 == required] while [1 == optional]
00059 *
00060 * @param element[IN] The element to test
00061 *
00062 * @return "true" if element is required, "false" otherwise.
00063 */
00064 bool is_required(const ce_tlv_element_s *element);
00065 
00066 #ifdef CERT_RENEWAL_TEST
00067 typedef struct ce_tlv_encoder_ {
00068     uint8_t *buf; // 4 bytes
00069     uint16_t encoded_length; // 2 bytes
00070     uint16_t _buf_size; // 2 bytes
00071 } ce_tlv_encoder_s;
00072 
00073 void ce_tlv_encoder_init(uint8_t *buf, uint16_t buf_size, ce_tlv_encoder_s *encoder);
00074 ce_tlv_status_e tlv_add_str(ce_tlv_type_e type, uint16_t length, const char *value, bool is_tlv_required, ce_tlv_encoder_s *encoder);
00075 
00076 #endif // CERT_RENEWAL_TEST
00077 
00078 #ifdef __cplusplus
00079 }
00080 #endif
00081 
00082 #endif // __CE_TLV_H__
00083