11 #include "pb_decode.h"
12 #include "pb_encode.h"
13 #include "settings.pb.h"
26 static void res_get_handler(
void* request,
void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
32 static void res_post_handler(
void* request,
void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
37 static uint8_t config_buffer[SensorConfig_size];
42 static uint8_t config_len;
47 RESOURCE(res_config,
"Config", res_get_handler, res_post_handler,
NULL,
NULL);
49 void res_get_handler(
void* request,
void* response, uint8_t *payload_buffer, uint16_t preferred_size, int32_t *offset) {
51 int16_t current_offset = *offset;
53 DEBUG(
"Serving request! Offset %d, PrefSize %d\n", current_offset, preferred_size);
56 if (current_offset == 0) {
62 DEBUG(
"Unable to get config!\n");
63 REST.set_response_status(response, REST.status.INTERNAL_SERVER_ERROR);
68 DEBUG(
"Got config\n");
71 if (config_len - current_offset <= preferred_size) {
72 DEBUG(
"Request fits in a single block\n");
74 payload_len = config_len - current_offset;
81 DEBUG(
"Request will be split into chunks\n");
83 payload_len = preferred_size;
85 *offset += preferred_size;
88 memcpy(payload_buffer, config_buffer + current_offset, payload_len);
90 REST.set_header_content_type(response, REST.type.APPLICATION_OCTET_STREAM);
91 REST.set_response_payload(response, payload_buffer, payload_len);
94 void res_post_handler(
void* request,
void* response, uint8_t *payload_buffer, uint16_t preferred_size, int32_t *offset) {
97 pb_istream_t pb_istream;
99 coap_packet_t *coap_req = (coap_packet_t *)request;
101 DEBUG(
"Config post request!\n");
104 if ((incoming_len = REST.get_request_payload(request, (
const uint8_t **)&incoming))) {
106 if (coap_req->block1_num * coap_req->block1_size + incoming_len <= SensorConfig_size) {
108 DEBUG(
"Got config payload\n");
111 memcpy(config_buffer + coap_req->block1_num * coap_req->block1_size, incoming, incoming_len);
112 config_len = coap_req->block1_num * coap_req->block1_size + incoming_len;
115 if (!coap_req->block1_more) {
117 DEBUG(
"Last (or only) config block, saving...\n");
119 pb_istream = pb_istream_from_buffer(config_buffer, config_len);
122 if (!pb_decode_delimited(&pb_istream, SensorConfig_fields, &config)) {
123 DEBUG(
"Failed to decode config!\n");
124 REST.set_response_status(response, REST.status.BAD_REQUEST);
130 DEBUG(
"Unsane config!\n");
131 REST.set_response_status(response, REST.status.BAD_REQUEST);
137 DEBUG(
"Failed to save config!\n");
138 REST.set_response_status(response, REST.status.INTERNAL_SERVER_ERROR);
142 DEBUG(
"Config saved and decoded\n");
147 REST.set_response_status(response, REST.status.CHANGED);
148 coap_set_header_block1(response, coap_req->block1_num, 0, coap_req->block1_size);
151 DEBUG(
"Config post request too big!\n");
152 REST.set_response_status(response, REST.status.REQUEST_ENTITY_TOO_LARGE);
157 DEBUG(
"Unable to get payload!\n");
158 REST.set_response_status(response, REST.status.BAD_REQUEST);
bool store_save_config(SensorConfig *config)
Save the configuration to flash.
An abstraction layer for RESTful Web services (Erbium).
bool sampler_check_config(SensorConfig *config)
Check a config for sanity.
#define NULL
The null pointer.
Convenience layer for storing readings and the config.
uint8_t store_get_raw_config(uint8_t buffer[SensorConfig_size])
Get the configuration from the flash.
void sampler_refresh_config(void)
Get the Sampler to reload it's config from flash.
RESOURCE(res_routes,"Routes", res_get_handler, NULL, NULL, NULL)
Route resource.
An implementation of the Constrained Application Protocol (RFC).
Process that periodically takes samples from onboard sensors and stores them in flash.