/* * vbios.c * * Created on: 2019-8-13 * Author: Administrator */ #include #include #include #include "vbios.h" struct vbios { int net_enable; struct vbios_net_param vnp; struct vbios_term_param vtp; struct vbios_param vp; vsem_t sem; }; static struct vbios _vs; static HANDLE hCfg; static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" }; static char *ReportStr[] = { "","Running","Updated","Complete","Fault" }; static char *StatusStr[] = { "Disabled","Waiting","IPTerm","Failed","Enabled" }; static void NetworkOpen(void ) { /* Create our local servers */ TaskCreate( echosrv, "echosrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 ); vsem_signal(_vs.sem); } static void NetworkClose(void ) { } static void NetworkIPAddr( IPN IPAddr, uint32_t IfIdx, uint32_t fAdd ) { IPN IPTmp; if( fAdd ) printf("Network Added: "); else printf("Network Removed: "); /* Print a message */ IPTmp = ntohl( IPAddr ); printf("If-%d:%d.%d.%d.%d \r\n", IfIdx, (uint8_t)(IPTmp>>24)&0xFF, (uint8_t)(IPTmp>>16)&0xFF, (uint8_t)(IPTmp>>8)&0xFF, (uint8_t)IPTmp&0xFF ); return; } static void service_report( uint32_t item, uint32_t status, uint32_t report, HANDLE h ) { printf( "Service Status: %-9s: %-9s: %-9s \r\n", TaskName[item-1], StatusStr[status],ReportStr[report/256]); } static void __net_ndk_start(void ) { int32_t rc; struct vbios_net_param *vnp = &_vs.vnp; /* Start the NDK stack - The stack will start the Ethernet driver */ rc = NC_SystemOpen( NC_PRIORITY_HIGH, NC_OPMODE_INTERRUPT ); if( rc ) { printf("NC_SystemOpen Failed (%d). Will die in an infinite loop so you need to reset...\n",rc); for(;;); } /* * Create and build the system configuration from scratch. */ /* Create a new configuration */ if (!(hCfg = CfgNew())) { printf("Unable to create a configuration for the IP stack.\n"); goto _exit; } do { CI_IPNET NA; /* Setup an IP address to this EVM */ bzero( &NA, sizeof(NA) ); NA.IPAddr = inet_addr(vnp->ip); NA.IPMask = inet_addr(vnp->mask); strcpy( NA.Domain, vnp->domain); /* Add the address to interface 1 */ CfgAddEntry( hCfg, CFGTAG_IPNET, 1, 0, sizeof(CI_IPNET), (uint8_t *)&NA, 0 ); }while(0); if (_vs.vnp.telnet_enable) { CI_SERVICE_TELNET telnet; /*telnet init same param*/ vtelnetd_init(); /* Specify TELNET service */ bzero( &telnet, sizeof(telnet) ); telnet.cisargs.IPAddr = INADDR_ANY; telnet.cisargs.pCbSrv = &service_report; telnet.param.MaxCon = TELNET_CLINET_MAX_COUNT; telnet.param.Callback = &vtelnetd_client; CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_TELNET, 0, sizeof(telnet), (uint8_t *)&telnet, &(telnet.cisargs.hService) ); } /* ** Configure IPStack/OS Options */ /* Set debug message level */ rc = DBG_WARN; CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL, CFG_ADDMODE_UNIQUE, sizeof(uint), (uint8_t *)&rc, 0 ); /* ** This code sets up the TCP and UDP buffer sizes ** (Note 8192 is actually the default. This code is here to ** illustrate how the buffer and limit sizes are configured.) */ /* TCP Transmit buffer size */ //rc = 8192; rc = 64000; CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKTCPTXBUF, CFG_ADDMODE_UNIQUE, sizeof(uint), (uint8_t *)&rc, 0 ); /* TCP Receive buffer size (copy mode) */ //rc = 8192; rc = 64000; CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKTCPRXBUF, CFG_ADDMODE_UNIQUE, sizeof(uint), (uint8_t *)&rc, 0 ); /* TCP Receive limit (non-copy mode) */ //rc = 8192; rc = 64000; CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKTCPRXLIMIT, CFG_ADDMODE_UNIQUE, sizeof(uint), (uint8_t *)&rc, 0 ); /* UDP Receive limit */ rc = 8192; CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKUDPRXLIMIT, CFG_ADDMODE_UNIQUE, sizeof(uint), (uint8_t *)&rc, 0 ); /* ** Boot the system using this configuration ** ** We keep booting until the function returns 0. This allows ** us to have a "reboot" command. */ do { rc = NC_NetStart( hCfg, NetworkOpen, NetworkClose, NetworkIPAddr ); } while( rc > 0 ); printf("Done with this utility. Shutting things down\n"); /* Delete Configuration */ CfgFree( hCfg ); /* Close the OS */ _exit: printf("Exiting the system\n"); NC_SystemClose(); } static void __main_task(void ) { if (_vs.net_enable) { vsem_wait(_vs.sem, 0); fdOpenSession(TaskSelf()); } if (_vs.vp.usrAppFunc) _vs.vp.usrAppFunc(); if (_vs.net_enable) fdCloseSession(TaskSelf()); } static int __vbios_puts(void *param, const char *s, int len) { if (_vs.vtp.puts) return _vs.vtp.puts(s, len); return 0; } static int __vbios_gets(void *param, char *buf, int len, int blocking /*in millis*/) { if (_vs.vtp.gets) return _vs.vtp.gets(buf, len, blocking); return 0; } static vconsole_t _vb_tty = NULL; int vbios_init(struct vbios_param *vp) { memset(&_vs, 0, sizeof _vs); memcpy(&_vs.vp, vp, sizeof *vp); if (vp->tp) { memcpy(&_vs.vtp, vp->tp, sizeof *vp->tp); struct vconsole_param vp; vp.debug_console = 1; vp.prompt = "vs> "; vp.param = NULL; vp.puts = __vbios_puts; vp.gets = __vbios_gets; _vb_tty = vconsole_new(&vp); } if (vp->usrDrvInitFunc) vp->usrDrvInitFunc(); if (vp->usrAppPrevFunc) vp->usrAppPrevFunc(); if (vp->np) { _vs.net_enable = 1; memcpy(&_vs.vnp, vp->np, sizeof *vp->np); _vs.sem = vsem_new(); vtask_create(__net_ndk_start); } return 0; } void vbios_start(void ) { vtask_create(__main_task); BIOS_start(); } void vbios_loop(void ) { return vconsole_loop(_vb_tty); }