File: MOTRSTRG\MSIMSTAT.CPP

    1 #include <fstream.h>
    2 #include "motrstrg\msimstat.h"
    3 
    4 #include "motrstrg\m_layer.h"
    5 
    6 //--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--
    7 
    8 static HINSTANCE theSimulationDLL;
    9 static ofstream* theLogFile;
   10 
   11 TMSimStatusWindow::TMSimStatusWindow(HINSTANCE aInstance) : TMDIWindow(aInstance)
   12 {
   13         strcpy(m_Title, "MSIM-Status");
   14 }
   15 
   16 TMSimStatusWindow::~TMSimStatusWindow()
   17 {}
   18 
   19 void TMSimStatusWindow::OnCreate()
   20 {
   21         // set a proper height and width
   22         SetWindowPos( GetHandle(), (HWND)0, 0, 0, 250, 400, 0 );
   23 }
   24 
   25 LRESULT TMSimStatusWindow::OnCommand(WPARAM wParam, LPARAM lParam )
   26 {
   27         if ( (wParam == 1) && (lParam == 1) )
   28                 InvalidateRect( GetHandle(), 0, TRUE); // gesendet von der MSIM.DLL (CVS Modul MSIM; Methode updateSimulationStatusWindow)
   29         return TMDIWindow::OnCommand( wParam, lParam );
   30 }
   31 
   32 void TMSimStatusWindow::DoPaint( HDC hdc, PAINTSTRUCT*)
   33 {
   34         char* (WINAPI* getsimulationstatus)(void)= (char * (WINAPI*)(void)) GetProcAddress(theSimulationDLL, "GETSIMULATIONSTATUS");
   35         if ( !getsimulationstatus )
   36                 return;
   37 
   38         char *buffer= getsimulationstatus();
   39         if ( !buffer )
   40                 return;
   41 
   42         TEXTMETRIC tm;
   43         GetTextMetrics(hdc, &tm);
   44         UINT y= 0, w= 0;
   45         char *token= strtok( buffer, "\r\n");
   46         while (token)
   47         {
   48                 int x= 0;
   49                 TextOut(hdc, x, y, token, strlen(token));
   50                 w= max(w, strlen(token) * tm.tmMaxCharWidth);
   51                 token= strtok( NULL, "\r\n");
   52                 y += tm.tmHeight;
   53         }
   54 }
   55 
   56 
   57 void InitializeMotorsSimulation(const char* ini_file)
   58 {
   59         char buffer[100];
   60         TSimulationType sim_type;
   61         int logLevel;
   62         //geaendert klier
   63         //------------------------------------------------------
   64         OFSTRUCT of;
   65         char szFileName[_MAX_PATH];
   66         //------------------------------------------------------
   67 
   68         // check whether we need to initialize the simulation
   69 
   70         GetPrivateProfileString("MOTORSIM", "SimulationType", "no_simulation",
   71                                                         buffer, 100, ini_file);
   72         if (stricmp(buffer, "no_simulation") == 0)
   73         {
   74                 sim_type= no_simulation;
   75         }
   76         else if (stricmp(buffer, "test_simulation") == 0)
   77         {
   78                 sim_type= test_simulation;
   79         }
   80         else if (stricmp(buffer, "simulation_only") == 0)
   81         {
   82                 sim_type= simulation_only;
   83         }
   84 
   85         //geaendert klier
   86         //------------------------------------------------------
   87         HINSTANCE hLib= 0;
   88 
   89         // load simulation dll
   90         GetPrivateProfileString("MOTORSIM", "dll", "msim.dll",
   91                                                         buffer, 100, ini_file);
   92 
   93         strcpy(szFileName, GetDirectory());
   94         strcat(szFileName, buffer);
   95 
   96         if (OpenFile(szFileName, &of, OF_EXIST) != HFILE_ERROR)
   97                 hLib= LoadLibrary(buffer);
   98         //------------------------------------------------------
   99 
  100         if (hLib == 0)
  101         {
  102                 // library not found, reset no_simulation
  103                 sim_type= no_simulation;
  104                 return;
  105         }
  106 
  107         msSetSimulationType(sim_type);
  108 
  109         // open logstream if the name is given in ini-file
  110         GetPrivateProfileString("MOTORSIM", "LogFile", "msim.log",
  111                                                         buffer, 100, ini_file);
  112         logLevel= GetPrivateProfileInt("MOTORSIM", "LogLevel", 0, ini_file);
  113         if ( (strlen(buffer) > 0) && (logLevel > 0) )
  114         {
  115                 theLogFile= new ofstream(buffer);
  116 
  117                 void (WINAPI* setlogstream)(ostream*)= (void (WINAPI*)(ostream*))
  118                                                                                                 GetProcAddress(hLib, "SETLOGSTREAM");
  119                 if (setlogstream)
  120                         setlogstream(theLogFile);
  121         }
  122 
  123 
  124         // create the status window if required
  125         if ( sim_type != no_simulation &&
  126                         GetPrivateProfileInt("MOTORSIM", "StatusWindow", 0, ini_file))
  127         {
  128                 TMSimStatusWindow* stat_wnd= new TMSimStatusWindow(GetMainInstance());
  129                 stat_wnd->Show();
  130                 void (WINAPI* wndf)(HWND)= (void (WINAPI*)(HWND))
  131                                                                         GetProcAddress(hLib, "SETSIMULATIONSTATUSWINDOW");
  132                 if (wndf)
  133                         wndf(stat_wnd -> GetHandle()); // Bildschirmaktualisierung direkt an das Simulationsfenster senden
  134         }
  135 
  136 
  137         // register the callbacks provided by msim.dll
  138         if ( logLevel > 0)
  139         {
  140                 switch (sim_type)
  141                 {
  142                         case no_simulation:
  143                                 msRegister_C812ISA_Get( (T812ISA_GET_CALLBACK)
  144                                                                                 GetProcAddress(hLib, "VERBOSE_C812ISA_GET_CALLBACK_NS") );
  145                                 msRegister_C812ISA_Put( (T812ISA_PUT_CALLBACK)
  146                                                                                 GetProcAddress(hLib, "VERBOSE_C812ISA_PUT_CALLBACK") );
  147                                 msRegister_C832_Get( (T832_GET_CALLBACK)
  148                                                                          GetProcAddress(hLib, "VERBOSE_C832_GET_CALLBACK_NS") );
  149                                 msRegister_C832_Put( (T832_PUT_CALLBACK)
  150                                                                          GetProcAddress(hLib, "VERBOSE_C832_PUT_CALLBACK") );
  151                                 break;
  152 
  153                         case test_simulation:
  154                                 msRegister_C812ISA_Get( (T812ISA_GET_CALLBACK)
  155                                                                                 GetProcAddress(hLib, "VERBOSE_C812ISA_GET_CALLBACK_TS") );
  156                                 msRegister_C812ISA_Put( (T812ISA_PUT_CALLBACK)
  157                                                                                 GetProcAddress(hLib, "VERBOSE_C812ISA_PUT_CALLBACK") );
  158                                 msRegister_C832_Get( (T832_GET_CALLBACK)
  159                                                                          GetProcAddress(hLib, "VERBOSE_C832_GET_CALLBACK_TS") );
  160                                 msRegister_C832_Put( (T832_PUT_CALLBACK)
  161                                                                          GetProcAddress(hLib, "VERBOSE_C832_PUT_CALLBACK") );
  162                                 break;
  163 
  164                         case simulation_only:
  165                                 msRegister_C812ISA_Get( (T812ISA_GET_CALLBACK)
  166                                                                                 GetProcAddress(hLib, "VERBOSE_C812ISA_GET_CALLBACK_SO") );
  167                                 msRegister_C812ISA_Put( (T812ISA_PUT_CALLBACK)
  168                                                                                 GetProcAddress(hLib, "VERBOSE_C812ISA_PUT_CALLBACK") );
  169                                 msRegister_C832_Get( (T832_GET_CALLBACK)
  170                                                                          GetProcAddress(hLib, "VERBOSE_C832_GET_CALLBACK_SO") );
  171                                 msRegister_C832_Put( (T832_PUT_CALLBACK)
  172                                                                          GetProcAddress(hLib, "VERBOSE_C832_PUT_CALLBACK") );
  173                                 break;
  174                 }
  175         }
  176         else
  177         {
  178                 // no log requested, so we use silent callbacks, but only if we simulate
  179                 if ( sim_type != no_simulation )
  180                 {
  181                         msRegister_C812ISA_Get( (T812ISA_GET_CALLBACK)
  182                                                                         GetProcAddress(hLib, "SILENT_C812ISA_GET_CALLBACK") );
  183                         msRegister_C812ISA_Put( (T812ISA_PUT_CALLBACK)
  184                                                                         GetProcAddress(hLib, "SILENT_C812ISA_PUT_CALLBACK") );
  185                         msRegister_C832_Get( (T832_GET_CALLBACK)
  186                                                                  GetProcAddress(hLib, "SILENT_C832_GET_CALLBACK") );
  187                         msRegister_C832_Put( (T832_PUT_CALLBACK)
  188                                                                  GetProcAddress(hLib, "SILENT_C832_PUT_CALLBACK") );
  189                 }
  190         }
  191 
  192 
  193         if ( sim_type != no_simulation )
  194         {
  195                 void (WINAPI* initf)(const char*)= (void (WINAPI*)(const char*))
  196                                                                                         GetProcAddress(hLib, "INITCONTROLLERFROMINIFILE");
  197                 if (initf)
  198                         initf(ini_file);
  199         }
  200 
  201         theSimulationDLL= hLib;
  202 }
  203 
  204 
  205 void UnInitializeMotorsSimulation()
  206 {
  207         if (theSimulationDLL)
  208         {
  209                 FreeLibrary(theSimulationDLL);
  210                 theSimulationDLL= 0;
  211         }
  212 
  213         if (theLogFile)
  214         {
  215                 theLogFile->close();
  216                 _FREEOBJ(theLogFile);
  217         }
  218 }
  219