Exemplo n.º 1
0
bool sysfTmrCreate(void)
{
	NCS_PATRICIA_PARAMS pat_param;
	uint32_t rc = NCSCC_RC_SUCCESS;

	if (ncs_tmr_create_done == false)
		ncs_tmr_create_done = true;
	else
		return true;

	/* Empty Timer Service control block. */
	memset(&gl_tcb, '\0', sizeof(SYSF_TMR_CB));

	/* put local persistent guard in start state */
	ncslpg_create(&gl_tcb.persist);

	/* Initialize the locks */
	m_NCS_LOCK_INIT(&gl_tcb.safe.enter_lock);
	m_NCS_LOCK_INIT(&gl_tcb.safe.free_lock);

	memset((void *)&pat_param, 0, sizeof(NCS_PATRICIA_PARAMS));

	pat_param.key_size = sizeof(uint64_t);

	rc = ncs_patricia_tree_init(&gl_tcb.tmr_pat_tree, &pat_param);
	if (rc != NCSCC_RC_SUCCESS) {
		return NCSCC_RC_FAILURE;
	}

	rc = m_NCS_SEL_OBJ_CREATE(&gl_tcb.sel_obj);
	if (rc != NCSCC_RC_SUCCESS) {
		ncs_patricia_tree_destroy(&gl_tcb.tmr_pat_tree);
		return NCSCC_RC_FAILURE;
	}
	tmr_destroying = false;

	/* create expiry thread */

	int policy = SCHED_RR; /*root defaults */
	int max_prio = sched_get_priority_max(policy);
	int min_prio = sched_get_priority_min(policy);
	int prio_val = ((max_prio - min_prio) * 0.87); 

	if (m_NCS_TASK_CREATE((NCS_OS_CB)ncs_tmr_wait,
			      0,
			      (char *)"OSAF_TMR",
			      prio_val, policy, NCS_TMR_STACKSIZE, &gl_tcb.p_tsk_hdl) != NCSCC_RC_SUCCESS) {
		ncs_patricia_tree_destroy(&gl_tcb.tmr_pat_tree);
		m_NCS_SEL_OBJ_DESTROY(&gl_tcb.sel_obj);
		return false;
	}

	if (m_NCS_TASK_START(gl_tcb.p_tsk_hdl) != NCSCC_RC_SUCCESS) {
		m_NCS_TASK_RELEASE(gl_tcb.p_tsk_hdl);
		ncs_patricia_tree_destroy(&gl_tcb.tmr_pat_tree);
		m_NCS_SEL_OBJ_DESTROY(&gl_tcb.sel_obj);
		return false;
	}
	return true;
}
Exemplo n.º 2
0
/****************************************************************************
  Name          : avnd_mon_task_create

  Description   : This routine creates & starts AvND Passive monitoring task.

  Arguments     : None.

  Return Values : NCSCC_RC_SUCCESS/NCSCC_RC_FAILURE

  Notes         : None
******************************************************************************/
uns32 avnd_mon_task_create(void)
{
	uns32 rc;

	/* create avnd task */
	rc = m_NCS_TASK_CREATE((NCS_OS_CB)avnd_mon_process, NULL,
			       "AVND_MON", m_AVND_TASK_PRIORITY, m_AVND_STACKSIZE, &gl_avnd_mon_task_hdl);
	if (NCSCC_RC_SUCCESS != rc) {
		LOG_CR("Passive Monitoring thread CREATE failed");
		goto err;
	}
	TRACE_1("Created Passive Monitoring thread");

	/* now start the task */
	rc = m_NCS_TASK_START(gl_avnd_mon_task_hdl);
	if (NCSCC_RC_SUCCESS != rc) {
		LOG_CR("Passive Monitoring thread START failed");
		goto err;
	}
	TRACE_1("Started Passive Monitoring thread");

	return rc;

 err:
	/* destroy the task */
	if (gl_avnd_mon_task_hdl) {
		/* release the task */
		m_NCS_TASK_RELEASE(gl_avnd_mon_task_hdl);

		gl_avnd_mon_task_hdl = 0;
	}

	return rc;
}
Exemplo n.º 3
0
/****************************************************************************
  Name          : pxy_pxd_amf_init
 
  Description   : This routine creates & starts the AMF interface task.
 
  Arguments     : None.
 
  Return Values : NCSCC_RC_SUCCESS/NCSCC_RC_FAILURE
 
  Notes         : None.
******************************************************************************/
uns32 pxy_pxd_amf_init(void)
{
   uns32 rc = NCSCC_RC_SUCCESS;

   /* Create AMF interface task */
   rc = m_NCS_TASK_CREATE ( (NCS_OS_CB)pxy_pxd_amf_process, (void *)0, "PXY-PXD",
                            PXY_PXD_TASK_PRIORITY, PXY_PXD_STACKSIZE, 
                            &gl_amf_task_hdl );
   if ( NCSCC_RC_SUCCESS != rc )
   {
      goto err;
   }

   /* Start the task */
   rc = m_NCS_TASK_START (gl_amf_task_hdl);
   if ( NCSCC_RC_SUCCESS != rc )
   {
      goto err;
   }

   printf("\n\n AMF-INTF TASK CREATION SUCCESS !!! \n\n");

   return rc;

err:
   /* destroy the task */
   if (gl_amf_task_hdl) m_NCS_TASK_RELEASE(gl_amf_task_hdl);
   printf("\n\n AMF-INTF TASK CREATION FAILED !!! \n\n");

   return rc;
}
Exemplo n.º 4
0
/**
 * Function to create the rcv task thread
 *
 * @param task_hdl
 *
 * @return NCSCC_RC_SUCCESS
 * @return NCSCC_RC_FAILURE
 *
 */
static uint32_t dtm_intranode_create_rcv_task(int task_hdl)
{
	/*
	   STEP 1: Create a recv task which will accept the connections, recv data from the local nodes */

	TRACE_ENTER();
	
	int policy = SCHED_RR; /*root defaults */
	int max_prio = sched_get_priority_max(policy);
	int min_prio = sched_get_priority_min(policy);
	int prio_val = ((max_prio - min_prio) * 0.87); 

	if (m_NCS_TASK_CREATE((NCS_OS_CB)dtm_intranode_processing,
			      (NCSCONTEXT)(long)task_hdl,
			      (char *)"OSAF_DTM_INTRANODE",
			      prio_val, policy, DTM_INTRANODE_STACKSIZE,
			      &dtm_intranode_cb->dtm_intranode_hdl_task) != NCSCC_RC_SUCCESS) {
		LOG_ER("Intra NODE Task Creation-failed");
		return NCSCC_RC_FAILURE;
	}

	/* Start the created task,
	 *   if start fails,
	 *        release the task by calling the NCS task release function*/
	if (m_NCS_TASK_START(dtm_intranode_cb->dtm_intranode_hdl_task) != NCSCC_RC_SUCCESS) {
		LOG_ER("DTM:Start of the Created Task-failed");
		m_NCS_TASK_RELEASE(dtm_intranode_cb->dtm_intranode_hdl_task);
		return NCSCC_RC_FAILURE;
	}
	/* return NCS success */
	TRACE_LEAVE();
	return NCSCC_RC_SUCCESS;
}
Exemplo n.º 5
0
NCS_BOOL sysfTmrCreate(void)
{
	NCS_PATRICIA_PARAMS pat_param;
	uns32 rc = NCSCC_RC_SUCCESS;

	if (ncs_tmr_create_done == FALSE)
		ncs_tmr_create_done = TRUE;
	else
		return TRUE;

	/* Empty Timer Service control block. */
	memset(&gl_tcb, '\0', sizeof(SYSF_TMR_CB));

	/* put local persistent guard in start state */
	ncslpg_create(&gl_tcb.persist);

	/* Initialize the locks */
	m_NCS_LOCK_INIT(&gl_tcb.safe.enter_lock);
	m_NCS_LOCK_INIT(&gl_tcb.safe.free_lock);

	memset((void *)&pat_param, 0, sizeof(NCS_PATRICIA_PARAMS));

	pat_param.key_size = sizeof(uns64);

	rc = ncs_patricia_tree_init(&gl_tcb.tmr_pat_tree, &pat_param);
	if (rc != NCSCC_RC_SUCCESS) {
		return NCSCC_RC_FAILURE;
	}

	rc = m_NCS_SEL_OBJ_CREATE(&gl_tcb.sel_obj);
	if (rc != NCSCC_RC_SUCCESS) {
		ncs_patricia_tree_destroy(&gl_tcb.tmr_pat_tree);
		return NCSCC_RC_FAILURE;
	}
	tmr_destroying = FALSE;

	/* create expiry thread */

	if (m_NCS_TASK_CREATE((NCS_OS_CB)ncs_tmr_wait,
			      0,
			      NCS_TMR_TASKNAME,
			      NCS_TMR_PRIORITY, NCS_TMR_STACKSIZE, &gl_tcb.p_tsk_hdl) != NCSCC_RC_SUCCESS) {
		ncs_patricia_tree_destroy(&gl_tcb.tmr_pat_tree);
		m_NCS_SEL_OBJ_DESTROY(gl_tcb.sel_obj);
		return FALSE;
	}

	if (m_NCS_TASK_START(gl_tcb.p_tsk_hdl) != NCSCC_RC_SUCCESS) {
		m_NCS_TASK_RELEASE(gl_tcb.p_tsk_hdl);
		ncs_patricia_tree_destroy(&gl_tcb.tmr_pat_tree);
		m_NCS_SEL_OBJ_DESTROY(gl_tcb.sel_obj);
		return FALSE;
	}
	return TRUE;
}
Exemplo n.º 6
0
/**************************************************************************\
 *
 * start_exec_mod_cb
 *
 * Description: Initialize execute module control block.
 *
 * Synopsis:
 *
 * Call Arguments:
 *   SUCCESS/FAILURE
 *
 * Returns:
 *   None.
 *
 * Notes:
 *
\**************************************************************************/
uns32 start_exec_mod_cb(void)
{
	NCS_PATRICIA_PARAMS pt_params;
	int spair[2];

	pt_params.key_size = sizeof(uns32);

	if (ncs_patricia_tree_init(&module_cb.pid_list, &pt_params) != NCSCC_RC_SUCCESS) {
		return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);
	}

	if (0 != socketpair(AF_UNIX, SOCK_DGRAM, 0, spair)) {
		perror("init_exec_mod_cb: socketpair: ");
		return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);
	}

	module_cb.read_fd = spair[0];
	module_cb.write_fd = spair[1];

	/* Create a task which will handle the signal and give call back  */

	if (m_NCS_TASK_CREATE((NCS_OS_CB)ncs_exec_mod_hdlr,
			      0,
			      NCS_EXEC_MOD_TASKNAME,
			      NCS_EXEC_MOD_PRIORITY,
			      NCS_EXEC_MOD_STACKSIZE, &module_cb.em_task_handle) != NCSCC_RC_SUCCESS) {
		return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);;
	}

	if (m_NCS_TASK_START(module_cb.em_task_handle) != NCSCC_RC_SUCCESS) {
		m_NCS_TASK_RELEASE(module_cb.em_task_handle);
		return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);;
	}

	module_cb.init = TRUE;

	m_NCS_SIGNAL(SIGCHLD, ncs_exec_module_signal_hdlr);

	return NCSCC_RC_SUCCESS;

}
void thread_creation(SaEvtHandleT *ptrEvtHandle)
{
  gl_threadEvtHandle=*ptrEvtHandle;
  gl_tCount++;

  if(gl_tCount<2)
    {
      gl_rc = m_NCS_TASK_CREATE((NCS_OS_CB)eda_selection_thread,NULL,
                                "edsv_test", 8, 8000, &eda_thread_handle);
      if (gl_rc != NCSCC_RC_SUCCESS)
        {
          printf("\nthread cannot be created");
          return ;
        }
      printf("\nThread is created");
      gl_rc = m_NCS_TASK_START(eda_thread_handle);
      if (gl_rc != NCSCC_RC_SUCCESS)
        {
          printf("\nthread cannot be started");
          return ;
        }
    }
}
Exemplo n.º 8
0
/****************************************************************************
 * Name          : gld_se_lib_init
 *
 * Description   : Invoked to Initialize the GLD
 *                 
 *
 * Arguments     : 
 * Return Values : NCSCC_RC_SUCCESS/NCSCC_RC_FAILURE..
 *
 * Notes         : None.
 *****************************************************************************/
uns32 gld_se_lib_init(NCS_LIB_REQ_INFO *req_info)
{
	GLSV_GLD_CB *gld_cb;
	SaAisErrorT amf_error;
	uns32 res = NCSCC_RC_SUCCESS;
	SaAmfHealthcheckKeyT Healthy;
	int8 *health_key;

	/* Register with Logging subsystem */
	if (NCS_GLSV_LOG == 1)
		gld_flx_log_reg();

	/* Allocate and initialize the control block */
	gld_cb = m_MMGR_ALLOC_GLSV_GLD_CB;

	if (gld_cb == NULL) {
		m_LOG_GLD_MEMFAIL(GLD_CB_ALLOC_FAILED, __FILE__, __LINE__);
		return NCSCC_RC_FAILURE;
	}
	memset(gld_cb, 0, sizeof(GLSV_GLD_CB));

	/* TBD- Pool id is to be set */
	gl_gld_hdl = gld_cb->my_hdl = ncshm_create_hdl(gld_cb->hm_poolid, NCS_SERVICE_ID_GLD, (NCSCONTEXT)gld_cb);
	if (0 == gld_cb->my_hdl) {
		m_LOG_GLD_HEADLINE(GLD_CREATE_HANDLE_FAILED, NCSFL_SEV_ERROR, __FILE__, __LINE__, 0);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		return NCSCC_RC_FAILURE;
	}

	/* Initialize the cb parameters */
	if (gld_cb_init(gld_cb) != NCSCC_RC_SUCCESS) {
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		return NCSCC_RC_FAILURE;
	}

	/* Initialize amf framework */
	if (gld_amf_init(gld_cb) != NCSCC_RC_SUCCESS) {
		m_LOG_GLD_SVC_PRVDR(GLD_AMF_INIT_ERROR, NCSFL_SEV_ERROR, __FILE__, __LINE__);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		return NCSCC_RC_FAILURE;
	}
	m_LOG_GLD_SVC_PRVDR(GLD_AMF_INIT_SUCCESS, NCSFL_SEV_INFO, __FILE__, __LINE__);

	/* Bind to MDS */
	if (gld_mds_init(gld_cb) != NCSCC_RC_SUCCESS) {
		saAmfFinalize(gld_cb->amf_hdl);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		m_LOG_GLD_SVC_PRVDR(GLD_MDS_INSTALL_FAIL, NCSFL_SEV_ERROR, __FILE__, __LINE__);
		return NCSCC_RC_FAILURE;
	} else
		m_LOG_GLD_SVC_PRVDR(GLD_MDS_INSTALL_SUCCESS, NCSFL_SEV_INFO, __FILE__, __LINE__);

	/*   Initialise with the MBCSV service  */
	if (glsv_gld_mbcsv_register(gld_cb) != NCSCC_RC_SUCCESS) {
		m_LOG_GLD_MBCSV(GLD_MBCSV_INIT_FAILED, NCSFL_SEV_ERROR, __FILE__, __LINE__);
		gld_mds_shut(gld_cb);
		saAmfFinalize(gld_cb->amf_hdl);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		return NCSCC_RC_FAILURE;

	} else {
		m_LOG_GLD_MBCSV(GLD_MBCSV_INIT_SUCCESS, NCSFL_SEV_INFO, __FILE__, __LINE__);

	}

	/* register glsv with imm */
	amf_error = gld_imm_init(gld_cb);
	if (amf_error != SA_AIS_OK) {
		glsv_gld_mbcsv_unregister(gld_cb);
		gld_mds_shut(gld_cb);
		saAmfFinalize(gld_cb->amf_hdl);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		gld_log(NCSFL_SEV_ERROR, "Imm Init Failed %u\n", amf_error);
		return NCSCC_RC_FAILURE;
	}

	/* TASK CREATION AND INITIALIZING THE MAILBOX */
	if ((m_NCS_IPC_CREATE(&gld_cb->mbx) != NCSCC_RC_SUCCESS) ||
	    (m_NCS_IPC_ATTACH(&gld_cb->mbx) != NCSCC_RC_SUCCESS) ||
	    (m_NCS_TASK_CREATE((NCS_OS_CB)gld_main_process,
			       &gld_cb->mbx, "GLD", m_GLD_TASK_PRIORITY,
			       m_GLD_STACKSIZE,
			       &gld_cb->task_hdl) != NCSCC_RC_SUCCESS) ||
	    (m_NCS_TASK_START(gld_cb->task_hdl) != NCSCC_RC_SUCCESS)) {
		m_LOG_GLD_HEADLINE(GLD_IPC_TASK_INIT, NCSFL_SEV_ERROR, __FILE__, __LINE__, 0);
		saImmOiFinalize(gld_cb->immOiHandle);
		glsv_gld_mbcsv_unregister(gld_cb);
		gld_mds_shut(gld_cb);
		saAmfFinalize(gld_cb->amf_hdl);
		m_NCS_TASK_RELEASE(gld_cb->task_hdl);
		m_NCS_IPC_RELEASE(&gld_cb->mbx, NULL);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		return (NCSCC_RC_FAILURE);
	}

	m_NCS_EDU_HDL_INIT(&gld_cb->edu_hdl);

	/* register GLD component with AvSv */
	amf_error = saAmfComponentRegister(gld_cb->amf_hdl, &gld_cb->comp_name, (SaNameT *)NULL);
	if (amf_error != SA_AIS_OK) {
		m_LOG_GLD_SVC_PRVDR(GLD_AMF_REG_ERROR, NCSFL_SEV_ERROR, __FILE__, __LINE__);
		m_NCS_EDU_HDL_FLUSH(&gld_cb->edu_hdl);
		m_NCS_TASK_RELEASE(gld_cb->task_hdl);
		m_NCS_IPC_RELEASE(&gld_cb->mbx, NULL);
		saImmOiFinalize(gld_cb->immOiHandle);
		glsv_gld_mbcsv_unregister(gld_cb);
		gld_mds_shut(gld_cb);
		saAmfFinalize(gld_cb->amf_hdl);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
		return NCSCC_RC_FAILURE;
	} else
		m_LOG_GLD_SVC_PRVDR(GLD_AMF_REG_SUCCESS, NCSFL_SEV_INFO, __FILE__, __LINE__);

   /** start the AMF health check **/
	memset(&Healthy, 0, sizeof(Healthy));
	health_key = (int8 *)getenv("GLSV_ENV_HEALTHCHECK_KEY");
	if (health_key == NULL) {
		if (strlen("A1B2") < sizeof(Healthy.key))
			strncpy((char *)Healthy.key, "A1B2", sizeof(Healthy.key));
		m_LOG_GLD_HEADLINE(GLD_HEALTH_KEY_DEFAULT_SET, NCSFL_SEV_INFO, __FILE__, __LINE__, 0);
	} else {
		if (strlen((char *)health_key) < sizeof(Healthy.key))
			strncpy((char *)Healthy.key, (char *)health_key, SA_AMF_HEALTHCHECK_KEY_MAX - 1);
	}
	Healthy.keyLen = strlen((char *)Healthy.key);

	amf_error = saAmfHealthcheckStart(gld_cb->amf_hdl, &gld_cb->comp_name, &Healthy,
					  SA_AMF_HEALTHCHECK_AMF_INVOKED, SA_AMF_COMPONENT_FAILOVER);
	if (amf_error != SA_AIS_OK) {
		m_LOG_GLD_SVC_PRVDR(GLD_AMF_HLTH_CHK_START_FAIL, NCSFL_SEV_ERROR, __FILE__, __LINE__);
		saAmfComponentUnregister(gld_cb->amf_hdl, &gld_cb->comp_name, (SaNameT *)NULL);
		m_NCS_EDU_HDL_FLUSH(&gld_cb->edu_hdl);
		m_NCS_TASK_RELEASE(gld_cb->task_hdl);
		m_NCS_IPC_RELEASE(&gld_cb->mbx, NULL);
		saImmOiFinalize(gld_cb->immOiHandle);
		glsv_gld_mbcsv_unregister(gld_cb);
		gld_mds_shut(gld_cb);
		saAmfFinalize(gld_cb->amf_hdl);
		m_MMGR_FREE_GLSV_GLD_CB(gld_cb);
	} else
		m_LOG_GLD_SVC_PRVDR(GLD_AMF_HLTH_CHK_START_DONE, NCSFL_SEV_INFO, __FILE__, __LINE__);

	return (res);
}
Exemplo n.º 9
0
/****************************************************************************
 * Name          : pcs_rda_reg_callback
 *
 * Description   :
 *
 *
 * Arguments     : PCS_RDA_CB_PTR - Callback function pointer
 *
 * Return Values :
 *
 * Notes         : None
 *****************************************************************************/
static int pcs_rda_reg_callback(uns32 cb_handle, PCS_RDA_CB_PTR rda_cb_ptr, void **task_cb)
{
    uns32 rc = PCSRDA_RC_SUCCESS;
    int sockfd = -1;
    NCS_BOOL is_task_spawned = FALSE;
    RDA_CALLBACK_CB *rda_callback_cb = NULL;

    if (*task_cb != NULL)
        return PCSRDA_RC_CALLBACK_ALREADY_REGD;

    *task_cb = (long)0;

    /*
     ** Connect
     */
    rc = rda_connect(&sockfd);
    if (rc != PCSRDA_RC_SUCCESS) {
        return rc;
    }

    do {
        /*
         ** Init leap
         */
        if (ncs_leap_startup() != NCSCC_RC_SUCCESS) {

            rc = PCSRDA_RC_LEAP_INIT_FAILED;
            break;
        }

        /*
         ** Send callback reg request messgae
         */
        rc = rda_callback_req(sockfd);
        if (rc != PCSRDA_RC_SUCCESS) {
            break;
        }

        /*
         ** Allocate callback control block
         */
        rda_callback_cb = m_NCS_MEM_ALLOC(sizeof(RDA_CALLBACK_CB), 0, 0, 0);
        if (rda_callback_cb == NULL) {
            rc = PCSRDA_RC_MEM_ALLOC_FAILED;
            break;
        }

        memset(rda_callback_cb, 0, sizeof(RDA_CALLBACK_CB));
        rda_callback_cb->sockfd = sockfd;
        rda_callback_cb->callback_ptr = rda_cb_ptr;
        rda_callback_cb->callback_handle = cb_handle;
        rda_callback_cb->task_terminate = FALSE;

        /*
         ** Spawn task
         */
        if (m_NCS_TASK_CREATE((NCS_OS_CB)rda_callback_task,
                              rda_callback_cb,
                              "RDATASK_CALLBACK",
                              0, NCS_STACKSIZE_HUGE, &rda_callback_cb->task_handle) != NCSCC_RC_SUCCESS) {

            m_NCS_MEM_FREE(rda_callback_cb, 0, 0, 0);
            rc = PCSRDA_RC_TASK_SPAWN_FAILED;
            break;
        }

        if (m_NCS_TASK_START(rda_callback_cb->task_handle) != NCSCC_RC_SUCCESS) {
            m_NCS_MEM_FREE(rda_callback_cb, 0, 0, 0);
            m_NCS_TASK_RELEASE(rda_callback_cb->task_handle);
            rc = PCSRDA_RC_TASK_SPAWN_FAILED;
            break;
        }

        is_task_spawned = TRUE;
        *task_cb = rda_callback_cb;

    } while (0);

    /*
     ** Disconnect
     */
    if (!is_task_spawned) {
        ncs_leap_shutdown();
        rda_disconnect(sockfd);

    }

    /*
     ** Done
     */
    return rc;
}
Exemplo n.º 10
0
/****************************************************************************
 * Name          : dts_lib_init
 *
 * Description   : This is the function which initalize the dts libarary.
 *                 This function creates an IPC mail Box and spawns DTS
 *                 thread.
 *                 This function initializes the CB, handle manager and MDS.
 *
 * Arguments     : req_info - Request info.
 *
 * Return Values : NCSCC_RC_SUCCESS/NCSCC_RC_FAILURE.
 *
 * Notes         : None.
 *****************************************************************************/
uns32 dts_lib_init(NCS_LIB_REQ_INFO *req_info)
{
	NCSCONTEXT task_handle;
	DTS_CB *inst = &dts_cb;
	PCS_RDA_REQ pcs_rda_req;
	uns32 rc = NCSCC_RC_SUCCESS;

	memset(inst, 0, sizeof(DTS_CB));

#if (DTS_SIM_TEST_ENV == 1)
	if ('n' == ncs_util_get_char_option(req_info->info.create.argc, req_info->info.create.argv, "DTS_TEST=")) {
		inst->is_test = FALSE;
	} else
		inst->is_test = TRUE;
#endif

	/* Initialize SAF stuff */
	/* Fill in the Health check key */
	strcpy((char *)inst->health_chk_key.key, DTS_AMF_HEALTH_CHECK_KEY);
	inst->health_chk_key.keyLen = strlen((char *)inst->health_chk_key.key);

	inst->invocationType = DTS_HB_INVOCATION_TYPE;
	/* Recommended recovery is to failover */
	inst->recommendedRecovery = DTS_RECOVERY;

	/* RDA init */
	memset(&pcs_rda_req, '\0', sizeof(pcs_rda_req));
	pcs_rda_req.req_type = PCS_RDA_LIB_INIT;
	rc = pcs_rda_request(&pcs_rda_req);
	if (rc != PCSRDA_RC_SUCCESS) {
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: pcs_rda_request() failed for PCS_RDA_LIB_INIT");
	}

	/* Get initial role from RDA */
	memset(&pcs_rda_req, '\0', sizeof(pcs_rda_req));
	pcs_rda_req.req_type = PCS_RDA_GET_ROLE;
	rc = pcs_rda_request(&pcs_rda_req);
	if (rc != PCSRDA_RC_SUCCESS) {
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to get initial role from RDA");
	}

	/* Set initial role now */
	switch (pcs_rda_req.info.io_role) {
	case PCS_RDA_ACTIVE:
		inst->ha_state = SA_AMF_HA_ACTIVE;
		m_DTS_DBG_SINK(NCSCC_RC_SUCCESS, "DTS init role is ACTIVE");
		m_LOG_DTS_API(DTS_INIT_ROLE_ACTIVE);
		break;
	case PCS_RDA_STANDBY:
		inst->ha_state = SA_AMF_HA_STANDBY;
		m_DTS_DBG_SINK(NCSCC_RC_SUCCESS, "DTS init role is STANDBY");
		m_LOG_DTS_API(DTS_INIT_ROLE_STANDBY);
		break;
	default:
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to get initial role from RDA");
	}

	/* RDA finalize */
	memset(&pcs_rda_req, '\0', sizeof(pcs_rda_req));
	pcs_rda_req.req_type = PCS_RDA_LIB_DESTROY;
	rc = pcs_rda_request(&pcs_rda_req);
	if (rc != PCSRDA_RC_SUCCESS) {
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to perform lib destroy on RDA");
	}

	inst->in_sync = TRUE;

	/* Attempt to open console device for logging */
	inst->cons_fd = -1;
	if ((inst->cons_fd = dts_cons_open(O_RDWR | O_NOCTTY)) < 0) {
		/* Should we return frm here on failure?? I guess NOT */
		m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to open console");
	}

	/* Create DTS mailbox */
	if (m_NCS_IPC_CREATE(&gl_dts_mbx) != NCSCC_RC_SUCCESS) {
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to create DTS mail box");
	}

	if (NCSCC_RC_SUCCESS != m_NCS_IPC_ATTACH(&gl_dts_mbx)) {
		m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: IPC attach failed.");
	}
#if (DTS_FLOW == 1)
	/* Keeping count of messages in DTS mailbox */
	if (NCSCC_RC_SUCCESS != m_NCS_IPC_CONFIG_USR_COUNTERS(&gl_dts_mbx, NCS_IPC_PRIORITY_LOW, &inst->msg_count)) {
		m_NCS_IPC_DETACH(&gl_dts_mbx, dts_clear_mbx, inst);
		m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE,
				      "dts_lib_init: Failed to initialize DTS msg counters with LEAP");
	}
#endif

	/* Smik - initialize the signal handler */
	if ((dts_app_signal_install(SIGUSR1, dts_amf_sigusr1_handler)) == -1) {
		m_NCS_IPC_DETACH(&gl_dts_mbx, dts_clear_mbx, inst);
		m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to install signal handler");
	}

	{
		DTS_LM_ARG arg;
		memset(&arg, 0, sizeof(DTS_LM_ARG));

		arg.i_op = DTS_LM_OP_CREATE;
		arg.info.create.i_hmpool_id = NCS_HM_POOL_ID_COMMON;
		arg.info.create.i_vrid = 1;
		arg.info.create.task_handle = task_handle;

		/* Now create and initialize DTS control block */
		if (dts_lm(&arg) != NCSCC_RC_SUCCESS) {
			m_NCS_IPC_DETACH(&gl_dts_mbx, dts_clear_mbx, inst);
			m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
			return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to create and init DTS CB");
		}
	}

	/* Create DTS's task */
	if (m_NCS_TASK_CREATE((NCS_OS_CB)dts_do_evts,
			      &gl_dts_mbx, NCS_DTS_TASKNAME,
			      NCS_DTS_PRIORITY, NCS_DTS_STACKSIZE, &task_handle) != NCSCC_RC_SUCCESS) {
		m_NCS_IPC_DETACH(&gl_dts_mbx, dts_clear_mbx, inst);
		m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to create DTS thread.");
	}

	/* Start DTS task */
	if (m_NCS_TASK_START(task_handle) != NCSCC_RC_SUCCESS) {
		m_NCS_TASK_RELEASE(task_handle);
		m_NCS_IPC_DETACH(&gl_dts_mbx, dts_clear_mbx, inst);
		m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Failed to start DTS task");
	}

	inst->task_handle = task_handle;

	if (dts_log_bind() != NCSCC_RC_SUCCESS) {
		m_DTS_LK(&inst->lock);
		m_LOG_DTS_LOCK(DTS_LK_LOCKED, &inst->lock);
		/* Do cleanup activities */
		m_NCS_TASK_STOP(task_handle);
		m_NCS_TASK_RELEASE(task_handle);
		m_NCS_IPC_DETACH(&gl_dts_mbx, dts_clear_mbx, inst);
		m_NCS_IPC_RELEASE(&gl_dts_mbx, NULL);
		inst->created = FALSE;
		dtsv_mbcsv_deregister(inst);
		dts_mds_unreg(inst, TRUE);
		dtsv_clear_asciispec_tree(inst);
		dtsv_clear_libname_tree(inst);
		ncs_patricia_tree_destroy(&inst->svcid_asciispec_tree);
		ncs_patricia_tree_destroy(&inst->libname_asciispec_tree);
		ncs_patricia_tree_destroy(&inst->svc_tbl);
		ncs_patricia_tree_destroy(&inst->dta_list);
		m_DTS_UNLK(&inst->lock);
		m_LOG_DTS_LOCK(DTS_LK_UNLOCKED, &inst->lock);
		return m_DTS_DBG_SINK(NCSCC_RC_FAILURE, "dts_lib_init: Unable to bind DTS with DTSv");
	}

	return NCSCC_RC_SUCCESS;
}