EVE 1.0
event-monitor.h
Go to the documentation of this file.
1 #ifndef EVE_EVENT_MONITOR_H_INCLUDED
2 #define EVE_EVENT_MONITOR_H_INCLUDED
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2016, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33  /**
34  * \file
35  * \brief Header file for the EVE event monitor primitive.
36  *
37  * \author DT, Jetro AS
38  */ /******************************************************************/
39 
40 #include <stdbool.h>
41 #include <core/pm.h>
42 #include <lib/dlist.h>
43 #include <lib/env.h>
44 #include <nrf_soc.h>
45 
46 /**
47  * \addtogroup eventmon Event monitor
48  * \{
49  * EVE event monitor primitive
50  */
51 
52 /**********************************************************************/
53 /**
54  * Event monitor structure.
55  *
56  */
58 {
59  struct dlist_t Link;
60  void (*OnSysEvent)(struct event_monitor_t *Self, uint32_t Event);
61 };
62 
63 #define EVENT_MONITOR_INIT(name, on_sys_event) \
64  { \
65  .Link = DLIST_INIT(name.Link), \
66  .OnSysEvent = on_sys_event, \
67  }
68 
69 #define DECLARE_EVENT_MONITOR(name, on_sys_event) \
70  struct event_monitor_t name \
71  __attribute__((cleanup(event_monitor_destructor))) = \
72  EVENT_MONITOR_INIT(name, on_sys_event); \
73  do { \
74  event_monitor_register(&name); \
75  } while (0)
76 
77 void event_monitor_register(struct event_monitor_t *Monitor);
78 
79 void event_monitor_unregister(struct event_monitor_t *Monitor);
80 
81 void event_monitor_on_sys_event(uint32_t Event);
82 
83 /**********************************************************************/
84 /**
85  * Event monitor destructor.
86  *
87  */
88 static inline void event_monitor_destructor(struct event_monitor_t *Self)
89 {
90  event_monitor_unregister(Self);
91 }
92 
93 /**********************************************************************/
94 /**
95  * Flash operation monitor.
96  *
97  */
99 {
100  struct event_monitor_t Super;
101  bool Status;
102  bool Done;
103 };
104 
105 #define FLASH_OP_MONITOR_INIT(name) \
106  { \
107  .Super = EVENT_MONITOR_INIT(name.Super, \
108  flash_op_monitor_on_sys_event), \
109  }
110 
111 #define DECLARE_FLASH_OP_MONITOR(name) \
112  struct flash_op_monitor_t name \
113  __attribute__((cleanup(flash_op_monitor_destructor))) = \
114  FLASH_OP_MONITOR_INIT(name); \
115  do { \
116  event_monitor_register(&name.Super); \
117  } while (0)
118 
119 #define FLASH_OP_WAIT(name) \
120  do { \
121  while (!name.Done) \
122  pm_relax(); \
123  } while (0)
124 
125 static inline void flash_op_monitor_destructor(struct flash_op_monitor_t *Self)
126 {
127  event_monitor_destructor(&Self->Super);
128 }
129 
130 static inline void flash_op_monitor_on_sys_event(struct event_monitor_t *Super, uint32_t Event)
131 {
132  struct flash_op_monitor_t *Self =
133  container_of(Super, struct flash_op_monitor_t, Super);
134 
135  if (Event == NRF_EVT_FLASH_OPERATION_SUCCESS || Event == NRF_EVT_FLASH_OPERATION_ERROR)
136  {
137  Self->Status = (Event == NRF_EVT_FLASH_OPERATION_SUCCESS);
138  Self->Done = true;
139  pm_wakeup();
140  }
141 }
142 
143 /** @} */ /* eventmon */
144 
145 #endif /* EVE_EVENT_MONITOR_H_INCLUDED */
static void event_monitor_destructor(struct event_monitor_t *Self)
Definition: event-monitor.h:88
#define container_of(ptr, type, mem)
Definition: env.h:114
Header file for the EVE power management framework.
EVE build environment.
The code implements Dummy Headed Doubly Linked Circularlist (DHDLC) primitive.
Definition: dlist.h:66
void pm_wakeup(void)