Refactor GENERIC_RESOURCE macro
Now callback functions get the URI of the request, this allows to use a single resource for multiple different URIs. The is_json flag is now gone for the to-string function, instead the macro has an is_str flag. If set this automagically produces quotes around the string for json output. Now from-string functions can return an error-code, 0 for success, -1 for error.
This commit is contained in:
parent
c7daa7c45d
commit
c6165a3bcf
9 changed files with 106 additions and 68 deletions
|
@ -21,32 +21,30 @@
|
|||
#include "er-coap.h"
|
||||
#include "generic_resource.h"
|
||||
|
||||
void timestamp_from_string (const char *name, const char *s)
|
||||
int timestamp_from_string (const char *name, const char *uri, const char *s)
|
||||
{
|
||||
struct timeval tv;
|
||||
// FIXME: Platform has no strtoll (long long)?
|
||||
tv.tv_sec = strtol (s, NULL, 10);
|
||||
settimeofday (&tv, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t
|
||||
timestamp_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
|
||||
timestamp_to_string (const char *name, const char *uri, char *buf, size_t bsize)
|
||||
{
|
||||
struct timeval tv;
|
||||
char *fmt = "%ld";
|
||||
if (is_json) {
|
||||
fmt = "\"%ld\"";
|
||||
}
|
||||
gettimeofday (&tv, NULL);
|
||||
// FIXME: Platform doesn't seem to support long long printing
|
||||
// We get empty string
|
||||
return snprintf (buf, bsize, fmt, (long)tv.tv_sec);
|
||||
return snprintf (buf, bsize, "%ld", (long)tv.tv_sec);
|
||||
}
|
||||
|
||||
GENERIC_RESOURCE
|
||||
( timestamp
|
||||
, Time
|
||||
, s
|
||||
, 1
|
||||
, timestamp_from_string
|
||||
, timestamp_to_string
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue