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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \defgroup Generic CoAP Resource Handler
|
|
|
|
*
|
|
|
|
* This factors the boilerplate code necessary for defining a resource
|
|
|
|
* together with the necessary handler. Currently this supports
|
|
|
|
* text/plain and application/json and outputs the resource with its
|
|
|
|
* name in json format. This may change in the future as more CoRE
|
|
|
|
* standards (e.g. see RFC 6690) get defined.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Generic CoAP Resource Handler
|
|
|
|
* \author
|
|
|
|
* Ralf Schlatterbeck <rsc@runtux.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define STR__(s) #s
|
|
|
|
#define STR_(s) STR__(s)
|
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
#define MAX_GET_STRING_LENGTH 100
|
|
|
|
#define MAX_URI_STRING_LENGTH 30
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
/*
|
|
|
|
* A macro that extends the resource definition and also sets up the
|
|
|
|
* necessary handler function that calls format/parse routines that
|
|
|
|
* convert from/to string (fs and ts). The ts function needs to return
|
|
|
|
* the length written, similar to sprintf.
|
|
|
|
* The content type definitions ct="0 5" are text/plain and
|
|
|
|
* application/json, respectively, see rfc7252 Table 9 p. 91.
|
|
|
|
* Yes, this *is* a hack. But I hate boilerplate code.
|
|
|
|
*/
|
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
#define GENERIC_RESOURCE(name, title, unit, is_str, fs, ts) \
|
2015-01-21 15:09:31 +01:00
|
|
|
static void name##_get_handler \
|
2014-06-27 22:25:51 +02:00
|
|
|
( void *request \
|
|
|
|
, void *response \
|
|
|
|
, uint8_t *buffer \
|
|
|
|
, uint16_t ps \
|
|
|
|
, int32_t *offset \
|
|
|
|
) \
|
|
|
|
{ \
|
2015-01-21 15:09:31 +01:00
|
|
|
generic_get_handler \
|
2016-02-26 07:54:05 +01:00
|
|
|
(request, response, buffer, ps, offset, STR_(name), is_str, ts); \
|
2015-01-21 15:09:31 +01:00
|
|
|
} \
|
|
|
|
static void name##_put_handler \
|
|
|
|
( void *request \
|
|
|
|
, void *response \
|
|
|
|
, uint8_t *buffer \
|
|
|
|
, uint16_t ps \
|
|
|
|
, int32_t *offset \
|
|
|
|
) \
|
|
|
|
{ \
|
|
|
|
generic_put_handler \
|
|
|
|
(request, response, buffer, ps, offset, STR_(name), fs); \
|
2014-06-27 22:25:51 +02:00
|
|
|
} \
|
|
|
|
\
|
2015-01-21 15:09:31 +01:00
|
|
|
RESOURCE ( res_##name \
|
2014-06-27 22:25:51 +02:00
|
|
|
, "title=\"" STR_(title) "\"" \
|
|
|
|
";rt=UCUM:\"" STR_(unit) "\"" \
|
|
|
|
";ct=\"0 5\"" \
|
2015-01-21 15:09:31 +01:00
|
|
|
, (ts) ? name##_get_handler : NULL \
|
|
|
|
, NULL /* POST */ \
|
|
|
|
, (fs) ? name##_put_handler : NULL \
|
|
|
|
, NULL /* DELETE */ \
|
2014-06-27 22:25:51 +02:00
|
|
|
)
|
|
|
|
|
2015-01-21 15:09:31 +01:00
|
|
|
/* Ignore constant pointer tests above */
|
|
|
|
#pragma GCC diagnostic ignored "-Waddress"
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
/**
|
|
|
|
* \brief Parse a resource in json format
|
|
|
|
* \param bytes: Input string received via coap
|
|
|
|
* \param len: Length of input string
|
|
|
|
* \param name: Name to search in json input
|
|
|
|
* \param buf: Output buffer
|
|
|
|
* \param buflen: Output buffer length
|
|
|
|
* \return 0 for success, -1 for error
|
|
|
|
*
|
|
|
|
* If compiled with DEBUG also prints the error encountered
|
|
|
|
*/
|
|
|
|
extern int8_t json_parse_variable
|
|
|
|
(const uint8_t *bytes, size_t len, char *name, char *buf, size_t buflen);
|
|
|
|
|
|
|
|
/**
|
2015-01-21 15:09:31 +01:00
|
|
|
* \brief Generic coap GET resource handler
|
2014-06-27 22:25:51 +02:00
|
|
|
* \param name: The name of the variable in json
|
|
|
|
* \param to_str: Application method to format value for output;
|
|
|
|
* the function may chose to format differently for coap or text
|
|
|
|
* The other parameters are the same as a normal resource handler
|
|
|
|
* This helps avoid boilerplate code for request handlers
|
|
|
|
*
|
|
|
|
* The callback functions get the name of the parameter as a first
|
|
|
|
* argument, this allows to re-use the same function for different
|
2015-01-21 15:09:31 +01:00
|
|
|
* parameters.
|
2014-06-27 22:25:51 +02:00
|
|
|
* For the to_str function the is_json flag allows to generate a
|
|
|
|
* different string depending on the content-type. In addition it gets a
|
|
|
|
* buffer and the size of the buffer. It needs to return the number of
|
|
|
|
* bytes output, similar to sprintf.
|
|
|
|
*/
|
2015-01-21 15:09:31 +01:00
|
|
|
extern 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
|
|
|
|
, size_t (*to_str)(const char *name, const char *uri, char *buf, size_t bsize)
|
2014-06-27 22:25:51 +02:00
|
|
|
);
|
|
|
|
|
2015-01-21 15:09:31 +01:00
|
|
|
/**
|
|
|
|
* \brief Generic coap PUT resource handler
|
|
|
|
* \param name: The name of the variable in json
|
|
|
|
* \param from_str: Application method to parse value from string
|
|
|
|
* and act on it, may be NULL in which case the resource only
|
|
|
|
* supports GET not PUT
|
|
|
|
* The other parameters are the same as a normal resource handler
|
|
|
|
* This helps avoid boilerplate code for request handlers
|
|
|
|
*
|
|
|
|
* The callback functions get the name of the parameter as a first
|
|
|
|
* argument, this allows to re-use the same function for different
|
|
|
|
* parameters. The from_str in addition gets the string to parse.
|
|
|
|
*/
|
|
|
|
extern void generic_put_handler
|
|
|
|
( 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 (*from_str)(const char *name, const char *uri, const char *s)
|
2015-01-21 15:09:31 +01:00
|
|
|
);
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
/*
|
|
|
|
* VI settings, see coding style
|
|
|
|
* ex:ts=8:et:sw=2
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|