Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-memmap-storage.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 #include "sol-buffer.h"
25 #include "sol-str-table.h"
26 #include "sol-types.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
67 #define SOL_MEMMAP_VERSION_ENTRY "_version"
68 
73 #define SOL_MEMMAP_ENTRY_BIT_SIZE(_name, _offset, _size, _bit_offset, _bit_size) \
74  static struct sol_memmap_entry _name = { .offset = (_offset), .size = (_size), .bit_size = (_bit_size), .bit_offset = (_bit_offset) }
75 
76 
81 #define SOL_MEMMAP_ENTRY(_name, _offset, _size) \
82  SOL_MEMMAP_ENTRY_BIT_SIZE(_name, _offset, _size, 0, 0)
83 
88 #define SOL_MEMMAP_BOOL_ENTRY(_name, _offset, _bit_offset) \
89  SOL_MEMMAP_ENTRY_BIT_SIZE(_name, _offset, 1, _bit_offset, 1)
90 
96 typedef struct sol_memmap_map {
97  uint8_t version;
98  const char *path;
120  uint32_t timeout;
122  const struct sol_str_table_ptr *entries; /* Memory trick in place, must be last on struct*/
124 
130 typedef struct sol_memmap_entry {
131  size_t offset;
132  size_t size;
133  uint32_t bit_size;
134  uint8_t bit_offset;
136 
156 int sol_memmap_write_raw(const char *name, struct sol_blob *blob,
157  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
158  const void *data);
159 
171 int sol_memmap_read_raw(const char *name, struct sol_buffer *buffer);
172 
183 int sol_memmap_add_map(const struct sol_memmap_map *map);
184 
192 int sol_memmap_remove_map(const struct sol_memmap_map *map);
193 
205 int sol_memmap_set_timeout(struct sol_memmap_map *map, uint32_t timeout);
206 
216 uint32_t sol_memmap_get_timeout(const struct sol_memmap_map *map);
217 
222 #define CREATE_BUFFER(_val) \
223  struct sol_buffer buf = SOL_BUFFER_INIT_FLAGS(_val, \
224  sizeof(*(_val)), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);
225 
229 #define CREATE_BLOB(_val) \
230  struct sol_blob *blob; \
231  size_t _s = sizeof(*_val); \
232  void *v = malloc(_s); \
233  SOL_NULL_CHECK(v, -ENOMEM); \
234  memcpy(v, _val, _s); \
235  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, v, _s); \
236  if (!blob) { \
237  free(v); \
238  return -EINVAL; \
239  }
240 
251 static inline int
252 sol_memmap_read_uint8(const char *name, uint8_t *value)
253 {
254  CREATE_BUFFER(value);
255 
256  return sol_memmap_read_raw(name, &buf);
257 }
258 
277 static inline int
278 sol_memmap_write_uint8(const char *name, uint8_t value,
279  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
280  const void *data)
281 {
282  int r;
283 
284  CREATE_BLOB(&value);
285 
286  r = sol_memmap_write_raw(name, blob, cb, data);
287  sol_blob_unref(blob);
288  return r;
289 }
290 
301 static inline int
302 sol_memmap_read_bool(const char *name, bool *value)
303 {
304  CREATE_BUFFER(value);
305 
306  return sol_memmap_read_raw(name, &buf);
307 }
308 
327 static inline int
328 sol_memmap_write_bool(const char *name, bool value,
329  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
330  const void *data)
331 {
332  int r;
333 
334  CREATE_BLOB(&value);
335 
336  r = sol_memmap_write_raw(name, blob, cb, data);
337  sol_blob_unref(blob);
338  return r;
339 }
340 
351 static inline int
352 sol_memmap_read_int32(const char *name, int32_t *value)
353 {
354  CREATE_BUFFER(value);
355 
356  return sol_memmap_read_raw(name, &buf);
357 }
358 
377 static inline int
378 sol_memmap_write_int32(const char *name, int32_t value,
379  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
380  const void *data)
381 {
382  int r;
383 
384  CREATE_BLOB(&value);
385 
386  r = sol_memmap_write_raw(name, blob, cb, data);
387  sol_blob_unref(blob);
388  return r;
389 }
390 
403 static inline int
404 sol_memmap_read_irange(const char *name, struct sol_irange *value)
405 {
406  CREATE_BUFFER(value);
407 
408  return sol_memmap_read_raw(name, &buf);
409 }
410 
430 static inline int
431 sol_memmap_write_irange(const char *name, struct sol_irange *value,
432  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
433  const void *data)
434 {
435  int r;
436 
437  CREATE_BLOB(value);
438 
439  r = sol_memmap_write_raw(name, blob, cb, data);
440  sol_blob_unref(blob);
441  return r;
442 }
443 
456 static inline int
457 sol_memmap_read_drange(const char *name, struct sol_drange *value)
458 {
459  CREATE_BUFFER(value);
460 
461  return sol_memmap_read_raw(name, &buf);
462 }
463 
483 static inline int
484 sol_memmap_write_drange(const char *name, struct sol_drange *value,
485  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
486  const void *data)
487 {
488  int r;
489 
490  CREATE_BLOB(value);
491 
492  r = sol_memmap_write_raw(name, blob, cb, data);
493  sol_blob_unref(blob);
494  return r;
495 }
496 
507 static inline int
508 sol_memmap_read_double(const char *name, double *value)
509 {
510  CREATE_BUFFER(value);
511 
512  return sol_memmap_read_raw(name, &buf);
513 }
514 
533 static inline int
534 sol_memmap_write_double(const char *name, double value,
535  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
536  const void *data)
537 {
538  int r;
539 
540  CREATE_BLOB(&value);
541 
542  r = sol_memmap_write_raw(name, blob, cb, data);
543  sol_blob_unref(blob);
544  return r;
545 }
546 
557 static inline int
558 sol_memmap_read_string(const char *name, char **value)
559 {
560  struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
561  int r;
562 
563  r = sol_memmap_read_raw(name, &buf);
564  if (r < 0) {
565  sol_buffer_fini(&buf);
566  return r;
567  }
568 
569  *value = (char *)sol_buffer_steal(&buf, NULL);
570 
571  return 0;
572 }
573 
592 static inline int
593 sol_memmap_write_string(const char *name, const char *value,
594  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
595  const void *data)
596 {
597  int r;
598  struct sol_blob *blob;
599  char *string;
600 
601  string = strdup(value);
602  SOL_NULL_CHECK(string, -ENOMEM);
603 
604  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, string, strlen(value) + 1);
605  SOL_NULL_CHECK_GOTO(blob, error);
606 
607  r = sol_memmap_write_raw(name, blob, cb, data);
608  sol_blob_unref(blob);
609  return r;
610 
611 error:
612  free(string);
613 
614  return -ENOMEM;
615 }
616 
621 #undef CREATE_BUFFER
622 
623 #undef CREATE_BLOB
624 
625 #ifdef __cplusplus
626 }
627 #endif
int sol_memmap_write_raw(const char *name, struct sol_blob *blob, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes buffer contents to storage.
size_t offset
Offset of this entry on storage, in bytes.
Definition: sol-memmap-storage.h:131
static int sol_memmap_write_int32(const char *name, int32_t value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes an int32_t contents to storage.
Definition: sol-memmap-storage.h:378
int sol_memmap_read_raw(const char *name, struct sol_buffer *buffer)
Read storage contents to buffer.
#define SOL_NULL_CHECK(ptr,...)
Convenience macro to check for NULL pointer.
Definition: sol-log.h:223
#define SOL_BUFFER_INIT_EMPTY
Helper macro to initialize an empty buffer.
Definition: sol-buffer.h:153
static int sol_memmap_write_double(const char *name, double value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes a double contents to storage.
Definition: sol-memmap-storage.h:534
void sol_buffer_fini(struct sol_buffer *buf)
Finalizes the buffer.
int sol_memmap_add_map(const struct sol_memmap_map *map)
Add a map to internal list of available maps.
static int sol_memmap_write_string(const char *name, const char *value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes a string contents to storage.
Definition: sol-memmap-storage.h:593
size_t size
Total size of this entry on storage, in bytes.
Definition: sol-memmap-storage.h:132
static int sol_memmap_read_irange(const char *name, struct sol_irange *value)
Reads an irange content.
Definition: sol-memmap-storage.h:404
static int sol_memmap_read_int32(const char *name, int32_t *value)
Reads an int32_t contents.
Definition: sol-memmap-storage.h:352
int sol_memmap_remove_map(const struct sol_memmap_map *map)
Removes a previously added map from internal list of available maps.
Memory map basic struct.
Definition: sol-memmap-storage.h:96
struct sol_memmap_map sol_memmap_map
Memory map basic struct.
These routines are used for Soletta types' manipulation.
static int sol_memmap_write_uint8(const char *name, uint8_t value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes an uint8_t contents to storage.
Definition: sol-memmap-storage.h:278
const struct sol_str_table_ptr * entries
Entries on map, containing name, offset and size.
Definition: sol-memmap-storage.h:122
static int sol_memmap_read_string(const char *name, char **value)
Reads a string contents.
Definition: sol-memmap-storage.h:558
Data type describing a Double range.
Definition: sol-types.h:187
uint8_t version
Version of map.
Definition: sol-memmap-storage.h:97
#define CREATE_BLOB(_val)
Macro to create a struct sol_blob with value passed as argument.
Definition: sol-memmap-storage.h:229
struct sol_blob * sol_blob_new(const struct sol_blob_type *type, struct sol_blob *parent, const void *mem, size_t size)
Creates a new blob instance of the given type type.
static int sol_memmap_read_bool(const char *name, bool *value)
Reads a boolean contents.
Definition: sol-memmap-storage.h:302
const struct sol_blob_type SOL_BLOB_TYPE_DEFAULT
Blob type object for the default implementation.
static struct sol_buffer value
Definition: server.c:42
These are routines that Soletta provides for its buffer implementation.
A memory map entry.
Definition: sol-memmap-storage.h:130
struct sol_memmap_entry sol_memmap_entry
A memory map entry.
static struct sol_timeout * timeout
Definition: browse.c:36
Data type describing Integer ranges.
Definition: sol-types.h:327
void * data
Buffer data.
Definition: sol-buffer.h:131
#define CREATE_BUFFER(_val)
Macro to create a struct sol_buffer with value passed as argument and flags SOL_BUFFER_FLAGS_MEMORY_N...
Definition: sol-memmap-storage.h:222
static int sol_memmap_write_drange(const char *name, struct sol_drange *value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes an drange contents to storage.
Definition: sol-memmap-storage.h:484
static int sol_memmap_write_bool(const char *name, bool value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes a bool contents to storage.
Definition: sol-memmap-storage.h:328
static int sol_memmap_read_double(const char *name, double *value)
Reads a double contents.
Definition: sol-memmap-storage.h:508
Data type describing the default blob implementation.
Definition: sol-types.h:468
uint32_t bit_size
Total size of this entry on storage, in bits.
Definition: sol-memmap-storage.h:133
uint32_t timeout
Timeout, in milliseconds, of writing operations.
Definition: sol-memmap-storage.h:120
uint8_t bit_offset
Bit offset on first byte.
Definition: sol-memmap-storage.h:134
String/Pointer table type.
Definition: sol-str-table.h:141
static int sol_memmap_write_irange(const char *name, struct sol_irange *value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes an irange contents to storage.
Definition: sol-memmap-storage.h:431
uint32_t sol_memmap_get_timeout(const struct sol_memmap_map *map)
Get map timeout.
static int sol_memmap_read_uint8(const char *name, uint8_t *value)
Read an uint8_t contents.
Definition: sol-memmap-storage.h:252
#define SOL_NULL_CHECK_GOTO(ptr, label)
Convenience macro to check for NULL pointer and jump to a given label.
Definition: sol-log.h:262
These are routines that Soletta provides for its string table implementation.
void * sol_buffer_steal(struct sol_buffer *buf, size_t *size)
'Steals' sol_buffer internal buffer and resets sol_buffer.
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
int sol_memmap_set_timeout(struct sol_memmap_map *map, uint32_t timeout)
Defines map timeout to actually perform write.
const char * path
Where to find the storage.
Definition: sol-memmap-storage.h:98
static int sol_memmap_read_drange(const char *name, struct sol_drange *value)
Reads a drange contents.
Definition: sol-memmap-storage.h:457
void sol_blob_unref(struct sol_blob *blob)
Decreases the reference counter of the given blob.