All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.smartdevicelink.trace.SdlTraceBase Maven / Gradle / Ivy

Go to download

The app library component of SDL is meant to run on the end user’s smart-device from within SDL enabled apps, as an embedded app, or connected to the cloud. App libraries allow the apps to connect to SDL enabled head-units and hardware through bluetooth, USB, and TCP for Android, and cloud and embedded apps can connect through web sockets, Java Beans, and other custom transports. Once the library establishes a connection between the smart device and head-unit through the preferred method of transport, the two components are able to communicate using the SDL defined protocol. The app integrating this library project is then able to expose its functionality to the head-unit through text, media, and other interactive elements.

There is a newer version: 5.7.0
Show newest version
/*
 * 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 ... 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 final 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("");
        sb.append(timestamp);
        sb.append("");
        sb.append(getPid());
        sb.append("");
        sb.append(Thread.currentThread().getId());
        sb.append("");
        sb.append(module.toString());
        sb.append("");
        if (msgDirection != InterfaceActivityDirection.None) {
            sb.append("");
            sb.append(interfaceActivityDirectionToString(msgDirection));
            sb.append("");
        } // end-if
        sb.append(msgBodyXml);
        sb.append("");

        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, "" + msg + "");
        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, "" + msg + "");
        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("");
        rpcAsXml.append(rpcMsg.getFunctionName());
        rpcAsXml.append("");
        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("");
            rpcAsXml.append(correlationID);
            rpcAsXml.append("");
        } // end-if
        rpcAsXml.append("");
        rpcAsXml.append(rpcMsg.getMessageType());
        rpcAsXml.append("");
        //rpcAsXml.append(newline);

        if (dl == DetailLevel.VERBOSE) {
            OpenRPCMessage openRPCMessage = new OpenRPCMessage(rpcMsg);
            String rpcParamList = openRPCMessage.msgDump();
            String msg = SdlTrace.B64EncodeForXML(rpcParamList);
            rpcAsXml.append("");
            rpcAsXml.append(msg);
            rpcAsXml.append("");
        } // 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("");
        msg.append(marshalledMessage.length);
        msg.append("");
        if (dl == DetailLevel.VERBOSE) {
            msg.append("");
            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("");
        }
        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("");
        protoMsg.append(SdlTraceBase.getProtocolFrameHeaderInfo(packet));
        if (dl == DetailLevel.VERBOSE) {
            if (packet.getPayload() != null && frameDataLength > 0) {
                protoMsg.append("");
                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("");
            }
        }
        protoMsg.append("");
        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("");
        sb.append("");
        sb.append(hdr.getVersion());
        sb.append("");
        sb.append(hdr.isEncrypted());
        sb.append("");
        sb.append(getProtocolFrameType(hdr.getFrameType()));
        sb.append("");
        sb.append(getProtocolSessionType(SessionType.valueOf((byte) hdr.getServiceType())));
        sb.append("");
        sb.append(hdr.getSessionId());
        sb.append("");
        sb.append(hdr.getDataSize());
        sb.append("");

        int frameData = hdr.getFrameInfo();
        if (hdr.getFrameType() == FrameType.Control) {
            sb.append("");
            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("");
        } else if (hdr.getFrameType() == FrameType.Consecutive) {
            sb.append("");
            if (frameData == 0)
                sb.append("lastFrame");
            else
                sb.append(String.format("%02X", frameData));
            sb.append("");
        } else if (hdr.getFrameType() == FrameType.First) {
            int totalSize = BitConverter.intFromByteArray(hdr.getPayload(), 0);
            int numFrames = BitConverter.intFromByteArray(hdr.getPayload(), 4);
            sb.append("").append(totalSize).append("").append(numFrames).append("");
        } else if (hdr.getFrameType() == FrameType.Single) {
            sb.append("");
        }

        sb.append("");

        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("");
            msg.append(preamble);
            msg.append("");
        }
        if (buf != null) {
            msg.append("");
            msg.append(byteLength);
            msg.append("");
            DetailLevel dl = DiagLevel.getLevel(Mod.tran);
            if (dl == DetailLevel.VERBOSE) {
                if (buf != null && byteLength > 0) {
                    msg.append("");
                    String bytesInfo = Mime.base64Encode(buf, offset, byteLength);
                    checkB64(bytesInfo, buf, offset, byteLength);
                    msg.append(bytesInfo);
                    msg.append("");
                }
            }
        }
        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 (!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("" + "" + seqNo
                + "" + "" + dumpReason
                + "");

        write.append("").append(DiagLevel.getLevel(Mod.tran)).append("");
        write.append("").append(DiagLevel.getLevel(Mod.proto)).append("");
        write.append("").append(DiagLevel.getLevel(Mod.mar)).append("");
        write.append("").append(DiagLevel.getLevel(Mod.rpc)).append("");
        write.append("").append(DiagLevel.getLevel(Mod.proxy)).append("");
        write.append("").append(DiagLevel.getLevel(Mod.app)).append("");

        write.append("");
        write.append("");
        return write.toString();
    } // end-method
} // end-class




© 2015 - 2025 Weber Informatics LLC | Privacy Policy