Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
retained_mem.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12#ifndef ZEPHYR_INCLUDE_DRIVERS_RETAINED_MEM_
13#define ZEPHYR_INCLUDE_DRIVERS_RETAINED_MEM_
14
15#include <stdint.h>
16#include <stddef.h>
17#include <sys/types.h>
18#include <zephyr/kernel.h>
19#include <zephyr/device.h>
20#include <zephyr/types.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27BUILD_ASSERT(!(sizeof(off_t) > sizeof(size_t)),
28 "Size of off_t must be equal or less than size of size_t");
29
42typedef ssize_t (*retained_mem_size_api)(const struct device *dev);
43
49typedef int (*retained_mem_read_api)(const struct device *dev, off_t offset, uint8_t *buffer,
50 size_t size);
51
57typedef int (*retained_mem_write_api)(const struct device *dev, off_t offset,
58 const uint8_t *buffer, size_t size);
59
65typedef int (*retained_mem_clear_api)(const struct device *dev);
66
78__subsystem struct retained_mem_driver_api {
83};
84
93__syscall ssize_t retained_mem_size(const struct device *dev);
94
95static inline ssize_t z_impl_retained_mem_size(const struct device *dev)
96{
97 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
98
99 return api->size(dev);
100}
101
112__syscall int retained_mem_read(const struct device *dev, off_t offset, uint8_t *buffer,
113 size_t size);
114
115static inline int z_impl_retained_mem_read(const struct device *dev, off_t offset,
116 uint8_t *buffer, size_t size)
117{
118 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
119 size_t area_size;
120
121 /* Validate user-supplied parameters */
122 if (size == 0) {
123 return 0;
124 }
125
126 area_size = api->size(dev);
127
128 if (offset < 0 || size > area_size || (area_size - size) < (size_t)offset) {
129 return -EINVAL;
130 }
131
132 return api->read(dev, offset, buffer, size);
133}
134
146__syscall int retained_mem_write(const struct device *dev, off_t offset, const uint8_t *buffer,
147 size_t size);
148
149static inline int z_impl_retained_mem_write(const struct device *dev, off_t offset,
150 const uint8_t *buffer, size_t size)
151{
152 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
153 size_t area_size;
154
155 /* Validate user-supplied parameters */
156 if (size == 0) {
157 return 0;
158 }
159
160 area_size = api->size(dev);
161
162 if (offset < 0 || size > area_size || (area_size - size) < (size_t)offset) {
163 return -EINVAL;
164 }
165
166 return api->write(dev, offset, buffer, size);
167}
168
176__syscall int retained_mem_clear(const struct device *dev);
177
178static inline int z_impl_retained_mem_clear(const struct device *dev)
179{
180 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
181
182 return api->clear(dev);
183}
184
189#ifdef __cplusplus
190}
191#endif
192
193#include <syscalls/retained_mem.h>
194
195#endif /* ZEPHYR_INCLUDE_DRIVERS_RETAINED_MEM_ */
int(* retained_mem_write_api)(const struct device *dev, off_t offset, const uint8_t *buffer, size_t size)
Callback API to write to retained memory area.
Definition: retained_mem.h:57
int retained_mem_write(const struct device *dev, off_t offset, const uint8_t *buffer, size_t size)
Writes data to the Retained memory area - underlying data does not need to be cleared prior to writin...
int(* retained_mem_clear_api)(const struct device *dev)
Callback API to clear retained memory area (reset all data to 0x00).
Definition: retained_mem.h:65
int retained_mem_read(const struct device *dev, off_t offset, uint8_t *buffer, size_t size)
Reads data from the Retained memory area.
ssize_t(* retained_mem_size_api)(const struct device *dev)
Callback API to get size of retained memory area.
Definition: retained_mem.h:42
int retained_mem_clear(const struct device *dev)
Clears data in the retained memory area by setting it to 0x00.
ssize_t retained_mem_size(const struct device *dev)
Returns the size of the retained memory area.
int(* retained_mem_read_api)(const struct device *dev, off_t offset, uint8_t *buffer, size_t size)
Callback API to read from retained memory area.
Definition: retained_mem.h:49
#define EINVAL
Invalid argument.
Definition: errno.h:61
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition: types.h:28
__INTPTR_TYPE__ off_t
Definition: types.h:36
Size of off_t must be equal or less than size of size_t
Definition: retained_mem.h:28
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition: device.h:381
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:387
Retained memory driver API API which can be used by a device to store data in a retained memory area.
Definition: retained_mem.h:78
retained_mem_size_api size
Definition: retained_mem.h:79
retained_mem_clear_api clear
Definition: retained_mem.h:82
retained_mem_read_api read
Definition: retained_mem.h:80
retained_mem_write_api write
Definition: retained_mem.h:81