summaryrefslogtreecommitdiffstats
path: root/ucs2-lib/doc/html/search/groups_62.js
blob: 5c4f987c743406b687666194f5748b17538a9b07 (plain)
1
2
3
4
var searchData=
[
  ['backchannel_20diagnosis',['BackChannel Diagnosis',['../group___g___u_c_s___b_c___d_i_a_g.html',1,'']]]
];
ref='#n325'>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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/*------------------------------------------------------------------------------------------------*/
/* 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 MNS Factory.
 *
 * \cond UCS_INTERNAL_DOC
 * \addtogroup G_FAC
 * @{
 */

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_factory.h"
#include "ucs_xrm_pv.h"

/*------------------------------------------------------------------------------------------------*/
/* Internal macros                                                                                */
/*------------------------------------------------------------------------------------------------*/
#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" */

/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes                                                                            */
/*------------------------------------------------------------------------------------------------*/
static void Fac_ConstructFbi (CFactory * self, CInic * fbi, uint16_t address);
static CInic * Fac_SearchFbi(CFactory * self, uint16_t address);
static void Fac_ConstructNsm (CFactory * self, CNodeScriptManagement * nsm, uint16_t address);
static CRemoteSyncManagement * Fac_SearchRsm(CFactory * self, uint16_t address);
static CExtendedResourceManager * Fac_SearchXrm(CFactory * self, uint16_t address);
static CGpio * Fac_SearchGpio(CFactory * self, uint16_t address);
static CI2c* Fac_SearchI2c(CFactory * self, uint16_t address);
static CNodeScriptManagement * Fac_SearchNsm(CFactory * self, uint16_t address);
static CInic * Fac_GetUninitializedFbi (CFactory * self);
static CNodeScriptManagement * Fac_GetUninitializedNsm (CFactory * self);
static CRemoteSyncManagement * Fac_GetUninitializedRsm (CFactory * self);
static CExtendedResourceManager * Fac_GetUninitializedXrm (CFactory * self);
static CGpio * Fac_GetUninitializedGpio (CFactory * self);
static CI2c * Fac_GetUninitializedI2c (CFactory * self);
static bool Fac_IsFbiUninitialized(CInic * fbi);
static bool Fac_IsRsmUninitialized(CRemoteSyncManagement * rsm);
static bool Fac_IsXrmUninitialized(CExtendedResourceManager * xrm);
static bool Fac_IsGpioUninitialized(CGpio * gpio);
static bool Fac_IsI2cUninitialized(CI2c * i2c);
static bool Fac_IsNsmUninitialized(CNodeScriptManagement * nsm);

/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CFactory                                                               */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the MNS Factory class.
 *  \param self        Instance pointer
 *  \param init_ptr    init data_ptr
 */
void Fac_Ctor(CFactory * self, Fac_InitData_t * init_ptr)
{
    uint8_t i;
    Rsm_InitData_t rsm_init_data;

    MISC_MEM_SET(self, 0, sizeof(CFactory));

    /* set base and net instances */
    self->base_ptr = init_ptr->base_ptr;
    self->net_ptr  = init_ptr->net_ptr;
    self->xrmp_ptr = init_ptr->xrmp_ptr;
    self->icm_transceiver = init_ptr->icm_transceiver;
    self->rcm_transceiver = init_ptr->rcm_transceiver;

    rsm_init_data.base_ptr = self->base_ptr;
    rsm_init_data.net_ptr  = self->net_ptr;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        rsm_init_data.inic_ptr = &self->fbi_list[i];
        Rsm_Ctor(&self->rsm_list[i], &rsm_init_data);
    }
}

/*------------------------------------------------------------------------------------------------*/
/* Service                                                                                        */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Returns the XRM instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \param res_debugging_fptr        The resources debugging callback function
 *  \param check_unmute_fptr         The check unmute callback function
 *  \return a reference to a XRM instance or \c NULL if no appropriate instance has been found.
 */
CExtendedResourceManager * Fac_GetXrm(CFactory * self, uint16_t address, Ucs_Xrm_ResourceDebugCb_t res_debugging_fptr, Ucs_Xrm_CheckUnmuteCb_t check_unmute_fptr)
{
    CRemoteSyncManagement * rsm_inst = NULL;
    CExtendedResourceManager * xrm_inst = NULL;

    if (IS_VALID_ADDR(address))
    {
        xrm_inst = Fac_SearchXrm(self, address);
        if (xrm_inst == NULL)
        {
            rsm_inst = Fac_GetRsm(self, address);
            if (rsm_inst != NULL)
            {
                Xrm_InitData_t xrm_init_data;
                xrm_inst = Fac_GetUninitializedXrm(self);
                if (xrm_inst != NULL)
                {
                    xrm_init_data.base_ptr = self->base_ptr;
                    xrm_init_data.net_ptr  = self->net_ptr;
                    xrm_init_data.rsm_ptr  = rsm_inst;
                    xrm_init_data.inic_ptr = rsm_inst->inic_ptr;
                    xrm_init_data.xrmp_ptr = self->xrmp_ptr;
                    xrm_init_data.check_unmute_fptr  = check_unmute_fptr;
                    xrm_init_data.res_debugging_fptr = res_debugging_fptr;
                    Xrm_Ctor(xrm_inst, &xrm_init_data);
                }
            }
        }
        Xrm_SetResourceDebugCbFn(xrm_inst, res_debugging_fptr);
    }

    return xrm_inst;
}

/*! \brief Returns the XRM instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \param check_unmute_fptr         The check unmute callback function
 *  \return a reference to a XRM instance or \c NULL if no appropriate instance has been found.
 */
CExtendedResourceManager * Fac_GetXrmLegacy(CFactory * self, uint16_t address, Ucs_Xrm_CheckUnmuteCb_t check_unmute_fptr)
{
    return Fac_GetXrm(self, address, NULL, check_unmute_fptr);
}


/*! \brief Returns the XRM instance associated with the resource list.
 *  \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>
 *  \param self                      Instance pointer
 *  \param resource_object_list      Reference to the job list
 *  \return a reference to a XRM instance or \c NULL if no appropriate instance has been found.
 */
CExtendedResourceManager * Fac_GetXrmByJobList(CFactory * self, UCS_XRM_CONST Ucs_Xrm_ResObject_t *resource_object_list[])
{
    uint8_t i;
    CExtendedResourceManager * ret_xrm = NULL;

    for(i=0U; i<FAC_NUM_DEVICES; i++)
    {
        if (Xrm_IsInMyJobList(&self->xrm_list[i], resource_object_list))
        {
            ret_xrm = &self->xrm_list[i];
            break;
        }
    }

    return ret_xrm;
}

/*! \brief Returns the FBlock INIC instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \return a reference to a FBI instance or \c NULL if no suitable instance has been found.
 */
CInic * Fac_GetInic(CFactory * self, uint16_t address)
{
    CInic * fbi_inst = NULL;

    if (IS_VALID_ADDR(address))
    {
        fbi_inst = Fac_SearchFbi(self, address);
        if (fbi_inst == NULL)
        {
            fbi_inst = Fac_GetUninitializedFbi(self);
            if (fbi_inst != NULL)
            {
                Fac_ConstructFbi(self, fbi_inst, address);
            }
        }
    }

    return fbi_inst;
}

/*! \brief Returns the CNodeScriptManagement instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \return a reference to a FBI instance or \c NULL if no suitable instance has been found.
 */
CNodeScriptManagement * Fac_GetNsm(CFactory * self, uint16_t address)
{
    CNodeScriptManagement * nsm_inst = NULL;

    if (IS_VALID_ADDR(address))
    {
        nsm_inst = Fac_SearchNsm(self, address);
        if (nsm_inst == NULL)
        {
            nsm_inst = Fac_GetUninitializedNsm(self);
            if (nsm_inst != NULL)
            {
                Fac_ConstructNsm(self, nsm_inst, address);
            }
        }
    }

    return nsm_inst;
}

/*! \brief Returns the RSM instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \return a reference to a RSM instance or \c NULL if no suitable instance has been found.
 */
CRemoteSyncManagement * Fac_GetRsm(CFactory * self, uint16_t address)
{
    CRemoteSyncManagement * rsm_inst = NULL;

    if (IS_VALID_ADDR(address))
    {
        rsm_inst = Fac_SearchRsm(self, address);
        if (rsm_inst == NULL)
        {
            rsm_inst = Fac_GetUninitializedRsm(self);
            if (rsm_inst != NULL)
            {
                Fac_ConstructFbi(self, rsm_inst->inic_ptr, address);
            }
        }
    }

    return rsm_inst;
}

/*! \brief Returns the GPIO instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \param trigger_event_status_fptr User GPIO trigger event status callback function pointer.
 *  \return a reference to a GPIO instance or \c NULL if no suitable instance has been found.
 */
CGpio * Fac_GetGpio(CFactory * self, uint16_t address, Ucs_Gpio_TriggerEventResultCb_t trigger_event_status_fptr)
{
    CGpio * gpio_inst = NULL;
    CNodeScriptManagement * nsm_inst = NULL;

    if (IS_VALID_ADDR(address))
    {
        gpio_inst = Fac_SearchGpio(self, address);
        if (NULL == gpio_inst)
        {
            nsm_inst = Fac_GetNsm(self, address);
            if (NULL != nsm_inst)
            {
                Gpio_InitData_t gpio_init_data;
                gpio_inst = Fac_GetUninitializedGpio(self);
                if (NULL != gpio_inst)
                {
                    gpio_init_data.nsm_ptr  = nsm_inst;
                    gpio_init_data.inic_ptr = nsm_inst->rsm_ptr->inic_ptr;
                    gpio_init_data.trigger_event_status_fptr = trigger_event_status_fptr;
                    Gpio_Ctor(gpio_inst, &gpio_init_data);
                }
            }
        }
    }

    return gpio_inst;
}

/*! \brief Returns the I2C instance associated with the given address.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with the instance
 *  \param i2c_interrupt_report_fptr User GPIO trigger event status callback function pointer.
 *  \return a reference to an I2C instance or \c NULL if no suitable instance has been found.
 */
CI2c * Fac_GetI2c(CFactory * self, uint16_t address, Ucs_I2c_IntEventReportCb_t i2c_interrupt_report_fptr)
{
    CI2c * i2c_inst = NULL;
    CNodeScriptManagement * nsm_inst = NULL;

    if (IS_VALID_ADDR(address))
    {
        i2c_inst = Fac_SearchI2c (self, address);
        if (NULL == i2c_inst)
        {
            nsm_inst = Fac_GetNsm(self, address); 
            if (nsm_inst != NULL)
            {
                I2c_InitData_t i2c_init_data;
                i2c_inst = Fac_GetUninitializedI2c(self);
                if (NULL != i2c_inst)
                {
                    i2c_init_data.nsm_ptr  = nsm_inst;
                    i2c_init_data.inic_ptr = nsm_inst->rsm_ptr->inic_ptr;
                    i2c_init_data.i2c_interrupt_report_fptr = i2c_interrupt_report_fptr;
                    I2c_Ctor(i2c_inst, &i2c_init_data);
                }
            }
        }
    }

    return i2c_inst;
}

/*! \brief Searches for the INIC instance associated with the given address and returns It if found.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with this instance
 *  \return a reference to the found instance otherwise \c NULL.
 */
CInic * Fac_FindInic(CFactory * self, uint16_t address)
{
    return Fac_SearchFbi (self, address);
}

/*! \brief Searches for the NSM instance associated with the given address and returns It if found.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with this instance
 *  \return a reference to the found instance otherwise \c NULL.
 */
CNodeScriptManagement * Fac_FindNsm(CFactory * self, uint16_t address)
{
    return Fac_SearchNsm (self, address);
}

/*! \brief Searches for the RSM instance associated with the given address and returns It if found.
 *  \param self                      Instance pointer
 *  \param address                   Address of the device associated with this instance
 *  \return a reference to the found instance otherwise \c NULL.
 */
CRemoteSyncManagement * Fac_FindRsm(CFactory * self, uint16_t address)
{
    return Fac_SearchRsm (self, address);
}

/*! \brief  Calls the given function for each instance of inst_type type. If the func_ptr 
 *          returns true the loop is stopped.
 *  \param  self            Reference to a Factory Instance
 *  \param  inst_type       The instance type to be looked for
 *  \param  func_ptr        Reference of the callback function which is called for each node
 *  \param  user_data_ptr   Reference of optional user data pass to the func_ptr
 */
void Fac_Foreach(CFactory * self, Fac_Inst_t inst_type, Fac_ForeachFunc_t func_ptr, void *user_data_ptr)
{
    uint8_t j;
    void * curr_inst = NULL;
    bool exit_loop = false;

    for(j=0U; j<FAC_NUM_DEVICES; j++)
    {
        switch(inst_type)
        {
            case FAC_INST_INIC:
                curr_inst = &self->fbi_list[j];
                if (Fac_IsFbiUninitialized((CInic *)curr_inst))
                {
                    curr_inst = NULL;
                }
                break;

            case FAC_INST_RSM:
                curr_inst = &self->rsm_list[j];
                if (Fac_IsRsmUninitialized((CRemoteSyncManagement *)curr_inst))
                {
                    curr_inst = NULL;
                }
                break;

            case FAC_INST_XRM:
                curr_inst = &self->xrm_list[j];
                if (Fac_IsXrmUninitialized((CExtendedResourceManager *)curr_inst))
                {
                    curr_inst = NULL;
                }
                break;

            case FAC_INST_GPIO:
                curr_inst = &self->gpio_list[j];
                if (Fac_IsGpioUninitialized((CGpio *)curr_inst))
                {
                    curr_inst = NULL;
                }
                break;

            case FAC_INST_I2C:
                curr_inst = &self->i2c_list[j];
                if (Fac_IsI2cUninitialized((CI2c *)curr_inst))
                {
                    curr_inst = NULL;
                }
                break;

            default:
                break;
        }

        if (curr_inst != NULL)
        {
            if (func_ptr(inst_type, curr_inst, user_data_ptr) != false) 
            {
                exit_loop = true;
            }
        }
        else
        {
            exit_loop = true;
        }

        if (exit_loop)
        {
            break;
        }
    }
}

/*------------------------------------------------------------------------------------------------*/
/* Private Methods                                                                                */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Search for the FBI instance associated with the given address and return It.
 *  \param self       Instance pointer
 *  \param address    Address to be looked for
 *  \return a reference to the found FBI or \c NULL if no suitable instance has been found.
 */
static CInic * Fac_SearchFbi(CFactory * self, uint16_t address)
{
    CInic * found_fbi = NULL;
    uint8_t i;
    uint16_t tmp_addr = address;

    if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
    {
        tmp_addr = UCS_ADDR_LOCAL_DEV;
    }

    for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsFbiUninitialized(&self->fbi_list[i])); i++)
    {
        if (tmp_addr == Inic_GetTargetAddress(&self->fbi_list[i]))
        {
            found_fbi = &self->fbi_list[i];
            break;
        }
    }

    return found_fbi;
}

/*! \brief Search for the NSM instance associated with the given address and return It.
 *  \param self       Instance pointer
 *  \param address    Address to be looked for
 *  \return a reference to the found NSM or \c NULL if no suitable instance has been found.
 */
static CNodeScriptManagement * Fac_SearchNsm(CFactory * self, uint16_t address)
{
    CNodeScriptManagement * found_nsm = NULL;
    uint8_t i;
    uint16_t tmp_addr = address;

    if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
    {
        tmp_addr = UCS_ADDR_LOCAL_DEV;
    }

    for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsNsmUninitialized(&self->nsm_list[i])); i++)
    {
        if (tmp_addr == self->nsm_list[i].target_address)
        {
            found_nsm = &self->nsm_list[i];
            break;
        }
    }

    return found_nsm;
}

/*! \brief Search for the RSM instance associated with the given address.
 *  \param self    Instance pointer
 *  \param address Address to be looked for
 *  \return a reference to the found RSM or \c NULL if no suitable instance has been found.
 */
static CRemoteSyncManagement * Fac_SearchRsm(CFactory * self, uint16_t address)
{
    CRemoteSyncManagement * found_rsm = NULL;
    uint8_t i;
    uint16_t tmp_addr = address;

    if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
    {
        tmp_addr = UCS_ADDR_LOCAL_DEV;
    }

    for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsFbiUninitialized(self->rsm_list[i].inic_ptr)); i++)
    {
        if (tmp_addr == Inic_GetTargetAddress(self->rsm_list[i].inic_ptr))
        {
            found_rsm = &self->rsm_list[i];
            break;
        }
    }

    return found_rsm;
}

/*! \brief Search for the XRM instance associated with the given address.
 *  \param self    Instance pointer
 *  \param address    Address to be looked for
 *  \return a reference to the found XRM or \c NULL if no suitable instance has been found.
 */
static CExtendedResourceManager * Fac_SearchXrm(CFactory * self, uint16_t address)
{
    CExtendedResourceManager * found_xrm = NULL;
    uint8_t i;
    uint16_t tmp_addr = address;

    if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
    {
        tmp_addr = UCS_ADDR_LOCAL_DEV;
    }

    for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsXrmUninitialized(&self->xrm_list[i])); i++)
    {
        if (tmp_addr == Inic_GetTargetAddress(self->xrm_list[i].rsm_ptr->inic_ptr))
        {
            found_xrm = &self->xrm_list[i];
            break;
        }
    }

    return found_xrm;
}

/*! \brief Search for the Gpio instance associated with the given address.
 *  \param self    Instance pointer
 *  \param address    Address to be looked for
 *  \return a reference to the found GPIO or \c NULL if no suitable instance has been found.
 */
static CGpio * Fac_SearchGpio(CFactory * self, uint16_t address)
{
    CGpio * found_gpio = NULL;
    uint8_t i;
    uint16_t tmp_addr = address;

    if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
    {
        tmp_addr = UCS_ADDR_LOCAL_DEV;
    }

    for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsGpioUninitialized(&self->gpio_list[i])); i++)
    {
        if (tmp_addr == Inic_GetTargetAddress(self->gpio_list[i].nsm_ptr->rsm_ptr->inic_ptr))
        {
            found_gpio = &self->gpio_list[i];
            break;
        }
    }

    return found_gpio;
}

/*! \brief Search for the I2c instance associated with the given address.
 *  \param self       Instance pointer
 *  \param address    Address to be looked for
 *  \return a reference to the found GPIO or \c NULL if no suitable instance has been found.
 */
static CI2c * Fac_SearchI2c(CFactory * self, uint16_t address)
{
    CI2c * found_i2c = NULL;
    uint8_t i;
    uint16_t tmp_addr = address;

    if ((tmp_addr != UCS_ADDR_LOCAL_DEV) && (Net_IsOwnAddress(self->net_ptr, tmp_addr) == NET_IS_OWN_ADDR_NODE))
    {
        tmp_addr = UCS_ADDR_LOCAL_DEV;
    }

    for (i = 0U; (i<FAC_NUM_DEVICES) && (!Fac_IsI2cUninitialized(&self->i2c_list[i])); i++)
    {
        if (tmp_addr == Inic_GetTargetAddress(self->i2c_list[i].nsm_ptr->rsm_ptr->inic_ptr))
        {
            found_i2c = &self->i2c_list[i];
            break;
        }
    }

    return found_i2c;
}

/*! \brief Returns the next free uninitialized XRM instance
 *  \param self    Instance pointer
 *  \return a reference to the next free uninitialized XRM instance if found, otherwise \c NULL.
 */
static CExtendedResourceManager * Fac_GetUninitializedXrm (CFactory * self)
{
    CExtendedResourceManager * tmp_xrm = NULL;
    uint8_t i;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        if (self->xrm_list[i].rsm_ptr == NULL)
        {
            tmp_xrm = &self->xrm_list[i];
            break;
        }
    }

    return tmp_xrm;
}

/*! \brief Returns the next free uninitialized FBI instance
 *  \param self    Instance pointer
 *  \return a reference to the next free uninitialized FBI instance if found, otherwise \c NULL.
 */
static CInic * Fac_GetUninitializedFbi (CFactory * self)
{
    CInic * tmp_inic = NULL;
    uint8_t i;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        if (self->fbi_list[i].base_ptr == NULL)
        {
            tmp_inic = &self->fbi_list[i];
            break;
        }
    }

    return tmp_inic;
}

/*! \brief Returns the next free uninitialized NSM instance
 *  \param self    Instance pointer
 *  \return a reference to the next free uninitialized NSM instance if found, otherwise \c NULL.
 */
static CNodeScriptManagement * Fac_GetUninitializedNsm (CFactory * self)
{
    CNodeScriptManagement * tmp_nsm = NULL;
    uint8_t i;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        if (self->nsm_list[i].base_ptr == NULL)
        {
            tmp_nsm = &self->nsm_list[i];
            break;
        }
    }

    return tmp_nsm;
}

/*! \brief Returns the next free uninitialized RSM instance
 *  \param self    Instance pointer
 *  \return a reference to the next free uninitialized RSM instance if found, otherwise \c NULL.
 */
static CRemoteSyncManagement * Fac_GetUninitializedRsm (CFactory * self)
{
    CRemoteSyncManagement * tmp_rsm = NULL;
    uint8_t i;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        if (Inic_GetTargetAddress(self->rsm_list[i].inic_ptr) == 0x0U)
        {
            tmp_rsm = &self->rsm_list[i];
            break;
        }
    }

    return tmp_rsm;
}

/*! \brief Returns the next free uninitialized GPIO instance
 *  \param self    Instance pointer
 *  \return a reference to the next free uninitialized GPIO instance if found, otherwise \c NULL.
 */
static CGpio * Fac_GetUninitializedGpio (CFactory * self)
{
    CGpio * tmp_gpio = NULL;
    uint8_t i;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        if (NULL == self->gpio_list[i].nsm_ptr)
        {
            tmp_gpio = &self->gpio_list[i];
            break;
        }
    }

    return tmp_gpio;
}

/*! \brief Returns the next free uninitialized I2C instance
 *  \param self    Instance pointer
 *  \return a reference to the next free uninitialized I2C instance if found, otherwise \c NULL.
 */
static CI2c * Fac_GetUninitializedI2c (CFactory * self)
{
    CI2c * tmp_i2c = NULL;
    uint8_t i;

    for (i = 0U; i<FAC_NUM_DEVICES; i++)
    {
        if (NULL == self->i2c_list[i].nsm_ptr)
        {
            tmp_i2c = &self->i2c_list[i];
            break;
        }
    }

    return tmp_i2c;
}

/*! \brief Constructs the given FBI instance
 *  \param self       the MNS factory Instance pointer
 *  \param fbi        the INIC Instance pointer
 *  \param address    the device address of this FBlock INIC
 */
static void Fac_ConstructFbi (CFactory * self, CInic * fbi, uint16_t address)
{
    Inic_InitData_t inic_init_data;

    if (address == UCS_ADDR_LOCAL_DEV)
    {
        inic_init_data.xcvr_ptr = self->icm_transceiver;
    }
    else
    {
        inic_init_data.xcvr_ptr = self->rcm_transceiver;
    }

    inic_init_data.base_ptr = self->base_ptr;
    inic_init_data.tgt_addr = address;

    Inic_Ctor(fbi, &inic_init_data);
}

/*! \brief Constructs the given NSM instance
 *  \param self       the MNS factory Instance pointer
 *  \param nsm        the NSM Instance pointer
 *  \param address    the device address
 */
static void Fac_ConstructNsm (CFactory * self, CNodeScriptManagement * nsm, uint16_t address)
{
    Nsm_InitData_t nsm_init_data;

    nsm_init_data.base_ptr = self->base_ptr;
    nsm_init_data.rcm_ptr  = self->rcm_transceiver;
    nsm_init_data.rsm_ptr  = Fac_GetRsm(self, address);

    Nsm_Ctor(nsm, &nsm_init_data);
}

/*! \brief Checks whether the given FBlock INIC instance is uninitialized
 *  \param fbi        the INIC Instance pointer
 *  \return \c true if the given Fbi instance is not initialized, otherwise \c False.
 */
static bool Fac_IsFbiUninitialized(CInic * fbi)
{
    return (fbi->base_ptr == NULL) ;
}

/*! \brief Checks whether the given NSM instance is uninitialized
 *  \param nsm        the NSM Instance pointer
 *  \return \c true if the given NSM instance is not initialized, otherwise \c False.
 */
static bool Fac_IsNsmUninitialized(CNodeScriptManagement * nsm)
{
    return (nsm->base_ptr == NULL) ;
}

/*! \brief Checks whether the given RSM instance is uninitialized
 *  \param rsm   Reference to the RSM instance pointer
 *  \return \c true if the given Fbi instance is not initialized, otherwise \c False.
 */
static bool Fac_IsRsmUninitialized(CRemoteSyncManagement * rsm)
{
    return Fac_IsFbiUninitialized(rsm->inic_ptr);
}

/*! \brief Checks whether the given XRM instance is uninitialized
 *  \param xrm        the XRM Instance pointer
 *  \return \c true if the given XRM instance is not initialized, otherwise \c False.
 */
static bool Fac_IsXrmUninitialized(CExtendedResourceManager * xrm)
{
    return (xrm->rsm_ptr == NULL) ;
}

/*! \brief Checks whether the given GPIO instance is uninitialized
 *  \param gpio        the GPIO Instance pointer
 *  \return \c true if the given GPIO instance is not initialized, otherwise \c False.
 */
static bool Fac_IsGpioUninitialized(CGpio * gpio)
{
    return (NULL == gpio->nsm_ptr);
}

/*! \brief Checks whether the given I2C instance is uninitialized
 *  \param i2c        the I2C Instance pointer
 *  \return \c true if the given I2C instance is not initialized, otherwise \c False.
 */
static bool Fac_IsI2cUninitialized(CI2c * i2c)
{
    return (NULL == i2c->nsm_ptr);
}

/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/