summaryrefslogtreecommitdiffstats
path: root/ucs2-lib/inc/ucs_obs.h
blob: 7ef2d839be0769ad0a8f583da75451237b658d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491                                                                            */
/* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
/*                                                                                                */
/* This program is free software: you can redistribute it and/or modify                           */
/* it under the terms of the GNU General Public License as published by                           */
/* the Free Software Foundation, either version 2 of the License, or                              */
/* (at your option) any later version.                                                            */
/*                                                                                                */
/* This program is distributed in the hope that it will be useful,                                */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
/* GNU General Public License for more details.                                                   */
/*                                                                                                */
/* You should have received a copy of the GNU General Public License                              */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
/*                                                                                                */
/* You may also obtain this software under a propriety license from Microchip.                    */
/* Please contact Microchip for further information.                                              */
/*------------------------------------------------------------------------------------------------*/

/*!
 * \file
 * \brief Internal header file of the observer library module. The module consists of the two
 *        classes CSubject and CObserver.
 *
 * \cond UCS_INTERNAL_DOC
 * \addtogroup G_OBS
 * @{
 */

#ifndef UCS_OBS_H
#define UCS_OBS_H

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_types_cfg.h"
#include "ucs_dl.h"
#include "ucs_ret_pb.h"

#ifdef __cplusplus
extern "C"
{
#endif

/*------------------------------------------------------------------------------------------------*/
/* Type definitions                                                                               */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Function signature used for callback functions which notifies the observers.
 *  \param self     Instance pointer
 *  \param data_ptr Reference to optional data
 */
typedef void (*Obs_UpdateCb_t)(void *self, void *data_ptr);

/*! \brief Function signature used for callback functions which notifies the single-observers.
 *  \param self     Instance pointer
 *  \param data_ptr Reference to optional data
 */
typedef void (*Sobs_UpdateCb_t)(void *self, void *data_ptr);

/*------------------------------------------------------------------------------------------------*/
/* Enumerators                                                                                    */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Standard return values of the subject class. */
typedef enum Sub_Ret_
{
    SUB_OK,                 /*!< \brief No error */
    SUB_DELAYED,            /*!< \brief Operation is queued since notification is still active */
    SUB_ALREADY_ADDED,      /*!< \brief Observer already added */
    SUB_UNKNOWN_OBSERVER,   /*!< \brief Unknown observer */
    SUB_INVALID_OPERATION   /*!< \brief Invalid operation */

} Sub_Ret_t;

/*! \brief Standard return values of the single-subject class. */
typedef enum Ssub_Ret_
{
    SSUB_OK,                /*!< \brief No error */
    SSUB_ALREADY_ADDED,     /*!< \brief Observer already added */
    SSUB_UNKNOWN_OBSERVER   /*!< \brief Unknown observer */

} Ssub_Ret_t;

/*------------------------------------------------------------------------------------------------*/
/* Structures                                                                                     */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Class structure of observers which are notified by subjects. */
typedef struct CObserver_
{
    CDlNode node;               /*!< \brief Node element to be able to add observer to list */
    void *inst_ptr;             /*!< \brief Reference to instance used by update_fptr() */
    Obs_UpdateCb_t update_fptr; /*!< \brief Callback function to update the observer */
    bool valid;                 /*!< \brief Used for queued remove operation */

} CObserver;

/*! \brief Class structure of subjects. */
typedef struct CSubject_
{
    CDlList list;               /*!< \brief Doubly linked list to manage observers */
    CDlList add_list;           /*!< \brief List to manage delayed add operations */
    uint8_t num_observers;      /*!< \brief Number of added observers */
    bool notify;                /*!< \brief Signals that the notification is in progress */
    bool changed;               /*!< \brief Signals that an add- or a remove-operation
                                            has been queued */
    void *ucs_user_ptr;         /*!< \brief User reference that needs to be passed in every callback function */

} CSubject;

/*! \brief Class structure of a single-observer which is notified by a single-subject. */
typedef struct CSingleObserver_
{
    void *inst_ptr;             /*!< \brief Reference to instance used by update_fptr() */
    Obs_UpdateCb_t update_fptr; /*!< \brief Callback function to update the observer */

} CSingleObserver;

/*! \brief Class structure of a single-subject. */
typedef struct CSingleSubject_
{
    CSingleObserver *observer_ptr;  /*!< \brief Reference to the assigned single-observer */
    void *ucs_user_ptr;         /*!< \brief User reference that needs to be passed in every callback function */
    uint32_t user_mask;             /*!< \brief Current user mask to the single observer */

} CSingleSubject;

/*! \brief Class structure of masked observers which are notified by subjects. */
typedef struct CMaskedObserver_
{
    CObserver parent;               /*!< \brief Parent class instance */
    uint32_t notification_mask;     /*!< \brief Notification bitmask */

} CMaskedObserver;

/*------------------------------------------------------------------------------------------------*/
/* Prototypes of class CSubject                                                                   */
/*------------------------------------------------------------------------------------------------*/
extern void Sub_Ctor(CSubject *self, void *ucs_user_ptr);
extern Sub_Ret_t Sub_AddObserver(CSubject *self, CObserver *obs_ptr);
extern Sub_Ret_t Sub_RemoveObserver(CSubject *self, CObserver *obs_ptr);
extern void Sub_Notify(CSubject *self, void *data_ptr);
extern uint8_t Sub_GetNumObservers(CSubject *self);
extern Sub_Ret_t Sub_SwitchObservers(CSubject *sub_target, CSubject *sub_source);

/*------------------------------------------------------------------------------------------------*/
/* Prototypes of class CObserver                                                                  */
/*------------------------------------------------------------------------------------------------*/
extern void Obs_Ctor(CObserver *self, void *inst_ptr, Obs_UpdateCb_t update_fptr);

/*------------------------------------------------------------------------------------------------*/
/* Prototypes of class CSingleSubject                                                             */
/*------------------------------------------------------------------------------------------------*/
extern void Ssub_Ctor(CSingleSubject *self, void *ucs_user_ptr);
extern Ssub_Ret_t Ssub_AddObserver(CSingleSubject *self, CSingleObserver *obs_ptr);
extern void Ssub_RemoveObserver(CSingleSubject *self);
extern void Ssub_Notify(CSingleSubject *self, void *data_ptr, bool auto_remove);

/*------------------------------------------------------------------------------------------------*/
/* Prototypes of class CSingleObserver                                                            */
/*------------------------------------------------------------------------------------------------*/
extern void Sobs_Ctor(CSingleObserver *self, void *inst_ptr, Sobs_UpdateCb_t update_fptr);

/*------------------------------------------------------------------------------------------------*/
/* Prototypes of class CMaskedObserver                                                            */
/*------------------------------------------------------------------------------------------------*/
extern void Mobs_Ctor(CMaskedObserver *self,
                      void *inst_ptr,
                      uint32_t notification_mask,
                      Obs_UpdateCb_t update_fptr);
extern void Mobs_SetNotificationMask(CMaskedObserver *self, uint32_t mask);
extern uint32_t Mobs_GetNotificationMask(CMaskedObserver *self);

/*------------------------------------------------------------------------------------------------*/
/* Additional prototypes of class CSubject used in combination with CMaskedObserver               */
/*------------------------------------------------------------------------------------------------*/
extern Sub_Ret_t Msub_AddObserver(CSubject *self, CMaskedObserver *obs_ptr);
extern Sub_Ret_t Msub_RemoveObserver(CSubject *self, CMaskedObserver *obs_ptr);
extern void Msub_Notify(CSubject *self, void *data_ptr, uint32_t notification_mask);

#ifdef __cplusplus
}   /* extern "C" */
#endif

#endif  /* #ifndef UCS_OBS_H */

/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/