BOOL
CcspCwmppoGetInitialContactFactory
    (
        ANSC_HANDLE                 hThisObject
    )
{
    PCCSP_CWMP_PROCESSOR_OBJECT      pMyObject              = (PCCSP_CWMP_PROCESSOR_OBJECT  )hThisObject;
    PCCSP_CWMP_CPE_CONTROLLER_OBJECT pCcspCwmpCpeController = (PCCSP_CWMP_CPE_CONTROLLER_OBJECT )pMyObject->hCcspCwmpCpeController;
    BOOL                             bInitialContactFactory = FALSE;

	if ( !s_bICLoaded )
	{
		char*						pValue               = NULL;
	    char                        psmKeyPrefixed[CCSP_TR069PA_PSM_NODE_NAME_MAX_LEN + 16];

    	CcspCwmpPrefixPsmKey(psmKeyPrefixed, pCcspCwmpCpeController->SubsysName, CCSP_TR069PA_PSM_KEY_InitialContactFactory);
	    pValue =
    	    pCcspCwmpCpeController->LoadCfgFromPsm
	            (
                	(ANSC_HANDLE)pCcspCwmpCpeController,
            	    psmKeyPrefixed
        	    );

    	bInitialContactFactory = pValue ? _ansc_atoi(pValue) : TRUE;

        CcspTr069PaTraceWarning(("InitialContact read from PSM is: <%s>\n", bInitialContactFactory ? "TRUE":"FALSE")); 

    	if ( pValue )
	    {
    	    CcspTr069PaFreeMemory(pValue);
	    }	
	}

    return  bInitialContactFactory;
}
ANSC_STATUS
CcspCwmppoSetInitialContact
    (
        ANSC_HANDLE                 hThisObject,
        BOOL                        bEnabled
    )
{
    ANSC_STATUS                     returnStatus       = ANSC_STATUS_SUCCESS;
    PCCSP_CWMP_PROCESSOR_OBJECT     pMyObject          = (PCCSP_CWMP_PROCESSOR_OBJECT  )hThisObject;
    PCCSP_CWMP_PROCESSOR_PROPERTY   pProperty          = (PCCSP_CWMP_PROCESSOR_PROPERTY)&pMyObject->Property;
    PCCSP_CWMP_CPE_CONTROLLER_OBJECT pCcspCwmpCpeController = (PCCSP_CWMP_CPE_CONTROLLER_OBJECT )pMyObject->hCcspCwmpCpeController;
    char                            psmKeyPrefixed[CCSP_TR069PA_PSM_NODE_NAME_MAX_LEN + 16];

    CcspCwmpPrefixPsmKey(psmKeyPrefixed, pCcspCwmpCpeController->SubsysName, CCSP_TR069PA_PSM_KEY_InitialContact);

    pProperty->bInitialContact = bEnabled;

    returnStatus =
        pCcspCwmpCpeController->SaveCfgToPsm
            (
                (ANSC_HANDLE)pCcspCwmpCpeController,
                psmKeyPrefixed,
                bEnabled ? "1" : "0"
            );

    return  returnStatus;
}
ANSC_STATUS
CcspCwmpsoSaveCwmpEvent
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                     returnStatus       = ANSC_STATUS_SUCCESS;
    PCCSP_CWMP_SESSION_OBJECT        pMyObject          = (PCCSP_CWMP_SESSION_OBJECT   )hThisObject;
    PCCSP_CWMP_EVENT                pCcspCwmpEvent     = (PCCSP_CWMP_EVENT           )NULL;
    PCCSP_CWMP_CPE_CONTROLLER_OBJECT     pCcspCwmpCpeController = (PCCSP_CWMP_CPE_CONTROLLER_OBJECT)pMyObject->hCcspCwmpCpeController;
    PCCSP_CWMP_CFG_INTERFACE             pCcspCwmpCfgIf         = (PCCSP_CWMP_CFG_INTERFACE        )pCcspCwmpCpeController->GetCcspCwmpCfgIf((ANSC_HANDLE)pCcspCwmpCpeController);
    char                            pSavedEvents[CCSP_CWMP_SAVED_EVENTS_MAX_LEN] = { 0 };
    PCHAR                           pCurBuf            = pSavedEvents;
    ULONG                           i                  = 0;
    ULONG                           uLength            = 0;
    char*                           pRootObjName       = pCcspCwmpCpeController->GetRootObject((ANSC_HANDLE)pCcspCwmpCpeController);
    BOOL                            bRootDevice        = AnscEqualString(pRootObjName, DM_ROOTNAME, FALSE);

    /*
     * All the undiscarded events will be saved in a string seperated by a ',' and put as 
     * the value of parameter "(InternetGateway)Device.ManagementServer.SavedEvents".
     */
    for( i = 0; i < pMyObject->EventCount; i ++)
    { 
        pCcspCwmpEvent = (PCCSP_CWMP_EVENT)pMyObject->EventArray[i];

        if( CcspCwmpIsUndiscardedEvent(pCcspCwmpEvent->EventCode))
        {
            if( pCurBuf != pSavedEvents)
            {
                pCurBuf[0] = ',';
                pCurBuf ++;
            }

            uLength = AnscSizeOfString(pCcspCwmpEvent->EventCode);

            if( pCurBuf + uLength >= pSavedEvents + CCSP_CWMP_SAVED_EVENTS_MAX_LEN)
            {
                CcspTr069PaTraceWarning(("Too many events to save, ignored.\n"));

                break;
            }

            AnscCopyMemory(pCurBuf, pCcspCwmpEvent->EventCode, uLength);
            pCurBuf   += uLength;
            pCurBuf[0]=  '+';
            pCurBuf   ++;

            if( pCcspCwmpEvent->CommandKey )
            {
                uLength = AnscSizeOfString(pCcspCwmpEvent->CommandKey);

                if( uLength > 0)
                {
                    if( pCurBuf + uLength >= pSavedEvents + CCSP_CWMP_SAVED_EVENTS_MAX_LEN)
                    {
                        CcspTr069PaTraceWarning(("Too many events to save, ignored.\n"));

                        break;
                    }

                    AnscCopyMemory(pCurBuf, pCcspCwmpEvent->EventCode, uLength);
                    pCurBuf   += uLength;
                }
            }
        }
    }

    if( AnscSizeOfString(pSavedEvents) > 0)
    {
        char                        psmKeyPrefixed[CCSP_TR069PA_PSM_NODE_NAME_MAX_LEN + 16];

        CcspCwmpPrefixPsmKey(psmKeyPrefixed, pCcspCwmpCpeController->SubsysName, CCSP_TR069PA_PSM_KEY_SavedEvents);

        CcspTr069PaTraceDebug(("Save the event to the registry before reboot:\n%s\n", pSavedEvents));

        returnStatus = 
            pCcspCwmpCpeController->SaveCfgToPsm
                (
                    (ANSC_HANDLE)pCcspCwmpCpeController,
                    psmKeyPrefixed,
                    pSavedEvents
                );
    }

    return  returnStatus;
}