2014-06-27 22:25:51 +02:00
|
|
|
/*
|
2015-01-21 15:09:31 +01:00
|
|
|
* Copyright (c) 2014-15, Ralf Schlatterbeck Open Source Consulting
|
2014-06-27 22:25:51 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \addtogroup Generic CoAP Resource Handler
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Generic CoAP Resource Handler
|
|
|
|
* \author
|
|
|
|
* Ralf Schlatterbeck <rsc@runtux.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "jsonparse.h"
|
2014-12-29 08:16:47 +01:00
|
|
|
#include "er-coap.h"
|
2014-06-27 22:25:51 +02:00
|
|
|
#include "generic_resource.h"
|
|
|
|
|
|
|
|
/* Error-handling macro */
|
|
|
|
# define BYE(_exp, _tag) \
|
|
|
|
do { \
|
|
|
|
PRINTF("Expect "_exp": %d\n",_tag); \
|
|
|
|
return -1; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define DEBUG 1
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
json_parse_variable
|
|
|
|
(const uint8_t *bytes, size_t len, char *name, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int tag = 0;
|
|
|
|
struct jsonparse_state state;
|
|
|
|
struct jsonparse_state *parser = &state;
|
|
|
|
PRINTF ("PUT: len: %d, %s\n", len, (const char *)bytes);
|
|
|
|
jsonparse_setup (parser, (const 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, name) != 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, buf, buflen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
static const char *get_uri (void *request)
|
|
|
|
{
|
|
|
|
static char buf [MAX_URI_STRING_LENGTH];
|
|
|
|
const char *uri;
|
|
|
|
size_t len = coap_get_header_uri_path (request, &uri);
|
|
|
|
if (len > sizeof (buf) - 1) {
|
|
|
|
*buf = '\0';
|
|
|
|
} else {
|
|
|
|
strncpy (buf, uri, len);
|
|
|
|
buf [len] = '\0';
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-08-20 15:01:30 +02:00
|
|
|
static const char *get_query (void *request)
|
|
|
|
{
|
|
|
|
static char buf [MAX_QUERY_STRING_LENGTH];
|
|
|
|
const char *query;
|
|
|
|
size_t len = coap_get_header_uri_query (request, &query);
|
|
|
|
if (len > sizeof (buf) - 1) {
|
|
|
|
*buf = '\0';
|
|
|
|
} else {
|
|
|
|
strncpy (buf, query, len);
|
|
|
|
buf [len] = '\0';
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-01-21 15:09:31 +01:00
|
|
|
void generic_get_handler
|
2014-06-27 22:25:51 +02:00
|
|
|
( void *request
|
|
|
|
, void *response
|
|
|
|
, uint8_t *buffer
|
|
|
|
, uint16_t preferred_size
|
|
|
|
, int32_t *offset
|
|
|
|
, char *name
|
2016-02-26 07:54:05 +01:00
|
|
|
, int is_str
|
2017-08-20 15:01:30 +02:00
|
|
|
, size_t (*to_str)
|
|
|
|
( const char *name
|
|
|
|
, const char *uri
|
|
|
|
, const char *query
|
|
|
|
, char *buf, size_t bsize
|
|
|
|
)
|
2014-06-27 22:25:51 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int success = 1;
|
2016-02-26 07:54:05 +01:00
|
|
|
char temp [MAX_GET_STRING_LENGTH];
|
2014-06-27 22:25:51 +02:00
|
|
|
size_t len = 0;
|
2015-01-21 15:09:31 +01:00
|
|
|
unsigned int accept = -1;
|
2017-08-20 15:01:30 +02:00
|
|
|
const char *uri = get_uri (request);
|
|
|
|
const char *query = get_query (request);
|
2015-01-21 15:09:31 +01:00
|
|
|
|
|
|
|
REST.get_header_accept (request, &accept);
|
|
|
|
if ( accept != -1
|
|
|
|
&& accept != REST.type.TEXT_PLAIN
|
|
|
|
&& accept != REST.type.APPLICATION_JSON
|
|
|
|
)
|
|
|
|
{
|
|
|
|
success = 0;
|
2016-02-26 07:54:05 +01:00
|
|
|
REST.set_response_status (response, REST.status.NOT_ACCEPTABLE);
|
2015-01-21 15:09:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-06-27 22:25:51 +02:00
|
|
|
|
2015-01-21 15:09:31 +01:00
|
|
|
// TEXT format
|
|
|
|
if (accept == REST.type.APPLICATION_JSON) {
|
|
|
|
len += snprintf
|
2016-02-26 07:54:05 +01:00
|
|
|
( temp + len
|
|
|
|
, sizeof (temp) - len
|
|
|
|
, "{\n \"%s\" : %s"
|
|
|
|
, name
|
|
|
|
, is_str ? "\"" : ""
|
|
|
|
);
|
2015-01-21 15:09:31 +01:00
|
|
|
if (len > sizeof (temp)) {
|
|
|
|
success = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-08-20 15:01:30 +02:00
|
|
|
len += to_str (name, uri, query, temp + len, sizeof (temp) - len);
|
2015-01-21 15:09:31 +01:00
|
|
|
if (len > sizeof (temp)) {
|
|
|
|
success = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-02-26 07:54:05 +01:00
|
|
|
len += snprintf
|
|
|
|
( temp + len
|
|
|
|
, sizeof (temp) - len
|
|
|
|
, "%s\n}\n"
|
|
|
|
, is_str ? "\"" : ""
|
|
|
|
);
|
2015-01-21 15:09:31 +01:00
|
|
|
if (len > sizeof (temp)) {
|
|
|
|
success = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else { // TEXT Format
|
2017-08-20 15:01:30 +02:00
|
|
|
len += to_str (name, uri, query, temp + len, sizeof (temp) - len);
|
2015-01-21 15:09:31 +01:00
|
|
|
if (len > sizeof (temp)) {
|
|
|
|
success = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
len += snprintf (temp + len, sizeof (temp) - len, "\n");
|
|
|
|
if (len > sizeof (temp)) {
|
|
|
|
success = 0;
|
|
|
|
goto out;
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 15:09:31 +01:00
|
|
|
memcpy (buffer, temp, len);
|
|
|
|
REST.set_header_content_type (response, accept);
|
|
|
|
REST.set_response_payload (response, buffer, len);
|
|
|
|
out :
|
|
|
|
if (!success) {
|
2016-02-26 07:54:05 +01:00
|
|
|
REST.set_response_status (response, REST.status.BAD_REQUEST);
|
2015-01-21 15:09:31 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 22:25:51 +02:00
|
|
|
|
2015-01-21 15:09:31 +01:00
|
|
|
void generic_put_handler
|
|
|
|
( void *request
|
|
|
|
, void *response
|
|
|
|
, uint8_t *buffer
|
|
|
|
, uint16_t preferred_size
|
|
|
|
, int32_t *offset
|
|
|
|
, char *name
|
2017-08-20 15:01:30 +02:00
|
|
|
, int (*from_str)
|
|
|
|
(const char *name, const char *uri, const char *query, const char *s)
|
2015-01-21 15:09:31 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int success = 1;
|
|
|
|
char temp [100];
|
|
|
|
size_t len = 0;
|
|
|
|
const uint8_t *bytes = NULL;
|
2016-03-28 17:25:30 +02:00
|
|
|
unsigned int c_ctype;
|
2016-02-26 07:54:05 +01:00
|
|
|
const char *uri = get_uri (request);
|
2017-08-20 15:01:30 +02:00
|
|
|
const char *query = get_query (request);
|
2015-01-21 15:09:31 +01:00
|
|
|
REST.get_header_content_type (request, &c_ctype);
|
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
if (from_str && (len = coap_get_payload (request, &bytes))) {
|
2015-01-21 15:09:31 +01:00
|
|
|
if (c_ctype == REST.type.TEXT_PLAIN) {
|
2015-01-27 10:43:03 +01:00
|
|
|
int l = MIN (len, sizeof (temp) - 1);
|
2015-01-21 15:09:31 +01:00
|
|
|
temp [sizeof (temp) - 1] = 0;
|
2015-01-27 10:43:03 +01:00
|
|
|
strncpy (temp, (const char *)bytes, l);
|
|
|
|
temp [l] = 0;
|
2015-01-21 15:09:31 +01:00
|
|
|
} else { // jSON Format
|
|
|
|
if (json_parse_variable (bytes, len, name, temp, sizeof (temp)) < 0) {
|
2014-06-27 22:25:51 +02:00
|
|
|
success = 0;
|
2015-01-21 15:09:31 +01:00
|
|
|
goto out;
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
2015-01-21 15:09:31 +01:00
|
|
|
}
|
2017-08-20 15:01:30 +02:00
|
|
|
if (from_str (name, uri, query, temp) < 0) {
|
2016-02-26 07:54:05 +01:00
|
|
|
success = 0;
|
|
|
|
} else {
|
|
|
|
REST.set_response_status (response, REST.status.CHANGED);
|
|
|
|
}
|
2015-01-21 15:09:31 +01:00
|
|
|
} else {
|
|
|
|
success = 0;
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
2015-01-21 15:09:31 +01:00
|
|
|
out:
|
2014-06-27 22:25:51 +02:00
|
|
|
if (!success) {
|
2016-02-26 07:54:05 +01:00
|
|
|
REST.set_response_status (response, REST.status.BAD_REQUEST);
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VI settings, see coding style
|
|
|
|
* ex:ts=8:et:sw=2
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|