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:
Ralf Schlatterbeck 2016-02-26 07:54:05 +01:00
parent c7daa7c45d
commit c6165a3bcf
9 changed files with 106 additions and 68 deletions

View file

@ -46,27 +46,25 @@ static uint8_t name_to_offset (const char * name)
}
static size_t
color_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
color_to_string (const char *name, const char *uri, char *buf, size_t bsize)
{
char *fmt = "%d";
if (is_json) {
fmt = "\"%d\"";
}
return snprintf (buf, bsize, fmt, color_rgb [name_to_offset (name)]);
return snprintf (buf, bsize, "%d", color_rgb [name_to_offset (name)]);
}
void color_from_string (const char *name, const char *s)
int color_from_string (const char *name, const char *uri, const char *s)
{
color_rgb [name_to_offset (name)] = atoi (s);
Driver.begin();
Driver.SetColor(color_rgb [0], color_rgb [1], color_rgb [2]);
Driver.end();
return 0;
}
GENERIC_RESOURCE
( red
, RED_LED
, s
, 1
, color_from_string
, color_to_string
);
@ -75,6 +73,7 @@ GENERIC_RESOURCE
( green
, GREEN_LED
, s
, 1
, color_from_string
, color_to_string
);
@ -83,6 +82,7 @@ GENERIC_RESOURCE
( blue
, BLUE_LED
, s
, 1
, color_from_string
, color_to_string
);
@ -136,4 +136,3 @@ void loop (void)
}
*/
}

View file

@ -17,17 +17,18 @@
#include "generic_resource.h"
#include "led_pwm.h"
void pwm_from_string (const char *name, const char *s)
int pwm_from_string (const char *name, const char *uri, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
if (tmp > 255) {
tmp = 255;
}
pwm = tmp;
return 0;
}
size_t
pwm_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
pwm_to_string (const char *name, const char *uri, char *buf, size_t bufsize)
{
return snprintf (buf, bufsize, "%d", pwm);
}
@ -36,11 +37,12 @@ GENERIC_RESOURCE \
( led_pwm
, LED PWM
, duty-cycle
, 0
, pwm_from_string
, pwm_to_string
);
void period_from_string (const char *name, const char *s)
int period_from_string (const char *name, const char *uri, const char *s)
{
uint32_t tmp = (strtoul (s, NULL, 10) + 50) / 100;
if (tmp > 10) {
@ -50,10 +52,11 @@ void period_from_string (const char *name, const char *s)
tmp = 1;
}
period_100ms = tmp;
return 0;
}
size_t
period_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
period_to_string (const char *name, const char *uri, char *buf, size_t bufsize)
{
return snprintf (buf, bufsize, "%d", period_100ms * 100);
}
@ -62,12 +65,13 @@ GENERIC_RESOURCE \
( led_period
, LED Period
, ms
, 0
, period_from_string
, period_to_string
);
size_t
analog2_v (const char *name, uint8_t is_json, char *buf, size_t bufsize)
analog2_v (const char *name, const char *uri, char *buf, size_t bufsize)
{
return snprintf
(buf, bufsize, "%d.%03d", analog2_voltage / 1000, analog2_voltage % 1000);
@ -77,12 +81,13 @@ GENERIC_RESOURCE \
( analog2_voltage
, Analog 2 voltage
, V
, 0
, NULL
, analog2_v
);
size_t
analog5_v (const char *name, uint8_t is_json, char *buf, size_t bufsize)
analog5_v (const char *name, const char *uri, char *buf, size_t bufsize)
{
return snprintf
(buf, bufsize, "%d.%03d", analog5_voltage / 1000, analog5_voltage % 1000);
@ -92,6 +97,7 @@ GENERIC_RESOURCE \
( analog5_voltage
, Analog 5 voltage
, V
, 0
, NULL
, analog5_v
);

View file

@ -77,16 +77,12 @@ static uint8_t name_to_offset (const char * name)
}
static size_t
color_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
color_to_string (const char *name, const char *uri, char *buf, size_t bsize)
{
char *fmt = "%d";
if (is_json) {
fmt = "\"%d\"";
}
return snprintf (buf, bsize, fmt, color_rgb [name_to_offset (name)]);
return snprintf (buf, bsize, "%d", color_rgb [name_to_offset (name)]);
}
void color_from_string (const char *name, const char *s)
int color_from_string (const char *name, const char *uri, const char *s)
{
color_rgb [name_to_offset (name)] = atoi (s);
led_strip_begin ();
@ -95,12 +91,14 @@ void color_from_string (const char *name, const char *s)
printf ("Set to R:%d G:%d B:%d\n"
, color_rgb [0], color_rgb [1], color_rgb [2])
;
return 0;
}
GENERIC_RESOURCE
( red
, RED_LED
, s
, 1
, color_from_string
, color_to_string
);
@ -109,6 +107,7 @@ GENERIC_RESOURCE
( green
, GREEN_LED
, s
, 1
, color_from_string
, color_to_string
);
@ -117,6 +116,7 @@ GENERIC_RESOURCE
( blue
, BLUE_LED
, s
, 1
, color_from_string
, color_to_string
);

View file

@ -77,74 +77,73 @@ char server_resource [20] = "led/G";
int interval = 10; /* Retransmit interval after no change in value */
static size_t
ip_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
ip_to_string (const char *name, const char *uri, char *buf, size_t bsize)
{
#define IP(x) UIP_NTOHS(server_ipaddr.u16[x])
char *q = "\"";
if (!is_json) {
q = "";
}
return snprintf
( buf, bsize, "%s%x:%x:%x:%x:%x:%x:%x:%x%s"
, q, IP(0), IP(1), IP(2), IP(3), IP(4), IP(5), IP(6), IP(7), q
( buf, bsize, "%x:%x:%x:%x:%x:%x:%x:%x"
, IP(0), IP(1), IP(2), IP(3), IP(4), IP(5), IP(6), IP(7)
);
}
void ip_from_string (const char *name, const char *s)
int ip_from_string (const char *name, const char *uri, const char *s)
{
/* Returns 1 if successful, only copy valid address */
if (uiplib_ip6addrconv (s, &tmp_addr)) {
uip_ip6addr_copy (&server_ipaddr, &tmp_addr);
return 0;
}
return -1;
}
GENERIC_RESOURCE
( server_ip
, ip
, ipv6_address
, 1
, ip_from_string
, ip_to_string
);
static size_t
resource_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
resource_to_string (const char *name, const char *uri, char *buf, size_t bsize)
{
char *q = "\"";
if (!is_json) {
q = "";
}
return snprintf (buf, bsize, "%s%s%s", q, server_resource, q);
return snprintf (buf, bsize, "%s", server_resource);
}
void resource_from_string (const char *name, const char *s)
int resource_from_string (const char *name, const char *uri, const char *s)
{
strncpy (server_resource, s, sizeof (server_resource));
server_resource [sizeof (server_resource) - 1] = 0;
return 0;
}
GENERIC_RESOURCE
( server_resource
, led-resource
, resource-name
, 1
, resource_from_string
, resource_to_string
);
static size_t
interval_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
interval_to_string (const char *name, const char *uri, char *buf, size_t bsize)
{
return snprintf (buf, bsize, "%d", interval);
}
void interval_from_string (const char *name, const char *s)
int interval_from_string (const char *name, const char *uri, const char *s)
{
interval = atoi (s);
return 0;
}
GENERIC_RESOURCE
( interval
, interval
, s
, 0
, interval_from_string
, interval_to_string
);