summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/main/java/com/smartdevicelink/SdlConnection/SdlSession.java
blob: 58fa8d5767f596b7178e3c7c0fcc85b56d3f4ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
 * 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.SdlConnection;

import android.annotation.SuppressLint;
import android.os.Build;
import android.util.Log;
import android.view.Surface;

import com.smartdevicelink.encoder.SdlEncoder;
import com.smartdevicelink.encoder.VirtualDisplayEncoder;
import com.smartdevicelink.exception.SdlException;
import com.smartdevicelink.protocol.ProtocolMessage;
import com.smartdevicelink.protocol.enums.SessionType;
import com.smartdevicelink.protocol.heartbeat.IHeartbeatMonitor;
import com.smartdevicelink.protocol.heartbeat.IHeartbeatMonitorListener;
import com.smartdevicelink.proxy.LockScreenManager;
import com.smartdevicelink.proxy.RPCRequest;
import com.smartdevicelink.proxy.interfaces.IAudioStreamListener;
import com.smartdevicelink.proxy.interfaces.ISdlServiceListener;
import com.smartdevicelink.proxy.interfaces.IVideoStreamListener;
import com.smartdevicelink.proxy.rpc.VideoStreamingFormat;
import com.smartdevicelink.proxy.rpc.enums.VideoStreamingProtocol;
import com.smartdevicelink.security.ISecurityInitializedListener;
import com.smartdevicelink.security.SdlSecurityBase;
import com.smartdevicelink.streaming.AbstractPacketizer;
import com.smartdevicelink.streaming.IStreamListener;
import com.smartdevicelink.streaming.StreamPacketizer;
import com.smartdevicelink.streaming.StreamRPCPacketizer;
import com.smartdevicelink.streaming.video.RTPH264Packetizer;
import com.smartdevicelink.streaming.video.VideoStreamingParameters;
import com.smartdevicelink.transport.BaseTransportConfig;
import com.smartdevicelink.transport.MultiplexTransport;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.util.Version;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

@Deprecated
public class SdlSession implements ISdlConnectionListener, IHeartbeatMonitorListener, IStreamListener, ISecurityInitializedListener {

   private static final String TAG = "SdlSession";

    protected final static int BUFF_READ_SIZE = 1024;

	private static CopyOnWriteArrayList<SdlConnection> shareConnections = new CopyOnWriteArrayList<SdlConnection>();

	private byte wiproProcolVer;

    protected BaseTransportConfig transportConfig;
    protected ISdlConnectionListener sessionListener;
	protected LockScreenManager lockScreenMan  = new LockScreenManager();
	protected SdlSecurityBase sdlSecurity = null;
	protected VideoStreamingParameters desiredVideoParams = null;
	protected VideoStreamingParameters acceptedVideoParams = null;

	protected byte sessionId;
	protected int sessionHashId = 0;
	protected HashMap<SessionType, CopyOnWriteArrayList<ISdlServiceListener>> serviceListeners;
	protected CopyOnWriteArrayList<SessionType> encryptedServices = new CopyOnWriteArrayList<SessionType>();


	SdlConnection _sdlConnection = null;

	IHeartbeatMonitor _outgoingHeartbeatMonitor = null;
	IHeartbeatMonitor _incomingHeartbeatMonitor = null;

    StreamRPCPacketizer mRPCPacketizer = null;
    AbstractPacketizer mVideoPacketizer = null;
    StreamPacketizer mAudioPacketizer = null;
    SdlEncoder mSdlEncoder = null;
    VirtualDisplayEncoder virtualDisplayEncoder = null;

    public static SdlSession createSession(byte wiproVersion, ISdlConnectionListener listener, BaseTransportConfig btConfig) {

        SdlSession session =  new SdlSession();
        session.wiproProcolVer = wiproVersion;
        session.sessionListener = listener;
        session.transportConfig = btConfig;

        return session;
    }

    public BaseTransportConfig getTransportConfig() {
        return this.transportConfig;
    }

    public LockScreenManager getLockScreenMan() {
        return lockScreenMan;
    }


    public IHeartbeatMonitor getOutgoingHeartbeatMonitor() {
        return _outgoingHeartbeatMonitor;
    }

    public IHeartbeatMonitor getIncomingHeartbeatMonitor() {
        return _incomingHeartbeatMonitor;
    }

    public void setOutgoingHeartbeatMonitor(IHeartbeatMonitor outgoingHeartbeatMonitor) {
        this._outgoingHeartbeatMonitor = outgoingHeartbeatMonitor;
        _outgoingHeartbeatMonitor.setListener(this);
    }

    public void setIncomingHeartbeatMonitor(IHeartbeatMonitor incomingHeartbeatMonitor) {
        this._incomingHeartbeatMonitor = incomingHeartbeatMonitor;
        _incomingHeartbeatMonitor.setListener(this);
    }

    public int getSessionHashId() {
        return this.sessionHashId;
    }

    public byte getSessionId() {
        return this.sessionId;
    }

    public SdlConnection getSdlConnection() {
        return this._sdlConnection;
    }

    public int getMtu(){
        if(this._sdlConnection!=null){
            return this._sdlConnection.getWiProProtocol().getMtu();
        }else{
            return 0;
        }
    }

    public long getMtu(SessionType type) {
        if (this._sdlConnection != null) {
            return this._sdlConnection.getWiProProtocol().getMtu(type);
        } else {
            return 0;
        }
    }

    public void close() {
        if (sdlSecurity != null)
        {
            sdlSecurity.resetParams();
            sdlSecurity.shutDown();
        }

        if (_sdlConnection != null) { //sessionId == 0 means session is not started.
            //_sdlConnection.unregisterSession(this);

            if (_sdlConnection.getRegisterCount() == 0) {
                shareConnections.remove(_sdlConnection);
            }

            _sdlConnection = null;
        }
    }

    public void resetSession(){

    }

    public void startStream(InputStream is, SessionType sType, byte rpcSessionID) throws IOException {
        if (sType.equals(SessionType.NAV))
        {
            // protocol is fixed to RAW
            StreamPacketizer packetizer = new StreamPacketizer(this, is, sType, rpcSessionID, this);
            packetizer.sdlConnection = this.getSdlConnection();
            mVideoPacketizer = packetizer;
            mVideoPacketizer.start();
        }
        else if (sType.equals(SessionType.PCM))
        {
            mAudioPacketizer = new StreamPacketizer(this, is, sType, rpcSessionID, this);
            mAudioPacketizer.sdlConnection = this.getSdlConnection();
            mAudioPacketizer.start();
        }
    }

    @SuppressLint("NewApi")
    public OutputStream startStream(SessionType sType, byte rpcSessionID) throws IOException {
        OutputStream os = new PipedOutputStream();
        InputStream is = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            is = new PipedInputStream((PipedOutputStream) os, BUFF_READ_SIZE);
        } else {
            is = new PipedInputStream((PipedOutputStream) os);
        }
        if (sType.equals(SessionType.NAV))
        {
            // protocol is fixed to RAW
            StreamPacketizer packetizer = new StreamPacketizer(this, is, sType, rpcSessionID, this);
            packetizer.sdlConnection = this.getSdlConnection();
            mVideoPacketizer = packetizer;
            mVideoPacketizer.start();
        }
        else if (sType.equals(SessionType.PCM))
        {
            mAudioPacketizer = new StreamPacketizer(this, is, sType, rpcSessionID, this);
            mAudioPacketizer.sdlConnection = this.getSdlConnection();
            mAudioPacketizer.start();
        }
        else
        {
            os.close();
            is.close();
            return null;
        }
        return os;
    }

    public IVideoStreamListener startVideoStream() {
        byte rpcSessionID = getSessionId();
        VideoStreamingProtocol protocol = getAcceptedProtocol();
        try {
            switch (protocol) {
                case RAW: {
                    StreamPacketizer packetizer = new StreamPacketizer(this, null, SessionType.NAV, rpcSessionID, this);
                    packetizer.sdlConnection = this.getSdlConnection();
                    mVideoPacketizer = packetizer;
                    mVideoPacketizer.start();
                    return packetizer;
                }
                case RTP: {
                    RTPH264Packetizer packetizer = new RTPH264Packetizer(this, SessionType.NAV, rpcSessionID, this);
                    mVideoPacketizer = packetizer;
                    mVideoPacketizer.start();
                    return packetizer;
                }
                default:
                    Log.e(TAG, "Protocol " + protocol + " is not supported.");
                    return null;
            }
        } catch (IOException e) {
            return null;
        }
    }

    public IAudioStreamListener startAudioStream() {
        byte rpcSessionID = getSessionId();
        try {
            StreamPacketizer packetizer = new StreamPacketizer(this, null, SessionType.PCM, rpcSessionID, this);
            packetizer.sdlConnection = this.getSdlConnection();
            mAudioPacketizer = packetizer;
            mAudioPacketizer.start();
            return packetizer;
        } catch (IOException e) {
            return null;
        }
    }

    public void startRPCStream(InputStream is, RPCRequest request, SessionType sType, byte rpcSessionID, byte wiproVersion) {
        try {
            mRPCPacketizer = new StreamRPCPacketizer(null, this, is, request, sType, rpcSessionID, wiproVersion, 0, this);
            mRPCPacketizer.start();
        } catch (Exception e) {
            Log.e(TAG, "Unable to start streaming:" + e.toString());
        }
    }

    public OutputStream startRPCStream(RPCRequest request, SessionType sType, byte rpcSessionID, byte wiproVersion) {
        try {
            OutputStream os = new PipedOutputStream();
            InputStream is = new PipedInputStream((PipedOutputStream) os);
            mRPCPacketizer = new StreamRPCPacketizer(null, this, is, request, sType, rpcSessionID, wiproVersion, 0, this);
            mRPCPacketizer.start();
            return os;
        } catch (Exception e) {
            Log.e(TAG, "Unable to start streaming:" + e.toString());
        }
        return null;
    }

    public void pauseRPCStream()
    {
        if (mRPCPacketizer != null)
        {
            mRPCPacketizer.pause();
        }
    }

    public void resumeRPCStream()
    {
        if (mRPCPacketizer != null)
        {
            mRPCPacketizer.resume();
        }
    }

    public void stopRPCStream()
    {
        if (mRPCPacketizer != null)
        {
            mRPCPacketizer.stop();
        }
    }

    public boolean stopAudioStream()
    {
        if (mAudioPacketizer != null)
        {
            mAudioPacketizer.stop();
            return true;
        }
        return false;
    }

    public boolean stopVideoStream()
    {
        if (mVideoPacketizer != null)
        {
            mVideoPacketizer.stop();
            return true;
        }
        return false;
    }

    public boolean pauseAudioStream()
    {
        if (mAudioPacketizer != null)
        {
            mAudioPacketizer.pause();
            return true;
        }
        return false;
    }

    public boolean pauseVideoStream()
    {
        if (mVideoPacketizer != null)
        {
            mVideoPacketizer.pause();
            return true;
        }
        return false;
    }

    public boolean resumeAudioStream()
    {
        if (mAudioPacketizer != null)
        {
            mAudioPacketizer.resume();
            return true;
        }
        return false;
    }

    public boolean resumeVideoStream()
    {
        if (mVideoPacketizer != null)
        {
            mVideoPacketizer.resume();
            return true;
        }
        return false;
    }

    public Surface createOpenGLInputSurface(int frameRate, int iFrameInterval, int width,
                                            int height, int bitrate, SessionType sType, byte rpcSessionID) {
        IVideoStreamListener encoderListener = startVideoStream();
        if (encoderListener == null) {
            return null;
        }

        mSdlEncoder = new SdlEncoder();
        mSdlEncoder.setFrameRate(frameRate);
        mSdlEncoder.setFrameInterval(iFrameInterval);
        mSdlEncoder.setFrameWidth(width);
        mSdlEncoder.setFrameHeight(height);
        mSdlEncoder.setBitrate(bitrate);
        mSdlEncoder.setOutputListener(encoderListener);
        return mSdlEncoder.prepareEncoder();
    }

    public void startEncoder () {
        if(mSdlEncoder != null) {
            mSdlEncoder.startEncoder();
        }
    }

    public void releaseEncoder() {
        if(mSdlEncoder != null) {
            mSdlEncoder.releaseEncoder();
        }
    }

    public void drainEncoder(boolean endOfStream) {
        if(mSdlEncoder != null) {
            mSdlEncoder.drainEncoder(endOfStream);
        }
    }

    @Override
    public void sendStreamPacket(ProtocolMessage pm) {
        sendMessage(pm);
    }

    public void setSdlSecurity(SdlSecurityBase sec) {
        sdlSecurity = sec;
    }

    public SdlSecurityBase getSdlSecurity() {
        return sdlSecurity;
    }

    public void startService (SessionType serviceType, byte sessionID, boolean isEncrypted) {
        if (_sdlConnection == null)
            return;

        if (isEncrypted)
        {
            if (sdlSecurity != null)
            {
                List<SessionType> serviceList = sdlSecurity.getServiceList();
                if (!serviceList.contains(serviceType))
                    serviceList.add(serviceType);

                sdlSecurity.initialize();
            }
            return;
        }
        _sdlConnection.startService(serviceType, sessionID, isEncrypted);
    }

    public void endService (SessionType serviceType, byte sessionID) {
        if (_sdlConnection == null)
            return;
        _sdlConnection.endService(serviceType, sessionID);
    }

    protected void processControlService(ProtocolMessage msg) {
        if (sdlSecurity == null)
            return;
        int ilen = msg.getData().length - 12;
        byte[] data = new byte[ilen];
        System.arraycopy(msg.getData(), 12, data, 0, ilen);

        byte[] dataToRead = new byte[4096];

        Integer iNumBytes = sdlSecurity.runHandshake(data, dataToRead);

        if (iNumBytes == null || iNumBytes <= 0)
            return;

        byte[] returnBytes = new byte[iNumBytes];
        System.arraycopy(dataToRead, 0, returnBytes, 0, iNumBytes);
        ProtocolMessage protocolMessage = new ProtocolMessage();
        protocolMessage.setSessionType(SessionType.CONTROL);
        protocolMessage.setData(returnBytes);
        protocolMessage.setFunctionID(0x01);
        protocolMessage.setVersion(wiproProcolVer);
        protocolMessage.setSessionID(getSessionId());

        //sdlSecurity.hs();

        sendMessage(protocolMessage);
    }

    public String getBroadcastComment(BaseTransportConfig myTransport) {
        SdlConnection connection = null;
        if (myTransport.shareConnection()) {
            connection = findTheProperConnection(myTransport);
        } else {
            connection = this._sdlConnection;
        }

        if (connection != null)
            return connection.getBroadcastComment();

        return "";
    }


    public void startSession() throws SdlException {
        SdlConnection connection = null;
        if (this.transportConfig.shareConnection()) {
            connection = findTheProperConnection(this.transportConfig);

            if (connection == null) {
                connection = new SdlConnection(this.transportConfig);
                shareConnections.add(connection);
            }
        } else {
            connection = new SdlConnection(this.transportConfig);
        }

        this._sdlConnection = connection;
        connection.registerSession(this); //Handshake will start when register.
    }

    protected void initialiseSession() {
        if (_outgoingHeartbeatMonitor != null) {
            _outgoingHeartbeatMonitor.start();
        }
        if (_incomingHeartbeatMonitor != null) {
            _incomingHeartbeatMonitor.start();
        }
    }

    public void sendMessage(ProtocolMessage msg) {
        if (_sdlConnection == null)
            return;
        _sdlConnection.sendMessage(msg);
    }

    public TransportType getCurrentTransportType() {
        if (_sdlConnection == null)
            return null;
        return _sdlConnection.getCurrentTransportType();
    }

    public boolean getIsConnected() {
        if (_sdlConnection == null)
            return false;
        return _sdlConnection != null && _sdlConnection.getIsConnected();
    }

    public boolean isServiceProtected(SessionType sType) {
        return encryptedServices.contains(sType);
    }

    @Override
    public void onTransportDisconnected(String info) {
        this.sessionListener.onTransportDisconnected(info);
    }

    @Override
    public void onTransportDisconnected(String info, boolean availablePrimary, BaseTransportConfig transportConfig) {
        this.sessionListener.onTransportDisconnected(info);
    }

    @Override
    public void onTransportError(String info, Exception e) {
        this.sessionListener.onTransportError(info, e);
    }

    @Override
    public void onProtocolMessageReceived(ProtocolMessage msg) {
        if (msg.getSessionType().equals(SessionType.CONTROL)) {
            processControlService(msg);
            return;
        }

        this.sessionListener.onProtocolMessageReceived(msg);
    }

    @Override
    public void onHeartbeatTimedOut(byte sessionID) {
        this.sessionListener.onHeartbeatTimedOut(sessionID);

    }


    @Override
    public void onProtocolSessionStarted(SessionType sessionType,
                                         byte sessionID, byte version, String correlationID, int hashID, boolean isEncrypted) {
        this.sessionId = sessionID;
        lockScreenMan.setSessionID(sessionID);
        if (sessionType.eq(SessionType.RPC)){
            sessionHashId = hashID;
            wiproProcolVer = version;
        }
        if (isEncrypted)
            encryptedServices.addIfAbsent(sessionType);
        this.sessionListener.onProtocolSessionStarted(sessionType, sessionID, version, correlationID, hashID, isEncrypted);
        if(serviceListeners != null && serviceListeners.containsKey(sessionType)){
            CopyOnWriteArrayList<ISdlServiceListener> listeners = serviceListeners.get(sessionType);
            for(ISdlServiceListener listener:listeners){
                listener.onServiceStarted(this, sessionType, isEncrypted);
            }
        }
        //if (version == 3)
        initialiseSession();

    }

    @Override
    public void onProtocolSessionEnded(SessionType sessionType, byte sessionID,
                                       String correlationID) {
        this.sessionListener.onProtocolSessionEnded(sessionType, sessionID, correlationID);
        if(serviceListeners != null && serviceListeners.containsKey(sessionType)){
            CopyOnWriteArrayList<ISdlServiceListener> listeners = serviceListeners.get(sessionType);
            for(ISdlServiceListener listener:listeners){
                listener.onServiceEnded(this, sessionType);
            }
        }
        encryptedServices.remove(sessionType);
    }

    @Override
    public void onProtocolError(String info, Exception e) {
        this.sessionListener.onProtocolError(info, e);
    }

    @Override
    public void sendHeartbeat(IHeartbeatMonitor monitor) {
        Log.d(TAG, "Asked to send heartbeat");
        if (_sdlConnection != null)
            _sdlConnection.sendHeartbeat(this);
    }

    @Override
    public void heartbeatTimedOut(IHeartbeatMonitor monitor) {
        if (_sdlConnection != null)
            _sdlConnection._connectionListener.onHeartbeatTimedOut(this.sessionId);
        close();
    }

    private static SdlConnection findTheProperConnection(BaseTransportConfig config) {
        SdlConnection connection = null;

        int minCount = 0;
        for (SdlConnection c : shareConnections) {
            if (c.getCurrentTransportType() == config.getTransportType()) {
                if (minCount == 0 || minCount >= c.getRegisterCount()) {
                    connection = c;
                    minCount = c.getRegisterCount();
                }
            }
        }

        return connection;
    }

    @Override
    public void onProtocolSessionStartedNACKed(SessionType sessionType,
                                               byte sessionID, byte version, String correlationID, List<String> rejectedParams) {
        this.sessionListener.onProtocolSessionStartedNACKed(sessionType,
                sessionID, version, correlationID, rejectedParams);
        if(serviceListeners != null && serviceListeners.containsKey(sessionType)){
            CopyOnWriteArrayList<ISdlServiceListener> listeners = serviceListeners.get(sessionType);
            for(ISdlServiceListener listener:listeners){
                listener.onServiceError(this, sessionType, "Start "+ sessionType.toString() +" Service NACK'ed");
            }
        }
    }

    @Override
    public void onProtocolSessionEndedNACKed(SessionType sessionType,
                                             byte sessionID, String correlationID) {
        this.sessionListener.onProtocolSessionEndedNACKed(sessionType, sessionID, correlationID);
        if(serviceListeners != null && serviceListeners.containsKey(sessionType)){
            CopyOnWriteArrayList<ISdlServiceListener> listeners = serviceListeners.get(sessionType);
            for(ISdlServiceListener listener:listeners){
                listener.onServiceError(this, sessionType, "End "+ sessionType.toString() +" Service NACK'ed");
            }
        }
    }

    @Override
    public void onProtocolServiceDataACK(SessionType sessionType, int dataSize, byte sessionID) {
        this.sessionListener.onProtocolServiceDataACK(sessionType, dataSize, sessionID);
    }

    @Override
    public void onAuthTokenReceived(String authToken, byte sessionId) {
        this.sessionListener.onAuthTokenReceived(authToken, sessionId);
    }

    @Override
    public void onSecurityInitialized() {

        if (_sdlConnection != null && sdlSecurity != null)
        {
            List<SessionType> list = sdlSecurity.getServiceList();

            SessionType service;
            ListIterator<SessionType> iter = list.listIterator();

            while (iter.hasNext()) {
                service = iter.next();

                if (service != null)
                    _sdlConnection.startService(service, getSessionId(), true);

                iter.remove();
            }
        }
    }

    public void clearConnection(){
        _sdlConnection = null;
    }

    public void checkForOpenMultiplexConnection(SdlConnection connection){
        removeConnection(connection);
        connection.unregisterSession(this);
        _sdlConnection = null;
        for (SdlConnection c : shareConnections) {
            if (c.getCurrentTransportType() == TransportType.MULTIPLEX) {
                if(c.getIsConnected() || ((MultiplexTransport)c._transport).isPendingConnected()){
                    _sdlConnection = c;
                    try {
                        _sdlConnection.registerSession(this);//Handshake will start when register.
                    } catch (SdlException e) {
                        e.printStackTrace();
                    }
                    return;
                }

            }
        }
    }
    public static boolean removeConnection(SdlConnection connection){
        return shareConnections.remove(connection);
    }

    public void addServiceListener(SessionType serviceType, ISdlServiceListener sdlServiceListener){
        if(serviceListeners == null){
            serviceListeners = new HashMap<>();
        }
        if(serviceType != null && sdlServiceListener != null){
            if(!serviceListeners.containsKey(serviceType)){
                serviceListeners.put(serviceType,new CopyOnWriteArrayList<ISdlServiceListener>());
            }
            serviceListeners.get(serviceType).add(sdlServiceListener);
        }
    }

    public boolean removeServiceListener(SessionType serviceType, ISdlServiceListener sdlServiceListener){
        if(serviceListeners!= null && serviceType != null && sdlServiceListener != null && serviceListeners.containsKey(serviceType)){
            return serviceListeners.get(serviceType).remove(sdlServiceListener);
        }
        return false;
    }


    public HashMap<SessionType, CopyOnWriteArrayList<ISdlServiceListener>> getServiceListeners(){
        return serviceListeners;
    }

    public void setDesiredVideoParams(VideoStreamingParameters params){
        this.desiredVideoParams = params;
    }

    /**
     * Returns the currently set desired video streaming parameters. If there haven't been any set,
     * the default options will be returned and set for this instance.
     * @return
     */
    public VideoStreamingParameters getDesiredVideoParams(){
        if(desiredVideoParams == null){
            desiredVideoParams = new VideoStreamingParameters();
        }
        return desiredVideoParams;
    }

    public void setAcceptedVideoParams(VideoStreamingParameters params){
        this.acceptedVideoParams = params;
    }

    public VideoStreamingParameters getAcceptedVideoParams(){
        return acceptedVideoParams;
    }

    private VideoStreamingProtocol getAcceptedProtocol() {
        // acquire default protocol (RAW)
        VideoStreamingProtocol protocol = new VideoStreamingParameters().getFormat().getProtocol();

        if (acceptedVideoParams != null) {
            VideoStreamingFormat format = acceptedVideoParams.getFormat();
            if (format != null && format.getProtocol() != null) {
                protocol = format.getProtocol();
            }
        }

        return protocol;
    }

    public Version getProtocolVersion(){
        //Since this session version never supported a minor protocol version this should be fine
        return new Version(wiproProcolVer,0,0);
    }

    /**
     * Check to see if a transport is available to start/use the supplied service.
     * @param sessionType the session that should be checked for transport availability
     * @return true if there is either a supported
     *         transport currently connected or a transport is
     *         available to connect with for the supplied service type.
     *         <br>false if there is no
     *         transport connected to support the service type in question and
     *          no possibility in the foreseeable future.
     */
    public boolean isTransportForServiceAvailable(SessionType sessionType){
        return _sdlConnection!= null
                && _sdlConnection._transport!= null
                && _sdlConnection._transport.getIsConnected()
                && ((sessionType == SessionType.RPC || sessionType == SessionType.CONTROL || sessionType == SessionType.BULK_DATA ) //If this is a service that can run on any transport just return true
                    || (_sdlConnection._transport.getTransportType() == TransportType.USB || _sdlConnection._transport.getTransportType() == TransportType.TCP));
    }


}