testmgmt/testdriver/shared/com.symbian.jstat/nativeSrc/JStat.cpp
changeset 0 f1112f777ce9
equal deleted inserted replaced
-1:000000000000 0:f1112f777ce9
       
     1 /*
       
     2 * Copyright (c) 2005-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 *
       
    16 */
       
    17 
       
    18 
       
    19 //Source file that creates the interface between the C++		*
       
    20 //STAT component and Java.	
       
    21 
       
    22 #include <iostream> 
       
    23 #include <String>
       
    24 
       
    25 #include <windows.h>
       
    26 
       
    27 #include "com_symbian_jstat_JStat.h"
       
    28 #include "Stat.h"
       
    29 #include "StatExp.h"
       
    30 
       
    31 static const int MAX_PORT_STRING=6;
       
    32 char ComPort[MAX_PORT_STRING]="";
       
    33 
       
    34 HMODULE	ihLib =	NULL;
       
    35 PROC_CONNECT	iptrConnect =	NULL;
       
    36 PROC_DISCONNECT	iptrDisconnect =	NULL;
       
    37 PROC_SENDRAWCOMMAND	iptrSendRawCommand =	NULL;
       
    38 PROC_SETCOMMANDLOGGING	iptrSetCommandLogging =	NULL;
       
    39 PROC_GETERROR	iptrGetError =	NULL;
       
    40 PROC_VERSION iptrVersion = NULL;
       
    41 
       
    42 PROC_GETRECEIVEDDATA iptrGetReceivedData = NULL;
       
    43 
       
    44 int iType;
       
    45 
       
    46 
       
    47 void jstring2string( JNIEnv *env, const jstring src, std::string &dest )
       
    48 {
       
    49 	const char	*ptr =	env->GetStringUTFChars( src, false );
       
    50 	dest =	ptr;
       
    51 	env->ReleaseStringUTFChars( src, ptr );
       
    52 }
       
    53 
       
    54 void string2jstring( JNIEnv *env, const char* src, jstring &dest )
       
    55 {
       
    56 	dest =	env->NewStringUTF( src );
       
    57 }
       
    58 
       
    59 
       
    60 JNIEXPORT jint JNICALL Java_com_symbian_jstat_JStatRunCommand_connect
       
    61   (JNIEnv *_env, jobject, jstring _pszPlatformType)
       
    62 {
       
    63 	try {
       
    64 		using std::cout;
       
    65 		using std::endl;
       
    66 
       
    67 		int connection=-1;
       
    68 		std::string aIPAddress;
       
    69 		std::string	pszPlatformType;
       
    70 
       
    71 		jstring2string( _env, _pszPlatformType, pszPlatformType );
       
    72 		
       
    73 		//***********************************************************************
       
    74 		//	USB
       
    75 		//***********************************************************************
       
    76 		if(strncmp(pszPlatformType.c_str(),"usb",3) == 0)
       
    77 			{
       
    78 			strcat(ComPort,pszPlatformType.c_str( )+3);
       
    79 			iType=SymbianUsb;
       
    80 			}
       
    81 	
       
    82 	
       
    83 		//***********************************************************************
       
    84 		//	TCP/IP
       
    85 		//***********************************************************************
       
    86 		else if(strncmp(pszPlatformType.c_str(),"tcp",3) == 0)
       
    87 			{
       
    88 			aIPAddress = pszPlatformType.substr(4);
       
    89 			pszPlatformType = pszPlatformType.substr(0,3);
       
    90 			iType=SymbianSocket;
       
    91 			}
       
    92 
       
    93 
       
    94 		//***********************************************************************
       
    95 		//	Infrared
       
    96 		//***********************************************************************
       
    97 		else if(strncmp(pszPlatformType.c_str( ),"ir",2) == 0)
       
    98 			{
       
    99 			strcpy(ComPort,"COM");				
       
   100 			strcat(ComPort,pszPlatformType.c_str( )+2);
       
   101 			}
       
   102 
       
   103 		//***********************************************************************
       
   104 		//	Serial
       
   105 		//***********************************************************************
       
   106 		else if(strncmp(pszPlatformType.c_str( ),"serial",6) == 0)
       
   107 			{
       
   108 			strcpy(ComPort,"COM");
       
   109 			strcat(ComPort,pszPlatformType.c_str( )+6);
       
   110 			iType=SymbianSerial;
       
   111 			}
       
   112 
       
   113 		//***********************************************************************
       
   114 		//	Bluetooth
       
   115 		//***********************************************************************
       
   116 		else if(strncmp(pszPlatformType.c_str( ),"bt",2) == 0)
       
   117 			{
       
   118 			strcpy(ComPort,"COM");
       
   119 			strcat(ComPort,pszPlatformType.c_str( )+2);
       
   120 			iType=SymbianBluetooth;
       
   121 			}
       
   122 
       
   123 		// load stat library first from local, if failed from epoc32
       
   124 
       
   125 		HMODULE	ihLib =	::LoadLibrary( DLLName );
       
   126 
       
   127 		if( ihLib  == NULL )
       
   128 			{
       
   129 			char path[MAX_PATH]=".\\stat.dll";
       
   130 			sprintf( path, "%s\\%s", DLLFolder, DLLName );
       
   131 			ihLib =	::LoadLibrary( path );
       
   132 			}
       
   133 
       
   134 		//if fails loading stat.dll return -1	
       
   135 		if( ihLib  == NULL )
       
   136 			{
       
   137 			cout << "Could not connect JStat.dll to STAT.dll." << endl;
       
   138 			return -1;
       
   139 			}
       
   140 		
       
   141 		// load functions
       
   142 		iptrConnect =	reinterpret_cast<PROC_CONNECT>(::GetProcAddress( ihLib, ProcConnect ));
       
   143 		iptrDisconnect =	reinterpret_cast<PROC_DISCONNECT>(::GetProcAddress( ihLib, ProcDisconnect ));
       
   144 		iptrSendRawCommand =	reinterpret_cast<PROC_SENDRAWCOMMAND>(::GetProcAddress( ihLib, ProcSendRawCommand ));
       
   145 		iptrSetCommandLogging =	reinterpret_cast<PROC_SETCOMMANDLOGGING>(::GetProcAddress( ihLib, ProcSetCommandLogging ));
       
   146 		iptrGetError =	reinterpret_cast<PROC_GETERROR>(::GetProcAddress( ihLib, ProcGetError ));
       
   147 		iptrGetReceivedData =	reinterpret_cast<PROC_GETRECEIVEDDATA>(::GetProcAddress( ihLib, ProcGetReceivedData ));
       
   148 		
       
   149 		//check all fuunctions were correctly loaded
       
   150 		if (iptrConnect == NULL)
       
   151 			{
       
   152 			cout << "Could not load JStat Function CONNECT." << endl;
       
   153 			return -1;	  
       
   154 			}
       
   155 		if (iptrDisconnect == NULL)
       
   156 			{
       
   157 			cout << "Could not load JStat Function DISCONNECT." << endl;
       
   158 			return -1;	  
       
   159 			}
       
   160 		if (iptrSendRawCommand == NULL)
       
   161 			{
       
   162 			cout << "Could not load JStat Function SENDRAWCOMMAND." << endl;
       
   163 			return -1;	  
       
   164 			}
       
   165 		if (iptrSetCommandLogging == NULL)
       
   166 			{
       
   167 			cout << "Could not load JStat Function SETCOMMANDLOGGING." << endl;
       
   168 			return -1;	  
       
   169 			}
       
   170 		if (iptrGetError == NULL)
       
   171 			{
       
   172 			cout << "Could not load JStat Function GETERROR." << endl;
       
   173 			return -1;	  
       
   174 			}
       
   175 		if (iptrGetReceivedData == NULL)
       
   176 		   {
       
   177 			cout << "Could not load JStat Function GETRECIEVEDDATA." << endl;
       
   178 			return -1;	   
       
   179 		   }
       
   180 		
       
   181 		
       
   182 		//connect using the previously set type
       
   183 		if(iType==SymbianSocket)
       
   184 			{
       
   185 			connection = (iptrConnect)((STATConnectType)iType,aIPAddress.c_str(),NULL,NULL);
       
   186 			}
       
   187 		else
       
   188 			{
       
   189 			connection = iptrConnect((STATConnectType)iType,ComPort,NULL,NULL);
       
   190 			}
       
   191 
       
   192 		
       
   193 		if(!connection)
       
   194 			{	
       
   195 			cout << "Could not connect JStat with transport." << endl;
       
   196 			return -1;
       
   197 			}
       
   198 		
       
   199 		
       
   200 		// Use default file name for logging (named according to the current date).
       
   201 		// Do not append.
       
   202 		char	*fileName =	NULL;
       
   203 		bool	append =	true;
       
   204 		(iptrSetCommandLogging)(connection,fileName,NULL,EVerbose,append,NULL,NULL);
       
   205 		
       
   206 		return connection;
       
   207 	} catch (...) {
       
   208 		return -1;
       
   209 	}
       
   210 
       
   211 }
       
   212 
       
   213 JNIEXPORT jint JNICALL Java_com_symbian_jstat_JStatRunCommand_disconnect
       
   214   (JNIEnv *_env, jobject, jint _handle)
       
   215 {
       
   216 	//disconnect from STAT
       
   217 	try {
       
   218 		int ret;
       
   219 		
       
   220 		ret=(iptrDisconnect)(_handle);
       
   221 		
       
   222 		if( ihLib != NULL )
       
   223 			{
       
   224 			::FreeLibrary( ihLib );
       
   225 			ihLib =	NULL;
       
   226 			iptrConnect =	NULL;
       
   227 			iptrDisconnect =	NULL;
       
   228 			iptrSendRawCommand =	NULL;
       
   229 			iptrGetError =	NULL;
       
   230 			}
       
   231 
       
   232 		return ret;
       
   233 	} catch (...) {
       
   234 		return -1;
       
   235 	}
       
   236 
       
   237 }
       
   238 
       
   239 JNIEXPORT jint JNICALL Java_com_symbian_jstat_JStatRunCommand_doCommand
       
   240   (JNIEnv *_env, jobject, jint _handle, jstring _command )
       
   241 {
       
   242 	//send a raw command to stat and returns the returned value
       
   243 	try {
       
   244 		int ret;
       
   245 		
       
   246 		std::string	command;
       
   247 		jstring2string( _env, _command, command );
       
   248 		ret = iptrSendRawCommand(_handle,command.c_str( ),NULL);
       
   249 
       
   250 		return (ret);
       
   251 	} catch (...) {
       
   252 		return -1;
       
   253 	}
       
   254 }
       
   255 
       
   256 
       
   257 JNIEXPORT jstring JNICALL Java_com_symbian_jstat_JStatRunCommand_getError
       
   258   (JNIEnv *_env, jobject, jint _handle)
       
   259 {
       
   260 	//return the error definition from stat
       
   261 	jstring error;
       
   262 
       
   263 	string2jstring( _env, iptrGetError(_handle), error);
       
   264 
       
   265 	return error;
       
   266 }
       
   267 
       
   268 
       
   269 JNIEXPORT jstring JNICALL Java_com_symbian_jstat_JStatRunCommand_getReceivedData
       
   270   (JNIEnv *_env, jobject, jint _handle)
       
   271 {
       
   272 	//return the exposed buffer from received data
       
   273 
       
   274 	return (_env->NewStringUTF(iptrGetReceivedData(_handle)));
       
   275 }
       
   276 
       
   277 JNIEXPORT jstring JNICALL Java_com_symbian_jstat_JStat_getStatVersion
       
   278 (JNIEnv *_env, jobject)
       
   279 {
       
   280 	// load stat library first from local, if failed from epoc32
       
   281 	HMODULE	ihLib =	::LoadLibrary( DLLName );
       
   282 
       
   283 	if( ihLib  == NULL )
       
   284 		{
       
   285 		char path[MAX_PATH]=".\\stat.dll";
       
   286 		sprintf( path, "%s\\%s", DLLFolder, DLLName );
       
   287 		ihLib =	::LoadLibrary( path );
       
   288 		}
       
   289 
       
   290 	//if fails loading stat.dll return -1	
       
   291 	if( ihLib  == NULL )
       
   292 		{
       
   293 		return _env->NewStringUTF("Error loading stat.dll");
       
   294 		}
       
   295 	
       
   296 	// load version function
       
   297 	iptrVersion =	reinterpret_cast<PROC_VERSION>(::GetProcAddress( ihLib, ProcVersion ));
       
   298 
       
   299 	//check version function was correctly loaded
       
   300 	if(iptrVersion ==NULL )
       
   301 	   {
       
   302 	   return _env->NewStringUTF("Error loading Version function");	   
       
   303 	   }
       
   304 	
       
   305 	return _env->NewStringUTF(iptrVersion());
       
   306 }
       
   307