[Tinyos-2-commits] [tinyos-main] r5754 committed - fixed example CoAP server and client.

tinyos-main at googlecode.com tinyos-main at googlecode.com
Tue Sep 20 08:00:47 PDT 2011


Revision: 5754
Author:   markushx at gmail.com
Date:     Tue Sep 20 08:00:19 2011
Log:      fixed example CoAP server and client.


http://code.google.com/p/tinyos-main/source/detail?r=5754

Modified:
  /trunk/support/sdk/c/coap/examples/client.c
  /trunk/support/sdk/c/coap/examples/server.c

=======================================
--- /trunk/support/sdk/c/coap/examples/client.c	Wed Jun  8 07:02:47 2011
+++ /trunk/support/sdk/c/coap/examples/client.c	Tue Sep 20 08:00:19 2011
@@ -301,32 +301,36 @@
      }

  #ifdef SHOWREALVALUES
-    if (strstr(uri.path.s, "st") ) {
+
+
+
+
+    if (strcmp(uri.path.s, "st") == 0 ) {
        printf("\n** Temperatur: %4.2f K\n\n",  
((float)char_to_uint16(node->pdu->data))/100);
-    } else if (strstr(uri.path.s, "sh")) {
+    } else if (strcmp(uri.path.s, "sh") == 0) {
        printf("\n** Humidity: %4.2f %% \n\n",   
((float)char_to_uint16(node->pdu->data))/100);
-    } else if (strstr(uri.path.s, "sv")) {
+    } else if (strcmp(uri.path.s, "sv") == 0) {
        printf("** Voltage: %4.2f V\n\n",   
((float)char_to_uint16(node->pdu->data))/100);
-    } else if (strstr(uri.path.s, "ck")) {
+    } else if (strcmp(uri.path.s, "ck") == 0) {
        if (len != 0) {
  	printf("** AES Key received \n");
        } else {
  	printf("** AES Key set\n");
        }
-    } else if (strstr(uri.path.s, "tsr")) {
+    } else if (strcmp(uri.path.s, "tsr") == 0) {
        printf("** tsr\n");
-    } else if (strstr(uri.path.s, "par")) {
+    } else if (strcmp(uri.path.s, "par") == 0) {
        printf("** par\n");
-    } else if (strstr(uri.path.s, "l")) {
+    } else if (strcmp(uri.path.s, "l") == 0) {
        if (len != 0) {
  	printf("\n** led 0 (red)   %s\n** led 1 (green) %s\n** led 2  
(blue)  %s\n\n",
  	       ( (char_to_uint16(node->pdu->data)    %2) == 0) ? "OFF": "ON",
  	       (((char_to_uint16(node->pdu->data)>>1)%2) == 0) ? "OFF": "ON",
  	       (((char_to_uint16(node->pdu->data)>>2)%2) == 0) ? "OFF": "ON");
        } else {
-	printf("** LED'S set\n");
-      }
-    } else if (strstr(uri.path.s, "r")) {
+	printf("** LEDs set\n");
+      }
+    } else if (strcmp(uri.path.s, "r") == 0) {
        val_all val = char_to_val_all(node->pdu->data);
        printf("\n** All values: \n");
        if (val.temp != (0xFFFF | 0xFFFE))
@@ -341,7 +345,9 @@
  	printf("** Voltage:     %4.2f V\n\n",  ((float) val.volt)/100);
        else
  	printf("** Voltage:     NaN\n\n");
-    }  else {
+    } else if (strcmp(uri.path.s, "lipsum") == 0) {
+    } else if (strcmp(uri.path.s, "time") == 0) {
+    } else {
        printf("** unknown URI\n");
      }
  #endif
@@ -793,9 +799,9 @@
  	coap_dispatch( ctx );	/* and dispatch PDUs from receivequeue */

  	// koo second read is done to process the asyn response. TODO this has to  
be done properly by checking the type of message
-	coap_read( ctx );	/* read received data */
-	coap_dispatch( ctx );	/* and dispatch PDUs from receivequeue */
-	// koo
+// 	coap_read( ctx );	/* read received data */
+// 	coap_dispatch( ctx );	/* and dispatch PDUs from receivequeue */
+// 	// koo
        }
      }
    }
=======================================
--- /trunk/support/sdk/c/coap/examples/server.c	Mon Jun 20 00:35:02 2011
+++ /trunk/support/sdk/c/coap/examples/server.c	Tue Sep 20 08:00:19 2011
@@ -2,17 +2,17 @@
   *         as defined in draft-ietf-core-coap-01
   *
   * Copyright (C) 2010 Olaf Bergmann <bergmann at tzi.org>
- *
+ *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
- *
+ *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
- *
+ *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA   
02110-1301, USA.
@@ -59,7 +59,7 @@
    }

  /* temporary storage for dynamic resource representations */
-static char resource_buf[20000];
+static char resource_buf[20000];
  static int quit = 0;

  /* SIGINT handler: set quit to 1 for graceful termination */
@@ -94,7 +94,7 @@
    unsigned char ct = COAP_MEDIATYPE_APPLICATION_LINK_FORMAT;
    if (!pdu)
      return;
-
+
    /* add content-encoding */
    coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, 1, &ct);

@@ -131,26 +131,26 @@
      if ( COAP_OPT_VALUE(*accept)[t] == mediatype )
        return 1;
    }
-
+
    return 0;
  }

  /* Check if provided path name is a valid CoAP URI path. */
-int
+int
  is_valid(char *prefix, unsigned char *path, unsigned int length) {
-  enum { START, PATH, DOT, DOTDOT } state;
+  enum { START, PATH, DOT, DOTDOT } state;

    if (!path || length < strlen(prefix) ||
        strncmp((char *)path, prefix, strlen(prefix)) != 0)
      return 0;
-
+
    path += strlen(prefix);
    length -= strlen(prefix);
    if ( length && *path == '/' ) {
      state = START;
      ++path;
      --length;
-  } else
+  } else
      state = PATH;

    while (length) {
@@ -193,12 +193,12 @@
    for (n = 0; n < maxlen && p[n]; n++)
      ;
    return n;
-}
+}
  #endif

-int
-resource_wellknown(coap_context_t *ctx, coap_resource_t *resource,
-		   unsigned char *mediatype, unsigned int offset,
+int
+resource_wellknown(coap_context_t *ctx, coap_resource_t *resource,
+		   unsigned char *mediatype, unsigned int offset,
  		   unsigned char *buf, unsigned int *buflen,
  		   int *finished);

@@ -228,7 +228,7 @@
      pdu = new_response(ctx, node, COAP_RESPONSE_200);
      if ( !pdu )
        return NULL;
-
+
      add_contents( pdu, COAP_MEDIATYPE_TEXT_PLAIN, sizeof(INDEX) - 1,  
(unsigned char *)INDEX );
      goto ok;
    }
@@ -239,7 +239,7 @@
      return new_response(ctx, node, COAP_RESPONSE_404);

    /* check if requested mediatypes match */
-  if ( coap_check_option(node->pdu, COAP_OPTION_ACCEPT)
+  if ( coap_check_option(node->pdu, COAP_OPTION_ACCEPT)
         && !mediatype_matches(node->pdu, resource->mediatype) ) {
      debug("media type mismatch\n");
      return new_response(ctx, node, COAP_RESPONSE_415);
@@ -247,7 +247,7 @@

    block = coap_check_option(node->pdu, COAP_OPTION_BLOCK);
    if ( block ) {
-    blk = coap_decode_var_bytes(COAP_OPT_VALUE(*block),
+    blk = coap_decode_var_bytes(COAP_OPT_VALUE(*block),
  				COAP_OPT_LENGTH(*block));
      blklen = 16 << (blk & 0x07);
    } else {
@@ -261,16 +261,16 @@
      mediatype = resource->mediatype;

      code = resource->data(&uri, &(node->pdu->hdr->id), &mediatype,
-			  (blk & ~0x0f) << (blk & 0x07), buf, &blklen,
+			  (blk & ~0x0f) << (blk & 0x07), buf, &blklen,
  			  &finished, COAP_REQUEST_GET);
    } else {
      /* check if the well-known URI was requested */
-    if (memcmp(uri.path.s, COAP_DEFAULT_URI_WELLKNOWN,
+    if (memcmp(uri.path.s, COAP_DEFAULT_URI_WELLKNOWN,
  	       MIN(uri.path.length, sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1))
  	== 0) {
        mediatype = resource->mediatype;
        code = resource_wellknown(ctx, resource, &mediatype,
-				(blk & ~0x0f) << (blk & 0x07), buf, &blklen,
+				(blk & ~0x0f) << (blk & 0x07), buf, &blklen,
  				&finished);
      } else {
        /* no callback available, set code, blklen and finished manually
@@ -283,21 +283,21 @@

    if ( !(pdu = new_response(ctx, node, code)) )
      return NULL;
-
-  if ( blklen > 0 ) {
+
+  if ( blklen > 0 ) {
      /* add content-type */
-    if ( mediatype != COAP_MEDIATYPE_ANY )
+    if ( mediatype != COAP_MEDIATYPE_ANY )
        coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, 1, &mediatype);

      /* set Max-age option unless resource->maxage is zero */
      if (resource->maxage) {
-      coap_add_option(pdu, COAP_OPTION_MAXAGE,
+      coap_add_option(pdu, COAP_OPTION_MAXAGE,
  		      coap_encode_var_bytes(optbuf, resource->maxage), optbuf);
      }

      /* set Etag option unless resource->etag is zero */
      if (*resource->etag) {
-      coap_add_option(pdu, COAP_OPTION_ETAG,
+      coap_add_option(pdu, COAP_OPTION_ETAG,
  		      strnlen((char *)resource->etag,4), resource->etag);
      }

@@ -307,26 +307,26 @@
        duration = COAP_PSEUDOFP_DECODE_8_4(*COAP_OPT_VALUE(*sub));
        debug("*** add subscription for %d seconds\n", duration);
        enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls);
-      coap_add_option(pdu, COAP_OPTION_SUBSCRIPTION, 1, &enc);
-
+      coap_add_option(pdu, COAP_OPTION_SUBSCRIPTION, 1, &enc);
+
        /* refresh only if already subscribed */
        token.length = 0;
        tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN);
        if (tok) {
  	COAP_SET_STR(&token, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok));
-	coap_add_option(pdu, COAP_OPTION_TOKEN,
+	coap_add_option(pdu, COAP_OPTION_TOKEN,
  			COAP_OPT_LENGTH(*tok),
  			COAP_OPT_VALUE(*tok));
        }

-      subscription =
-	coap_find_subscription(ctx, coap_uri_hash(&uri), &(node->remote),
+      subscription =
+	coap_find_subscription(ctx, coap_uri_hash(&uri), &(node->remote),
  			       tok ? &token : NULL);

        if (subscription) {	/* refresh existing subscription */
  	subscription->expires = time(NULL)+duration;
        } else {			/* add new subscription */
-	subscription = coap_new_subscription(ctx, &uri, &(node->remote),
+	subscription = coap_new_subscription(ctx, &uri, &(node->remote),
  					     time(NULL)+duration);
  	if (subscription) {
  	  if (token.length) {
@@ -348,7 +348,7 @@
      if ( block || !finished ) {
        blk = (blk & ~0x08) | (!finished << 3);
        /* add block option to PDU */
-      coap_add_option(pdu, COAP_OPTION_BLOCK,
+      coap_add_option(pdu, COAP_OPTION_BLOCK,
  		      coap_encode_var_bytes(optbuf, blk), optbuf);
      }

@@ -366,7 +366,7 @@
    return pdu;
  }

-int
+int
  write_file(char *filename, unsigned char *text, int length) {
    FILE *file;
    ssize_t written;
@@ -405,7 +405,7 @@

    if ( !(pdu = new_response(ctx, node, COAP_RESPONSE_200)) )
      return NULL;
-
+
    /* create a zero-terminated string */
    memcpy(filename, uri.path.s, uri.path.length);
    filename[uri.path.length] = '\0';
@@ -414,13 +414,13 @@
    written = write_file(filename, node->pdu->data, length);

    /* set etag from file's modification time (byte-order does not care) */
-  if ( (stat(filename, &statbuf) == 0) &&
+  if ( (stat(filename, &statbuf) == 0) &&
         S_ISREG(statbuf.st_mode) ) {
      memcpy(resource->etag, &statbuf.st_mtime, sizeof(resource->etag));
    } else {			/* clear etag */
      *resource->etag = 0;
    }
-
+
    if (written < length)
      return new_response(ctx, node, COAP_RESPONSE_500);

@@ -430,11 +430,11 @@
    return pdu;
  }

-int
-resource_from_file(coap_uri_t *uri,
-		   unsigned char *mediatype, unsigned int offset,
+int
+resource_from_file(coap_uri_t *uri, coap_tid_t  *id,
+		   unsigned char *mediatype, unsigned int offset,
  		   unsigned char *buf, unsigned int *buflen,
-		   int *finished);
+		   int *finished, unsigned int method);

  coap_pdu_t *
  handle_post(coap_context_t  *ctx, coap_queue_t *node, void *data) {
@@ -455,7 +455,7 @@
      return handle_put(ctx, node, data);

    /* create new resource */
-  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
+  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
      return new_response(ctx, node, COAP_RESPONSE_500);

    tok = coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE);
@@ -464,9 +464,9 @@
     * file system. We restrict the storage area to DATASINK_PREFIX.
     */
    memset(name, 0, sizeof(name));
-  namelen = snprintf(name, sizeof(name)-1, DATASINK_PREFIX "%lX",
+  namelen = snprintf(name, sizeof(name)-1, DATASINK_PREFIX "%lX",
  		     coap_uri_hash(&uri));
-
+
    r->uri = coap_new_uri((unsigned char *)name, namelen);
    r->name = coap_new_string(uri.path.length);
    if (r->name) {
@@ -478,14 +478,14 @@
    r->dirty = 1;
    r->writable = 1;
    r->data = resource_from_file;
-
+
    /* we know that r->uri.s is zero-terminated */
-  length =
+  length =
      (unsigned char *)node->pdu->hdr + node->pdu->length - node->pdu->data;
    written = write_file((char *)r->uri->path.s, node->pdu->data, length);

    /* set etag from file's modification time (byte-order does not care) */
-  if ( (stat((char *)r->uri->path.s, &statbuf) == 0) &&
+  if ( (stat((char *)r->uri->path.s, &statbuf) == 0) &&
         S_ISREG(statbuf.st_mode) ) {
      memcpy(r->etag, &statbuf.st_mtime, sizeof(r->etag));
    } else {			/* clear etag */
@@ -497,19 +497,19 @@
      return new_response(ctx, node, COAP_RESPONSE_500);
    } else
      coap_add_resource(ctx, r);
-
+
    /* create the response */
    pdu = new_response(ctx, node, COAP_RESPONSE_201);

    /* add location header */
-  coap_add_option(pdu, COAP_OPTION_LOCATION,
+  coap_add_option(pdu, COAP_OPTION_LOCATION,
  		  namelen, (unsigned char *)name);

    /* we do not need the request URI, only a token, if specified */

    tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN);
-  if (tok)
-    coap_add_option(pdu, COAP_OPTION_TOKEN,
+  if (tok)
+    coap_add_option(pdu, COAP_OPTION_TOKEN,
  		    COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok));

    return pdu;
@@ -529,7 +529,7 @@
    r = coap_get_resource(ctx, &uri);
    if (!r)
      return new_response(ctx, node, COAP_RESPONSE_404);
-
+
    if (!r->writable) {
      debug("tried to remove resource that is read-only\n");
      pdu = new_response(ctx, node, COAP_RESPONSE_400);
@@ -539,31 +539,31 @@

    if (FILENAME_MAX < uri.path.length)
      return new_response(ctx, node, COAP_RESPONSE_500);
-
+
    memcpy(filename, uri.path.s, uri.path.length);
    filename[uri.path.length] = '\0';

    debug("unlink %s\n", filename);
    unlink(filename);

-  /* create the response */
+  /* create the response */
    pdu = new_response(ctx, node, COAP_RESPONSE_200);

    if (uri.na.length)
-    coap_add_option(pdu, COAP_OPTION_URI_AUTHORITY,
+    coap_add_option(pdu, COAP_OPTION_URI_AUTHORITY,
  		    uri.na.length, uri.na.s);

    if (uri.path.length)
-    coap_add_option(pdu, COAP_OPTION_URI_PATH,
+    coap_add_option(pdu, COAP_OPTION_URI_PATH,
  		    uri.path.length, uri.path.s);
-
+
    tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN);
-  if (tok)
-    coap_add_option(pdu, COAP_OPTION_TOKEN,
+  if (tok)
+    coap_add_option(pdu, COAP_OPTION_TOKEN,
  		    COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok));

    if (uri.query.length)
-    coap_add_option(pdu, COAP_OPTION_URI_QUERY,
+    coap_add_option(pdu, COAP_OPTION_URI_QUERY,
  		    uri.query.length, uri.query.s);

    coap_delete_resource(ctx, coap_uri_hash(&uri));
@@ -571,7 +571,7 @@
    return pdu;
  }

-void
+void
  message_handler(coap_context_t  *ctx, coap_queue_t *node, void *data) {
    coap_pdu_t *pdu = NULL;

@@ -584,7 +584,7 @@
      debug("dropped packet with unknown version %u\n",  
node->pdu->hdr->version);
      return;
    }
-
+
    switch (node->pdu->hdr->code) {
    case COAP_REQUEST_GET :
      pdu = handle_get(ctx, node, data);
@@ -625,7 +625,7 @@

  }

-void
+void
  usage( const char *program, const char *version) {
    const char *p;

@@ -662,7 +662,7 @@
    /* get the first suitable interface identifier */
    for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) {
      if ( ainfo->ai_family == AF_INET6 ) {
-      mreq.ipv6mr_interface =
+      mreq.ipv6mr_interface =
  	      ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id;
        break;
      }
@@ -682,34 +682,34 @@

    for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
      if ( ainfo->ai_family == AF_INET6 ) {
-      mreq.ipv6mr_multiaddr =
+      mreq.ipv6mr_multiaddr =
  	((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
        break;
      }
    }
-
+
    result = setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  		       (char *)&mreq, sizeof(mreq) );
-  if ( result < 0 )
+  if ( result < 0 )
      perror("join: setsockopt");

   finish:
    freeaddrinfo(resmulti);
    freeaddrinfo(reslocal);
-
+
    return result;
  }

-int
+int
  print_link(coap_resource_t *resource, unsigned char *buf, size_t buflen) {
    size_t n = 0;

    assert(resource);
    assert(buf);

-  if (buflen < resource->uri->path.length + 3)
+  if (buflen < resource->uri->path.length + 3)
      return -1;
-
+
    /* FIXME: calculate maximum length and return if longer than buflen */
    buf[n++] = '<'; buf[n++] = '/';

@@ -740,21 +740,21 @@

        n += snprintf((char *)(buf + n), buflen - n, " (read-only)");
      }
-
+
      buf[n++] = '"';
    }

    return  n;
  }

-int
+int
  resource_wellknown(coap_context_t *ctx,
-		   coap_resource_t *resource,
-		   unsigned char *mediatype, unsigned int offset,
+		   coap_resource_t *resource,
+		   unsigned char *mediatype, unsigned int offset,
  		   unsigned char *buf, unsigned int *buflen,
  		   int *finished) {
  #define RESOURCE_BUFLEN 4000
-  static unsigned char resources[RESOURCE_BUFLEN];
+  static unsigned char resources[RESOURCE_BUFLEN];
    size_t maxlen = 0;
    int n;
    coap_list_t *node;
@@ -769,7 +769,7 @@
        debug("resource description too long, truncating\n");
        resources[maxlen] = '\0';
        break;
-    }
+    }
      maxlen += n;

      if (node->next) 		/* check if another entry follows */
@@ -786,7 +786,7 @@
      *mediatype = COAP_MEDIATYPE_APPLICATION_LINK_FORMAT;
      break;
    default :
-    *buflen = 0;
+    *buflen = 0;
      return COAP_RESPONSE_415;
    }

@@ -802,11 +802,11 @@
    return COAP_RESPONSE_200;
  }

-int
-resource_time(coap_uri_t *uri,
-	      unsigned char *mediatype, unsigned int offset,
+int
+resource_time(coap_uri_t *uri, coap_tid_t  *id,
+	      unsigned char *mediatype, unsigned int offset,
  	      unsigned char *buf, unsigned int *buflen,
-	      int *finished) {
+	      int *finished, unsigned int method) {
    static unsigned char b[400];
    size_t maxlen;
    time_t now;
@@ -849,11 +849,11 @@
    return COAP_RESPONSE_200;
  }

-int
-resource_lipsum(coap_uri_t *uri,
-		unsigned char *mediatype, unsigned int offset,
+int
+resource_lipsum(coap_uri_t *uri, coap_tid_t  *id,
+		unsigned char *mediatype, unsigned int offset,
  		unsigned char *buf, unsigned int *buflen,
-		int *finished) {
+		int *finished, unsigned int method) {
    static unsigned char verylargebuf[] = "Lorem ipsum dolor sit amet,  
consectetur adipiscing elit. Mauris fermentum, lacus elementum venenatis  
aliquet, tortor risus laoreet sapien, a vulputate libero dolor ut odio.  
Vivamus congue elementum fringilla. Suspendisse porttitor, lectus sed  
gravida volutpat, dolor magna gravida massa, id fermentum lectus mi quis  
erat. Suspendisse lacinia, libero in euismod bibendum, magna nisi tempus  
lacus, eu suscipit augue nisi vel nulla. Praesent gravida lacus nec elit  
vestibulum sit amet rhoncus dui fringilla. Quisque diam lacus, ullamcorper  
non consectetur vitae, pellentesque eget lectus. Vestibulum velit nulla,  
venenatis vel mattis at, scelerisque nec mauris. Nulla facilisi. Mauris vel  
erat mi. Morbi et nulla nibh, vitae cursus eros. In convallis, magna  
egestas dictum porttitor, diam magna sagittis nisi, rhoncus tincidunt  
ligula felis sed mauris. Pellentesque pulvinar ante id velit convallis in  
porttitor justo imperdiet. Curabitur viverra placerat tincidunt. Vestibulum  
justo lacus, sollicitudin in facilisis vel, tempus nec erat. Duis varius  
viverra aliquet. In tempor varius elit vel pharetra. Sed mattis, quam in  
pulvinar ullamcorper, est ipsum tempor dui, at fringilla magna sem in  
sapien. Phasellus sollicitudin ornare sem, nec porta libero tempus vitae.  
Maecenas posuere pulvinar dictum. Vestibulum ante ipsum primis in faucibus  
orci luctus et ultrices posuere cubilia Curae; Cras eros mauris, pulvinar  
tempor facilisis ut, condimentum in magna. Nullam eget ipsum sit amet lacus  
massa nunc.<EOT>";
    unsigned int maxlen = sizeof(verylargebuf) - 1;

@@ -880,9 +880,9 @@
    return COAP_RESPONSE_200;
  }

-int
-_resource_from_dir(char *filename,
-		   unsigned char *mediatype, unsigned int offset,
+int
+_resource_from_dir(char *filename,
+		   unsigned char *mediatype, unsigned int offset,
  		   unsigned char *buf, unsigned int *buflen,
  		   int *finished) {
    DIR *dir;
@@ -922,7 +922,7 @@
  	continue;
      }

-    if (pos + overhead + namelen * 2 > sizeof(resource_buf) - 1)
+    if (pos + overhead + namelen * 2 > sizeof(resource_buf) - 1)
        break;			/* broken */

      if ( pos + overhead + namelen * 2 < offset) {
@@ -931,11 +931,11 @@
        pos += sprintf(resource_buf + pos, "</%s/%s>;n=\"%s\",",
  		     filename, dirent->d_name, dirent->d_name);
      }
-
-    if ( pos > offset + *buflen )
+
+    if ( pos > offset + *buflen )
        break;
    }
-
+
    if (errno != 0)
      goto error;

@@ -949,7 +949,7 @@
    if ( (offset < pos) && (pos <= offset + *buflen) ) {
      *buflen = pos - offset - 1;
      *finished = 1;
-  } else
+  } else
      *finished = 0;

    memcpy(buf, resource_buf + offset, *buflen);
@@ -963,11 +963,11 @@
    return COAP_RESPONSE_500;
  }

-int
-resource_from_file(coap_uri_t *uri,
-		   unsigned char *mediatype, unsigned int offset,
+int
+resource_from_file(coap_uri_t *uri, coap_tid_t  *id,
+		   unsigned char *mediatype, unsigned int offset,
  		   unsigned char *buf, unsigned int *buflen,
-		   int *finished) {
+		   int *finished, unsigned int method) {
    static char filename[FILENAME_MAX+1];
    struct stat statbuf;
    FILE *file;
@@ -1004,7 +1004,7 @@
      code = COAP_RESPONSE_404;
      goto error;
    }
-
+
    if ( offset > statbuf.st_size ) {
      code = COAP_RESPONSE_400;
      goto error;
@@ -1023,25 +1023,25 @@
      code = COAP_RESPONSE_500;
      goto error;
    }
-
+
    *buflen = fread(buf, 1, *buflen, file);
    fclose(file);
-
+
    *finished = offset + *buflen >= statbuf.st_size;

    return COAP_RESPONSE_200;

- error:
-  *buflen = 0;
+ error:
+  *buflen = 0;
    *finished = 1;
    return code;
  }

  int
-resource_ni(coap_uri_t *uri,
+resource_ni(coap_uri_t *uri, coap_tid_t  *id,
  	    unsigned char *mediatype, unsigned int offset,
  	    unsigned char *buf, unsigned int *buflen,
-	    int *finished) {
+	    int *finished, unsigned int method) {
    *finished = 1;
    return COAP_RESPONSE_200;
  }
@@ -1055,7 +1055,7 @@
      memcpy((r)->name->s, (st), (r)->name->length); \
    }

-void
+void
  init_resources(coap_context_t *ctx) {
    static const char *u_lipsum = "/lipsum";
    static const char *d_lipsum = "some large text to test buffer sizes  
(<EOT> marks its end)";
@@ -1069,7 +1069,7 @@
    static const char *d_ni = "node integrate";
    coap_resource_t *r;

-  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
+  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
      return;

    memset(r, 0, sizeof(coap_resource_t));
@@ -1080,7 +1080,7 @@
    r->writable = 0;
    coap_add_resource( ctx, r );

-  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
+  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
      return;

    memset(r, 0, sizeof(coap_resource_t));
@@ -1093,7 +1093,7 @@
    r->maxage = 1209600;		/* two weeks */
    coap_add_resource( ctx, r );

-  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
+  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
      return;

    memset(r, 0, sizeof(coap_resource_t));
@@ -1106,7 +1106,7 @@
    r->maxage = 1;
    coap_add_resource( ctx, r );

-  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
+  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
      return;

    memset(r, 0, sizeof(coap_resource_t));
@@ -1119,7 +1119,7 @@
    write_file("filestorage",(unsigned char *)"initial text", 12);
    coap_add_resource( ctx, r );

-  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
+  if ( !(r = coap_malloc( sizeof(coap_resource_t) )))
      return;

    memset(r, 0, sizeof(coap_resource_t));
@@ -1146,7 +1146,7 @@
    coap_add_resource(ctx, r);
  }

-int
+int
  main(int argc, char **argv) {
    coap_context_t  *ctx;
    fd_set readfds;
@@ -1185,9 +1185,9 @@
    signal(SIGINT, handle_sigint);

    while ( !quit ) {
-    FD_ZERO(&readfds);
+    FD_ZERO(&readfds);
      FD_SET( ctx->sockfd, &readfds );
-
+
      nextpdu = coap_peek_next( ctx );

      time(&now);
@@ -1207,7 +1207,7 @@
        timeout = &tv;
      }
      result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
-
+
      if ( result < 0 ) {		/* error */
        if (errno != EINTR)
  	perror("select");
@@ -1215,7 +1215,7 @@
        if ( FD_ISSET( ctx->sockfd, &readfds ) ) {
  	coap_read( ctx );	/* read received data */
  	coap_dispatch( ctx );	/* and dispatch PDUs from receivequeue */
-      }
+      }
      } else {			/* timeout */
        coap_check_resource_list( ctx );
        coap_check_subscriptions( ctx );


More information about the Tinyos-2-commits mailing list