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
@@ -533,6 +534,36 @@ event_event_handler(resource_t *r)
533
534
}
534
535
#endif /* PLATFORM_HAS_BUTTON */
535
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
+
536
567
/******************************************************************************/
537
568
#if defined (PLATFORM_HAS_LEDS )
538
569
/******************************************************************************/
@@ -613,23 +644,23 @@ light_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
613
644
if ((num == 0 ) || (num && accept [0 ]== REST .type .TEXT_PLAIN ))
614
645
{
615
646
REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
616
- 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 );
617
648
618
- REST .set_response_payload (response , (uint8_t * )buffer , strlen (buffer ));
649
+ REST .set_response_payload (response , (uint8_t * )buffer , strlen (( char * ) buffer ));
619
650
}
620
651
else if (num && (accept [0 ]== REST .type .APPLICATION_XML ))
621
652
{
622
653
REST .set_header_content_type (response , REST .type .APPLICATION_XML );
623
- 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 );
624
655
625
- REST .set_response_payload (response , buffer , strlen (buffer ));
656
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
626
657
}
627
658
else if (num && (accept [0 ]== REST .type .APPLICATION_JSON ))
628
659
{
629
660
REST .set_header_content_type (response , REST .type .APPLICATION_JSON );
630
- 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 );
631
662
632
- REST .set_response_payload (response , buffer , strlen (buffer ));
663
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
633
664
}
634
665
else
635
666
{
@@ -655,16 +686,16 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
655
686
if ((num == 0 ) || (num && accept [0 ]== REST .type .TEXT_PLAIN ))
656
687
{
657
688
REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
658
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "%d" , battery );
689
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "%d" , battery );
659
690
660
- REST .set_response_payload (response , (uint8_t * )buffer , strlen (buffer ));
691
+ REST .set_response_payload (response , (uint8_t * )buffer , strlen (( char * ) buffer ));
661
692
}
662
693
else if (num && (accept [0 ]== REST .type .APPLICATION_JSON ))
663
694
{
664
695
REST .set_header_content_type (response , REST .type .APPLICATION_JSON );
665
- snprintf (buffer , REST_MAX_CHUNK_SIZE , "{'battery':%d}" , battery );
696
+ snprintf (( char * ) buffer , REST_MAX_CHUNK_SIZE , "{'battery':%d}" , battery );
666
697
667
- REST .set_response_payload (response , buffer , strlen (buffer ));
698
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
668
699
}
669
700
else
670
701
{
@@ -710,7 +741,7 @@ radio_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
710
741
REST .set_header_content_type (response , REST .type .TEXT_PLAIN );
711
742
snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , "%d" , radio_sensor .value (param ));
712
743
713
- REST .set_response_payload (response , (uint8_t * )buffer , strlen (buffer ));
744
+ REST .set_response_payload (response , (uint8_t * )buffer , strlen (( char * ) buffer ));
714
745
}
715
746
else if (num && (accept [0 ]== REST .type .APPLICATION_JSON ))
716
747
{
@@ -722,7 +753,7 @@ radio_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
722
753
snprintf ((char * )buffer , REST_MAX_CHUNK_SIZE , "{'rssi':%d}" , radio_sensor .value (param ));
723
754
}
724
755
725
- REST .set_response_payload (response , buffer , strlen (buffer ));
756
+ REST .set_response_payload (response , buffer , strlen (( char * ) buffer ));
726
757
}
727
758
else
728
759
{
@@ -791,6 +822,9 @@ PROCESS_THREAD(rest_server_example, ev, data)
791
822
#if defined (PLATFORM_HAS_BUTTON ) && (REST_RES_EVENT || (REST_RES_SEPARATE && WITH_COAP > 3 ))
792
823
SENSORS_ACTIVATE (button_sensor );
793
824
#endif
825
+ #if REST_RES_SUB
826
+ rest_activate_resource (& resource_sub );
827
+ #endif
794
828
#if defined (PLATFORM_HAS_LEDS )
795
829
#if REST_RES_LEDS
796
830
rest_activate_resource (& resource_leds );
0 commit comments