10 #include "pb_decode.h"
11 #include "pb_encode.h"
12 #include "readings.pb.h"
25 #define MAX_URI_LENGTH 20
30 #define NO_SAMPLE_ID -1
35 #define INVALID_SAMPLE_ID -2
40 #define SEPARATOR_CHAR '/'
41 #define SEPARATOR_STR "/"
48 static void res_get_handler(
void* request,
void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
55 static void res_delete_handler(
void* request,
void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
72 void res_get_handler(
void* request,
void* response, uint8_t *payload_buffer, uint16_t preferred_size, int32_t *offset) {
73 static uint8_t sample_buffer[Sample_size];
74 static uint8_t sample_len;
78 int16_t current_offset = *offset;
80 DEBUG(
"Serving request! Offset %d, PrefSize %d\n", current_offset, preferred_size);
83 if (current_offset == 0) {
89 DEBUG(
"Get request with invalid sample id!\n");
90 REST.set_response_status(response, REST.status.BAD_REQUEST);
102 DEBUG(
"Unable to get sample!\n");
103 REST.set_response_status(response, REST.status.NOT_FOUND);
108 DEBUG(
"Got a Sample, size: %u\n", sample_len);
111 if (sample_len - current_offset <= preferred_size) {
112 DEBUG(
"Request fits in a single block\n");
114 payload_len = sample_len - current_offset;
121 DEBUG(
"Request will be split into chunks\n");
123 payload_len = preferred_size;
125 *offset += preferred_size;
128 memcpy(payload_buffer, sample_buffer + current_offset, payload_len);
130 REST.set_header_content_type(response, REST.type.APPLICATION_OCTET_STREAM);
131 REST.set_response_payload(response, payload_buffer, payload_len);
135 void res_delete_handler(
void* request,
void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) {
141 DEBUG(
"Delete request with invalid / missing sample id!\n");
142 REST.set_response_status(response, REST.status.BAD_REQUEST);
146 DEBUG(
"Delete request for: %d\n", sample_id);
149 DEBUG(
"Failed to delete sample\n");
150 REST.set_response_status(response, REST.status.INTERNAL_SERVER_ERROR);
154 REST.set_response_status(response, REST.status.DELETED);
158 const char *uri_path;
166 uri_length = REST.get_url(request, &uri_path);
173 strncpy(terminated_uri_path, uri_path, uri_length);
174 terminated_uri_path[uri_length] =
'\0';
175 token_ptr = &terminated_uri_path[0];
178 strsep(&token_ptr, SEPARATOR_STR);
179 if (token_ptr ==
NULL) {
180 DEBUG(
"Request with no sample id!\n");
186 sample_id = strtoul(token_ptr, &end_ptr, 0);
190 if (errno != 0 || (*end_ptr !=
'\0' && *end_ptr !=
SEPARATOR_CHAR)) {
191 DEBUG(
"Request with invalid sample id!\n");
#define NO_SAMPLE_ID
Indicates no trailing sample id was found in the URI.
PARENT_RESOURCE(res_sample,"Sample", res_get_handler, NULL, NULL, res_delete_handler)
Sample ressource.
#define MAX_URI_LENGTH
Maximum length of a URI.
An abstraction layer for RESTful Web services (Erbium).
uint8_t store_get_raw_sample(uint16_t id, uint8_t buffer[Sample_size])
Get a given sample from the flash, in the form of an encoded protocol buffer.
#define INVALID_SAMPLE_ID
Indicates a sample id was found in the URI, but that it could not be sucesfully parsed.
static void res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
Delete handler for Samples.
#define NULL
The null pointer.
Convenience layer for storing readings and the config.
bool store_delete_sample(uint16_t id)
Delete a given sample from the flash.
#define SEPARATOR_CHAR
The separator used in URIs, either as a character, or as a string literal.
static int16_t parse_sample_id(void *request)
Parse a trailing sample id from a URI.
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
Get handler for Samples.
uint8_t store_get_latest_raw_sample(uint8_t buffer[Sample_size])
Get the most recent sample from the flash, in the form of an encoded protocol buffer.