hti/PC_Tools/HTIGateway/HtiGateway/src/main.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *    This module contains the UI operations of DataGateway.
       
    16 */
       
    17 
       
    18 #pragma warning ( disable : 4786 )
       
    19 
       
    20 #include "Socket.h"
       
    21 
       
    22 #include <windows.h>
       
    23 #include <process.h>
       
    24 #include <string>
       
    25 #include <map>
       
    26 
       
    27 #include "util.h"
       
    28 #include "datagateway.h"
       
    29 #include "common.h"
       
    30 
       
    31 // GLOBAL DEFINES
       
    32 // Command line switches
       
    33 #define PARAM_SWITCH_PORT       "-port"
       
    34 #define PARAM_SWITCH_BUFSIZE    "-bufsize"
       
    35 #define PARAM_SWITCH_COMMPLUGIN "-commchannel"
       
    36 #define PARAM_SWITCH_STAYALIVE  "-stayalive"
       
    37 #define PARAM_SWITCH_CCLATEINIT "-cclateinit"
       
    38 #define PARAM_SWITCH_SWT        "-swt"
       
    39 #define PARAM_SWITCH_VERBOSE    "-v"
       
    40 #define PARAM_SWITCH_TIMESTAMP  "-t"
       
    41 
       
    42 #define VERSION "1.68.0"
       
    43 #define INFO "HtiGateway %s - Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.\n\n"
       
    44 
       
    45 // Event object to catch console signal
       
    46 Event g_ConsoleCloseEvent;
       
    47 
       
    48 // Event object to wait datagateway shutdown
       
    49 Event g_DataGatewayClosedEvent;
       
    50 
       
    51 // Maximum time to wait
       
    52 extern long g_MaximumShutdownWaitTime;
       
    53 
       
    54 extern DWORD g_ErrorCode;
       
    55 
       
    56 // Global parameters to be used in datagateway and all plugins
       
    57 map<string, string> g_parameters;
       
    58 
       
    59 // FUNCTION PROTOTYPES
       
    60 // Prints usage of DataGateway
       
    61 void usage();
       
    62 
       
    63 // Initializes phoenixtool default values
       
    64 void init(map<string, string>&, map<string, string>&);
       
    65 
       
    66 // Catches console control signals
       
    67 BOOL WINAPI ConsoleControlHandler(DWORD);
       
    68 
       
    69 int main(int argv, char *args[])
       
    70 {
       
    71     printf(INFO, VERSION);
       
    72     if (argv == 2 && strcmp("-h", args[1]) == 0)
       
    73     {
       
    74         usage();
       
    75         return 0;
       
    76     }
       
    77     map<string, string> props;
       
    78 
       
    79     try {
       
    80         Util::ReadProperties("HtiGateway.ini", props);
       
    81 
       
    82         // Sets values from properties file
       
    83         init(props, g_parameters);
       
    84 
       
    85         if (argv > 1)
       
    86         {
       
    87             // Overrides properties file values
       
    88             // if specified in command line
       
    89             Util::ParseCmdLine(argv, args, g_parameters);
       
    90             Util::SetTimestamp(g_parameters[PARAM_SWITCH_TIMESTAMP]);
       
    91         }
       
    92         Util::SetVerboseLevel(g_parameters[PARAM_SWITCH_VERBOSE]);
       
    93 
       
    94         bool stayalive = (strcmp("true", g_parameters[PARAM_SWITCH_STAYALIVE].c_str()) == 0
       
    95                             ? true
       
    96                             : false);
       
    97         bool cclateinit = (strcmp("true", g_parameters[PARAM_SWITCH_CCLATEINIT].c_str()) == 0
       
    98                             ? true
       
    99                             : false);
       
   100         int port = atoi(g_parameters[PARAM_SWITCH_PORT].c_str());
       
   101         long bufsize = atol(g_parameters[PARAM_SWITCH_BUFSIZE].c_str());
       
   102         g_MaximumShutdownWaitTime = atol(g_parameters[PARAM_SWITCH_SWT].c_str());
       
   103         string ch = Util::ToUpper(g_parameters[PARAM_SWITCH_COMMPLUGIN]);
       
   104 
       
   105         DataGateway dg(port, bufsize, ch, stayalive, cclateinit);
       
   106 
       
   107         // Installs console control handler for catching
       
   108         // signals
       
   109         if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleControlHandler, TRUE))
       
   110         {
       
   111             Util::Error("Error registering the console control handler");
       
   112             return ERR_DG_CONSOLEHANDLER;
       
   113         }
       
   114 
       
   115         dg.Start();
       
   116         HANDLE handles[2];
       
   117         handles[0] = dg.ThreadHandle();
       
   118         handles[1] = g_ConsoleCloseEvent.EventHandle();
       
   119         DWORD dwResult = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
       
   120         switch (dwResult)
       
   121         {
       
   122             case WAIT_OBJECT_0 + 0:
       
   123             {
       
   124                 Util::Debug("main() HtiGateway thread stopped");
       
   125             }
       
   126             break;
       
   127             case WAIT_OBJECT_0 + 1:
       
   128             {
       
   129                 dg.Stop();
       
   130                 if (g_MaximumShutdownWaitTime > 0)
       
   131                 {
       
   132                     Util::Info("[HtiGateway] Waiting HtiGateway to shutdown");
       
   133                     WaitForSingleObject(dg.ThreadHandle(), g_MaximumShutdownWaitTime);
       
   134                 }
       
   135             }
       
   136             break;
       
   137         }
       
   138         g_DataGatewayClosedEvent.Set();
       
   139     } catch (UtilError ue) {
       
   140         Util::Error(ue.iError, ue.iResult);
       
   141         g_ErrorCode = ue.iResult;
       
   142     }
       
   143     if (g_ErrorCode != NO_ERRORS)
       
   144     {
       
   145         // Resolve error code to clear text
       
   146         ERROR_LOOKUP(g_ErrorCode);
       
   147     }
       
   148 
       
   149     return g_ErrorCode;
       
   150 }
       
   151 
       
   152 void init(map<string, string>& props,
       
   153           map<string, string>& params)
       
   154 {
       
   155     // Setting default values from properties file
       
   156     // to parameters
       
   157     params[PARAM_SWITCH_BUFSIZE]    = props["TCPIP_RECV_BUFFER_SIZE"];
       
   158     params[PARAM_SWITCH_PORT]       = props["TCPIP_PORT"];
       
   159     params[PARAM_SWITCH_COMMPLUGIN] = props["DEFAULT_COMM_CHANNEL_PLUGIN"];
       
   160     params[PARAM_SWITCH_SWT]        = props["MAXIMUM_SHUTDOWN_WAITTIME"];
       
   161     params[PARAM_SWITCH_VERBOSE]    = "info";
       
   162     params[PARAM_SWITCH_STAYALIVE]  = "true";
       
   163     params[PARAM_SWITCH_TIMESTAMP]  = "false";
       
   164     params[PARAM_SWITCH_CCLATEINIT] = "false";
       
   165 }
       
   166 
       
   167 void usage()
       
   168 {
       
   169     
       
   170 	cout << "USAGE: HtiGateway.exe [SWITCHES in form -switch=value]\n\n";
       
   171     cout << "\tSWITCHES:\n";
       
   172     cout << "\t-port         TCP/IP port\n";
       
   173     cout << "\t-bufsize      TCP/IP receive buffer size in bytes\n";
       
   174     cout << "\t-commchannel  Communication Channel Plugin (CCP) name e.g. SERIAL\n";
       
   175     cout << "\t-stayalive    Whether HtiGateway remains when client disconnects\n";
       
   176     cout << "\t              or not\n";
       
   177     cout << "\t-cclateinit   Whether CCP is initialized when client connects or not\n";
       
   178     cout << "\t-swt          Maximum shutdown wait time in milliseconds. 0 means\n";
       
   179     cout << "\t              no waiting.\n";
       
   180     cout << "\t-v            Verbose mode\n";
       
   181     cout << "\t-t            Timestamped output\n\n";
       
   182     cout << "\tBy default these values are defined in HtiGateway.ini file.\n";
       
   183     cout << "\tIf any switches are used those override values from ini file\n\n";
       
   184     cout << "\tSTAYALIVE:\n";
       
   185     cout << "\tfalse      HtiGateway shutdown after client closes connection\n";
       
   186     cout << "\ttrue       HtiGateway remains waiting new connections (*)\n\n";
       
   187     cout << "\tCCLATEINIT:\n";
       
   188     cout << "\tfalse      HtiGateway initializes CCP when started (*)\n";
       
   189     cout << "\ttrue       HtiGateway initializes CCP only when client connects to\n\n";
       
   190     cout << "\tVERBOSE:\n";
       
   191     cout << "\toff        Silent mode\n";
       
   192     cout << "\tresult     Prints only result (no info/errors)\n";
       
   193     cout << "\terror      Prints result and errors (*)\n";
       
   194     cout << "\tinfo       Prints normal output\n";
       
   195     cout << "\tdebug      Prints all\n\n";
       
   196     cout << "\tTIMESTAMPED:\n";
       
   197     cout << "\tfalse      No timestamp (*)\n";
       
   198     cout << "\ttrue       Adds timestamp to output\n\n";
       
   199     cout << "\t(*) means default value\n\n";
       
   200     cout << "\tAll communication channel plugin parameters can also be passed from\n";
       
   201     cout << "\tcommand line just by adding \"-\" to the parameter name. For example:\n";
       
   202 	cout << "\t\"-COMPORT=COM3\" or -REMOTE_PORT=3000\n";
       
   203     cout << endl;
       
   204 }
       
   205 
       
   206 BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
       
   207 {
       
   208     BOOL bReturnStatus = FALSE;
       
   209     switch (dwCtrlType)
       
   210     {
       
   211         case CTRL_C_EVENT:
       
   212         case CTRL_BREAK_EVENT:
       
   213         case CTRL_CLOSE_EVENT:
       
   214         case CTRL_LOGOFF_EVENT:
       
   215         case CTRL_SHUTDOWN_EVENT:
       
   216         {
       
   217             Util::Info("[HtiGateway] Request to terminate");
       
   218             g_ConsoleCloseEvent.Set();
       
   219 
       
   220             // Wait DG shutdown before returning control to system
       
   221             WaitForSingleObject(g_DataGatewayClosedEvent.EventHandle(), g_MaximumShutdownWaitTime);
       
   222         }
       
   223         break;
       
   224     }
       
   225     bReturnStatus = TRUE;
       
   226     return bReturnStatus;
       
   227 }
       
   228 
       
   229 // End of the file