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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*------------------------------------------------------------------------------------------------*/
/* 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 Implementation of the observer library module. The module consists of the two classes
* CSubject and CObserver.
*
* \cond UCS_INTERNAL_DOC
* \addtogroup G_OBS
* @{
*/
/*------------------------------------------------------------------------------------------------*/
/* Includes */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_obs.h"
#include "ucs_misc.h"
#include "ucs_trace.h"
/*------------------------------------------------------------------------------------------------*/
/* Internal Prototypes */
/*------------------------------------------------------------------------------------------------*/
static void Sub_UpdateList(CSubject *self);
static bool Sub_CheckObserver(void *current_obs_ptr, void *subject_ptr);
/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CSubject */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the subject class. Initializes a subject which distributes its data to
* a list of observers.
* \param self Instance pointer
* \param ucs_user_ptr User reference that needs to be passed in every callback function
*/
void Sub_Ctor(CSubject *self, void *ucs_user_ptr)
{
MISC_MEM_SET(self, 0, sizeof(*self));
self->ucs_user_ptr = ucs_user_ptr;
Dl_Ctor(&self->list, self->ucs_user_ptr);
Dl_Ctor(&self->add_list, self->ucs_user_ptr);
}
/*! \brief Adds an observer to a subjects list.
* \param self Instance pointer
* \param obs_ptr Pointer to observer instance
* \return \c SUB_OK: No error
* \return \c SUB_ALREADY_ADDED: Observer is already added
* \return \c SUB_UNKNOWN_OBSERVER: Given observer is not valid
*/
Sub_Ret_t Sub_AddObserver(CSubject *self, CObserver *obs_ptr)
{
Sub_Ret_t ret_val;
if(obs_ptr == NULL)
{
ret_val = SUB_UNKNOWN_OBSERVER;
}
else if(obs_ptr->valid != false)
{
ret_val = SUB_ALREADY_ADDED;
}
else if((self->notify != false) &&
(Dl_IsNodeInList(&self->list, &obs_ptr->node) == false) &&
(Dl_IsNodeInList(&self->add_list, &obs_ptr->node) == false))
{
TR_ASSERT(self->ucs_user_ptr, "[OBS]", (self->num_observers < 0xFFU));
Dl_InsertTail(&self->add_list, &obs_ptr->node);
obs_ptr->valid = true;
self->changed = true;
ret_val = SUB_DELAYED;
}
else if((self->notify == false) && (Dl_IsNodeInList(&self->list, &obs_ptr->node) == false))
{
TR_ASSERT(self->ucs_user_ptr, "[OBS]", (self->num_observers < 0xFFU));
ret_val = SUB_OK;
Dl_InsertTail(&self->list, &obs_ptr->node);
obs_ptr->valid = true;
self->num_observers++;
}
else
{
ret_val = SUB_UNKNOWN_OBSERVER;
}
return ret_val;
}
/*! \brief Removes an observer from a subjects list.
* \param self Instance pointer
* \param obs_ptr Pointer to observer instance
* \return \c SUB_OK: No error
* \return \c SUB_UNKNOWN_OBSERVER: Unknown observer is given
* \return \c SUB_UNKNOWN_OBSERVER: Given observer is not valid
*/
Sub_Ret_t Sub_RemoveObserver(CSubject *self, CObserver *obs_ptr)
{
Sub_Ret_t ret_val;
if(obs_ptr == NULL)
{
ret_val = SUB_UNKNOWN_OBSERVER;
}
else if(obs_ptr->valid == false)
{
ret_val = SUB_UNKNOWN_OBSERVER;
}
else if((self->notify != false) &&
(Dl_IsNodeInList(&self->list, &obs_ptr->node) != false))
{
TR_ASSERT(self->ucs_user_ptr, "[OBS]", (self->num_observers > 0U));
obs_ptr->valid = false;
self->changed = true;
self->num_observers--;
ret_val = SUB_DELAYED;
}
else if((self->notify == false) &&
(Dl_Remove(&self->list, &obs_ptr->node) == DL_OK))
{
TR_ASSERT(self->ucs_user_ptr, "[OBS]", (self->num_observers > 0U));
self->num_observers--;
ret_val = SUB_OK;
}
else
{
ret_val = SUB_UNKNOWN_OBSERVER;
}
return ret_val;
}
/*! \brief Notifies all registered observers of a subject.
* \param self Instance pointer
* \param data_ptr Reference to value to distribute (optional)
*/
void Sub_Notify(CSubject *self, void *data_ptr)
{
if(self != NULL)
{
CDlNode *n_tmp = self->list.head;
self->notify = true;
self->changed = false;
while(n_tmp != NULL)
{
CObserver *o_tmp = (CObserver *)n_tmp->data_ptr;
if((o_tmp->update_fptr != NULL) && (o_tmp->valid != false))
{
(o_tmp->update_fptr)(o_tmp->inst_ptr, data_ptr);
}
n_tmp = n_tmp->next;
}
if(self->changed != false)
{
Sub_UpdateList(self);
}
self->notify = false;
}
}
/*! \brief Updates the list of observers. Delayed remove- and add-operations are processed.
* \param self Instance pointer
*/
static void Sub_UpdateList(CSubject *self)
{
(void)Dl_Foreach(&self->list, &Sub_CheckObserver, self);
Dl_AppendList(&self->list, &self->add_list);
}
/*! \brief Checks if the given observer is still valid. If the observer is invalid it will be
* removed from the list. This function is used by the foreach loop in Sub_UpdateList().
* \param current_obs_ptr Reference to the current observer object
* \param subject_ptr Reference to the subject object
* \return Returns always \c false. Force to process the whole list.
*/
static bool Sub_CheckObserver(void *current_obs_ptr, void *subject_ptr)
{
CObserver *current_obs_ptr_ = (CObserver *)current_obs_ptr;
CSubject *subject_ptr_ = (CSubject *)subject_ptr;
if(current_obs_ptr_->valid == false)
{
(void)Dl_Remove(&subject_ptr_->list, ¤t_obs_ptr_->node);
}
return false;
}
/*! \brief Returns the number of registered observers of a subject.
* \param self Instance pointer
* \return The number of registered observers
*/
uint8_t Sub_GetNumObservers(CSubject *self)
{
return self->num_observers;
}
/*! \brief Switches all observers of the source-subject to the target-subject.
* \param sub_target Target subject
* \param sub_source Source subject
* \return \c SUB_OK: No error
* \return \c SUB_INVALID_OPERATION: Target and source must be different objects
*/
Sub_Ret_t Sub_SwitchObservers(CSubject *sub_target, CSubject *sub_source)
{
Sub_Ret_t ret_val;
if(sub_target == sub_source)
{
ret_val = SUB_INVALID_OPERATION;
}
else
{
Dl_AppendList(&sub_target->list, &sub_source->list);
sub_target->num_observers += sub_source->num_observers;
sub_source->num_observers = 0U;
ret_val = SUB_OK;
}
return ret_val;
}
/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CObserver */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the observer class. Initializes an observer which is notified
* by a corresponding subject.
* \param self Instance pointer
* \param inst_ptr Instance pointer used by update_fptr()
* \param update_fptr Callback function to update the observer
*/
void Obs_Ctor(CObserver *self, void *inst_ptr, Obs_UpdateCb_t update_fptr)
{
MISC_MEM_SET(self, 0, sizeof(*self));
self->inst_ptr = inst_ptr;
self->update_fptr = update_fptr;
Dln_Ctor(&self->node, self);
}
/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CSingleSubject */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the single-subject class. Initializes a single-subject which distributes
* its data to the registered single-observer.
* \param self Instance pointer
* \param ucs_user_ptr User reference that needs to be passed in every callback function
*/
void Ssub_Ctor(CSingleSubject *self, void *ucs_user_ptr)
{
self->observer_ptr = NULL;
self->ucs_user_ptr = ucs_user_ptr;
self->user_mask = 0U;
}
/*! \brief Adds a single-observer to a single-subject.
* \param self Instance pointer
* \param obs_ptr Pointer to single-observer instance
* \return \c SSUB_OK: No error
* \return \c SSUB_ALREADY_ADDED: Observer is already added
* \return \c SSUB_UNKNOWN_OBSERVER: Given observer is not valid
*/
Ssub_Ret_t Ssub_AddObserver(CSingleSubject *self, CSingleObserver *obs_ptr)
{
Ssub_Ret_t ret_val;
if(obs_ptr == NULL)
{
ret_val = SSUB_UNKNOWN_OBSERVER;
}
else if(self->observer_ptr != obs_ptr)
{
#ifdef UCS_TR_INFO
if(self->observer_ptr != NULL)
{
TR_INFO((self->ucs_user_ptr, "[SSUB]", "Observer callback has been overwritten", 0U));
}
#endif
ret_val = SSUB_OK;
self->observer_ptr = obs_ptr;
}
else
{
ret_val = SSUB_ALREADY_ADDED;
}
return ret_val;
}
/*! \brief Removes an single-observer from a single-subject.
* \param self Instance pointer
*/
void Ssub_RemoveObserver(CSingleSubject *self)
{
self->observer_ptr = NULL;
}
/*! \brief Notifies the registered single-observer of the given single-subject.
* \param self Instance pointer
* \param data_ptr Reference to value to distribute (optional)
* \param auto_remove If true the observer will be removed
*/
void Ssub_Notify(CSingleSubject *self, void *data_ptr, bool auto_remove)
{
void *inst_ptr = NULL;
Obs_UpdateCb_t update_fptr = NULL;
if(self->observer_ptr != NULL)
{
inst_ptr = self->observer_ptr->inst_ptr;
update_fptr = self->observer_ptr->update_fptr;
if(auto_remove != false)
{
self->observer_ptr = NULL;
}
}
if(update_fptr != NULL)
{
update_fptr(inst_ptr, data_ptr);
}
}
/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CSingleObserver */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the single-observer class. Initializes an single-observer which is
* notified by a corresponding single-subject.
* \param self Instance pointer
* \param inst_ptr Instance pointer used by update_fptr()
* \param update_fptr Callback function to update the observer
*/
void Sobs_Ctor(CSingleObserver *self, void *inst_ptr, Sobs_UpdateCb_t update_fptr)
{
self->inst_ptr = inst_ptr;
self->update_fptr = update_fptr;
}
/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CMaskedObserver */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the masked-observer class. Initializes an observer which is notified
* by a corresponding subject.
* \param self Instance pointer
* \param inst_ptr Instance pointer used by update_fptr()
* \param notification_mask Notification bitmask
* \param update_fptr Callback function to update the observer
*/
void Mobs_Ctor(CMaskedObserver *self,
void *inst_ptr,
uint32_t notification_mask,
Obs_UpdateCb_t update_fptr)
{
MISC_MEM_SET(self, 0, sizeof(*self));
Obs_Ctor(&self->parent, inst_ptr, update_fptr);
self->notification_mask = notification_mask;
}
/*! \brief Sets the notification mask of a masked-observer.
* \param self Instance pointer
* \param mask Bitmask to set
*/
void Mobs_SetNotificationMask(CMaskedObserver *self, uint32_t mask)
{
self->notification_mask = mask;
}
/*! \brief Retrieves the notification mask of a masked-observer.
* \param self Instance pointer
* \return Returns the current notification bitmask of the given observer
*/
uint32_t Mobs_GetNotificationMask(CMaskedObserver *self)
{
return self->notification_mask;
}
/*------------------------------------------------------------------------------------------------*/
/* Additional methods of class CSubject used in combination with CMaskedObserver */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Adds an masked-observer to a masked-subjects list.
* \param self Instance pointer
* \param obs_ptr Pointer to observer instance
* \return \c SUB_OK: No error
* \return \c SUB_ALREADY_ADDED: Observer is already added
* \return \c SUB_UNKNOWN_OBSERVER: Given observer is not valid
*/
Sub_Ret_t Msub_AddObserver(CSubject *self, CMaskedObserver *obs_ptr)
{
return Sub_AddObserver(self, &obs_ptr->parent);
}
/*! \brief Removes an masked-observer from a subjects list.
* \param self Instance pointer
* \param obs_ptr Pointer to observer instance
* \return \c SUB_OK: No error
* \return \c SUB_UNKNOWN_OBSERVER: Unknown observer is given
* \return \c SUB_UNKNOWN_OBSERVER: Given observer is not valid
*/
Sub_Ret_t Msub_RemoveObserver(CSubject *self, CMaskedObserver *obs_ptr)
{
return Sub_RemoveObserver(self, &obs_ptr->parent);
}
/*! \brief Notifies all registered masked-observers of a masked-subject.
* \param self Instance pointer
* \param data_ptr Reference to value to distribute (optional)
* \param notification_mask Bitmask indicates notified observers
*/
void Msub_Notify(CSubject *self, void *data_ptr, uint32_t notification_mask)
{
if(self != NULL)
{
CDlNode *n_tmp = self->list.head;
self->notify = true;
self->changed = false;
while(n_tmp != NULL)
{
CMaskedObserver *o_tmp = (CMaskedObserver *)n_tmp->data_ptr;
if( (o_tmp->parent.update_fptr != NULL) &&
(o_tmp->parent.valid != false) &&
((o_tmp->notification_mask & notification_mask) != 0U) )
{
(o_tmp->parent.update_fptr)(o_tmp->parent.inst_ptr, data_ptr);
}
n_tmp = n_tmp->next;
}
if(self->changed != false)
{
Sub_UpdateList(self);
}
self->notify = false;
}
}
/*!
* @}
* \endcond
*/
/*------------------------------------------------------------------------------------------------*/
/* End of file */
/*------------------------------------------------------------------------------------------------*/
|