diff options
author | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2020-08-11 11:31:56 -0400 |
---|---|---|
committer | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2020-08-11 11:31:56 -0400 |
commit | e0672237a8b054ac0dd5bb4482199991bb133ac1 (patch) | |
tree | 03d08b7eb95552e7a560924e3bc0738d506de489 | |
parent | 9af716055b16c1de3a4e274a8590166d9d642555 (diff) | |
download | sdl_android-e0672237a8b054ac0dd5bb4482199991bb133ac1.tar.gz |
Handle files that exist in both base & android
-rw-r--r-- | android/sdl_android/src/main/java/com/smartdevicelink/trace/SdlTrace.java | 406 | ||||
-rw-r--r-- | base/src/main/java/com/smartdevicelink/streaming/StreamPacketizer.java | 300 | ||||
-rw-r--r-- | base/src/main/java/com/smartdevicelink/trace/SdlTrace.java | 406 | ||||
-rw-r--r-- | base/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java | 444 | ||||
l--------- | baseAndroid/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java | 1 | ||||
-rw-r--r-- | javaSE/src/main/java/com/smartdevicelink/protocol/SdlProtocol.java (renamed from base/src/main/java/com/smartdevicelink/protocol/SdlProtocol.java) | 0 | ||||
-rw-r--r-- | javaSE/src/main/java/com/smartdevicelink/transport/TransportManager.java (renamed from base/src/main/java/com/smartdevicelink/transport/TransportManager.java) | 0 |
7 files changed, 452 insertions, 1105 deletions
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/trace/SdlTrace.java b/android/sdl_android/src/main/java/com/smartdevicelink/trace/SdlTrace.java index e3bedd6a2..956985128 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/trace/SdlTrace.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/trace/SdlTrace.java @@ -35,22 +35,6 @@ import android.annotation.SuppressLint; import android.bluetooth.BluetoothDevice;
import android.os.Build;
import android.os.Debug;
-import android.os.Process;
-
-import com.smartdevicelink.protocol.SdlPacket;
-import com.smartdevicelink.protocol.enums.FrameDataControlFrameType;
-import com.smartdevicelink.protocol.enums.FrameType;
-import com.smartdevicelink.protocol.enums.SessionType;
-import com.smartdevicelink.proxy.RPCMessage;
-import com.smartdevicelink.proxy.RPCRequest;
-import com.smartdevicelink.proxy.RPCResponse;
-import com.smartdevicelink.trace.enums.DetailLevel;
-import com.smartdevicelink.trace.enums.InterfaceActivityDirection;
-import com.smartdevicelink.trace.enums.Mod;
-import com.smartdevicelink.transport.SiphonServer;
-import com.smartdevicelink.util.BitConverter;
-import com.smartdevicelink.util.DebugTool;
-import com.smartdevicelink.util.NativeLogTool;
import java.sql.Timestamp;
@@ -63,291 +47,12 @@ import java.sql.Timestamp; */
@SuppressLint("DefaultLocale")
-public class SdlTrace {
- private static final String SDL_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66";
-
- public static final String SYSTEM_LOG_TAG = "SdlTrace";
-
- private static long baseTics = java.lang.System.currentTimeMillis();
- private static boolean acceptAPITraceAdjustments = true;
-
- protected static ISTListener m_appTraceListener = null;
-
- ///
- /// The PUBLIC interface to SdlTrace starts here
- ///
-
-
- public static void setAcceptAPITraceAdjustments(Boolean APITraceAdjustmentsAccepted) {
- if (APITraceAdjustmentsAccepted != null) {
- acceptAPITraceAdjustments = APITraceAdjustmentsAccepted;
- }
- }
-
- public static boolean getAcceptAPITraceAdjustments() {
- return acceptAPITraceAdjustments;
- }
-
- public static void setAppTraceListener(ISTListener listener) {
- m_appTraceListener = listener;
- } // end-method
-
- public static void setAppTraceLevel(DetailLevel dt) {
- if ( dt != null && acceptAPITraceAdjustments)
- DiagLevel.setLevel(Mod.app, dt);
- } // end-method
-
- public static void setProxyTraceLevel(DetailLevel dt) {
- if (dt != null && acceptAPITraceAdjustments)
- DiagLevel.setLevel(Mod.proxy, dt);
- } // end-method
-
- public static void setRpcTraceLevel(DetailLevel dt) {
- if (dt != null && acceptAPITraceAdjustments)
- DiagLevel.setLevel(Mod.rpc, dt);
- } // end-method
-
- public static void setMarshallingTraceLevel(DetailLevel dt) {
- if (dt != null && acceptAPITraceAdjustments)
- DiagLevel.setLevel(Mod.mar, dt);
- } // end-method
-
- public static void setProtocolTraceLevel(DetailLevel dt) {
- if (dt != null && acceptAPITraceAdjustments)
- DiagLevel.setLevel(Mod.proto, dt);
- } // end-method
-
- public static void setTransportTraceLevel(DetailLevel dt) {
- if (dt != null && acceptAPITraceAdjustments)
- DiagLevel.setLevel(Mod.tran, dt);
- } // end-method
-
- private static String encodeTraceMessage(long timestamp, Mod module, InterfaceActivityDirection msgDirection, String msgBodyXml) {
- StringBuilder sb = new StringBuilder("<msg><dms>");
- sb.append(timestamp);
- sb.append("</dms><pid>");
- sb.append(Process.myPid());
- sb.append("</pid><tid>");
- sb.append(Thread.currentThread().getId());
- sb.append("</tid><mod>");
- sb.append(module.toString());
- sb.append("</mod>");
- if (msgDirection != InterfaceActivityDirection.None) {
- sb.append("<dir>");
- sb.append(interfaceActivityDirectionToString(msgDirection));
- sb.append("</dir>");
- } // end-if
- sb.append(msgBodyXml);
- sb.append("</msg>");
-
- return sb.toString();
- } // end-method
-
- private static String interfaceActivityDirectionToString(InterfaceActivityDirection iaDirection) {
- String str = "";
- switch (iaDirection) {
- case Receive:
- str = "rx";
- break;
- case Transmit:
- str = "tx";
- break;
- default:
- break;
- } // end-switch
- return str;
- } // end-method
-
- static String B64EncodeForXML(String data) {
- return Mime.base64Encode(data);
- // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: return Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
- } // end-method
-
- public static boolean logProxyEvent(String eventText, String token) {
- if (DiagLevel.getLevel(Mod.proxy) == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
- return false;
- }
-
- String msg = SdlTrace.B64EncodeForXML(eventText);
- String xml = SdlTrace.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.proxy, InterfaceActivityDirection.None, "<d>" + msg + "</d>");
- return writeXmlTraceMessage(xml);
+public class SdlTrace extends SdlTraceBase {
+ private static String getPid(){
+ return String.valueOf(android.os.Process.myPid());
}
- public static boolean logAppEvent(String eventText) {
- if (DiagLevel.getLevel(Mod.app) == DetailLevel.OFF) {
- return false;
- }
-
- long timestamp = SdlTrace.getBaseTicsDelta();
- String msg = SdlTrace.B64EncodeForXML(eventText);
- String xml = SdlTrace.encodeTraceMessage(timestamp, Mod.app, InterfaceActivityDirection.None, "<d>" + msg + "</d>");
- return writeXmlTraceMessage(xml);
- }
-
- public static boolean logRPCEvent(InterfaceActivityDirection msgDirection, RPCMessage rpcMsg, String token) {
- DetailLevel dl = DiagLevel.getLevel(Mod.rpc);
- if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
- return false;
- }
-
- long timestamp = SdlTrace.getBaseTicsDelta();
- String xml = SdlTrace.encodeTraceMessage(timestamp, Mod.rpc, msgDirection, rpc2Xml(dl, rpcMsg));
- return writeXmlTraceMessage(xml);
- }
-
- private static String rpc2Xml(DetailLevel dl, RPCMessage rpcMsg) {
- StringBuilder rpcAsXml = new StringBuilder();
- rpcAsXml.append("<op>");
- rpcAsXml.append(rpcMsg.getFunctionName());
- rpcAsXml.append("</op>");
- boolean hasCorrelationID = false;
- Integer correlationID = -1;
- if (rpcMsg instanceof RPCRequest) {
- hasCorrelationID = true;
- correlationID = ((RPCRequest)rpcMsg).getCorrelationID();
- } else if (rpcMsg instanceof RPCResponse) {
- hasCorrelationID = true;
- correlationID = ((RPCResponse)rpcMsg).getCorrelationID();
- } // end-if
- if (hasCorrelationID) {
- rpcAsXml.append("<cid>");
- rpcAsXml.append(correlationID);
- rpcAsXml.append("</cid>");
- } // end-if
- rpcAsXml.append("<type>");
- rpcAsXml.append(rpcMsg.getMessageType());
- rpcAsXml.append("</type>");
- //rpcAsXml.append(newline);
-
- if (dl == DetailLevel.VERBOSE) {
- OpenRPCMessage orpcmsg = new OpenRPCMessage(rpcMsg);
- String rpcParamList = orpcmsg.msgDump();
- String msg = SdlTrace.B64EncodeForXML(rpcParamList);
- rpcAsXml.append("<d>");
- rpcAsXml.append(msg);
- rpcAsXml.append("</d>");
- } // end-if
- return rpcAsXml.toString();
- } // end-method
-
- public static boolean logMarshallingEvent(InterfaceActivityDirection msgDirection, byte[] marshalledMessage, String token) {
- DetailLevel dl = DiagLevel.getLevel(Mod.mar);
- if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
- return false;
- }
-
- long timestamp = SdlTrace.getBaseTicsDelta();
- StringBuilder msg = new StringBuilder();
- msg.append("<sz>");
- msg.append(marshalledMessage.length);
- msg.append("</sz>");
- if (dl == DetailLevel.VERBOSE) {
- msg.append("<d>");
- msg.append(Mime.base64Encode(marshalledMessage));
- // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: msg.append(Base64.encodeToString(marshalledMessage, Base64.DEFAULT));
- msg.append("</d>");
- }
- String xml = SdlTrace.encodeTraceMessage(timestamp, Mod.mar, msgDirection, msg.toString());
- return writeXmlTraceMessage(xml);
- }
-
- public static boolean logProtocolEvent(InterfaceActivityDirection frameDirection, SdlPacket packet, int frameDataOffset, int frameDataLength, String token) {
- DetailLevel dl = DiagLevel.getLevel(Mod.proto);
- if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
- return false;
- }
-
- StringBuffer protoMsg = new StringBuffer();
- protoMsg.append("<frame>");
- protoMsg.append(SdlTrace.getProtocolFrameHeaderInfo(packet));
- if (dl == DetailLevel.VERBOSE) {
- if (packet.getPayload() != null && frameDataLength > 0) {
- protoMsg.append("<d>");
- String bytesInfo = "";
- bytesInfo = Mime.base64Encode(packet.getPayload(), frameDataOffset, frameDataLength);
- // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: bytesInfo = Base64.encodeToString(frameData, frameDataOffset, frameDataLength, Base64.DEFAULT);
- protoMsg.append(bytesInfo);
- protoMsg.append("</d>");
- }
- }
- protoMsg.append("</frame>");
- String xml = SdlTrace.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.proto, frameDirection, protoMsg.toString());
- return writeXmlTraceMessage(xml);
- }
-
- private static String getProtocolFrameType(FrameType f) {
- if (f == FrameType.Control)
- return "Control";
- else if (f == FrameType.Consecutive)
- return "Consecutive";
- else if (f == FrameType.First)
- return "First";
- else if (f == FrameType.Single)
- return "Single";
-
- return "Unknown";
- } // end-method
-
- private static String getProtocolSessionType(SessionType serviceType) {
- String s;
- if (serviceType == SessionType.RPC )
- s = "rpc";
- else if (serviceType == SessionType.BULK_DATA)
- s = "bulk";
- else
- s = "Unknown";
- return s;
- } // end-method
-
- private static String getProtocolFrameHeaderInfo(SdlPacket hdr) {
- StringBuilder sb = new StringBuilder();
- sb.append("<hdr>");
- sb.append("<ver>");
- sb.append(hdr.getVersion());
- sb.append("</ver><cmp>");
- sb.append(hdr.isEncrypted());
- sb.append("</cmp><ft>");
- sb.append(getProtocolFrameType(hdr.getFrameType()));
- sb.append("</ft><st>");
- sb.append(getProtocolSessionType(SessionType.valueOf((byte)hdr.getServiceType())));
- sb.append("</st><sid>");
- sb.append(hdr.getSessionId());
- sb.append("</sid><sz>");
- sb.append(hdr.getDataSize());
- sb.append("</sz>");
-
- int frameData = hdr.getFrameInfo();
- if (hdr.getFrameType() == FrameType.Control) {
- sb.append("<ca>");
- if (frameData == FrameDataControlFrameType.StartSession.getValue())
- sb.append("StartSession");
- else if (frameData == FrameDataControlFrameType.StartSessionACK.getValue())
- sb.append("StartSessionACK");
- else if (frameData == FrameDataControlFrameType.StartSessionNACK.getValue())
- sb.append("StartSessionNACK");
- else if (frameData == FrameDataControlFrameType.EndSession.getValue())
- sb.append("EndSession");
- sb.append("</ca>");
- } else if (hdr.getFrameType() == FrameType.Consecutive ) {
- sb.append("<fsn>");
- if (frameData == 0 )
- sb.append("lastFrame");
- else
- sb.append(String.format("%02X",frameData));
- sb.append("</fsn>");
- } else if (hdr.getFrameType() == FrameType.First ) {
- int totalSize = BitConverter.intFromByteArray(hdr.getPayload(), 0);
- int numFrames = BitConverter.intFromByteArray(hdr.getPayload(), 4);
- sb.append("<total>").append(totalSize).append("</total><numframes>").append(numFrames).append("</numframes>");
- } else if (hdr.getFrameType() == FrameType.Single ) {
- sb.append("<single/>");
- }
-
- sb.append("</hdr>");
-
- return sb.toString();
- } // end-method
-
+ @SuppressLint("MissingPermission")
public static String getBTDeviceInfo(BluetoothDevice btDevice) {
StringBuilder sb = new StringBuilder();
sb.append("<btp>");
@@ -361,92 +66,9 @@ public class SdlTrace { return sb.toString();
} // end-method
- public static boolean logTransportEvent(String preamble, String transportSpecificInfoXml, InterfaceActivityDirection msgDirection, byte buf[], int byteLength, String token) {
- return logTransportEvent(preamble, transportSpecificInfoXml, msgDirection, buf, 0, byteLength, token);
- }
-
- private static void checkB64(String x, byte[] buf, int offset, int byteLength) {
- if ((x.length() % 4) != 0) {
- NativeLogTool.logWarning(SdlTrace.SYSTEM_LOG_TAG, "b64 string length (" + x.length() + ") isn't multiple of 4: buf.length=" + buf.length + ", offset=" + offset + ", len=" + byteLength);
- } // end-if
- } // end-method
-
- public static boolean logTransportEvent(String preamble, String transportSpecificInfoXml, InterfaceActivityDirection msgDirection, byte buf[], int offset, int byteLength, String token) {
- if (DiagLevel.getLevel(Mod.tran) == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
- return false;
- }
-
- StringBuilder msg = new StringBuilder();
- if (transportSpecificInfoXml != null && transportSpecificInfoXml.length() > 0) {
- msg.append(transportSpecificInfoXml);
- }
- if (preamble != null && preamble.length() > 0) {
- msg.append("<desc>");
- msg.append(preamble);
- msg.append("</desc>");
- }
- if (buf != null) {
- msg.append("<sz>");
- msg.append(byteLength);
- msg.append("</sz>");
- DetailLevel dl = DiagLevel.getLevel(Mod.tran);
- if (dl == DetailLevel.VERBOSE) {
- if (buf != null && byteLength > 0) {
- msg.append("<d>");
- String bytesInfo = Mime.base64Encode(buf, offset, byteLength);
- checkB64(bytesInfo, buf, offset, byteLength);
- msg.append(bytesInfo);
- msg.append("</d>");
- }
- }
- }
- String xml = SdlTrace.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.tran, msgDirection, msg.toString());
- return writeXmlTraceMessage(xml);
- }
-
- // Package-scoped
- static long getBaseTicsDelta() {
- return java.lang.System.currentTimeMillis() - getBaseTics();
- }
-
- // Package-scoped
- static long getBaseTics() {
- return baseTics;
- } // end-method
-
- public static Boolean writeMessageToSiphonServer(String info) {
- return SiphonServer.sendFormattedTraceMessage(info);
- }
-
- private static boolean writeXmlTraceMessage(String msg) {
- try {
- // Attempt to write formatted message to the Siphon
- if (false == writeMessageToSiphonServer(msg)) {
- // If writing to the Siphon fails, write to the native log
- NativeLogTool.logInfo(SdlTrace.SYSTEM_LOG_TAG, msg);
- return false;
- }
-
- ISTListener localTraceListener = m_appTraceListener;
-
- if (localTraceListener != null) {
- try {
- localTraceListener.logXmlMsg(msg, SDL_LIB_TRACE_KEY);
- } catch (Exception ex) {
- DebugTool.logError(SYSTEM_LOG_TAG, "Failure calling ISTListener: " + ex.toString(), ex);
- return false;
- }
- }
- } catch (Exception ex) {
- NativeLogTool.logError(SdlTrace.SYSTEM_LOG_TAG, "Failure writing XML trace message: " + ex.toString());
- return false;
- }
- return true;
- }
-
// Package-scoped
@SuppressWarnings("deprecation")
- public static String getLogHeader(String dumpReason, int seqNo) {
+ public static String getLogHeader(String dumpReason, int seqNo) {
final String Sep = "-";
StringBuilder write = new StringBuilder("<?xml version=\"1.0\"?>" + "<logs>");
write.append("<info>");
@@ -461,7 +83,7 @@ public class SdlTrace { long heapAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
infoBlock.append("<mem><hf>").append(heapSize).append("KB</hf><ha>").append(heapAllocated).append("KB</ha></mem>");
infoBlock.append("<np>").append(Runtime.getRuntime().availableProcessors()).append("</np>");
- infoBlock.append("<pid>").append(Process.myPid()).append("</pid>");
+ infoBlock.append("<pid>").append(getPid()).append("</pid>");
infoBlock.append("<tid>").append(Thread.currentThread().getId()).append("</tid>");
// String dateStamp = (String)
@@ -480,21 +102,5 @@ public class SdlTrace { write.append("</info>" + "<msgs>");
return write.toString();
} // end-method
-
- private static String getSmartDeviceLinkTraceRoot(String dumpReason, int seqNo) {
- StringBuilder write = new StringBuilder("<SmartDeviceLinktraceroot>" + "<sequencenum>" + seqNo
- + "</sequencenum>" + "<dumpreason>" + dumpReason
- + "</dumpreason><tracelevel>");
-
- write.append("<tran>").append(DiagLevel.getLevel(Mod.tran)).append("</tran>");
- write.append("<proto>").append(DiagLevel.getLevel(Mod.proto)).append("</proto>");
- write.append("<mar>").append(DiagLevel.getLevel(Mod.mar)).append("</mar>");
- write.append("<rpc>").append(DiagLevel.getLevel(Mod.rpc)).append("</rpc>");
- write.append("<proxy>").append(DiagLevel.getLevel(Mod.proxy)).append("</proxy>");
- write.append("<app>").append(DiagLevel.getLevel(Mod.app)).append("</app>");
- write.append("</tracelevel>");
- write.append("</SmartDeviceLinktraceroot>");
- return write.toString();
- } // end-method
} // end-class
\ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/streaming/StreamPacketizer.java b/base/src/main/java/com/smartdevicelink/streaming/StreamPacketizer.java deleted file mode 100644 index 64ee4f795..000000000 --- a/base/src/main/java/com/smartdevicelink/streaming/StreamPacketizer.java +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (c) 2017 - 2019, SmartDeviceLink Consortium, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the SmartDeviceLink Consortium, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package com.smartdevicelink.streaming; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -import com.smartdevicelink.SdlConnection.SdlSession; -import com.smartdevicelink.managers.CompletionListener; -import com.smartdevicelink.protocol.ProtocolMessage; -import com.smartdevicelink.protocol.enums.SessionType; -import com.smartdevicelink.proxy.interfaces.IAudioStreamListener; -import com.smartdevicelink.proxy.interfaces.IVideoStreamListener; -import com.smartdevicelink.util.DebugTool; - -public class StreamPacketizer extends AbstractPacketizer implements IVideoStreamListener, IAudioStreamListener, Runnable{ - - public final static String TAG = "StreamPacketizer"; - - private Thread t = null; - - - private final static int TLS_MAX_RECORD_SIZE = 16384; - private final static int TLS_RECORD_HEADER_SIZE = 5; - private final static int TLS_RECORD_MES_AUTH_CDE_SIZE = 32; - private final static int TLS_MAX_RECORD_PADDING_SIZE = 256; - - - private final static int BUFF_READ_SIZE = TLS_MAX_RECORD_SIZE - TLS_RECORD_HEADER_SIZE - TLS_RECORD_MES_AUTH_CDE_SIZE - TLS_MAX_RECORD_PADDING_SIZE; - - // Approximate size of data that mOutputQueue can hold in bytes. - // By adding a buffer, we accept underlying transport being stuck for a short time. By setting - // a limit of the buffer size, we avoid buffer overflows when underlying transport is too slow. - private static final int MAX_QUEUE_SIZE = 256 * 1024; - - private Object mPauseLock; - private boolean mPaused; - private boolean isServiceProtected = false; - private BlockingQueue<ByteBufferWithListener> mOutputQueue; - - public StreamPacketizer(IStreamListener streamListener, InputStream is, SessionType sType, byte rpcSessionID, SdlSession session) throws IOException { - super(streamListener, is, sType, rpcSessionID, session); - mPauseLock = new Object(); - mPaused = false; - isServiceProtected = _session.isServiceProtected(_serviceType); - if (bufferSize == 0) { - // fail safe - bufferSize = BUFF_READ_SIZE; - } - if(isServiceProtected){ //If our service is encrypted we can only use 1024 as the max buffer size. - bufferSize = BUFF_READ_SIZE; - buffer = new byte[bufferSize]; - } - mOutputQueue = new LinkedBlockingQueue<ByteBufferWithListener>(MAX_QUEUE_SIZE / bufferSize); - } - - public void start() throws IOException { - if (t == null) { - t = new Thread(this); - t.start(); - } - } - - public void stop() { - - if (t != null) - { - t.interrupt(); - t = null; - } - - } - - public void run() { - int length; - try - { - while (t != null && !t.isInterrupted()) - { - synchronized(mPauseLock) - { - while (mPaused) - { - try - { - mPauseLock.wait(); - } - catch (InterruptedException e) { - DebugTool.logError(TAG, "Streaming thread has been interrupted", e); - } - } - } - - if (is != null) { // using InputStream interface - length = is.read(buffer, 0, bufferSize); - - if (length >= 0) { - ProtocolMessage pm = new ProtocolMessage(); - pm.setSessionID(_rpcSessionID); - pm.setSessionType(_serviceType); - pm.setFunctionID(0); - pm.setCorrID(0); - pm.setData(buffer, length); - pm.setPayloadProtected(isServiceProtected); - - if (t != null && !t.isInterrupted()) { - _streamListener.sendStreamPacket(pm); - } - } - } else { // using sendFrame interface - ByteBufferWithListener byteBufferWithListener; - ByteBuffer frame; - CompletionListener completionListener; - try { - byteBufferWithListener = mOutputQueue.take(); - frame = byteBufferWithListener.byteBuffer; - completionListener = byteBufferWithListener.completionListener; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - - while (frame.hasRemaining()) { - int len = frame.remaining() > bufferSize ? bufferSize : frame.remaining(); - - ProtocolMessage pm = new ProtocolMessage(); - pm.setSessionID(_rpcSessionID); - pm.setSessionType(_serviceType); - pm.setFunctionID(0); - pm.setCorrID(0); - pm.setData(frame.array(), frame.arrayOffset() + frame.position(), len); - pm.setPayloadProtected(isServiceProtected); - - if (t != null && !t.isInterrupted()) { - _streamListener.sendStreamPacket(pm); - } - - frame.position(frame.position() + len); - } - - if (!frame.hasRemaining() && completionListener != null){ - completionListener.onComplete(true); - } - } - } - } catch (IOException e) - { - e.printStackTrace(); - } - finally - { - if(_session != null) { - _session.endService(_serviceType); - } - - - } - } - - @Override - public void pause() { - synchronized (mPauseLock) { - mPaused = true; - } - } - - @Override - public void resume() { - synchronized (mPauseLock) { - mPaused = false; - mPauseLock.notifyAll(); - } - } - - /** - * Called by the app. - * - * @see IVideoStreamListener#sendFrame(byte[], int, int, long) - */ - @Override - public void sendFrame(byte[] data, int offset, int length, long presentationTimeUs) - throws ArrayIndexOutOfBoundsException { - sendArrayData(data, offset, length); - } - - /** - * Called by the app. - * - * @see IVideoStreamListener#sendFrame(ByteBuffer, long) - */ - @Override - public void sendFrame(ByteBuffer data, long presentationTimeUs) { - sendByteBufferData(data, null); - } - - /** - * Called by the app. - * - * @see IAudioStreamListener#sendAudio(byte[], int, int, long) - */ - @Override - public void sendAudio(byte[] data, int offset, int length, long presentationTimeUs) - throws ArrayIndexOutOfBoundsException { - sendArrayData(data, offset, length); - } - - /** - * Called by the app. - * - * @see IAudioStreamListener#sendAudio(ByteBuffer, long) - */ - @Deprecated - @Override - public void sendAudio(ByteBuffer data, long presentationTimeUs) { - sendByteBufferData(data, null); - } - - @Override - public void sendAudio(ByteBuffer data, long presentationTimeUs, CompletionListener completionListener) { - sendByteBufferData(data, completionListener); - } - - private void sendArrayData(byte[] data, int offset, int length) - throws ArrayIndexOutOfBoundsException { - if (offset < 0 || offset > data.length || length <= 0 || offset + length > data.length) { - throw new ArrayIndexOutOfBoundsException(); - } - - // StreamPacketizer does not need to split a video frame into NAL units - ByteBuffer buffer = ByteBuffer.allocate(length); - buffer.put(data, offset, length); - buffer.flip(); - - try { - mOutputQueue.put(new ByteBufferWithListener(buffer, null)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - private void sendByteBufferData(ByteBuffer data, CompletionListener completionListener) { - if (data == null || data.remaining() == 0) { - return; - } - - // copy the whole buffer, so that even if the app modifies original ByteBuffer after - // sendFrame() or sendAudio() call, our buffer will stay intact - ByteBuffer buffer = ByteBuffer.allocate(data.remaining()); - buffer.put(data); - buffer.flip(); - - try { - mOutputQueue.put(new ByteBufferWithListener(buffer, completionListener)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - - private class ByteBufferWithListener{ - ByteBuffer byteBuffer; - CompletionListener completionListener; - ByteBufferWithListener (ByteBuffer byteBuffer, CompletionListener completionListener){ - this.byteBuffer = byteBuffer; - this.completionListener = completionListener; - } - } -} diff --git a/base/src/main/java/com/smartdevicelink/trace/SdlTrace.java b/base/src/main/java/com/smartdevicelink/trace/SdlTrace.java index 4e654603b..482b4a846 100644 --- a/base/src/main/java/com/smartdevicelink/trace/SdlTrace.java +++ b/base/src/main/java/com/smartdevicelink/trace/SdlTrace.java @@ -32,22 +32,6 @@ package com.smartdevicelink.trace; - -import com.smartdevicelink.protocol.SdlPacket; -import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; -import com.smartdevicelink.protocol.enums.FrameType; -import com.smartdevicelink.protocol.enums.SessionType; -import com.smartdevicelink.proxy.RPCMessage; -import com.smartdevicelink.proxy.RPCRequest; -import com.smartdevicelink.proxy.RPCResponse; -import com.smartdevicelink.trace.enums.DetailLevel; -import com.smartdevicelink.trace.enums.InterfaceActivityDirection; -import com.smartdevicelink.trace.enums.Mod; -import com.smartdevicelink.transport.SiphonServer; -import com.smartdevicelink.util.BitConverter; -import com.smartdevicelink.util.DebugTool; -import com.smartdevicelink.util.NativeLogTool; - /* This class handles the global TraceSettings as requested by the users either through the combination of the following 1. System defaults 2. Application XML config @@ -56,397 +40,9 @@ import com.smartdevicelink.util.NativeLogTool; It is manifested in the <SmartDeviceLink>...</SmartDeviceLink> tags */ -//@SuppressLint("DefaultLocale") -public class SdlTrace { - private static final String SDL_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66"; - - public static final String SYSTEM_LOG_TAG = "SdlTrace"; - - private static long baseTics = System.currentTimeMillis(); - private static boolean acceptAPITraceAdjustments = true; - - protected static ISTListener m_appTraceListener = null; - - /// - /// The PUBLIC interface to SdlTrace starts here - /// - - - public static void setAcceptAPITraceAdjustments(Boolean APITraceAdjustmentsAccepted) { - if (APITraceAdjustmentsAccepted != null) { - acceptAPITraceAdjustments = APITraceAdjustmentsAccepted; - } - } - - public static boolean getAcceptAPITraceAdjustments() { - return acceptAPITraceAdjustments; - } - - public static void setAppTraceListener(ISTListener listener) { - m_appTraceListener = listener; - } // end-method - - public static void setAppTraceLevel(DetailLevel dt) { - if ( dt != null && acceptAPITraceAdjustments) - DiagLevel.setLevel(Mod.app, dt); - } // end-method - - public static void setProxyTraceLevel(DetailLevel dt) { - if (dt != null && acceptAPITraceAdjustments) - DiagLevel.setLevel(Mod.proxy, dt); - } // end-method - - public static void setRpcTraceLevel(DetailLevel dt) { - if (dt != null && acceptAPITraceAdjustments) - DiagLevel.setLevel(Mod.rpc, dt); - } // end-method - - public static void setMarshallingTraceLevel(DetailLevel dt) { - if (dt != null && acceptAPITraceAdjustments) - DiagLevel.setLevel(Mod.mar, dt); - } // end-method - - public static void setProtocolTraceLevel(DetailLevel dt) { - if (dt != null && acceptAPITraceAdjustments) - DiagLevel.setLevel(Mod.proto, dt); - } // end-method - - public static void setTransportTraceLevel(DetailLevel dt) { - if (dt != null && acceptAPITraceAdjustments) - DiagLevel.setLevel(Mod.tran, dt); - } // end-method - +public class SdlTrace extends SdlTraceBase{ private static String getPid(){ //Default implementation is not able to get this information return "UNKNOWN"; } - - private static String encodeTraceMessage(long timestamp, Mod module, InterfaceActivityDirection msgDirection, String msgBodyXml) { - StringBuilder sb = new StringBuilder("<msg><dms>"); - sb.append(timestamp); - sb.append("</dms><pid>"); - sb.append(getPid()); - sb.append("</pid><tid>"); - sb.append(Thread.currentThread().getId()); - sb.append("</tid><mod>"); - sb.append(module.toString()); - sb.append("</mod>"); - if (msgDirection != InterfaceActivityDirection.None) { - sb.append("<dir>"); - sb.append(interfaceActivityDirectionToString(msgDirection)); - sb.append("</dir>"); - } // end-if - sb.append(msgBodyXml); - sb.append("</msg>"); - - return sb.toString(); - } // end-method - - private static String interfaceActivityDirectionToString(InterfaceActivityDirection iaDirection) { - String str = ""; - switch (iaDirection) { - case Receive: - str = "rx"; - break; - case Transmit: - str = "tx"; - break; - default: - break; - } // end-switch - return str; - } // end-method - - static String B64EncodeForXML(String data) { - return Mime.base64Encode(data); - // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: return Base64.encodeToString(data.getBytes(), Base64.DEFAULT); - } // end-method - - public static boolean logProxyEvent(String eventText, String token) { - if (DiagLevel.getLevel(Mod.proxy) == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) { - return false; - } - - String msg = SdlTrace.B64EncodeForXML(eventText); - String xml = SdlTrace.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.proxy, InterfaceActivityDirection.None, "<d>" + msg + "</d>"); - return writeXmlTraceMessage(xml); - } - - public static boolean logAppEvent(String eventText) { - if (DiagLevel.getLevel(Mod.app) == DetailLevel.OFF) { - return false; - } - - long timestamp = SdlTrace.getBaseTicsDelta(); - String msg = SdlTrace.B64EncodeForXML(eventText); - String xml = SdlTrace.encodeTraceMessage(timestamp, Mod.app, InterfaceActivityDirection.None, "<d>" + msg + "</d>"); - return writeXmlTraceMessage(xml); - } - - public static boolean logRPCEvent(InterfaceActivityDirection msgDirection, RPCMessage rpcMsg, String token) { - DetailLevel dl = DiagLevel.getLevel(Mod.rpc); - if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) { - return false; - } - - long timestamp = SdlTrace.getBaseTicsDelta(); - String xml = SdlTrace.encodeTraceMessage(timestamp, Mod.rpc, msgDirection, rpc2Xml(dl, rpcMsg)); - return writeXmlTraceMessage(xml); - } - - private static String rpc2Xml(DetailLevel dl, RPCMessage rpcMsg) { - StringBuilder rpcAsXml = new StringBuilder(); - rpcAsXml.append("<op>"); - rpcAsXml.append(rpcMsg.getFunctionName()); - rpcAsXml.append("</op>"); - boolean hasCorrelationID = false; - Integer correlationID = -1; - if (rpcMsg instanceof RPCRequest) { - hasCorrelationID = true; - correlationID = ((RPCRequest)rpcMsg).getCorrelationID(); - } else if (rpcMsg instanceof RPCResponse) { - hasCorrelationID = true; - correlationID = ((RPCResponse)rpcMsg).getCorrelationID(); - } // end-if - if (hasCorrelationID) { - rpcAsXml.append("<cid>"); - rpcAsXml.append(correlationID); - rpcAsXml.append("</cid>"); - } // end-if - rpcAsXml.append("<type>"); - rpcAsXml.append(rpcMsg.getMessageType()); - rpcAsXml.append("</type>"); - //rpcAsXml.append(newline); - - if (dl == DetailLevel.VERBOSE) { - OpenRPCMessage orpcmsg = new OpenRPCMessage(rpcMsg); - String rpcParamList = orpcmsg.msgDump(); - String msg = SdlTrace.B64EncodeForXML(rpcParamList); - rpcAsXml.append("<d>"); - rpcAsXml.append(msg); - rpcAsXml.append("</d>"); - } // end-if - return rpcAsXml.toString(); - } // end-method - - public static boolean logMarshallingEvent(InterfaceActivityDirection msgDirection, byte[] marshalledMessage, String token) { - DetailLevel dl = DiagLevel.getLevel(Mod.mar); - if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) { - return false; - } - - long timestamp = SdlTrace.getBaseTicsDelta(); - StringBuilder msg = new StringBuilder(); - msg.append("<sz>"); - msg.append(marshalledMessage.length); - msg.append("</sz>"); - if (dl == DetailLevel.VERBOSE) { - msg.append("<d>"); - msg.append(Mime.base64Encode(marshalledMessage)); - // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: msg.append(Base64.encodeToString(marshalledMessage, Base64.DEFAULT)); - msg.append("</d>"); - } - String xml = SdlTrace.encodeTraceMessage(timestamp, Mod.mar, msgDirection, msg.toString()); - return writeXmlTraceMessage(xml); - } - - public static boolean logProtocolEvent(InterfaceActivityDirection frameDirection, SdlPacket packet, int frameDataOffset, int frameDataLength, String token) { - DetailLevel dl = DiagLevel.getLevel(Mod.proto); - if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) { - return false; - } - - StringBuffer protoMsg = new StringBuffer(); - protoMsg.append("<frame>"); - protoMsg.append(SdlTrace.getProtocolFrameHeaderInfo(packet)); - if (dl == DetailLevel.VERBOSE) { - if (packet.getPayload() != null && frameDataLength > 0) { - protoMsg.append("<d>"); - String bytesInfo = ""; - bytesInfo = Mime.base64Encode(packet.getPayload(), frameDataOffset, frameDataLength); - // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: bytesInfo = Base64.encodeToString(frameData, frameDataOffset, frameDataLength, Base64.DEFAULT); - protoMsg.append(bytesInfo); - protoMsg.append("</d>"); - } - } - protoMsg.append("</frame>"); - String xml = SdlTrace.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.proto, frameDirection, protoMsg.toString()); - return writeXmlTraceMessage(xml); - } - - private static String getProtocolFrameType(FrameType f) { - if (f == FrameType.Control) - return "Control"; - else if (f == FrameType.Consecutive) - return "Consecutive"; - else if (f == FrameType.First) - return "First"; - else if (f == FrameType.Single) - return "Single"; - - return "Unknown"; - } // end-method - - private static String getProtocolSessionType(SessionType serviceType) { - String s; - if (serviceType == SessionType.RPC ) - s = "rpc"; - else if (serviceType == SessionType.BULK_DATA) - s = "bulk"; - else - s = "Unknown"; - return s; - } // end-method - - private static String getProtocolFrameHeaderInfo(SdlPacket hdr) { - StringBuilder sb = new StringBuilder(); - sb.append("<hdr>"); - sb.append("<ver>"); - sb.append(hdr.getVersion()); - sb.append("</ver><cmp>"); - sb.append(hdr.isEncrypted()); - sb.append("</cmp><ft>"); - sb.append(getProtocolFrameType(hdr.getFrameType())); - sb.append("</ft><st>"); - sb.append(getProtocolSessionType(SessionType.valueOf((byte)hdr.getServiceType()))); - sb.append("</st><sid>"); - sb.append(hdr.getSessionId()); - sb.append("</sid><sz>"); - sb.append(hdr.getDataSize()); - sb.append("</sz>"); - - int frameData = hdr.getFrameInfo(); - if (hdr.getFrameType() == FrameType.Control) { - sb.append("<ca>"); - if (frameData == FrameDataControlFrameType.StartSession.getValue()) - sb.append("StartSession"); - else if (frameData == FrameDataControlFrameType.StartSessionACK.getValue()) - sb.append("StartSessionACK"); - else if (frameData == FrameDataControlFrameType.StartSessionNACK.getValue()) - sb.append("StartSessionNACK"); - else if (frameData == FrameDataControlFrameType.EndSession.getValue()) - sb.append("EndSession"); - sb.append("</ca>"); - } else if (hdr.getFrameType() == FrameType.Consecutive ) { - sb.append("<fsn>"); - if (frameData == 0 ) - sb.append("lastFrame"); - else - sb.append(String.format("%02X",frameData)); - sb.append("</fsn>"); - } else if (hdr.getFrameType() == FrameType.First ) { - int totalSize = BitConverter.intFromByteArray(hdr.getPayload(), 0); - int numFrames = BitConverter.intFromByteArray(hdr.getPayload(), 4); - sb.append("<total>" + totalSize + "</total><numframes>" + numFrames + "</numframes>"); - } else if (hdr.getFrameType() == FrameType.Single ) { - sb.append("<single/>"); - } - - sb.append("</hdr>"); - - return sb.toString(); - } // end-method - - public static boolean logTransportEvent(String preamble, String transportSpecificInfoXml, InterfaceActivityDirection msgDirection, byte buf[], int byteLength, String token) { - return logTransportEvent(preamble, transportSpecificInfoXml, msgDirection, buf, 0, byteLength, token); - } - - private static void checkB64(String x, byte[] buf, int offset, int byteLength) { - if ((x.length() % 4) != 0) { - NativeLogTool.logWarning(SdlTrace.SYSTEM_LOG_TAG, "b64 string length (" + x.length() + ") isn't multiple of 4: buf.length=" + buf.length + ", offset=" + offset + ", len=" + byteLength); - } // end-if - } // end-method - - public static boolean logTransportEvent(String preamble, String transportSpecificInfoXml, InterfaceActivityDirection msgDirection, byte buf[], int offset, int byteLength, String token) { - if (DiagLevel.getLevel(Mod.tran) == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) { - return false; - } - - StringBuilder msg = new StringBuilder(); - if (transportSpecificInfoXml != null && transportSpecificInfoXml.length() > 0) { - msg.append(transportSpecificInfoXml); - } - if (preamble != null && preamble.length() > 0) { - msg.append("<desc>"); - msg.append(preamble); - msg.append("</desc>"); - } - if (buf != null) { - msg.append("<sz>"); - msg.append(byteLength); - msg.append("</sz>"); - DetailLevel dl = DiagLevel.getLevel(Mod.tran); - if (dl == DetailLevel.VERBOSE) { - if (buf != null && byteLength > 0) { - msg.append("<d>"); - String bytesInfo = Mime.base64Encode(buf, offset, byteLength); - checkB64(bytesInfo, buf, offset, byteLength); - msg.append(bytesInfo); - msg.append("</d>"); - } - } - } - String xml = SdlTrace.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.tran, msgDirection, msg.toString()); - return writeXmlTraceMessage(xml); - } - - // Package-scoped - static long getBaseTicsDelta() { - return System.currentTimeMillis() - getBaseTics(); - } - - // Package-scoped - static long getBaseTics() { - return baseTics; - } // end-method - - public static Boolean writeMessageToSiphonServer(String info) { - return SiphonServer.sendFormattedTraceMessage(info); - } - - private static boolean writeXmlTraceMessage(String msg) { - try { - // Attempt to write formatted message to the Siphon - if (false == writeMessageToSiphonServer(msg)) { - // If writing to the Siphon fails, write to the native log - NativeLogTool.logInfo(SdlTrace.SYSTEM_LOG_TAG, msg); - return false; - } - - ISTListener localTraceListener = m_appTraceListener; - - if (localTraceListener != null) { - try { - localTraceListener.logXmlMsg(msg, SDL_LIB_TRACE_KEY); - } catch (Exception ex) { - DebugTool.logError(SYSTEM_LOG_TAG,"Failure calling ISTListener: " + ex.toString(), ex); - return false; - } - } - } catch (Exception ex) { - NativeLogTool.logError(SdlTrace.SYSTEM_LOG_TAG, "Failure writing XML trace message: " + ex.toString()); - return false; - } - return true; - } - - - - - private static String getSmartDeviceLinkTraceRoot(String dumpReason, int seqNo) { - StringBuilder write = new StringBuilder("<SmartDeviceLinktraceroot>" + "<sequencenum>" + seqNo - + "</sequencenum>" + "<dumpreason>" + dumpReason - + "</dumpreason><tracelevel>"); - - write.append("<tran>" + DiagLevel.getLevel(Mod.tran) + "</tran>"); - write.append("<proto>" + DiagLevel.getLevel(Mod.proto) + "</proto>"); - write.append("<mar>" + DiagLevel.getLevel(Mod.mar) + "</mar>"); - write.append("<rpc>" + DiagLevel.getLevel(Mod.rpc) + "</rpc>"); - write.append("<proxy>" + DiagLevel.getLevel(Mod.proxy) + "</proxy>"); - write.append("<app>" + DiagLevel.getLevel(Mod.app) + "</app>"); - - write.append("</tracelevel>"); - write.append("</SmartDeviceLinktraceroot>"); - return write.toString(); - } // end-method } // end-class
\ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java b/base/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java new file mode 100644 index 000000000..bbddd6453 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java @@ -0,0 +1,444 @@ +/*
+ * Copyright (c) 2017 - 2019, SmartDeviceLink Consortium, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the SmartDeviceLink Consortium, Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.smartdevicelink.trace;
+
+import com.smartdevicelink.protocol.SdlPacket;
+import com.smartdevicelink.protocol.enums.FrameDataControlFrameType;
+import com.smartdevicelink.protocol.enums.FrameType;
+import com.smartdevicelink.protocol.enums.SessionType;
+import com.smartdevicelink.proxy.RPCMessage;
+import com.smartdevicelink.proxy.RPCRequest;
+import com.smartdevicelink.proxy.RPCResponse;
+import com.smartdevicelink.trace.enums.DetailLevel;
+import com.smartdevicelink.trace.enums.InterfaceActivityDirection;
+import com.smartdevicelink.trace.enums.Mod;
+import com.smartdevicelink.transport.SiphonServer;
+import com.smartdevicelink.util.BitConverter;
+import com.smartdevicelink.util.DebugTool;
+import com.smartdevicelink.util.NativeLogTool;
+
+/* This class handles the global TraceSettings as requested by the users either through the combination of the following
+ 1. System defaults
+ 2. Application XML config
+ 3. Programmatic requests from application itself
+
+ It is manifested in the <SmartDeviceLink>...</SmartDeviceLink> tags
+ */
+
+class SdlTraceBase {
+ private static final String SDL_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66";
+
+ public static final String SYSTEM_LOG_TAG = "SdlTrace";
+
+ private static long baseTics = java.lang.System.currentTimeMillis();
+ private static boolean acceptAPITraceAdjustments = true;
+
+ protected static ISTListener m_appTraceListener = null;
+
+ ///
+ /// The PUBLIC interface to SdlTrace starts here
+ ///
+
+ public static void setAcceptAPITraceAdjustments(Boolean APITraceAdjustmentsAccepted) {
+ if (APITraceAdjustmentsAccepted != null) {
+ acceptAPITraceAdjustments = APITraceAdjustmentsAccepted;
+ }
+ }
+
+ public static boolean getAcceptAPITraceAdjustments() {
+ return acceptAPITraceAdjustments;
+ }
+
+ public static void setAppTraceListener(ISTListener listener) {
+ m_appTraceListener = listener;
+ } // end-method
+
+ public static void setAppTraceLevel(DetailLevel dt) {
+ if ( dt != null && acceptAPITraceAdjustments)
+ DiagLevel.setLevel(Mod.app, dt);
+ } // end-method
+
+ public static void setProxyTraceLevel(DetailLevel dt) {
+ if (dt != null && acceptAPITraceAdjustments)
+ DiagLevel.setLevel(Mod.proxy, dt);
+ } // end-method
+
+ public static void setRpcTraceLevel(DetailLevel dt) {
+ if (dt != null && acceptAPITraceAdjustments)
+ DiagLevel.setLevel(Mod.rpc, dt);
+ } // end-method
+
+ public static void setMarshallingTraceLevel(DetailLevel dt) {
+ if (dt != null && acceptAPITraceAdjustments)
+ DiagLevel.setLevel(Mod.mar, dt);
+ } // end-method
+
+ public static void setProtocolTraceLevel(DetailLevel dt) {
+ if (dt != null && acceptAPITraceAdjustments)
+ DiagLevel.setLevel(Mod.proto, dt);
+ } // end-method
+
+ public static void setTransportTraceLevel(DetailLevel dt) {
+ if (dt != null && acceptAPITraceAdjustments)
+ DiagLevel.setLevel(Mod.tran, dt);
+ } // end-method
+
+ private static String getPid() {
+ return "UNKNOWN";
+ }
+
+ private static String encodeTraceMessage(long timestamp, Mod module, InterfaceActivityDirection msgDirection, String msgBodyXml) {
+ StringBuilder sb = new StringBuilder("<msg><dms>");
+ sb.append(timestamp);
+ sb.append("</dms><pid>");
+ sb.append(getPid());
+ sb.append("</pid><tid>");
+ sb.append(Thread.currentThread().getId());
+ sb.append("</tid><mod>");
+ sb.append(module.toString());
+ sb.append("</mod>");
+ if (msgDirection != InterfaceActivityDirection.None) {
+ sb.append("<dir>");
+ sb.append(interfaceActivityDirectionToString(msgDirection));
+ sb.append("</dir>");
+ } // end-if
+ sb.append(msgBodyXml);
+ sb.append("</msg>");
+
+ return sb.toString();
+ } // end-method
+
+ private static String interfaceActivityDirectionToString(InterfaceActivityDirection iaDirection) {
+ String str = "";
+ switch (iaDirection) {
+ case Receive:
+ str = "rx";
+ break;
+ case Transmit:
+ str = "tx";
+ break;
+ default:
+ break;
+ } // end-switch
+ return str;
+ } // end-method
+
+ static String B64EncodeForXML(String data) {
+ return Mime.base64Encode(data);
+ // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: return Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
+ } // end-method
+
+ public static boolean logProxyEvent(String eventText, String token) {
+ if (DiagLevel.getLevel(Mod.proxy) == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
+ return false;
+ }
+
+ String msg = SdlTrace.B64EncodeForXML(eventText);
+ String xml = SdlTraceBase.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.proxy, InterfaceActivityDirection.None, "<d>" + msg + "</d>");
+ return writeXmlTraceMessage(xml);
+ }
+
+ public static boolean logAppEvent(String eventText) {
+ if (DiagLevel.getLevel(Mod.app) == DetailLevel.OFF) {
+ return false;
+ }
+
+ long timestamp = SdlTrace.getBaseTicsDelta();
+ String msg = SdlTrace.B64EncodeForXML(eventText);
+ String xml = SdlTraceBase.encodeTraceMessage(timestamp, Mod.app, InterfaceActivityDirection.None, "<d>" + msg + "</d>");
+ return writeXmlTraceMessage(xml);
+ }
+
+ public static boolean logRPCEvent(InterfaceActivityDirection msgDirection, RPCMessage rpcMsg, String token) {
+ DetailLevel dl = DiagLevel.getLevel(Mod.rpc);
+ if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
+ return false;
+ }
+
+ long timestamp = SdlTrace.getBaseTicsDelta();
+ String xml = SdlTraceBase.encodeTraceMessage(timestamp, Mod.rpc, msgDirection, rpc2Xml(dl, rpcMsg));
+ return writeXmlTraceMessage(xml);
+ }
+
+ private static String rpc2Xml(DetailLevel dl, RPCMessage rpcMsg) {
+ StringBuilder rpcAsXml = new StringBuilder();
+ rpcAsXml.append("<op>");
+ rpcAsXml.append(rpcMsg.getFunctionName());
+ rpcAsXml.append("</op>");
+ boolean hasCorrelationID = false;
+ Integer correlationID = -1;
+ if (rpcMsg instanceof RPCRequest) {
+ hasCorrelationID = true;
+ correlationID = ((RPCRequest)rpcMsg).getCorrelationID();
+ } else if (rpcMsg instanceof RPCResponse) {
+ hasCorrelationID = true;
+ correlationID = ((RPCResponse)rpcMsg).getCorrelationID();
+ } // end-if
+ if (hasCorrelationID) {
+ rpcAsXml.append("<cid>");
+ rpcAsXml.append(correlationID);
+ rpcAsXml.append("</cid>");
+ } // end-if
+ rpcAsXml.append("<type>");
+ rpcAsXml.append(rpcMsg.getMessageType());
+ rpcAsXml.append("</type>");
+ //rpcAsXml.append(newline);
+
+ if (dl == DetailLevel.VERBOSE) {
+ OpenRPCMessage orpcmsg = new OpenRPCMessage(rpcMsg);
+ String rpcParamList = orpcmsg.msgDump();
+ String msg = SdlTrace.B64EncodeForXML(rpcParamList);
+ rpcAsXml.append("<d>");
+ rpcAsXml.append(msg);
+ rpcAsXml.append("</d>");
+ } // end-if
+ return rpcAsXml.toString();
+ } // end-method
+
+ public static boolean logMarshallingEvent(InterfaceActivityDirection msgDirection, byte[] marshalledMessage, String token) {
+ DetailLevel dl = DiagLevel.getLevel(Mod.mar);
+ if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
+ return false;
+ }
+
+ long timestamp = SdlTrace.getBaseTicsDelta();
+ StringBuilder msg = new StringBuilder();
+ msg.append("<sz>");
+ msg.append(marshalledMessage.length);
+ msg.append("</sz>");
+ if (dl == DetailLevel.VERBOSE) {
+ msg.append("<d>");
+ msg.append(Mime.base64Encode(marshalledMessage));
+ // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: msg.append(Base64.encodeToString(marshalledMessage, Base64.DEFAULT));
+ msg.append("</d>");
+ }
+ String xml = SdlTraceBase.encodeTraceMessage(timestamp, Mod.mar, msgDirection, msg.toString());
+ return writeXmlTraceMessage(xml);
+ }
+
+ public static boolean logProtocolEvent(InterfaceActivityDirection frameDirection, SdlPacket packet, int frameDataOffset, int frameDataLength, String token) {
+ DetailLevel dl = DiagLevel.getLevel(Mod.proto);
+ if (dl == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
+ return false;
+ }
+
+ StringBuffer protoMsg = new StringBuffer();
+ protoMsg.append("<frame>");
+ protoMsg.append(SdlTraceBase.getProtocolFrameHeaderInfo(packet));
+ if (dl == DetailLevel.VERBOSE) {
+ if (packet.getPayload() != null && frameDataLength > 0) {
+ protoMsg.append("<d>");
+ String bytesInfo = "";
+ bytesInfo = Mime.base64Encode(packet.getPayload(), frameDataOffset, frameDataLength);
+ // Base64 only available in 2.2, when SmartDeviceLink base is 2.2 use: bytesInfo = Base64.encodeToString(frameData, frameDataOffset, frameDataLength, Base64.DEFAULT);
+ protoMsg.append(bytesInfo);
+ protoMsg.append("</d>");
+ }
+ }
+ protoMsg.append("</frame>");
+ String xml = SdlTraceBase.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.proto, frameDirection, protoMsg.toString());
+ return writeXmlTraceMessage(xml);
+ }
+
+ private static String getProtocolFrameType(FrameType f) {
+ if (f == FrameType.Control)
+ return "Control";
+ else if (f == FrameType.Consecutive)
+ return "Consecutive";
+ else if (f == FrameType.First)
+ return "First";
+ else if (f == FrameType.Single)
+ return "Single";
+
+ return "Unknown";
+ } // end-method
+
+ private static String getProtocolSessionType(SessionType serviceType) {
+ String s;
+ if (serviceType == SessionType.RPC )
+ s = "rpc";
+ else if (serviceType == SessionType.BULK_DATA)
+ s = "bulk";
+ else
+ s = "Unknown";
+ return s;
+ } // end-method
+
+ private static String getProtocolFrameHeaderInfo(SdlPacket hdr) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("<hdr>");
+ sb.append("<ver>");
+ sb.append(hdr.getVersion());
+ sb.append("</ver><cmp>");
+ sb.append(hdr.isEncrypted());
+ sb.append("</cmp><ft>");
+ sb.append(getProtocolFrameType(hdr.getFrameType()));
+ sb.append("</ft><st>");
+ sb.append(getProtocolSessionType(SessionType.valueOf((byte)hdr.getServiceType())));
+ sb.append("</st><sid>");
+ sb.append(hdr.getSessionId());
+ sb.append("</sid><sz>");
+ sb.append(hdr.getDataSize());
+ sb.append("</sz>");
+
+ int frameData = hdr.getFrameInfo();
+ if (hdr.getFrameType() == FrameType.Control) {
+ sb.append("<ca>");
+ if (frameData == FrameDataControlFrameType.StartSession.getValue())
+ sb.append("StartSession");
+ else if (frameData == FrameDataControlFrameType.StartSessionACK.getValue())
+ sb.append("StartSessionACK");
+ else if (frameData == FrameDataControlFrameType.StartSessionNACK.getValue())
+ sb.append("StartSessionNACK");
+ else if (frameData == FrameDataControlFrameType.EndSession.getValue())
+ sb.append("EndSession");
+ sb.append("</ca>");
+ } else if (hdr.getFrameType() == FrameType.Consecutive ) {
+ sb.append("<fsn>");
+ if (frameData == 0 )
+ sb.append("lastFrame");
+ else
+ sb.append(String.format("%02X",frameData));
+ sb.append("</fsn>");
+ } else if (hdr.getFrameType() == FrameType.First ) {
+ int totalSize = BitConverter.intFromByteArray(hdr.getPayload(), 0);
+ int numFrames = BitConverter.intFromByteArray(hdr.getPayload(), 4);
+ sb.append("<total>").append(totalSize).append("</total><numframes>").append(numFrames).append("</numframes>");
+ } else if (hdr.getFrameType() == FrameType.Single ) {
+ sb.append("<single/>");
+ }
+
+ sb.append("</hdr>");
+
+ return sb.toString();
+ } // end-method
+
+ public static boolean logTransportEvent(String preamble, String transportSpecificInfoXml, InterfaceActivityDirection msgDirection, byte buf[], int byteLength, String token) {
+ return logTransportEvent(preamble, transportSpecificInfoXml, msgDirection, buf, 0, byteLength, token);
+ }
+
+ private static void checkB64(String x, byte[] buf, int offset, int byteLength) {
+ if ((x.length() % 4) != 0) {
+ NativeLogTool.logWarning(SdlTrace.SYSTEM_LOG_TAG, "b64 string length (" + x.length() + ") isn't multiple of 4: buf.length=" + buf.length + ", offset=" + offset + ", len=" + byteLength);
+ } // end-if
+ } // end-method
+
+ public static boolean logTransportEvent(String preamble, String transportSpecificInfoXml, InterfaceActivityDirection msgDirection, byte buf[], int offset, int byteLength, String token) {
+ if (DiagLevel.getLevel(Mod.tran) == DetailLevel.OFF || !token.equals(SDL_LIB_TRACE_KEY)) {
+ return false;
+ }
+
+ StringBuilder msg = new StringBuilder();
+ if (transportSpecificInfoXml != null && transportSpecificInfoXml.length() > 0) {
+ msg.append(transportSpecificInfoXml);
+ }
+ if (preamble != null && preamble.length() > 0) {
+ msg.append("<desc>");
+ msg.append(preamble);
+ msg.append("</desc>");
+ }
+ if (buf != null) {
+ msg.append("<sz>");
+ msg.append(byteLength);
+ msg.append("</sz>");
+ DetailLevel dl = DiagLevel.getLevel(Mod.tran);
+ if (dl == DetailLevel.VERBOSE) {
+ if (buf != null && byteLength > 0) {
+ msg.append("<d>");
+ String bytesInfo = Mime.base64Encode(buf, offset, byteLength);
+ checkB64(bytesInfo, buf, offset, byteLength);
+ msg.append(bytesInfo);
+ msg.append("</d>");
+ }
+ }
+ }
+ String xml = SdlTraceBase.encodeTraceMessage(SdlTrace.getBaseTicsDelta(), Mod.tran, msgDirection, msg.toString());
+ return writeXmlTraceMessage(xml);
+ }
+
+ // Package-scoped
+ static long getBaseTicsDelta() {
+ return java.lang.System.currentTimeMillis() - getBaseTics();
+ }
+
+ // Package-scoped
+ static long getBaseTics() {
+ return baseTics;
+ } // end-method
+
+ public static Boolean writeMessageToSiphonServer(String info) {
+ return SiphonServer.sendFormattedTraceMessage(info);
+ }
+
+ private static boolean writeXmlTraceMessage(String msg) {
+ try {
+ // Attempt to write formatted message to the Siphon
+ if (false == writeMessageToSiphonServer(msg)) {
+ // If writing to the Siphon fails, write to the native log
+ NativeLogTool.logInfo(SdlTrace.SYSTEM_LOG_TAG, msg);
+ return false;
+ }
+
+ ISTListener localTraceListener = m_appTraceListener;
+
+ if (localTraceListener != null) {
+ try {
+ localTraceListener.logXmlMsg(msg, SDL_LIB_TRACE_KEY);
+ } catch (Exception ex) {
+ DebugTool.logError(SYSTEM_LOG_TAG, "Failure calling ISTListener: " + ex.toString(), ex);
+ return false;
+ }
+ }
+ } catch (Exception ex) {
+ NativeLogTool.logError(SdlTrace.SYSTEM_LOG_TAG, "Failure writing XML trace message: " + ex.toString());
+ return false;
+ }
+ return true;
+ }
+
+ static String getSmartDeviceLinkTraceRoot(String dumpReason, int seqNo) {
+ StringBuilder write = new StringBuilder("<SmartDeviceLinktraceroot>" + "<sequencenum>" + seqNo
+ + "</sequencenum>" + "<dumpreason>" + dumpReason
+ + "</dumpreason><tracelevel>");
+
+ write.append("<tran>").append(DiagLevel.getLevel(Mod.tran)).append("</tran>");
+ write.append("<proto>").append(DiagLevel.getLevel(Mod.proto)).append("</proto>");
+ write.append("<mar>").append(DiagLevel.getLevel(Mod.mar)).append("</mar>");
+ write.append("<rpc>").append(DiagLevel.getLevel(Mod.rpc)).append("</rpc>");
+ write.append("<proxy>").append(DiagLevel.getLevel(Mod.proxy)).append("</proxy>");
+ write.append("<app>").append(DiagLevel.getLevel(Mod.app)).append("</app>");
+
+ write.append("</tracelevel>");
+ write.append("</SmartDeviceLinktraceroot>");
+ return write.toString();
+ } // end-method
+} // end-class
\ No newline at end of file diff --git a/baseAndroid/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java b/baseAndroid/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java new file mode 120000 index 000000000..1996c64bb --- /dev/null +++ b/baseAndroid/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java @@ -0,0 +1 @@ +../../../../../../../base/src/main/java/com/smartdevicelink/trace/SdlTraceBase.java
\ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlProtocol.java b/javaSE/src/main/java/com/smartdevicelink/protocol/SdlProtocol.java index 67ea25cc1..67ea25cc1 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlProtocol.java +++ b/javaSE/src/main/java/com/smartdevicelink/protocol/SdlProtocol.java diff --git a/base/src/main/java/com/smartdevicelink/transport/TransportManager.java b/javaSE/src/main/java/com/smartdevicelink/transport/TransportManager.java index c8a07a1cc..c8a07a1cc 100644 --- a/base/src/main/java/com/smartdevicelink/transport/TransportManager.java +++ b/javaSE/src/main/java/com/smartdevicelink/transport/TransportManager.java |