Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-cloud-workshop-connect-HTS221 by
cn-create.c
00001 #ifndef CN_CREATE_C 00002 #define CN_CREATE_C 00003 00004 #ifdef __cplusplus 00005 extern "C" { 00006 #endif 00007 00008 #include <string.h> 00009 #include <stdlib.h> 00010 00011 #include "cn-cbor.h" 00012 #include "cbor.h" 00013 00014 #define INIT_CB(v) \ 00015 if (errp) {errp->err = CN_CBOR_NO_ERROR;} \ 00016 (v) = CN_CALLOC_CONTEXT(); \ 00017 if (!(v)) { if (errp) {errp->err = CN_CBOR_ERR_OUT_OF_MEMORY;} return NULL; } 00018 00019 cn_cbor* cn_cbor_map_create(CBOR_CONTEXT_COMMA cn_cbor_errback *errp) 00020 { 00021 cn_cbor* ret; 00022 INIT_CB(ret); 00023 00024 ret->type = CN_CBOR_MAP; 00025 ret->flags |= CN_CBOR_FL_COUNT; 00026 00027 return ret; 00028 } 00029 00030 cn_cbor* cn_cbor_data_create(const uint8_t* data, int len 00031 CBOR_CONTEXT, 00032 cn_cbor_errback *errp) 00033 { 00034 cn_cbor* ret; 00035 INIT_CB(ret); 00036 00037 ret->type = CN_CBOR_BYTES; 00038 ret->length = len; 00039 ret->v.str = (const char*) data; // TODO: add v.ustr to the union? 00040 00041 return ret; 00042 } 00043 00044 cn_cbor* cn_cbor_text_create(const uint8_t* data, int len 00045 CBOR_CONTEXT, 00046 cn_cbor_errback *errp) 00047 { 00048 cn_cbor* ret; 00049 INIT_CB(ret); 00050 00051 ret->type = CN_CBOR_TEXT; 00052 ret->length = len; 00053 ret->v.str = (const char*)data; 00054 00055 return ret; 00056 } 00057 00058 cn_cbor* cn_cbor_string_create(const char* data 00059 CBOR_CONTEXT, 00060 cn_cbor_errback *errp) 00061 { 00062 cn_cbor* ret; 00063 INIT_CB(ret); 00064 00065 ret->type = CN_CBOR_TEXT; 00066 ret->length = strlen(data); 00067 ret->v.str = data; 00068 00069 return ret; 00070 } 00071 00072 cn_cbor* cn_cbor_int_create(int64_t value 00073 CBOR_CONTEXT, 00074 cn_cbor_errback *errp) 00075 { 00076 cn_cbor* ret; 00077 INIT_CB(ret); 00078 00079 if (value<0) { 00080 ret->type = CN_CBOR_INT; 00081 ret->v.sint = value; 00082 } else { 00083 ret->type = CN_CBOR_UINT; 00084 ret->v.uint = value; 00085 } 00086 00087 return ret; 00088 } 00089 00090 cn_cbor* cn_cbor_uint_create(uint64_t value 00091 CBOR_CONTEXT, 00092 cn_cbor_errback *errp) 00093 { 00094 cn_cbor* ret; 00095 INIT_CB(ret); 00096 00097 ret->type = CN_CBOR_UINT; 00098 ret->v.uint = value; 00099 00100 return ret; 00101 } 00102 00103 static bool _append_kv(cn_cbor *cb_map, cn_cbor *key, cn_cbor *val) 00104 { 00105 //Connect key and value and insert them into the map. 00106 key->parent = cb_map; 00107 key->next = val; 00108 val->parent = cb_map; 00109 val->next = NULL; 00110 00111 if(cb_map->last_child) { 00112 cb_map->last_child->next = key; 00113 } else { 00114 cb_map->first_child = key; 00115 } 00116 cb_map->last_child = val; 00117 cb_map->length += 2; 00118 return true; 00119 } 00120 00121 bool cn_cbor_map_put(cn_cbor* cb_map, 00122 cn_cbor *cb_key, cn_cbor *cb_value, 00123 cn_cbor_errback *errp) 00124 { 00125 //Make sure input is a map. Otherwise 00126 if(!cb_map || !cb_key || !cb_value || cb_map->type != CN_CBOR_MAP) 00127 { 00128 if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;} 00129 return false; 00130 } 00131 00132 return _append_kv(cb_map, cb_key, cb_value); 00133 } 00134 00135 bool cn_cbor_mapput_int(cn_cbor* cb_map, 00136 int64_t key, cn_cbor* cb_value 00137 CBOR_CONTEXT, 00138 cn_cbor_errback *errp) 00139 { 00140 cn_cbor* cb_key; 00141 00142 //Make sure input is a map. Otherwise 00143 if(!cb_map || !cb_value || cb_map->type != CN_CBOR_MAP) 00144 { 00145 if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;} 00146 return false; 00147 } 00148 00149 cb_key = cn_cbor_int_create(key CBOR_CONTEXT_PARAM, errp); 00150 if (!cb_key) { return false; } 00151 return _append_kv(cb_map, cb_key, cb_value); 00152 } 00153 00154 bool cn_cbor_mapput_string(cn_cbor* cb_map, 00155 const char* key, cn_cbor* cb_value 00156 CBOR_CONTEXT, 00157 cn_cbor_errback *errp) 00158 { 00159 cn_cbor* cb_key; 00160 00161 //Make sure input is a map. Otherwise 00162 if(!cb_map || !cb_value || cb_map->type != CN_CBOR_MAP) 00163 { 00164 if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;} 00165 return false; 00166 } 00167 00168 cb_key = cn_cbor_string_create(key CBOR_CONTEXT_PARAM, errp); 00169 if (!cb_key) { return false; } 00170 return _append_kv(cb_map, cb_key, cb_value); 00171 } 00172 00173 cn_cbor* cn_cbor_array_create(CBOR_CONTEXT_COMMA cn_cbor_errback *errp) 00174 { 00175 cn_cbor* ret; 00176 INIT_CB(ret); 00177 00178 ret->type = CN_CBOR_ARRAY; 00179 ret->flags |= CN_CBOR_FL_COUNT; 00180 00181 return ret; 00182 } 00183 00184 bool cn_cbor_array_append(cn_cbor* cb_array, 00185 cn_cbor* cb_value, 00186 cn_cbor_errback *errp) 00187 { 00188 //Make sure input is an array. 00189 if(!cb_array || !cb_value || cb_array->type != CN_CBOR_ARRAY) 00190 { 00191 if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;} 00192 return false; 00193 } 00194 00195 cb_value->parent = cb_array; 00196 cb_value->next = NULL; 00197 if(cb_array->last_child) { 00198 cb_array->last_child->next = cb_value; 00199 } else { 00200 cb_array->first_child = cb_value; 00201 } 00202 cb_array->last_child = cb_value; 00203 cb_array->length++; 00204 return true; 00205 } 00206 00207 #ifdef __cplusplus 00208 } 00209 #endif 00210 00211 #endif /* CN_CBOR_C */
Generated on Tue Jul 12 2022 19:12:11 by
1.7.2
