trace/traceviewer/com.nokia.traceviewer.ost/src/com/nokia/traceviewer/ost/OstMessageSender.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 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  * OST message sender class
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.ost;
       
    20 
       
    21 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    22 
       
    23 /**
       
    24  * OST message sender class
       
    25  * 
       
    26  */
       
    27 public class OstMessageSender implements OstConsts {
       
    28 
       
    29 	/**
       
    30 	 * Sends message to the device
       
    31 	 * 
       
    32 	 * @param message
       
    33 	 *            the message
       
    34 	 * @param addHeader
       
    35 	 *            if true, adds header to the message before sending
       
    36 	 * @param messageId
       
    37 	 *            message ID to be used in message
       
    38 	 */
       
    39 	public void sendMessage(byte[] message, boolean addHeader, byte messageId) {
       
    40 		byte[] ostMessage;
       
    41 		if (addHeader) {
       
    42 			// Add TraceCore protocol subscriber header
       
    43 			ostMessage = addTraceCoreProtocolHeader(message, messageId);
       
    44 			messageId = OST_TRACECORE_PROTOCOL_ID;
       
    45 
       
    46 			// Wrap the message to OST message
       
    47 			ostMessage = wrapInOstMessage(ostMessage, messageId);
       
    48 		} else {
       
    49 			ostMessage = message;
       
    50 		}
       
    51 
       
    52 		// Write message to connection
       
    53 		if (TraceViewerGlobals.getTraceViewer().getConnection() != null) {
       
    54 			TraceViewerGlobals.getTraceViewer().getConnection().write(
       
    55 					ostMessage);
       
    56 		}
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * Sends activation message to the device
       
    61 	 * 
       
    62 	 * @param activate
       
    63 	 *            if true, activate, otherwise deactivate
       
    64 	 * @param componentId
       
    65 	 *            component ID
       
    66 	 * @param groupIds
       
    67 	 *            array of group IDs
       
    68 	 */
       
    69 	public void sendActivationMessage(boolean activate, int componentId,
       
    70 			int[] groupIds) {
       
    71 
       
    72 		// Don't send anything if activating zero groups
       
    73 		if (groupIds.length > 0) {
       
    74 
       
    75 			// Create activation message
       
    76 			byte[] activationMessage = createActivationMessage(activate,
       
    77 					componentId, groupIds);
       
    78 
       
    79 			// Wrap the message to OST message
       
    80 			byte[] ostMessage = wrapInOstMessage(activationMessage,
       
    81 					OST_TRACE_ACTIVATION_ID);
       
    82 
       
    83 			// Write message to connection
       
    84 			if (TraceViewerGlobals.getTraceViewer().getConnection() != null) {
       
    85 				TraceViewerGlobals.getTraceViewer().getConnection().write(
       
    86 						ostMessage);
       
    87 			}
       
    88 		}
       
    89 	}
       
    90 
       
    91 	/**
       
    92 	 * Creates activation message
       
    93 	 * 
       
    94 	 * @param activate
       
    95 	 *            if true, message is activation message. If false, message is
       
    96 	 *            deactivation message.
       
    97 	 * @param componentId
       
    98 	 *            component ID
       
    99 	 * @param groupIds
       
   100 	 *            array of group ID's
       
   101 	 * @return the activation message
       
   102 	 */
       
   103 	private byte[] createActivationMessage(boolean activate, int componentId,
       
   104 			int[] groupIds) {
       
   105 		int j = 0;
       
   106 		byte[] message = null;
       
   107 
       
   108 		// Calculate length of the message. The last "2" is from activation
       
   109 		// status + filler
       
   110 		int length = OST_TRACE_ACTIVATION_HEADER_LENGTH + COMPONENT_ID_LENGTH
       
   111 				+ GROUP_ID_LENGTH * groupIds.length + 2;
       
   112 
       
   113 		message = new byte[length];
       
   114 
       
   115 		// Transaction ID
       
   116 		message[OST_TRACE_ACTIVATION_TRANSID_OFFSET] = 0x01;
       
   117 
       
   118 		// Message ID is set application status request
       
   119 		message[OST_TRACE_ACTIVATION_MESSAGEID_OFFSET] = OST_TRACE_ACTIVATION_SET_STATUS_REQUEST;
       
   120 
       
   121 		// Set next four bytes to be component id
       
   122 		byte[] componentIdBytes = getIntAsFourBytesBigEndian(componentId);
       
   123 		for (int k = 0; k < componentIdBytes.length; k++) {
       
   124 			message[OST_TRACE_ACTIVATION_COMPONENTID_OFFSET + k] = componentIdBytes[k];
       
   125 		}
       
   126 
       
   127 		// Activate or deactivate
       
   128 		if (activate) {
       
   129 			message[OST_TRACE_ACTIVATION_STATUS_OFFSET] = OST_TRACE_ACTIVATION_ACTIVATE_INDICATOR;
       
   130 		} else {
       
   131 			message[OST_TRACE_ACTIVATION_STATUS_OFFSET] = OST_TRACE_ACTIVATION_DEACTIVATE_INDICATOR;
       
   132 		}
       
   133 
       
   134 		message[OST_TRACE_ACTIVATION_FILLER_OFFSET] = 0x00;
       
   135 
       
   136 		// Iterate through groups
       
   137 		for (j = 0; j < groupIds.length; j++) {
       
   138 
       
   139 			// Set next 2 bytes to be group id
       
   140 			byte[] groupIdBytes = getIntAsTwoBytesBigEndian(groupIds[j]);
       
   141 			for (int k = 0; k < groupIdBytes.length; k++) {
       
   142 				int offset = OST_TRACE_ACTIVATION_GROUPID_START_OFFSET + k
       
   143 						+ (j * 2);
       
   144 				message[offset] = groupIdBytes[k];
       
   145 			}
       
   146 		}
       
   147 
       
   148 		return message;
       
   149 	}
       
   150 
       
   151 	/**
       
   152 	 * Adds TraceCore protocol header to the message
       
   153 	 * 
       
   154 	 * @param message
       
   155 	 *            message
       
   156 	 * @param messageId
       
   157 	 *            message (subscriber) ID to be used
       
   158 	 * @return message with TraceCore protocol header added
       
   159 	 */
       
   160 	private byte[] addTraceCoreProtocolHeader(byte[] message, byte messageId) {
       
   161 		byte[] ostMessage = new byte[message.length
       
   162 				+ OST_TRACECORE_PROTOCOL_HEADER_LENGTH];
       
   163 
       
   164 		// TraceCore protocol ID to be Subscriber ID
       
   165 		ostMessage[OST_TRACECORE_PROTOCOL_ID_OFFSET] = OST_TRACECORE_SUBSCRIBER_PROTOCOL_ID;
       
   166 
       
   167 		// Add original message ID as a subscriber ID
       
   168 		ostMessage[OST_TRACECORE_SUBSCRIBER_ID_OFFSET] = messageId;
       
   169 
       
   170 		// Two next bytes are reserved for future use
       
   171 		ostMessage[OST_TRACECORE_SUBSCRIBER_ID_OFFSET + 1] = 0x00;
       
   172 		ostMessage[OST_TRACECORE_SUBSCRIBER_ID_OFFSET + 2] = 0x00;
       
   173 
       
   174 		// Copy the rest of the bytes to the new array
       
   175 		System.arraycopy(message, 0, ostMessage,
       
   176 				OST_TRACECORE_PROTOCOL_HEADER_LENGTH, message.length);
       
   177 
       
   178 		return ostMessage;
       
   179 	}
       
   180 
       
   181 	/**
       
   182 	 * Wraps given byte array to OST message. Header to be used is OST header
       
   183 	 * version 0.1
       
   184 	 * 
       
   185 	 * @param message
       
   186 	 *            message to wrap
       
   187 	 * @param protocolId
       
   188 	 *            protocol ID to be used in message
       
   189 	 * @return message wrapped in OST message
       
   190 	 */
       
   191 	public byte[] wrapInOstMessage(byte[] message, byte protocolId) {
       
   192 		byte[] ostMessage = new byte[message.length + OST_V01_HEADER_LENGTH];
       
   193 
       
   194 		// Version
       
   195 		ostMessage[OST_VERSION_OFFSET] = 0x01;
       
   196 
       
   197 		// Protocol ID
       
   198 		ostMessage[OST_V01_PROTOCOLID_OFFSET] = protocolId;
       
   199 
       
   200 		// Length bytes
       
   201 		byte[] length = getIntAsTwoBytesBigEndian(message.length);
       
   202 		ostMessage[OST_V01_LENGTH_OFFSET_1] = length[0];
       
   203 		ostMessage[OST_V01_LENGTH_OFFSET_2] = length[1];
       
   204 
       
   205 		// Copy the rest of the bytes to the new array
       
   206 		System.arraycopy(message, 0, ostMessage, OST_V01_HEADER_LENGTH,
       
   207 				message.length);
       
   208 
       
   209 		return ostMessage;
       
   210 	}
       
   211 
       
   212 	/**
       
   213 	 * Gets int as two bytes in big endian
       
   214 	 * 
       
   215 	 * @param integer
       
   216 	 *            integer to get as bytes
       
   217 	 * @return byte array[2] containing given integer in big endian
       
   218 	 */
       
   219 	private byte[] getIntAsTwoBytesBigEndian(int integer) {
       
   220 		byte[] byteArr = new byte[2];
       
   221 		byteArr[0] = (byte) ((integer >> 8) & 0xFF);
       
   222 		byteArr[1] = (byte) (integer & 0xFF);
       
   223 		return byteArr;
       
   224 	}
       
   225 
       
   226 	/**
       
   227 	 * Gets int as four bytes in big endian
       
   228 	 * 
       
   229 	 * @param integer
       
   230 	 *            int
       
   231 	 * @return four byte array in big endian
       
   232 	 */
       
   233 	private byte[] getIntAsFourBytesBigEndian(int integer) {
       
   234 		byte[] byteArr = new byte[4];
       
   235 		byteArr[0] = (byte) (integer >> 24);
       
   236 		byteArr[1] = (byte) ((integer << 8) >> 24);
       
   237 		byteArr[2] = (byte) ((integer << 16) >> 24);
       
   238 		byteArr[3] = (byte) ((integer << 24) >> 24);
       
   239 		return byteArr;
       
   240 	}
       
   241 }