/** * \file * Resource for Arduino PWM * \author * Ralf Schlatterbeck * * \brief get/put pwm and period for LED pin * * quick&dirty implementation, this should factor json parsing. * But json format will probably change, there is a draft rfc. */ #include #include #include #include "contiki.h" #include "jsonparse.h" /* Only coap 13 for now */ #include "er-coap-13.h" #include "led_pwm.h" /* Error-handling macro */ # define BYE(_exp, _tag) \ do { \ PRINTF("Expect "_exp": %d\n",_tag); \ success=0; \ goto bye; \ } while(0) #define DEBUG 1 #if DEBUG #define PRINTF(...) printf(__VA_ARGS__) #else #define PRINTF(...) #endif RESOURCE \ ( led_pwm, METHOD_GET | METHOD_PUT , "led/pwm" , "title=\"LED PWM\";rt=\"led pwm\"" ); void led_pwm_handler ( void* request , void* response , uint8_t *buffer , uint16_t preferred_size , int32_t *offset ) { int success = 1; int i; char temp[100]; int index = 0; int length = 0; int tag = 0; const uint8_t *bytes = NULL; size_t len = 0; int n_acc = 0; const uint16_t *accept = NULL; uint16_t a_ctype = REST.type.APPLICATION_JSON; uint16_t c_ctype = REST.get_header_content_type (request); uint32_t tmp = 0; /* Seems like accepted type is currently unsupported? */ n_acc = REST.get_header_accept (request, &accept); for (i=0; i 255) { tmp = 255; } pwm = tmp; PRINTF ("Setting: %d\n", pwm); REST.set_response_status(response, REST.status.CHANGED); } else { PRINTF ("PUT: len: %d\n", len); success = 0; } bye : break; default: success = 0; } if (!success) { REST.set_response_status(response, REST.status.BAD_REQUEST); } } RESOURCE \ ( led_period, METHOD_GET | METHOD_PUT , "led/period" , "title=\"LED Period\";rt=\"led period\"" ); void led_period_handler ( void* request , void* response , uint8_t *buffer , uint16_t preferred_size , int32_t *offset ) { int success = 1; int i; char temp[100]; int index = 0; int length = 0; int tag = 0; const uint8_t *bytes = NULL; size_t len = 0; int n_acc = 0; const uint16_t *accept = NULL; uint16_t a_ctype = REST.type.APPLICATION_JSON; uint16_t c_ctype = REST.get_header_content_type (request); uint32_t tmp = 0; /* Seems like accepted type is currently unsupported? */ n_acc = REST.get_header_accept (request, &accept); for (i=0; i 10) { tmp = 10; } if (tmp == 0) { tmp = 1; } period_100ms = tmp; PRINTF ("Setting: %dms\n", period_100ms * 100); REST.set_response_status(response, REST.status.CHANGED); } else { PRINTF ("PUT: len: %d\n", len); success = 0; } bye : break; default: success = 0; } if (!success) { REST.set_response_status(response, REST.status.BAD_REQUEST); } }