Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
onoff.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Peter Bigot Consulting, LLC
3 * Copyright (c) 2020 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_SYS_ONOFF_H_
9#define ZEPHYR_INCLUDE_SYS_ONOFF_H_
10
11#include <kernel.h>
12#include <zephyr/types.h>
13#include <sys/notify.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
30#define ONOFF_FLAG_ERROR BIT(0)
31
33#define ONOFF_FLAG_ONOFF BIT(1)
35#define ONOFF_FLAG_TRANSITION BIT(2)
36
45#define ONOFF_STATE_MASK (ONOFF_FLAG_ERROR \
46 | ONOFF_FLAG_ONOFF \
47 | ONOFF_FLAG_TRANSITION)
48
52#define ONOFF_STATE_OFF 0U
53
57#define ONOFF_STATE_ON ONOFF_FLAG_ONOFF
58
63#define ONOFF_STATE_ERROR ONOFF_FLAG_ERROR
64
69#define ONOFF_STATE_TO_ON (ONOFF_FLAG_TRANSITION | ONOFF_STATE_ON)
70
75#define ONOFF_STATE_TO_OFF (ONOFF_FLAG_TRANSITION | ONOFF_STATE_OFF)
76
81#define ONOFF_STATE_RESETTING (ONOFF_FLAG_TRANSITION | ONOFF_STATE_ERROR)
82
83/* Forward declarations */
84struct onoff_manager;
85struct onoff_monitor;
86
101typedef void (*onoff_notify_fn)(struct onoff_manager *mgr,
102 int res);
103
130 onoff_notify_fn notify);
131
134 /* Function to invoke to transition the service to on. */
136
137 /* Function to invoke to transition the service to off. */
139
140 /* Function to force the service state to reset, where
141 * supported.
142 */
144};
145
155 /* List of clients waiting for request or reset completion
156 * notifications.
157 */
159
160 /* List of monitors to be notified of state changes including
161 * errors and transition completion.
162 */
164
165 /* Transition functions. */
167
168 /* Mutex protection for other fields. */
170
171 /* The result of the last transition. */
173
174 /* Flags identifying the service state. */
176
177 /* Number of active clients for the service. */
179};
180
190#define ONOFF_TRANSITIONS_INITIALIZER(_start, _stop, _reset) { \
191 .start = _start, \
192 .stop = _stop, \
193 .reset = _reset, \
194}
195
197#define ONOFF_MANAGER_INITIALIZER(_transitions) { \
198 .transitions = _transitions, \
199}
200
221 const struct onoff_transitions *transitions);
222
223/* Forward declaration */
224struct onoff_client;
225
252 struct onoff_client *cli,
254 int res);
255
281
284};
285
298#define ONOFF_CLIENT_EXTENSION_POS SYS_NOTIFY_EXTENSION_POS
299
313static inline bool onoff_has_error(const struct onoff_manager *mgr)
314{
315 return (mgr->flags & ONOFF_FLAG_ERROR) != 0;
316}
317
346 struct onoff_client *cli);
347
367
400 struct onoff_client *cli);
401
433static inline int onoff_cancel_or_release(struct onoff_manager *mgr,
434 struct onoff_client *cli)
435{
436 int rv = onoff_cancel(mgr, cli);
437
438 if (rv == -EALREADY) {
439 rv = onoff_release(mgr);
440 }
441 return rv;
442}
443
480 struct onoff_client *cli);
481
511 struct onoff_monitor *mon,
513 int res);
514
523 /* Links the client into the set of waiting service users.
524 *
525 * This must be zero-initialized.
526 */
528
534};
535
548 struct onoff_monitor *mon);
549
562 struct onoff_monitor *mon);
563
575 /* Mutex protection for other fields. */
577
578 /* Negative is error, non-negative is reference count. */
580};
581
599 k_spinlock_key_t *keyp);
600
635 struct onoff_client *cli,
636 int res,
637 bool on);
638
641#ifdef __cplusplus
642}
643#endif
644
645#endif /* ZEPHYR_INCLUDE_SYS_ONOFF_H_ */
void
Definition: eswifi_shell.c:15
volatile int rv
Definition: main.c:45
int onoff_sync_lock(struct onoff_sync_service *srv, k_spinlock_key_t *keyp)
Lock a synchronous onoff service and provide its state.
int onoff_release(struct onoff_manager *mgr)
Release a reserved use of an on-off service.
int onoff_request(struct onoff_manager *mgr, struct onoff_client *cli)
Request a reservation to use an on-off service.
static bool onoff_has_error(const struct onoff_manager *mgr)
Test whether an on-off service has recorded an error.
Definition: onoff.h:313
void(* onoff_client_callback)(struct onoff_manager *mgr, struct onoff_client *cli, uint32_t state, int res)
Signature used to notify an on-off service client of the completion of an operation.
Definition: onoff.h:251
int onoff_monitor_unregister(struct onoff_manager *mgr, struct onoff_monitor *mon)
Remove a monitor of state changes from a manager.
void(* onoff_transition_fn)(struct onoff_manager *mgr, onoff_notify_fn notify)
Signature used by service implementations to effect a transition.
Definition: onoff.h:129
int onoff_manager_init(struct onoff_manager *mgr, const struct onoff_transitions *transitions)
Initialize an on-off service to off state.
static int onoff_cancel_or_release(struct onoff_manager *mgr, struct onoff_client *cli)
Helper function to safely cancel a request.
Definition: onoff.h:433
int onoff_monitor_register(struct onoff_manager *mgr, struct onoff_monitor *mon)
Add a monitor of state changes for a manager.
#define ONOFF_FLAG_ERROR
Flag indicating an error state.
Definition: onoff.h:30
void(* onoff_notify_fn)(struct onoff_manager *mgr, int res)
Signature used to notify an on-off manager that a transition has completed.
Definition: onoff.h:101
int onoff_cancel(struct onoff_manager *mgr, struct onoff_client *cli)
Attempt to cancel an in-progress client operation.
int onoff_sync_finalize(struct onoff_sync_service *srv, k_spinlock_key_t key, struct onoff_client *cli, int res, bool on)
Process the completion of a transition in a synchronous service and release lock.
void(* onoff_monitor_callback)(struct onoff_manager *mgr, struct onoff_monitor *mon, uint32_t state, int res)
Signature used to notify a monitor of an onoff service of errors or completion of a state transition.
Definition: onoff.h:510
int onoff_reset(struct onoff_manager *mgr, struct onoff_client *cli)
Clear errors on an on-off service and reset it to its off state.
#define EALREADY
Definition: errno.h:105
state
Definition: http_parser_state.h:30
struct _slist sys_slist_t
Definition: slist.h:40
struct _snode sys_snode_t
Definition: slist.h:33
struct z_spinlock_key k_spinlock_key_t
Spinlock key type.
Definition: spinlock.h:87
static k_spinlock_key_t key
Definition: spinlock_error_case.c:14
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__INT32_TYPE__ int32_t
Definition: stdint.h:44
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Kernel Spin Lock.
Definition: spinlock.h:29
State associated with a client of an on-off service.
Definition: onoff.h:273
sys_snode_t node
Definition: onoff.h:280
struct sys_notify notify
Notification configuration.
Definition: onoff.h:283
State associated with an on-off manager.
Definition: onoff.h:154
sys_slist_t clients
Definition: onoff.h:158
uint16_t refs
Definition: onoff.h:178
sys_slist_t monitors
Definition: onoff.h:163
uint16_t flags
Definition: onoff.h:175
int last_res
Definition: onoff.h:172
const struct onoff_transitions * transitions
Definition: onoff.h:166
struct k_spinlock lock
Definition: onoff.h:169
Registration state for notifications of onoff service transitions.
Definition: onoff.h:522
sys_snode_t node
Definition: onoff.h:527
onoff_monitor_callback callback
Callback to be invoked on state change.
Definition: onoff.h:533
State used when a driver uses the on-off service API for synchronous operations.
Definition: onoff.h:574
struct k_spinlock lock
Definition: onoff.h:576
int32_t count
Definition: onoff.h:579
On-off service transition functions.
Definition: onoff.h:133
onoff_transition_fn reset
Definition: onoff.h:143
onoff_transition_fn start
Definition: onoff.h:135
onoff_transition_fn stop
Definition: onoff.h:138
State associated with notification for an asynchronous operation.
Definition: notify.h:138