ANSC_STATUS
CosaDmlEthPortGetStats
    (
        ANSC_HANDLE                 hContext,
        ULONG                       ulInstanceNumber,
        PCOSA_DML_ETH_STATS         pStats
    )
{
    AnscTraceFlow(("%s...\n", __FUNCTION__));

    if (!pStats)
        return ANSC_STATUS_FAILURE;

    _ansc_memset(pStats, 0, sizeof(COSA_DML_ETH_STATS));
    
    if (ulInstanceNumber == 1)
    {
        pStats->BytesSent = 11;
    }
    else if (ulInstanceNumber == 2)
    {
        pStats->BytesSent = 22;
    }
    else
        return ANSC_STATUS_FAILURE;

    return ANSC_STATUS_SUCCESS;
}
ANSC_STATUS
CosaDmlEthPortGetEntry
    (
        ANSC_HANDLE                 hContext,
        ULONG                       ulIndex,
        PCOSA_DML_ETH_PORT_FULL     pEntry
    )
{
    AnscTraceFlow(("%s...\n", __FUNCTION__));

    if (pEntry)
        _ansc_memset(pEntry, 0, sizeof(COSA_DML_ETH_PORT_FULL));
    else
        return ANSC_STATUS_FAILURE;
    
    if (ulIndex == 0)
    {
        AnscCopyMemory(pEntry, &g_EthPortFull1, sizeof(COSA_DML_ETH_PORT_FULL));
    }
    else if (ulIndex == 1)
    {
        AnscCopyMemory(pEntry, &g_EthPortFull2, sizeof(COSA_DML_ETH_PORT_FULL));
    }
    else
    {
        return ANSC_STATUS_FAILURE;
    }

    return ANSC_STATUS_SUCCESS;
}
ANSC_STATUS
CosaDmlEthPortGetDinfo
    (
        ANSC_HANDLE                 hContext,
        ULONG                       ulInstanceNumber,
        PCOSA_DML_ETH_PORT_DINFO    pInfo
    )
{
    AnscTraceFlow(("%s...\n", __FUNCTION__));

    if (!pInfo)
        return ANSC_STATUS_FAILURE;

    _ansc_memset(pInfo, 0, sizeof(COSA_DML_ETH_PORT_DINFO));
    
    if (ulInstanceNumber == 1)
    {
        AnscCopyMemory(pInfo, &g_EthPortFull1.DynamicInfo, sizeof(COSA_DML_ETH_PORT_DINFO));
        pInfo->Status = COSA_DML_IF_STATUS_Up;
    }
    else if (ulInstanceNumber == 2)
    {
        AnscCopyMemory(pInfo, &g_EthPortFull2.DynamicInfo, sizeof(COSA_DML_ETH_PORT_DINFO));
        pInfo->Status = COSA_DML_IF_STATUS_Up;
    }
    else
        return ANSC_STATUS_FAILURE;
    
    return ANSC_STATUS_SUCCESS;
}
ANSC_STATUS
CosaDeviceInfoInitialize
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                     returnStatus = ANSC_STATUS_SUCCESS;
    PCOSA_DATAMODEL_DEVICEINFO      pMyObject    = (PCOSA_DATAMODEL_DEVICEINFO)hThisObject;

    /* Initiation all functions */
    _ansc_memset(pMyObject->ProvisioningCode, 0, 64);

    CosaDmlDiInit(NULL, NULL);
    #if 1//LNT_EMU
    CosaDmlDiGetXfinityWiFiEnable(&pMyObject->bxfinitywifiEnable);
    printf("%s : bxfinitywifiEnable value is : %d\n",__FUNCTION__,pMyObject->bxfinitywifiEnable);
    #endif
    //Get TR69CertLocation
        CosaDmlDiGetSyndicationTR69CertLocation( (ANSC_HANDLE)pMyObject,
                                                                                          pMyObject->TR69CertLocation );
        //Get Syndication Enable
        CosaDmlDiGetSyndicationEnable( (ANSC_HANDLE)pMyObject,
                                                                    &pMyObject->SyndicationEnable );
        CosaDeriveSyndicationPartnerID(pMyObject->PartnerID);
        CosaDmlDiUiBrandingInit(&pMyObject->UiBrand);


    return returnStatus;
}
ANSC_STATUS
CosaDeviceInfoInitialize
(
    ANSC_HANDLE                 hThisObject
)
{
    ANSC_STATUS                     returnStatus = ANSC_STATUS_SUCCESS;
    PCOSA_DATAMODEL_DEVICEINFO      pMyObject    = (PCOSA_DATAMODEL_DEVICEINFO)hThisObject;

    /* Initiation all functions */
    _ansc_memset(pMyObject->ProvisioningCode, 0, 64);

    CosaDmlDiInit(NULL, NULL);

    return returnStatus;
}
ANSC_STATUS
AnscSctoEngage
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                     returnStatus = ANSC_STATUS_SUCCESS;
    PANSC_SIMPLE_CLIENT_TCP_OBJECT  pMyObject    = (PANSC_SIMPLE_CLIENT_TCP_OBJECT)hThisObject;
    PANSC_SCTO_WORKER_OBJECT        pWorker      = (PANSC_SCTO_WORKER_OBJECT      )pMyObject->hWorker;
    int                             s_error      = 0;
    ansc_socket_addr_in             ansc_client_addr;
    ansc_socket_addr_in             ansc_server_addr;
    xskt_socket_addr_in             xskt_client_addr;
    xskt_socket_addr_in             xskt_server_addr;

#ifdef _ANSC_IPV6_COMPATIBLE_
    ansc_addrinfo                   ansc_hints            = {0};
    ansc_addrinfo*                  pansc_server_addrinfo = NULL;
    ansc_addrinfo*                  pansc_client_addrinfo = NULL;
    xskt_addrinfo                   xskt_hints            = {0};
    xskt_addrinfo*                  pxskt_server_addrinfo = NULL;
    xskt_addrinfo*                  pxskt_client_addrinfo = NULL;
    USHORT                          usPort                = 0;
    char                            port[6] = {0};
#endif

    if ( pMyObject->bActive )
    {
        return  ANSC_STATUS_SUCCESS;
    }
    else if ( !pWorker )
    {
        return  ANSC_STATUS_UNAPPLICABLE;
    }
    else
    {
        /*
         * Just like any other socket-based ANSC object, we will create a separate async recv task
         * which is dedicated to receiving packets. This async recv task is controlled by 'bActive'
         * flag. What if at this moment right before we're about to enable the socket operation and
         * setting 'bActive' flag to TRUE, the old recv task created by the last call of Engage()
         * is still running? While it may not cause crash, but it certainly confuses the owner
         * object because all async recv tasks share the same worker interface. The most obvious
         * solution is to wait for previous recv task to exit before creating a new one.
         */
    	while ( pMyObject->EngineTaskCount != 0 )
        {
            AnscSleep(50);
        }

        pMyObject->bActive = TRUE;
        pMyObject->bClosed = FALSE;
    }

    /*
     * The underlying socket wrapper may require an explicit startup() call, such is the case on
     * Microsoft windows platforms. The wrapper initialization has to done for each task. On most
     * real-time operating systems, this call is not required.
     */
    if ( pMyObject->Mode & ANSC_SCTO_MODE_XSOCKET )
    {
        AnscStartupXsocketWrapper((ANSC_HANDLE)pMyObject);
    }
    else
    {
        AnscStartupSocketWrapper((ANSC_HANDLE)pMyObject);
    }

    #ifdef _ANSC_IPV6_COMPATIBLE_

    if ( pMyObject->Mode & ANSC_SCTO_MODE_XSOCKET )
    {
        xskt_hints.ai_family   = AF_UNSPEC;
        xskt_hints.ai_socktype = XSKT_SOCKET_STREAM;
        xskt_hints.ai_flags    = AI_CANONNAME;

        usPort = pMyObject->GetPeerPort((ANSC_HANDLE)pMyObject);
        _ansc_sprintf(port, "%d", usPort);

        AnscTrace("!!! Peer Port: %s !!!\n", port);

        char * pPeerName = pMyObject->GetPeerName((ANSC_HANDLE)pMyObject);

        AnscTrace("Peer Name: %s!!!\n", pPeerName);

/*
        struct addrinfo hints,*res=NULL;
	memset(&hints,0,sizeof(hints));
	hints.ai_family=PF_UNSPEC;
	hints.ai_socktype=SOCK_DGRAM;
	hints.ai_protocol=IPPROTO_UDP;
	s_error=getaddrinfo("127.0.0.1","123",&hints,&res);
*/
/*
         s_error = _xskt_getaddrinfo
                (
                    "10.74.52.92",
                    port,
                    &xskt_hints,
                    &pxskt_server_addrinfo
                );

        AnscTrace("!!!!!! _xskt_getaddrinfo returns: %d %s !!!\n", s_error, gai_strerror(s_error));
*/
        if ( _xskt_getaddrinfo
                (
                    pMyObject->GetPeerName((ANSC_HANDLE)pMyObject), 
                    port,
                    &xskt_hints,
                    &pxskt_server_addrinfo
                ) ||
             _xskt_getaddrinfo
                (
                    "localhost", 
                    NULL,
                    &xskt_hints,
                    &pxskt_client_addrinfo
                )

            )
        {
            AnscTrace("!!! error 1 !!!\n");

            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	            	(
            		    pWorker->hWorkerContext,
            		    ANSC_SCTOWO_EVENT_SOCKET_ERROR,
            		    (ANSC_HANDLE)NULL
            		);
            }

            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT1;
        }

        AnscTrace("!!! after getaddrinfo !!!\n");
    }
    else
    {
        ansc_hints.ai_family   = AF_UNSPEC;
        ansc_hints.ai_socktype = ANSC_SOCKET_STREAM;
        ansc_hints.ai_flags    = AI_CANONNAME;

        usPort = pMyObject->GetPeerPort((ANSC_HANDLE)pMyObject);
        _ansc_sprintf(port, "%d", usPort);

        if ( _ansc_getaddrinfo
                (
                    pMyObject->GetPeerName((ANSC_HANDLE)pMyObject), 
                    port,
                    &ansc_hints,
                    &pansc_server_addrinfo
                ) ||
             _ansc_getaddrinfo
                (
                    "localhost", 
                    NULL,
                    &ansc_hints,
                    &pansc_client_addrinfo
                )
            )
        {
            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	            	(
            		    pWorker->hWorkerContext,
		                ANSC_SCTOWO_EVENT_SOCKET_ERROR,
            	        (ANSC_HANDLE)NULL
	                );
            }

            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT1;
        }
    }

#endif

    /*
     * To engage the Tcp Client, we need to perform following acts in the respective order:
     *
     *      (1) create the os-dependent socket
     *      (2) bind to the newly socket
     *      (3) connect to the specified  server address
     *      (4) allocate a buffer for receiving
     *      (5) spawn a separate thread and start receiving
     */
    if ( pMyObject->Mode & ANSC_SCTO_MODE_XSOCKET )
    {
#ifdef _ANSC_IPV6_COMPATIBLE_
        pMyObject->Socket = (ANSC_SOCKET)_xskt_socket(pxskt_server_addrinfo->ai_family, pxskt_server_addrinfo->ai_socktype, 0);
#else
        pMyObject->Socket = (ANSC_SOCKET)_xskt_socket(XSKT_SOCKET_AF_INET, XSKT_SOCKET_STREAM, 0);
#endif

        if ( (XSKT_SOCKET)pMyObject->Socket == XSKT_SOCKET_INVALID_SOCKET )
        {
            AnscTrace("!!!!!!!!!! _xskt_socket error !!!!!!!!!!\n");
            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	    			(
		    			pWorker->hWorkerContext,
			    		ANSC_SCTOWO_EVENT_SOCKET_ERROR,
				    	(ANSC_HANDLE)NULL
    				);
            }

            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT1;
        }
        else  AnscTrace("Opening IPv4 socket Ok\n");
    }
    else
    {
#ifdef _ANSC_IPV6_COMPATIBLE_
        pMyObject->Socket = _ansc_socket(pansc_server_addrinfo->ai_family, pansc_server_addrinfo->ai_socktype, 0);
#else
        pMyObject->Socket = _ansc_socket(ANSC_SOCKET_AF_INET, ANSC_SOCKET_STREAM, 0);
#endif

        if ( pMyObject->Socket == ANSC_SOCKET_INVALID_SOCKET )
        {
            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	    			(
		    			pWorker->hWorkerContext,
			    		ANSC_SCTOWO_EVENT_SOCKET_ERROR,
				    	(ANSC_HANDLE)NULL
    				);
            }

            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT1;
        }
    }
    /*
     * Normally we don't need to know which local network interface we shall bind to, and the
     * underlying operating system usually supports such notation as "any address".
     */
#ifndef _ANSC_IPV6_COMPATIBLE_
    if ( pMyObject->Mode & ANSC_SCTO_MODE_XSOCKET )
    {
        xskt_client_addr.sin_family = XSKT_SOCKET_AF_INET;
        xskt_client_addr.sin_port   = _xskt_htons(pMyObject->HostPort);

        if (pMyObject->bSocketBindToDevice && *(pMyObject->SocketDeviceName)) 
        {
            if (_xskt_setsocketopt
                    (
                         pMyObject->Socket, 
                         XSKT_SOCKET_SOL_SOCKET, 
                         XSKT_SOCKET_SO_BINDTODEVICE, 
                         pMyObject->SocketDeviceName, 
                         _ansc_strlen(pMyObject->SocketDeviceName) + 1
                    )
                < 0)
            {
                perror("setsockopt-SOL_SOCKET-SO_BINDTODEVICE");
                returnStatus = ANSC_STATUS_FAILURE;
                goto EXIT2;
            }
        }
        //        fprintf(stderr, "<RT XSKT> Binding socket to Device '%s'.\n", pMyObject->SocketDeviceName); 

        if ( pMyObject->HostAddress.Value == 0 )
        {
            ((pansc_socket_addr_in)&xskt_client_addr)->sin_addr.s_addr = XSKT_SOCKET_ANY_ADDRESS;
        }
        else
        {
            ((pansc_socket_addr_in)&xskt_client_addr)->sin_addr.s_addr = pMyObject->HostAddress.Value;
        }

        if ( _xskt_bind((XSKT_SOCKET)pMyObject->Socket, (xskt_socket_addr*)&xskt_client_addr, sizeof(xskt_client_addr)) != 0 )
        {
            AnscTrace("!!!!!!!!!! _xskt_bind error: socket=%d, error=%d !!!!!!!!!!\n", (XSKT_SOCKET)pMyObject->Socket, errno);
            {
                int j;                 
                char s[256];
                char *ptr1 = ((xskt_socket_addr*)(&xskt_client_addr))->sa_data;
                char stmp[16];

                s[0] = '\0';                  
                for(j=0; j<13; j++) {
                    sprintf(stmp, "%.2x:", *(ptr1++));
                    strcat(s, stmp);
                }
                sprintf(stmp, "%.2x", *ptr1); 
                strcat(s, stmp);
                AnscTrace("!!!!!!!!!! _xskt_bind error: client_addr=%s\n", s);
            }
            perror("_xskt_bind error");

            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	    			(
		    			pWorker->hWorkerContext,
			    		ANSC_SCTOWO_EVENT_SOCKET_ERROR,
				    	(ANSC_HANDLE)NULL
    				);
            }
            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT2;
        }
    }
    else
    {
        ansc_client_addr.sin_family = ANSC_SOCKET_AF_INET;
        ansc_client_addr.sin_port   = _ansc_htons(pMyObject->HostPort);

        if (pMyObject->bSocketBindToDevice && *(pMyObject->SocketDeviceName)) 
        {
            if (_xskt_setsocketopt
                    (
                         pMyObject->Socket, 
                         ANSC_SOCKET_SOL_SOCKET, 
                         ANSC_SOCKET_SO_BINDTODEVICE, 
                         pMyObject->SocketDeviceName, 
                         _ansc_strlen(pMyObject->SocketDeviceName) + 1
                    )
                < 0)
            {
                perror("setsockopt-SOL_SOCKET-SO_BINDTODEVICE");
                returnStatus = ANSC_STATUS_FAILURE;
                goto EXIT2;
            }
        }
        // fprintf(stderr, "<RT AnscSKT> Binding socket to Device '%s'.\n", pMyObject->SocketDeviceName); 


        if ( pMyObject->HostAddress.Value == 0 )
        {
            ansc_client_addr.sin_addr.s_addr = ANSC_SOCKET_ANY_ADDRESS;
        }
        else
        {
            ansc_client_addr.sin_addr.s_addr = pMyObject->HostAddress.Value;
        }

        if ( _ansc_bind(pMyObject->Socket, (ansc_socket_addr*)&ansc_client_addr, sizeof(ansc_client_addr)) != 0 )
        {
            AnscTrace("!!!!!!!!!! _ansc_bind error: socket=%d, error=%d !!!!!!!!!!\n", (XSKT_SOCKET)pMyObject->Socket, errno);
            {
                int j;                 
                char s[256];
                char *ptr1 = ((ansc_socket_addr*)(&ansc_client_addr))->sa_data;
                char stmp[16];

                s[0] = '\0';                  
                for(j=0; j<13; j++) {
                    sprintf(stmp, "%.2x:", *(ptr1++));
                    strcat(s, stmp);
                }
                sprintf(stmp, "%.2x", *ptr1); 
                strcat(s, stmp);
                AnscTrace("!!!!!!!!!! _ansc_bind error: client_addr=%s\n", s);
            }
            perror("_ansc_bind error");

            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	    			(
		    			pWorker->hWorkerContext,
			    		ANSC_SCTOWO_EVENT_SOCKET_ERROR,
				    	(ANSC_HANDLE)NULL
    				);
            }

            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT2;
        }
    }
#endif

    /*
     * As a Tcp client application, we now try to connect the network server, whose address is
     * specified by the "peer address" and "peer port" fields.
     */
    if ( pMyObject->Mode & ANSC_SCTO_MODE_XSOCKET )
    {
#ifdef _ANSC_IPV6_COMPATIBLE_
        BOOL                        bNoConn = TRUE;
#endif

        _ansc_memset(&xskt_server_addr, 0, sizeof(xskt_server_addr));
    	xskt_server_addr.sin_family                                 = XSKT_SOCKET_AF_INET;
        xskt_server_addr.sin_addr.s_addr  = pMyObject->PeerAddress.Value;
        xskt_server_addr.sin_port                                   = _xskt_htons(pMyObject->PeerPort);

#ifdef _ANSC_IPV6_COMPATIBLE_
        while ( bNoConn && pxskt_server_addrinfo )
        {
            if ( _xskt_connect((XSKT_SOCKET)pMyObject->Socket, pxskt_server_addrinfo->ai_addr, pxskt_server_addrinfo->ai_addrlen) != 0 )
            {
                pxskt_server_addrinfo = pxskt_server_addrinfo->ai_next;     /* try next ip address */
            }
            else
            {
                bNoConn = FALSE;
                break;
            }
        }

        if ( bNoConn )
#else
            if ( _xskt_connect((XSKT_SOCKET)pMyObject->Socket, (xskt_socket_addr*)&xskt_server_addr, sizeof(xskt_server_addr)) != 0 )
#endif
        {
            AnscTrace("!!!!!!!!!! _xskt_connect error: socket=%d, error=%d !!!!!!!!!!\n", (XSKT_SOCKET)pMyObject->Socket, errno);
            {
                int j;                 
                char s[256];
                char *ptr1 = ((xskt_socket_addr*)(&xskt_server_addr))->sa_data;
                char stmp[16];

                s[0] = '\0';                  
                for(j=0; j<13; j++) {
                    sprintf(stmp, "%.2x:", *(ptr1++));
                    strcat(s, stmp);
                }
                sprintf(stmp, "%.2x", *ptr1); 
                strcat(s, stmp);
                AnscTrace("!!!!!!!!!! _xskt_connect error: server_addr=%s\n", s);
            }
            perror("_xskt_connect error");

            s_error = _xskt_get_last_error();

            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
	    			(
		    			pWorker->hWorkerContext,
			    		ANSC_SCTOWO_EVENT_SOCKET_TIMEOUT,
				    	(ANSC_HANDLE)NULL
    				);
            }
            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT2;
        }
    }
    else
    {
        _ansc_memset(&ansc_server_addr, 0, sizeof(ansc_server_addr));
        ansc_server_addr.sin_family      = ANSC_SOCKET_AF_INET;
        ansc_server_addr.sin_addr.s_addr = pMyObject->PeerAddress.Value;
        ansc_server_addr.sin_port        = _ansc_htons(pMyObject->PeerPort);

#ifdef _ANSC_IPV6_COMPATIBLE_
        if ( _ansc_connect(pMyObject->Socket, pansc_server_addrinfo->ai_addr, pansc_server_addrinfo->ai_addrlen) != 0 )
#else
        if ( _ansc_connect(pMyObject->Socket, (ansc_socket_addr*)&ansc_server_addr, sizeof(ansc_server_addr)) != 0 )
#endif
        {
            s_error = _ansc_get_last_error();

            AnscTrace("!!! Connect error: %d, %s !!!\n", s_error, strerror(s_error));

            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
				    (
			    		pWorker->hWorkerContext,
		    			ANSC_SCTOWO_EVENT_SOCKET_TIMEOUT,
	    				(ANSC_HANDLE)NULL
    				);
            }

            returnStatus = ANSC_STATUS_FAILURE;

            goto  EXIT2;
        }
    }

    /*
     * We have gone so far that all socket operations succeeded, we want to allocate a buffer that
     * is big enough for any incoming message.
     */
    if ( !pMyObject->RecvBuffer && !(pMyObject->Mode & ANSC_SCTO_MODE_FOREIGN_BUFFER) )
    {
        pMyObject->RecvBuffer     = AnscAllocateMemory(pMyObject->RecvBufferSize);
        pMyObject->RecvPacketSize = 0;
        pMyObject->RecvOffset     = 0;

        if ( !pMyObject->RecvBuffer )
        {
            returnStatus = ANSC_STATUS_RESOURCES;

            goto  EXIT2;
        }
    }

    pMyObject->RecvBytesCount = 0;
    pMyObject->SendBytesCount = 0;
    pMyObject->LastRecvAt     = AnscGetTickInSeconds();
    pMyObject->LastSendAt     = AnscGetTickInSeconds();

    /*
     * If the compilation option '_ANSC_SOCKET_TLS_LAYER_' is enabled, we can simply let the ANSC
     * socket layer to perform the SSL/TLS functionality; otherwise, we need to prepare for doing
     * SSL/TLS internally.
     */
    if ( pMyObject->Mode & ANSC_SCTO_MODE_TLS_ENABLED )
    {
#ifdef _ANSC_USE_OPENSSL_
        pMyObject->bTlsEnabled = TRUE;
        if ( !openssl_init(SSL_CLIENT_CALLS) )
        {
            AnscTrace("AnscSctoEngage - openssl_init() failed!\n");
            returnStatus = ANSC_STATUS_FAILURE;
            goto  EXIT2;
        }

#else
        #ifdef  _ANSC_SOCKET_TLS_LAYER_
        {
            _ansc_en_usetls(pMyObject->Socket);

            pMyObject->bTlsEnabled = FALSE;
        }
        #else
        {
            pMyObject->hTlsScsIf   = (pMyObject->hTlsScsIf != NULL)? pMyObject->hTlsScsIf : AnscSocketTlsGetScsIf();
            pMyObject->bTlsEnabled = TRUE;
        }
        #endif
#endif
    }

    /*
     * To save the worker object from having to deal with blocking/non-blocking/async receiving
     * functions provided by underlying socket layer, we create a separate task to do that.
     */
    returnStatus =
        pMyObject->SpawnTask3
            (
                (ANSC_HANDLE)pMyObject,
                (void*      )pMyObject->RecvTask,
                (ANSC_HANDLE)pMyObject,
                ANSC_SCTO_RECV_TASK_NAME,
                USER_DEFAULT_TASK_PRIORITY,
                11*USER_DEFAULT_TASK_STACK_SIZE

            );
#ifdef _ANSC_USE_OPENSSL_
    if ( pMyObject->bTlsEnabled )
    {
   	    SSL *ssl = NULL;
   	    ssl = openssl_connect (pMyObject->Socket);
	    if ( !ssl )
		{

	        pMyObject->bTlsConnected = FALSE;
	        returnStatus = ANSC_STATUS_FAILURE;

	        //PANSC_SCTO_WORKER_OBJECT        pWorker      = (PANSC_SCTO_WORKER_OBJECT)pMyObject->hWorker;
            if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
            {
                pWorker->Notify
                    (
                        pWorker->hWorkerContext,
                        ANSC_SCTOWO_EVENT_TLS_ERROR,
                        (ANSC_HANDLE)0L
                    );
            }
		}
	    else
	    {
			s_error = openssl_validate_certificate (pMyObject->Socket, pMyObject->HostName, ssl, SSL_CLIENT_CALLS);
		    if ( s_error == 0 )
		    {
		        AnscTrace("AnscSctoEngage - openssl_validate_certificate() failed %p.\n", ssl);

		        //PANSC_SCTO_WORKER_OBJECT        pWorker      = (PANSC_SCTO_WORKER_OBJECT      )pMyObject->hWorker;
                if ( pMyObject->Mode & ANSC_SCTO_MODE_NO_BSP_NOTIFY_CONN_ERR == 0 )
                {
	                pWorker->Notify
	                    (
	                        pWorker->hWorkerContext,
	                        ANSC_SCTOWO_EVENT_TLS_ERROR,
	                        (ANSC_HANDLE)NULL
	                    );
                }
		        returnStatus = ANSC_STATUS_FAILURE;
		    }

		    pMyObject->hTlsConnection = ssl;
		    pMyObject->bTlsConnected = TRUE;
	    }
    }
#else
    /*
     * If SSL/TLS is enabled, we should complete TLS handshake before returning from Engage(). This
     * behavior allows a single consistent API to used between this object and the worker object.
     * Since the handshake will take multiple messages to complete, we need to block the current
     * task until being notified by the TLS module.
     */
    if ( pMyObject->bTlsEnabled && pMyObject->hTlsScsIf )
    {
        pMyObject->bTlsConnected = FALSE;

        pMyObject->InitTlsClient((ANSC_HANDLE)pMyObject);
    }
#endif

    if ( returnStatus != ANSC_STATUS_SUCCESS )
    {
    	goto  EXIT2;
    }

    return  ANSC_STATUS_SUCCESS;


    /******************************************************************
                GRACEFUL ROLLBACK PROCEDURES AND EXIT DOORS
    ******************************************************************/

EXIT2:

	AnscTrace("AnscSctoEngage - failed with status %lu, socket error %d!\n", returnStatus, s_error);

    if ( pMyObject->Mode & ANSC_SCTO_MODE_XSOCKET )
    {
        _xskt_closesocket((XSKT_SOCKET)pMyObject->Socket);
    }
    else
    {
        _ansc_closesocket(pMyObject->Socket);
    }

EXIT1:

    if ( returnStatus != ANSC_STATUS_SUCCESS )
    {
        pMyObject->bActive = FALSE;

        pMyObject->Reset((ANSC_HANDLE)pMyObject);
    }

    return  returnStatus;
}
ANSC_STATUS
CosaUserinterfaceInitialize
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                         returnStatus = ANSC_STATUS_SUCCESS;
    PCOSA_DATAMODEL_USERINTERFACE       pMyObject    = (PCOSA_DATAMODEL_USERINTERFACE)hThisObject;
    PPOAM_IREP_FOLDER_OBJECT        pPoamIrepFoCOSA        = (PPOAM_IREP_FOLDER_OBJECT)NULL;
    PPOAM_IREP_FOLDER_OBJECT        hIrepFolderUIIPRange   = (PPOAM_IREP_FOLDER_OBJECT)NULL;
    PSLAP_VARIABLE                  pSlapVariable          = (PSLAP_VARIABLE          )NULL;
    PCOSA_CONTEXT_LINK_OBJECT       pCosaContext           = (PCOSA_CONTEXT_LINK_OBJECT)NULL;
    PCOSA_DML_UI_IPRANGE_ENTRY      pEntry                 = (PCOSA_DML_UI_IPRANGE_ENTRY)NULL;
    ULONG                           ulEntryCount           = 0;
    ULONG                           ulIndex                = 0;
    ULONG                           ulNextInsNum           = 0;

    /* Initiation all functions */
    _ansc_memset(&pMyObject->RaCfg, 0, sizeof(COSA_DML_RA_CFG));
    
    CosaDmlRaInit(NULL, NULL);

    CosaDmlRaGetCfg(NULL, &pMyObject->RaCfg);

    pMyObject->hIrepFolderCOSA = g_GetRegistryRootFolder(g_pDslhDmlAgent);


    /* Initiation Device.UserInterface.X_CISCO_COM_RemoteAccess.iprange.{i}. */

    AnscSListInitializeHeader(&pMyObject->iprangeList);

    pMyObject->uliprangeNextInsNum = 1;

    pPoamIrepFoCOSA = (PPOAM_IREP_FOLDER_OBJECT)pMyObject->hIrepFolderCOSA;
    if ( !pPoamIrepFoCOSA )
    {
        return ANSC_STATUS_FAILURE;
    }

    hIrepFolderUIIPRange =
        (PPOAM_IREP_FOLDER_OBJECT)pPoamIrepFoCOSA->GetFolder
            (
                (ANSC_HANDLE)pPoamIrepFoCOSA,
                COSA_IREP_FOLDER_NAME_UserInterface_IPRange
            );

    if ( !hIrepFolderUIIPRange )
    {
        hIrepFolderUIIPRange =
            pPoamIrepFoCOSA->AddFolder
                (
                    (ANSC_HANDLE)pPoamIrepFoCOSA,
                    COSA_IREP_FOLDER_NAME_UserInterface_IPRange,
                    0
                );
    }

    if ( !hIrepFolderUIIPRange )
    {
        return ANSC_STATUS_FAILURE;
    }
    else
    {
        pMyObject->hIrepFolderUIIPRange = (ANSC_HANDLE)hIrepFolderUIIPRange;
    }

    /* Retrieve Next Instance Number */
    
    if ( TRUE )
    {
        pSlapVariable =
            (PSLAP_VARIABLE)hIrepFolderUIIPRange->GetRecord
                (
                    (ANSC_HANDLE)hIrepFolderUIIPRange,
                    COSA_DML_RR_NAME_UserInterface_IPRangeNextInsNunmber,
                    NULL
                );

        if ( pSlapVariable )
        {
            pMyObject->uliprangeNextInsNum = pSlapVariable->Variant.varUint32;

            SlapFreeVariable(pSlapVariable);
        }
    }

    /* Initialize middle layer */

    ulEntryCount = CosaDmlUIIPRangeGetNumberOfEntries(NULL);

    for ( ulIndex = 0; ulIndex < ulEntryCount; ulIndex++ )
    {
        pEntry = (PCOSA_DML_UI_IPRANGE_ENTRY)AnscAllocateMemory(sizeof(COSA_DML_UI_IPRANGE_ENTRY));

        if ( !pEntry )
        {
            return ANSC_STATUS_RESOURCES;
        }
        
        CosaDmlUIIPRangeGetEntry(NULL, ulIndex, pEntry);

        if ( TRUE )
        {
            pCosaContext = (PCOSA_CONTEXT_LINK_OBJECT)AnscAllocateMemory(sizeof(COSA_CONTEXT_LINK_OBJECT));

            if ( !pCosaContext )
            {
                AnscFreeMemory(pEntry);
                
                return ANSC_STATUS_RESOURCES;
            }

            if ( pEntry->InstanceNumber != 0 )
            {
                pCosaContext->InstanceNumber = pEntry->InstanceNumber;

                if ( pMyObject->uliprangeNextInsNum <= pEntry->InstanceNumber )
                {
                    pMyObject->uliprangeNextInsNum = pEntry->InstanceNumber + 1;

                    if ( pMyObject->uliprangeNextInsNum == 0 )
                    {
                        pMyObject->uliprangeNextInsNum = 1;
                    }
                }
            }
            else
            {
                pEntry->InstanceNumber = pCosaContext->InstanceNumber = pMyObject->uliprangeNextInsNum;

                pMyObject->uliprangeNextInsNum++;

                if ( pMyObject->uliprangeNextInsNum == 0 )
                {
                    pMyObject->uliprangeNextInsNum = 1;
                }

                /* Generate Alias */
                _ansc_sprintf(pEntry->Alias, "iprange%d", pCosaContext->InstanceNumber);

                CosaDmlUIIPRangeSetValues
                (
                    NULL,
                    ulIndex,
                    pCosaContext->InstanceNumber,
                    pEntry->Alias
                );
            }

            pCosaContext->hContext      = (ANSC_HANDLE)pEntry;
            pCosaContext->hParentTable  = NULL;
            pCosaContext->bNew          = FALSE;
            pCosaContext->hPoamIrepUpperFo = NULL;
            pCosaContext->hPoamIrepFo      = NULL;

            CosaSListPushEntryByInsNum(&pMyObject->iprangeList, pCosaContext);
        }
    }
    
    return returnStatus;
}
ANSC_STATUS
CosaBridgingInitialize
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                     returnStatus        = ANSC_STATUS_SUCCESS;
    PCOSA_DATAMODEL_BRIDGING        pMyObject           = (PCOSA_DATAMODEL_BRIDGING)hThisObject;
    PSLAP_VARIABLE                  pSlapVariable       = (PSLAP_VARIABLE             )NULL;
    PPOAM_IREP_FOLDER_OBJECT        pPoamIrepFoCOSA     = (PPOAM_IREP_FOLDER_OBJECT   )NULL;
    PPOAM_IREP_FOLDER_OBJECT        pPoamIrepFoBRG      = (PPOAM_IREP_FOLDER_OBJECT   )NULL;
    PPOAM_IREP_FOLDER_OBJECT        pPoamIrepFoBRGHA    = (PPOAM_IREP_FOLDER_OBJECT   )NULL;
    PPOAM_IREP_FOLDER_OBJECT        pPoamIrepFoNextIns  = (PPOAM_IREP_FOLDER_OBJECT   )NULL;
    PCOSA_CONTEXT_LINK_OBJECT       pCosaContext        = (PCOSA_CONTEXT_LINK_OBJECT  )NULL;
    PCOSA_CONTEXT_LINK_OBJECT       pCosaContext2       = (PCOSA_CONTEXT_LINK_OBJECT  )NULL;
    PCOSA_CONTEXT_LINK_OBJECT       pCosaContext3       = (PCOSA_CONTEXT_LINK_OBJECT  )NULL;
    PCOSA_DML_BRG_FULL_ALL          pDmlBridge          = (PCOSA_DML_BRG_FULL_ALL     )NULL;
    PCOSA_DML_BRG_PORT_FULL         pPort               = (PCOSA_DML_BRG_PORT_FULL    )NULL;
    ULONG                           ulBridgeCount       = 0;
    ULONG                           ulSubCount          = 0;
    ULONG                           ulIndex             = 0;
    ULONG                           ulSubIndex          = 0;
    char                            FolderName[32]      = {0};
    PCOSA_DML_BRG_VLAN_FULL         pVLAN           = (PCOSA_DML_BRG_VLAN_FULL  )NULL;

    /* Initiation all functions */

//$HL 4/24/2013
    pMyObject->ulNextBridgeInstance = 1;

	AnscSListInitializeHeader(&pMyObject->BridgeList);

    //$HL 4/25/2013
    pMyObject->BridgeList.Depth = 0;
    
    pMyObject->hIrepFolderCOSA = g_GetRegistryRootFolder(g_pDslhDmlAgent);

    pPoamIrepFoCOSA = (PPOAM_IREP_FOLDER_OBJECT)pMyObject->hIrepFolderCOSA;

    if ( !pPoamIrepFoCOSA )
    {
        returnStatus = ANSC_STATUS_FAILURE;

        goto EXIT;
    }

    pPoamIrepFoBRG =
        (PPOAM_IREP_FOLDER_OBJECT)pPoamIrepFoCOSA->GetFolder
            (
			    (ANSC_HANDLE)pPoamIrepFoCOSA,
                COSA_IREP_FOLDER_NAME_BRIDGING
            );

    if ( !pPoamIrepFoBRG )
    {
        pPoamIrepFoBRG =
            pPoamIrepFoCOSA->AddFolder
                (
                    (ANSC_HANDLE)pPoamIrepFoCOSA,
                    COSA_IREP_FOLDER_NAME_BRIDGING,
                    0
                );
    }

    if ( !pPoamIrepFoBRG )
    {
        returnStatus = ANSC_STATUS_FAILURE;

        goto  EXIT;
    }
    else
    {
        pMyObject->hIrepFolderBRG = (ANSC_HANDLE)pPoamIrepFoBRG;
    }

    pPoamIrepFoBRGHA = 
        (PPOAM_IREP_FOLDER_OBJECT)pPoamIrepFoBRG->GetFolder
            (
                (ANSC_HANDLE)pPoamIrepFoCOSA,
                COSA_IREP_FOLDER_NAME_BRIDGINGHA
            );

    if ( !pPoamIrepFoBRGHA )
    {
        pPoamIrepFoBRGHA =
            pPoamIrepFoBRG->AddFolder
                (
                    (ANSC_HANDLE)pPoamIrepFoBRG,
                    COSA_IREP_FOLDER_NAME_BRIDGINGHA,
                    0
                );
    }

    if ( !pPoamIrepFoBRGHA )
    {
        returnStatus = ANSC_STATUS_FAILURE;

        goto  EXIT;
    }
    else
    {
        pMyObject->hIrepFolderBRGHA = (ANSC_HANDLE)pPoamIrepFoBRGHA;
    }

    if ( TRUE )
    {
        _ansc_sprintf
        (
            FolderName, 
            "%s%d", 
            COSA_DML_RR_NAME_Bridge_NextInsNunmber,
            0
        );
        
        pPoamIrepFoNextIns = (PPOAM_IREP_FOLDER_OBJECT)pPoamIrepFoBRG->GetFolder
                                (
                                    (ANSC_HANDLE)pPoamIrepFoBRG,
                                    FolderName
                                );

        AnscZeroMemory(FolderName, 32);

        if ( pPoamIrepFoNextIns )
        {
            pSlapVariable = (PSLAP_VARIABLE)pPoamIrepFoNextIns->GetRecord
                                (
                                    (ANSC_HANDLE)pPoamIrepFoNextIns,
                                    COSA_DML_RR_NAME_Bridge_NextInsNunmber,
                                    NULL
                                );

            if ( pSlapVariable )
            {
                pMyObject->ulNextBridgeInstance = pSlapVariable->Variant.varUint32;
                
                SlapFreeVariable(pSlapVariable);
            }

            pPoamIrepFoNextIns->Remove((ANSC_HANDLE)pPoamIrepFoNextIns);
            pPoamIrepFoNextIns = NULL;
        }
    }

    ulBridgeCount = CosaDmlBrgGetNumberOfEntries(NULL);

    for ( ulIndex = 0; ulIndex < ulBridgeCount; ulIndex++ )
    {
        pDmlBridge = (PCOSA_DML_BRG_FULL_ALL)AnscAllocateMemory(sizeof(COSA_DML_BRG_FULL_ALL));
        _ansc_memset(pDmlBridge, 0, sizeof(COSA_DML_BRG_FULL_ALL));
        if ( !pDmlBridge )
        {
            returnStatus = ANSC_STATUS_RESOURCES;

            goto EXIT;
        }

        AnscSListInitializeHeader(&pDmlBridge->PortList);
        AnscSListInitializeHeader(&pDmlBridge->VLANList);
        AnscSListInitializeHeader(&pDmlBridge->VLANPortList);

        pDmlBridge->ulNextPortInsNum = 1;
        pDmlBridge->ulNextVLANInsNum = 1;
        pDmlBridge->ulNextVLANPortInsNum = 1;

        //$HL 4/25/2013
        if (CosaDmlBrgGetEntry(NULL, ulIndex, (PCOSA_DML_BRG_FULL)pDmlBridge)!= ANSC_STATUS_SUCCESS)
        {
            AnscFreeMemory(pDmlBridge);
            break;    
        }
        else
        //if ( TRUE )
        {
            pCosaContext = (PCOSA_CONTEXT_LINK_OBJECT)AnscAllocateMemory(sizeof(COSA_CONTEXT_LINK_OBJECT));
            _ansc_memset(pCosaContext, 0, sizeof(COSA_CONTEXT_LINK_OBJECT));
            if ( !pCosaContext )
            {
                AnscFreeMemory(pDmlBridge);

                returnStatus = ANSC_STATUS_RESOURCES;

                AnscTraceFlow(("<HL>%s cannot allocate pCosaContext\n", __FUNCTION__));
                goto  EXIT;
            }
            
            if ( pDmlBridge->Cfg.InstanceNumber !=0 )
            {
                pCosaContext->InstanceNumber = pDmlBridge->Cfg.InstanceNumber;

                if ( pMyObject->ulNextBridgeInstance <= pDmlBridge->Cfg.InstanceNumber )
                {
                    pMyObject->ulNextBridgeInstance = pDmlBridge->Cfg.InstanceNumber + 1;

                    if ( pMyObject->ulNextBridgeInstance == 0 )
                    {
                        pMyObject->ulNextBridgeInstance = 1;
                    }
                }
            }
            else
            {
                pDmlBridge->Cfg.InstanceNumber = pCosaContext->InstanceNumber = pMyObject->ulNextBridgeInstance;

                pMyObject->ulNextBridgeInstance++;

                if ( pMyObject->ulNextBridgeInstance == 0)
                {
                    pMyObject->ulNextBridgeInstance = 1;
                }

                _ansc_sprintf(pDmlBridge->Cfg.Alias, "DmlBridge%d", pMyObject->ulNextBridgeInstance);

                CosaDmlBrgSetValues(NULL, ulIndex, pDmlBridge->Cfg.InstanceNumber, pDmlBridge->Cfg.Alias);
            }

            pCosaContext->hContext     = (ANSC_HANDLE)pDmlBridge;
            pCosaContext->hParentTable = NULL;
            pCosaContext->bNew         = FALSE;
            //$HL 4/29/2013
            //pCosaContext->hPoamIrepUpperFo = NULL;
            CosaSListPushEntryByInsNum(&pMyObject->BridgeList, pCosaContext);
        }

        /* Port */
        
        if ( TRUE )
        {
            _ansc_sprintf
                (
                    FolderName, 
                    "%s%d", 
                    COSA_DML_RR_NAME_Bridge_Port_NextInsNumber, 
                    pDmlBridge->Cfg.InstanceNumber
                );

            pPoamIrepFoNextIns = 
                (PPOAM_IREP_FOLDER_OBJECT)pPoamIrepFoBRG->GetFolder
                    (
                        (ANSC_HANDLE)pPoamIrepFoBRG,
                        FolderName
                    );

            if ( pPoamIrepFoNextIns )
            {
                pSlapVariable =
                    (PSLAP_VARIABLE)pPoamIrepFoNextIns->GetRecord
                        (
                            (ANSC_HANDLE)pPoamIrepFoNextIns,
                            COSA_DML_RR_NAME_Bridge_Port_NextInsNumber,
                            NULL
                        );

                if ( pSlapVariable )
                {
                    
                    pDmlBridge->ulNextPortInsNum = pSlapVariable->Variant.varUint32;
                    SlapFreeVariable(pSlapVariable);
                }
                else
                {
                    AnscTraceFlow(("<HL>%s pSlapVariable == NULL\n", __FUNCTION__));
                }

                pPoamIrepFoNextIns->Remove((ANSC_HANDLE)pPoamIrepFoNextIns);
                pPoamIrepFoNextIns = NULL;
            }
            else
            {
                AnscTraceFlow(("<HL>%s pPoamIrepFoNextIns == NULL\n", __FUNCTION__));
            }            
        }

        ulSubCount = CosaDmlBrgPortGetNumberOfEntries(NULL, pDmlBridge->Cfg.InstanceNumber);

        for ( ulSubIndex = 0; ulSubIndex < ulSubCount; ulSubIndex++ )
        {
            pPort = (PCOSA_DML_BRG_PORT_FULL)AnscAllocateMemory(sizeof(COSA_DML_BRG_PORT_FULL));

            if ( !pPort )
            {
                returnStatus = ANSC_STATUS_RESOURCES;

                goto EXIT;
            }

            _ansc_memset(pPort, 0, sizeof(COSA_DML_BRG_PORT_FULL));

            if(CosaDmlBrgPortGetEntry(NULL, pDmlBridge->Cfg.InstanceNumber, ulSubIndex, pPort)
                != ANSC_STATUS_SUCCESS)
            {
                AnscFreeMemory(pPort);
                break;
            }
            else 
            {
                pCosaContext2 = (PCOSA_CONTEXT_LINK_OBJECT)AnscAllocateMemory(sizeof(COSA_CONTEXT_LINK_OBJECT));

                if ( !pCosaContext2 )
                {  
                    AnscFreeMemory(pPort);
                    returnStatus = ANSC_STATUS_RESOURCES;
                    AnscTraceFlow(("<$HL> %s bridge instance:%d cannot allocate any memory for pCosaContext2\n",__FUNCTION__,
                        pDmlBridge->Cfg.InstanceNumber));
                    AnscFreeMemory(pPort);
                    goto EXIT;
                }

                _ansc_memset(pCosaContext2, 0, sizeof(COSA_CONTEXT_LINK_OBJECT));

                if ( pPort->Cfg.InstanceNumber != 0 )
                {
                    pCosaContext2->InstanceNumber = pPort->Cfg.InstanceNumber;

                    if ( pDmlBridge->ulNextPortInsNum <= pPort->Cfg.InstanceNumber )
                    {
                        pDmlBridge->ulNextPortInsNum = pPort->Cfg.InstanceNumber + 1;

                        if ( pDmlBridge->ulNextPortInsNum == 0 )
                        {
                            pDmlBridge->ulNextPortInsNum = 1;
                        }
                    }
                }
                else
                {
                    pCosaContext2->InstanceNumber = pPort->Cfg.InstanceNumber = pDmlBridge->ulNextPortInsNum;

                    pDmlBridge->ulNextPortInsNum++;

                    if ( pDmlBridge->ulNextPortInsNum == 0 )
                    {
                        pDmlBridge->ulNextPortInsNum = 1;
                    }

                    _ansc_sprintf(pPort->Cfg.Alias, "Port%d", pCosaContext2->InstanceNumber);

                    CosaDmlBrgPortSetValues
                        (
                            NULL,
                            pDmlBridge->Cfg.InstanceNumber,
                            ulSubIndex,
                            pPort->Cfg.InstanceNumber,
                            pPort->Cfg.Alias
                        );
                }

                pCosaContext2->hContext     = (ANSC_HANDLE)pPort;
                pCosaContext2->hParentTable = (ANSC_HANDLE)pDmlBridge;
                pCosaContext2->bNew         = FALSE;

                CosaSListPushEntryByInsNum(&pDmlBridge->PortList, pCosaContext2);
            }
        }
    

        //$HL 7/3/2013
        /* VLAN */
        
        if ( TRUE )
        {
            _ansc_sprintf
                (
                    FolderName, 
                    "%s%d", 
                    COSA_DML_RR_NAME_Bridge_VLAN_NextInsNumber, 
                    pDmlBridge->Cfg.InstanceNumber
                );

            pPoamIrepFoNextIns = 
                (PPOAM_IREP_FOLDER_OBJECT)pPoamIrepFoBRG->GetFolder
                    (
                        (ANSC_HANDLE)pPoamIrepFoBRG,
                        FolderName
                    );

            if ( pPoamIrepFoNextIns )
            {
                pSlapVariable =
                    (PSLAP_VARIABLE)pPoamIrepFoNextIns->GetRecord
                        (
                            (ANSC_HANDLE)pPoamIrepFoNextIns,
                            COSA_DML_RR_NAME_Bridge_VLAN_NextInsNumber,
                            NULL
                        );

                if ( pSlapVariable )
                {
                    
                    pDmlBridge->ulNextVLANInsNum = pSlapVariable->Variant.varUint32;
                    SlapFreeVariable(pSlapVariable);
                }
                else
                {
                    AnscTraceFlow(("<HL>%s pSlapVariable == NULL\n", __FUNCTION__));
                }

                pPoamIrepFoNextIns->Remove((ANSC_HANDLE)pPoamIrepFoNextIns);
                pPoamIrepFoNextIns = NULL;
            }
            else
            {
                AnscTraceFlow(("<HL>%s pPoamIrepFoNextIns == NULL\n", __FUNCTION__));
            }            
        }

        ulSubCount = CosaDmlBrgVlanGetNumberOfEntries(NULL, pDmlBridge->Cfg.InstanceNumber);

        for ( ulSubIndex = 0; ulSubIndex < ulSubCount; ulSubIndex++ )
        {
            pVLAN = (PCOSA_DML_BRG_VLAN_FULL)AnscAllocateMemory(sizeof(COSA_DML_BRG_VLAN_FULL));

            if ( !pVLAN )
            {
                returnStatus = ANSC_STATUS_RESOURCES;

                goto EXIT;
            }

            _ansc_memset(pVLAN, 0, sizeof(COSA_DML_BRG_VLAN_FULL));

            if(CosaDmlBrgVlanGetEntry(NULL, pDmlBridge->Cfg.InstanceNumber, ulSubIndex, pVLAN)
                != ANSC_STATUS_SUCCESS)
            {
                AnscFreeMemory(pVLAN);
                break;
            }
            else 
            {
                pCosaContext3 = (PCOSA_CONTEXT_LINK_OBJECT)AnscAllocateMemory(sizeof(COSA_CONTEXT_LINK_OBJECT));

                if ( !pCosaContext3 )
                {  
                    returnStatus = ANSC_STATUS_RESOURCES;
                    AnscTraceFlow(("<$HL> %s bridge instance:%d cannot allocate any memory for pCosaContext3\n",__FUNCTION__,
                        pDmlBridge->Cfg.InstanceNumber));
                    AnscFreeMemory(pVLAN);
                    goto EXIT;
                }

                _ansc_memset(pCosaContext3, 0, sizeof(COSA_CONTEXT_LINK_OBJECT));

                if ( pVLAN->Cfg.InstanceNumber != 0 )
                {
                    pCosaContext3->InstanceNumber = pVLAN->Cfg.InstanceNumber;

                    if ( pDmlBridge->ulNextVLANInsNum <= pVLAN->Cfg.InstanceNumber )
                    {
                        pDmlBridge->ulNextVLANInsNum = pVLAN->Cfg.InstanceNumber + 1;

                        if ( pDmlBridge->ulNextVLANInsNum == 0 )
                        {
                            pDmlBridge->ulNextVLANInsNum = 1;
                        }
                    }
                }
                else
                {
                    pCosaContext2->InstanceNumber = pVLAN->Cfg.InstanceNumber = pDmlBridge->ulNextPortInsNum;

                    pDmlBridge->ulNextVLANInsNum++;

                    if ( pDmlBridge->ulNextVLANInsNum == 0 )
                    {
                        pDmlBridge->ulNextVLANInsNum = 1;
                    }

                    _ansc_sprintf(pVLAN->Cfg.Alias, "VLAN%d", pCosaContext2->InstanceNumber);

                    CosaDmlBrgVlanSetValues //new sbAPI
                        (
                            NULL,
                            pDmlBridge->Cfg.InstanceNumber,
                            ulSubIndex,
                            pVLAN->Cfg.InstanceNumber,
                            pVLAN->Cfg.Alias
                        );
                }

                pCosaContext3->hContext     = (ANSC_HANDLE)pVLAN;
                pCosaContext3->hParentTable = (ANSC_HANDLE)pDmlBridge;
                pCosaContext3->bNew         = FALSE;

                CosaSListPushEntryByInsNum(&pDmlBridge->VLANList, pCosaContext3);
            }
        }
    }
    //$HL 4/29/2013
    CosaBridgingRegGetInfo((ANSC_HANDLE)pMyObject);

EXIT:

    return returnStatus;
}