|
35 | 35 | import android.app.Service; |
36 | 36 | import android.content.Context; |
37 | 37 | import android.support.annotation.RestrictTo; |
| 38 | +import android.util.Log; |
38 | 39 |
|
39 | 40 | import com.smartdevicelink.SdlConnection.SdlSession; |
40 | 41 | import com.smartdevicelink.SdlConnection.SdlSession2; |
| 42 | +import com.smartdevicelink.exception.SdlException; |
| 43 | +import com.smartdevicelink.exception.SdlExceptionCause; |
41 | 44 | import com.smartdevicelink.protocol.enums.SessionType; |
42 | 45 | import com.smartdevicelink.proxy.interfaces.ISdlServiceListener; |
| 46 | +import com.smartdevicelink.proxy.rpc.enums.SdlDisconnectedReason; |
43 | 47 | import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType; |
44 | 48 | import com.smartdevicelink.security.SdlSecurityBase; |
45 | 49 | import com.smartdevicelink.streaming.video.VideoStreamingParameters; |
46 | 50 | import com.smartdevicelink.transport.BaseTransportConfig; |
47 | 51 | import com.smartdevicelink.transport.MultiplexTransportConfig; |
48 | 52 | import com.smartdevicelink.transport.TCPTransportConfig; |
| 53 | +import com.smartdevicelink.transport.USBTransportConfig; |
49 | 54 | import com.smartdevicelink.transport.enums.TransportType; |
50 | 55 | import com.smartdevicelink.util.DebugTool; |
51 | 56 |
|
| 57 | +import java.util.ArrayList; |
| 58 | +import java.util.Collections; |
52 | 59 | import java.util.concurrent.Callable; |
53 | 60 | import java.util.concurrent.Executors; |
54 | 61 | import java.util.concurrent.FutureTask; |
@@ -77,13 +84,45 @@ public LifecycleManager(AppConfig appConfig, BaseTransportConfig config, Lifecyc |
77 | 84 | } |
78 | 85 |
|
79 | 86 | @Override |
80 | | - void createSession(BaseTransportConfig config) { |
81 | | - if (config != null && config.getTransportType().equals(TransportType.MULTIPLEX)) { |
82 | | - this.session = new SdlSession2(sdlConnectionListener, (MultiplexTransportConfig) config); |
83 | | - } else if (config != null && config.getTransportType().equals(TransportType.TCP)) { |
84 | | - this.session = new SdlSession2(sdlConnectionListener, (TCPTransportConfig) config); |
| 87 | + void initializeProxy() { |
| 88 | + super.initializeProxy(); |
| 89 | + |
| 90 | + //Handle legacy USB connections |
| 91 | + if (_transportConfig != null && TransportType.USB.equals(_transportConfig.getTransportType())) { |
| 92 | + //A USB transport config was provided |
| 93 | + USBTransportConfig usbTransportConfig = (USBTransportConfig) _transportConfig; |
| 94 | + if (usbTransportConfig.getUsbAccessory() == null) { |
| 95 | + DebugTool.logInfo("Legacy USB transport config was used, but received null for accessory. Attempting to connect with router service"); |
| 96 | + //The accessory was null which means it came from a router service |
| 97 | + MultiplexTransportConfig multiplexTransportConfig = new MultiplexTransportConfig(usbTransportConfig.getUSBContext(), appConfig.getAppID()); |
| 98 | + multiplexTransportConfig.setRequiresHighBandwidth(true); |
| 99 | + multiplexTransportConfig.setSecurityLevel(MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF); |
| 100 | + multiplexTransportConfig.setPrimaryTransports(Collections.singletonList(TransportType.USB)); |
| 101 | + multiplexTransportConfig.setSecondaryTransports(new ArrayList<TransportType>()); |
| 102 | + _transportConfig = multiplexTransportConfig; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (_transportConfig != null && _transportConfig.getTransportType().equals(TransportType.MULTIPLEX)) { |
| 107 | + this.session = new SdlSession2(sdlConnectionListener, (MultiplexTransportConfig) _transportConfig); |
| 108 | + } else if (_transportConfig != null && _transportConfig.getTransportType().equals(TransportType.TCP)) { |
| 109 | + this.session = new SdlSession2(sdlConnectionListener, (TCPTransportConfig) _transportConfig); |
85 | 110 | } else { |
86 | | - this.session = SdlSession.createSession((byte) getProtocolVersion().getMajor(), sdlConnectionListener, config); |
| 111 | + this.session = SdlSession.createSession((byte) getProtocolVersion().getMajor(), sdlConnectionListener, _transportConfig); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void cycleProxy(SdlDisconnectedReason disconnectedReason) { |
| 116 | + cleanProxy(); |
| 117 | + initializeProxy(); |
| 118 | + if(!SdlDisconnectedReason.LEGACY_BLUETOOTH_MODE_ENABLED.equals(disconnectedReason) && !SdlDisconnectedReason.PRIMARY_TRANSPORT_CYCLE_REQUEST.equals(disconnectedReason)){ |
| 119 | + //We don't want to alert higher if we are just cycling for legacy bluetooth |
| 120 | + onClose("Sdl Proxy Cycled", new SdlException("Sdl Proxy Cycled", SdlExceptionCause.SDL_PROXY_CYCLED)); |
| 121 | + } |
| 122 | + try { |
| 123 | + session.startSession(); |
| 124 | + } catch (SdlException e) { |
| 125 | + e.printStackTrace(); |
87 | 126 | } |
88 | 127 | } |
89 | 128 |
|
@@ -113,6 +152,18 @@ void onProtocolSessionStarted(SessionType sessionType) { |
113 | 152 | } |
114 | 153 | } |
115 | 154 |
|
| 155 | + @Override |
| 156 | + void onTransportDisconnected(String info, boolean availablePrimary, BaseTransportConfig transportConfig){ |
| 157 | + super.onTransportDisconnected(info, availablePrimary, transportConfig); |
| 158 | + if (availablePrimary) { |
| 159 | + _transportConfig = transportConfig; |
| 160 | + Log.d(TAG, "notifying RPC session ended, but potential primary transport available"); |
| 161 | + cycleProxy(SdlDisconnectedReason.PRIMARY_TRANSPORT_CYCLE_REQUEST); |
| 162 | + } else { |
| 163 | + onClose(info, null); |
| 164 | + } |
| 165 | + } |
| 166 | + |
116 | 167 | @Override |
117 | 168 | void onProtocolSessionStartedNACKed(SessionType sessionType) { |
118 | 169 | super.onProtocolSessionStartedNACKed(sessionType); |
|
0 commit comments