Factor/Fix generic resources

Now the old GENERIC_RESOURCE macros works again (but usage has changed).
Common resources (battery, leds, radio) are now in resources-common.
This commit is contained in:
Ralf Schlatterbeck 2015-01-21 15:09:31 +01:00
parent 76e39b2b5d
commit baaa2c5741
59 changed files with 440 additions and 2705 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Ralf Schlatterbeck Open Source Consulting
* Copyright (c) 2014-15, Ralf Schlatterbeck Open Source Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -105,7 +105,72 @@ json_parse_variable
return 0;
}
void generic_handler
void generic_get_handler
( void *request
, void *response
, uint8_t *buffer
, uint16_t preferred_size
, int32_t *offset
, char *name
, size_t (*to_str)(const char *name, uint8_t is_json, char *buf, size_t bsize)
)
{
int success = 1;
char temp [100];
size_t len = 0;
unsigned int accept = -1;
REST.get_header_accept (request, &accept);
if ( accept != -1
&& accept != REST.type.TEXT_PLAIN
&& accept != REST.type.APPLICATION_JSON
)
{
success = 0;
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
return;
}
// TEXT format
if (accept == REST.type.APPLICATION_JSON) {
len += snprintf
(temp + len, sizeof (temp) - len, "{\n \"%s\" : ", name);
if (len > sizeof (temp)) {
success = 0;
goto out;
}
len += to_str (name, 1, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
goto out;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n}\n");
if (len > sizeof (temp)) {
success = 0;
goto out;
}
} else { // TEXT Format
len += to_str (name, 0, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
goto out;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n");
if (len > sizeof (temp)) {
success = 0;
goto out;
}
}
memcpy (buffer, temp, len);
REST.set_header_content_type (response, accept);
REST.set_response_payload (response, buffer, len);
out :
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
void generic_put_handler
( void *request
, void *response
, uint8_t *buffer
@ -113,88 +178,31 @@ void generic_handler
, int32_t *offset
, char *name
, void (*from_str)(const char *name, const char *s)
, size_t (*to_str)(const char *name, uint8_t is_json, char *buf, size_t bsize)
)
{
int success = 1;
char temp [100];
int i = 0;
size_t len = 0;
int n_acc = 0;
const uint8_t *bytes = NULL;
const uint16_t *accept = NULL;
uint16_t a_ctype = REST.type.APPLICATION_JSON;
uint16_t c_ctype;
n_acc = REST.get_header_content_type (request, &c_ctype);
REST.get_header_content_type (request, &c_ctype);
/* 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) {
len += to_str (name, 0, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
break;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n");
if (len > sizeof (temp)) {
success = 0;
break;
}
} else { // jSON Format
len += snprintf
(temp + len, sizeof (temp) - len, "{\n \"%s\" : ", name);
if (len > sizeof (temp)) {
success = 0;
break;
}
len += to_str (name, 1, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
break;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n}\n");
if (len > sizeof (temp)) {
success = 0;
break;
}
}
memcpy (buffer, temp, len);
REST.set_header_content_type (response, a_ctype);
REST.set_response_payload (response, buffer, len);
break;
case METHOD_PUT:
if (from_str && (len = coap_get_payload(request, &bytes))) {
if (c_ctype == REST.type.TEXT_PLAIN) {
temp [sizeof (temp) - 1] = 0;
strncpy (temp, (const char *)bytes, MIN (len, sizeof (temp) - 1));
} else { // jSON Format
if (json_parse_variable (bytes, len, name, temp, sizeof (temp)) < 0) {
success = 0;
break;
}
}
from_str (name, temp);
REST.set_response_status(response, REST.status.CHANGED);
} else {
if (from_str && (len = coap_get_payload(request, &bytes))) {
if (c_ctype == REST.type.TEXT_PLAIN) {
temp [sizeof (temp) - 1] = 0;
strncpy (temp, (const char *)bytes, MIN (len, sizeof (temp) - 1));
} else { // jSON Format
if (json_parse_variable (bytes, len, name, temp, sizeof (temp)) < 0) {
success = 0;
goto out;
}
break;
default:
success = 0;
}
from_str (name, temp);
REST.set_response_status(response, REST.status.CHANGED);
} else {
success = 0;
}
out:
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}