50
50
#define REST_RES_SEPARATE 1
51
51
#define REST_RES_PUSHING 1
52
52
#define REST_RES_EVENT 1
53
+ #define REST_RES_SUB 1
53
54
#define REST_RES_LEDS 1
54
55
#define REST_RES_TOGGLE 1
55
56
#define REST_RES_LIGHT 0
56
57
#define REST_RES_BATTERY 0
58
+ #define REST_RES_RADIO 1
57
59
58
60
59
61
80
82
#if defined (PLATFORM_HAS_SHT11 )
81
83
#include "dev/sht11-sensor.h"
82
84
#endif
85
+ #if defined (PLATFORM_HAS_RADIO )
86
+ #include "dev/radio-sensor.h"
87
+ #endif
83
88
84
89
85
90
/* For CoAP-specific example: not required for normal RESTful Web service. */
@@ -475,7 +480,7 @@ pushing_periodic_handler(resource_t *r)
475
480
476
481
++ obs_counter ;
477
482
478
- PRINTF ("TICK %u for /%s\n" , periodic_i , r -> url );
483
+ PRINTF ("TICK %u for /%s\n" , obs_counter , r -> url );
479
484
480
485
/* Build notification. */
481
486
coap_packet_t notification [1 ]; /* This way the packet can be treated as pointer as usual. */
@@ -529,6 +534,36 @@ event_event_handler(resource_t *r)
529
534
}
530
535
#endif /* PLATFORM_HAS_BUTTON */
531
536
537
+ /******************************************************************************/
538
+ #if REST_RES_SUB
539
+ /*
540
+ * Example for a resource that also handles all its sub-resources.
541
+ * Use REST.get_url() to multiplex the handling of the request depending on the Uri-Path.
542
+ */
543
+ RESOURCE (sub , METHOD_GET | HAS_SUB_RESOURCES , "test/path" , "title=\"Sub-resource demo\"" );
544
+
545
+ void
546
+ sub_handler (void * request , void * response , uint8_t * buffer , uint16_t preferred_size , int32_t * offset )
547
+ {
548
+ REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
549
+
550
+ const char * uri_path = NULL ;
551
+ int len = REST .get_url (request , & uri_path );
552
+ int base_len = strlen (resource_sub .url );
553
+
554
+ if (len == base_len )
555
+ {
556
+ snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , "Request any sub-resource of /%s" , resource_sub .url );
557
+ }
558
+ else
559
+ {
560
+ snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , ".%s" , uri_path + base_len );
561
+ }
562
+
563
+ REST .set_response_payload (response , buffer , strlen ((char * )buffer ));
564
+ }
565
+ #endif
566
+
532
567
/******************************************************************************/
533
568
#if defined (PLATFORM_HAS_LEDS )
534
569
/******************************************************************************/
@@ -609,23 +644,23 @@ light_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
609
644
if ((num == 0 ) || (num && accept [0 ]== REST .type .TEXT_PLAIN ))
610
645
{
611
646
REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
612
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "%u;%u" , light_photosynthetic , light_solar );
647
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "%u;%u" , light_photosynthetic , light_solar );
613
648
614
- REST .set_response_payload (response , (uint8_t * )buffer , strlen (buffer ));
649
+ REST .set_response_payload (response , (uint8_t * )buffer , strlen (( char * ) buffer ));
615
650
}
616
651
else if (num && (accept [0 ]== REST .type .APPLICATION_XML ))
617
652
{
618
653
REST .set_header_content_type (response , REST .type .APPLICATION_XML );
619
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "<light photosynthetic=\"%u\" solar=\"%u\"/>" , light_photosynthetic , light_solar );
654
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "<light photosynthetic=\"%u\" solar=\"%u\"/>" , light_photosynthetic , light_solar );
620
655
621
- REST .set_response_payload (response , buffer , strlen (buffer ));
656
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
622
657
}
623
658
else if (num && (accept [0 ]== REST .type .APPLICATION_JSON ))
624
659
{
625
660
REST .set_header_content_type (response , REST .type .APPLICATION_JSON );
626
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "{'light':{'photosynthetic':%u,'solar':%u}}" , light_photosynthetic , light_solar );
661
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "{'light':{'photosynthetic':%u,'solar':%u}}" , light_photosynthetic , light_solar );
627
662
628
- REST .set_response_payload (response , buffer , strlen (buffer ));
663
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
629
664
}
630
665
else
631
666
{
@@ -651,16 +686,16 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
651
686
if ((num == 0 ) || (num && accept [0 ]== REST .type .TEXT_PLAIN ))
652
687
{
653
688
REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
654
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "%d" , battery );
689
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "%d" , battery );
655
690
656
- REST .set_response_payload (response , (uint8_t * )buffer , strlen (buffer ));
691
+ REST .set_response_payload (response , (uint8_t * )buffer , strlen (( char * ) buffer ));
657
692
}
658
693
else if (num && (accept [0 ]== REST .type .APPLICATION_JSON ))
659
694
{
660
695
REST .set_header_content_type (response , REST .type .APPLICATION_JSON );
661
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "{'battery':%d}" , battery );
696
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "{'battery':%d}" , battery );
662
697
663
- REST .set_response_payload (response , buffer , strlen (buffer ));
698
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
664
699
}
665
700
else
666
701
{
@@ -672,6 +707,67 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
672
707
#endif /* PLATFORM_HAS_BATTERY */
673
708
674
709
710
+ #if defined (PLATFORM_HAS_RADIO ) && REST_RES_RADIO
711
+ /* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
712
+ RESOURCE (radio , METHOD_GET , "sensor/radio" , "title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"" );
713
+
714
+ void
715
+ radio_handler (void * request , void * response , uint8_t * buffer , uint16_t preferred_size , int32_t * offset )
716
+ {
717
+ size_t len = 0 ;
718
+ const char * p = NULL ;
719
+ uint8_t param = 0 ;
720
+ int success = 1 ;
721
+
722
+ const uint16_t * accept = NULL ;
723
+ int num = REST .get_header_accept (request , & accept );
724
+
725
+ if ((len = REST .get_query_variable (request , "p" , & p ))) {
726
+ PRINTF ("p %.*s\n" , len , p );
727
+ if (strncmp (p , "lqi" , len )== 0 ) {
728
+ param = RADIO_SENSOR_LAST_VALUE ;
729
+ } else if (strncmp (p ,"rssi" , len )== 0 ) {
730
+ param = RADIO_SENSOR_LAST_PACKET ;
731
+ } else {
732
+ success = 0 ;
733
+ }
734
+ } else {
735
+ success = 0 ;
736
+ }
737
+
738
+ if (success ) {
739
+ if ((num == 0 ) || (num && accept [0 ]== REST .type .TEXT_PLAIN ))
740
+ {
741
+ REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
742
+ snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , "%d" , radio_sensor .value (param ));
743
+
744
+ REST .set_response_payload (response , (uint8_t * )buffer , strlen ((char * )buffer ));
745
+ }
746
+ else if (num && (accept [0 ]== REST .type .APPLICATION_JSON ))
747
+ {
748
+ REST .set_header_content_type (response , REST .type .APPLICATION_JSON );
749
+
750
+ if (param == RADIO_SENSOR_LAST_VALUE ) {
751
+ snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , "{'lqi':%d}" , radio_sensor .value (param ));
752
+ } else if (param == RADIO_SENSOR_LAST_PACKET ) {
753
+ snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , "{'rssi':%d}" , radio_sensor .value (param ));
754
+ }
755
+
756
+ REST .set_response_payload (response , buffer , strlen ((char * )buffer ));
757
+ }
758
+ else
759
+ {
760
+ REST .set_response_status (response , REST .status .UNSUPPORTED_MADIA_TYPE );
761
+ const char * msg = "Supporting content-types text/plain and application/json" ;
762
+ REST .set_response_payload (response , msg , strlen (msg ));
763
+ }
764
+ } else {
765
+ REST .set_response_status (response , REST .status .BAD_REQUEST );
766
+ }
767
+ }
768
+ #endif
769
+
770
+
675
771
676
772
PROCESS (rest_server_example , "Erbium Example Server" );
677
773
AUTOSTART_PROCESSES (& rest_server_example );
@@ -726,6 +822,9 @@ PROCESS_THREAD(rest_server_example, ev, data)
726
822
#if defined (PLATFORM_HAS_BUTTON ) && (REST_RES_EVENT || (REST_RES_SEPARATE && WITH_COAP > 3 ))
727
823
SENSORS_ACTIVATE (button_sensor );
728
824
#endif
825
+ #if REST_RES_SUB
826
+ rest_activate_resource (& resource_sub );
827
+ #endif
729
828
#if defined (PLATFORM_HAS_LEDS )
730
829
#if REST_RES_LEDS
731
830
rest_activate_resource (& resource_leds );
@@ -742,6 +841,10 @@ PROCESS_THREAD(rest_server_example, ev, data)
742
841
SENSORS_ACTIVATE (battery_sensor );
743
842
rest_activate_resource (& resource_battery );
744
843
#endif
844
+ #if defined (PLATFORM_HAS_RADIO ) && REST_RES_RADIO
845
+ SENSORS_ACTIVATE (radio_sensor );
846
+ rest_activate_resource (& resource_radio );
847
+ #endif
745
848
746
849
/* Define application-specific events here. */
747
850
while (1 ) {
0 commit comments