void setup_ethernet(void) { int error; _enet_handle ehandle; /* for Ethernet driver */ _rtcs_if_handle ihandle; _enet_address address; error = RTCS_create(); if (error) { err_sys("failed to create RTCS"); } ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, address); /* Set up the Ethernet driver */ error = ENET_initialize(BSP_DEFAULT_ENET_DEVICE, address, 0, &ehandle); if (error) err_sys("failed to initialize Ethernet driver"); error = RTCS_if_add(ehandle, RTCS_IF_ENET, &ihandle); if (error) err_sys("failed to add interface for Ethernet"); error = RTCS_if_bind(ihandle, ENET_IPADDR, ENET_IPMASK); if (error) err_sys("failed to bind interface for Ethernet"); #ifdef GATE_IPADDR RTCS_gate_add(GATE_IPADDR, INADDR_ANY, INADDR_ANY); #endif printf("Ethernet device %d bound to %X\n", BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR); }
/*FUNCTION*------------------------------------------------ * * Function Name: PPP_start * Comments : * * *END*-----------------------------------------------------*/ void PPP_start(void) { uint_32 error; _rtcs_if_handle ihandle; MQX_FILE_PTR pfile; _iopcb_handle pio; _ppp_handle phandle; IPCP_DATA_STRUCT ipcp_data; _PPP_PAP_RSECRETS = rsecrets; /* Require authentication, allow PAP */ _PPP_PAP_LSECRET = &lsecret; /* Allow peer to request PAP */ _PPP_CHAP_LNAME = localname; /* Needed by CHAP */ _PPP_CHAP_RSECRETS = rsecrets; /* Require authentication, allow CHAP */ _PPP_CHAP_LSECRETS = lsecrets; /* Allow peer to request CHAP */ /* Install a route for a default gateway */ RTCS_gate_add(GATE_ADDR, INADDR_ANY, INADDR_ANY); pfile = fopen(PPP_DEVICE, NULL); #ifdef PPP_DEVICE_DUN _io_dun_install("dun:"); pfile = fopen("dun:", (char_ptr)pfile); #endif pio = _iopcb_ppphdlc_init(pfile); _PPP_ACCM = 0; error = PPP_initialize(pio, &phandle); if (error) { printf("\nPPP initialize: %lx", error); _task_block(); } /* Endif */ _iopcb_open(pio, PPP_lowerup, PPP_lowerdown, phandle); error = RTCS_if_add(phandle, RTCS_IF_PPP, &ihandle); if (error) { printf("\nIF add failed, error = %lx", error); _task_block(); } /* Endif */ _lwsem_create(&ppp_sem, 0); _mem_zero(&ipcp_data, sizeof(ipcp_data)); ipcp_data.IP_UP = PPP_linkup; ipcp_data.IP_DOWN = NULL; ipcp_data.IP_PARAM = &ppp_sem; ipcp_data.ACCEPT_LOCAL_ADDR = FALSE; ipcp_data.ACCEPT_REMOTE_ADDR = FALSE; ipcp_data.LOCAL_ADDR = PPP_LOCADDR; ipcp_data.REMOTE_ADDR = PPP_PEERADDR; ipcp_data.DEFAULT_NETMASK = TRUE; ipcp_data.DEFAULT_ROUTE = TRUE; error = RTCS_if_bind_IPCP(ihandle, &ipcp_data); if (error) { printf("\nIF bind failed, error = %lx", error); _task_block(); } /* Endif */ printf("\nPlease initiate PPP connection. Waiting..."); _lwsem_wait(&ppp_sem); printf("\nPPP device %s bound to %d.%d.%d.%d", PPP_DEVICE, IPBYTES(ipcp_data.LOCAL_ADDR)); }
_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 */
/*FUNCTION*------------------------------------------------ * * Function Name: shell_ppp_start() * Comments : * This function start PPP communication and try to establish PPP connection. *END*-----------------------------------------------------*/ void shell_ppp_start(SHELL_PPP_LINK_PTR ppp_current) { uint_32 error; _ip_address local_address; _ip_address peer_address=0; IPCP_DATA_STRUCT ipcp_data; /* This is configuration for PAP. */ char* localname = ppp_current->LOCAL_NAME; char* localsecret = ppp_current->LOCAL_PASSWORD; PPP_SECRET lsecret; if(default_ppp.SERVER_FLAG==0) { /* Setup PAP for client mode.*/ lsecret.PPP_ID_LENGTH = strlen(localname); lsecret.PPP_PW_LENGTH = strlen(localsecret); lsecret.PPP_ID_PTR = localname; lsecret.PPP_PW_PTR = localsecret; _PPP_PAP_LSECRET = &lsecret; /* lsecrets */ _PPP_PAP_RSECRETS = NULL; /* rsecrets */ _PPP_CHAP_LNAME = NULL; /* localname */ _PPP_CHAP_LSECRETS = NULL; /* lsecrets */ _PPP_CHAP_RSECRETS = NULL; /* rsecrets */ } else if(default_ppp.SERVER_FLAG==1) { /* Setup PAP for server mode.*/ _PPP_PAP_LSECRET = NULL; /* lsecrets */ _PPP_PAP_RSECRETS = ppp_current->rsecrets; /* rsecrets */ _PPP_CHAP_LNAME = NULL; /* localname */ _PPP_CHAP_LSECRETS = NULL; /* lsecrets */ _PPP_CHAP_RSECRETS = NULL; /* rsecrets */ } else { ppp_current->PPP_HANDLE = NULL; return; } /* Install a route for a default gateway */ RTCS_gate_add(ppp_current->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); ppp_current->PPP_IO_DRIVER_HANDLE = _iopcb_ppphdlc_init(ppp_current->PPP_DEV_HANDLE); _PPP_ACCM = 0; error = PPP_initialize(ppp_current->PPP_IO_DRIVER_HANDLE, &ppp_current->PPP_HANDLE); if (error) { printf("\n PPP initialize: %lx", error); _task_block(); } _iopcb_open(ppp_current->PPP_IO_DRIVER_HANDLE, PPP_lowerup, PPP_lowerdown,ppp_current->PPP_HANDLE); error = RTCS_if_add(ppp_current->PPP_HANDLE, RTCS_IF_PPP, &ppp_current->PPP_IF_HANDLE); if (error) { printf("\n IF add failed, error = %lx", error); _task_block(); } _lwsem_create(&ppp_current->PPP_SEM, 0); _mem_zero(&ipcp_data, sizeof(ipcp_data)); ipcp_data.IP_UP = PPP_linkup; ipcp_data.IP_DOWN = PPP_linkdown; ipcp_data.IP_PARAM = &ppp_current->PPP_SEM; if(default_ppp.SERVER_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 = ppp_current->PPP_LOCAL_ADDRESS; ipcp_data.REMOTE_ADDR = ppp_current->PPP_REMOTE_ADDRESS; ipcp_data.DEFAULT_NETMASK = TRUE; ipcp_data.DEFAULT_ROUTE = TRUE; error = RTCS_if_bind_IPCP(ppp_current->PPP_IF_HANDLE, &ipcp_data); if (error) { printf("\n IF bind failed, error = %lx", error); _task_block(); } /*In case "client" we need wait for connection to server*/ if(default_ppp.SERVER_FLAG==0) { printf("\n Please wait initiate PPP connection. Waiting...\n"); /*Handshake with RAS server */ /* 10 attempts to connect */ error = 0; while (error < 10) { if (PPP_link == FALSE) { _time_delay(1000); } else { printf("\n PPP_link = %d, time = %d\n",PPP_link,error); break; } error ++; } if (PPP_link == TRUE) { local_address = IPCP_get_local_addr(ppp_current->PPP_IF_HANDLE); peer_address = IPCP_get_peer_addr(ppp_current->PPP_IF_HANDLE); printf("\n PPP device on %s is bound on.\n", ppp_current->PPP_CONNECTION_DEVICE ); printf(" PPP local address is : %d.%d.%d.%d\n", IPBYTES(local_address)); printf(" PPP remote address is : %d.%d.%d.%d\n", IPBYTES(peer_address)); printf(" Now PPP connection is established on %d.%d.%d.%d\n", IPBYTES(peer_address)); } else { if(ppp_current->PPP_HANDLE) { /* clean up all PPP structure */ error = PPP_shutdown(ppp_current->PPP_HANDLE); } ppp_current->PPP_HANDLE = NULL; /* We shoul remove route to clear part block. */ RTCS_gate_remove(ppp_current->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); } } else { /* In case of "server" we do not need wait at all. */ printf("\n PPP server ready and waiting for connection.\n"); } }
void SEC_InitializeNetworking(uint_32 pcbs, uint_32 msgs, uint_32 sockets, boolean dhcp) { int_32 error; IPCFG_IP_ADDRESS_DATA ip_data; _enet_address enet_address; #if PSP_MQX_CPU_IS_MCF51CN _enet_handle ehandle; _rtcs_if_handle ihandle; #endif /* runtime RTCS configuration */ _RTCSPCB_init = pcbs; _RTCS_msgpool_init = msgs; _RTCS_socket_part_init = sockets; error = RTCS_create(); if (error == RTCS_OK) { ip_data.ip = ENET_IPADDR; ip_data.mask = ENET_IPMASK; ip_data.gateway = ENET_IPGATEWAY; #if RTCSCFG_ENABLE_LWDNS LWDNS_server_ipaddr = ENET_IPDNS; #endif ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, enet_address); #if PSP_MQX_CPU_IS_MCF51CN ENET_initialize_ex(&ENET_param, BSP_DEFAULT_ENET_DEVICE, &ehandle); if (RTCS_if_add(ehandle, RTCS_IF_ENET, &ihandle) == 0) { if (ipcfg_init_interface(-1, ihandle) != 0) { RTCS_if_remove(ihandle); ENET_shutdown(ehandle); _task_block(); } } else { ENET_shutdown(ehandle); } #else ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_address); #endif ipcfg_add_dns_ip(BSP_DEFAULT_ENET_DEVICE,LWDNS_server_ipaddr); // check link status printf("\nWaiting for ethernet cable plug in ... "); while(!ipcfg_get_link_active(BSP_DEFAULT_ENET_DEVICE)) {}; printf("Cable connected\n"); /* If DHCP Enabled, get IP address from DHCP server */ if (dhcp) { printf("\nDHCP bind ... "); error = ipcfg_bind_dhcp_wait(BSP_DEFAULT_ENET_DEVICE, 1, &ip_data); if (error != IPCFG_ERROR_OK) { printf("Error %08x!\n", error); } else { printf("Successful!\n"); } } else { /* Else bind with static IP */ printf ("\nStatic IP bind ... "); error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data); if (error != IPCFG_ERROR_OK) { printf("Error %08x!\n",error); } else { printf("Successful!\n"); } } if (error == IPCFG_ERROR_OK) { ipcfg_get_ip(BSP_DEFAULT_ENET_DEVICE, &ip_data); printf("\nIP Address : %d.%d.%d.%d\n",IPBYTES(ip_data.ip)); printf("\nSubnet Address : %d.%d.%d.%d\n",IPBYTES(ip_data.mask)); printf("\nGateway Address : %d.%d.%d.%d\n",IPBYTES(ip_data.gateway)); printf("\nDNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE,0))); } } else { printf("\nRTCS_Create failed !\n"); _task_block(); } SEC_GetTime(); }