CoAP Device Server Client

Dependencies:   EthernetInterface mbed-rtos mbed nsdl_lib

Fork of COAPmbed by th.iotkit2.ch

Constrained Application Protocol (Coap) ist ein Software-Protokoll welches für Internet der Dinge Geräte zugeschnitten ist.

COAP ist auf den meisten Geräten, die UDP Unterstützen, lauffähig.

Ein COAP fähiges Gerät publiziert seine Sensoren und Aktoren in einem Resource Directory oder stellt selber ein solches zur Verfügung.

Mittels Resource Discovery können die vorhandenen Sensoren und Aktoren mit ihren Attributen abgefragt werden.

Zeile 29: Node Name aendern und folgende Adresse aufrufen: http://nsp.cloudapp.net:8083/, User/PW = demo

Revision:
1:e35d7f10999a
Parent:
0:2edbfea18d23
Child:
2:7e489126fe7a
--- a/main.cpp	Wed Oct 09 09:31:17 2013 +0000
+++ b/main.cpp	Tue Oct 15 09:17:29 2013 +0000
@@ -24,26 +24,16 @@
 #define GW      "10.45.0.1"
 
 /* Change this IP address to that of your NanoService Platform installation */
-const char* NSP_ADDRESS = "10.45.3.42"; /* internal NSP*/
+const char* NSP_ADDRESS = "217.140.101.40"; /* internal NSP*/
 const int NSP_PORT = 5683;
 
 uint8_t nsp_addr[4];
 
 /* The number of seconds between NSP registration messages */
 #define RD_UPDATE_PERIOD  60
+static uint8_t res_gps_val[] = {"52.182382,0.178849"};
 
-/* Resource paths and registration parameters */
-static uint8_t res_mgf[] = {"dev/mfg"};
-static uint8_t res_mgf_val[] = {"Sensinode"};
-static uint8_t res_mdl[] = {"dev/mdl"};
-static uint8_t res_mdl_val[] = {"NSDL-C power node"};
-static uint8_t res_temp[] = {"sen/temp"};
-static uint8_t res_light[] = {"lt/0/dim"};
-static uint8_t res_gps[] = {"gps/loc"};
-static uint8_t res_gps_val[] = {"52.182382,0.178849"};
-static uint8_t res_rel[] = {"lt/0/on"};
-
-static uint8_t ep_type[] = {"PowerNode"};
+static uint8_t ep_type[] = {"mbed_device"};
 static uint8_t lifetime_ptr[] = {"1200"};
 
 /* stored data for observable resource */
@@ -212,13 +202,13 @@
     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
 
     /* Static resurces */
-    CREATE_STATIC_RESOURCE(resource_ptr, sizeof(res_mgf)-1, (uint8_t*) res_mgf, 0, 0,  (uint8_t*) res_mgf_val, sizeof(res_mgf_val)-1);
-    CREATE_STATIC_RESOURCE(resource_ptr, sizeof(res_mdl)-1, (uint8_t*) res_mdl, 0, 0,  (uint8_t*) res_mdl_val, sizeof(res_mdl_val)-1);
+    CREATE_STATIC_RESOURCE(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0,  (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
+    CREATE_STATIC_RESOURCE(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0,  (uint8_t*) "NSDL-C mbed device", sizeof("NSDL-C mbed device")-1);
 
-    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof(res_temp)-1, (uint8_t*) res_temp, 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED);
-    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof(res_light)-1, (uint8_t*) res_light, 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
-    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof(res_gps)-1, (uint8_t*) res_gps, 0, 0, 0, &gps_resource_cb, SN_GRS_GET_ALLOWED);
-    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof(res_rel)-1, (uint8_t*) res_rel, 0, 0, 0, &relay_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
+    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("sen/temp")-1, (uint8_t*) "sen/temp", 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED);
+    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("lt/0/dim")-1, (uint8_t*) "lt/0/dim", 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
+    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("gps/loc")-1, (uint8_t*) "gps/loc", 0, 0, 0, &gps_resource_cb, SN_GRS_GET_ALLOWED);
+    CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("beep/0/on")-1, (uint8_t*) "beep/0/on", 0, 0, 0, &relay_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
 
 
         /* Register with NSP */
@@ -317,14 +307,8 @@
 
     sn_nsdl_send_coap_message(address, coap_res_ptr);
 
-    /* sn_coap_release.... */
-    if(coap_res_ptr->options_list_ptr)
-        free(coap_res_ptr->options_list_ptr);
-    if(coap_res_ptr->token_ptr)
-    {
-        own_free(coap_res_ptr->token_ptr);
-    }
-    own_free(coap_res_ptr);
+    coap_res_ptr->options_list_ptr->observe_ptr = 0;
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
     return 0;
 }
 
@@ -347,8 +331,9 @@
 
         coap_res_ptr->payload_len = strlen(led_dimm_temp);
         coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp;
+        sn_nsdl_send_coap_message(address, coap_res_ptr);
     }
-    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
+    else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
     {
         memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
 
@@ -363,16 +348,10 @@
         //led4.write(led_dimm);
 
         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
+        sn_nsdl_send_coap_message(address, coap_res_ptr);
     }
 
-    sn_nsdl_send_coap_message(address, coap_res_ptr);
-
-    /* sn_coap_release.... */
-    if(coap_res_ptr->token_ptr)
-    {
-        own_free(coap_res_ptr->token_ptr);
-    }
-    own_free(coap_res_ptr);
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
     return 0;
 }
 
@@ -396,12 +375,8 @@
 
     sn_nsdl_send_coap_message(address, coap_res_ptr);
 
-    /* sn_coap_release.... */
-    if(coap_res_ptr->token_ptr)
-    {
-        own_free(coap_res_ptr->token_ptr);
-    }
-    own_free(coap_res_ptr);
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
+    
     return 0;
 }
 
@@ -419,8 +394,9 @@
 
         coap_res_ptr->payload_len = 1;
         coap_res_ptr->payload_ptr = &relay_state;
+        sn_nsdl_send_coap_message(address, coap_res_ptr);
     }
-    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
+    else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
     {
         if(received_coap_ptr->payload_len)
         {
@@ -435,19 +411,12 @@
                 buzzer.nobeep();
                 relay_state = '0';
             }
-            
             coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
-            
+            sn_nsdl_send_coap_message(address, coap_res_ptr);
         }
     }
 
-    sn_nsdl_send_coap_message(address, coap_res_ptr);
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
 
-    /* sn_coap_release.... */
-    if(coap_res_ptr->token_ptr)
-    {
-        own_free(coap_res_ptr->token_ptr);
-    }
-    own_free(coap_res_ptr);
     return 0;
 }
\ No newline at end of file