116253bff142b4c4025a3bc49e84531ac68e977d
[apps/agl-service-unicens.git] / ucs2-lib / src / ucs_xrmpool.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 Connection Storage Pool.
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup G_UCS_XRM_INT
28  * @{
29  */
30
31 /*------------------------------------------------------------------------------------------------*/
32 /* Includes                                                                                       */
33 /*------------------------------------------------------------------------------------------------*/
34 #include "ucs_xrmpool.h"
35 #include "ucs_xrm_pv.h"
36
37 /*------------------------------------------------------------------------------------------------*/
38 /* Implementation of class XrmPool                                                                */
39 /*------------------------------------------------------------------------------------------------*/
40 /*------------------------------------------------------------------------------------------------*/
41 /* Initialization Methods                                                                         */
42 /*------------------------------------------------------------------------------------------------*/
43 /*! \brief Constructor of the XrmPool class.
44  *  \param self        Instance pointer
45  */
46 void Xrmp_Ctor(CXrmPool * self)
47 {
48     uint8_t i;
49     MISC_MEM_SET(self, 0, sizeof(CXrmPool));
50
51     /* Initialize resource handle list */
52     for(i=0U; i<XRM_NUM_RESOURCE_HANDLES; i++)
53     {
54         self->resource_handle_list[i].resource_handle       = XRM_INVALID_RESOURCE_HANDLE;
55         self->resource_handle_list[i].job_ptr               = NULL;
56         self->resource_handle_list[i].resource_object_ptr   = NULL;
57     }
58 }
59
60 /*------------------------------------------------------------------------------------------------*/
61 /* Service                                                                                        */
62 /*------------------------------------------------------------------------------------------------*/
63 /*! \brief  Stores the given resource handle in the resource handle list.
64  *  \param  self_ptr            XrmPool Instance pointer
65  *  \param  resource_handle     Resource handle to save
66  *  \param  job_ptr             Reference to job
67  *  \param  resource_object_ptr Reference to resource object
68  *  \return \c true if free slot in handle list was found, otherwise \c false
69  */
70 bool Xrmp_StoreResourceHandle(CXrmPool * self_ptr, uint16_t resource_handle, Xrm_Job_t * job_ptr, UCS_XRM_CONST Ucs_Xrm_ResObject_t * resource_object_ptr)
71 {
72     bool ret_val = false;
73     uint8_t i;
74
75     for(i=0U; i<XRM_NUM_RESOURCE_HANDLES; i++)
76     {
77         if(self_ptr->resource_handle_list[i].job_ptr == NULL)
78         {
79             self_ptr->resource_handle_list[i].job_ptr = job_ptr;
80             self_ptr->resource_handle_list[i].resource_object_ptr = resource_object_ptr;
81             self_ptr->resource_handle_list[i].resource_handle = resource_handle;
82             ret_val = true;
83             break;
84         }
85     }
86
87     return ret_val;
88 }
89
90 /*! \brief  Retrieves the resource handle identified by the given job reference and the given
91  *          resource object reference.
92  *  \param  self                    Instance pointer
93  *  \param  job_ptr                 Reference to the job. Use NULL as wildcard.
94  *  \param  resource_object_ptr     Reference to the resource object
95  *  \param  func_ptr                Optional function pointer in order to check whether the found job belongs to the provided XRM instance.
96  *  \param  usr_ptr                 User pointer used to store the XRM instance to be looked for
97  *  \return Resource handle if handle was found, otherwise XRM_INVALID_RESOURCE_HANDLE.
98  */
99 uint16_t Xrmp_GetResourceHandle(CXrmPool * self, Xrm_Job_t * job_ptr, UCS_XRM_CONST Ucs_Xrm_ResObject_t * resource_object_ptr, Xrmp_CheckJobListFunc_t func_ptr, void * usr_ptr)
100 {
101     uint16_t ret_val = XRM_INVALID_RESOURCE_HANDLE;
102     uint8_t i;
103     bool job_found = true;
104
105     for(i=0U; i<XRM_NUM_RESOURCE_HANDLES; i++)
106     {
107         if(((self->resource_handle_list[i].job_ptr == job_ptr) || (job_ptr == NULL)) &&
108            (self->resource_handle_list[i].resource_object_ptr == resource_object_ptr))
109         {
110             if ((func_ptr != NULL) && (usr_ptr != NULL))
111             {
112                 job_found = func_ptr(usr_ptr, self->resource_handle_list[i].job_ptr);
113             }
114
115             if (job_found)
116             {
117                 ret_val = self->resource_handle_list[i].resource_handle;
118                 break;
119             }
120         }
121     }
122
123     return ret_val;
124 }
125
126 /*! \brief  Returns the table index of the given resource object.
127  *  \param  self        Instance pointer
128  *  \param  job_ptr     Reference to job
129  *  \param  obj_pptr    Reference to array of references to INIC resource objects
130  *  \return Table index of the given resource object. If entry is not found 0xFF is returned.
131  */
132 uint8_t Xrmp_GetResourceHandleIdx(CXrmPool *self, Xrm_Job_t *job_ptr, UCS_XRM_CONST Ucs_Xrm_ResObject_t **obj_pptr)
133 {
134     uint8_t i = 0U;
135     uint8_t ret_val = 0xFFU;
136
137     MISC_UNUSED(self);
138
139     while(job_ptr->resource_object_list_ptr[i] != NULL)
140     {
141         if(job_ptr->resource_object_list_ptr[i] == *obj_pptr)
142         {
143             ret_val = i;
144             break;
145         }
146         i++;
147     }
148
149     return ret_val;
150 }
151
152 /*! \brief  Returns the reference of the job that is identified by the given resource object list.
153  *  \param  self                        Instance pointer
154  *  \param  resource_object_list[]      Reference to array of references to INIC resource objects
155  *  \return Reference to the desired job if the job was found, otherwise NULL.
156  */
157 Xrm_Job_t * Xrmp_GetJob(CXrmPool * self, UCS_XRM_CONST Ucs_Xrm_ResObject_t * resource_object_list[])
158 {
159     uint8_t i;
160     Xrm_Job_t *ret_ptr = NULL;
161
162     for(i=0U; i<(uint8_t)XRM_NUM_JOBS; i++)
163     {
164         if(self->job_list[i].resource_object_list_ptr == resource_object_list)
165         {
166             ret_ptr = &self->job_list[i];
167             break;
168         }
169         else if((self->job_list[i].resource_object_list_ptr == NULL) && (ret_ptr == NULL))
170         {
171             ret_ptr = &self->job_list[i];
172         }
173     }
174
175     return ret_ptr;
176 }
177
178 /*! \brief  Calls the given function for each node in the resource list. If the func_ptr 
179  *          returns true the loop is stopped.
180  *  \param  self            Instance pointer
181  *  \param  func_ptr        Reference of the callback function which is called for each node
182  *  \param  user_data_ptr1  Reference of optional user data 1 pass to func_ptr
183  *  \param  user_data_ptr2  Reference of optional user data 2 pass to func_ptr
184  *  \param  user_data_ptr3  Reference of optional user data 3 pass to func_ptr
185  */
186 void Xrmp_Foreach(CXrmPool *self, Xrmp_ForeachFunc_t func_ptr, void *user_data_ptr1, void *user_data_ptr2, void *user_data_ptr3)
187 {
188     uint8_t j;
189
190     for(j=0U; j<XRM_NUM_RESOURCE_HANDLES; j++)
191     {
192         if (self->resource_handle_list[j].job_ptr != NULL)
193         {
194             if (func_ptr(&self->resource_handle_list[j], user_data_ptr1, user_data_ptr2, user_data_ptr3) != false) 
195             {
196                 break;
197             }
198         }
199     }
200 }
201
202 /*!
203  * @}
204  * \endcond
205  */
206
207 /*------------------------------------------------------------------------------------------------*/
208 /* End of file                                                                                    */
209 /*------------------------------------------------------------------------------------------------*/
210