CoAP Device Server Client mit leshan Server

Dependencies:   EthernetInterface mbed-rtos mbed nsdl_lib

Fork of COAPmbed by smd.iotkit2.ch

Sandbox Server

Eclipse stellt einen Testserver unter http://leshan.eclipse.org/ zur Verfügung. Das Programm verbindet sich mit diesem und ist unter mbed-k64f... erreichbar.

Installation lokale Version leshan

wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-server-demo.jar
java -jar ./leshan-server-demo.jar

cURL

Die Funktionen können mittels cURL oder Browser wie folgt getestet werden:

# Alle Clients abfragen (Antwort im JSON Format)
curl -X GET  http://localhost:8080/api/clients
                         
# Wert von LED2 abfragen (Antwort im text/plain Format)                      
curl -X GET  http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 
 
# Wert für LED2 setzen
curl -X GET -vvv http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 -H "Content-Type: text/plain" -d "10"
Committer:
stefan1691
Date:
Sun Jun 21 12:12:34 2015 +0000
Revision:
15:fba25b7e63cd
CoAP Device Server Client mit leshan Server

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefan1691 15:fba25b7e63cd 1 /**
stefan1691 15:fba25b7e63cd 2 * Implementierung PwmOut Resource
stefan1691 15:fba25b7e63cd 3 */
stefan1691 15:fba25b7e63cd 4
stefan1691 15:fba25b7e63cd 5 #include "PwmOutResource.h"
stefan1691 15:fba25b7e63cd 6
stefan1691 15:fba25b7e63cd 7 /** Erste verkettete Resource */
stefan1691 15:fba25b7e63cd 8 PwmOutResource * firstPwmOutResource;
stefan1691 15:fba25b7e63cd 9
stefan1691 15:fba25b7e63cd 10 /** Default Konstruktor
stefan1691 15:fba25b7e63cd 11 * @param pin Pin fuer PwmOut
stefan1691 15:fba25b7e63cd 12 */
stefan1691 15:fba25b7e63cd 13 PwmOutResource::PwmOutResource( PinName pin, char *name, char *id )
stefan1691 15:fba25b7e63cd 14 {
stefan1691 15:fba25b7e63cd 15 resource = new PwmOut( pin );
stefan1691 15:fba25b7e63cd 16 this->id = id;
stefan1691 15:fba25b7e63cd 17 this->name = name;
stefan1691 15:fba25b7e63cd 18
stefan1691 15:fba25b7e63cd 19 // neu verketten - letztes hinzugefuegstes Element = first.
stefan1691 15:fba25b7e63cd 20 if ( firstPwmOutResource != (PwmOutResource *) 0 )
stefan1691 15:fba25b7e63cd 21 this->next = firstPwmOutResource;
stefan1691 15:fba25b7e63cd 22 firstPwmOutResource = this;
stefan1691 15:fba25b7e63cd 23 }
stefan1691 15:fba25b7e63cd 24
stefan1691 15:fba25b7e63cd 25 /** Erstellt die Resource
stefan1691 15:fba25b7e63cd 26 * @param sn_nsdl_resource_info_s CoAP struct
stefan1691 15:fba25b7e63cd 27 * @return 0 wenn ohne Fehler
stefan1691 15:fba25b7e63cd 28 */
stefan1691 15:fba25b7e63cd 29 int PwmOutResource::create( sn_nsdl_resource_info_s *resource_ptr )
stefan1691 15:fba25b7e63cd 30 {
stefan1691 15:fba25b7e63cd 31 nsdl_create_dynamic_resource(resource_ptr, strlen(id), (uint8_t*) id, strlen(name), (uint8_t*) name, 0, &PwmOutResource::callback, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
stefan1691 15:fba25b7e63cd 32 return ( 0 );
stefan1691 15:fba25b7e63cd 33 }
stefan1691 15:fba25b7e63cd 34
stefan1691 15:fba25b7e63cd 35 /** Callback wenn die Resource gelesen oder geaendert werden soll
stefan1691 15:fba25b7e63cd 36 * @param sn_coap_hdr_s
stefan1691 15:fba25b7e63cd 37 * @param sn_nsdl_addr_s
stefan1691 15:fba25b7e63cd 38 * @param sn_proto_info_s
stefan1691 15:fba25b7e63cd 39 * @return 0 wenn ohne Fehler
stefan1691 15:fba25b7e63cd 40 */
stefan1691 15:fba25b7e63cd 41 uint8_t PwmOutResource::callback( sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto )
stefan1691 15:fba25b7e63cd 42 {
stefan1691 15:fba25b7e63cd 43 sn_coap_hdr_s *coap_res_ptr = 0;
stefan1691 15:fba25b7e63cd 44 char buf[8];
stefan1691 15:fba25b7e63cd 45 char path[8];
stefan1691 15:fba25b7e63cd 46 PwmOutResource* current;
stefan1691 15:fba25b7e63cd 47
stefan1691 15:fba25b7e63cd 48 // Path wegkopieren mit \0
stefan1691 15:fba25b7e63cd 49 strncpy( path, (const char*) received_coap_ptr->uri_path_ptr, received_coap_ptr->uri_path_len );
stefan1691 15:fba25b7e63cd 50 printf("light callback %s\r\n", path );
stefan1691 15:fba25b7e63cd 51
stefan1691 15:fba25b7e63cd 52 // Resource laut path suchen
stefan1691 15:fba25b7e63cd 53 for ( current = firstPwmOutResource; current != (PwmOutResource *) 0; current = current->next )
stefan1691 15:fba25b7e63cd 54 if ( strcmp( path, current->id ) == 0 )
stefan1691 15:fba25b7e63cd 55 break;
stefan1691 15:fba25b7e63cd 56 // nichts gefunden - keine weitere Abhandlung
stefan1691 15:fba25b7e63cd 57 if ( current == (PwmOutResource*) 0 )
stefan1691 15:fba25b7e63cd 58 return ( 1 );
stefan1691 15:fba25b7e63cd 59
stefan1691 15:fba25b7e63cd 60 // CoAP GET
stefan1691 15:fba25b7e63cd 61 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
stefan1691 15:fba25b7e63cd 62 {
stefan1691 15:fba25b7e63cd 63 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
stefan1691 15:fba25b7e63cd 64
stefan1691 15:fba25b7e63cd 65 sprintf( buf, "%d", (int) (current->resource->read() * 100) );
stefan1691 15:fba25b7e63cd 66 coap_res_ptr->payload_len = strlen( buf );
stefan1691 15:fba25b7e63cd 67 coap_res_ptr->payload_ptr = (uint8_t*) buf;
stefan1691 15:fba25b7e63cd 68 sn_nsdl_send_coap_message(address, coap_res_ptr);
stefan1691 15:fba25b7e63cd 69 }
stefan1691 15:fba25b7e63cd 70 // CoAP PUT
stefan1691 15:fba25b7e63cd 71 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
stefan1691 15:fba25b7e63cd 72 {
stefan1691 15:fba25b7e63cd 73 memcpy(buf, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
stefan1691 15:fba25b7e63cd 74 buf[received_coap_ptr->payload_len] = '\0';
stefan1691 15:fba25b7e63cd 75 current->resource->write( atof( buf ) / 100 );
stefan1691 15:fba25b7e63cd 76
stefan1691 15:fba25b7e63cd 77 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
stefan1691 15:fba25b7e63cd 78 sn_nsdl_send_coap_message(address, coap_res_ptr);
stefan1691 15:fba25b7e63cd 79 }
stefan1691 15:fba25b7e63cd 80
stefan1691 15:fba25b7e63cd 81 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
stefan1691 15:fba25b7e63cd 82 return ( 0 );
stefan1691 15:fba25b7e63cd 83 }