Coverage Report

Created: 2026-12-01 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcoap/src/coap_subscribe.c
Line
Count
Source
1
/* coap_subscribe.c -- subscription handling for CoAP
2
 *                see RFC7641
3
 *
4
 * Copyright (C) 2010-2019,2022-2026 Olaf Bergmann <bergmann@tzi.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * This file is part of the CoAP library libcoap. Please see
9
 * README for terms of use.
10
 */
11
12
/**
13
 * @file coap_subscribe.c
14
 * @brief Subscription handling functions
15
 */
16
17
#include "coap3/coap_libcoap_build.h"
18
19
#ifndef min
20
#define min(a,b) ((a) < (b) ? (a) : (b))
21
#endif
22
23
int
24
0
coap_observe_persist_is_supported(void) {
25
0
#if COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST
26
0
  return 1;
27
#else /* ! (COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST) */
28
  return 0;
29
#endif /* ! (COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST) */
30
0
}
31
32
#if COAP_SERVER_SUPPORT
33
void
34
0
coap_subscription_init(coap_subscription_t *s) {
35
0
  assert(s);
36
0
  memset(s, 0, sizeof(coap_subscription_t));
37
0
}
38
39
void
40
coap_persist_track_funcs(coap_context_t *context,
41
                         coap_observe_added_t observe_added,
42
                         coap_observe_deleted_t observe_deleted,
43
                         coap_track_observe_value_t track_observe_value,
44
                         coap_dyn_resource_added_t dyn_resource_added,
45
                         coap_resource_deleted_t resource_deleted,
46
                         uint32_t save_freq,
47
0
                         void *user_data) {
48
0
  context->observe_added_cb = observe_added;
49
0
  context->observe_deleted_cb = observe_deleted;
50
0
  context->observe_user_data = user_data;
51
0
  context->observe_save_freq = save_freq ? save_freq : 1;
52
0
  context->track_observe_value_cb = track_observe_value;
53
0
  context->dyn_resource_added_cb = dyn_resource_added;
54
0
  context->resource_deleted_cb = resource_deleted;
55
0
}
56
57
COAP_API coap_subscription_t *
58
coap_persist_observe_add(coap_context_t *context,
59
                         coap_proto_t e_proto,
60
                         const coap_address_t *e_listen_addr,
61
                         const coap_addr_tuple_t *s_addr_info,
62
                         const coap_bin_const_t *raw_packet,
63
0
                         const coap_bin_const_t *oscore_info) {
64
0
  coap_subscription_t *subs;
65
66
0
  coap_lock_lock(return NULL);
67
0
  subs = coap_persist_observe_add_lkd(context,
68
0
                                      e_proto,
69
0
                                      e_listen_addr,
70
0
                                      s_addr_info,
71
0
                                      raw_packet,
72
0
                                      oscore_info);
73
0
  coap_lock_unlock();
74
0
  return subs;
75
0
}
76
77
coap_subscription_t *
78
coap_persist_observe_add_lkd(coap_context_t *context,
79
                             coap_proto_t e_proto,
80
                             const coap_address_t *e_listen_addr,
81
                             const coap_addr_tuple_t *s_addr_info,
82
                             const coap_bin_const_t *raw_packet,
83
0
                             const coap_bin_const_t *oscore_info) {
84
0
  coap_session_t *session = NULL;
85
0
  const uint8_t *data;
86
0
  size_t data_len;
87
0
  coap_pdu_t *pdu = NULL;
88
#if COAP_CONSTRAINED_STACK
89
  /* e_packet can be protected by global_lock if needed */
90
  static coap_packet_t e_packet;
91
#else /* ! COAP_CONSTRAINED_STACK */
92
0
  coap_packet_t e_packet;
93
0
#endif /* ! COAP_CONSTRAINED_STACK */
94
0
  coap_packet_t *packet = &e_packet;
95
0
  coap_tick_t now;
96
0
  coap_string_t *uri_path = NULL;
97
0
  coap_opt_iterator_t opt_iter;
98
0
  coap_opt_t *observe;
99
0
  int observe_action;
100
0
  coap_resource_t *r;
101
0
  coap_subscription_t *s = NULL;
102
0
  coap_endpoint_t *ep;
103
104
0
  coap_lock_check_locked();
105
0
  if (e_listen_addr == NULL || s_addr_info == NULL || raw_packet == NULL)
106
0
    return NULL;
107
108
  /* Will be creating a local 'open' session */
109
0
  if (e_proto != COAP_PROTO_UDP)
110
0
    return NULL;
111
112
0
  ep = context->endpoint;
113
0
  while (ep) {
114
0
    if (ep->proto == e_proto &&
115
0
        memcmp(e_listen_addr, &ep->bind_addr, sizeof(ep->bind_addr)) == 0)
116
0
      break;
117
0
    ep = ep->next;
118
0
  }
119
0
  if (!ep) {
120
0
    if (COAP_LOG_WARN <= coap_get_log_level()) {
121
#ifndef INET6_ADDRSTRLEN
122
#define INET6_ADDRSTRLEN 40
123
#endif
124
0
      unsigned char addr_str[INET6_ADDRSTRLEN + 8];
125
126
0
      if (coap_print_addr(e_listen_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
127
0
        coap_log_warn("coap_persist_observe_add: Endpoint %s not defined\n",
128
0
                      addr_str);
129
0
      }
130
0
    }
131
0
    return NULL;
132
0
  }
133
134
  /* Build up packet */
135
0
  memcpy(&packet->addr_info, s_addr_info, sizeof(packet->addr_info));
136
0
  packet->ifindex = 0;
137
0
  memcpy(&packet->payload, &raw_packet->s, sizeof(packet->payload));
138
0
  packet->length = raw_packet->length;
139
140
0
  data = raw_packet->s;
141
0
  data_len = raw_packet->length;
142
0
  if (data_len < 4)
143
0
    goto malformed;
144
145
  /* Get the session */
146
147
0
  coap_ticks(&now);
148
0
  session = coap_endpoint_get_session(ep, packet, now);
149
0
  if (session == NULL)
150
0
    goto fail;
151
  /* Need max space in case PDU is updated with updated token, huge size etc. */
152
0
  pdu = coap_pdu_init(0, 0, 0, 0);
153
0
  if (!pdu)
154
0
    goto fail;
155
156
0
  if (!coap_pdu_parse(session->proto, data, data_len, pdu)) {
157
0
    goto malformed;
158
0
  }
159
0
  pdu->max_size = pdu->used_size;
160
161
0
  if (pdu->code != COAP_REQUEST_CODE_GET &&
162
0
      pdu->code != COAP_REQUEST_CODE_FETCH)
163
0
    goto malformed;
164
165
0
  observe = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
166
0
  if (observe == NULL)
167
0
    goto malformed;
168
0
  observe_action = coap_decode_var_bytes(coap_opt_value(observe),
169
0
                                         coap_opt_length(observe));
170
0
  if (observe_action != COAP_OBSERVE_ESTABLISH)
171
0
    goto malformed;
172
173
  /* Get the resource */
174
175
0
  uri_path = coap_get_uri_path(pdu);
176
0
  if (!uri_path)
177
0
    goto malformed;
178
179
0
  r = coap_get_resource_from_uri_path_lkd(session->context,
180
0
                                          (coap_str_const_t *)uri_path);
181
0
  if (r == NULL) {
182
0
    coap_log_warn("coap_persist_observe_add: resource '%s' not defined\n",
183
0
                  uri_path->s);
184
0
    goto fail;
185
0
  }
186
0
  if (!r->observable) {
187
0
    coap_log_warn("coap_persist_observe_add: resource '%s' not observable\n",
188
0
                  uri_path->s);
189
0
    goto fail;
190
0
  }
191
0
  coap_delete_string(uri_path);
192
0
  uri_path = NULL;
193
194
  /* Create / update subscription for observing */
195
  /* Now set up the subscription */
196
0
  s = coap_add_observer(r, session, &pdu->actual_token, pdu);
197
0
  if (s == NULL)
198
0
    goto fail;
199
200
0
#if COAP_OSCORE_SUPPORT
201
0
  if (oscore_info) {
202
0
    coap_log_debug("persist: OSCORE association being updated\n");
203
    /*
204
     * Need to track the association used for tracking this observe, done as
205
     * a CBOR array. Written in coap_add_observer().
206
     *
207
     * If an entry is null, then use nil, else a set of bytes
208
     *
209
     * Currently tracking 5 items
210
     *  recipient_id
211
     *  id_context
212
     *  aad        (from oscore_association_t)
213
     *  partial_iv (from oscore_association_t)
214
     *  nonce      (from oscore_association_t)
215
     */
216
0
    oscore_ctx_t *osc_ctx;
217
0
    const uint8_t *info_buf = oscore_info->s;
218
0
    size_t info_buf_len = oscore_info->length;
219
0
    size_t ret = 0;
220
0
    coap_bin_const_t oscore_key_id;
221
0
    coap_bin_const_t partial_iv;
222
0
    coap_bin_const_t aad;
223
0
    coap_bin_const_t id_context;
224
0
    coap_bin_const_t nonce;
225
0
    int have_aad = 0;
226
0
    int have_partial_iv = 0;
227
0
    int have_id_context = 0;
228
0
    int have_nonce = 0;
229
230
0
    ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
231
0
    if (ret != CBOR_ARRAY)
232
0
      goto oscore_fail;
233
0
    if (oscore_cbor_get_element_size(&info_buf, &info_buf_len) != 5)
234
0
      goto oscore_fail;
235
236
    /* recipient_id */
237
0
    ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
238
0
    if (ret != CBOR_BYTE_STRING)
239
0
      goto oscore_fail;
240
0
    oscore_key_id.length = oscore_cbor_get_element_size(&info_buf,
241
0
                                                        &info_buf_len);
242
0
    if (oscore_key_id.length > info_buf_len)
243
0
      goto oscore_fail;
244
0
    oscore_key_id.s = info_buf;
245
0
    info_buf += oscore_key_id.length;
246
0
    info_buf_len -= oscore_key_id.length;
247
248
    /* id_context */
249
0
    ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
250
0
    if (ret == CBOR_BYTE_STRING) {
251
0
      id_context.length = oscore_cbor_get_element_size(&info_buf,
252
0
                                                       &info_buf_len);
253
0
      if (id_context.length > info_buf_len)
254
0
        goto oscore_fail;
255
0
      id_context.s = info_buf;
256
0
      info_buf += id_context.length;
257
0
      info_buf_len -= id_context.length;
258
0
      have_id_context = 1;
259
0
    } else if (ret == CBOR_SIMPLE_VALUE &&
260
0
               oscore_cbor_get_element_size(&info_buf,
261
0
                                            &info_buf_len) == CBOR_NULL) {
262
0
    } else
263
0
      goto oscore_fail;
264
265
    /* aad */
266
0
    ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
267
0
    if (ret == CBOR_BYTE_STRING) {
268
0
      aad.length = oscore_cbor_get_element_size(&info_buf, &info_buf_len);
269
0
      if (aad.length > info_buf_len)
270
0
        goto oscore_fail;
271
0
      aad.s = info_buf;
272
0
      info_buf += aad.length;
273
0
      info_buf_len -= aad.length;
274
0
      have_aad = 1;
275
0
    } else if (ret == CBOR_SIMPLE_VALUE &&
276
0
               oscore_cbor_get_element_size(&info_buf,
277
0
                                            &info_buf_len) == CBOR_NULL) {
278
0
    } else
279
0
      goto oscore_fail;
280
281
    /* partial_iv */
282
0
    ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
283
0
    if (ret == CBOR_BYTE_STRING) {
284
0
      partial_iv.length = oscore_cbor_get_element_size(&info_buf,
285
0
                                                       &info_buf_len);
286
0
      if (partial_iv.length > info_buf_len)
287
0
        goto oscore_fail;
288
0
      partial_iv.s = info_buf;
289
0
      info_buf += partial_iv.length;
290
0
      info_buf_len -= partial_iv.length;
291
0
      have_partial_iv = 1;
292
0
    } else if (ret == CBOR_SIMPLE_VALUE &&
293
0
               oscore_cbor_get_element_size(&info_buf,
294
0
                                            &info_buf_len) == CBOR_NULL) {
295
0
    } else
296
0
      goto oscore_fail;
297
298
    /* nonce */
299
0
    ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
300
0
    if (ret == CBOR_BYTE_STRING) {
301
0
      nonce.length = oscore_cbor_get_element_size(&info_buf,
302
0
                                                  &info_buf_len);
303
0
      if (nonce.length > info_buf_len)
304
0
        goto oscore_fail;
305
0
      nonce.s = info_buf;
306
0
      info_buf += nonce.length;
307
0
      info_buf_len -= nonce.length;
308
0
      have_nonce = 1;
309
0
    } else if (ret == CBOR_SIMPLE_VALUE &&
310
0
               oscore_cbor_get_element_size(&info_buf,
311
0
                                            &info_buf_len) == CBOR_NULL) {
312
0
    } else
313
0
      goto oscore_fail;
314
315
0
    osc_ctx = oscore_find_context(session, oscore_key_id,
316
0
                                  have_id_context ? &id_context : NULL, NULL,
317
0
                                  &session->recipient_ctx);
318
0
    if (osc_ctx) {
319
0
      session->oscore_encryption = 1;
320
0
      oscore_new_association(session, pdu, &pdu->actual_token,
321
0
                             session->recipient_ctx,
322
0
                             have_aad ? &aad : NULL,
323
0
                             have_nonce ? &nonce : NULL,
324
0
                             have_partial_iv ? &partial_iv : NULL,
325
0
                             1);
326
0
      coap_log_debug("persist: OSCORE association added\n");
327
0
      oscore_log_hex_value(COAP_LOG_OSCORE, "partial_iv",
328
0
                           have_partial_iv ? &partial_iv : NULL);
329
0
    }
330
0
  }
331
0
oscore_fail:
332
#else /* ! COAP_OSCORE_SUPPORT */
333
  (void)oscore_info;
334
#endif /* ! COAP_OSCORE_SUPPORT */
335
0
  coap_delete_pdu_lkd(pdu);
336
0
  return s;
337
338
0
malformed:
339
0
  coap_log_warn("coap_persist_observe_add: discard malformed PDU\n");
340
0
fail:
341
0
  coap_delete_string(uri_path);
342
0
  coap_delete_pdu_lkd(pdu);
343
0
  return NULL;
344
0
}
345
346
#if COAP_WITH_OBSERVE_PERSIST
347
#include <stdio.h>
348
#ifdef _WIN32
349
#include <windows.h>
350
#endif
351
352
/*
353
 * read in active observe entry.
354
 */
355
static int
356
coap_op_observe_read(FILE *fp, coap_subscription_t **observe_key,
357
                     coap_proto_t *e_proto, coap_address_t *e_listen_addr,
358
                     coap_addr_tuple_t *s_addr_info,
359
0
                     coap_bin_const_t **raw_packet, coap_bin_const_t **oscore_info) {
360
0
  ssize_t size;
361
0
  coap_binary_t *scratch = NULL;
362
363
0
  assert(fp && observe_key && e_proto && e_listen_addr && s_addr_info &&
364
0
         raw_packet && oscore_info);
365
366
0
  *raw_packet = NULL;
367
0
  *oscore_info = NULL;
368
369
0
  if (fread(observe_key, sizeof(*observe_key), 1, fp) == 1) {
370
    /* New record 'key proto listen addr_info len raw_packet len oscore' */
371
0
    if (fread(e_proto, sizeof(*e_proto), 1, fp) != 1)
372
0
      goto fail;
373
0
    if (fread(e_listen_addr, sizeof(*e_listen_addr), 1, fp) != 1)
374
0
      goto fail;
375
0
    if (fread(s_addr_info, sizeof(*s_addr_info), 1, fp) != 1)
376
0
      goto fail;
377
0
    if (fread(&size, sizeof(size), 1, fp) != 1)
378
0
      goto fail;
379
0
    if (size < 0 || size > 0x10000)
380
0
      goto fail;
381
0
    scratch = coap_new_binary(size);
382
0
    if ((scratch) == NULL)
383
0
      goto fail;
384
0
    if (fread(scratch->s, scratch->length, 1, fp) != 1)
385
0
      goto fail;
386
0
    *raw_packet = (coap_bin_const_t *)scratch;
387
0
    scratch = NULL;
388
0
    if (fread(&size, sizeof(size), 1, fp) != 1)
389
0
      goto fail;
390
    /* If size == -1, then no oscore information */
391
0
    if (size == -1)
392
0
      return 1;
393
0
    else if (size < 0 || size > 0x10000)
394
0
      goto fail;
395
0
    else {
396
0
      scratch = coap_new_binary(size);
397
0
      if (scratch == NULL)
398
0
        goto fail;
399
0
      if (fread(scratch->s, scratch->length, 1, fp) != 1)
400
0
        goto fail;
401
0
      *oscore_info = (coap_bin_const_t *)scratch;
402
0
    }
403
0
    return 1;
404
0
  }
405
0
fail:
406
0
  coap_delete_bin_const(*raw_packet);
407
0
  coap_delete_binary(scratch);
408
409
0
  *observe_key = NULL;
410
0
  memset(e_proto, 0, sizeof(*e_proto));
411
0
  memset(e_listen_addr, 0, sizeof(*e_listen_addr));
412
0
  memset(s_addr_info, 0, sizeof(*s_addr_info));
413
0
  *raw_packet = NULL;
414
0
  return 0;
415
0
}
416
417
/*
418
 * write out active observe entry.
419
 */
420
static int
421
coap_op_observe_write(FILE *fp, coap_subscription_t *observe_key,
422
                      coap_proto_t e_proto, coap_address_t e_listen_addr,
423
                      coap_addr_tuple_t s_addr_info,
424
0
                      coap_bin_const_t *raw_packet, coap_bin_const_t *oscore_info) {
425
0
  if (fwrite(&observe_key, sizeof(observe_key), 1, fp) != 1)
426
0
    goto fail;
427
0
  if (fwrite(&e_proto, sizeof(e_proto), 1, fp) != 1)
428
0
    goto fail;
429
0
  if (fwrite(&e_listen_addr, sizeof(e_listen_addr),
430
0
             1, fp) != 1)
431
0
    goto fail;
432
0
  if (fwrite(&s_addr_info, sizeof(s_addr_info), 1, fp) != 1)
433
0
    goto fail;
434
0
  if (fwrite(&raw_packet->length, sizeof(raw_packet->length), 1, fp) != 1)
435
0
    goto fail;
436
0
  if (fwrite(raw_packet->s, raw_packet->length, 1, fp) != 1)
437
0
    goto fail;
438
0
  if (oscore_info) {
439
0
    if (fwrite(&oscore_info->length, sizeof(oscore_info->length), 1, fp) != 1)
440
0
      goto fail;
441
0
    if (fwrite(oscore_info->s, oscore_info->length, 1, fp) != 1)
442
0
      goto fail;
443
0
  } else {
444
0
    ssize_t not_defined = -1;
445
446
0
    if (fwrite(&not_defined, sizeof(not_defined), 1, fp) != 1)
447
0
      goto fail;
448
0
  }
449
0
  return 1;
450
0
fail:
451
0
  return 0;
452
0
}
453
454
/*
455
 * This should be called before coap_persist_track_funcs() to prevent
456
 * coap_op_observe_added() getting unnecessarily called.
457
 * It should be called after init_resources() and coap_op_resource_load_disk()
458
 * so that all the resources are in place.
459
 */
460
static void
461
0
coap_op_observe_load_disk(coap_context_t *ctx) {
462
0
  FILE *fp_orig = fopen((const char *)ctx->observe_save_file->s, "r");
463
0
  FILE *fp_new = NULL;
464
0
  coap_subscription_t *observe_key = NULL;
465
0
  coap_proto_t e_proto;
466
0
  coap_address_t e_listen_addr;
467
0
  coap_addr_tuple_t s_addr_info;
468
0
  coap_bin_const_t *raw_packet = NULL;
469
0
  coap_bin_const_t *oscore_info = NULL;
470
0
  char *new = NULL;
471
472
0
  if (fp_orig == NULL)
473
0
    goto fail;
474
475
0
  new = coap_malloc_type(COAP_STRING, ctx->observe_save_file->length + 5);
476
0
  if (!new)
477
0
    goto fail;
478
479
0
  strcpy(new, (const char *)ctx->observe_save_file->s);
480
0
  strcat(new, ".tmp");
481
0
  fp_new = fopen(new, "w+");
482
0
  if (fp_new == NULL)
483
0
    goto fail;
484
485
  /* Go through and load oscore entry, updating key on the way */
486
0
  while (1) {
487
0
    if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
488
0
                              &s_addr_info, &raw_packet, &oscore_info))
489
0
      break;
490
0
    coap_log_debug("persist: New session/observe being created\n");
491
0
    observe_key = coap_persist_observe_add_lkd(ctx, e_proto,
492
0
                                               &e_listen_addr,
493
0
                                               &s_addr_info,
494
0
                                               raw_packet,
495
0
                                               oscore_info);
496
0
    if (observe_key) {
497
0
      if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
498
0
                                 s_addr_info, raw_packet, oscore_info))
499
0
        goto fail;
500
0
    }
501
0
    coap_delete_bin_const(raw_packet);
502
0
    raw_packet = NULL;
503
0
    coap_delete_bin_const(oscore_info);
504
0
    oscore_info = NULL;
505
0
  }
506
0
  coap_delete_bin_const(raw_packet);
507
0
  raw_packet = NULL;
508
0
  coap_delete_bin_const(oscore_info);
509
0
  oscore_info = NULL;
510
511
0
  if (fflush(fp_new) == EOF)
512
0
    goto fail;
513
0
  fclose(fp_new);
514
0
  fclose(fp_orig);
515
  /* Either old or new is in place */
516
0
  if (rename(new, (const char *)ctx->observe_save_file->s) == -1) {
517
0
    coap_log_warn("rename %s -> %s: failed: %s (%d)\n",
518
0
                  new, (const char *)ctx->observe_save_file->s,
519
0
                  coap_socket_strerror(), errno);
520
0
  }
521
0
  coap_free_type(COAP_STRING, new);
522
0
  return;
523
524
0
fail:
525
0
  coap_delete_bin_const(raw_packet);
526
0
  coap_delete_bin_const(oscore_info);
527
0
  if (fp_new)
528
0
    fclose(fp_new);
529
0
  if (fp_orig)
530
0
    fclose(fp_orig);
531
0
  if (new) {
532
0
    (void)remove(new);
533
0
  }
534
0
  coap_free_type(COAP_STRING, new);
535
0
  return;
536
0
}
537
538
/*
539
 * client has registered a new observe subscription request.
540
 */
541
static int
542
coap_op_observe_added(coap_session_t *session,
543
                      coap_subscription_t *a_observe_key,
544
                      coap_proto_t a_e_proto, coap_address_t *a_e_listen_addr,
545
                      coap_addr_tuple_t *a_s_addr_info,
546
                      coap_bin_const_t *a_raw_packet,
547
0
                      coap_bin_const_t *a_oscore_info, void *user_data) {
548
0
  FILE *fp_orig = fopen((const char *)session->context->observe_save_file->s,
549
0
                        "r");
550
0
  FILE *fp_new = NULL;
551
0
  coap_subscription_t *observe_key = NULL;
552
0
  coap_proto_t e_proto;
553
0
  coap_address_t e_listen_addr;
554
0
  coap_addr_tuple_t s_addr_info;
555
0
  coap_bin_const_t *raw_packet = NULL;
556
0
  coap_bin_const_t *oscore_info = NULL;
557
0
  char *new = NULL;
558
559
0
  (void)user_data;
560
561
0
  new = coap_malloc_type(COAP_STRING,
562
0
                         session->context->observe_save_file->length + 5);
563
0
  if (!new)
564
0
    goto fail;
565
566
0
  strcpy(new, (const char *)session->context->observe_save_file->s);
567
0
  strcat(new, ".tmp");
568
0
  fp_new = fopen(new, "w+");
569
0
  if (fp_new == NULL)
570
0
    goto fail;
571
572
  /* Go through and delete observe entry if it exists */
573
0
  while (fp_orig) {
574
0
    if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
575
0
                              &s_addr_info, &raw_packet, &oscore_info))
576
0
      break;
577
0
    if (observe_key != a_observe_key) {
578
0
      if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
579
0
                                 s_addr_info, raw_packet, oscore_info))
580
0
        goto fail;
581
0
    }
582
0
    coap_delete_bin_const(raw_packet);
583
0
    raw_packet = NULL;
584
0
    coap_delete_bin_const(oscore_info);
585
0
    oscore_info = NULL;
586
0
  }
587
0
  coap_delete_bin_const(raw_packet);
588
0
  raw_packet = NULL;
589
0
  coap_delete_bin_const(oscore_info);
590
0
  oscore_info = NULL;
591
592
  /* Add in new entry to the end */
593
0
  if (!coap_op_observe_write(fp_new, a_observe_key, a_e_proto, *a_e_listen_addr,
594
0
                             *a_s_addr_info, a_raw_packet, a_oscore_info))
595
0
    goto fail;
596
597
0
  if (fflush(fp_new) == EOF)
598
0
    goto fail;
599
0
  fclose(fp_new);
600
0
  if (fp_orig)
601
0
    fclose(fp_orig);
602
  /* Either old or new is in place */
603
0
  (void)rename(new, (const char *)session->context->observe_save_file->s);
604
0
  coap_free_type(COAP_STRING, new);
605
0
  return 1;
606
607
0
fail:
608
0
  coap_delete_bin_const(raw_packet);
609
0
  coap_delete_bin_const(oscore_info);
610
0
  if (fp_new)
611
0
    fclose(fp_new);
612
0
  if (fp_orig)
613
0
    fclose(fp_orig);
614
0
  if (new) {
615
0
    (void)remove(new);
616
0
  }
617
0
  coap_free_type(COAP_STRING, new);
618
0
  return 0;
619
0
}
620
621
/*
622
 * client has de-registered a observe subscription request.
623
 */
624
static int
625
coap_op_observe_deleted(coap_session_t *session,
626
                        coap_subscription_t *d_observe_key,
627
0
                        void *user_data) {
628
0
  FILE *fp_orig = fopen((const char *)session->context->observe_save_file->s,
629
0
                        "r");
630
0
  FILE *fp_new = NULL;
631
0
  coap_subscription_t *observe_key = NULL;
632
0
  coap_proto_t e_proto;
633
0
  coap_address_t e_listen_addr;
634
0
  coap_addr_tuple_t s_addr_info;
635
0
  coap_bin_const_t *raw_packet = NULL;
636
0
  coap_bin_const_t *oscore_info = NULL;
637
0
  char *new = NULL;
638
639
0
  (void)user_data;
640
641
0
  if (fp_orig == NULL)
642
0
    goto fail;
643
0
  new = coap_malloc_type(COAP_STRING,
644
0
                         session->context->observe_save_file->length + 5);
645
0
  if (!new)
646
0
    goto fail;
647
648
0
  strcpy(new, (const char *)session->context->observe_save_file->s);
649
0
  strcat(new, ".tmp");
650
0
  fp_new = fopen(new, "w+");
651
0
  if (fp_new == NULL)
652
0
    goto fail;
653
654
  /* Go through and locate observe entry to delete and not copy it across */
655
0
  while (1) {
656
0
    if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
657
0
                              &s_addr_info, &raw_packet, &oscore_info))
658
0
      break;
659
0
    if (observe_key != d_observe_key) {
660
0
      if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
661
0
                                 s_addr_info, (coap_bin_const_t *)raw_packet,
662
0
                                 (coap_bin_const_t *)oscore_info))
663
0
        goto fail;
664
0
    }
665
0
    coap_delete_bin_const(raw_packet);
666
0
    raw_packet = NULL;
667
0
    coap_delete_bin_const(oscore_info);
668
0
    oscore_info = NULL;
669
0
  }
670
0
  coap_delete_bin_const(raw_packet);
671
0
  raw_packet = NULL;
672
0
  coap_delete_bin_const(oscore_info);
673
0
  oscore_info = NULL;
674
675
0
  if (fflush(fp_new) == EOF)
676
0
    goto fail;
677
0
  fclose(fp_new);
678
0
  fclose(fp_orig);
679
  /* Either old or new is in place */
680
0
  (void)rename(new, (const char *)session->context->observe_save_file->s);
681
0
  coap_free_type(COAP_STRING, new);
682
0
  return 1;
683
684
0
fail:
685
0
  coap_delete_bin_const(raw_packet);
686
0
  coap_delete_bin_const(oscore_info);
687
0
  if (fp_new)
688
0
    fclose(fp_new);
689
0
  if (fp_orig)
690
0
    fclose(fp_orig);
691
0
  if (new) {
692
0
    (void)remove(new);
693
0
  }
694
0
  coap_free_type(COAP_STRING, new);
695
0
  return 0;
696
0
}
697
698
/*
699
 * This should be called before coap_persist_track_funcs() to prevent
700
 * coap_op_obs_cnt_track_observe() getting unnecessarily called.
701
 * Should be called after coap_op_dyn_resource_load_disk() to make sure that
702
 * all the resources are in the right place.
703
 */
704
static void
705
0
coap_op_obs_cnt_load_disk(coap_context_t *context) {
706
0
  FILE *fp = fopen((const char *)context->obs_cnt_save_file->s, "r");
707
0
  char buf[1500];
708
709
0
  if (fp == NULL)
710
0
    return;
711
712
0
  while (fgets(buf, sizeof(buf), fp) != NULL) {
713
0
    char *cp = strchr(buf, ' ');
714
0
    coap_str_const_t resource_key;
715
0
    uint32_t observe_num;
716
0
    coap_resource_t *r;
717
718
0
    if (!cp)
719
0
      break;
720
721
0
    *cp = '\000';
722
0
    cp++;
723
0
    observe_num = atoi(cp);
724
    /*
725
     * Need to assume 0 .. (context->observe_save_freq-1) have in addition
726
     * been sent so need to round up to latest possible send value
727
     */
728
0
    observe_num = ((observe_num + context->observe_save_freq) /
729
0
                   context->observe_save_freq) *
730
0
                  context->observe_save_freq - 1;
731
0
    resource_key.s = (uint8_t *)buf;
732
0
    resource_key.length = strlen(buf);
733
0
    r = coap_get_resource_from_uri_path_lkd(context, &resource_key);
734
0
    if (r) {
735
0
      coap_log_debug("persist: Initial observe number being updated '%s' %u\n",
736
0
                     buf, observe_num);
737
0
      coap_persist_set_observe_num(r, observe_num);
738
0
    }
739
0
  }
740
0
  fclose(fp);
741
0
}
742
743
/* Close both streams before replacing the counter file. */
744
static int
745
0
coap_op_obs_cnt_commit(FILE *src, FILE *dst, char *tmp, const char *path) {
746
0
  int ok = !src || !ferror(src);
747
748
0
  if (fclose(dst) != 0)
749
0
    ok = 0;
750
0
  if (src)
751
0
    fclose(src);
752
0
  if (ok) {
753
#ifdef _WIN32
754
    ok = MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING) != 0;
755
#else
756
0
    ok = rename(tmp, path) == 0;
757
0
#endif
758
0
  }
759
0
  if (!ok) {
760
0
    coap_log_warn("persist: could not update Observe counter file '%s'\n", path);
761
0
    remove(tmp);
762
0
  }
763
0
  coap_free_type(COAP_STRING, tmp);
764
0
  return ok;
765
0
}
766
767
/*
768
 * Called when the observe value of a resource has been changed, but limited
769
 * to be called every context->context->observe_save_freq to reduce update
770
 * overheads.
771
 */
772
static int
773
coap_op_obs_cnt_track_observe(coap_context_t *context,
774
                              coap_str_const_t *resource_name,
775
                              uint32_t n_observe_num,
776
0
                              void *user_data) {
777
0
  FILE *fp_orig = fopen((const char *)context->obs_cnt_save_file->s, "r");
778
0
  FILE *fp_new = NULL;
779
0
  char buf[1500];
780
0
  char *new = NULL;
781
782
0
  (void)user_data;
783
784
0
  if (!fp_orig && errno != ENOENT)
785
0
    return 0;
786
0
  new = coap_malloc_type(COAP_STRING, context->obs_cnt_save_file->length + 5);
787
0
  if (!new)
788
0
    goto fail;
789
790
0
  strcpy(new, (const char *)context->obs_cnt_save_file->s);
791
0
  strcat(new, ".tmp");
792
0
  fp_new = fopen(new, "w+");
793
0
  if (fp_new == NULL)
794
0
    goto fail;
795
796
  /* Go through and locate resource entry to update */
797
0
  while (fp_orig && fgets(buf, sizeof(buf), fp_orig) != NULL) {
798
0
    char *cp = strchr(buf, ' ');
799
0
    uint32_t observe_num;
800
0
    coap_bin_const_t resource_key;
801
802
0
    if (!cp)
803
0
      break;
804
805
0
    *cp = '\000';
806
0
    cp++;
807
0
    observe_num = atoi(cp);
808
0
    resource_key.s = (uint8_t *)buf;
809
0
    resource_key.length = strlen(buf);
810
0
    if (!coap_binary_equal(resource_name, &resource_key)) {
811
0
      if (fprintf(fp_new, "%s %u\n", resource_key.s, observe_num) < 0)
812
0
        goto fail;
813
0
    }
814
0
  }
815
0
  if (fprintf(fp_new, "%s %u\n", resource_name->s, n_observe_num) < 0)
816
0
    goto fail;
817
0
  return coap_op_obs_cnt_commit(fp_orig, fp_new, new,
818
0
                                (const char *)context->obs_cnt_save_file->s);
819
820
0
fail:
821
0
  if (fp_new) {
822
0
    fclose(fp_new);
823
0
    (void)remove(new);
824
0
  }
825
0
  if (fp_orig)
826
0
    fclose(fp_orig);
827
0
  coap_free_type(COAP_STRING, new);
828
0
  return 0;
829
0
}
830
831
/*
832
 * Called when a resource has been deleted.
833
 */
834
static int
835
coap_op_obs_cnt_deleted(coap_context_t *context,
836
0
                        coap_str_const_t *resource_name) {
837
0
  FILE *fp_orig = fopen((const char *)context->obs_cnt_save_file->s, "r");
838
0
  FILE *fp_new = NULL;
839
0
  char buf[1500];
840
0
  char *new = NULL;
841
842
0
  if (fp_orig == NULL)
843
0
    return errno == ENOENT;
844
0
  new = coap_malloc_type(COAP_STRING, context->obs_cnt_save_file->length + 5);
845
0
  if (!new)
846
0
    goto fail;
847
848
0
  strcpy(new, (const char *)context->obs_cnt_save_file->s);
849
0
  strcat(new, ".tmp");
850
0
  fp_new = fopen(new, "w+");
851
0
  if (fp_new == NULL)
852
0
    goto fail;
853
854
  /* Go through and locate resource entry to delete */
855
0
  while (fgets(buf, sizeof(buf), fp_orig) != NULL) {
856
0
    char *cp = strchr(buf, ' ');
857
0
    uint32_t observe_num;
858
0
    coap_bin_const_t resource_key;
859
860
0
    if (!cp)
861
0
      break;
862
863
0
    *cp = '\000';
864
0
    cp++;
865
0
    observe_num = atoi(cp);
866
0
    resource_key.s = (uint8_t *)buf;
867
0
    resource_key.length = strlen(buf);
868
0
    if (!coap_binary_equal(resource_name, &resource_key)) {
869
0
      if (fprintf(fp_new, "%s %u\n", resource_key.s, observe_num) < 0)
870
0
        goto fail;
871
0
    }
872
0
  }
873
0
  return coap_op_obs_cnt_commit(fp_orig, fp_new, new,
874
0
                                (const char *)context->obs_cnt_save_file->s);
875
876
0
fail:
877
0
  if (fp_new) {
878
0
    fclose(fp_new);
879
0
    (void)remove(new);
880
0
  }
881
0
  if (fp_orig)
882
0
    fclose(fp_orig);
883
0
  coap_free_type(COAP_STRING, new);
884
0
  return 0;
885
0
}
886
887
0
#define COAP_DYN_RESOURCE_MAX_FIELD_SIZE 0x10000
888
889
/*
890
 * read in dynamic resource entry, allocating name & raw_packet
891
 * which need to be freed off by caller. Return 1 for a record, 0 at EOF,
892
 * or -1 on error (including an incomplete record).
893
 */
894
static int
895
coap_op_dyn_resource_read(FILE *fp, coap_proto_t *e_proto,
896
                          coap_string_t **name,
897
0
                          coap_binary_t **raw_packet) {
898
0
  ssize_t size;
899
0
  size_t nread;
900
901
0
  *name = NULL;
902
0
  *raw_packet = NULL;
903
904
0
  nread = fread(e_proto, 1, sizeof(*e_proto), fp);
905
0
  if (nread == 0 && feof(fp) && !ferror(fp))
906
0
    return 0;
907
0
  if (nread == sizeof(*e_proto)) {
908
    /* New record 'proto len resource_name len raw_packet' */
909
0
    if (fread(&size, sizeof(size), 1, fp) != 1)
910
0
      goto fail;
911
0
    if (size < 0 || size > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
912
0
      goto fail;
913
0
    *name = coap_new_string(size);
914
0
    if (!(*name))
915
0
      goto fail;
916
0
    if (fread((*name)->s, 1, size, fp) != (size_t)size)
917
0
      goto fail;
918
0
    if (fread(&size, sizeof(size), 1, fp) != 1)
919
0
      goto fail;
920
0
    if (size <= 0 || size > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
921
0
      goto fail;
922
0
    *raw_packet = coap_new_binary(size);
923
0
    if (!(*raw_packet))
924
0
      goto fail;
925
0
    if (fread((*raw_packet)->s, size, 1, fp) != 1)
926
0
      goto fail;
927
0
    return 1;
928
0
  }
929
0
fail:
930
0
  return -1;
931
0
}
932
933
/*
934
 * write out dynamic resource entry.
935
 */
936
static int
937
coap_op_dyn_resource_write(FILE *fp, coap_proto_t e_proto,
938
                           coap_str_const_t *name,
939
0
                           coap_bin_const_t *raw_packet) {
940
0
  if (name->length > COAP_DYN_RESOURCE_MAX_FIELD_SIZE || !raw_packet->length ||
941
0
      raw_packet->length > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
942
0
    return 0;
943
0
  if (fwrite(&e_proto, sizeof(e_proto), 1, fp) != 1)
944
0
    goto fail;
945
0
  if (fwrite(&name->length, sizeof(name->length), 1, fp) != 1)
946
0
    goto fail;
947
0
  if (name->length && fwrite(name->s, 1, name->length, fp) != name->length)
948
0
    goto fail;
949
0
  if (fwrite(&raw_packet->length, sizeof(raw_packet->length), 1, fp) != 1)
950
0
    goto fail;
951
0
  if (fwrite(raw_packet->s, raw_packet->length, 1, fp) != 1)
952
0
    goto fail;
953
0
  return 1;
954
0
fail:
955
0
  return 0;
956
0
}
957
958
/*
959
 * This should be called before coap_persist_track_funcs() to prevent
960
 * coap_op_dyn_resource_added() getting unnecessarily called.
961
 *
962
 * Each record 'proto len resource_name len raw_packet'
963
 */
964
static void
965
0
coap_op_dyn_resource_load_disk(coap_context_t *ctx) {
966
0
  FILE *fp_orig = NULL;
967
0
  coap_proto_t e_proto;
968
0
  coap_string_t *name = NULL;
969
0
  coap_binary_t *raw_packet = NULL;
970
0
  coap_resource_t *r;
971
0
  coap_session_t *session = NULL;
972
0
  coap_pdu_t *request = NULL;
973
0
  coap_pdu_t *response = NULL;
974
0
  coap_string_t *query = NULL;
975
976
0
  if (!ctx->unknown_resource && !ctx->dyn_create_handler)
977
0
    return;
978
979
0
  fp_orig = fopen((const char *)ctx->dyn_resource_save_file->s, "rb");
980
0
  if (fp_orig == NULL)
981
0
    return;
982
0
  session = (coap_session_t *)coap_malloc_type(COAP_SESSION,
983
0
                                               sizeof(coap_session_t));
984
0
  if (!session)
985
0
    goto fail;
986
0
  memset(session, 0, sizeof(coap_session_t));
987
0
  session->context = ctx;
988
989
  /* Go through and create each dynamic resource if it does not exist*/
990
0
  while (1) {
991
0
    if (coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet) != 1)
992
0
      break;
993
0
    r = coap_get_resource_from_uri_path_lkd(ctx, (coap_str_const_t *)name);
994
0
    if (!r) {
995
      /* Create the new resource using the application logic */
996
997
0
      coap_log_debug("persist: dynamic resource '%s' being re-created\n", name->s);
998
      /*
999
       * Need max space in case PDU is updated with updated token,
1000
       * huge size etc.
1001
       * */
1002
0
      request = coap_pdu_init(0, 0, 0, 0);
1003
0
      if (!request)
1004
0
        goto fail;
1005
1006
0
      session->proto = e_proto;
1007
0
      if (!coap_pdu_parse(session->proto, raw_packet->s,
1008
0
                          raw_packet->length, request)) {
1009
0
        goto fail;
1010
0
      }
1011
0
      if (request->code == 0 || request->code > 7) {
1012
        /* Need to protect r->handler[request->code-1] */
1013
0
        goto next;
1014
0
      }
1015
0
      r = coap_add_dynamic_resource(session, request);
1016
0
      if (!r) {
1017
0
        r = ctx->unknown_resource;
1018
0
      }
1019
0
      if (!r || !r->handler[request->code-1])
1020
0
        goto next;
1021
1022
0
      response = coap_pdu_init(0, 0, 0, 0);
1023
0
      if (!response)
1024
0
        goto fail;
1025
0
      query = coap_get_query(request);
1026
      /* Call the application handler to set up this dynamic resource */
1027
0
      if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) {
1028
0
        coap_lock_callback_release(r->handler[request->code-1](r,
1029
0
                                                               session, request,
1030
0
                                                               query, response),
1031
                                   /* context is being freed off */
1032
0
                                   goto fail);
1033
0
      } else {
1034
0
        coap_lock_specific_callback_release(&r->lock,
1035
0
                                            r->handler[request->code-1](r,
1036
0
                                                session, request,
1037
0
                                                query, response),
1038
                                            /* context is being freed off */
1039
0
                                            goto fail);
1040
0
      }
1041
0
      coap_delete_string(query);
1042
0
      query = NULL;
1043
0
      coap_delete_pdu_lkd(response);
1044
0
      response = NULL;
1045
0
next:
1046
0
      coap_delete_pdu_lkd(request);
1047
0
      request = NULL;
1048
0
    }
1049
0
    coap_delete_string(name);
1050
0
    coap_delete_binary(raw_packet);
1051
0
  }
1052
0
fail:
1053
0
  coap_delete_string(name);
1054
0
  coap_delete_binary(raw_packet);
1055
0
  coap_delete_string(query);
1056
0
  coap_delete_pdu_lkd(request);
1057
0
  coap_delete_pdu_lkd(response);
1058
0
  fclose(fp_orig);
1059
0
  coap_free_type(COAP_SESSION, session);
1060
0
}
1061
1062
/* Copy the other resource records, leaving the original file intact on error. */
1063
static int
1064
0
coap_op_dyn_resource_copy(FILE *src, FILE *dst, coap_str_const_t *skip) {
1065
0
  for (;;) {
1066
0
    coap_proto_t proto;
1067
0
    coap_string_t *name;
1068
0
    coap_binary_t *packet;
1069
0
    int status = coap_op_dyn_resource_read(src, &proto, &name, &packet);
1070
1071
0
    if (status == 1 && !coap_string_equal(name, skip) &&
1072
0
        !coap_op_dyn_resource_write(dst, proto, (coap_str_const_t *)name,
1073
0
                                    (coap_bin_const_t *)packet))
1074
0
      status = -1;
1075
0
    coap_delete_string(name);
1076
0
    coap_delete_binary(packet);
1077
0
    if (status != 1)
1078
0
      return status == 0;
1079
0
  }
1080
0
}
1081
1082
static int
1083
coap_op_dyn_resource_update(const coap_bin_const_t *file, coap_str_const_t *name,
1084
0
                            coap_proto_t proto, coap_bin_const_t *packet) {
1085
0
  const char *path = (const char *)file->s;
1086
0
  FILE *src = fopen(path, "rb");
1087
0
  FILE *dst;
1088
0
  char *tmp = NULL;
1089
0
  int ok = 0;
1090
1091
0
  if (!src) {
1092
0
    if (errno != ENOENT)
1093
0
      goto finish;
1094
0
    if (!packet)
1095
0
      return 1;
1096
0
  }
1097
1098
0
  tmp = coap_malloc_type(COAP_STRING, file->length + sizeof(".tmp"));
1099
0
  if (!tmp)
1100
0
    goto finish;
1101
0
  memcpy(tmp, path, file->length);
1102
0
  memcpy(tmp + file->length, ".tmp", sizeof(".tmp"));
1103
0
  dst = fopen(tmp, "wb");
1104
0
  if (!dst)
1105
0
    goto finish;
1106
1107
0
  ok = (!src || coap_op_dyn_resource_copy(src, dst, name)) &&
1108
0
       (!packet || coap_op_dyn_resource_write(dst, proto, name, packet));
1109
0
  if (fclose(dst) != 0)
1110
0
    ok = 0;
1111
0
  if (src) {
1112
0
    fclose(src);
1113
0
    src = NULL;
1114
0
  }
1115
0
  if (ok) {
1116
#ifdef _WIN32
1117
    ok = MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING) != 0;
1118
#else
1119
0
    ok = rename(tmp, path) == 0;
1120
0
#endif
1121
0
  }
1122
0
  if (!ok)
1123
0
    remove(tmp);
1124
1125
0
finish:
1126
0
  if (src)
1127
0
    fclose(src);
1128
0
  coap_free_type(COAP_STRING, tmp);
1129
0
  if (!ok)
1130
0
    coap_log_warn("persist: could not update dynamic resource file '%s'\n", path);
1131
0
  return ok;
1132
0
}
1133
1134
static int
1135
coap_op_dyn_resource_added(coap_session_t *session,
1136
                           coap_str_const_t *resource_name,
1137
                           coap_bin_const_t *packet,
1138
0
                           void *user_data) {
1139
0
  (void)user_data;
1140
0
  return coap_op_dyn_resource_update(session->context->dyn_resource_save_file,
1141
0
                                     resource_name, session->proto, packet);
1142
0
}
1143
1144
static int
1145
coap_op_resource_deleted(coap_context_t *context,
1146
                         coap_str_const_t *resource_name,
1147
0
                         void *user_data) {
1148
0
  int counter_ok = 1;
1149
1150
0
  (void)user_data;
1151
0
  if (context->obs_cnt_save_file)
1152
0
    counter_ok = coap_op_obs_cnt_deleted(context, resource_name);
1153
0
  if (context->dyn_resource_save_file &&
1154
0
      !coap_op_dyn_resource_update(context->dyn_resource_save_file,
1155
0
                                   resource_name, COAP_PROTO_NONE, NULL))
1156
0
    return 0;
1157
0
  return counter_ok;
1158
0
}
1159
1160
COAP_API int
1161
coap_persist_startup(coap_context_t *context,
1162
                     const char *dyn_resource_save_file,
1163
                     const char *observe_save_file,
1164
                     const char *obs_cnt_save_file,
1165
0
                     uint32_t save_freq) {
1166
0
  int ret;
1167
1168
0
  coap_lock_lock(return 0);
1169
0
  ret = coap_persist_startup_lkd(context,
1170
0
                                 dyn_resource_save_file,
1171
0
                                 observe_save_file,
1172
0
                                 obs_cnt_save_file,
1173
0
                                 save_freq);
1174
0
  coap_lock_unlock();
1175
0
  return ret;
1176
0
}
1177
1178
int
1179
coap_persist_startup_lkd(coap_context_t *context,
1180
                         const char *dyn_resource_save_file,
1181
                         const char *observe_save_file,
1182
                         const char *obs_cnt_save_file,
1183
0
                         uint32_t save_freq) {
1184
0
  coap_lock_check_locked();
1185
0
  if (dyn_resource_save_file) {
1186
0
    context->dyn_resource_save_file =
1187
0
        coap_new_bin_const((const uint8_t *)dyn_resource_save_file,
1188
0
                           strlen(dyn_resource_save_file));
1189
0
    if (!context->dyn_resource_save_file)
1190
0
      return 0;
1191
0
    coap_op_dyn_resource_load_disk(context);
1192
0
    context->dyn_resource_added_cb = coap_op_dyn_resource_added;
1193
0
    context->resource_deleted_cb = coap_op_resource_deleted;
1194
0
  }
1195
0
  if (obs_cnt_save_file) {
1196
0
    context->obs_cnt_save_file =
1197
0
        coap_new_bin_const((const uint8_t *)obs_cnt_save_file,
1198
0
                           strlen(obs_cnt_save_file));
1199
0
    if (!context->obs_cnt_save_file)
1200
0
      return 0;
1201
0
    context->observe_save_freq = save_freq ? save_freq : 1;
1202
0
    coap_op_obs_cnt_load_disk(context);
1203
0
    context->track_observe_value_cb = coap_op_obs_cnt_track_observe;
1204
0
    context->resource_deleted_cb = coap_op_resource_deleted;
1205
0
  }
1206
0
  if (observe_save_file) {
1207
0
    context->observe_save_file =
1208
0
        coap_new_bin_const((const uint8_t *)observe_save_file,
1209
0
                           strlen(observe_save_file));
1210
0
    if (!context->observe_save_file)
1211
0
      return 0;
1212
0
    coap_op_observe_load_disk(context);
1213
0
    context->observe_added_cb = coap_op_observe_added;
1214
0
    context->observe_deleted_cb = coap_op_observe_deleted;
1215
0
  }
1216
0
  return 1;
1217
0
}
1218
1219
void
1220
0
coap_persist_cleanup(coap_context_t *context) {
1221
0
  coap_delete_bin_const(context->dyn_resource_save_file);
1222
0
  coap_delete_bin_const(context->obs_cnt_save_file);
1223
0
  coap_delete_bin_const(context->observe_save_file);
1224
0
  context->dyn_resource_save_file = NULL;
1225
0
  context->obs_cnt_save_file = NULL;
1226
0
  context->observe_save_file = NULL;
1227
1228
  /* Close down any tracking */
1229
0
  coap_persist_track_funcs(context, NULL, NULL, NULL, NULL,
1230
0
                           NULL, 0, NULL);
1231
0
}
1232
1233
COAP_API void
1234
0
coap_persist_stop(coap_context_t *context) {
1235
0
  if (!context)
1236
0
    return;
1237
0
  coap_lock_lock(return);
1238
0
  coap_persist_stop_lkd(context);
1239
0
  coap_lock_unlock();
1240
0
}
1241
1242
void
1243
0
coap_persist_stop_lkd(coap_context_t *context) {
1244
0
  if (context == NULL)
1245
0
    return;
1246
0
  coap_lock_check_locked();
1247
0
  context->observe_no_clear = 1;
1248
0
  coap_persist_cleanup(context);
1249
0
}
1250
#else /* ! COAP_WITH_OBSERVE_PERSIST */
1251
COAP_API int
1252
coap_persist_startup(coap_context_t *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) {
1257
  (void)context;
1258
  (void)dyn_resource_save_file;
1259
  (void)observe_save_file;
1260
  (void)obs_cnt_save_file;
1261
  (void)save_freq;
1262
  return 0;
1263
}
1264
1265
COAP_API void
1266
coap_persist_stop(coap_context_t *context) {
1267
  context->observe_no_clear = 1;
1268
  /* Close down any tracking */
1269
  coap_persist_track_funcs(context, NULL, NULL, NULL, NULL,
1270
                           NULL, 0, NULL);
1271
}
1272
1273
#endif /* ! COAP_WITH_OBSERVE_PERSIST */
1274
1275
#endif /* COAP_SERVER_SUPPORT */