Initial Commit
[apps/agl-service-unicens.git] / ucs2-lib / src / ucs_eh.c
1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491                                                                            */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
4 /*                                                                                                */
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.                                                            */
9 /*                                                                                                */
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.                                                   */
14 /*                                                                                                */
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/>.                          */
17 /*                                                                                                */
18 /* You may also obtain this software under a propriety license from Microchip.                    */
19 /* Please contact Microchip for further information.                                              */
20 /*------------------------------------------------------------------------------------------------*/
21
22 /*!
23  * \file
24  * \brief Implementation of the event handler.
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup G_EH
28  * @{
29  */
30
31 /*------------------------------------------------------------------------------------------------*/
32 /* Includes                                                                                       */
33 /*------------------------------------------------------------------------------------------------*/
34 #include "ucs_eh.h"
35 #include "ucs_misc.h"
36
37 /*------------------------------------------------------------------------------------------------*/
38 /* Internal prototypes                                                                            */
39 /*------------------------------------------------------------------------------------------------*/
40 static bool Eh_EncodeEvent(uint32_t event_code, Ucs_Error_t *public_error_code_ptr);
41
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
48  */
49 void Eh_Ctor(CEventHandler *self, void * ucs_user_ptr)
50 {
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);
58 }
59
60 /*! \brief Adds an observer which reports public errors
61  *  \param self        Instance pointer
62  *  \param obs_ptr     Reference to an observer
63  */
64 void Eh_AddObsrvPublicError(CEventHandler *self, CSingleObserver *obs_ptr)
65 {
66     (void)Ssub_AddObserver(&self->public_error_subject, obs_ptr);
67 }
68
69 /*! \brief Removes an observer registered by Eh_AddObsrvPublicError().
70  *  \param self        Instance pointer
71  */
72 void Eh_DelObsrvPublicError(CEventHandler *self)
73 {
74     Ssub_RemoveObserver(&self->public_error_subject);
75 }
76
77 /*! \brief Reports an event to the event handler.
78  *  \param self        Instance pointer
79  *  \param event_code  Event code to report
80  */
81 void Eh_ReportEvent(CEventHandler *self, uint32_t event_code)
82 {
83     Ucs_Error_t public_error_code;
84     /* Check if event code exists */
85     if((event_code & EH_M_ALL_EVENTS) != 0U)
86     {
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? */
92         if(result != false)
93         {
94             Ssub_Notify(&self->public_error_subject, &public_error_code, false);
95         }
96     }
97 }
98
99 /*! \brief  Encodes an internal event code. Some internal event codes are mapped to public
100  *          error codes.
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
104  */
105 static bool Eh_EncodeEvent(uint32_t event_code, Ucs_Error_t *public_error_code_ptr)
106 {
107     bool ret_val = true;
108
109     /* Translate internal event code into public error code */
110     switch(event_code)
111     {
112         case EH_E_BIST_FAILED:
113             *public_error_code_ptr = UCS_GEN_ERR_INIC;
114             break;
115         case EH_E_UNSYNC_COMPLETE:
116         case EH_E_UNSYNC_FAILED:
117             *public_error_code_ptr = UCS_GEN_ERR_COMMUNICATION;
118             break;
119         default:
120             ret_val = false;    /* Do not report this event to application. */
121             break;
122     }
123
124     return ret_val;
125 }
126
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
130  */
131 void Eh_AddObsrvInternalEvent(CEventHandler *self, CMaskedObserver *obs_ptr)
132 {
133     (void)Sub_AddObserver(&self->internal_event_subject, &obs_ptr->parent);
134 }
135
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
139  */
140 void Eh_DelObsrvInternalEvent(CEventHandler *self, CMaskedObserver *obs_ptr)
141 {
142     (void)Sub_RemoveObserver(&self->internal_event_subject, &obs_ptr->parent);
143 }
144
145 /*!
146  * @}
147  * \endcond
148  */
149
150 /*------------------------------------------------------------------------------------------------*/
151 /* End of file                                                                                    */
152 /*------------------------------------------------------------------------------------------------*/
153