connectivity/com.nokia.tcf/src/com/nokia/tcf/impl/TCAPIConnection.java
author cawthron
Fri, 15 Jan 2010 11:19:08 -0600
branchRCL_2_4
changeset 768 774b5f74e4ed
parent 60 9d2210c8eed2
child 665 12ea338ad1f6
child 916 6743933eec70
permissions -rw-r--r--
add release notes about WINSCW compiler 3.2.5 build 487
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
/**
cawthron
parents:
diff changeset
    18
 * 
cawthron
parents:
diff changeset
    19
 */
cawthron
parents:
diff changeset
    20
package com.nokia.tcf.impl;
cawthron
parents:
diff changeset
    21
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    22
import com.nokia.tcf.Activator;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    23
import com.nokia.tcf.api.*;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    24
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    25
import org.eclipse.core.runtime.*;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    26
2
cawthron
parents:
diff changeset
    27
import java.io.File;
cawthron
parents:
diff changeset
    28
import java.io.IOException;
cawthron
parents:
diff changeset
    29
import java.text.MessageFormat;
cawthron
parents:
diff changeset
    30
cawthron
parents:
diff changeset
    31
public class TCAPIConnection implements ITCAPIConnection {
cawthron
parents:
diff changeset
    32
cawthron
parents:
diff changeset
    33
	static {
cawthron
parents:
diff changeset
    34
		Activator plugin = Activator.getDefault();
cawthron
parents:
diff changeset
    35
		IPath path = null;
cawthron
parents:
diff changeset
    36
		String dePath = null;
cawthron
parents:
diff changeset
    37
		if (plugin != null)
cawthron
parents:
diff changeset
    38
			path = plugin.getDebuggerPath();
cawthron
parents:
diff changeset
    39
		if (path != null)
cawthron
parents:
diff changeset
    40
			dePath = path.toOSString();
cawthron
parents:
diff changeset
    41
		if (dePath == null) {
cawthron
parents:
diff changeset
    42
			System.loadLibrary("TCFClient");
cawthron
parents:
diff changeset
    43
		} else {
cawthron
parents:
diff changeset
    44
			try{
cawthron
parents:
diff changeset
    45
				System.load(dePath + java.io.File.separator + "TCFClient.dll");
cawthron
parents:
diff changeset
    46
			} catch (UnsatisfiedLinkError e) {
cawthron
parents:
diff changeset
    47
				// if Carbide DLL is not found in DE, 
cawthron
parents:
diff changeset
    48
				// try to load one from the plugin itself
cawthron
parents:
diff changeset
    49
				System.loadLibrary("TCFClient");
cawthron
parents:
diff changeset
    50
			}
cawthron
parents:
diff changeset
    51
		}
cawthron
parents:
diff changeset
    52
	}
cawthron
parents:
diff changeset
    53
	private TCErrorListenerList<ITCErrorListener> errorListenerList;
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    54
	protected ITCCookie cookie;		// this client's handle to native
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    55
	protected ITCMessageIds messageIds; // this client's message ids
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    56
	protected TCMessageInputStream inputStream; // this client's message input stream (not using message file)
2
cawthron
parents:
diff changeset
    57
	protected ITCMessageOptions messageOptions; // this client's message options
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
    58
	protected TCFMonitorThread monitorThread; // the client's native monitor
2
cawthron
parents:
diff changeset
    59
	public boolean stopTCFMonitorThread;	//  stream monitor start/stop flag
cawthron
parents:
diff changeset
    60
	protected IStatus statusOK;
cawthron
parents:
diff changeset
    61
	protected ITCConnection connection; // the client's connection
cawthron
parents:
diff changeset
    62
cawthron
parents:
diff changeset
    63
	public ITCConnection getConnection() {
cawthron
parents:
diff changeset
    64
		return this.connection;
cawthron
parents:
diff changeset
    65
	}
cawthron
parents:
diff changeset
    66
	public ITCMessageOptions getMessageOptions() {
cawthron
parents:
diff changeset
    67
		return this.messageOptions;
cawthron
parents:
diff changeset
    68
	}
cawthron
parents:
diff changeset
    69
	/**
cawthron
parents:
diff changeset
    70
	 * Only constructor - fields are created using the public methods
cawthron
parents:
diff changeset
    71
	 */
cawthron
parents:
diff changeset
    72
	public TCAPIConnection() {
cawthron
parents:
diff changeset
    73
		super();
cawthron
parents:
diff changeset
    74
		this.errorListenerList = new TCErrorListenerList<ITCErrorListener>();
cawthron
parents:
diff changeset
    75
		this.errorListenerList.clear();
cawthron
parents:
diff changeset
    76
		this.connection = null;
cawthron
parents:
diff changeset
    77
		IStatus status = new Status(Status.OK, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_NONE, "OK", null);
cawthron
parents:
diff changeset
    78
		statusOK = status;
cawthron
parents:
diff changeset
    79
		this.cookie = new TCCookie(-1, status, false, false);
cawthron
parents:
diff changeset
    80
		this.inputStream = null;
cawthron
parents:
diff changeset
    81
		this.messageIds = null;
cawthron
parents:
diff changeset
    82
		this.messageOptions = null;
cawthron
parents:
diff changeset
    83
		this.monitorThread = null;
cawthron
parents:
diff changeset
    84
		this.stopTCFMonitorThread = false;
cawthron
parents:
diff changeset
    85
	}
cawthron
parents:
diff changeset
    86
cawthron
parents:
diff changeset
    87
	/* (non-Javadoc)
cawthron
parents:
diff changeset
    88
	 * @see com.nokia.tcf.api.ITCAPIConnection#addErrorListener(com.nokia.tcf.api.ITCErrorListener)
cawthron
parents:
diff changeset
    89
	 */
cawthron
parents:
diff changeset
    90
	public IStatus addErrorListener(ITCErrorListener inErrorListener) {
cawthron
parents:
diff changeset
    91
		IStatus status = statusOK;
cawthron
parents:
diff changeset
    92
		try {
cawthron
parents:
diff changeset
    93
			errorListenerList.add(inErrorListener);
cawthron
parents:
diff changeset
    94
		} catch (NullPointerException e) {
cawthron
parents:
diff changeset
    95
			int err = (int)TCErrorConstants.TCAPI_ERR_ERRLISTENER_NULL;
cawthron
parents:
diff changeset
    96
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, err,	TCErrorConstants.getErrorMessage(err), e);
cawthron
parents:
diff changeset
    97
		}
cawthron
parents:
diff changeset
    98
//		System.out.printf("addErrorListener status = %s\n", status.getMessage());
cawthron
parents:
diff changeset
    99
		return status;
cawthron
parents:
diff changeset
   100
	}
cawthron
parents:
diff changeset
   101
cawthron
parents:
diff changeset
   102
	private IStatus checkConnected() {
cawthron
parents:
diff changeset
   103
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   104
		if (this.connection == null || this.cookie.isConnected() == false) {
cawthron
parents:
diff changeset
   105
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MEDIA_NOT_OPEN,
cawthron
parents:
diff changeset
   106
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MEDIA_NOT_OPEN), null);
cawthron
parents:
diff changeset
   107
		}
cawthron
parents:
diff changeset
   108
		return status;
cawthron
parents:
diff changeset
   109
	}
cawthron
parents:
diff changeset
   110
	protected IStatus checkDecodeFormat(String decodeFormat) {
cawthron
parents:
diff changeset
   111
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   112
		return status;		
cawthron
parents:
diff changeset
   113
	}
cawthron
parents:
diff changeset
   114
	protected IStatus checkConnectionType(ITCConnection inConnection) {
cawthron
parents:
diff changeset
   115
		IStatus status = statusOK;
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   116
		String type = inConnection.getConnectionType();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   117
		if (type.compareToIgnoreCase("virtualserial") == 0) {
2
cawthron
parents:
diff changeset
   118
			ITCVirtualSerialConnection c = (ITCVirtualSerialConnection)inConnection;
cawthron
parents:
diff changeset
   119
			String p = c.getComPort();
cawthron
parents:
diff changeset
   120
			if (p == null) {
cawthron
parents:
diff changeset
   121
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MISSING_MEDIA_DATA, 
cawthron
parents:
diff changeset
   122
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MISSING_MEDIA_DATA), null);
cawthron
parents:
diff changeset
   123
			}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   124
		} else if (type.compareToIgnoreCase("tcp") == 0) {
2
cawthron
parents:
diff changeset
   125
			ITCRealTCPConnection c = (ITCRealTCPConnection)inConnection;
cawthron
parents:
diff changeset
   126
			String ip = c.getIpAddress();
cawthron
parents:
diff changeset
   127
			String p = c.getPort();
cawthron
parents:
diff changeset
   128
			if (ip == null || p == null) {
cawthron
parents:
diff changeset
   129
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MISSING_MEDIA_DATA, 
cawthron
parents:
diff changeset
   130
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MISSING_MEDIA_DATA), null);
cawthron
parents:
diff changeset
   131
			}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   132
		} else if (type.compareToIgnoreCase("serial") == 0) {
2
cawthron
parents:
diff changeset
   133
			ITCRealSerialConnection c = (ITCRealSerialConnection)inConnection;
cawthron
parents:
diff changeset
   134
			long err = checkRealSerialSettings(c);
cawthron
parents:
diff changeset
   135
			if (err != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   136
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)err, TCErrorConstants.getErrorMessage(err), null);
cawthron
parents:
diff changeset
   137
			}
cawthron
parents:
diff changeset
   138
		} else {
cawthron
parents:
diff changeset
   139
			// Add other connection types here
cawthron
parents:
diff changeset
   140
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_UNKNOWN_MEDIA_TYPE,
cawthron
parents:
diff changeset
   141
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_UNKNOWN_MEDIA_TYPE), null);
cawthron
parents:
diff changeset
   142
		}
cawthron
parents:
diff changeset
   143
		
cawthron
parents:
diff changeset
   144
		return status;
cawthron
parents:
diff changeset
   145
	}
cawthron
parents:
diff changeset
   146
	private IStatus checkConnection(ITCConnection inConnection) {
cawthron
parents:
diff changeset
   147
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   148
		if (this.cookie.isConnected()) {
cawthron
parents:
diff changeset
   149
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_ALREADY_CONNECTED, 
cawthron
parents:
diff changeset
   150
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_ALREADY_CONNECTED), null);
cawthron
parents:
diff changeset
   151
		}
cawthron
parents:
diff changeset
   152
		if (inConnection == null) {
cawthron
parents:
diff changeset
   153
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MISSING_CONNECTION_SPEC, 
cawthron
parents:
diff changeset
   154
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MISSING_CONNECTION_SPEC), null);
cawthron
parents:
diff changeset
   155
		}
cawthron
parents:
diff changeset
   156
		if (status.isOK()) {
cawthron
parents:
diff changeset
   157
			String decodeFormat = inConnection.getDecodeFormat();
cawthron
parents:
diff changeset
   158
			status = checkDecodeFormat(decodeFormat);
cawthron
parents:
diff changeset
   159
		}
cawthron
parents:
diff changeset
   160
		if (status.isOK()) {
cawthron
parents:
diff changeset
   161
			long retryI = inConnection.getRetryInterval();
cawthron
parents:
diff changeset
   162
			long retryT = inConnection.getRetryTimeout();
cawthron
parents:
diff changeset
   163
			if (retryI == 0 || retryT == 0 || retryI > retryT) {
cawthron
parents:
diff changeset
   164
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_INVALID_RETRY_PERIODS, 
cawthron
parents:
diff changeset
   165
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_INVALID_RETRY_PERIODS), null);
cawthron
parents:
diff changeset
   166
			}
cawthron
parents:
diff changeset
   167
		}
cawthron
parents:
diff changeset
   168
		
cawthron
parents:
diff changeset
   169
		if (status.isOK()) {
cawthron
parents:
diff changeset
   170
			status = checkConnectionType(inConnection);
cawthron
parents:
diff changeset
   171
		}
cawthron
parents:
diff changeset
   172
	
cawthron
parents:
diff changeset
   173
		return status;
cawthron
parents:
diff changeset
   174
	}
cawthron
parents:
diff changeset
   175
	private long checkRealSerialSettings(ITCRealSerialConnection c) {
cawthron
parents:
diff changeset
   176
		String[] setting = new String[6];
cawthron
parents:
diff changeset
   177
		setting[0] = c.getBaudRate();
cawthron
parents:
diff changeset
   178
		setting[1] = c.getComPort();
cawthron
parents:
diff changeset
   179
		setting[2] = c.getDataBits();
cawthron
parents:
diff changeset
   180
		setting[3] = c.getFlowControl();
cawthron
parents:
diff changeset
   181
		setting[4] = c.getParity();
cawthron
parents:
diff changeset
   182
		setting[5] = c.getStopBits();
cawthron
parents:
diff changeset
   183
		for (int i = 0; i < 6; i++) {
cawthron
parents:
diff changeset
   184
			if (setting[i] == null) {
cawthron
parents:
diff changeset
   185
				return TCErrorConstants.TCAPI_ERR_MISSING_MEDIA_DATA;
cawthron
parents:
diff changeset
   186
			}
cawthron
parents:
diff changeset
   187
		}
cawthron
parents:
diff changeset
   188
		if ((setting[2] != ITCRealSerialConnection.DATABITS_4) && (setting[2] != ITCRealSerialConnection.DATABITS_5) && (setting[2] != ITCRealSerialConnection.DATABITS_6) &&
cawthron
parents:
diff changeset
   189
				(setting[2] != ITCRealSerialConnection.DATABITS_7) && (setting[2] != ITCRealSerialConnection.DATABITS_8)) {
cawthron
parents:
diff changeset
   190
			return TCErrorConstants.TCAPI_ERR_COMM_INVALID_DATABITS;
cawthron
parents:
diff changeset
   191
		}
cawthron
parents:
diff changeset
   192
		if ((setting[3] != ITCRealSerialConnection.FLOWCONTROL_HW) && (setting[3] != ITCRealSerialConnection.FLOWCONTROL_NONE) && (setting[3] != ITCRealSerialConnection.FLOWCONTROL_SW)) {
cawthron
parents:
diff changeset
   193
			return TCErrorConstants.TCAPI_ERR_COMM_INVALID_FLOWCONTROL;
cawthron
parents:
diff changeset
   194
		}
cawthron
parents:
diff changeset
   195
		if ((setting[4] != ITCRealSerialConnection.PARITY_EVEN) && (setting[4] != ITCRealSerialConnection.PARITY_NONE) && (setting[4] != ITCRealSerialConnection.PARITY_ODD)) {
cawthron
parents:
diff changeset
   196
			return TCErrorConstants.TCAPI_ERR_COMM_INVALID_PARITY;
cawthron
parents:
diff changeset
   197
		}
cawthron
parents:
diff changeset
   198
		if ((setting[5] != ITCRealSerialConnection.STOPBITS_1) && (setting[5] != ITCRealSerialConnection.STOPBITS_1_5) && (setting[5] != ITCRealSerialConnection.STOPBITS_2)) {
cawthron
parents:
diff changeset
   199
			return TCErrorConstants.TCAPI_ERR_COMM_INVALID_STOPBITS;
cawthron
parents:
diff changeset
   200
		}
cawthron
parents:
diff changeset
   201
		
cawthron
parents:
diff changeset
   202
		return TCErrorConstants.TCAPI_ERR_NONE;
cawthron
parents:
diff changeset
   203
	}
cawthron
parents:
diff changeset
   204
	private IStatus checkMessage(ITCMessage inMessage) {
cawthron
parents:
diff changeset
   205
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   206
cawthron
parents:
diff changeset
   207
		if (inMessage == null) {
cawthron
parents:
diff changeset
   208
			// inMessage cannot be null
cawthron
parents:
diff changeset
   209
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MISSING_MESSAGE,
cawthron
parents:
diff changeset
   210
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MISSING_MESSAGE), null);
cawthron
parents:
diff changeset
   211
		} else if (inMessage.size() == 0) {
cawthron
parents:
diff changeset
   212
			// bytes to send may be 0 if we're doing the header (header is the message)
cawthron
parents:
diff changeset
   213
			if (this.messageOptions.getMessageEncodeFormat() == ITCMessageOptions.ENCODE_NO_FORMAT) {
cawthron
parents:
diff changeset
   214
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MISSING_MESSAGE,
cawthron
parents:
diff changeset
   215
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MISSING_MESSAGE), null);
cawthron
parents:
diff changeset
   216
			}
cawthron
parents:
diff changeset
   217
		}
cawthron
parents:
diff changeset
   218
		if (status.isOK()) {
cawthron
parents:
diff changeset
   219
			// check options
cawthron
parents:
diff changeset
   220
			if (inMessage.isUseMyMessageId()) {
cawthron
parents:
diff changeset
   221
				if (this.messageOptions.getMessageEncodeFormat() == ITCMessageOptions.ENCODE_NO_FORMAT) {
cawthron
parents:
diff changeset
   222
					// use my id, but don't encode = error
cawthron
parents:
diff changeset
   223
					status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MESSAGE_OPTIONS_CONFLICT,
cawthron
parents:
diff changeset
   224
							TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MESSAGE_OPTIONS_CONFLICT), null);
cawthron
parents:
diff changeset
   225
				} else {
cawthron
parents:
diff changeset
   226
					// use my id, and encode = OK
cawthron
parents:
diff changeset
   227
				}
cawthron
parents:
diff changeset
   228
			} else {
cawthron
parents:
diff changeset
   229
				if (this.messageOptions.getMessageEncodeFormat() == ITCMessageOptions.ENCODE_NO_FORMAT) {
cawthron
parents:
diff changeset
   230
					// don't use my id, and don't encode = OK
cawthron
parents:
diff changeset
   231
				} else {
cawthron
parents:
diff changeset
   232
					// don't use my id, but encode = warning (we'll go ahead send this message raw)
cawthron
parents:
diff changeset
   233
					status = new Status(Status.WARNING, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MESSAGE_OPTIONS_CONFLICT,
cawthron
parents:
diff changeset
   234
							TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MESSAGE_OPTIONS_CONFLICT), null);
cawthron
parents:
diff changeset
   235
				}
cawthron
parents:
diff changeset
   236
			}
cawthron
parents:
diff changeset
   237
		}
cawthron
parents:
diff changeset
   238
		return status;
cawthron
parents:
diff changeset
   239
	}
cawthron
parents:
diff changeset
   240
	private IStatus checkMessageIds(ITCMessageIds inMessageIds) {
cawthron
parents:
diff changeset
   241
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   242
cawthron
parents:
diff changeset
   243
		if (inMessageIds == null || inMessageIds.size() == 0) {
cawthron
parents:
diff changeset
   244
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_NO_MESSAGESIDS_REGISTERED,
cawthron
parents:
diff changeset
   245
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_NO_MESSAGESIDS_REGISTERED), null);
cawthron
parents:
diff changeset
   246
		}
cawthron
parents:
diff changeset
   247
		if (status.isOK()) {
cawthron
parents:
diff changeset
   248
			if (inMessageIds.size() > 256) {
cawthron
parents:
diff changeset
   249
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MESSAGEID_MAXIMUM,
cawthron
parents:
diff changeset
   250
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MESSAGEID_MAXIMUM), null);
cawthron
parents:
diff changeset
   251
			}
cawthron
parents:
diff changeset
   252
		}
cawthron
parents:
diff changeset
   253
		return status;
cawthron
parents:
diff changeset
   254
	}
cawthron
parents:
diff changeset
   255
cawthron
parents:
diff changeset
   256
cawthron
parents:
diff changeset
   257
	private IStatus checkMessageOptions(ITCMessageOptions inMessageOptions) {
cawthron
parents:
diff changeset
   258
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   259
cawthron
parents:
diff changeset
   260
		if (inMessageOptions == null) {
cawthron
parents:
diff changeset
   261
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_MISSING_MESSAGE_OPTIONS,
cawthron
parents:
diff changeset
   262
					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_MISSING_MESSAGE_OPTIONS), null);
cawthron
parents:
diff changeset
   263
		}
cawthron
parents:
diff changeset
   264
//		if (status.isOK()) {
cawthron
parents:
diff changeset
   265
//			long option = inMessageOptions.getMessageDestination();
cawthron
parents:
diff changeset
   266
//			String file = inMessageOptions.getMessageFile().toOSString();
cawthron
parents:
diff changeset
   267
//			if (option == ITCMessageOptions.DESTINATION_CLIENTFILE) {
cawthron
parents:
diff changeset
   268
//				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_FEATURE_NOT_IMPLEMENTED,
cawthron
parents:
diff changeset
   269
//					TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_FEATURE_NOT_IMPLEMENTED), null);
cawthron
parents:
diff changeset
   270
//			}
cawthron
parents:
diff changeset
   271
//		}
cawthron
parents:
diff changeset
   272
		
cawthron
parents:
diff changeset
   273
		if (status.isOK()) {
cawthron
parents:
diff changeset
   274
			long option = inMessageOptions.getMessageEncodeFormat();
cawthron
parents:
diff changeset
   275
			switch ((int)option) {
cawthron
parents:
diff changeset
   276
			case (int)ITCMessageOptions.ENCODE_NO_FORMAT:
cawthron
parents:
diff changeset
   277
			case (int)ITCMessageOptions.ENCODE_FORMAT:
cawthron
parents:
diff changeset
   278
			case (int)ITCMessageOptions.ENCODE_TRK_FORMAT:
cawthron
parents:
diff changeset
   279
				break;
cawthron
parents:
diff changeset
   280
			default:
cawthron
parents:
diff changeset
   281
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_INVALID_ENCODE_FORMAT,
cawthron
parents:
diff changeset
   282
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_INVALID_ENCODE_FORMAT), null);
cawthron
parents:
diff changeset
   283
			}
cawthron
parents:
diff changeset
   284
		}
cawthron
parents:
diff changeset
   285
		if (status.isOK()) {
cawthron
parents:
diff changeset
   286
			long option = inMessageOptions.getUnWrapFormat();
cawthron
parents:
diff changeset
   287
			switch ((int)option) {
cawthron
parents:
diff changeset
   288
			case (int)ITCMessageOptions.UNWRAP_DELETE_HEADERS:
cawthron
parents:
diff changeset
   289
			case (int)ITCMessageOptions.UNWRAP_LEAVE_HEADERS:
cawthron
parents:
diff changeset
   290
				break;
cawthron
parents:
diff changeset
   291
			default:
cawthron
parents:
diff changeset
   292
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_INVALID_MESSAGE_UNWRAP_OPTION,
cawthron
parents:
diff changeset
   293
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_INVALID_MESSAGE_UNWRAP_OPTION), null);
cawthron
parents:
diff changeset
   294
			}
cawthron
parents:
diff changeset
   295
		}
cawthron
parents:
diff changeset
   296
		
cawthron
parents:
diff changeset
   297
		if (status.isOK()) {
cawthron
parents:
diff changeset
   298
			long bufferSize = inMessageOptions.getInputStreamSize();
cawthron
parents:
diff changeset
   299
			if (bufferSize <= 0) {
cawthron
parents:
diff changeset
   300
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_INVALID_STREAM_BUFFER_SIZE,
cawthron
parents:
diff changeset
   301
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_INVALID_STREAM_BUFFER_SIZE), null);
cawthron
parents:
diff changeset
   302
			}
cawthron
parents:
diff changeset
   303
		}
cawthron
parents:
diff changeset
   304
		
cawthron
parents:
diff changeset
   305
		return status;
cawthron
parents:
diff changeset
   306
	}
cawthron
parents:
diff changeset
   307
cawthron
parents:
diff changeset
   308
	protected IStatus checkConnectOptions(ITCConnection inConnection,
cawthron
parents:
diff changeset
   309
			ITCMessageOptions inMessageOptions, ITCMessageIds inMessageIds) {
cawthron
parents:
diff changeset
   310
		
cawthron
parents:
diff changeset
   311
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   312
cawthron
parents:
diff changeset
   313
		status = checkConnection(inConnection);
cawthron
parents:
diff changeset
   314
		
cawthron
parents:
diff changeset
   315
		if (status.isOK()) {
cawthron
parents:
diff changeset
   316
			status = checkMessageOptions(inMessageOptions);
cawthron
parents:
diff changeset
   317
		}
cawthron
parents:
diff changeset
   318
		if (status.isOK()) {
cawthron
parents:
diff changeset
   319
			status = checkMessageIds(inMessageIds);
cawthron
parents:
diff changeset
   320
		}
cawthron
parents:
diff changeset
   321
		return status;
cawthron
parents:
diff changeset
   322
	}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   323
	protected IStatus finishConnect(String type, String[] settings, 
2
cawthron
parents:
diff changeset
   324
			ITCConnection inConnection, ITCMessageOptions inMessageOptions, ITCMessageIds inMessageIds) {
cawthron
parents:
diff changeset
   325
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   326
		// connect
cawthron
parents:
diff changeset
   327
		long[] clientId = new long[1];
cawthron
parents:
diff changeset
   328
		clientId[0] = -1;
cawthron
parents:
diff changeset
   329
		long[] options = new long[3];
cawthron
parents:
diff changeset
   330
		options[0] = inConnection.getRetryInterval();		// connection options
cawthron
parents:
diff changeset
   331
		options[1] = inConnection.getRetryTimeout();
cawthron
parents:
diff changeset
   332
		options[2] = 0;
cawthron
parents:
diff changeset
   333
		long[] moptions = new long[2];
cawthron
parents:
diff changeset
   334
		moptions[0] = inMessageOptions.getUnWrapFormat();
cawthron
parents:
diff changeset
   335
		moptions[1] = inMessageOptions.getOSTVersion();
cawthron
parents:
diff changeset
   336
		String filePath = null;
cawthron
parents:
diff changeset
   337
		if (inMessageOptions.getMessageDestination() == ITCMessageOptions.DESTINATION_CLIENTFILE) {
cawthron
parents:
diff changeset
   338
			filePath = inMessageOptions.getMessageFile().toOSString();
cawthron
parents:
diff changeset
   339
			try {
cawthron
parents:
diff changeset
   340
				ensureWritableFile(filePath);
cawthron
parents:
diff changeset
   341
			} catch (IOException e) {
cawthron
parents:
diff changeset
   342
				String msg = e.getMessage();
cawthron
parents:
diff changeset
   343
				long err = TCErrorConstants.TCAPI_ERR_CREATE_FILE;
cawthron
parents:
diff changeset
   344
				String tcErr = String.format("%s OSError: %s", TCErrorConstants.getErrorMessage(err), msg);
cawthron
parents:
diff changeset
   345
				status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 
cawthron
parents:
diff changeset
   346
						(int)TCErrorConstants.TCAPI_ERR_CREATE_FILE, tcErr, e);
cawthron
parents:
diff changeset
   347
			}
cawthron
parents:
diff changeset
   348
		}
cawthron
parents:
diff changeset
   349
		
cawthron
parents:
diff changeset
   350
		try {
cawthron
parents:
diff changeset
   351
			long ret = nativeConnect(type, options, settings, moptions, filePath, clientId); 
cawthron
parents:
diff changeset
   352
			if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   353
				this.cookie.setClientId(clientId[0]);
cawthron
parents:
diff changeset
   354
				this.cookie.setConnected(true);
cawthron
parents:
diff changeset
   355
				this.connection = inConnection;
cawthron
parents:
diff changeset
   356
				this.messageOptions = inMessageOptions;
cawthron
parents:
diff changeset
   357
			} else {
cawthron
parents:
diff changeset
   358
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   359
			}
cawthron
parents:
diff changeset
   360
		} catch (Exception e) {
cawthron
parents:
diff changeset
   361
			// exception is thrown ONLY when err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA from native
cawthron
parents:
diff changeset
   362
			// and the message is the OS error generated
cawthron
parents:
diff changeset
   363
			String msg = e.getMessage();
cawthron
parents:
diff changeset
   364
			long ret = TCErrorConstants.TCAPI_ERR_WHILE_CONFIGURING_MEDIA;
cawthron
parents:
diff changeset
   365
			String tcErr = String.format("%s OSError: %s", TCErrorConstants.getErrorMessage(ret), msg);
cawthron
parents:
diff changeset
   366
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, tcErr, null);
cawthron
parents:
diff changeset
   367
		}
cawthron
parents:
diff changeset
   368
		// send message ids to capture
cawthron
parents:
diff changeset
   369
		if (status.isOK()) {
cawthron
parents:
diff changeset
   370
			int number = (int)inMessageIds.size();
cawthron
parents:
diff changeset
   371
			byte[] ids = new byte[number];
cawthron
parents:
diff changeset
   372
			for (int i = 0; i < number; i++) {
cawthron
parents:
diff changeset
   373
				ids[i] = inMessageIds.getMessageIds().get(i);
cawthron
parents:
diff changeset
   374
			}
cawthron
parents:
diff changeset
   375
			long ret = nativeSetMessageIds(this.cookie.getClientId(), ids);
cawthron
parents:
diff changeset
   376
			if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   377
				this.messageIds = inMessageIds;
cawthron
parents:
diff changeset
   378
			} else {
cawthron
parents:
diff changeset
   379
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   380
			}
cawthron
parents:
diff changeset
   381
		}
cawthron
parents:
diff changeset
   382
		// setup input stream
cawthron
parents:
diff changeset
   383
		if (status.isOK() && inMessageOptions.getMessageDestination() == ITCMessageOptions.DESTINATION_INPUTSTREAM) {
cawthron
parents:
diff changeset
   384
			inputStream = new TCMessageInputStream(this,
cawthron
parents:
diff changeset
   385
					inMessageOptions.getInputStreamSize(),
cawthron
parents:
diff changeset
   386
					cookie.getClientId());
cawthron
parents:
diff changeset
   387
			try {
cawthron
parents:
diff changeset
   388
				inputStream.open();
cawthron
parents:
diff changeset
   389
			} catch (IOException e) {
cawthron
parents:
diff changeset
   390
				// stream didn't open
cawthron
parents:
diff changeset
   391
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
cawthron
parents:
diff changeset
   392
				e.printStackTrace();
cawthron
parents:
diff changeset
   393
			}
cawthron
parents:
diff changeset
   394
		}
cawthron
parents:
diff changeset
   395
		// create error monitor
cawthron
parents:
diff changeset
   396
		if (status.isOK()) {
cawthron
parents:
diff changeset
   397
			String monitorName = String.format("TCFMonitor%d", this.cookie.getClientId());
cawthron
parents:
diff changeset
   398
			monitorThread = new TCFMonitorThread(this, monitorName);
cawthron
parents:
diff changeset
   399
			stopTCFMonitorThread = false;
cawthron
parents:
diff changeset
   400
			monitorThread.start();
cawthron
parents:
diff changeset
   401
		}
cawthron
parents:
diff changeset
   402
		
cawthron
parents:
diff changeset
   403
		// start capture
cawthron
parents:
diff changeset
   404
		if (status.isOK()) {
cawthron
parents:
diff changeset
   405
			long ret = nativeStart(this.cookie.getClientId());
cawthron
parents:
diff changeset
   406
			if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   407
				this.cookie.setMessageProcessing(true);
cawthron
parents:
diff changeset
   408
			} else {
cawthron
parents:
diff changeset
   409
				// error in starting capture
cawthron
parents:
diff changeset
   410
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   411
			}
cawthron
parents:
diff changeset
   412
		}
cawthron
parents:
diff changeset
   413
		if (!status.isOK()) {
cawthron
parents:
diff changeset
   414
			// an error occurred along the way - unravel what we've done
cawthron
parents:
diff changeset
   415
			if (this.cookie.isMessageProcessing()) {
cawthron
parents:
diff changeset
   416
				long ret = nativeStop(this.cookie.getClientId());
cawthron
parents:
diff changeset
   417
				// ignore error
cawthron
parents:
diff changeset
   418
				this.cookie.setMessageProcessing(false);
cawthron
parents:
diff changeset
   419
			}
cawthron
parents:
diff changeset
   420
			if (this.monitorThread != null) {
cawthron
parents:
diff changeset
   421
				this.stopTCFMonitorThread = true;
cawthron
parents:
diff changeset
   422
				try {
cawthron
parents:
diff changeset
   423
					this.monitorThread.join();
cawthron
parents:
diff changeset
   424
				} catch (InterruptedException e) {
cawthron
parents:
diff changeset
   425
				}
cawthron
parents:
diff changeset
   426
				this.monitorThread = null;
cawthron
parents:
diff changeset
   427
			}
cawthron
parents:
diff changeset
   428
			if (this.inputStream != null && this.inputStream.isOpen()) {
cawthron
parents:
diff changeset
   429
				try {
cawthron
parents:
diff changeset
   430
					this.inputStream.close();
cawthron
parents:
diff changeset
   431
				} catch (IOException e) {
cawthron
parents:
diff changeset
   432
				}
cawthron
parents:
diff changeset
   433
				this.inputStream = null;
cawthron
parents:
diff changeset
   434
			}
cawthron
parents:
diff changeset
   435
			if (this.cookie.isConnected()) {
cawthron
parents:
diff changeset
   436
				long ret = nativeDisconnect(this.getClientId());
cawthron
parents:
diff changeset
   437
				this.cookie.setConnected(false);
cawthron
parents:
diff changeset
   438
				this.cookie.setClientId(-1);
cawthron
parents:
diff changeset
   439
			}
cawthron
parents:
diff changeset
   440
		}
cawthron
parents:
diff changeset
   441
		return status;
cawthron
parents:
diff changeset
   442
	}
cawthron
parents:
diff changeset
   443
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   444
	 * @see com.nokia.tcf.api.ITCAPIConnection#connect(com.nokia.tcf.api.ITCConnection, com.nokia.tcf.api.ITCMessageOptions, com.nokia.tcf.api.ITCMessageIds)
cawthron
parents:
diff changeset
   445
	 */
cawthron
parents:
diff changeset
   446
	public IStatus connect(ITCConnection inConnection,
cawthron
parents:
diff changeset
   447
			ITCMessageOptions inMessageOptions, ITCMessageIds inMessageIds) {
cawthron
parents:
diff changeset
   448
		
cawthron
parents:
diff changeset
   449
		IStatus status = checkConnectOptions(inConnection, inMessageOptions, inMessageIds);
cawthron
parents:
diff changeset
   450
		String[] settings = null;
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   451
		String type = null;
2
cawthron
parents:
diff changeset
   452
cawthron
parents:
diff changeset
   453
		// do connect
cawthron
parents:
diff changeset
   454
		if (status.isOK()) {
cawthron
parents:
diff changeset
   455
			settings = null;
cawthron
parents:
diff changeset
   456
			type = inConnection.getConnectionType();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   457
			if (type.compareToIgnoreCase("tcp") == 0) {
2
cawthron
parents:
diff changeset
   458
				settings = new String[3];
cawthron
parents:
diff changeset
   459
				ITCRealTCPConnection t = (ITCRealTCPConnection)inConnection;
cawthron
parents:
diff changeset
   460
				settings[0] = t.getIpAddress();
cawthron
parents:
diff changeset
   461
				settings[1] = t.getPort();
cawthron
parents:
diff changeset
   462
				settings[2] = t.getDecodeFormat().toLowerCase();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   463
			} else if (type.compareToIgnoreCase("virtualserial") == 0) {
2
cawthron
parents:
diff changeset
   464
				settings = new String[2];
cawthron
parents:
diff changeset
   465
				ITCVirtualSerialConnection s = (ITCVirtualSerialConnection)inConnection;
cawthron
parents:
diff changeset
   466
				settings[0] = s.getComPort();
cawthron
parents:
diff changeset
   467
				settings[1] = s.getDecodeFormat().toLowerCase();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   468
			} else if (type.compareToIgnoreCase("serial") == 0) {
2
cawthron
parents:
diff changeset
   469
				settings = new String[7];
cawthron
parents:
diff changeset
   470
				ITCRealSerialConnection s = (ITCRealSerialConnection)inConnection;
cawthron
parents:
diff changeset
   471
				settings[0] = s.getComPort();
cawthron
parents:
diff changeset
   472
				settings[1] = s.getBaudRate();
cawthron
parents:
diff changeset
   473
				settings[2] = s.getDataBits();
cawthron
parents:
diff changeset
   474
				settings[3] = s.getParity();
cawthron
parents:
diff changeset
   475
				settings[4] = s.getStopBits();
cawthron
parents:
diff changeset
   476
				settings[5] = s.getFlowControl();
cawthron
parents:
diff changeset
   477
				settings[6] = s.getDecodeFormat().toLowerCase();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   478
			} else if (type.compareToIgnoreCase("usb") == 0) {
2
cawthron
parents:
diff changeset
   479
				settings = new String[1];
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   480
			} else {
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   481
				// Add other connections here
2
cawthron
parents:
diff changeset
   482
			}
cawthron
parents:
diff changeset
   483
		}
cawthron
parents:
diff changeset
   484
		return finishConnect(type, settings, inConnection, inMessageOptions, inMessageIds);
cawthron
parents:
diff changeset
   485
	}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   486
	protected void ensureWritableFile(String filePath) throws IOException {
2
cawthron
parents:
diff changeset
   487
		// ensure file path points to a writable regular file
cawthron
parents:
diff changeset
   488
		IPath path = new Path(filePath);
cawthron
parents:
diff changeset
   489
		File file = path.toFile();
cawthron
parents:
diff changeset
   490
		if (!file.exists()) {
cawthron
parents:
diff changeset
   491
			file.createNewFile();
cawthron
parents:
diff changeset
   492
		}
cawthron
parents:
diff changeset
   493
		else { // file exists
cawthron
parents:
diff changeset
   494
			if (file.isDirectory()) {
cawthron
parents:
diff changeset
   495
				throw new IOException(MessageFormat.format("Path is a directory: {0}", filePath));
cawthron
parents:
diff changeset
   496
			}
cawthron
parents:
diff changeset
   497
			else if (!file.canWrite()) {
cawthron
parents:
diff changeset
   498
				throw new IOException(MessageFormat.format("File exists and is not writable: {0}", filePath));
cawthron
parents:
diff changeset
   499
			}
cawthron
parents:
diff changeset
   500
		}
cawthron
parents:
diff changeset
   501
	}
cawthron
parents:
diff changeset
   502
	
cawthron
parents:
diff changeset
   503
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   504
	 * @see com.nokia.tcf.api.ITCAPIConnection#connect(com.nokia.tcf.api.ITCMessageOptions, com.nokia.tcf.api.ITCMessageIds)
cawthron
parents:
diff changeset
   505
	 */
cawthron
parents:
diff changeset
   506
	public IStatus connect(ITCMessageOptions inMessageOptions, ITCMessageIds inMessageIds) {
cawthron
parents:
diff changeset
   507
		// not currently implemented or tested
cawthron
parents:
diff changeset
   508
		long ret = TCErrorConstants.TCAPI_ERR_FEATURE_NOT_IMPLEMENTED;
cawthron
parents:
diff changeset
   509
		IStatus status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   510
		return status;
cawthron
parents:
diff changeset
   511
	}
cawthron
parents:
diff changeset
   512
cawthron
parents:
diff changeset
   513
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   514
	 * @see com.nokia.tcf.api.ITCAPIConnection#disconnect()
cawthron
parents:
diff changeset
   515
	 */
cawthron
parents:
diff changeset
   516
	public IStatus disconnect() {
cawthron
parents:
diff changeset
   517
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   518
		
cawthron
parents:
diff changeset
   519
		IStatus connectStatus = checkConnected();
cawthron
parents:
diff changeset
   520
		// ignore error
cawthron
parents:
diff changeset
   521
		if (connectStatus.isOK()) {
cawthron
parents:
diff changeset
   522
			if (this.cookie.isMessageProcessing()) {
cawthron
parents:
diff changeset
   523
				long ret = nativeStop(this.cookie.getClientId());
cawthron
parents:
diff changeset
   524
				this.cookie.setMessageProcessing(false);
cawthron
parents:
diff changeset
   525
				if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   526
					status = new Status(IStatus.ERROR,Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   527
				}
cawthron
parents:
diff changeset
   528
			}
cawthron
parents:
diff changeset
   529
			if (this.monitorThread != null) {
cawthron
parents:
diff changeset
   530
				this.stopTCFMonitorThread = true;
cawthron
parents:
diff changeset
   531
				this.monitorThread.stop = true;
cawthron
parents:
diff changeset
   532
				try {
cawthron
parents:
diff changeset
   533
					this.monitorThread.join();
cawthron
parents:
diff changeset
   534
				} catch (InterruptedException e) {
cawthron
parents:
diff changeset
   535
				}
cawthron
parents:
diff changeset
   536
				this.monitorThread = null;
cawthron
parents:
diff changeset
   537
			}
cawthron
parents:
diff changeset
   538
			if (this.inputStream != null && this.inputStream.isOpen()) {
cawthron
parents:
diff changeset
   539
				try {
cawthron
parents:
diff changeset
   540
					this.inputStream.close();
cawthron
parents:
diff changeset
   541
				} catch (IOException e) {
cawthron
parents:
diff changeset
   542
				}
cawthron
parents:
diff changeset
   543
				this.inputStream = null;
cawthron
parents:
diff changeset
   544
			}
cawthron
parents:
diff changeset
   545
			if (this.cookie.isConnected()) {
cawthron
parents:
diff changeset
   546
				long ret = nativeDisconnect(this.cookie.getClientId());
cawthron
parents:
diff changeset
   547
				if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   548
					this.cookie.setConnected(false);
cawthron
parents:
diff changeset
   549
					this.cookie.setClientId(-1);
cawthron
parents:
diff changeset
   550
				} else {
cawthron
parents:
diff changeset
   551
					// error from nativeDisconnect
cawthron
parents:
diff changeset
   552
					status = new Status(Status.ERROR,Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   553
				}
cawthron
parents:
diff changeset
   554
			}
cawthron
parents:
diff changeset
   555
		}
cawthron
parents:
diff changeset
   556
		return status;
cawthron
parents:
diff changeset
   557
	}
cawthron
parents:
diff changeset
   558
cawthron
parents:
diff changeset
   559
	// private methods
cawthron
parents:
diff changeset
   560
	/**
cawthron
parents:
diff changeset
   561
	 * This is called from TCFMonitorThread to send errors to registered listeners
cawthron
parents:
diff changeset
   562
	 */
cawthron
parents:
diff changeset
   563
	public void fireErrorListeners(long errorCode, String errorString) {
cawthron
parents:
diff changeset
   564
		int s = this.errorListenerList.size();
cawthron
parents:
diff changeset
   565
//		System.out.printf("fireErrorListeners s = %d\n", s);
cawthron
parents:
diff changeset
   566
		for (int i = 0; i < s; i++) {
cawthron
parents:
diff changeset
   567
			ITCErrorListener e = (ITCErrorListener)this.errorListenerList.get(i);
cawthron
parents:
diff changeset
   568
			if (e != null) {
cawthron
parents:
diff changeset
   569
				int errorStatus = Status.ERROR;
cawthron
parents:
diff changeset
   570
				if (errorCode == TCErrorConstants.TCAPI_INFO_COMM_RECONNECTED)
cawthron
parents:
diff changeset
   571
					errorStatus = Status.INFO;
cawthron
parents:
diff changeset
   572
				IStatus status = new Status(errorStatus, Activator.PLUGIN_ID, (int)errorCode, errorString, null);
cawthron
parents:
diff changeset
   573
				e.errorOccurred(status);
cawthron
parents:
diff changeset
   574
			}
cawthron
parents:
diff changeset
   575
		}
cawthron
parents:
diff changeset
   576
	}
cawthron
parents:
diff changeset
   577
cawthron
parents:
diff changeset
   578
	public long getClientId() {
cawthron
parents:
diff changeset
   579
		return this.cookie.getClientId();
cawthron
parents:
diff changeset
   580
	}
cawthron
parents:
diff changeset
   581
cawthron
parents:
diff changeset
   582
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   583
	 * @see com.nokia.tcf.api.ITCAPIConnection#getConnections()
cawthron
parents:
diff changeset
   584
	 */
cawthron
parents:
diff changeset
   585
	public ITCConnection[] getConnections() {
cawthron
parents:
diff changeset
   586
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   587
		ITCConnection[] connections = null;
cawthron
parents:
diff changeset
   588
		long numberConnections = 0;
cawthron
parents:
diff changeset
   589
		long[] number = new long[1];
cawthron
parents:
diff changeset
   590
		long ret = nativeGetNumberConnections(number);
cawthron
parents:
diff changeset
   591
		if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   592
			numberConnections = number[0];
cawthron
parents:
diff changeset
   593
		} else {
cawthron
parents:
diff changeset
   594
			// error processing from nativeGetNumberConnections
cawthron
parents:
diff changeset
   595
			status = new Status(Status.ERROR,Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   596
		}
cawthron
parents:
diff changeset
   597
		if (numberConnections > 0)
cawthron
parents:
diff changeset
   598
		{
cawthron
parents:
diff changeset
   599
			connections = new ITCConnection[(int)numberConnections];
cawthron
parents:
diff changeset
   600
			for (int inIndex = 0; inIndex < numberConnections; inIndex++)
cawthron
parents:
diff changeset
   601
			{
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   602
				String[] type = new String[1];
2
cawthron
parents:
diff changeset
   603
				ret = nativeGetTypeOfConnection(inIndex, type);
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   604
				if (type[0].compareToIgnoreCase("virtualserial") == 0) {
2
cawthron
parents:
diff changeset
   605
					ITCVirtualSerialConnection outConnection = (ITCVirtualSerialConnection)TCFClassFactory.createITCVirtualSerialConnection(null);
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   606
					String[] outType = new String[1];
2
cawthron
parents:
diff changeset
   607
					long[] outOptions = new long[3];
cawthron
parents:
diff changeset
   608
					String[] outSettings = new String[2];
cawthron
parents:
diff changeset
   609
					ret = nativeGetConnectionSettings(inIndex, outType, outOptions, outSettings);
cawthron
parents:
diff changeset
   610
					if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   611
						outConnection.setConnectionType(outType[0]);
cawthron
parents:
diff changeset
   612
						outConnection.setRetryInterval(outOptions[0]);
cawthron
parents:
diff changeset
   613
						outConnection.setRetryTimeout(outOptions[1]);
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   614
						outConnection.setComPort(outSettings[0]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   615
						outConnection.setDecodeFormat(outSettings[1]);
2
cawthron
parents:
diff changeset
   616
						
cawthron
parents:
diff changeset
   617
						connections[inIndex] = outConnection;
cawthron
parents:
diff changeset
   618
					} else {
cawthron
parents:
diff changeset
   619
						// error processing from nativeGetConnectionSettings
cawthron
parents:
diff changeset
   620
						status = new Status(Status.ERROR,Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   621
					}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   622
				} else if (type[0].compareToIgnoreCase("serial") == 0) {
2
cawthron
parents:
diff changeset
   623
					ITCRealSerialConnection outConnection = (ITCRealSerialConnection)TCFClassFactory.createITCRealSerialConnection(null);
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   624
					String[] outType = new String[1];
2
cawthron
parents:
diff changeset
   625
					long[] outOptions = new long[6];
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   626
					String[] outSettings = new String[7];
2
cawthron
parents:
diff changeset
   627
					ret = nativeGetConnectionSettings(inIndex, outType, outOptions, outSettings);
cawthron
parents:
diff changeset
   628
					if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   629
						outConnection.setConnectionType(outType[0]);
cawthron
parents:
diff changeset
   630
						outConnection.setRetryInterval(outOptions[0]);
cawthron
parents:
diff changeset
   631
						outConnection.setRetryTimeout(outOptions[1]);
cawthron
parents:
diff changeset
   632
						outConnection.setComPort(outSettings[0]);
cawthron
parents:
diff changeset
   633
						outConnection.setBaudRate(outSettings[1]);
cawthron
parents:
diff changeset
   634
						outConnection.setDataBits(outSettings[2]);
cawthron
parents:
diff changeset
   635
						outConnection.setParity(outSettings[3]);
cawthron
parents:
diff changeset
   636
						outConnection.setStopBits(outSettings[4]);
cawthron
parents:
diff changeset
   637
						outConnection.setFlowControl(outSettings[5]);
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   638
						outConnection.setDecodeFormat(outSettings[6]);
2
cawthron
parents:
diff changeset
   639
						
cawthron
parents:
diff changeset
   640
						connections[inIndex] = outConnection;
cawthron
parents:
diff changeset
   641
					} else {
cawthron
parents:
diff changeset
   642
						// error processing from nativeGetConnectionSettings
cawthron
parents:
diff changeset
   643
						status = new Status(Status.ERROR,Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   644
					}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   645
				} else if (type[0].compareToIgnoreCase("tcp") == 0) {
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   646
					ITCRealTCPConnection outConnection = (ITCRealTCPConnection)TCFClassFactory.createITCRealTCPConnection(null, null);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   647
					String[] outType = new String[1];
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   648
					long[] outOptions = new long[3];
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   649
					String[] outSettings = new String[3];
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   650
					ret = nativeGetConnectionSettings(inIndex, outType, outOptions, outSettings);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   651
					if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   652
						outConnection.setConnectionType(outType[0]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   653
						outConnection.setRetryInterval(outOptions[0]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   654
						outConnection.setRetryTimeout(outOptions[1]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   655
						outConnection.setIpAddress(outSettings[0]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   656
						outConnection.setPort(outSettings[1]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   657
						outConnection.setDecodeFormat(outSettings[2]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   658
						
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   659
						connections[inIndex] = outConnection;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   660
					} else {
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   661
						// error processing from nativeGetConnectionSettings
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   662
						status = new Status(Status.ERROR,Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   663
					}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   664
				} else if (type[0].compareToIgnoreCase("usb") == 0) {
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   665
					// Finish this sometime when real USB is used on PC
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   666
				} else {
2
cawthron
parents:
diff changeset
   667
					// Add other connection types here
cawthron
parents:
diff changeset
   668
				}
cawthron
parents:
diff changeset
   669
			}
cawthron
parents:
diff changeset
   670
		}
cawthron
parents:
diff changeset
   671
		return connections;
cawthron
parents:
diff changeset
   672
	}
cawthron
parents:
diff changeset
   673
cawthron
parents:
diff changeset
   674
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   675
	 * @see com.nokia.tcf.api.ITCAPIConnection#getInputStream()
cawthron
parents:
diff changeset
   676
	 */
cawthron
parents:
diff changeset
   677
	public ITCMessageInputStream getInputStream() {
cawthron
parents:
diff changeset
   678
		return this.inputStream;
cawthron
parents:
diff changeset
   679
	}
cawthron
parents:
diff changeset
   680
cawthron
parents:
diff changeset
   681
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   682
	 * @see com.nokia.tcf.api.ITCAPIConnection#getVersions()
cawthron
parents:
diff changeset
   683
	 */
cawthron
parents:
diff changeset
   684
	public ITCVersion[] getVersions() {
cawthron
parents:
diff changeset
   685
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   686
		ITCVersion[] versions = null;
cawthron
parents:
diff changeset
   687
		status = checkConnected();
cawthron
parents:
diff changeset
   688
		if (status.isOK()) {
cawthron
parents:
diff changeset
   689
			// we're connected call native
cawthron
parents:
diff changeset
   690
			long numberVersions = nativeGetNumberVersionEntities(this.cookie.getClientId());
cawthron
parents:
diff changeset
   691
			if (numberVersions > 0) {
cawthron
parents:
diff changeset
   692
				versions = new TCVersion[(int)numberVersions];
cawthron
parents:
diff changeset
   693
				String[] versStrings = new String[(int)numberVersions];
cawthron
parents:
diff changeset
   694
				long numberGotten = nativeGetVersion(this.cookie.getClientId(), numberVersions, versStrings);
cawthron
parents:
diff changeset
   695
				for (int i = 0; i < numberGotten; i++) {
cawthron
parents:
diff changeset
   696
					versions[i] = new TCVersion(versStrings[i]);
cawthron
parents:
diff changeset
   697
				}
cawthron
parents:
diff changeset
   698
			}
cawthron
parents:
diff changeset
   699
		}
cawthron
parents:
diff changeset
   700
		return versions;
cawthron
parents:
diff changeset
   701
	}
cawthron
parents:
diff changeset
   702
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   703
	 * @see com.nokia.tcf.api.ITCAPIConnection#removeErrorListener(com.nokia.tcf.api.ITCErrorListener)
cawthron
parents:
diff changeset
   704
	 */
cawthron
parents:
diff changeset
   705
	public IStatus removeErrorListener(ITCErrorListener inErrorListener) {
cawthron
parents:
diff changeset
   706
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   707
//		System.out.printf("removeErrorListener\n");
cawthron
parents:
diff changeset
   708
		this.errorListenerList.remove(inErrorListener);
cawthron
parents:
diff changeset
   709
		return status;
cawthron
parents:
diff changeset
   710
	}
cawthron
parents:
diff changeset
   711
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   712
	 * @see com.nokia.tcf.api.ITCAPIConnection#sendMessage(com.nokia.tcf.api.ITCMessage)
cawthron
parents:
diff changeset
   713
	 */
cawthron
parents:
diff changeset
   714
	public IStatus sendMessage(ITCMessage inMessage) {
cawthron
parents:
diff changeset
   715
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   716
		status = checkConnected();
cawthron
parents:
diff changeset
   717
		if (status.isOK()) {
cawthron
parents:
diff changeset
   718
			// we are connected are we processing?
cawthron
parents:
diff changeset
   719
			if (this.cookie.isMessageProcessing()) {
cawthron
parents:
diff changeset
   720
				// we are running, check message
cawthron
parents:
diff changeset
   721
				status = checkMessage(inMessage);
cawthron
parents:
diff changeset
   722
				if (status.getSeverity() != Status.ERROR) {
cawthron
parents:
diff changeset
   723
					// Status.OK or Status.WARNING (still attempt to send on warning)
cawthron
parents:
diff changeset
   724
					// Encode if option is set
cawthron
parents:
diff changeset
   725
cawthron
parents:
diff changeset
   726
					long[] formattingOptions = new long[5];
cawthron
parents:
diff changeset
   727
					formattingOptions[0] = 0;
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   728
					formattingOptions[1] = messageOptions.getMessageEncodeFormat(); // add protocol or not
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   729
					formattingOptions[2] = messageOptions.getOSTVersion();			// OST version byte to use if OST
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   730
					formattingOptions[3] = inMessage.getMyMessageId();				// message ID to use of adding protocol
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   731
					formattingOptions[4] = (inMessage.isUseMyMessageId() == true) ? 1 : 0; // use my ID or not
2
cawthron
parents:
diff changeset
   732
					String[] settings = new String[1];
cawthron
parents:
diff changeset
   733
					settings[0] = connection.getDecodeFormat().toLowerCase();
cawthron
parents:
diff changeset
   734
					try {
cawthron
parents:
diff changeset
   735
						long ret = TCErrorConstants.TCAPI_ERR_NONE;
cawthron
parents:
diff changeset
   736
						if (inMessage == null) { // OK if we're just sending a header
cawthron
parents:
diff changeset
   737
							ret = nativeSendMessage(this.cookie.getClientId(), formattingOptions, settings, null);
cawthron
parents:
diff changeset
   738
						} else {
cawthron
parents:
diff changeset
   739
							ret = nativeSendMessage(this.cookie.getClientId(), formattingOptions, settings, inMessage.getMessage());
cawthron
parents:
diff changeset
   740
						}
cawthron
parents:
diff changeset
   741
						if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   742
							status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   743
						}
cawthron
parents:
diff changeset
   744
					} catch (Exception e) {
cawthron
parents:
diff changeset
   745
						// exception is thrown ONLY when err = TCAPI_ERR_COMM_ERROR from native
cawthron
parents:
diff changeset
   746
						// message is OS Error
cawthron
parents:
diff changeset
   747
						String msg = e.getMessage();
cawthron
parents:
diff changeset
   748
						long ret = TCErrorConstants.TCAPI_ERR_COMM_ERROR;
cawthron
parents:
diff changeset
   749
						String tcErr = String.format("%s OSError: %s", TCErrorConstants.getErrorMessage(ret), msg);
cawthron
parents:
diff changeset
   750
						status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, tcErr, null);
cawthron
parents:
diff changeset
   751
					}
cawthron
parents:
diff changeset
   752
				}
cawthron
parents:
diff changeset
   753
			} else {
cawthron
parents:
diff changeset
   754
				// we are stopped
cawthron
parents:
diff changeset
   755
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_ROUTING_STOPPED,
cawthron
parents:
diff changeset
   756
						TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_ROUTING_STOPPED), null);
cawthron
parents:
diff changeset
   757
			}
cawthron
parents:
diff changeset
   758
		}
cawthron
parents:
diff changeset
   759
		return status;
cawthron
parents:
diff changeset
   760
	}
cawthron
parents:
diff changeset
   761
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   762
	 * @see com.nokia.tcf.api.ITCAPIConnection#setTCMessageIds(com.nokia.tcf.api.ITCMessageIds)
cawthron
parents:
diff changeset
   763
	 */
cawthron
parents:
diff changeset
   764
	public IStatus setTCMessageIds(ITCMessageIds inMessageIds) {
cawthron
parents:
diff changeset
   765
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   766
		status = checkMessageIds(inMessageIds);
cawthron
parents:
diff changeset
   767
		if (status.isOK()) {
cawthron
parents:
diff changeset
   768
			// input is OK
cawthron
parents:
diff changeset
   769
			status = checkConnected();
cawthron
parents:
diff changeset
   770
			if (status.isOK()) {
cawthron
parents:
diff changeset
   771
				// connected
cawthron
parents:
diff changeset
   772
				if (this.cookie.isMessageProcessing()) {
cawthron
parents:
diff changeset
   773
					// must be stopped to set new IDs
cawthron
parents:
diff changeset
   774
					status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)TCErrorConstants.TCAPI_ERR_ROUTING_IN_PROGRESS,
cawthron
parents:
diff changeset
   775
							TCErrorConstants.getErrorMessage(TCErrorConstants.TCAPI_ERR_ROUTING_IN_PROGRESS), null);
cawthron
parents:
diff changeset
   776
				}
cawthron
parents:
diff changeset
   777
			}
cawthron
parents:
diff changeset
   778
		}
cawthron
parents:
diff changeset
   779
		if (status.isOK()) {
cawthron
parents:
diff changeset
   780
			// everything OK, so we can save our IDs and register them
cawthron
parents:
diff changeset
   781
			this.messageIds = inMessageIds;
cawthron
parents:
diff changeset
   782
			int size = (int)inMessageIds.size();
cawthron
parents:
diff changeset
   783
			byte[] messageids = new byte[size];
cawthron
parents:
diff changeset
   784
			for (int i = 0; i < size; i++) {
cawthron
parents:
diff changeset
   785
				messageids[0] = inMessageIds.getMessageIds().get(i);
cawthron
parents:
diff changeset
   786
			}
cawthron
parents:
diff changeset
   787
			long ret = nativeSetMessageIds(this.cookie.getClientId(), messageids);
cawthron
parents:
diff changeset
   788
			if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   789
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret,
cawthron
parents:
diff changeset
   790
						TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   791
			}
cawthron
parents:
diff changeset
   792
		}
cawthron
parents:
diff changeset
   793
		return status;
cawthron
parents:
diff changeset
   794
	}
cawthron
parents:
diff changeset
   795
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   796
	 * @see com.nokia.tcf.api.ITCAPIConnection#start()
cawthron
parents:
diff changeset
   797
	 */
cawthron
parents:
diff changeset
   798
	public IStatus start() {
cawthron
parents:
diff changeset
   799
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   800
		// check connected
cawthron
parents:
diff changeset
   801
		status = checkConnected();
cawthron
parents:
diff changeset
   802
		if (status.isOK()) {
cawthron
parents:
diff changeset
   803
			if (this.cookie.isMessageProcessing() == false) {
cawthron
parents:
diff changeset
   804
				// call native
cawthron
parents:
diff changeset
   805
				long ret = nativeStart(this.cookie.getClientId());
cawthron
parents:
diff changeset
   806
				if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   807
					this.cookie.setMessageProcessing(true);
cawthron
parents:
diff changeset
   808
				} else {
cawthron
parents:
diff changeset
   809
					status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   810
				}
cawthron
parents:
diff changeset
   811
			}
cawthron
parents:
diff changeset
   812
		}
cawthron
parents:
diff changeset
   813
		
cawthron
parents:
diff changeset
   814
		return status;
cawthron
parents:
diff changeset
   815
	}
cawthron
parents:
diff changeset
   816
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   817
	 * @see com.nokia.tcf.api.ITCAPIConnection#stop()
cawthron
parents:
diff changeset
   818
	 */
cawthron
parents:
diff changeset
   819
	public IStatus stop() {
cawthron
parents:
diff changeset
   820
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   821
		status = checkConnected();
cawthron
parents:
diff changeset
   822
		if (status.isOK()) {
cawthron
parents:
diff changeset
   823
			if (this.cookie.isMessageProcessing()) {
cawthron
parents:
diff changeset
   824
				long ret = nativeStop(this.cookie.getClientId());
cawthron
parents:
diff changeset
   825
				if (ret == TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   826
					this.cookie.setMessageProcessing(false);
cawthron
parents:
diff changeset
   827
				} else {
cawthron
parents:
diff changeset
   828
					status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   829
				}
cawthron
parents:
diff changeset
   830
			}
cawthron
parents:
diff changeset
   831
		}
cawthron
parents:
diff changeset
   832
		return status;
cawthron
parents:
diff changeset
   833
	}
cawthron
parents:
diff changeset
   834
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   835
	 * @see com.nokia.tcf.api.ITCAPIConnection#testConnection()
cawthron
parents:
diff changeset
   836
	 */
cawthron
parents:
diff changeset
   837
	public IStatus testConnection() {
cawthron
parents:
diff changeset
   838
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   839
		status = checkConnected();
cawthron
parents:
diff changeset
   840
		if (status.isOK()) {
cawthron
parents:
diff changeset
   841
			long ret = nativeTestConnection(this.cookie.getClientId());
cawthron
parents:
diff changeset
   842
			if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   843
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)(ret), TCErrorConstants.getErrorMessage((int)ret), null);
cawthron
parents:
diff changeset
   844
			}
cawthron
parents:
diff changeset
   845
		}
cawthron
parents:
diff changeset
   846
		return status;
cawthron
parents:
diff changeset
   847
	}
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   848
	protected IStatus doTestConnection(String type, String[] settings, ITCConnection inConnection) {
2
cawthron
parents:
diff changeset
   849
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   850
		// test connection
cawthron
parents:
diff changeset
   851
		long[] options = new long[3];
cawthron
parents:
diff changeset
   852
		options[0] = inConnection.getRetryInterval();		// connection options
cawthron
parents:
diff changeset
   853
		options[1] = inConnection.getRetryTimeout();
cawthron
parents:
diff changeset
   854
		options[2] = 0;
cawthron
parents:
diff changeset
   855
		long ret = nativeTestConnection(type, options, settings);
cawthron
parents:
diff changeset
   856
		if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   857
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   858
		}
cawthron
parents:
diff changeset
   859
		return status;
cawthron
parents:
diff changeset
   860
	}
cawthron
parents:
diff changeset
   861
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   862
	 * @see com.nokia.tcf.api.ITCAPIConnection#testConnection(com.nokia.tcf.api.ITCConnection)
cawthron
parents:
diff changeset
   863
	 */
cawthron
parents:
diff changeset
   864
	public IStatus testConnection(ITCConnection inConnection) {
cawthron
parents:
diff changeset
   865
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   866
		String[] settings = null;
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   867
		String type = inConnection.getConnectionType();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   868
		if (type.compareToIgnoreCase("serial") == 0) {
2
cawthron
parents:
diff changeset
   869
			settings = new String[7];
cawthron
parents:
diff changeset
   870
			ITCRealSerialConnection s = (ITCRealSerialConnection)inConnection;
cawthron
parents:
diff changeset
   871
			settings[0] = s.getComPort();
cawthron
parents:
diff changeset
   872
			settings[1] = s.getBaudRate();
cawthron
parents:
diff changeset
   873
			settings[2] = s.getDataBits();
cawthron
parents:
diff changeset
   874
			settings[3] = s.getParity();
cawthron
parents:
diff changeset
   875
			settings[4] = s.getStopBits();
cawthron
parents:
diff changeset
   876
			settings[5] = s.getFlowControl();
cawthron
parents:
diff changeset
   877
			settings[6] = s.getDecodeFormat().toLowerCase();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   878
		} else if (type.compareToIgnoreCase("tcp") == 0) {
2
cawthron
parents:
diff changeset
   879
			settings = new String[3];
cawthron
parents:
diff changeset
   880
			ITCRealTCPConnection s = (ITCRealTCPConnection)inConnection;
cawthron
parents:
diff changeset
   881
			settings[0] = s.getIpAddress();
cawthron
parents:
diff changeset
   882
			settings[1] = s.getPort();
cawthron
parents:
diff changeset
   883
			settings[2] = s.getDecodeFormat().toLowerCase();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   884
		} else if (type.compareToIgnoreCase("usb") == 0) {
2
cawthron
parents:
diff changeset
   885
			settings = new String[1];
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   886
			// finish this sometime when real USB is used on PC
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   887
		} else if (type.compareToIgnoreCase("virtualserial") == 0) {
2
cawthron
parents:
diff changeset
   888
			settings = new String[2];
cawthron
parents:
diff changeset
   889
			ITCVirtualSerialConnection s = (ITCVirtualSerialConnection)inConnection;
cawthron
parents:
diff changeset
   890
			settings[0] = s.getComPort();
cawthron
parents:
diff changeset
   891
			settings[1] = s.getDecodeFormat().toLowerCase();
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   892
		} else {
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   893
			// add new connections here
2
cawthron
parents:
diff changeset
   894
			long err = TCErrorConstants.TCAPI_ERR_MEDIA_NOT_SUPPORTED;
cawthron
parents:
diff changeset
   895
			status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)err, TCErrorConstants.getErrorMessage((int)err), null);
cawthron
parents:
diff changeset
   896
		}
cawthron
parents:
diff changeset
   897
		if (status.isOK()) {
cawthron
parents:
diff changeset
   898
			status = doTestConnection(type, settings, inConnection);
cawthron
parents:
diff changeset
   899
/*			// test connection
cawthron
parents:
diff changeset
   900
			long[] options = new long[3];
cawthron
parents:
diff changeset
   901
			options[0] = inConnection.getRetryInterval();		// connection options
cawthron
parents:
diff changeset
   902
			options[1] = inConnection.getRetryTimeout();
cawthron
parents:
diff changeset
   903
			options[2] = inConnection.getDecodeFormat();
cawthron
parents:
diff changeset
   904
			long ret = nativeTestConnection(type, options, settings);
cawthron
parents:
diff changeset
   905
			if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   906
				status = new Status(Status.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   907
			} */
cawthron
parents:
diff changeset
   908
		}
cawthron
parents:
diff changeset
   909
		return status;
cawthron
parents:
diff changeset
   910
	}
cawthron
parents:
diff changeset
   911
	public IStatus clearMessageFile() {
cawthron
parents:
diff changeset
   912
		IStatus status = statusOK;
cawthron
parents:
diff changeset
   913
		IStatus connStatus = checkConnected();
cawthron
parents:
diff changeset
   914
		if (connStatus.isOK()) {
cawthron
parents:
diff changeset
   915
			if (messageOptions.getMessageDestination() == ITCMessageOptions.DESTINATION_CLIENTFILE) {
cawthron
parents:
diff changeset
   916
				if (messageOptions.getMessageFile() != null) {
cawthron
parents:
diff changeset
   917
					long ret = nativeClearFile(cookie.getClientId());
cawthron
parents:
diff changeset
   918
					if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
cawthron
parents:
diff changeset
   919
						status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, (int)ret, TCErrorConstants.getErrorMessage(ret), null);
cawthron
parents:
diff changeset
   920
					}
cawthron
parents:
diff changeset
   921
				}
cawthron
parents:
diff changeset
   922
			}
cawthron
parents:
diff changeset
   923
		}
cawthron
parents:
diff changeset
   924
		return status;
cawthron
parents:
diff changeset
   925
	}
cawthron
parents:
diff changeset
   926
cawthron
parents:
diff changeset
   927
	// natives
cawthron
parents:
diff changeset
   928
	// connect/disconnect
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   929
	protected native long nativeConnect(String inType, long[] inOptions, String[] inSettings, long[] inMessageOptions, String inFilePath, long[] outClientId);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   930
	protected native long nativeDisconnect(long inClientId); 
2
cawthron
parents:
diff changeset
   931
	// connections
cawthron
parents:
diff changeset
   932
	protected native long nativeGetNumberConnections(long[] outNumber);
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   933
	protected native long nativeGetTypeOfConnection(long inIndex, String[] outType);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   934
	protected native long nativeGetConnectionSettings(long inIndex, String[] outType, long[] outOptions, String[] outSettings);
2
cawthron
parents:
diff changeset
   935
	// port handling errors
cawthron
parents:
diff changeset
   936
	public native boolean nativePollError(long inClienId, int[] outErrorCode, boolean[] outHasOSErrorCode, long[] outOSErrorCode);
cawthron
parents:
diff changeset
   937
	// versions
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   938
	protected native long nativeGetNumberVersionEntities(long inClientId);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   939
	protected native long nativeGetVersion(long inClientId, long inNumToGet, String[] outVersion);
2
cawthron
parents:
diff changeset
   940
	// input stream
cawthron
parents:
diff changeset
   941
	public native long nativePollInputStream(long inClientId, long[] outNumberTotalMessages);
cawthron
parents:
diff changeset
   942
	public native long nativePollInputStream2(long inClientId, long inNumberMessagesToPeek, long[] outNumberMessagesPeeked, long[] outNumberBytesPeeked);
cawthron
parents:
diff changeset
   943
	public native long nativeGetInputStreamMessageBytes(long inClientId, long inNumberMessagesToPeek, long[] outNumberBytesPerMessage);
cawthron
parents:
diff changeset
   944
	public native long nativeReadInputStream(long inClientId, long inNumberMessagesToRead, long[] outNumberMessagesRead, long[] outNumberBytesRead, long inNumberMaxBytes, byte[] outMessages);
cawthron
parents:
diff changeset
   945
	public native long nativeOpenInputStream(long inClientId, String inBaseFilePath, long inInputStreamSize, boolean inOverFlowToFile);
cawthron
parents:
diff changeset
   946
	public native long nativeCloseInputStream(long inClientId);
cawthron
parents:
diff changeset
   947
	// message file
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   948
	protected native long nativeClearFile(long inClientId);
2
cawthron
parents:
diff changeset
   949
	// send message
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   950
	protected native long nativeSendMessage(long inClientId, long[] inFormattingOptions, String[] inSettings, byte[] inMessage);
2
cawthron
parents:
diff changeset
   951
	// register message IDs
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   952
	protected native long nativeSetMessageIds(long inClientId, byte[] inMessageIds);
2
cawthron
parents:
diff changeset
   953
	// start/stop processing
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   954
	protected native long nativeStart(long inClientId);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   955
	protected native long nativeStop(long inClientId);
2
cawthron
parents:
diff changeset
   956
	// test connections
60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   957
	protected native long nativeTestConnection(String inType, long[] inOptions, String[] inSettings);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents: 2
diff changeset
   958
	protected native long nativeTestConnection(long inClientId);
2
cawthron
parents:
diff changeset
   959
	// start/stop server - done from plugin activator start/stop methods
cawthron
parents:
diff changeset
   960
	public native long nativeStartServer();
cawthron
parents:
diff changeset
   961
	public native long nativeStopServer();
cawthron
parents:
diff changeset
   962
}