示例#1
0
_ppp_handle PPP_init
   (
        PPP_PARAM_STRUCT*     params
   )
{ /* Body */

#if RTCSCFG_ENABLE_IP4 
    PPP_CFG_PTR          ppp_ptr;
    uint32_t             error;
    IPCP_DATA_STRUCT     ipcp_data;
    int stage = 0;

    /* Allocate the state structure */
    ppp_ptr = _mem_alloc_zero(sizeof(PPP_CFG));
    
    if (!ppp_ptr)
    {
        return(NULL);
    }
    ppp_ptr->DEVICE_NAME = _mem_alloc_zero(strlen(params->device)+1);
    if (ppp_ptr->DEVICE_NAME == NULL)
    {
        _mem_free(ppp_ptr);
        return(NULL);
    }
    strcpy(ppp_ptr->DEVICE_NAME, params->device);
    /* Stage 0 - Open low level device */
    ppp_ptr->IOPCB_DEVICE = fopen(params->device, NULL);
    if (ppp_ptr->IOPCB_DEVICE == NULL)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    stage++;

    /* Stage 1 - Initialize HDLC and lwsem */
    ppp_ptr->DEVICE = _iopcb_ppphdlc_init(ppp_ptr->IOPCB_DEVICE);
    if (ppp_ptr->DEVICE == NULL)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }

    ppp_ptr->RECV_OPTIONS = &PPP_DEFAULT_OPTIONS;
    ppp_ptr->SEND_OPTIONS = &PPP_DEFAULT_OPTIONS;

    if (_lwsem_create(&ppp_ptr->MUTEX, 1))
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    stage++;

    /* Stage 2 - Initialize LCP */
    error = LCP_init(ppp_ptr);
    if (error)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    stage++;

    /* Stage 3 - Initialize and open CCP */
    /*
    error = CCP_init(ppp_ptr);
    if (error)
    {
        PPP_init_fail(ppp_ptr, stage);
        return error;
    }
    CCP_open(ppp_ptr);
    */
    stage++;

    /* Stage 4 - Create a pool of message buffers */
    ppp_ptr->MSG_POOL = RTCS_msgpool_create(sizeof(PPP_MESSAGE), PPP_MESSAGE_INITCOUNT, PPP_MESSAGE_GROWTH, PPP_MESSAGE_LIMIT);
    if (ppp_ptr->MSG_POOL == MSGPOOL_NULL_POOL_ID)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    stage++;

    /* Stage 5 - Create the Tx Task */
    error = RTCS_task_create("PPP tx", _PPPTASK_priority, _PPPTASK_stacksize + 1000, PPP_tx_task, ppp_ptr);
    if (error)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    } 
    stage++;

    /* Stage 6 - Create the Rx Task */
    ppp_ptr->STOP_RX = FALSE; 

    error = RTCS_task_create("PPP rx", _PPPTASK_priority, _PPPTASK_stacksize + 1000, PPP_rx_task, ppp_ptr);
    if (error)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    stage++;

    /* Stage 7 - Open HDLC layer */
    error = _iopcb_open(ppp_ptr->DEVICE, PPP_lowerup, PPP_lowerdown, ppp_ptr);
    if (error != PPPHDLC_OK)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    ppp_ptr->VALID = PPP_VALID;
    stage++;

    /* Stage 8 - Add PPP interface to RTCS */
    error = RTCS_if_add(ppp_ptr, RTCS_IF_PPP, &ppp_ptr->IF_HANDLE);
    if (error != RTCS_OK)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    stage++;

    /* Stage 9 - Bind IP address to PPP interface */
    _mem_zero(&ipcp_data, sizeof(ipcp_data));
    ipcp_data.IP_UP              = params->up;
    ipcp_data.IP_DOWN            = params->down;
    ipcp_data.IP_PARAM           = params->callback_param;

    if(params->listen_flag == 0)
    {
        ipcp_data.ACCEPT_LOCAL_ADDR  = TRUE;
        ipcp_data.ACCEPT_REMOTE_ADDR = TRUE;
    }
    else
    {
        ipcp_data.ACCEPT_LOCAL_ADDR  = FALSE;
        ipcp_data.ACCEPT_REMOTE_ADDR = FALSE;
    }
    ipcp_data.LOCAL_ADDR         = params->local_addr;
    ipcp_data.REMOTE_ADDR        = params->remote_addr;
    ipcp_data.DEFAULT_NETMASK    = TRUE;
    ipcp_data.DEFAULT_ROUTE      = TRUE;

    error = RTCS_if_bind_IPCP(ppp_ptr->IF_HANDLE, &ipcp_data);
    if (error != RTCS_OK)
    {
        PPP_init_fail(ppp_ptr, stage);
        return(NULL);
    }
    params->if_handle = ppp_ptr->IF_HANDLE;
    stage++;

    /* Stage 10 - Init complete, return handle */
    return(ppp_ptr);

#else

    return(NULL);    

#endif /* RTCSCFG_ENABLE_IP4 */

} /* Endbody */
示例#2
0
文件: ppp.c 项目: zhouglu/K60F120M
uint_32 PPP_initialize
   (
      _iopcb_handle       device,
            /* [IN] - I/O stream to use */
      _ppp_handle _PTR_   handle
            /* [OUT] - the PPP state structure */
   )
{ /* Body */

#if RTCSCFG_ENABLE_IP4 

   PPP_CFG_PTR          ppp_ptr;
   uint_32              i, error;

   /* Allocate the state structure */
   ppp_ptr = PPP_memalloc(sizeof(PPP_CFG));
   if (!ppp_ptr) {
      return RTCSERR_PPP_ALLOC_FAILED;
   } /* Endif */

   /* Initialize it */
   PPP_memzero(ppp_ptr, sizeof(PPP_CFG));
   ppp_ptr->LINK_STATE = FALSE;
   ppp_ptr->DEVICE = device;
   for (i = 0; i < PPP_CALL_MAX; i++) {
      ppp_ptr->LCP_CALL[i].CALLBACK = NULL;
      ppp_ptr->LCP_CALL[i].PARAM    = NULL;
   } /* Endfor */
   ppp_ptr->PROT_CALLS = NULL;
   ppp_ptr->RECV_OPTIONS = &PPP_DEFAULT_OPTIONS;
   ppp_ptr->SEND_OPTIONS = &PPP_DEFAULT_OPTIONS;

   /* Initialize the lwsem */
   if (PPP_mutex_init(&ppp_ptr->MUTEX)) {
      return RTCSERR_PPP_INIT_MUTEX_FAILED;
   } /* Endif */

   /* Initialize LCP */
   error = LCP_init(ppp_ptr);
   if (error) {
      PPP_mutex_destroy(&ppp_ptr->MUTEX);
      return error;
   } /* Endif */

   /* Initialize CCP */
   error = CCP_init(ppp_ptr);
   if (error) {
      LCP_destroy(ppp_ptr);
      PPP_mutex_destroy(&ppp_ptr->MUTEX);
      return error;
   } /* Endif */
   CCP_open(ppp_ptr);

   /* Create a pool of message buffers */
   ppp_ptr->MSG_POOL = RTCS_msgpool_create(sizeof(PPP_MESSAGE),
      PPP_MESSAGE_INITCOUNT, PPP_MESSAGE_GROWTH, PPP_MESSAGE_LIMIT);
   if (ppp_ptr->MSG_POOL == MSGPOOL_NULL_POOL_ID) {
      CCP_destroy(ppp_ptr);
      LCP_destroy(ppp_ptr);
      PPP_mutex_destroy(&ppp_ptr->MUTEX);
      return RTCSERR_PPP_CREATE_PKT_POOL_FAILED;
   } /* Endif */

 
   /* Create the Tx Task */
   error = RTCS_task_create("PPP tx", _PPPTASK_priority,
      _PPPTASK_stacksize + 1000,
      PPP_tx_task, ppp_ptr);
   if (error) {
     RTCS_msgpool_destroy(ppp_ptr->MSG_POOL);
      CCP_destroy(ppp_ptr);
      LCP_destroy(ppp_ptr);
     PPP_mutex_destroy(&ppp_ptr->MUTEX);
      return error;
   } /* Endif */

   /* Create the Rx Task        */
   /* Set task ready for run    */
   ppp_ptr->STOP_RX = FALSE; 
   
   error = RTCS_task_create("PPP rx", _PPPTASK_priority,
      _PPPTASK_stacksize + 1000,
      PPP_rx_task, ppp_ptr);
   if (error) {
      RTCS_msgpool_destroy(ppp_ptr->MSG_POOL);
      CCP_destroy(ppp_ptr);
      LCP_destroy(ppp_ptr);
      PPP_mutex_destroy(&ppp_ptr->MUTEX);
      return error;
   } /* Endif */

   /* Return the handle */
   ppp_ptr->VALID = PPP_VALID;
   *handle = ppp_ptr;
   return PPP_OK;

#else

    return RTCSERR_IP_IS_DISABLED;    

#endif /* RTCSCFG_ENABLE_IP4 */

} /* Endbody */