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-fs-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-log.h"
26 #include "sol-types.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
65 int sol_fs_write_raw(const char *name, struct sol_blob *blob,
66  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
67  const void *data);
68 
78 int sol_fs_read_raw(const char *name, struct sol_buffer *buffer);
79 
84 #ifdef __cplusplus
85 #define CREATE_BUFFER(_val) \
86  struct sol_buffer buf SOL_BUFFER_INIT_FLAGS(_val, \
87  sizeof(*(_val)), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);
88 #else
89 #define CREATE_BUFFER(_val) \
90  struct sol_buffer buf = SOL_BUFFER_INIT_FLAGS(_val, \
91  sizeof(*(_val)), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);
92 #endif /* __cplusplus */
93 
97 #define CREATE_BLOB(_val) \
98  struct sol_blob *blob; \
99  size_t _s = sizeof(*_val); \
100  void *v = malloc(_s); \
101  SOL_NULL_CHECK(v, -ENOMEM); \
102  memcpy(v, _val, _s); \
103  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, v, _s); \
104  if (!blob) { \
105  free(v); \
106  return -EINVAL; \
107  }
108 
117 static inline int
118 sol_fs_read_uint8(const char *name, uint8_t *value)
119 {
120  CREATE_BUFFER(value);
121 
122  return sol_fs_read_raw(name, &buf);
123 }
124 
137 static inline int
138 sol_fs_write_uint8(const char *name, uint8_t value,
139  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
140  const void *data)
141 {
142  int r;
143 
144  CREATE_BLOB(&value);
145 
146  r = sol_fs_write_raw(name, blob, cb, data);
147  sol_blob_unref(blob);
148  return r;
149 }
150 
160 static inline int
161 sol_fs_read_bool(const char *name, bool *value)
162 {
163  CREATE_BUFFER(value);
164 
165  return sol_fs_read_raw(name, &buf);
166 }
167 
180 static inline int
181 sol_fs_write_bool(const char *name, bool value,
182  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
183  const void *data)
184 {
185  int r;
186 
187  CREATE_BLOB(&value);
188 
189  r = sol_fs_write_raw(name, blob, cb, data);
190  sol_blob_unref(blob);
191  return r;
192 }
193 
203 static inline int
204 sol_fs_read_int32(const char *name, int32_t *value)
205 {
206  CREATE_BUFFER(value);
207 
208  return sol_fs_read_raw(name, &buf);
209 }
210 
223 static inline int
224 sol_fs_write_int32(const char *name, int32_t value,
225  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
226  const void *data)
227 {
228  int r;
229 
230  CREATE_BLOB(&value);
231 
232  r = sol_fs_write_raw(name, blob, cb, data);
233  sol_blob_unref(blob);
234  return r;
235 }
236 
246 static inline int
247 sol_fs_read_irange(const char *name, struct sol_irange *value)
248 {
249  CREATE_BUFFER(value);
250 
251  return sol_fs_read_raw(name, &buf);
252 }
253 
266 static inline int
267 sol_fs_write_irange(const char *name, struct sol_irange *value,
268  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
269  const void *data)
270 {
271  int r;
272 
273  CREATE_BLOB(value);
274 
275  r = sol_fs_write_raw(name, blob, cb, data);
276  sol_blob_unref(blob);
277  return r;
278 }
279 
289 static inline int
290 sol_fs_read_drange(const char *name, struct sol_drange *value)
291 {
292  CREATE_BUFFER(value);
293 
294  return sol_fs_read_raw(name, &buf);
295 }
296 
309 static inline int
310 sol_fs_write_drange(const char *name, struct sol_drange *value,
311  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
312  const void *data)
313 {
314  int r;
315 
316  CREATE_BLOB(value);
317 
318  r = sol_fs_write_raw(name, blob, cb, data);
319  sol_blob_unref(blob);
320  return r;
321 }
322 
332 static inline int
333 sol_fs_read_double(const char *name, double *value)
334 {
335  CREATE_BUFFER(value);
336 
337  return sol_fs_read_raw(name, &buf);
338 }
339 
352 static inline int
353 sol_fs_write_double(const char *name, double value,
354  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
355  const void *data)
356 {
357  int r;
358 
359  CREATE_BLOB(&value);
360 
361  r = sol_fs_write_raw(name, blob, cb, data);
362  sol_blob_unref(blob);
363  return r;
364 }
365 
375 static inline int
376 sol_fs_read_string(const char *name, char **value)
377 {
378  struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
379  int r;
380 
381  r = sol_fs_read_raw(name, &buf);
382  if (r < 0) {
383  sol_buffer_fini(&buf);
384  return r;
385  }
386 
387  *value = (char *)sol_buffer_steal(&buf, NULL);
388 
389  return 0;
390 }
391 
404 static inline int
405 sol_fs_write_string(const char *name, const char *value,
406  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
407  const void *data)
408 {
409  int r;
410  struct sol_blob *blob;
411  char *string;
412 
413  string = strdup(value);
414  SOL_NULL_CHECK(string, -ENOMEM);
415 
416  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, string, strlen(value));
417  SOL_NULL_CHECK_GOTO(blob, error);
418 
419  r = sol_fs_write_raw(name, blob, cb, data);
420  sol_blob_unref(blob);
421  return r;
422 
423 error:
424  free(string);
425 
426  return -ENOMEM;
427 }
428 
433 #undef CREATE_BUFFER
434 
435 #undef CREATE_BLOB
436 
437 #ifdef __cplusplus
438 }
439 #endif
static int sol_fs_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 sol_drange struct in storage.
Definition: sol-fs-storage.h:310
static int sol_fs_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 sol_irange struct in storage.
Definition: sol-fs-storage.h:267
static int sol_fs_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 in storage.
Definition: sol-fs-storage.h:405
#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
void sol_buffer_fini(struct sol_buffer *buf)
Finalizes the buffer.
These routines are used for Soletta types' manipulation.
Data type describing a Double range.
Definition: sol-types.h:187
static int sol_fs_read_string(const char *name, char **value)
Read string in storage.
Definition: sol-fs-storage.h:376
static int sol_fs_read_double(const char *name, double *value)
Read double in storage.
Definition: sol-fs-storage.h:333
static int sol_fs_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 uint8_t in storage.
Definition: sol-fs-storage.h:138
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.
const struct sol_blob_type SOL_BLOB_TYPE_DEFAULT
Blob type object for the default implementation.
These routines are used for Soletta logging.
static struct sol_buffer value
Definition: server.c:42
These are routines that Soletta provides for its buffer implementation.
#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-fs-storage.h:89
Data type describing Integer ranges.
Definition: sol-types.h:327
void * data
Buffer data.
Definition: sol-buffer.h:131
Data type describing the default blob implementation.
Definition: sol-types.h:468
static int sol_fs_read_bool(const char *name, bool *value)
Read a boolean from storage.
Definition: sol-fs-storage.h:161
static int sol_fs_read_uint8(const char *name, uint8_t *value)
Read an uint8_t from storage.
Definition: sol-fs-storage.h:118
static int sol_fs_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 int32 in storage.
Definition: sol-fs-storage.h:224
static int sol_fs_read_drange(const char *name, struct sol_drange *value)
Read sol_drange struct from storage.
Definition: sol-fs-storage.h:290
#define CREATE_BLOB(_val)
Macro to create a struct sol_blob with value passed as argument.
Definition: sol-fs-storage.h:97
#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
static int sol_fs_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 in storage.
Definition: sol-fs-storage.h:353
void * sol_buffer_steal(struct sol_buffer *buf, size_t *size)
'Steals' sol_buffer internal buffer and resets sol_buffer.
int sol_fs_read_raw(const char *name, struct sol_buffer *buffer)
Read stored contents and set to buffer.
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
static int sol_fs_read_int32(const char *name, int32_t *value)
Read an int32 from storage.
Definition: sol-fs-storage.h:204
int sol_fs_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.
static int sol_fs_read_irange(const char *name, struct sol_irange *value)
Read sol_irange struct from storage.
Definition: sol-fs-storage.h:247
static int sol_fs_write_bool(const char *name, bool value, void(*cb)(void *data, const char *name, struct sol_blob *blob, int status), const void *data)
Writes boolean in storage.
Definition: sol-fs-storage.h:181
void sol_blob_unref(struct sol_blob *blob)
Decreases the reference counter of the given blob.