/********************************************************************* * Function: void FTPInit(void) * * PreCondition: TCP module is already initialized. * * Input: None * * Output: None * * Side Effects: None * * Overview: Initializes internal variables of FTP * * Note: ********************************************************************/ void FTPInit(void) { FTPSocket = TCPListen(FTP_COMMAND_PORT); smFTP = SM_FTP_NOT_CONNECTED; FTPStringLen = 0; FTPFlags.Val = 0; FTPDataPort.Val = FTP_DATA_PORT; }
/********************************************************************* * Function: void HTTPInit(void) * * PreCondition: TCP must already be initialized. * * Input: None * * Output: None * * Side Effects: None * * Overview: Sets all HTTP sockets to listening state. * Initialize state machine for each connection. * * Note: This function is called only one during lifetime * of the application. ********************************************************************/ void HTTPInit(void) { WORD oldPtr; // Make sure the file handles are invalidated curHTTP.file = MPFS_INVALID_HANDLE; curHTTP.offsets = MPFS_INVALID_HANDLE; for(curHTTPID = 0; curHTTPID < MAX_HTTP_CONNECTIONS; curHTTPID++) { smHTTP = SM_HTTP_IDLE; sktHTTP = TCPListen(HTTP_PORT); // Save the default record (just invalid file handles) oldPtr = MACSetWritePtr(BASE_HTTPB_ADDR + curHTTPID*sizeof(HTTP_CONN)); MACPutArray((BYTE*)&curHTTP, sizeof(HTTP_CONN)); MACSetWritePtr(oldPtr); } }
NTSTATUS DispTdiListen( PIRP Irp) /* * FUNCTION: TDI_LISTEN handler * ARGUMENTS: * Irp = Pointer to an I/O request packet * RETURNS: * Status of operation */ { PCONNECTION_ENDPOINT Connection; PTDI_REQUEST_KERNEL Parameters; PTRANSPORT_CONTEXT TranContext; PIO_STACK_LOCATION IrpSp; NTSTATUS Status = STATUS_SUCCESS; KIRQL OldIrql; TI_DbgPrint(DEBUG_IRP, ("Called.\n")); IrpSp = IoGetCurrentIrpStackLocation(Irp); /* Get associated connection endpoint file object. Quit if none exists */ TranContext = IrpSp->FileObject->FsContext; if (TranContext == NULL) { TI_DbgPrint(MID_TRACE, ("Bad transport context.\n")); Status = STATUS_INVALID_PARAMETER; goto done; } Connection = (PCONNECTION_ENDPOINT)TranContext->Handle.ConnectionContext; if (Connection == NULL) { TI_DbgPrint(MID_TRACE, ("No connection endpoint file object.\n")); Status = STATUS_INVALID_PARAMETER; goto done; } Parameters = (PTDI_REQUEST_KERNEL)&IrpSp->Parameters; Status = DispPrepareIrpForCancel (TranContext->Handle.ConnectionContext, Irp, (PDRIVER_CANCEL)DispCancelListenRequest); LockObject(Connection, &OldIrql); if (Connection->AddressFile == NULL) { TI_DbgPrint(MID_TRACE, ("No associated address file\n")); UnlockObject(Connection, OldIrql); Status = STATUS_INVALID_PARAMETER; goto done; } LockObjectAtDpcLevel(Connection->AddressFile); /* Listening will require us to create a listening socket and store it in * the address file. It will be signalled, and attempt to complete an irp * when a new connection arrives. */ /* The important thing to note here is that the irp we'll complete belongs * to the socket to be accepted onto, not the listener */ if( NT_SUCCESS(Status) && !Connection->AddressFile->Listener ) { Connection->AddressFile->Listener = TCPAllocateConnectionEndpoint( NULL ); if( !Connection->AddressFile->Listener ) Status = STATUS_NO_MEMORY; if( NT_SUCCESS(Status) ) { ReferenceObject(Connection->AddressFile); Connection->AddressFile->Listener->AddressFile = Connection->AddressFile; Status = TCPSocket( Connection->AddressFile->Listener, Connection->AddressFile->Family, SOCK_STREAM, Connection->AddressFile->Protocol ); } if( NT_SUCCESS(Status) ) { ReferenceObject(Connection->AddressFile->Listener); Status = TCPListen( Connection->AddressFile->Listener, 1024 ); /* BACKLOG */ } } if( NT_SUCCESS(Status) ) { Status = TCPAccept ( (PTDI_REQUEST)Parameters, Connection->AddressFile->Listener, Connection, DispDataRequestComplete, Irp ); } UnlockObjectFromDpcLevel(Connection->AddressFile); UnlockObject(Connection, OldIrql); done: if (Status != STATUS_PENDING) { DispDataRequestComplete(Irp, Status, 0); } else IoMarkIrpPending(Irp); TI_DbgPrint(MID_TRACE,("Leaving %x\n", Status)); return Status; }
/* * Main entry point. */ void main(void) { static TICK8 t = 0; #ifdef HEATHERD NODE_INFO tcpServerNode; static TCP_SOCKET tcpSocketUser = INVALID_SOCKET; BYTE c; #endif static BYTE testLED; testLED = 1; //Set SWDTEN bit, this will enable the watch dog timer WDTCON_SWDTEN = 1; aliveCntrMain = 0xff; //Disable alive counter during initialization. Setting to 0xff disables it. //Initialize any application specific hardware. InitializeBoard(); //Initialize all stack related components. Following steps must //be performed for all applications using PICmicro TCP/IP Stack. TickInit(); //Initialize buses busInit(); //Initialize serial ports early, because they could be required for debugging if (appcfgGetc(APPCFG_USART1_CFG & APPCFG_USART_ENABLE)) { appcfgUSART(); //Configure the USART1 } if (appcfgGetc(APPCFG_USART2_CFG & APPCFG_USART_ENABLE)) { appcfgUSART2(); //Configure the USART2 } //After initializing all modules that use interrupts, enable global interrupts INTCON_GIEH = 1; INTCON_GIEL = 1; //Initialize file system. fsysInit(); //Intialize HTTP Execution unit htpexecInit(); //Initialize Stack and application related NV variables. appcfgInit(); //First call appcfgCpuIOValues() and then only appcfgCpuIO()!!! This ensures the value are set, before enabling ports. appcfgCpuIOValues(); //Configure the CPU's I/O port pin default values appcfgCpuIO(); //Configure the CPU's I/O port pin directions - input or output appcfgADC(); //Configure ADC unit appcfgPWM(); //Configure PWM Channels //Serial configuration menu - display it for configured time and allow user to enter configuration menu scfInit(appcfgGetc(APPCFG_STARTUP_SER_DLY)); //LCD Display Initialize lcdInit(); //Initialize expansion board appcfgXboard(); StackInit(); #if defined(STACK_USE_HTTP_SERVER) HTTPInit(); #endif #if defined(STACK_USE_FTP_SERVER) FTPInit(); #endif //Intialise network componet of buses - only call after StackInit()! busNetInit(); //Initializes events. evtInit(); //Initializes "UDP Command Port" and "UDP Even Port". cmdInit(); ioInit(); #if (DEBUG_MAIN >= LOG_DEBUG) debugPutMsg(1); //@mxd:1:Starting main loop #endif /* * Once all items are initialized, go into infinite loop and let * stack items execute their tasks. * If application needs to perform its own task, it should be * done at the end of while loop. * Note that this is a "co-operative mult-tasking" mechanism * where every task performs its tasks (whether all in one shot * or part of it) and returns so that other tasks can do their * job. * If a task needs very long time to do its job, it must broken * down into smaller pieces so that other tasks can have CPU time. */ #ifdef HEATHERD //Create a TCP socket that listens on port 54123 tcpSocketUser = TCPListen(HEATHERD); #define HEATHERD_ENABLE (!(appcfgGetc(APPCFG_TRISA) & 1)) #define HEATHERD_WRITE_ENABLE (!(appcfgGetc(APPCFG_TRISA) & 2)) #endif while(1) { aliveCntrMain = 38; //Reset if not services in 52.42ms x 38 = 2 seconds //Blink SYSTEM LED every second. if (appcfgGetc(APPCFG_SYSFLAGS) & APPCFG_SYSFLAGS_BLINKB6) { //Configure RB6 as output, and blink it every 500ms if ( TickGetDiff8bit(t) >= ((TICK8)TICKS_PER_SECOND / (TICK8)2) ) { t = TickGet8bit(); //If B6 is configured as input, change to output if (appcfgGetc(APPCFG_TRISB) & 0x40) { appcfgPutc(APPCFG_TRISB, appcfgGetc(APPCFG_TRISB) & 0b10111111); } TRISB_RB6 = 0; LATB6 ^= 1; //Toggle //Toggle IOR5E LED, if IOR5E is present if (appcfgGetc(APPCFG_XBRD_TYPE) == XBRD_TYPE_IOR5E) { ior5eLatchData.bits.ledPWR ^= 1; // Toggle } } } //This task performs normal stack task including checking for incoming packet, //type of packet and calling appropriate stack entity to process it. StackTask(); //Service LCD display lcdService(); //Process commands cmdTask(); //Process events evtTask(); //Process serial busses busTask(); //I2C Task i2cTask(); #ifdef HEATHERD //Has a remote node made connection with the port we are listening on if ((tcpSocketUser != INVALID_SOCKET) && TCPIsConnected(tcpSocketUser)) { if (HEATHERD_ENABLE) { //Is there any data waiting for us on the TCP socket? //Because of the design of the Modtronix TCP/IP stack we have to //consume all data sent to us as soon as we detect it. while(TCPIsGetReady(tcpSocketUser)) { //We are only interrested in the first byte of the message. TCPGet(tcpSocketUser, &c); if (HEATHERD_WRITE_ENABLE) serPutByte(c); } //Discard the socket buffer. TCPDiscard(tcpSocketUser); while (serIsGetReady() && TCPIsPutReady(tcpSocketUser)) { TCPPut(tcpSocketUser,serGetByte()); } TCPFlush(tcpSocketUser); } else { TCPDisconnect(tcpSocketUser); } } #endif #if defined(STACK_USE_HTTP_SERVER) //This is a TCP application. It listens to TCP port 80 //with one or more sockets and responds to remote requests. HTTPServer(); #endif #if defined(STACK_USE_FTP_SERVER) FTPServer(); #endif #if defined(STACK_USE_ANNOUNCE) DiscoveryTask(); #endif #if defined(STACK_USE_NBNS) NBNSTask(); #endif //Add your application speicifc tasks here. ProcessIO(); //For DHCP information, display how many times we have renewed the IP //configuration since last reset. if ( DHCPBindCount != myDHCPBindCount ) { #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(2); //@mxd:2:DHCP Bind Count = %D debugPutByteHex(DHCPBindCount); #endif //Display new IP address #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(3); //@mxd:3:DHCP complete, IP = %D.%D.%D.%D debugPutByteHex(AppConfig.MyIPAddr.v[0]); debugPutByteHex(AppConfig.MyIPAddr.v[1]); debugPutByteHex(AppConfig.MyIPAddr.v[2]); debugPutByteHex(AppConfig.MyIPAddr.v[3]); #endif myDHCPBindCount = DHCPBindCount; #if defined(STACK_USE_ANNOUNCE) AnnounceIP(); #endif } } }