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-efivarfs-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 
61 int sol_efivars_write_raw(const char *name, struct sol_blob *blob,
62  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
63  const void *data);
64 
78 int sol_efivars_read_raw(const char *name, struct sol_buffer *buffer);
79 
85 #ifdef __cplusplus
86 #define CREATE_BUFFER(_val) \
87  struct sol_buffer buf SOL_BUFFER_INIT_FLAGS(_val, \
88  sizeof(*(_val)), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);
89 #else
90 #define CREATE_BUFFER(_val) \
91  struct sol_buffer buf = SOL_BUFFER_INIT_FLAGS(_val, \
92  sizeof(*(_val)), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);
93 #endif /* __cplusplus */
94 
98 #define CREATE_BLOB(_val) \
99  struct sol_blob *blob; \
100  size_t _s = sizeof(*_val); \
101  void *v = malloc(_s); \
102  SOL_NULL_CHECK(v, -ENOMEM); \
103  memcpy(v, _val, _s); \
104  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, v, _s); \
105  if (!blob) { \
106  free(v); \
107  return -EINVAL; \
108  }
109 
122 static inline int
123 sol_efivars_read_uint8(const char *name, uint8_t *value)
124 {
125  CREATE_BUFFER(value);
126 
127  return sol_efivars_read_raw(name, &buf);
128 }
129 
145 static inline int
146 sol_efivars_write_uint8(const char *name, uint8_t value,
147  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
148  const void *data)
149 {
150  int r;
151 
152  CREATE_BLOB(&value);
153 
154  r = sol_efivars_write_raw(name, blob, cb, data);
155  sol_blob_unref(blob);
156  return r;
157 }
158 
171 static inline int
172 sol_efivars_read_bool(const char *name, bool *value)
173 {
174  CREATE_BUFFER(value);
175 
176  return sol_efivars_read_raw(name, &buf);
177 }
178 
194 static inline int
195 sol_efivars_write_bool(const char *name, bool value,
196  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
197  const void *data)
198 {
199  int r;
200 
201  CREATE_BLOB(&value);
202 
203  r = sol_efivars_write_raw(name, blob, cb, data);
204  sol_blob_unref(blob);
205  return r;
206 }
207 
220 static inline int
221 sol_efivars_read_int32(const char *name, int32_t *value)
222 {
223  CREATE_BUFFER(value);
224 
225  return sol_efivars_read_raw(name, &buf);
226 }
227 
243 static inline int
244 sol_efivars_write_int32(const char *name, int32_t value,
245  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
246  const void *data)
247 {
248  int r;
249 
250  CREATE_BLOB(&value);
251 
252  r = sol_efivars_write_raw(name, blob, cb, data);
253  sol_blob_unref(blob);
254  return r;
255 }
256 
269 static inline int
270 sol_efivars_read_irange(const char *name, struct sol_irange *value)
271 {
272  CREATE_BUFFER(value);
273 
274  return sol_efivars_read_raw(name, &buf);
275 }
276 
292 static inline int
293 sol_efivars_write_irange(const char *name, struct sol_irange *value,
294  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
295  const void *data)
296 {
297  int r;
298 
299  CREATE_BLOB(value);
300 
301  r = sol_efivars_write_raw(name, blob, cb, data);
302  sol_blob_unref(blob);
303  return r;
304 }
305 
318 static inline int
319 sol_efivars_read_drange(const char *name, struct sol_drange *value)
320 {
321  CREATE_BUFFER(value);
322 
323  return sol_efivars_read_raw(name, &buf);
324 }
325 
341 static inline int
342 sol_efivars_write_drange(const char *name, struct sol_drange *value,
343  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
344  const void *data)
345 {
346  int r;
347 
348  CREATE_BLOB(value);
349 
350  r = sol_efivars_write_raw(name, blob, cb, data);
351  sol_blob_unref(blob);
352  return r;
353 }
354 
367 static inline int
368 sol_efivars_read_double(const char *name, double *value)
369 {
370  CREATE_BUFFER(value);
371 
372  return sol_efivars_read_raw(name, &buf);
373 }
374 
390 static inline int
391 sol_efivars_write_double(const char *name, double value,
392  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
393  const void *data)
394 {
395  int r;
396 
397  CREATE_BLOB(&value);
398 
399  r = sol_efivars_write_raw(name, blob, cb, data);
400  sol_blob_unref(blob);
401  return r;
402 }
403 
416 static inline int
417 sol_efivars_read_string(const char *name, char **value)
418 {
419  struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
420  int r;
421 
422  r = sol_efivars_read_raw(name, &buf);
423  if (r < 0) {
424  sol_buffer_fini(&buf);
425  return r;
426  }
427 
428  *value = (char *)sol_buffer_steal(&buf, NULL);
429 
430  return 0;
431 }
432 
448 static inline int
449 sol_efivars_write_string(const char *name, const char *value,
450  void (*cb)(void *data, const char *name, struct sol_blob *blob, int status),
451  const void *data)
452 {
453  int r;
454  struct sol_blob *blob;
455  char *string;
456 
457  string = strdup(value);
458  SOL_NULL_CHECK(string, -ENOMEM);
459 
460  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, string, strlen(value));
461  SOL_NULL_CHECK_GOTO(blob, error);
462 
463  r = sol_efivars_write_raw(name, blob, cb, data);
464  sol_blob_unref(blob);
465  return r;
466 
467 error:
468  free(string);
469 
470  return -ENOMEM;
471 }
472 
477 #undef CREATE_BUFFER
478 
479 #undef CREATE_BLOB
480 
481 #ifdef __cplusplus
482 }
483 #endif
static int sol_efivars_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 a sol_drange into an EFI variable.
Definition: sol-efivarfs-storage.h:342
#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.
static int sol_efivars_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 into an EFI variable.
Definition: sol-efivarfs-storage.h:449
These routines are used for Soletta types' manipulation.
static int sol_efivars_read_double(const char *name, double *value)
Reads a double from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:368
static int sol_efivars_read_uint8(const char *name, uint8_t *value)
Reads an uint8_t from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:123
static int sol_efivars_read_bool(const char *name, bool *value)
Reads a boolean from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:172
Data type describing a Double range.
Definition: sol-types.h:187
static int sol_efivars_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 into an EFI variable.
Definition: sol-efivarfs-storage.h:244
static int sol_efivars_read_drange(const char *name, struct sol_drange *value)
Reads a sol_drange from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:319
static int sol_efivars_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 boolean into an EFI variable.
Definition: sol-efivarfs-storage.h:195
static int sol_efivars_read_int32(const char *name, int32_t *value)
Reads an int32_t from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:221
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.
static int sol_efivars_read_irange(const char *name, struct sol_irange *value)
Reads a sol_irange from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:270
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.
static int sol_efivars_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 into an EFI variable.
Definition: sol-efivarfs-storage.h:391
Data type describing Integer ranges.
Definition: sol-types.h:327
#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-efivarfs-storage.h:90
void * data
Buffer data.
Definition: sol-buffer.h:131
static int sol_efivars_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 into an EFI variable.
Definition: sol-efivarfs-storage.h:146
Data type describing the default blob implementation.
Definition: sol-types.h:468
#define CREATE_BLOB(_val)
Macro to create a struct sol_blob with value passed as argument.
Definition: sol-efivarfs-storage.h:98
int sol_efivars_read_raw(const char *name, struct sol_buffer *buffer)
Read stored contents and set to buffer.
static int sol_efivars_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 a sol_irange into an EFI variable.
Definition: sol-efivarfs-storage.h:293
#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
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
static int sol_efivars_read_string(const char *name, char **value)
Reads a string from the EFI given variable and set to value.
Definition: sol-efivarfs-storage.h:417
int sol_efivars_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.
void sol_blob_unref(struct sol_blob *blob)
Decreases the reference counter of the given blob.