Added Mike's tester application (not ideal but easy to update)
[profile/ivi/smartdevicelink.git] / SDL_Android / SmartDeviceLinkProxyAndroid / src / com / smartdevicelink / syncConnection / SmartDeviceLinkConnection.java
1 //
2 // Copyright (c) 2013 Ford Motor Company
3 //
4 package com.smartdevicelink.syncConnection;
5
6 import com.smartdevicelink.exception.SmartDeviceLinkException;
7 import com.smartdevicelink.protocol.AbstractProtocol;
8 import com.smartdevicelink.protocol.IProtocolListener;
9 import com.smartdevicelink.protocol.ProtocolMessage;
10 import com.smartdevicelink.protocol.SmartDeviceLinkProtocol;
11 import com.smartdevicelink.protocol.enums.SessionType;
12 import com.smartdevicelink.transport.BTTransport;
13 import com.smartdevicelink.transport.BaseTransportConfig;
14 import com.smartdevicelink.transport.ITransportListener;
15 import com.smartdevicelink.transport.SmartDeviceLinkTransport;
16 import com.smartdevicelink.transport.TCPTransport;
17 import com.smartdevicelink.transport.TCPTransportConfig;
18 import com.smartdevicelink.transport.TransportType;
19
20 public class SmartDeviceLinkConnection implements IProtocolListener, ITransportListener {
21
22         SmartDeviceLinkTransport _transport = null;
23         AbstractProtocol _protocol = null;
24         ISmartDeviceLinkConnectionListener _connectionListener = null;
25
26         // Thread safety locks
27         Object TRANSPORT_REFERENCE_LOCK = new Object();
28         Object PROTOCOL_REFERENCE_LOCK = new Object();
29         
30         public SmartDeviceLinkConnection(ISmartDeviceLinkConnectionListener listener, BaseTransportConfig transportConfig) {
31                 _connectionListener = listener;
32                 
33                 // Initialize the transport
34                 synchronized(TRANSPORT_REFERENCE_LOCK) {
35                         // Ensure transport is null
36                         if (_transport != null) {
37                                 if (_transport.getIsConnected()) {
38                                         _transport.disconnect();
39                                 }
40                                 _transport = null;
41                         }
42                         
43                         if (transportConfig.getTransportType() == TransportType.BLUETOOTH)
44                         {
45                                 _transport = new BTTransport(this);     
46                         }
47                         else if (transportConfig.getTransportType() == TransportType.TCP)
48                         {
49                 _transport = new TCPTransport((TCPTransportConfig) transportConfig, this);
50             }
51                 }
52                 
53                 // Initialize the protocol
54                 synchronized(PROTOCOL_REFERENCE_LOCK) {
55                         // Ensure protocol is null
56                         if (_protocol != null) {
57                                 _protocol = null;
58                         }
59                         
60                         _protocol = new SmartDeviceLinkProtocol(this);
61                 }
62         }
63         
64         public void closeConnection(byte rpcSessionID) {
65                 synchronized(PROTOCOL_REFERENCE_LOCK) {
66                         if (_protocol != null) {
67                                 // If transport is still connected, sent EndProtocolSessionMessage
68                                 if (_transport != null && _transport.getIsConnected()) {
69                                         _protocol.EndProtocolSession(SessionType.RPC, rpcSessionID);
70                                 }
71                                 _protocol = null;
72                         } // end-if
73                 }
74                 
75                 synchronized (TRANSPORT_REFERENCE_LOCK) {
76                         if (_transport != null) {
77                                 _transport.disconnect();
78                         }
79                         _transport = null;
80                 }
81         }
82         
83         public void startTransport() throws SmartDeviceLinkException {
84                 _transport.openConnection();
85         }
86         
87         public Boolean getIsConnected() {
88                 
89                 // If _transport is null, then it can't be connected
90                 if (_transport == null) {
91                         return false;
92                 }
93                 
94                 return _transport.getIsConnected();
95         }
96         
97         public void sendMessage(ProtocolMessage msg) {
98                 _protocol.SendMessage(msg);
99         }
100         
101         @Override
102         public void onTransportBytesReceived(byte[] receivedBytes,
103                         int receivedBytesLength) {
104                 // Send bytes to protocol to be interpreted 
105                 synchronized(PROTOCOL_REFERENCE_LOCK) {
106                         if (_protocol != null) {
107                                 _protocol.HandleReceivedBytes(receivedBytes, receivedBytesLength);
108                         }
109                 }
110         }
111
112         @Override
113         public void onTransportConnected() {
114                 synchronized(PROTOCOL_REFERENCE_LOCK){
115                         if(_protocol != null){
116                                 _protocol.StartProtocolSession(SessionType.RPC);
117                         }
118                 }
119         }
120
121         @Override
122         public void onTransportDisconnected(String info) {
123                 // Pass directly to connection listener
124                 _connectionListener.onTransportDisconnected(info);
125         }
126
127         @Override
128         public void onTransportError(String info, Exception e) {
129                 // Pass directly to connection listener
130                 _connectionListener.onTransportError(info, e);
131         }
132
133         @Override
134         public void onProtocolMessageBytesToSend(byte[] msgBytes, int offset,
135                         int length) {
136                 // Protocol has packaged bytes to send, pass to transport for transmission 
137                 synchronized(TRANSPORT_REFERENCE_LOCK) {
138                         if (_transport != null) {
139                                 _transport.sendBytes(msgBytes, offset, length);
140                         }
141                 }
142         }
143
144         @Override
145         public void onProtocolMessageReceived(ProtocolMessage msg) {
146                 _connectionListener.onProtocolMessageReceived(msg);
147         }
148
149         @Override
150         public void onProtocolSessionStarted(SessionType sessionType,
151                         byte sessionID, byte version, String correlationID) {
152                 _connectionListener.onProtocolSessionStarted(sessionType, sessionID, version, correlationID);
153         }
154
155         @Override
156         public void onProtocolSessionEnded(SessionType sessionType, byte sessionID,
157                         String correlationID) {
158                 _connectionListener.onProtocolSessionEnded(sessionType, sessionID, correlationID);
159         }
160
161         @Override
162         public void onProtocolError(String info, Exception e) {
163                 _connectionListener.onProtocolError(info, e);
164         }
165         
166         public TransportType getCurrentTransportType() {
167                 return _transport.getTransportType();
168         }
169 }