appl.c

Go to the documentation of this file.
00001 // ////////////////////////////////////////////////////////////////////////////
00002 // ////////////////////////////////////////////////////////////////////////////
00014     #define     APPL_C
00015 
00016 
00017 // ////////////////////////////////////////////////////////////////////////////
00018 // Includes
00019 // ////////////////////////////////////////////////////////////////////////////
00020 
00021     #include    "appl.h"
00022     #include    "mdns.h"
00023     #include    "TCPIP.h"
00024     #include    "stringtable.h"
00025 
00026     #include    "projectdefs.h"
00027 
00028 
00029 // ////////////////////////////////////////////////////////////////////////////
00030 // Local Defines
00031 // ////////////////////////////////////////////////////////////////////////////
00032 
00033 
00034 // ////////////////////////////////////////////////////////////////////////////
00035 // Local Typedefs
00036 // ////////////////////////////////////////////////////////////////////////////
00037 
00038 // States of application
00039 typedef enum
00040 {
00041     APPL_SM_START = 0,
00042     APPL_SM_ADD_ERR_ACK,
00043     APPL_SM_PROCESS_ADD,
00044     APPL_SM_PREPARE_SRCP,
00045     APPL_SM_SET_SRCP,
00046     APPL_SM_PROCESS_SRCP,
00047     APPL_SM_WAIT_FOR_MDNS_RESOLVE,
00048     APPL_SM_RESOLVE_MAC,
00049     APPL_SM_WAIT_FOR_SERVER_REPLY,
00050     APPL_SM_IDLE_CONNECT,
00051     APPL_SM_HANDLE_CONNECTION,
00052     APPL_SM_WAIT_FOR_USER,
00053     APPL_SM_CONNECTED,
00054     APPL_SM_CONNECTION_CLOSED,
00055     APPL_SM_START_SHUTDOWN,
00056     APPL_SM_SHUTDOWN,
00057     APPL_SM_SERVERSET,
00058     APPL_SM_ERROR_CONNECT
00059 } SM_APPL;
00060 
00061 typedef struct __attribute__((__packed__)) _servernet_config 
00062 {
00063     NODE_INFO   sSrvAdd;
00064     WORD_VAL    sPort;
00065 } SSERVERNET;
00066 
00067 
00068 // ////////////////////////////////////////////////////////////////////////////
00069 // Local variables
00070 // ////////////////////////////////////////////////////////////////////////////
00071 
00072 static SM_APPL _eApplSM;
00073 static BYTE _u8retry_counter;
00074 static SSERVERNET _sServerConfig;
00075 
00076 
00077 // ////////////////////////////////////////////////////////////////////////////
00078 // Local function prototypes
00079 // ////////////////////////////////////////////////////////////////////////////
00080 
00081 static BOOL disconnect(void);
00082 
00083 
00084 // ////////////////////////////////////////////////////////////////////////////
00085 // ////////////////////////////////////////////////////////////////////////////
00086 
00087 void ApplInit(void)
00088 {
00089     _eApplSM = APPL_SM_START;
00090     u8ApplTimer = 0;
00091 
00092     BootupInit();
00093 
00094     // Network-Application data from external EEPROM
00095     {
00096         BYTE * confPtr = (BYTE *)(&sPApplConfig);
00097         XEEReadArray(APPL_EEPROM_ADD, confPtr, sizeof(SPAPPLDATA));
00098     }
00099 
00100     // ////////////////////////////////////////////////////////////////////////
00101     // Init the 10 ms timer:
00102 
00103     // Switch timer3 off
00104     T3CON = 0;  
00105 
00106     // Init the periode register for a 10 ms duration...
00107     CCPR2H = (GetPeripheralClock() * 0.005) / 256;
00108     CCPR2L = (GetPeripheralClock() * 0.005);
00109     CCP2CON = 0x0B;     // Compare mode, generate software interrupt on match.
00110     IPR2bits.CCP2IP = 0;    // Low interrupt priority.
00111     PIR2bits.CCP2IF = 0;    // Interrupt flag zuruecksetzen.
00112     PIE2bits.CCP2IE = 0;    // Disable the interrupt flag.
00113 
00114     TMR3L = 0;  // Reset the
00115     TMR3H = 0;  // timer.
00116 
00117     // Timer1 as clock source for CCP1, Timer3 as clock source for CCP2
00118     T3CONbits.T3CCP1 = 1;
00119 
00120     // Set prescaler to 1:2
00121     T3CONbits.T3CKPS0 = 1;
00122 
00123     // Enable timer
00124     T3CONbits.TMR3ON = 1;
00125 
00126 
00127     PROJECT_INITIAL_INIT();
00128 }
00129 
00130 // ////////////////////////////////////////////////////////////////////////////
00131 // ////////////////////////////////////////////////////////////////////////////
00132 
00133 IP_ADDR * ApplGetServerIP(void)
00134 {
00135     return &_sServerConfig.sSrvAdd.IPAddr;
00136 }
00137 
00138 // ////////////////////////////////////////////////////////////////////////////
00139 // ////////////////////////////////////////////////////////////////////////////
00140 
00141 NODE_INFO * ApplGetServerNodeInfo(void)
00142 {
00143     return &_sServerConfig.sSrvAdd;
00144 }
00145 
00146 // ////////////////////////////////////////////////////////////////////////////
00147 // ////////////////////////////////////////////////////////////////////////////
00148 
00149 WORD_VAL ApplGetServerPort(void)
00150 {
00151     return _sServerConfig.sPort;
00152 }
00153 
00154 // ////////////////////////////////////////////////////////////////////////////
00155 // ////////////////////////////////////////////////////////////////////////////
00156 
00157 BOOL ApplIsConnected(void)
00158 {
00159     return PROJECT_IS_CONNECTED();
00160 }
00161 
00162 // ////////////////////////////////////////////////////////////////////////////
00163 // ////////////////////////////////////////////////////////////////////////////
00164 
00165 static BOOL disconnect(void)
00166 {
00167     PROJECT_START_DISCONNECT();
00168 }
00169 
00170 // ////////////////////////////////////////////////////////////////////////////
00171 // ////////////////////////////////////////////////////////////////////////////
00172 
00173 void ApplStop(void)
00174 {
00175     InputResetPowerButton();
00176 
00177     while (1)
00178     {
00179         if (PIR2bits.CCP2IF == 1)
00180         {   
00181             // Reset the timer flag.
00182             PIR2bits.CCP2IF = 0;
00183         
00184             // call the input routines every 10 ms!
00185             InputMain();
00186             if (bInputButtonPushed(PWR_BTN) == TRUE)
00187             {
00188                 break;
00189             }
00190         }
00191     }
00192 }
00193 
00194 // ////////////////////////////////////////////////////////////////////////////
00195 // ////////////////////////////////////////////////////////////////////////////
00196 
00197 void ApplMain(void)
00198 {
00199     // Execute the application all 10 ms!
00200     if (PIR2bits.CCP2IF == 0)
00201     {
00202         return;
00203     }
00204 
00205     // Reset the timer flag.
00206     PIR2bits.CCP2IF = 0;
00207 
00208     InputMain();
00209 
00210 
00211     if (((u16GlobalFlags & SHUTDOWN_MASK) == 0) &&
00212          (bInputButtonPushedLong(PWR_BTN) == TRUE))
00213     {
00214         // Starting shut down sequence...
00215         ApplShutDown();
00216     }
00217 
00218 
00219     // ////////////////////////////////////////////////////////////////////////
00220     // Application Task starts here!!!!
00221 
00222     if (u8ApplTimer > 0)
00223     {
00224         u8ApplTimer--;
00225     }
00226 
00227     if (bBootupMain() == FALSE)
00228     {
00229         if (u16GlobalFlags & SHUTDOWN_MASK)
00230         {
00231             _eApplSM = APPL_SM_SHUTDOWN;
00232         }
00233         else
00234         {
00235             _eApplSM = APPL_SM_START;
00236         }
00237         return;
00238     }
00239 
00240     if ((_eApplSM>APPL_SM_START)&&(_eApplSM<APPL_SM_WAIT_FOR_USER))
00241     {
00242         if (bInputButtonPushed(ENC_BTN) == TRUE)
00243         {
00244             _eApplSM = APPL_SM_WAIT_FOR_USER;
00245         }
00246 
00247         switch(u8ApplTimer)
00248         {
00249             case 200:
00250                 OutSetLED(LED_F1, LED_ON);
00251                 OutSetLED(LED_F4, LED_OFF);
00252                 break;
00253 
00254             case 150:
00255                 OutSetLED(LED_F2, LED_ON);
00256                 OutSetLED(LED_F1, LED_OFF);
00257                 break;
00258 
00259             case 100:
00260                 OutSetLED(LED_F3, LED_ON);
00261                 OutSetLED(LED_F2, LED_OFF);
00262                 break;
00263 
00264             case 50:
00265                 OutSetLED(LED_F4, LED_ON);
00266                 OutSetLED(LED_F3, LED_OFF);
00267                 break;
00268 
00269             case 0:
00270                 u8ApplTimer = 201;
00271                 break;
00272         }
00273 
00274         if(OutIsLCDBusy()==TRUE)
00275         {
00276             return;
00277         }
00278     }
00279 
00280 
00281     switch(_eApplSM)
00282     {
00283     case APPL_SM_START:
00284         if (sPApplConfig.eFindMode == SEARCH_UNKNOWN)
00285         {
00286             // The first time or after a factory reset:
00287             IPSetInit(TRUE);
00288             _eApplSM = APPL_SM_SERVERSET;
00289             break;
00290         }
00291 
00292         StrTbl_GetString(lcdram_row1, STRTBLID_SEARCHING);
00293         if (sPApplConfig.eFindMode==SEARCH_IP)
00294         {
00295             sprintf(lcdram_row2, (ROM char *) "%hhu.%hhu.%hhu.%hhu", 
00296                     sPApplConfig.sSrvIPAdd.v[0], sPApplConfig.sSrvIPAdd.v[1], 
00297                     sPApplConfig.sSrvIPAdd.v[2], sPApplConfig.sSrvIPAdd.v[3]);
00298         }
00299         else if (sPApplConfig.eFindMode==SEARCH_NAME)
00300         {
00301             strcpy(lcdram_row2, sPApplConfig.au8Name);
00302         }
00303         else if (sPApplConfig.eFindMode==SEARCH_SERVICE)
00304         {
00305             strcpy(lcdram_row2, sPApplConfig.au8Service);
00306         }
00307 
00308         lcd_addr2 = (16 - strlen(lcdram_row2))>>1;
00309         lcd_status = LCD_CLR_MASK | LCD_ROW1_MASK |
00310                      LCD_CENTER1_MASK | LCD_ROW2_MASK;
00311 
00312         // Prepare encoder button for abortion of server search:
00313         InputButtonReset(ENC_BTN);
00314 
00315         // Prepare the server configuration:
00316         memset(&_sServerConfig, 0, sizeof(SSERVERNET));
00317         
00318         // Init ARP (ARP flush):
00319         ARPFlush();
00320         
00321         u8ApplTimer = 0;
00322 
00323         if (sPApplConfig.eFindMode == SEARCH_IP)
00324         {   
00325             _sServerConfig.sSrvAdd.IPAddr.Val = sPApplConfig.sSrvIPAdd.Val;
00326             _sServerConfig.sPort.Val = sPApplConfig.sIPPort.Val;
00327              _eApplSM = APPL_SM_RESOLVE_MAC;
00328             break;
00329         }
00330 
00331         if (sPApplConfig.eFindMode == SEARCH_SERVICE)
00332         {
00333             MDNSResolveService(sPApplConfig.au8Service,(ROM char *) PROJECT_SERVICE_NAME);
00334         }
00335         else if (sPApplConfig.eFindMode == SEARCH_NAME)
00336         {
00337             MDNSResolveName(sPApplConfig.au8Name);
00338         }
00339 
00340         _eApplSM = APPL_SM_WAIT_FOR_MDNS_RESOLVE;
00341         break;
00342 
00343     case APPL_SM_WAIT_FOR_MDNS_RESOLVE:
00344         if (bMDNSNameIsResolved(&_sServerConfig.sSrvAdd.IPAddr) == TRUE)
00345         {
00346             // Successfully resolved the name of the server.
00347             if (sPApplConfig.eFindMode == SEARCH_SERVICE)
00348             {
00349                 // A service query also resolves the port of the server!
00350                 bMDNSServiceIsResolved(&_sServerConfig.sSrvAdd.IPAddr, 
00351                                             &_sServerConfig.sPort);
00352             }
00353             else
00354             {
00355                 _sServerConfig.sPort.Val = sPApplConfig.sNamePort.Val;
00356             }
00357             
00358             _eApplSM = APPL_SM_RESOLVE_MAC;
00359         }
00360         break;
00361 
00362     case APPL_SM_RESOLVE_MAC:
00363         if (MACIsTxReady())
00364         {
00365             // Send ARP request
00366             ARPResolve(&_sServerConfig.sSrvAdd.IPAddr);
00367             _eApplSM = APPL_SM_WAIT_FOR_SERVER_REPLY;
00368         }
00369         break;
00370 
00371     case APPL_SM_WAIT_FOR_SERVER_REPLY:
00372         if (u8ApplTimer == 1)
00373         {
00374             _eApplSM = APPL_SM_RESOLVE_MAC;
00375         }
00376         else if (ARPIsResolved(&_sServerConfig.sSrvAdd.IPAddr, 
00377                                &_sServerConfig.sSrvAdd.MACAddr))
00378         {
00379             _eApplSM = APPL_SM_IDLE_CONNECT;
00380             StrTbl_GetString(lcdram_row1, STRTBLID_CONNECT_TO);
00381             lcd_status |= LCD_ROW1_MASK | LCD_CENTER1_MASK;
00382         }
00383         break;
00384 
00385     case APPL_SM_SERVERSET:
00386         // Call periodically the IP set menu (Server) and wait for 
00387         // finish.
00388         if (IPSetMain() == TRUE)
00389         {
00390             _eApplSM = APPL_SM_START;
00391         }
00392         break;
00393 
00394     case APPL_SM_IDLE_CONNECT:
00395         if (u8ApplTimer == 1)
00396         {
00397             PROJECT_PREPARE_CONNECTION();
00398 
00399             InputButtonReset(ALL_BTN);
00400             _eApplSM = APPL_SM_HANDLE_CONNECTION;
00401         }
00402         break;
00403 
00404     case APPL_SM_HANDLE_CONNECTION:
00405     {
00406         APPL_CONNECTING_RETCODE ret;
00407 
00408         ret = PROJECT_HANDLE_SERVER_CONNECTION();
00409 
00410         if (ret == APPL_CR_ERROR)
00411         {
00412             // Errors occured during connect resp. disconnect.
00413             _eApplSM = APPL_SM_ERROR_CONNECT;
00414         }
00415         else if (ret == APPL_CR_CLOSED)
00416         {
00417             // Closing of the connection was handled by the application
00418             // module.
00419             _eApplSM = APPL_SM_CONNECTION_CLOSED;
00420         }
00421         else if (ret == APPL_CR_CONNECTED)
00422         {
00423             // Successfully connected to server! Show user:
00424             StrTbl_GetString(lcdram_row1, STRTBLID_SUCCESSFULLY_CONNECTED);
00425             sprintf(lcdram_row2, (ROM char *) "%hhu.%hhu.%hhu.%hhu", 
00426                     _sServerConfig.sSrvAdd.IPAddr.v[0], 
00427                     _sServerConfig.sSrvAdd.IPAddr.v[1], 
00428                     _sServerConfig.sSrvAdd.IPAddr.v[2], 
00429                     _sServerConfig.sSrvAdd.IPAddr.v[3]);
00430 
00431             lcd_status = LCD_CLR_MASK | LCD_ROW1_MASK | LCD_CENTER1_MASK |
00432                          LCD_ROW2_MASK | LCD_CENTER2_MASK;
00433 
00434             _eApplSM = APPL_SM_CONNECTED;
00435         }
00436         break;
00437     }
00438 
00439     case APPL_SM_CONNECTED:
00440         if (PROJECT_MAIN() == FALSE)
00441         {
00442             _eApplSM = APPL_SM_HANDLE_CONNECTION;
00443         }
00444         break;
00445 
00446     case APPL_SM_CONNECTION_CLOSED:
00447         // Connection was closed. Show user:
00448         OutSwitchOffAllLEDs();
00449         StrTbl_GetString(lcdram_row1, STRTBLID_CONNECTION_CLOSED1);
00450         StrTbl_GetString(lcdram_row2, STRTBLID_CONNECTION_CLOSED2);
00451         lcd_status = LCD_CLR_MASK | LCD_ROW1_MASK | LCD_CENTER1_MASK |
00452                      LCD_ROW2_MASK | LCD_CENTER2_MASK;
00453 
00454         InputButtonReset(ALL_BTN);
00455 
00456         if (u16GlobalFlags & SHUTDOWN_MASK)
00457         {
00458             _eApplSM = APPL_SM_START_SHUTDOWN;
00459             u8ApplTimer = 100;
00460             break;
00461         }
00462     
00463         // Wait for acknowledging user.
00464         _eApplSM = APPL_SM_WAIT_FOR_USER;
00465         
00466         // Intented fall through.
00467 
00468     case APPL_SM_WAIT_FOR_USER:
00469         OutSwitchOffAllLEDs();
00470 
00471         if (ApplIsConnected())
00472         {
00473             InputButtonReset(ALL_BTN);
00474             disconnect();   
00475             _eApplSM = APPL_SM_HANDLE_CONNECTION;
00476             break;
00477         }   
00478 
00479         if (bInputButtonPushedOnce(ENC_BTN) == TRUE)
00480         {
00481             // New server settings:
00482             IPSetInit(TRUE);
00483             _eApplSM = APPL_SM_SERVERSET;
00484         }
00485         else if(bInputButtonPushedTwice(ENC_BTN)||bInputButtonPushedLong(ENC_BTN))
00486         {
00487             // Setting the whole IP data!
00488             IPSetInit(FALSE);
00489             BootupSetIP();
00490             _eApplSM = APPL_SM_START;
00491         }
00492 
00493         break;
00494 
00495     case APPL_SM_START_SHUTDOWN:
00496         if ((u8ApplTimer == 0) && (HTTPCheckForIdle() == TRUE))
00497         {
00498             // Stop the MDNS service...
00499             StopMDNSResponder();
00500             _eApplSM = APPL_SM_SHUTDOWN;
00501         }
00502         break;
00503 
00504     case APPL_SM_ERROR_CONNECT:
00505         StrTbl_GetString(lcdram_row1, STRTBLID_CONNECTION_ERROR1);
00506         StrTbl_GetString(lcdram_row2, STRTBLID_CONNECTION_ERROR2);
00507         lcd_status = LCD_CLR_MASK | LCD_ROW1_MASK | LCD_CENTER1_MASK |
00508                      LCD_ROW2_MASK | LCD_CENTER2_MASK;
00509         _eApplSM = APPL_SM_WAIT_FOR_USER;
00510         InputButtonReset(ALL_BTN);
00511         break;
00512     }
00513 }
00514 
00515 // ////////////////////////////////////////////////////////////////////////////
00516 // ////////////////////////////////////////////////////////////////////////////
00517 
00518 void ApplShutDown(void)
00519 {
00520     if (ApplIsConnected())
00521     {
00522         // Before shuting down, all connection will be closed!
00523         disconnect();
00524         _eApplSM = APPL_SM_HANDLE_CONNECTION;
00525     }
00526     else
00527     {
00528         _eApplSM = APPL_SM_START_SHUTDOWN;
00529         u8ApplTimer = 100;
00530     }
00531 
00532     // Remember the shutdown request.
00533     u16GlobalFlags |= SHUTDOWN_MASK;
00534 }
00535 
00536 // ////////////////////////////////////////////////////////////////////////////
00537 // ////////////////////////////////////////////////////////////////////////////
00538 
00539 BOOL ApplIsRunning(void)
00540 {
00541     if (_eApplSM == APPL_SM_SHUTDOWN)
00542     {
00543         return FALSE;
00544     }
00545     else
00546     {
00547         return TRUE;
00548     }
00549 }
00550 
00551 // ////////////////////////////////////////////////////////////////////////////
00552 // ////////////////////////////////////////////////////////////////////////////
00553 
00554 void ApplResetToFactory(BOOL KeepBootup)
00555 {
00556     BYTE * confPtr = (BYTE *)(&sPApplConfig);
00557 
00558     // Erase the EEPROM before rewriting the standard settings:
00559     XEEEraseEEPROM();
00560 
00561 
00562     // Restore BOOTUP!
00563     if (KeepBootup == TRUE)
00564     {
00565         BootupRestoreState();
00566     }
00567     else
00568     {
00569         BootupResetToFactory();
00570     }
00571 
00572     // Restoring application specific network data:
00573     sPApplConfig.sSrvIPAdd.v[0] = 192;
00574     sPApplConfig.sSrvIPAdd.v[1] = 168;
00575     sPApplConfig.sSrvIPAdd.v[2] = 1;
00576     sPApplConfig.sSrvIPAdd.v[3] = 1;
00577     sPApplConfig.sIPPort.Val = PROJECT_DEFAULT_PORT;
00578     sPApplConfig.sNamePort.Val = sPApplConfig.sIPPort.Val;
00579     sPApplConfig.eFindMode = SEARCH_UNKNOWN;
00580     memset(sPApplConfig.au8Name, 0, sizeof(sPApplConfig.au8Name));
00581     memset(sPApplConfig.au8Service, 0, sizeof(sPApplConfig.au8Service));
00582     sPApplConfig.sFlags.bToggleLocoDirMode = TOGGLEDIRMODE_CLICK;
00583 
00584     XEEWriteArray(APPL_EEPROM_ADD, confPtr, sizeof(SPAPPLDATA));
00585 
00586 
00587     // Restore application related:
00588     PROJECT_RESET_TO_FACTORY();
00589 }
00590 
00591 // ////////////////////////////////////////////////////////////////////////////
00592 // ////////////////////////////////////////////////////////////////////////////
00593 
00594 void ApplChangeLocoDirMode(BOOL Mode)
00595 {
00596     if (Mode != sPApplConfig.sFlags.bToggleLocoDirMode)
00597     {
00598         sPApplConfig.sFlags.bToggleLocoDirMode = Mode;
00599         XEEWriteArray(APPL_EEPROM_ADD,(BYTE *)(&sPApplConfig), sizeof(SPAPPLDATA));
00600 
00601         PROJECT_LOCO_DIR_MODE_HAS_CHANGED();
00602     }
00603 }
00604 
00605 // ////////////////////////////////////////////////////////////////////////////
00606 // ////////////////////////////////////////////////////////////////////////////
00607 
00608 void ApplRestart(BOOL ResetToFactory)
00609 {
00610     if ((u16GlobalFlags & SHUTDOWN_MASK) == 0)
00611     {
00612         if (ResetToFactory == TRUE)
00613         {
00614             // Reset to factory settings requested
00615             u16GlobalFlags |= FACRES_MASK;
00616         }
00617         else
00618         {
00619             // Restart requested
00620             u16GlobalFlags |= RESTART_MASK;
00621         }
00622 
00623         StopMDNSResponder();
00624         ApplShutDown();
00625     }
00626 }
00627 
00628 // ////////////////////////////////////////////////////////////////////////////
00629 // ////////////////////////////////////////////////////////////////////////////

Generated on Sun Nov 27 20:02:37 2011 for eWicht by  doxygen 1.5.5