20#define min(a,b) ((a) < (b) ? (a) : (b))
25#if COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST
32#if COAP_SERVER_SUPPORT
48 context->observe_added_cb = observe_added;
49 context->observe_deleted_cb = observe_deleted;
50 context->observe_user_data = user_data;
51 context->observe_save_freq = save_freq ? save_freq : 1;
52 context->track_observe_value_cb = track_observe_value;
53 context->dyn_resource_added_cb = dyn_resource_added;
54 context->resource_deleted_cb = resource_deleted;
67 subs = coap_persist_observe_add_lkd(context,
88#if COAP_CONSTRAINED_STACK
105 if (e_listen_addr ==
NULL || s_addr_info ==
NULL || raw_packet ==
NULL)
112 ep = context->endpoint;
114 if (ep->proto == e_proto &&
115 memcmp(e_listen_addr, &ep->bind_addr,
sizeof(ep->bind_addr)) == 0)
121#ifndef INET6_ADDRSTRLEN
122#define INET6_ADDRSTRLEN 40
127 coap_log_warn(
"coap_persist_observe_add: Endpoint %s not defined\n",
140 data = raw_packet->
s;
141 data_len = raw_packet->
length;
148 session = coap_endpoint_get_session(ep, packet, now);
179 r = coap_get_resource_from_uri_path_lkd(session->
context,
182 coap_log_warn(
"coap_persist_observe_add: resource '%s' not defined\n",
186 if (!r->observable) {
187 coap_log_warn(
"coap_persist_observe_add: resource '%s' not observable\n",
196 s = coap_add_observer(r, session, &pdu->
actual_token, pdu);
200#if COAP_OSCORE_SUPPORT
217 const uint8_t *info_buf = oscore_info->
s;
218 size_t info_buf_len = oscore_info->
length;
226 int have_partial_iv = 0;
227 int have_id_context = 0;
242 if (oscore_key_id.
length > info_buf_len)
244 oscore_key_id.
s = info_buf;
245 info_buf += oscore_key_id.
length;
246 info_buf_len -= oscore_key_id.
length;
253 if (id_context.
length > info_buf_len)
255 id_context.
s = info_buf;
256 info_buf += id_context.
length;
257 info_buf_len -= id_context.
length;
269 if (aad.
length > info_buf_len)
273 info_buf_len -= aad.
length;
286 if (partial_iv.
length > info_buf_len)
288 partial_iv.
s = info_buf;
289 info_buf += partial_iv.
length;
290 info_buf_len -= partial_iv.
length;
303 if (nonce.
length > info_buf_len)
307 info_buf_len -= nonce.
length;
316 have_id_context ? &id_context :
NULL,
NULL,
317 &session->recipient_ctx);
319 session->oscore_encryption = 1;
321 session->recipient_ctx,
322 have_aad ? &aad :
NULL,
323 have_nonce ? &nonce :
NULL,
324 have_partial_iv ? &partial_iv :
NULL,
328 have_partial_iv ? &partial_iv :
NULL);
339 coap_log_warn(
"coap_persist_observe_add: discard malformed PDU\n");
346#if COAP_WITH_OBSERVE_PERSIST
363 assert(fp && observe_key && e_proto && e_listen_addr && s_addr_info &&
364 raw_packet && oscore_info);
369 if (fread(observe_key,
sizeof(*observe_key), 1, fp) == 1) {
371 if (fread(e_proto,
sizeof(*e_proto), 1, fp) != 1)
373 if (fread(e_listen_addr,
sizeof(*e_listen_addr), 1, fp) != 1)
375 if (fread(s_addr_info,
sizeof(*s_addr_info), 1, fp) != 1)
377 if (fread(&size,
sizeof(size), 1, fp) != 1)
379 if (size < 0 || size > 0x10000)
382 if ((scratch) ==
NULL)
384 if (fread(scratch->
s, scratch->
length, 1, fp) != 1)
388 if (fread(&size,
sizeof(size), 1, fp) != 1)
393 else if (size < 0 || size > 0x10000)
399 if (fread(scratch->
s, scratch->
length, 1, fp) != 1)
410 memset(e_proto, 0,
sizeof(*e_proto));
411 memset(e_listen_addr, 0,
sizeof(*e_listen_addr));
412 memset(s_addr_info, 0,
sizeof(*s_addr_info));
425 if (fwrite(&observe_key,
sizeof(observe_key), 1, fp) != 1)
427 if (fwrite(&e_proto,
sizeof(e_proto), 1, fp) != 1)
429 if (fwrite(&e_listen_addr,
sizeof(e_listen_addr),
432 if (fwrite(&s_addr_info,
sizeof(s_addr_info), 1, fp) != 1)
434 if (fwrite(&raw_packet->
length,
sizeof(raw_packet->
length), 1, fp) != 1)
436 if (fwrite(raw_packet->
s, raw_packet->
length, 1, fp) != 1)
439 if (fwrite(&oscore_info->
length,
sizeof(oscore_info->
length), 1, fp) != 1)
441 if (fwrite(oscore_info->
s, oscore_info->
length, 1, fp) != 1)
444 ssize_t not_defined = -1;
446 if (fwrite(¬_defined,
sizeof(not_defined), 1, fp) != 1)
462 FILE *fp_orig = fopen((
const char *)ctx->observe_save_file->s,
"r");
479 strcpy(
new, (
const char *)ctx->observe_save_file->s);
481 fp_new = fopen(
new,
"w+");
487 if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
488 &s_addr_info, &raw_packet, &oscore_info))
491 observe_key = coap_persist_observe_add_lkd(ctx, e_proto,
497 if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
498 s_addr_info, raw_packet, oscore_info))
511 if (fflush(fp_new) == EOF)
516 if (rename(
new, (
const char *)ctx->observe_save_file->s) == -1) {
518 new, (
const char *)ctx->observe_save_file->s,
548 FILE *fp_orig = fopen((
const char *)session->
context->observe_save_file->s,
562 session->
context->observe_save_file->length + 5);
566 strcpy(
new, (
const char *)session->
context->observe_save_file->s);
568 fp_new = fopen(
new,
"w+");
574 if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
575 &s_addr_info, &raw_packet, &oscore_info))
577 if (observe_key != a_observe_key) {
578 if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
579 s_addr_info, raw_packet, oscore_info))
593 if (!coap_op_observe_write(fp_new, a_observe_key, a_e_proto, *a_e_listen_addr,
594 *a_s_addr_info, a_raw_packet, a_oscore_info))
597 if (fflush(fp_new) == EOF)
603 (void)rename(
new, (
const char *)session->
context->observe_save_file->s);
628 FILE *fp_orig = fopen((
const char *)session->
context->observe_save_file->s,
644 session->
context->observe_save_file->length + 5);
648 strcpy(
new, (
const char *)session->
context->observe_save_file->s);
650 fp_new = fopen(
new,
"w+");
656 if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
657 &s_addr_info, &raw_packet, &oscore_info))
659 if (observe_key != d_observe_key) {
660 if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
675 if (fflush(fp_new) == EOF)
680 (void)rename(
new, (
const char *)session->
context->observe_save_file->s);
706 FILE *fp = fopen((
const char *)context->obs_cnt_save_file->s,
"r");
712 while (fgets(buf,
sizeof(buf), fp) !=
NULL) {
713 char *cp = strchr(buf,
' ');
715 uint32_t observe_num;
723 observe_num = atoi(cp);
728 observe_num = ((observe_num + context->observe_save_freq) /
729 context->observe_save_freq) *
730 context->observe_save_freq - 1;
731 resource_key.
s = (uint8_t *)buf;
732 resource_key.
length = strlen(buf);
733 r = coap_get_resource_from_uri_path_lkd(context, &resource_key);
735 coap_log_debug(
"persist: Initial observe number being updated '%s' %u\n",
745coap_op_obs_cnt_commit(FILE *src, FILE *dst,
char *tmp,
const char *path) {
746 int ok = !src || !ferror(src);
748 if (fclose(dst) != 0)
754 ok = MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING) != 0;
756 ok = rename(tmp, path) == 0;
760 coap_log_warn(
"persist: could not update Observe counter file '%s'\n", path);
775 uint32_t n_observe_num,
777 FILE *fp_orig = fopen((
const char *)context->obs_cnt_save_file->s,
"r");
784 if (!fp_orig && errno != ENOENT)
790 strcpy(
new, (
const char *)context->obs_cnt_save_file->s);
792 fp_new = fopen(
new,
"w+");
797 while (fp_orig && fgets(buf,
sizeof(buf), fp_orig) !=
NULL) {
798 char *cp = strchr(buf,
' ');
799 uint32_t observe_num;
807 observe_num = atoi(cp);
808 resource_key.
s = (uint8_t *)buf;
809 resource_key.
length = strlen(buf);
811 if (fprintf(fp_new,
"%s %u\n", resource_key.
s, observe_num) < 0)
815 if (fprintf(fp_new,
"%s %u\n", resource_name->
s, n_observe_num) < 0)
817 return coap_op_obs_cnt_commit(fp_orig, fp_new,
new,
818 (
const char *)context->obs_cnt_save_file->s);
837 FILE *fp_orig = fopen((
const char *)context->obs_cnt_save_file->s,
"r");
843 return errno == ENOENT;
848 strcpy(
new, (
const char *)context->obs_cnt_save_file->s);
850 fp_new = fopen(
new,
"w+");
855 while (fgets(buf,
sizeof(buf), fp_orig) !=
NULL) {
856 char *cp = strchr(buf,
' ');
857 uint32_t observe_num;
865 observe_num = atoi(cp);
866 resource_key.
s = (uint8_t *)buf;
867 resource_key.
length = strlen(buf);
869 if (fprintf(fp_new,
"%s %u\n", resource_key.
s, observe_num) < 0)
873 return coap_op_obs_cnt_commit(fp_orig, fp_new,
new,
874 (
const char *)context->obs_cnt_save_file->s);
887#define COAP_DYN_RESOURCE_MAX_FIELD_SIZE 0x10000
895coap_op_dyn_resource_read(FILE *fp,
coap_proto_t *e_proto,
904 nread = fread(e_proto, 1,
sizeof(*e_proto), fp);
905 if (nread == 0 && feof(fp) && !ferror(fp))
907 if (nread ==
sizeof(*e_proto)) {
909 if (fread(&size,
sizeof(size), 1, fp) != 1)
911 if (size < 0 || size > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
916 if (fread((*name)->s, 1, size, fp) != (
size_t)size)
918 if (fread(&size,
sizeof(size), 1, fp) != 1)
920 if (size <= 0 || size > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
925 if (fread((*raw_packet)->s, size, 1, fp) != 1)
937coap_op_dyn_resource_write(FILE *fp,
coap_proto_t e_proto,
940 if (name->
length > COAP_DYN_RESOURCE_MAX_FIELD_SIZE || !raw_packet->
length ||
941 raw_packet->
length > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
943 if (fwrite(&e_proto,
sizeof(e_proto), 1, fp) != 1)
945 if (fwrite(&name->
length,
sizeof(name->
length), 1, fp) != 1)
949 if (fwrite(&raw_packet->
length,
sizeof(raw_packet->
length), 1, fp) != 1)
951 if (fwrite(raw_packet->
s, raw_packet->
length, 1, fp) != 1)
966 FILE *fp_orig =
NULL;
979 fp_orig = fopen((
const char *)ctx->dyn_resource_save_file->s,
"rb");
991 if (coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet) != 1)
997 coap_log_debug(
"persist: dynamic resource '%s' being re-created\n", name->
s);
1006 session->
proto = e_proto;
1008 raw_packet->
length, request)) {
1011 if (request->
code == 0 || request->
code > 7) {
1015 r = coap_add_dynamic_resource(session, request);
1017 r = ctx->unknown_resource;
1019 if (!r || !r->handler[request->
code-1])
1035 r->handler[request->
code-1](r,
1069 int status = coap_op_dyn_resource_read(src, &proto, &name, &packet);
1085 const char *path = (
const char *)file->
s;
1086 FILE *src = fopen(path,
"rb");
1092 if (errno != ENOENT)
1101 memcpy(tmp, path, file->length);
1102 memcpy(tmp + file->length,
".tmp",
sizeof(
".tmp"));
1103 dst = fopen(tmp,
"wb");
1107 ok = (!src || coap_op_dyn_resource_copy(src, dst, name)) &&
1108 (!packet || coap_op_dyn_resource_write(dst, proto, name, packet));
1109 if (fclose(dst) != 0)
1117 ok = MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING) != 0;
1119 ok = rename(tmp, path) == 0;
1130 coap_log_warn(
"persist: could not update dynamic resource file '%s'\n", path);
1140 return coap_op_dyn_resource_update(session->
context->dyn_resource_save_file,
1141 resource_name, session->
proto, packet);
1151 if (context->obs_cnt_save_file)
1152 counter_ok = coap_op_obs_cnt_deleted(context, resource_name);
1153 if (context->dyn_resource_save_file &&
1154 !coap_op_dyn_resource_update(context->dyn_resource_save_file,
1162 const char *dyn_resource_save_file,
1163 const char *observe_save_file,
1164 const char *obs_cnt_save_file,
1165 uint32_t save_freq) {
1169 ret = coap_persist_startup_lkd(context,
1170 dyn_resource_save_file,
1180 const char *dyn_resource_save_file,
1181 const char *observe_save_file,
1182 const char *obs_cnt_save_file,
1183 uint32_t save_freq) {
1185 if (dyn_resource_save_file) {
1186 context->dyn_resource_save_file =
1188 strlen(dyn_resource_save_file));
1189 if (!context->dyn_resource_save_file)
1191 coap_op_dyn_resource_load_disk(context);
1192 context->dyn_resource_added_cb = coap_op_dyn_resource_added;
1193 context->resource_deleted_cb = coap_op_resource_deleted;
1195 if (obs_cnt_save_file) {
1196 context->obs_cnt_save_file =
1198 strlen(obs_cnt_save_file));
1199 if (!context->obs_cnt_save_file)
1201 context->observe_save_freq = save_freq ? save_freq : 1;
1202 coap_op_obs_cnt_load_disk(context);
1203 context->track_observe_value_cb = coap_op_obs_cnt_track_observe;
1204 context->resource_deleted_cb = coap_op_resource_deleted;
1206 if (observe_save_file) {
1207 context->observe_save_file =
1209 strlen(observe_save_file));
1210 if (!context->observe_save_file)
1212 coap_op_observe_load_disk(context);
1213 context->observe_added_cb = coap_op_observe_added;
1214 context->observe_deleted_cb = coap_op_observe_deleted;
1224 context->dyn_resource_save_file =
NULL;
1225 context->obs_cnt_save_file =
NULL;
1226 context->observe_save_file =
NULL;
1238 coap_persist_stop_lkd(context);
1244 if (context ==
NULL)
1247 context->observe_no_clear = 1;
1248 coap_persist_cleanup(context);
1253 const char *dyn_resource_save_file,
1254 const char *observe_save_file,
1255 const char *obs_cnt_save_file,
1256 uint32_t save_freq) {
1258 (void)dyn_resource_save_file;
1259 (void)observe_save_file;
1260 (void)obs_cnt_save_file;
1267 context->observe_no_clear = 1;
struct coap_endpoint_t coap_endpoint_t
struct coap_subscription_t coap_subscription_t
struct coap_resource_t coap_resource_t
const char * coap_socket_strerror(void)
Library specific build wrapper for coap_internal.h.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_RESOURCE_SAFE_REQUEST_HANDLER
Don't lock this resource when calling app call-back handler for requests as handler will not be manip...
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
#define coap_lock_specific_callback_release(lock, func, failed)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_callback_release(func, failed)
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_get_log_level(void)
Get the current logging level.
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
#define coap_log_warn(...)
COAP_API coap_subscription_t * coap_persist_observe_add(coap_context_t *context, coap_proto_t e_proto, const coap_address_t *e_listen_addr, const coap_addr_tuple_t *s_addr_info, const coap_bin_const_t *raw_packet, const coap_bin_const_t *oscore_info)
Set up an active subscription for an observe that was previously active over a coap-server inadverten...
int(* coap_dyn_resource_added_t)(coap_session_t *session, coap_str_const_t *resource_name, coap_bin_const_t *raw_packet, void *user_data)
Callback handler definition called when a dynamic resource is getting created, as defined in coap_per...
void coap_persist_set_observe_num(coap_resource_t *resource, uint32_t observe_num)
Sets the current observe number value.
int(* coap_resource_deleted_t)(coap_context_t *context, coap_str_const_t *resource_name, void *user_data)
Callback handler definition called when resource is removed, as defined in coap_persist_track_funcs()...
int(* coap_observe_added_t)(coap_session_t *session, coap_subscription_t *observe_key, coap_proto_t e_proto, coap_address_t *e_listen_addr, coap_addr_tuple_t *s_addr_info, coap_bin_const_t *raw_packet, coap_bin_const_t *oscore_info, void *user_data)
Callback handler definition called when a new observe has been set up, as defined in coap_persist_tra...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
COAP_API void coap_persist_stop(coap_context_t *context)
Stop tracking persist information, leaving the current persist information in the files defined in co...
void coap_persist_track_funcs(coap_context_t *context, coap_observe_added_t observe_added, coap_observe_deleted_t observe_deleted, coap_track_observe_value_t track_observe_value, coap_dyn_resource_added_t dyn_resource_added, coap_resource_deleted_t resource_deleted, uint32_t save_freq, void *user_data)
Set up callbacks to handle persist tracking so on a coap-server inadvertent restart,...
int(* coap_track_observe_value_t)(coap_context_t *context, coap_str_const_t *resource_name, uint32_t observe_num, void *user_data)
Callback handler definition called when an observe unsolicited response is being sent,...
int(* coap_observe_deleted_t)(coap_session_t *session, coap_subscription_t *observe_key, void *user_data)
Callback handler definition called when an observe is being removed, as defined in coap_persist_track...
COAP_API int coap_persist_startup(coap_context_t *context, const char *dyn_resource_save_file, const char *observe_save_file, const char *obs_cnt_save_file, uint32_t save_freq)
Start up persist tracking using the libcoap module.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
uint64_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_size)
#define CBOR_SIMPLE_VALUE
uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_size)
int oscore_new_association(coap_session_t *session, coap_pdu_t *sent_pdu, coap_bin_const_t *token, oscore_recipient_ctx_t *recipient_ctx, coap_bin_const_t *aad, coap_bin_const_t *nonce, coap_bin_const_t *partial_iv, int is_observe)
oscore_ctx_t * oscore_find_context(coap_session_t *session, const coap_bin_const_t rcpkey_id, const coap_bin_const_t *ctxkey_id, uint8_t *oscore_r2, oscore_recipient_ctx_t **recipient_ctx)
oscore_find_context - Locate recipient context (and hence OSCORE context)
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
@ COAP_REQUEST_CODE_FETCH
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
int coap_observe_persist_is_supported(void)
Check whether Observe Persist is available.
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Multi-purpose address abstraction.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
The CoAP stack's global state is stored in a coap_context_t object.
coap_resource_dynamic_create_t dyn_create_handler
Dynamic resource create handler.
Iterator to run through PDU options.
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char * payload
payload
int ifindex
the interface index
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
size_t used_size
used bytes of storage for token, options and payload
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_proto_t proto
protocol used
coap_context_t * context
session's context
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
CoAP string data definition.