Initial Commit
[apps/agl-service-unicens.git] / ucs2-lib / src / ucs_factory.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 MNS Factory.
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup G_FAC
28  * @{
29  */
30
31 /*------------------------------------------------------------------------------------------------*/
32 /* Includes                                                                                       */
33 /*------------------------------------------------------------------------------------------------*/
34 #include "ucs_factory.h"
35 #include "ucs_xrm_pv.h"
36
37 /*------------------------------------------------------------------------------------------------*/
38 /* Internal macros                                                                                */
39 /*------------------------------------------------------------------------------------------------*/
40 #define IS_VALID_ADDR(addr) ((UCS_ADDR_LOCAL_DEV == (addr)) || ((0x0FU < (addr)) && (0x300U > (addr))) || ((0x04FFU < (addr)) && (0x0FF0U > (addr)))) /* parasoft-suppress  MISRA2004-19_7 "common definition of type cast improves code" */
41
42 /*------------------------------------------------------------------------------------------------*/
43 /* Internal prototypes                                                                            */
44 /*------------------------------------------------------------------------------------------------*/
45 static void Fac_ConstructFbi (CFactory * self, CInic * fbi, uint16_t address);
46 static CInic * Fac_SearchFbi(CFactory * self, uint16_t address);
47 static void Fac_ConstructNsm (CFactory * self, CNodeScriptManagement * nsm, uint16_t address);
48 static CRemoteSyncManagement * Fac_SearchRsm(CFactory * self, uint16_t address);
49 static CExtendedResourceManager * Fac_SearchXrm(CFactory * self, uint16_t address);
50 static CGpio * Fac_SearchGpio(CFactory * self, uint16_t address);
51 static CI2c* Fac_SearchI2c(CFactory * self, uint16_t address);
52 static CNodeScriptManagement * Fac_SearchNsm(CFactory * self, uint16_t address);
53 static CInic * Fac_GetUninitializedFbi (CFactory * self);
54 static CNodeScriptManagement * Fac_GetUninitializedNsm (CFactory * self);
55 static CRemoteSyncManagement * Fac_GetUninitializedRsm (CFactory * self);
56 static CExtendedResourceManager * Fac_GetUninitializedXrm (CFactory * self);
57 static CGpio * Fac_GetUninitializedGpio (CFactory * self);
58 static CI2c * Fac_GetUninitializedI2c (CFactory * self);
59 static bool Fac_IsFbiUninitialized(CInic * fbi);
60 static bool Fac_IsRsmUninitialized(CRemoteSyncManagement * rsm);
61 static bool Fac_IsXrmUninitialized(CExtendedResourceManager * xrm);
62 static bool Fac_IsGpioUninitialized(CGpio * gpio);
63 static bool Fac_IsI2cUninitialized(CI2c * i2c);
64 static bool Fac_IsNsmUninitialized(CNodeScriptManagement * nsm);
65
66 /*------------------------------------------------------------------------------------------------*/
67 /* Implementation of class CFactory                                                               */
68 /*------------------------------------------------------------------------------------------------*/
69 /*! \brief Constructor of the MNS Factory class.
70  *  \param self        Instance pointer
71  *  \param init_ptr    init data_ptr
72  */
73 void Fac_Ctor(CFactory * self, Fac_InitData_t * init_ptr)
74 {
75     uint8_t i;
76     Rsm_InitData_t rsm_init_data;
77
78     MISC_MEM_SET(self, 0, sizeof(CFactory));
79
80     /* set base and net instances */
81     self->base_ptr = init_ptr->base_ptr;
82     self->net_ptr  = init_ptr->net_ptr;
83     self->xrmp_ptr = init_ptr->xrmp_ptr;
84     self->icm_transceiver = init_ptr->icm_transceiver;
85     self->rcm_transceiver = init_ptr->rcm_transceiver;
86
87     rsm_init_data.base_ptr = self->base_ptr;
88     rsm_init_data.net_ptr  = self->net_ptr;
89
90     for (i = 0U; i<FAC_NUM_DEVICES; i++)
91     {
92         rsm_init_data.inic_ptr = &self->fbi_list[i];
93         Rsm_Ctor(&self->rsm_list[i], &rsm_init_data);
94     }
95 }
96
97 /*------------------------------------------------------------------------------------------------*/
98 /* Service                                                                                        */
99 /*------------------------------------------------------------------------------------------------*/
100 /*! \brief Returns the XRM instance associated with the given address.
101  *  \param self                      Instance pointer
102  *  \param address                   Address of the device associated with the instance
103  *  \param res_debugging_fptr        The resources debugging callback function
104  *  \param check_unmute_fptr         The check unmute callback function
105  *  \return a reference to a XRM instance or \c NULL if no appropriate instance has been found.
106  */
107 CExtendedResourceManager * Fac_GetXrm(CFactory * self, uint16_t address, Ucs_Xrm_ResourceDebugCb_t res_debugging_fptr, Ucs_Xrm_CheckUnmuteCb_t check_unmute_fptr)
108 {
109     CRemoteSyncManagement * rsm_inst = NULL;
110     CExtendedResourceManager * xrm_inst = NULL;
111
112     if (IS_VALID_ADDR(address))
113     {
114         xrm_inst = Fac_SearchXrm(self, address);
115         if (xrm_inst == NULL)
116         {
117             rsm_inst = Fac_GetRsm(self, address);
118             if (rsm_inst != NULL)
119             {
120                 Xrm_InitData_t xrm_init_data;
121                 xrm_inst = Fac_GetUninitializedXrm(self);
122                 if (xrm_inst != NULL)
123                 {
124                     xrm_init_data.base_ptr = self->base_ptr;
125                     xrm_init_data.net_ptr  = self->net_ptr;
126                     xrm_init_data.rsm_ptr  = rsm_inst;
127                     xrm_init_data.inic_ptr = rsm_inst->inic_ptr;
128                     xrm_init_data.xrmp_ptr = self->xrmp_ptr;
129                     xrm_init_data.check_unmute_fptr  = check_unmute_fptr;
130                     xrm_init_data.res_debugging_fptr = res_debugging_fptr;
131                     Xrm_Ctor(xrm_inst, &xrm_init_data);
132                 }
133             }
134         }
135         Xrm_SetResourceDebugCbFn(xrm_inst, res_debugging_fptr);
136     }
137
138     return xrm_inst;
139 }
140
141 /*! \brief Returns the XRM instance associated with the given address.
142  *  \param self                      Instance pointer
143  *  \param address                   Address of the device associated with the instance
144  *  \param check_unmute_fptr         The check unmute callback function
145  *  \return a reference to a XRM instance or \c NULL if no appropriate instance has been found.
146  */
147 CExtendedResourceManager * Fac_GetXrmLegacy(CFactory * self, uint16_t address, Ucs_Xrm_CheckUnmuteCb_t check_unmute_fptr)
148 {
149     return Fac_GetXrm(self, address, NULL, check_unmute_fptr);
150 }
151
152
153 /*! \brief Returns the XRM instance associated with the resource list.
154  *  \note    <b>This function should only be used in case of Ucs_Xrm_Destroy() since it's certain in that case that the XRM instance for the given job list already exists!</b>
155  *  \param self                      Instance pointer
156  *  \param resource_object_list      Reference to the job list
157  *  \return a reference to a XRM instance or \c NULL if no appropriate instance has been found.
158  */
159 CExtendedResourceManager * Fac_GetXrmByJobList(CFactory * self, UCS_XRM_CONST Ucs_Xrm_ResObject_t *resource_object_list[])
160 {
161     uint8_t i;
162     CExtendedResourceManager * ret_xrm = NULL;
163
164     for(i=0U; i<FAC_NUM_DEVICES; i++)
165     {
166         if (Xrm_IsInMyJobList(&self->xrm_list[i], resource_object_list))
167         {
168             ret_xrm = &self->xrm_list[i];
169             break;
170         }
171     }
172
173     return ret_xrm;
174 }
175
176 /*! \brief Returns the FBlock INIC instance associated with the given address.
177  *  \param self                      Instance pointer
178  *  \param address                   Address of the device associated with the instance
179  *  \return a reference to a FBI instance or \c NULL if no suitable instance has been found.
180  */
181 CInic * Fac_GetInic(CFactory * self, uint16_t address)
182 {
183     CInic * fbi_inst = NULL;
184
185     if (IS_VALID_ADDR(address))
186     {
187         fbi_inst = Fac_SearchFbi(self, address);
188         if (fbi_inst == NULL)
189         {
190             fbi_inst = Fac_GetUninitializedFbi(self);
191             if (fbi_inst != NULL)
192             {
193                 Fac_ConstructFbi(self, fbi_inst, address);
194             }
195         }
196     }
197
198     return fbi_inst;
199 }
200
201 /*! \brief Returns the CNodeScriptManagement instance associated with the given address.
202  *  \param self                      Instance pointer
203  *  \param address                   Address of the device associated with the instance
204  *  \return a reference to a FBI instance or \c NULL if no suitable instance has been found.
205  */
206 CNodeScriptManagement * Fac_GetNsm(CFactory * self, uint16_t address)
207 {
208     CNodeScriptManagement * nsm_inst = NULL;
209
210     if (IS_VALID_ADDR(address))
211     {
212         nsm_inst = Fac_SearchNsm(self, address);
213         if (nsm_inst == NULL)
214         {
215             nsm_inst = Fac_GetUninitializedNsm(self);
216             if (nsm_inst != NULL)
217             {
218                 Fac_ConstructNsm(self, nsm_inst, address);
219             }
220         }
221     }
222
223     return nsm_inst;
224 }
225
226 /*! \brief Returns the RSM instance associated with the given address.
227  *  \param self                      Instance pointer
228  *  \param address                   Address of the device associated with the instance
229  *  \return a reference to a RSM instance or \c NULL if no suitable instance has been found.
230  */
231 CRemoteSyncManagement * Fac_GetRsm(CFactory * self, uint16_t address)
232 {
233     CRemoteSyncManagement * rsm_inst = NULL;
234
235     if (IS_VALID_ADDR(address))
236     {
237         rsm_inst = Fac_SearchRsm(self, address);
238         if (rsm_inst == NULL)
239         {
240             rsm_inst = Fac_GetUninitializedRsm(self);
241             if (rsm_inst != NULL)
242             {
243                 Fac_ConstructFbi(self, rsm_inst->inic_ptr, address);
244             }
245         }
246     }
247
248     return rsm_inst;
249 }
250
251 /*! \brief Returns the GPIO instance associated with the given address.
252  *  \param self                      Instance pointer
253  *  \param address                   Address of the device associated with the instance
254  *  \param trigger_event_status_fptr User GPIO trigger event status callback function pointer.
255  *  \return a reference to a GPIO instance or \c NULL if no suitable instance has been found.
256  */
257 CGpio * Fac_GetGpio(CFactory * self, uint16_t address, Ucs_Gpio_TriggerEventResultCb_t trigger_event_status_fptr)
258 {
259     CGpio * gpio_inst = NULL;
260     CNodeScriptManagement * nsm_inst = NULL;
261
262     if (IS_VALID_ADDR(address))
263     {
264         gpio_inst = Fac_SearchGpio(self, address);
265         if (NULL == gpio_inst)
266         {
267             nsm_inst = Fac_GetNsm(self, address);
268             if (NULL != nsm_inst)
269             {
270                 Gpio_InitData_t gpio_init_data;
271                 gpio_inst = Fac_GetUninitializedGpio(self);
272                 if (NULL != gpio_inst)
273                 {
274                     gpio_init_data.nsm_ptr  = nsm_inst;
275                     gpio_init_data.inic_ptr = nsm_inst->rsm_ptr->inic_ptr;
276                     gpio_init_data.trigger_event_status_fptr = trigger_event_status_fptr;
277                     Gpio_Ctor(gpio_inst, &gpio_init_data);
278                 }
279             }
280         }
281     }
282
283     return gpio_inst;
284 }
285
286 /*! \brief Returns the I2C instance associated with the given address.
287  *  \param self                      Instance pointer
288  *  \param address                   Address of the device associated with the instance
289  *  \param i2c_interrupt_report_fptr User GPIO trigger event status callback function pointer.
290  *  \return a reference to an I2C instance or \c NULL if no suitable instance has been found.
291  */
292 CI2c * Fac_GetI2c(CFactory * self, uint16_t address, Ucs_I2c_IntEventReportCb_t i2c_interrupt_report_fptr)
293 {
294     CI2c * i2c_inst = NULL;
295     CNodeScriptManagement * nsm_inst = NULL;
296
297     if (IS_VALID_ADDR(address))
298     {
299         i2c_inst = Fac_SearchI2c (self, address);
300         if (NULL == i2c_inst)
301         {
302             nsm_inst = Fac_GetNsm(self, address); 
303             if (nsm_inst != NULL)
304             {
305                 I2c_InitData_t i2c_init_data;
306                 i2c_inst = Fac_GetUninitializedI2c(self);
307                 if (NULL != i2c_inst)
308                 {
309                     i2c_init_data.nsm_ptr  = nsm_inst;
310                     i2c_init_data.inic_ptr = nsm_inst->rsm_ptr->inic_ptr;
311                     i2c_init_data.i2c_interrupt_report_fptr = i2c_interrupt_report_fptr;
312                     I2c_Ctor(i2c_inst, &i2c_init_data);
313                 }
314             }
315         }
316     }
317
318     return i2c_inst;
319 }
320
321 /*! \brief Searches for the INIC instance associated with the given address and returns It if found.
322  *  \param self                      Instance pointer
323  *  \param address                   Address of the device associated with this instance
324  *  \return a reference to the found instance otherwise \c NULL.
325  */
326 CInic * Fac_FindInic(CFactory * self, uint16_t address)
327 {
328     return Fac_SearchFbi (self, address);
329 }
330
331 /*! \brief Searches for the NSM instance associated with the given address and returns It if found.
332  *  \param self                      Instance pointer
333  *  \param address                   Address of the device associated with this instance
334  *  \return a reference to the found instance otherwise \c NULL.
335  */
336 CNodeScriptManagement * Fac_FindNsm(CFactory * self, uint16_t address)
337 {
338     return Fac_SearchNsm (self, address);
339 }
340
341 /*! \brief Searches for the RSM instance associated with the given address and returns It if found.
342  *  \param self                      Instance pointer
343  *  \param address                   Address of the device associated with this instance
344  *  \return a reference to the found instance otherwise \c NULL.
345  */
346 CRemoteSyncManagement * Fac_FindRsm(CFactory * self, uint16_t address)
347 {
348     return Fac_SearchRsm (self, address);
349 }
350
351 /*! \brief  Calls the given function for each instance of inst_type type. If the func_ptr 
352  *          returns true the loop is stopped.
353  *  \param  self            Reference to a Factory Instance
354  *  \param  inst_type       The instance type to be looked for
355  *  \param  func_ptr        Reference of the callback function which is called for each node
356  *  \param  user_data_ptr   Reference of optional user data pass to the func_ptr
357  */
358 void Fac_Foreach(CFactory * self, Fac_Inst_t inst_type, Fac_ForeachFunc_t func_ptr, void *user_data_ptr)
359 {
360     uint8_t j;
361     void * curr_inst = NULL;
362     bool exit_loop = false;
363
364     for(j=0U; j<FAC_NUM_DEVICES; j++)
365     {
366         switch(inst_type)
367         {
368             case FAC_INST_INIC:
369                 curr_inst = &self->fbi_list[j];
370                 if (Fac_IsFbiUninitialized((CInic *)curr_inst))
371                 {
372                     curr_inst = NULL;
373                 }
374                 break;
375
376             case FAC_INST_RSM:
377                 curr_inst = &self->rsm_list[j];
378                 if (Fac_IsRsmUninitialized((CRemoteSyncManagement *)curr_inst))
379                 {
380                     curr_inst = NULL;
381                 }
382                 break;
383
384             case FAC_INST_XRM:
385                 curr_inst = &self->xrm_list[j];
386                 if (Fac_IsXrmUninitialized((CExtendedResourceManager *)curr_inst))
387                 {
388                     curr_inst = NULL;
389                 }
390                 break;
391
392             case FAC_INST_GPIO:
393                 curr_inst = &self->gpio_list[j];
394                 if (Fac_IsGpioUninitialized((CGpio *)curr_inst))
395                 {
396                     curr_inst = NULL;
397                 }
398                 break;
399
400             case FAC_INST_I2C:
401                 curr_inst = &self->i2c_list[j];
402                 if (Fac_IsI2cUninitialized((CI2c *)curr_inst))
403                 {
404                     curr_inst = NULL;
405                 }
406                 break;
407
408             default:
409                 break;
410         }
411
412         if (curr_inst != NULL)
413         {
414             if (func_ptr(inst_type, curr_inst, user_data_ptr) != false) 
415             {
416                 exit_loop = true;
417             }
418         }
419         else
420         {
421             exit_loop = true;
422         }
423
424         if (exit_loop)
425         {
426             break;
427         }
428     }
429 }
430
431 /*------------------------------------------------------------------------------------------------*/
432 /* Private Methods                                                                                */
433 /*------------------------------------------------------------------------------------------------*/
434 /*! \brief Search for the FBI instance associated with the given address and return It.
435  *  \param self       Instance pointer
436  *  \param address    Address to be looked for
437  *  \return a reference to the found FBI or \c NULL if no suitable instance has been found.
438  */
439 static CInic * Fac_SearchFbi(CFactory * self, uint16_t address)
440 {
441     CInic * found_fbi = NULL;
442     uint8_t i;
443     uint16_t tmp_addr = address;
444
445     if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
446     {
447         tmp_addr = UCS_ADDR_LOCAL_DEV;
448     }
449
450     for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsFbiUninitialized(&self->fbi_list[i])); i++)
451     {
452         if (tmp_addr == Inic_GetTargetAddress(&self->fbi_list[i]))
453         {
454             found_fbi = &self->fbi_list[i];
455             break;
456         }
457     }
458
459     return found_fbi;
460 }
461
462 /*! \brief Search for the NSM instance associated with the given address and return It.
463  *  \param self       Instance pointer
464  *  \param address    Address to be looked for
465  *  \return a reference to the found NSM or \c NULL if no suitable instance has been found.
466  */
467 static CNodeScriptManagement * Fac_SearchNsm(CFactory * self, uint16_t address)
468 {
469     CNodeScriptManagement * found_nsm = NULL;
470     uint8_t i;
471     uint16_t tmp_addr = address;
472
473     if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
474     {
475         tmp_addr = UCS_ADDR_LOCAL_DEV;
476     }
477
478     for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsNsmUninitialized(&self->nsm_list[i])); i++)
479     {
480         if (tmp_addr == self->nsm_list[i].target_address)
481         {
482             found_nsm = &self->nsm_list[i];
483             break;
484         }
485     }
486
487     return found_nsm;
488 }
489
490 /*! \brief Search for the RSM instance associated with the given address.
491  *  \param self    Instance pointer
492  *  \param address Address to be looked for
493  *  \return a reference to the found RSM or \c NULL if no suitable instance has been found.
494  */
495 static CRemoteSyncManagement * Fac_SearchRsm(CFactory * self, uint16_t address)
496 {
497     CRemoteSyncManagement * found_rsm = NULL;
498     uint8_t i;
499     uint16_t tmp_addr = address;
500
501     if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
502     {
503         tmp_addr = UCS_ADDR_LOCAL_DEV;
504     }
505
506     for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsFbiUninitialized(self->rsm_list[i].inic_ptr)); i++)
507     {
508         if (tmp_addr == Inic_GetTargetAddress(self->rsm_list[i].inic_ptr))
509         {
510             found_rsm = &self->rsm_list[i];
511             break;
512         }
513     }
514
515     return found_rsm;
516 }
517
518 /*! \brief Search for the XRM instance associated with the given address.
519  *  \param self    Instance pointer
520  *  \param address    Address to be looked for
521  *  \return a reference to the found XRM or \c NULL if no suitable instance has been found.
522  */
523 static CExtendedResourceManager * Fac_SearchXrm(CFactory * self, uint16_t address)
524 {
525     CExtendedResourceManager * found_xrm = NULL;
526     uint8_t i;
527     uint16_t tmp_addr = address;
528
529     if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
530     {
531         tmp_addr = UCS_ADDR_LOCAL_DEV;
532     }
533
534     for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsXrmUninitialized(&self->xrm_list[i])); i++)
535     {
536         if (tmp_addr == Inic_GetTargetAddress(self->xrm_list[i].rsm_ptr->inic_ptr))
537         {
538             found_xrm = &self->xrm_list[i];
539             break;
540         }
541     }
542
543     return found_xrm;
544 }
545
546 /*! \brief Search for the Gpio instance associated with the given address.
547  *  \param self    Instance pointer
548  *  \param address    Address to be looked for
549  *  \return a reference to the found GPIO or \c NULL if no suitable instance has been found.
550  */
551 static CGpio * Fac_SearchGpio(CFactory * self, uint16_t address)
552 {
553     CGpio * found_gpio = NULL;
554     uint8_t i;
555     uint16_t tmp_addr = address;
556
557     if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
558     {
559         tmp_addr = UCS_ADDR_LOCAL_DEV;
560     }
561
562     for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsGpioUninitialized(&self->gpio_list[i])); i++)
563     {
564         if (tmp_addr == Inic_GetTargetAddress(self->gpio_list[i].nsm_ptr->rsm_ptr->inic_ptr))
565         {
566             found_gpio = &self->gpio_list[i];
567             break;
568         }
569     }
570
571     return found_gpio;
572 }
573
574 /*! \brief Search for the I2c instance associated with the given address.
575  *  \param self       Instance pointer
576  *  \param address    Address to be looked for
577  *  \return a reference to the found GPIO or \c NULL if no suitable instance has been found.
578  */
579 static CI2c * Fac_SearchI2c(CFactory * self, uint16_t address)
580 {
581     CI2c * found_i2c = NULL;
582     uint8_t i;
583     uint16_t tmp_addr = address;
584
585     if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
586     {
587         tmp_addr = UCS_ADDR_LOCAL_DEV;
588     }
589
590     for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsI2cUninitialized(&self->i2c_list[i])); i++)
591     {
592         if (tmp_addr == Inic_GetTargetAddress(self->i2c_list[i].nsm_ptr->rsm_ptr->inic_ptr))
593         {
594             found_i2c = &self->i2c_list[i];
595             break;
596         }
597     }
598
599     return found_i2c;
600 }
601
602 /*! \brief Returns the next free uninitialized XRM instance
603  *  \param self    Instance pointer
604  *  \return a reference to the next free uninitialized XRM instance if found, otherwise \c NULL.
605  */
606 static CExtendedResourceManager * Fac_GetUninitializedXrm (CFactory * self)
607 {
608     CExtendedResourceManager * tmp_xrm = NULL;
609     uint8_t i;
610
611     for (i = 0U; i<FAC_NUM_DEVICES; i++)
612     {
613         if (self->xrm_list[i].rsm_ptr == NULL)
614         {
615             tmp_xrm = &self->xrm_list[i];
616             break;
617         }
618     }
619
620     return tmp_xrm;
621 }
622
623 /*! \brief Returns the next free uninitialized FBI instance
624  *  \param self    Instance pointer
625  *  \return a reference to the next free uninitialized FBI instance if found, otherwise \c NULL.
626  */
627 static CInic * Fac_GetUninitializedFbi (CFactory * self)
628 {
629     CInic * tmp_inic = NULL;
630     uint8_t i;
631
632     for (i = 0U; i<FAC_NUM_DEVICES; i++)
633     {
634         if (self->fbi_list[i].base_ptr == NULL)
635         {
636             tmp_inic = &self->fbi_list[i];
637             break;
638         }
639     }
640
641     return tmp_inic;
642 }
643
644 /*! \brief Returns the next free uninitialized NSM instance
645  *  \param self    Instance pointer
646  *  \return a reference to the next free uninitialized NSM instance if found, otherwise \c NULL.
647  */
648 static CNodeScriptManagement * Fac_GetUninitializedNsm (CFactory * self)
649 {
650     CNodeScriptManagement * tmp_nsm = NULL;
651     uint8_t i;
652
653     for (i = 0U; i<FAC_NUM_DEVICES; i++)
654     {
655         if (self->nsm_list[i].base_ptr == NULL)
656         {
657             tmp_nsm = &self->nsm_list[i];
658             break;
659         }
660     }
661
662     return tmp_nsm;
663 }
664
665 /*! \brief Returns the next free uninitialized RSM instance
666  *  \param self    Instance pointer
667  *  \return a reference to the next free uninitialized RSM instance if found, otherwise \c NULL.
668  */
669 static CRemoteSyncManagement * Fac_GetUninitializedRsm (CFactory * self)
670 {
671     CRemoteSyncManagement * tmp_rsm = NULL;
672     uint8_t i;
673
674     for (i = 0U; i<FAC_NUM_DEVICES; i++)
675     {
676         if (Inic_GetTargetAddress(self->rsm_list[i].inic_ptr) == 0x0U)
677         {
678             tmp_rsm = &self->rsm_list[i];
679             break;
680         }
681     }
682
683     return tmp_rsm;
684 }
685
686 /*! \brief Returns the next free uninitialized GPIO instance
687  *  \param self    Instance pointer
688  *  \return a reference to the next free uninitialized GPIO instance if found, otherwise \c NULL.
689  */
690 static CGpio * Fac_GetUninitializedGpio (CFactory * self)
691 {
692     CGpio * tmp_gpio = NULL;
693     uint8_t i;
694
695     for (i = 0U; i<FAC_NUM_DEVICES; i++)
696     {
697         if (NULL == self->gpio_list[i].nsm_ptr)
698         {
699             tmp_gpio = &self->gpio_list[i];
700             break;
701         }
702     }
703
704     return tmp_gpio;
705 }
706
707 /*! \brief Returns the next free uninitialized I2C instance
708  *  \param self    Instance pointer
709  *  \return a reference to the next free uninitialized I2C instance if found, otherwise \c NULL.
710  */
711 static CI2c * Fac_GetUninitializedI2c (CFactory * self)
712 {
713     CI2c * tmp_i2c = NULL;
714     uint8_t i;
715
716     for (i = 0U; i<FAC_NUM_DEVICES; i++)
717     {
718         if (NULL == self->i2c_list[i].nsm_ptr)
719         {
720             tmp_i2c = &self->i2c_list[i];
721             break;
722         }
723     }
724
725     return tmp_i2c;
726 }
727
728 /*! \brief Constructs the given FBI instance
729  *  \param self       the MNS factory Instance pointer
730  *  \param fbi        the INIC Instance pointer
731  *  \param address    the device address of this FBlock INIC
732  */
733 static void Fac_ConstructFbi (CFactory * self, CInic * fbi, uint16_t address)
734 {
735     Inic_InitData_t inic_init_data;
736
737     if (address == UCS_ADDR_LOCAL_DEV)
738     {
739         inic_init_data.xcvr_ptr = self->icm_transceiver;
740     }
741     else
742     {
743         inic_init_data.xcvr_ptr = self->rcm_transceiver;
744     }
745
746     inic_init_data.base_ptr = self->base_ptr;
747     inic_init_data.tgt_addr = address;
748
749     Inic_Ctor(fbi, &inic_init_data);
750 }
751
752 /*! \brief Constructs the given NSM instance
753  *  \param self       the MNS factory Instance pointer
754  *  \param nsm        the NSM Instance pointer
755  *  \param address    the device address
756  */
757 static void Fac_ConstructNsm (CFactory * self, CNodeScriptManagement * nsm, uint16_t address)
758 {
759     Nsm_InitData_t nsm_init_data;
760
761     nsm_init_data.base_ptr = self->base_ptr;
762     nsm_init_data.rcm_ptr  = self->rcm_transceiver;
763     nsm_init_data.rsm_ptr  = Fac_GetRsm(self, address);
764
765     Nsm_Ctor(nsm, &nsm_init_data);
766 }
767
768 /*! \brief Checks whether the given FBlock INIC instance is uninitialized
769  *  \param fbi        the INIC Instance pointer
770  *  \return \c true if the given Fbi instance is not initialized, otherwise \c False.
771  */
772 static bool Fac_IsFbiUninitialized(CInic * fbi)
773 {
774     return (fbi->base_ptr == NULL) ;
775 }
776
777 /*! \brief Checks whether the given NSM instance is uninitialized
778  *  \param nsm        the NSM Instance pointer
779  *  \return \c true if the given NSM instance is not initialized, otherwise \c False.
780  */
781 static bool Fac_IsNsmUninitialized(CNodeScriptManagement * nsm)
782 {
783     return (nsm->base_ptr == NULL) ;
784 }
785
786 /*! \brief Checks whether the given RSM instance is uninitialized
787  *  \param rsm   Reference to the RSM instance pointer
788  *  \return \c true if the given Fbi instance is not initialized, otherwise \c False.
789  */
790 static bool Fac_IsRsmUninitialized(CRemoteSyncManagement * rsm)
791 {
792     return Fac_IsFbiUninitialized(rsm->inic_ptr);
793 }
794
795 /*! \brief Checks whether the given XRM instance is uninitialized
796  *  \param xrm        the XRM Instance pointer
797  *  \return \c true if the given XRM instance is not initialized, otherwise \c False.
798  */
799 static bool Fac_IsXrmUninitialized(CExtendedResourceManager * xrm)
800 {
801     return (xrm->rsm_ptr == NULL) ;
802 }
803
804 /*! \brief Checks whether the given GPIO instance is uninitialized
805  *  \param gpio        the GPIO Instance pointer
806  *  \return \c true if the given GPIO instance is not initialized, otherwise \c False.
807  */
808 static bool Fac_IsGpioUninitialized(CGpio * gpio)
809 {
810     return (NULL == gpio->nsm_ptr);
811 }
812
813 /*! \brief Checks whether the given I2C instance is uninitialized
814  *  \param i2c        the I2C Instance pointer
815  *  \return \c true if the given I2C instance is not initialized, otherwise \c False.
816  */
817 static bool Fac_IsI2cUninitialized(CI2c * i2c)
818 {
819     return (NULL == i2c->nsm_ptr);
820 }
821
822 /*!
823  * @}
824  * \endcond
825  */
826
827 /*------------------------------------------------------------------------------------------------*/
828 /* End of file                                                                                    */
829 /*------------------------------------------------------------------------------------------------*/
830