1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491 */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG. */
5 /* This program is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 2 of the License, or */
8 /* (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* You may also obtain this software under a propriety license from Microchip. */
19 /* Please contact Microchip for further information. */
20 /*------------------------------------------------------------------------------------------------*/
24 * \brief Implementation of the event handler.
26 * \cond UCS_INTERNAL_DOC
31 /*------------------------------------------------------------------------------------------------*/
33 /*------------------------------------------------------------------------------------------------*/
37 /*------------------------------------------------------------------------------------------------*/
38 /* Internal prototypes */
39 /*------------------------------------------------------------------------------------------------*/
40 static bool Eh_EncodeEvent(uint32_t event_code, Ucs_Error_t *public_error_code_ptr);
42 /*------------------------------------------------------------------------------------------------*/
43 /* Implementation of class CEventHandler */
44 /*------------------------------------------------------------------------------------------------*/
45 /*! \brief Constructor of the event handler class.
46 * \param self Instance pointer
47 * \param ucs_user_ptr User reference that needs to be passed in every callback function
49 void Eh_Ctor(CEventHandler *self, void * ucs_user_ptr)
51 MISC_MEM_SET(self, 0, sizeof(*self));
52 /* Save UNICENS instance ID */
53 self->ucs_user_ptr = ucs_user_ptr;
54 /* Initialize subject for internal events */
55 Sub_Ctor(&self->internal_event_subject, self->ucs_user_ptr);
56 /* Initialize subject for public error reporting */
57 Ssub_Ctor(&self->public_error_subject, self->ucs_user_ptr);
60 /*! \brief Adds an observer which reports public errors
61 * \param self Instance pointer
62 * \param obs_ptr Reference to an observer
64 void Eh_AddObsrvPublicError(CEventHandler *self, CSingleObserver *obs_ptr)
66 (void)Ssub_AddObserver(&self->public_error_subject, obs_ptr);
69 /*! \brief Removes an observer registered by Eh_AddObsrvPublicError().
70 * \param self Instance pointer
72 void Eh_DelObsrvPublicError(CEventHandler *self)
74 Ssub_RemoveObserver(&self->public_error_subject);
77 /*! \brief Reports an event to the event handler.
78 * \param self Instance pointer
79 * \param event_code Event code to report
81 void Eh_ReportEvent(CEventHandler *self, uint32_t event_code)
83 Ucs_Error_t public_error_code;
84 /* Check if event code exists */
85 if((event_code & EH_M_ALL_EVENTS) != 0U)
87 /* Encode internal event code */
88 bool result = Eh_EncodeEvent(event_code, &public_error_code);
89 /* Notify all registered observers */
90 Msub_Notify(&self->internal_event_subject, &event_code, event_code);
91 /* Report error to application? */
94 Ssub_Notify(&self->public_error_subject, &public_error_code, false);
99 /*! \brief Encodes an internal event code. Some internal event codes are mapped to public
101 * \param event_code Internal event code to report
102 * \param public_error_code_ptr Returned public error code
103 * \return true if error must be reported to the application, otherwise false
105 static bool Eh_EncodeEvent(uint32_t event_code, Ucs_Error_t *public_error_code_ptr)
109 /* Translate internal event code into public error code */
112 case EH_E_BIST_FAILED:
113 *public_error_code_ptr = UCS_GEN_ERR_INIC;
115 case EH_E_UNSYNC_COMPLETE:
116 case EH_E_UNSYNC_FAILED:
117 *public_error_code_ptr = UCS_GEN_ERR_COMMUNICATION;
120 ret_val = false; /* Do not report this event to application. */
127 /*! \brief Registers an observer on the given event code.
128 * \param self Instance pointer
129 * \param obs_ptr Reference to the masked-observer object
131 void Eh_AddObsrvInternalEvent(CEventHandler *self, CMaskedObserver *obs_ptr)
133 (void)Sub_AddObserver(&self->internal_event_subject, &obs_ptr->parent);
136 /*! \brief Unregisters the given observer from the given event code.
137 * \param self Instance pointer
138 * \param obs_ptr Reference to the masked-observer object
140 void Eh_DelObsrvInternalEvent(CEventHandler *self, CMaskedObserver *obs_ptr)
142 (void)Sub_RemoveObserver(&self->internal_event_subject, &obs_ptr->parent);
150 /*------------------------------------------------------------------------------------------------*/
152 /*------------------------------------------------------------------------------------------------*/