replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / resource.c
index d6ae50f..4909dbe 100644 (file)
@@ -3,22 +3,22 @@
  * Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
  *
  * This file is part of the CoAP library libcoap. Please see
- * README for terms of use. 
+ * README for terms of use.
  */
 
-#include "config.h"
-#include "net.h"
-#include "debug.h"
-#include "resource.h"
-#include "subscribe.h"
+#include "include/coap/config.h"
+#include "include/coap/net.h"
+#include "include/coap/debug.h"
+#include "include/coap/resource.h"
+#include "include/coap/subscribe.h"
 
 #ifdef WITH_LWIP
-#include "utlist.h"
+#include "include/coap/utlist.h"
 /* mem.h is only needed for the string free calls for
  * COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
  * COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
  * do on lwip. */
-#include "mem.h"
+#include "include/coap/mem.h"
 
 #include <lwip/memp.h>
 
@@ -27,9 +27,9 @@
 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
 
 #endif
-#ifdef WITH_POSIX
-#include "utlist.h"
-#include "mem.h"
+#if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
+#include "include/coap/utlist.h"
+#include "include/coap/mem.h"
 
 #define COAP_MALLOC_TYPE(Type) \
   ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
@@ -64,7 +64,9 @@ coap_free_subscription(coap_subscription_t *subscription)
 }
 #endif /* WITH_CONTIKI */
 
+#ifndef min
 #define min(a,b) ((a) < (b) ? (a) : (b))
+#endif
 
 /* Helper functions for conditional output of character sequences into
  * a given buffer. The first Offset characters are skipped.
@@ -74,21 +76,21 @@ coap_free_subscription(coap_subscription_t *subscription)
  * Adds Char to Buf if Offset is zero. Otherwise, Char is not written
  * and Offset is decremented.
  */
-#define PRINT_WITH_OFFSET(Buf,Offset,Char)             \
-  if ((Offset) == 0) {                                 \
-    (*(Buf)++) = (Char);                               \
-  } else {                                             \
-    (Offset)--;                                                \
-  }                                                    \
+#define PRINT_WITH_OFFSET(Buf,Offset,Char)      \
+  if ((Offset) == 0) {                  \
+    (*(Buf)++) = (Char);                \
+  } else {                      \
+    (Offset)--;                     \
+  }                         \
 
 /**
  * Adds Char to Buf if Offset is zero and Buf is less than Bufend.
  */
-#define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) {                \
-    if ((Buf) < (Bufend)) {                                            \
-      PRINT_WITH_OFFSET(Buf,Offset,Char);                              \
-    }                                                                  \
-    (Result)++;                                                                \
+#define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) {     \
+    if ((Buf) < (Bufend)) {                     \
+      PRINT_WITH_OFFSET(Buf,Offset,Char);               \
+    }                                   \
+    (Result)++;                             \
   }
 
 /**
@@ -96,14 +98,14 @@ coap_free_subscription(coap_subscription_t *subscription)
  * characters are skipped. Output may be truncated to Bufend - Buf
  * characters.
  */
-#define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) {   \
-    size_t i;                                                          \
-    for (i = 0; i < (Length); i++) {                                   \
+#define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) {    \
+    size_t i;                               \
+    for (i = 0; i < (Length); i++) {                    \
       PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
-    }                                                                  \
+    }                                   \
   }
 
-int match(const str *text, const str *pattern, int match_prefix, int match_substring)
+static int match(const str *text, const str *pattern, int match_prefix, int match_substring)
 {
     assert(text);
     assert(pattern);
@@ -119,7 +121,7 @@ int match(const str *text, const str *pattern, int match_prefix, int match_subst
         {
             size_t token_length;
             unsigned char *token = next_token;
-            next_token = memchr(token, ' ', remaining_length);
+            next_token = (unsigned char *) memchr(token, ' ', remaining_length);
 
             if (next_token)
             {
@@ -144,22 +146,22 @@ int match(const str *text, const str *pattern, int match_prefix, int match_subst
             && memcmp(text->s, pattern->s, pattern->length) == 0;
 }
 
-/** 
+/**
  * Prints the names of all known resources to @p buf. This function
  * sets @p buflen to the number of bytes actually written and returns
  * @c 1 on succes. On error, the value in @p buflen is undefined and
  * the return value will be @c 0.
- * 
+ *
  * @param context The context with the resource map.
  * @param buf     The buffer to write the result.
  * @param buflen  Must be initialized to the maximum length of @p buf and will be
  *                set to the length of the well-known response on return.
  * @param offset  The offset in bytes where the output shall start and is
  *                shifted accordingly with the characters that have been
- *                processed. This parameter is used to support the block 
- *                option. 
+ *                processed. This parameter is used to support the block
+ *                option.
  * @param query_filter A filter query according to <a href="http://tools.ietf.org/html/draft-ietf-core-link-format-11#section-4.1">Link Format</a>
- * 
+ *
  * @return COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are
  *         set to the number of bytes that have actually been written to
  *         @p buf. COAP_PRINT_STATUS_TRUNC is set when the output has been
@@ -339,7 +341,7 @@ coap_resource_init(const unsigned char *uri, size_t len, int flags)
 {
     coap_resource_t *r;
 
-#ifdef WITH_POSIX
+#if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
     r = (coap_resource_t *)coap_malloc(sizeof(coap_resource_t));
 #endif
 #ifdef WITH_LWIP
@@ -376,12 +378,12 @@ coap_attr_t *
 coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen,
         const unsigned char *val, size_t vlen, int flags)
 {
-    coap_attr_t *attr;
+    coap_attr_t *attr = NULL;
 
     if (!resource || !name)
         return NULL;
 
-#ifdef WITH_POSIX
+#if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
     attr = (coap_attr_t *)coap_malloc(sizeof(coap_attr_t));
 #endif
 #ifdef WITH_LWIP
@@ -431,7 +433,7 @@ coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen
         for (attr = list_head(resource->link_attr); attr;
                 attr = list_item_next(attr))
         {
-#endif /* WITH_CONTIKI */    
+#endif /* WITH_CONTIKI */
         if (attr->name.length == nlen && memcmp(attr->name.s, name, nlen) == 0)
             return attr;
     }
@@ -487,8 +489,8 @@ void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
 
 int coap_delete_resource(coap_context_t *context, coap_key_t key)
 {
-    coap_resource_t *resource;
-    coap_attr_t *attr, *tmp;
+    coap_resource_t *resource = NULL;
+    coap_attr_t *attr = NULL, *tmp = NULL;
 #ifdef WITH_CONTIKI
     coap_subscription_t *obs;
 #endif
@@ -501,7 +503,7 @@ int coap_delete_resource(coap_context_t *context, coap_key_t key)
     if (!resource)
         return 0;
 
-#if defined(WITH_POSIX) || defined(WITH_LWIP)
+#if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO) || defined(_WIN32)
 #ifdef COAP_RESOURCES_NOHASH
     LL_DELETE(context->resources, resource);
 #else
@@ -512,15 +514,15 @@ int coap_delete_resource(coap_context_t *context, coap_key_t key)
     LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
 
     if (resource->flags & COAP_RESOURCE_FLAGS_RELEASE_URI)
-    coap_free(resource->uri.s);
+        coap_free(resource->uri.s);
 
-#ifdef WITH_POSIX
+#if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
     coap_free(resource);
 #endif
 #ifdef WITH_LWIP
     memp_free(MEMP_COAP_RESOURCE, resource);
 #endif
-#else /* not (WITH_POSIX || WITH_LWIP) */
+#else /* not (WITH_POSIX || WITH_LWIP || WITH_ARDUINO) */
     /* delete registered attributes */
     while ((attr = list_pop(resource->link_attr)))
         memb_free(&attribute_storage, attr);
@@ -637,7 +639,8 @@ coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, const
     assert(resource);
     assert(peer);
 
-    for (s = list_head(resource->subscribers); s; s = list_item_next(s))
+    for (s = (coap_subscription_t *) list_head(resource->subscribers);
+            s; s = (coap_subscription_t *) list_item_next((void *) s))
     {
         if (coap_address_equals(&s->subscriber, peer)
                 && (!token
@@ -752,10 +755,12 @@ static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
         assert(h); /* we do not allow subscriptions if no
          * GET handler is defined */
 
-        for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs))
+        for (obs = (coap_subscription_t *) list_head(r->subscribers);
+                obs; obs = (coap_subscription_t *) list_item_next((void *) obs))
         {
             if (r->dirty == 0 && obs->dirty == 0)
-                /* running this resource due to partiallydirty, but this observation's notification was already enqueued */
+                /* running this resource due to partiallydirty,
+                 * but this observation's notification was already enqueued */
                 continue;
 
             coap_tid_t tid = COAP_INVALID_TID;
@@ -782,19 +787,19 @@ static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
             token.length = obs->token_length;
             token.s = obs->token;
 
-            response->hdr->id = coap_new_message_id(context);
+            response->transport_hdr->udp.id = coap_new_message_id(context);
             if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON)
             {
-                response->hdr->type = COAP_MESSAGE_NON;
+                response->transport_hdr->udp.type = COAP_MESSAGE_NON;
             }
             else
             {
-                response->hdr->type = COAP_MESSAGE_CON;
+                response->transport_hdr->udp.type = COAP_MESSAGE_CON;
             }
             /* fill with observer-specific data */
             h(context, r, &obs->subscriber, NULL, &token, response);
 
-            if (response->hdr->type == COAP_MESSAGE_CON)
+            if (response->transport_hdr->udp.type == COAP_MESSAGE_CON)
             {
                 tid = coap_send_confirmed(context, &obs->subscriber, response);
                 obs->non_cnt = 0;
@@ -805,7 +810,7 @@ static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
                 obs->non_cnt++;
             }
 
-            if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON)
+            if (COAP_INVALID_TID == tid || response->transport_hdr->udp.type != COAP_MESSAGE_CON)
                 coap_delete_pdu(response);
             if (COAP_INVALID_TID == tid)
             {
@@ -866,7 +871,8 @@ static void coap_remove_failed_observers(coap_context_t *context, coap_resource_
 {
     coap_subscription_t *obs;
 
-    for (obs = list_head(resource->subscribers); obs; obs = list_item_next(obs))
+    for (obs = (coap_subscription_t *) list_head(resource->subscribers);
+            obs; obs = (coap_subscription_t *) list_item_next((void *) obs))
     {
         if (coap_address_equals(peer, &obs->subscriber) && token->length == obs->token_length
                 && memcmp(token->s, obs->token, token->length) == 0)