1 /*
2 * Copyright (c) 2019 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef ZEPHYR_FAKE_DRIVER_H
8 #define ZEPHYR_FAKE_DRIVER_H
9
10 #include <device.h>
11
12 #define SAMPLE_DRIVER_NAME_0 "SAMPLE_DRIVER_0"
13 #define SAMPLE_DRIVER_MSG_SIZE 128
14
15 typedef void (*sample_driver_callback_t)(const struct device *dev,
16 void *context, void *data);
17
18 typedef int (*sample_driver_write_t)(const struct device *dev, void *buf);
19
20 typedef int (*sample_driver_set_callback_t)(const struct device *dev,
21 sample_driver_callback_t cb,
22 void *context);
23
24 typedef int (*sample_driver_state_set_t)(const struct device *dev,
25 bool active);
26
27 __subsystem struct sample_driver_api {
28 sample_driver_write_t write;
29 sample_driver_set_callback_t set_callback;
30 sample_driver_state_set_t state_set;
31 };
32
33 /*
34 * Write some processed data to the sample driver
35 *
36 * Having done some processing on data received in the sample driver callback,
37 * write this processed data back to the driver.
38 *
39 * @param dev Sample driver device
40 * @param buf Processed data, of size SAMPLE_DRIVER_MSG_SIZE
41 * @return 0 Success, nonzero if an error occurred
42 */
43 __syscall int sample_driver_write(const struct device *dev, void *buf);
44
z_impl_sample_driver_write(const struct device * dev,void * buf)45 static inline int z_impl_sample_driver_write(const struct device *dev,
46 void *buf)
47 {
48 const struct sample_driver_api *api = dev->api;
49
50 return api->write(dev, buf);
51 }
52
53 /*
54 * Set whether the sample driver will respond to interrupts
55 *
56 * @param dev Sample driver device
57 * @param active Whether to activate/deactivate interrupts
58 */
59 __syscall int sample_driver_state_set(const struct device *dev, bool active);
60
z_impl_sample_driver_state_set(const struct device * dev,bool active)61 static inline int z_impl_sample_driver_state_set(const struct device *dev,
62 bool active)
63 {
64 const struct sample_driver_api *api = dev->api;
65
66 return api->state_set(dev, active);
67 }
68
69 /*
70 * Register a callback function for the sample driver
71 *
72 * This callback runs in interrupt context. The provided data
73 * blob will be of size SAMPLE_DRIVER_MSG_SIZE.
74 *
75 * @param dev Sample driver device to install callabck
76 * @param cb Callback function pointer
77 * @param context Context passed to callback function, or NULL if not needed
78 * @return 0 Success, nonzero if an error occurred
79 */
sample_driver_set_callback(const struct device * dev,sample_driver_callback_t cb,void * context)80 static inline int sample_driver_set_callback(const struct device *dev,
81 sample_driver_callback_t cb,
82 void *context)
83 {
84 const struct sample_driver_api *api = dev->api;
85
86 return api->set_callback(dev, cb, context);
87 }
88
89 #include <syscalls/sample_driver.h>
90
91 #endif
92