hti/PC_Tools/HTIGateway/HtiGateway/inc/SerialCommPlugin.h
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 file contains headers of SerialComm communication channel plugin objects. 
       
    16 *   These classes are SerialComm, SerialCommPlugin and SerialCommIoThread classes.
       
    17 */
       
    18 
       
    19 #ifndef SERIALCOMMPLUGIN_H
       
    20 #define SERIALCOMMPLUGIN_H
       
    21 
       
    22 #pragma warning ( disable : 4786 )
       
    23 #include <iostream>
       
    24 #include <process.h>
       
    25 #include <string>
       
    26 #include <map>
       
    27 #include <comdef.h>
       
    28 
       
    29 #include "common.h"
       
    30 #include "plugin.h"
       
    31 #include "safequeue.h"
       
    32 #include "thread.h"
       
    33 
       
    34 using namespace std;
       
    35 
       
    36 #define SERIAL_INI_FILE_NAME             "serialplugin.ini"
       
    37 
       
    38 #define SERIAL_INI_INIT_PARAM            "INIT_STRING"
       
    39 #define PARAM_SWITCH_INIT_PARAM          "-INIT_STRING"
       
    40 #define SERIAL_INI_COMPORT_PARAM         "COMPORT"
       
    41 #define PARAM_SWITCH_COMPORT_PARAM       "-COMPORT"
       
    42 #define SERIAL_INI_COMPORT_TIMEOUT       "TIMEOUT"
       
    43 #define PARAM_SWITCH_COMPORT_TIMEOUT     "-TIMEOUT"
       
    44 #define SERIAL_INI_USE_HW_FLOW_CONTROL   "HW_FLOWCONTROL"
       
    45 #define PARAM_SWITCH_USE_HW_FLOW_CONTROL "-HW_FLOWCONTROL"
       
    46 #define SERIAL_INI_SEND_SLEEP_PAUSE      "SEND_PAUSE"
       
    47 #define PARAM_SWITCH_SEND_SLEEP_PAUSE    "-SEND_PAUSE"
       
    48 #define SERIAL_INI_SEND_SIZE             "SEND_SIZE"
       
    49 #define PARAM_SWITCH_SEND_SIZE           "-SEND_SIZE"
       
    50 
       
    51 #define SERIAL_DEFAULT_INIT_STRING       "115200,n,8,1"
       
    52 #define SERIAL_DEFAULT_COMPORT           "COM2"
       
    53 
       
    54 const DWORD g_SerialCommDefaultTimeout    = 40000; //ms
       
    55 const DWORD g_SerialPluginTimeout         = 10000; //not used
       
    56 const long  g_SerialCommDefaultSendPause  = 0;
       
    57 
       
    58 // Maximum time to wait
       
    59 extern long g_MaximumShutdownWaitTime;
       
    60 
       
    61 //**********************************************************************************
       
    62 // Class SerialComm
       
    63 //
       
    64 // This class implements the actual serial communication
       
    65 //**********************************************************************************
       
    66 
       
    67 class SerialComm
       
    68 {
       
    69 public:
       
    70 	SerialComm();
       
    71 	~SerialComm();
       
    72 	/*
       
    73 	 * This method opens a communication device that uses serial communication and
       
    74 	 * configures it according initialization parameters
       
    75 	 */	
       
    76 	WORD Open( const string& commPort, const string& initParameters, long timeout,
       
    77 				bool hwFlowControl, long sendPause, long sendMaxSize);
       
    78 	DWORD Close();
       
    79 	/*
       
    80 	 * This method is used to send Data object using serial communication
       
    81 	 */		
       
    82 	DWORD Send(const Data& data_in);
       
    83 	/*
       
    84 	 * This method reads all available data from communication media 
       
    85 	 * and returns it in the Data object given as parameter
       
    86 	 */	
       
    87 	DWORD Receive(Data* data_out);
       
    88 	/*
       
    89 	 * This method returns number of bytes available in incoming queue or
       
    90 	 * -1 if communication error has occurred
       
    91 	 */	
       
    92 	LONG GetRxBytesAvailable();
       
    93 private:
       
    94 	//handle to communication resource
       
    95 	HANDLE m_Com;
       
    96 	bool m_Open;
       
    97 	long m_SendPause;
       
    98 	long m_SendMaxSize;
       
    99 };
       
   100 
       
   101 //**********************************************************************************
       
   102 // Class SerialCommIoThread
       
   103 //
       
   104 // This thread is used to send data from outgoing queue to serial port
       
   105 // and read bytes from serial port and push it to incoming queue 
       
   106 //**********************************************************************************
       
   107 
       
   108 class SerialCommIoThread : public Thread<SerialCommIoThread>
       
   109 {
       
   110 public:
       
   111 	SerialCommIoThread(SafeQueue<Data*>* in, SafeQueue<Data*>* out,
       
   112 						const string& init, const string& commPort, long timeout,
       
   113 						bool hwFlowControl, long sendPause, long sendMaxSize);
       
   114 	/*
       
   115 	 * Main execution loop which is used to send Data from outgoing queue to serial port
       
   116 	 * and read bytes from serial port, encapsulate it to Data objects and push them to incoming queue 
       
   117 	 */		
       
   118 	void Run();
       
   119 	void Stop();
       
   120 	void SetTimeout(long timeout);
       
   121 	long GetTimeout() const;
       
   122 	Event GetOpenedEvent();
       
   123 private:
       
   124 	//incoming queue
       
   125 	SafeQueue<Data*>* m_InQueue;
       
   126 	//outgoing queue
       
   127 	SafeQueue<Data*>* m_OutQueue;
       
   128 	Event m_OpenedEvent;
       
   129 	bool m_Running;
       
   130 	long m_PropertySerialCommTimeout;
       
   131 	const string& m_InitString;
       
   132 	const string& m_CommPort;
       
   133 	long m_ProperySerialSendPause;
       
   134 	bool m_PropertySerialHwFlowControl;
       
   135 	long m_PropertySerialMaxSendSize;
       
   136 };
       
   137 
       
   138 //**********************************************************************************
       
   139 // Class SerialCommPlugin
       
   140 //
       
   141 // This class implements a CommChannelPlugin used in serial communication with device
       
   142 //**********************************************************************************
       
   143 
       
   144 class SerialCommPlugin : public CommChannelPlugin
       
   145 {
       
   146 public:
       
   147 	/*
       
   148 	 * This method initializes SerialCommPlugin and starts SerialCommPluginIoThread
       
   149 	 */
       
   150 	DWORD Init();
       
   151 	/*
       
   152 	 * This method is used to:
       
   153 	 * -push given data to outgoing queue
       
   154 	 * -wait for data to become available in incoming queue
       
   155 	 * -pop the first Data object from queue and store it to the Data object given as parameter
       
   156 	 */		
       
   157 	DWORD SendReceive(Data* data_in, Data** data_out, long timeout = g_SerialPluginTimeout);
       
   158 	/*
       
   159 	 * This method pushes the given Data object(of type Data::EData) to outgoing queue
       
   160 	 */		
       
   161 	DWORD Send(Data* data_in, long timeout = g_SerialPluginTimeout);
       
   162 	/*
       
   163 	 * This method is used to pop the first data object from incoming queue and store it to data object given as parameter
       
   164 	 */		
       
   165 	DWORD Receive(Data** data_out, long timeout = g_SerialPluginTimeout);
       
   166 	/*
       
   167 	 * This method is used to wait for data to become available in incoming queue 
       
   168 	 * and then pop the first Data object from the queue and and store it to Data object given as parameter.
       
   169 	 */		
       
   170 	DWORD ReceiveWait(Data** data_out, long timeout = g_SerialPluginTimeout);
       
   171 	/*
       
   172 	 * This method checks if data is available on incoming queue
       
   173 	 */		
       
   174 	bool IsDataAvailable();
       
   175 	DWORD Open();
       
   176 	/*
       
   177 	 * This method stops SerialCommIoThread and waits for it to be in signaled state
       
   178 	 */		
       
   179 	DWORD Close();
       
   180 	SerialCommPlugin(const CommChannelPluginObserver* observer = NULL);
       
   181 	~SerialCommPlugin();
       
   182 private:
       
   183 	void CheckProperties(map<string, string>& props);
       
   184 private:
       
   185     //outgoing queue
       
   186 	SafeQueue<Data*> m_TxQueue;
       
   187 	//incoming queue	
       
   188 	SafeQueue<Data*> m_RxQueue;
       
   189 	SerialCommIoThread* m_IoThread;
       
   190 	//map that properties are read to from ini file
       
   191 	map<string, string> m_SerialCommPluginProperties;
       
   192 	long m_PropertySerialCommTimeout;
       
   193 	string m_PropertySerialCommInitString;
       
   194 	string m_PropertySerialCommPort;
       
   195 	long m_ProperySerialSendPause;
       
   196 	bool m_PropertySerialHwFlowControl;
       
   197 	long m_PropertySerialMaxSendSize;
       
   198 };
       
   199 
       
   200 #endif
       
   201 
       
   202 // End of the file