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
@ -60,8 +60,8 @@
* Yes, this *is* a hack. But I hate boilerplate code.
*/
#define GENERIC_RESOURCE(name, methods, path, title, unit, fs, ts) \
void name##_handler \
#define GENERIC_RESOURCE(name, title, unit, fs, ts) \
static void name##_get_handler \
( void *request \
, void *response \
, uint8_t *buffer \
@ -69,16 +69,34 @@
, int32_t *offset \
) \
{ \
generic_handler \
(request, response, buffer, ps, offset, STR_(name), fs, ts); \
generic_get_handler \
(request, response, buffer, ps, offset, STR_(name), ts); \
} \
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); \
} \
\
RESOURCE ( name, methods, path \
RESOURCE ( res_##name \
, "title=\"" STR_(title) "\"" \
";rt=UCUM:\"" STR_(unit) "\"" \
";ct=\"0 5\"" \
, (ts) ? name##_get_handler : NULL \
, NULL /* POST */ \
, (fs) ? name##_put_handler : NULL \
, NULL /* DELETE */ \
)
/* Ignore constant pointer tests above */
#pragma GCC diagnostic ignored "-Waddress"
/**
* \brief Parse a resource in json format
* \param bytes: Input string received via coap
@ -94,11 +112,8 @@ extern int8_t json_parse_variable
(const uint8_t *bytes, size_t len, char *name, char *buf, size_t buflen);
/**
* \brief Generic coap resource handler
* \brief Generic coap GET 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
* \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
@ -106,13 +121,36 @@ extern int8_t json_parse_variable
*
* 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.
* parameters.
* 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.
*/
extern void generic_handler
extern 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)
);
/**
* \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
@ -120,7 +158,6 @@ extern 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)
);
/*