Factor resources, fix time
Now there is a generic resource that can generate and parse application/json as well as text/plain. It can be re-used, only the from_string and to_string routines have to be written and the resource properly set up. A new resource format is specified, see GENERIC_RESOURCE in, e.g., examples/osd/pwm-example. This is now used in all my examples, namely pwm-example, arduino-sketch, wallclock-time. There was an off by one error for the month in time formatting (in gmtime and localtime). And the leap-year computation was broken. Both fixed now, so we get a correct date. For localtime we are still 2 hours off because daylight saving isn't implemented yet. Also renamed gmtime to utc.
This commit is contained in:
parent
4f20df042f
commit
d5284eebe1
12 changed files with 488 additions and 612 deletions
|
@ -68,7 +68,7 @@ CFLAGS += -DUIP_CONF_TCP=1
|
|||
APPS += er-http-engine
|
||||
endif
|
||||
|
||||
APPS += erbium time json arduino
|
||||
APPS += erbium time json arduino json-resource
|
||||
|
||||
include $(CONTIKI)/Makefile.include
|
||||
include $(CONTIKI)/apps/arduino/Makefile.include
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
*
|
||||
* \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 <stdio.h>
|
||||
|
@ -17,270 +15,61 @@
|
|||
#include "jsonparse.h"
|
||||
/* Only coap 13 for now */
|
||||
#include "er-coap-13.h"
|
||||
#include "generic_resource.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)
|
||||
void pwm_from_string (const char *name, const char *s)
|
||||
{
|
||||
uint32_t tmp = strtoul (s, NULL, 10);
|
||||
if (tmp > 255) {
|
||||
tmp = 255;
|
||||
}
|
||||
pwm = tmp;
|
||||
}
|
||||
|
||||
#define DEBUG 1
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
size_t
|
||||
pwm_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
|
||||
{
|
||||
return snprintf (buf, bufsize, "%d", pwm);
|
||||
}
|
||||
|
||||
RESOURCE \
|
||||
GENERIC_RESOURCE \
|
||||
( led_pwm, METHOD_GET | METHOD_PUT
|
||||
, "led/pwm"
|
||||
, "title=\"LED PWM\";rt=\"led pwm\""
|
||||
, LED PWM
|
||||
, duty-cycle
|
||||
, pwm_from_string
|
||||
, pwm_to_string
|
||||
);
|
||||
|
||||
void
|
||||
led_pwm_handler
|
||||
( void* request
|
||||
, void* response
|
||||
, uint8_t *buffer
|
||||
, uint16_t preferred_size
|
||||
, int32_t *offset
|
||||
)
|
||||
void period_from_string (const char *name, const char *s)
|
||||
{
|
||||
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<n_acc; i++) {
|
||||
if ( accept [i] == REST.type.TEXT_PLAIN
|
||||
|| accept [i] == REST.type.APPLICATION_JSON
|
||||
)
|
||||
{
|
||||
a_ctype = accept [i];
|
||||
break;
|
||||
uint32_t tmp = (strtoul (s, NULL, 10) + 50) / 100;
|
||||
if (tmp > 10) {
|
||||
tmp = 10;
|
||||
}
|
||||
}
|
||||
|
||||
switch(REST.get_method_type(request)) {
|
||||
case METHOD_GET:
|
||||
// TEXT format
|
||||
if (a_ctype == REST.type.TEXT_PLAIN) {
|
||||
index += sprintf (temp + index, "%d\n", pwm);
|
||||
} else { // jSON Format
|
||||
index += sprintf
|
||||
( temp + index
|
||||
,"{\n \"pwm\" : \"%d\"\n}\n"
|
||||
, pwm
|
||||
);
|
||||
}
|
||||
length = strlen(temp);
|
||||
memcpy (buffer, temp, length);
|
||||
REST.set_header_content_type (response, a_ctype);
|
||||
REST.set_response_payload (response, buffer, length);
|
||||
|
||||
break;
|
||||
case METHOD_PUT:
|
||||
if ((len = coap_get_payload(request, &bytes))) {
|
||||
PRINTF ("PUT: len: %d, %s\n", len, (char *)bytes);
|
||||
if (c_ctype == REST.type.TEXT_PLAIN) {
|
||||
temp [sizeof (temp) - 1] = 0;
|
||||
strncpy (temp, (char *)bytes, MIN (len + 1, sizeof (temp) - 1));
|
||||
} else { // jSON Format
|
||||
struct jsonparse_state state;
|
||||
struct jsonparse_state *parser = &state;
|
||||
jsonparse_setup (parser, (char *)bytes, len);
|
||||
if ((tag = jsonparse_next (parser)) != JSON_TYPE_OBJECT) {
|
||||
BYE ("OBJECT", tag);
|
||||
}
|
||||
if ((tag = jsonparse_next (parser)) != JSON_TYPE_PAIR_NAME) {
|
||||
BYE ("PAIR_NAME", tag);
|
||||
}
|
||||
while (jsonparse_strcmp_value (parser, "pwm") != 0) {
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_PAIR) {
|
||||
BYE ("PAIR", tag);
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != ',') {
|
||||
BYE (",", tag);
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_PAIR_NAME) {
|
||||
BYE ("PAIR_NAME", tag);
|
||||
}
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_PAIR) {
|
||||
BYE ("PAIR", tag);
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_STRING) {
|
||||
BYE ("STRING", tag);
|
||||
}
|
||||
jsonparse_copy_value (parser, temp, sizeof (temp));
|
||||
temp [sizeof (temp) - 1] = 0;
|
||||
}
|
||||
PRINTF ("GOT: %s\n", temp);
|
||||
tmp = strtoul (temp, NULL, 10);
|
||||
if (tmp > 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);
|
||||
}
|
||||
if (tmp == 0) {
|
||||
tmp = 1;
|
||||
}
|
||||
period_100ms = tmp;
|
||||
}
|
||||
|
||||
RESOURCE \
|
||||
size_t
|
||||
period_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
|
||||
{
|
||||
return snprintf (buf, bufsize, "%d", period_100ms * 100);
|
||||
}
|
||||
|
||||
GENERIC_RESOURCE \
|
||||
( led_period, METHOD_GET | METHOD_PUT
|
||||
, "led/period"
|
||||
, "title=\"LED Period\";rt=\"led period\""
|
||||
, LED Period
|
||||
, ms
|
||||
, period_from_string
|
||||
, period_to_string
|
||||
);
|
||||
|
||||
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<n_acc; i++) {
|
||||
if ( accept [i] == REST.type.TEXT_PLAIN
|
||||
|| accept [i] == REST.type.APPLICATION_JSON
|
||||
)
|
||||
{
|
||||
a_ctype = accept [i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch(REST.get_method_type(request)) {
|
||||
case METHOD_GET:
|
||||
// TEXT format
|
||||
if (a_ctype == REST.type.TEXT_PLAIN) {
|
||||
index += sprintf (temp + index, "%d\n", period_100ms * 100);
|
||||
} else { // jSON Format
|
||||
index += sprintf
|
||||
( temp + index
|
||||
,"{\n \"period\" : \"%d\"\n}\n"
|
||||
, period_100ms * 100
|
||||
);
|
||||
}
|
||||
length = strlen(temp);
|
||||
memcpy (buffer, temp, length);
|
||||
REST.set_header_content_type (response, a_ctype);
|
||||
REST.set_response_payload (response, buffer, length);
|
||||
|
||||
break;
|
||||
case METHOD_PUT:
|
||||
if ((len = coap_get_payload(request, &bytes))) {
|
||||
PRINTF ("PUT: len: %d, %s\n", len, (char *)bytes);
|
||||
if (c_ctype == REST.type.TEXT_PLAIN) {
|
||||
temp [sizeof (temp) - 1] = 0;
|
||||
strncpy (temp, (char *)bytes, MIN (len + 1, sizeof (temp) - 1));
|
||||
} else { // jSON Format
|
||||
struct jsonparse_state state;
|
||||
struct jsonparse_state *parser = &state;
|
||||
jsonparse_setup (parser, (char *)bytes, len);
|
||||
if ((tag = jsonparse_next (parser)) != JSON_TYPE_OBJECT) {
|
||||
BYE ("OBJECT", tag);
|
||||
}
|
||||
if ((tag = jsonparse_next (parser)) != JSON_TYPE_PAIR_NAME) {
|
||||
BYE ("PAIR_NAME", tag);
|
||||
}
|
||||
while (jsonparse_strcmp_value (parser, "period") != 0) {
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_PAIR) {
|
||||
BYE ("PAIR", tag);
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != ',') {
|
||||
BYE (",", tag);
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_PAIR_NAME) {
|
||||
BYE ("PAIR_NAME", tag);
|
||||
}
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_PAIR) {
|
||||
BYE ("PAIR", tag);
|
||||
}
|
||||
tag = jsonparse_next (parser);
|
||||
if (tag != JSON_TYPE_STRING) {
|
||||
BYE ("STRING", tag);
|
||||
}
|
||||
jsonparse_copy_value (parser, temp, sizeof (temp));
|
||||
temp [sizeof (temp) - 1] = 0;
|
||||
}
|
||||
PRINTF ("GOT: %s\n", temp);
|
||||
tmp = (strtoul (temp, NULL, 10) + 50) / 100;
|
||||
if (tmp > 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);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* VI settings, see coding style
|
||||
* ex:ts=8:et:sw=2
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue