Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
44typedef ssize_t (*retained_mem_size_api)(const struct device *dev);
45
51typedef int (*retained_mem_read_api)(const struct device *dev, off_t offset, uint8_t *buffer,
52 size_t size);
53
59typedef int (*retained_mem_write_api)(const struct device *dev, off_t offset,
60 const uint8_t *buffer, size_t size);
61
67typedef int (*retained_mem_clear_api)(const struct device *dev);
68
86
95__syscall ssize_t retained_mem_size(const struct device *dev);
96
97static inline ssize_t z_impl_retained_mem_size(const struct device *dev)
98{
99 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
100
101 return api->size(dev);
102}
103
114__syscall int retained_mem_read(const struct device *dev, off_t offset, uint8_t *buffer,
115 size_t size);
116
117static inline int z_impl_retained_mem_read(const struct device *dev, off_t offset,
118 uint8_t *buffer, size_t size)
119{
120 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
121 size_t area_size;
122
123 /* Validate user-supplied parameters */
124 if (size == 0) {
125 return 0;
126 }
127
128 area_size = api->size(dev);
129
130 if (offset < 0 || size > area_size || (area_size - size) < (size_t)offset) {
131 return -EINVAL;
132 }
133
134 return api->read(dev, offset, buffer, size);
135}
136
148__syscall int retained_mem_write(const struct device *dev, off_t offset, const uint8_t *buffer,
149 size_t size);
150
151static inline int z_impl_retained_mem_write(const struct device *dev, off_t offset,
152 const uint8_t *buffer, size_t size)
153{
154 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
155 size_t area_size;
156
157 /* Validate user-supplied parameters */
158 if (size == 0) {
159 return 0;
160 }
161
162 area_size = api->size(dev);
163
164 if (offset < 0 || size > area_size || (area_size - size) < (size_t)offset) {
165 return -EINVAL;
166 }
167
168 return api->write(dev, offset, buffer, size);
169}
170
178__syscall int retained_mem_clear(const struct device *dev);
179
180static inline int z_impl_retained_mem_clear(const struct device *dev)
181{
182 struct retained_mem_driver_api *api = (struct retained_mem_driver_api *)dev->api;
183
184 return api->clear(dev);
185}
186
191#ifdef __cplusplus
192}
193#endif
194
195#include <zephyr/syscalls/retained_mem.h>
196
197#endif /* ZEPHYR_INCLUDE_DRIVERS_RETAINED_MEM_ */
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_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:51
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:67
int retained_mem_read(const struct device *dev, off_t offset, uint8_t *buffer, size_t size)
Reads data from the Retained memory area.
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_api)(const struct device *dev)
Callback API to get size of retained memory area.
Definition retained_mem.h:44
ssize_t retained_mem_size(const struct device *dev)
Returns the size of the retained memory area.
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:59
#define EINVAL
Invalid argument.
Definition errno.h:60
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition types.h:28
__INTPTR_TYPE__ off_t
Definition types.h:36
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:411
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:417
Retained memory driver API API which can be used by a device to store data in a retained memory area.
Definition retained_mem.h:80
retained_mem_size_api size
Definition retained_mem.h:81
retained_mem_clear_api clear
Definition retained_mem.h:84
retained_mem_read_api read
Definition retained_mem.h:82
retained_mem_write_api write
Definition retained_mem.h:83