#define THIS_IS_STACK_APPLICATION #include "stacktsk.h" #include "dhcp.h" #include "tick.h" #include "delay.h" #include "udp.h" #include "arp.h" #include "arptsk.h" #include "tcp.h" #include bit bIsDhcpUp = 0; char netlevel = 0; // All TFTP command statuts display will be done on first line of LCD. //#define TFTP_COMMAND_DISPLAY_LINE 0 // Result will be displayed at y = 15. #define TFTP_COMMAND_RESULT_POSITION 15 /*#define STARTUP_MSG "G1_Build_0x00" ROM char StartupMsg[] = STARTUP_MSG; #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING) ROM char DHCPMsg[] = "DHCP/Gleaning..."; #endif ROM char SetupMsg[] = "Board Setup..."; // 1234567890123456 //ROM char blankLCDLine[] = " "; */ /* * This is used by other stack elements. * Main application must define this and initialize it with * proper values. */ APP_CONFIG AppConfig; BYTE myDHCPBindCount = 0; #if defined(STACK_USE_DHCP) extern BYTE DHCPBindCount; #else /* * If DHCP is not enabled, force DHCP update. */ BYTE DHCPBindCount = 1; #endif /* * Set configuration fuses for HITECH compiler. * For MCC18 compiler, separately linked config.asm file * will set the correct fuses. */ #if defined(HITECH_C18) __CONFIG(1, UNPROTECT & HS); __CONFIG(2, PWRTEN & BORDIS & WDTDIS); #endif /* * Private helper functions. * These may or may not be present in all applications. */ static void InitAppConfig(void); static void InitializeBoard(void); static void SetConfig(void); /***************************** Enummerations ****************************/ typedef enum _NetworkState { off, DhcpIsBound, ArpIsTxReady, ArpIsResolved, SockOpening, SockOpen, TcpReadyToSend, TcpSend, HelloDone, StregkodeSendDone, ReadyToRecieve, StregkodeRecieveDone, TotalRequested, TotalRequestedDone, ChangeRequested, ChangeRequestedDone, Acknowledged, CancelDone, SockClosing, SockClosed } NetworkState; /***************************** GLOBAL INIT ****************************/ NODE_INFO rnode; NetworkState state; TCP_SOCKET tsock1; /***************************** network funktions ****************************/ unsigned long global_network_amount; unsigned char global_network_char[16]; unsigned char global_network_charlen; void network_init(void) { state = off; } void network_worker(void) { TickGet(); StackTask(); } void network_parse_amount(unsigned char buf[]) { global_network_amount = buf[0]<<24 + buf[1]<<16 + buf[2]<<8 + buf[3]; } void network_read_amount(void) { unsigned char buf[4]; TCPGetArray(tsock1, buf,4); network_parse_amount(buf); } void network_wait(unsigned char n) { int i; for (i=0; i>24); TCPPut(tsock1, amount>>16); TCPPut(tsock1, amount>>8); TCPPut(tsock1, amount); TCPFlush(tsock1); state = ChangeRequested; break; } network_worker(); } while (state != ChangeRequestedDone) { if (TCPIsGetReady(tsock1)) { TCPGet(tsock1, &ack); if (ack == 0) { TCPFlush(tsock1); state = TcpReadyToSend; return 4; } network_read_amount(); TCPFlush(tsock1); state = ChangeRequestedDone; } network_worker(); } state = TcpReadyToSend; return 0; } //request 4+5 char network_send_cancel_generic(char id) { while(state != CancelDone) { switch (state) { case TcpReadyToSend: if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) ) state = TcpSend; break; case TcpSend: TCPPut(tsock1, id); TCPFlush(tsock1); state = CancelDone; } network_worker(); } return network_read_ack(); } //Request ID 4 char network_send_cancel_last(void) { return network_send_cancel_generic(0x04); } //Request ID 5 char network_send_cancel_all(void) { return network_send_cancel_generic(0x05); } //Request ID 6 char network_send_goodbye(void) { char ack; while( state != SockClosed) { switch (state) { case TcpReadyToSend: if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) ) state = TcpSend; break; case TcpSend: TCPPut( tsock1, 0x06 ); TCPFlush( tsock1 ); state = SockClosing; break; case SockClosing: ack = network_read_ack(); TCPDisconnect( tsock1 ); state = SockClosed; break; } network_worker(); } if( TCPIsConnected(tsock1)) return 3; // Didn't close the socket properly; else return ack; } void network_wait_for_dhcp() { while (!DHCPIsBound()) network_worker(); } void main(void) { // Tick to blink SYSTEM led. static TICK t = 0; TCP_SOCKET usock1; char sock_state = 0; char is_resolved = 0; char has_arp = 0; TICK tick; TICK lastTick; TICK diffTicks; /* * 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 Stack and application related NV variables. */ InitAppConfig(); StackInit(); network_init(); /* * 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. */ // Defines the IP add. on the server machine rnode.IPAddr.v[0] = 192; rnode.IPAddr.v[1] = 168; rnode.IPAddr.v[2] = 1; rnode.IPAddr.v[3] = 20; while(1) { unsigned char antal = 8; unsigned char stregkode[] = "12345678"; /* * Blink SYSTEM LED every second. */ if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 ) { t = TickGet(); LATA4 ^= 1; } /* * This task performs normal stack task including checking * for incoming packet, type of packet and calling * appropriate stack entity to process it. */ StackTask(); /* * For DHCP information, display how many times we have renewed the IP * configuration since last reset. */ network_send_hello( 0x01 ); strncpy(global_network_char, "12345678", 8); network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 strncpy(global_network_char, "87654444", 8); network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 network_send_calc_total(); network_send_cash_payed(50000); network_send_cancel_last(); network_send_cancel_all(); network_send_goodbye(); } } void interrupt HighISR(void) { TickUpdate(); } #if defined(MCHP_C18) #pragma code highVector=0x08 void HighVector (void) { _asm goto HighISR _endasm } #pragma code /* return to default code section */ #endif /********************************************************************* * Function: void InitializeBoard(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: Initialize board specific hardware. * * Note: None ********************************************************************/ static void InitializeBoard(void) { /* * Setup for PORTA.RA0 as analog input while rests * as digital i/o lines. */ ADCON1 = 0b10001110; // RA0 as analog input, Right justified TRISA = 0x03; /* * LCD is enabled using RA5. */ // PORTA_RA5 = 0; // Disable LCD. /* * Turn off the LED's. */ LATA2 = 1; LATA3 = 1; /* * External data EEPROM needs pull-ups, so enable internal * pull-ups. */ INTCON2_RBPU = 0; T0CON = 0; INTCON_GIEH = 1; INTCON_GIEL = 1; } /********************************************************************* * Function: void InitAppConfig(void) * * PreCondition: MPFSInit() is already called. * * Input: None * * Output: Write/Read non-volatile config variables. * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ static void InitAppConfig(void) { /* * Load default configuration into RAM. */ AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1; AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2; AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3; AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4; AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1; AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2; AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3; AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4; AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1; AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2; AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3; AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4; AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1; AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2; AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3; AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4; AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5; AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6; #if defined(STACK_USE_DHCP) AppConfig.Flags.bIsDHCPEnabled = TRUE; #else AppConfig.Flags.bIsDHCPEnabled = FALSE; #endif } BOOL StringToIPAddress(char *str, IP_ADDR *buffer) { BYTE v; char *temp; BYTE byteIndex; temp = str; byteIndex = 0; while( v = *str ) { if ( v == '.' ) { *str++ = '\0'; buffer->v[byteIndex++] = atoi(temp); temp = str; } else if ( v < '0' || v > '9' ) return FALSE; str++; } buffer->v[byteIndex] = atoi(temp); return (byteIndex == 3); }