Skip to content

Commit 7024b2a

Browse files
committed
Align to iOS
1 parent b929655 commit 7024b2a

3 files changed

Lines changed: 39 additions & 49 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testCorrectHeaderAssembly() {
6868
dummyBqh.setCorrelationID(3);
6969
dummyBqh.setJsonData(new byte[0]);
7070

71-
byte[] assembledHeader = dummyBqh.assembleSecurityQueryPayload(0);
71+
byte[] assembledHeader = dummyBqh.assembleBinaryData();
7272
assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0]));
7373
byte[] queryIDFromHeader = new byte[3];
7474
System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3);
@@ -81,7 +81,7 @@ public void testCorrectHeaderAssembly() {
8181
public void testAssemblyAndParse() {
8282
SecurityQueryPayload bqh = createDummyBqh();
8383

84-
byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0);
84+
byte[] bqhBytes = bqh.assembleBinaryData();
8585
assertNotNull(bqhBytes);
8686

8787
SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes);
@@ -99,7 +99,7 @@ public void testAssemblyAndParse() {
9999
public void testCorruptHeader() {
100100
SecurityQueryPayload bqh = createDummyBqh();
101101

102-
byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0);
102+
byte[] bqhBytes = bqh.assembleBinaryData();
103103

104104
assertNotNull(safeParse(bqhBytes));
105105

base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class SecurityQueryPayload {
1616
private SecurityQueryID _securityQueryID;
1717
private int _correlationID;
1818
private int _jsonSize;
19-
private SecurityQueryErrorCode _errorCode;
19+
private int _bulkDataSize;
2020

2121
private byte[] _jsonData = null;
2222
private byte[] _bulkData = null;
@@ -51,11 +51,6 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) {
5151
int _jsonSize = BitConverter.intFromByteArray(binHeader, 8);
5252
msg.setJsonSize(_jsonSize);
5353

54-
//If we get an error message we want the error code from the last 8 bits
55-
if (msg.getQueryType() == SecurityQueryType.NOTIFICATION && msg.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR) {
56-
msg.setErrorCode(SecurityQueryErrorCode.valueOf(binHeader[binHeader.length - 1]));
57-
}
58-
5954
try {
6055
//Get the JsonData after the header (after 96 bits) based on the jsonData size
6156
if (_jsonSize > 0 && _jsonSize <= (binHeader.length - SECURITY_QUERY_HEADER_SIZE)) {
@@ -84,40 +79,28 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) {
8479
return msg;
8580
}
8681

87-
public byte[] assembleSecurityQueryPayload(int payloadSize) {
88-
byte[] payLoad = new byte[SECURITY_QUERY_HEADER_SIZE];
89-
if (_securityQueryID == SecurityQueryID.SEND_INTERNAL_ERROR && _securityQueryType == SecurityQueryType.NOTIFICATION) {
90-
payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize + 1];
91-
System.arraycopy(_jsonData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, _jsonSize);
92-
byte[] errorCode = new byte[1];
93-
if (this._errorCode != null) {
94-
errorCode[0] = _errorCode.getValue();
95-
} else {
96-
errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue();
97-
}
98-
System.arraycopy(errorCode, 0, payLoad, payLoad.length - 1, 1);
99-
} else if (_securityQueryID == SecurityQueryID.SEND_HANDSHAKE_DATA && _securityQueryType == SecurityQueryType.RESPONSE) {
100-
payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize];
101-
System.arraycopy(_bulkData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, payloadSize);
102-
}
103-
104-
System.arraycopy(assembleHeaderBytes(), 0, payLoad, 0, SECURITY_QUERY_HEADER_SIZE);
105-
106-
return payLoad;
107-
}
108-
109-
private byte[] assembleHeaderBytes() {
82+
public byte[] assembleBinaryData() {
11083
// From the properties, create a data buffer
11184
// Query Type - first 8 bits
11285
// Query ID - next 24 bits
11386
// Sequence Number - next 32 bits
11487
// JSON size - next 32 bits
115-
byte[] ret = new byte[SECURITY_QUERY_HEADER_SIZE];
116-
ret[0] = _securityQueryType.getValue();
117-
System.arraycopy(_securityQueryID.getValue(), 0, ret, 1, 3);
118-
System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4);
119-
System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4);
120-
return ret;
88+
byte[] header = new byte[SECURITY_QUERY_HEADER_SIZE];
89+
header[0] = _securityQueryType.getValue();
90+
System.arraycopy(_securityQueryID.getValue(), 0, header, 1, 3);
91+
System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, header, 4, 4);
92+
System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, header, 8, 4);
93+
94+
int size = _jsonSize + _bulkDataSize + SECURITY_QUERY_HEADER_SIZE;
95+
byte[] dataOut = new byte[size];
96+
System.arraycopy(header, 0, dataOut, 0, SECURITY_QUERY_HEADER_SIZE);
97+
if (_jsonData != null) {
98+
System.arraycopy(_jsonData, 0, dataOut, SECURITY_QUERY_HEADER_SIZE, _jsonSize);
99+
}
100+
if (_bulkData != null) {
101+
System.arraycopy(_bulkData, 0, dataOut, SECURITY_QUERY_HEADER_SIZE + _jsonSize, _bulkDataSize);
102+
}
103+
return dataOut;
121104
}
122105

123106
public SecurityQueryType getQueryType() {
@@ -152,12 +135,12 @@ private void setJsonSize(int _jsonSize) {
152135
this._jsonSize = _jsonSize;
153136
}
154137

155-
public SecurityQueryErrorCode getErrorCode() {
156-
return _errorCode;
138+
public int getBulkDataSize() {
139+
return _bulkDataSize;
157140
}
158141

159-
public void setErrorCode(SecurityQueryErrorCode _errorCode) {
160-
this._errorCode = _errorCode;
142+
private void setBulkDataSize(int _bulkDataSize) {
143+
this._bulkDataSize = _bulkDataSize;
161144
}
162145

163146
public byte[] getJsonData() {
@@ -180,6 +163,13 @@ public byte[] getBulkData() {
180163
}
181164

182165
public void setBulkData(byte[] _bulkData) {
183-
this._bulkData = _bulkData;
166+
if(_bulkData == null) {
167+
this._bulkDataSize = 0;
168+
this._bulkData = null;
169+
return;
170+
}
171+
this._bulkDataSize = _bulkData.length;
172+
this._bulkData = new byte[this._bulkDataSize];
173+
System.arraycopy(_bulkData, 0, this._bulkData, 0, _bulkDataSize);
184174
}
185175
}

base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ protected void processControlService(ProtocolMessage msg) {
221221
// If the query is of type `Notification` and the id represents a client internal error, we abort the response message and the encryptionManager will not be in state ready.
222222
if (receivedHeader.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR
223223
&& receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION) {
224-
if (receivedHeader.getErrorCode() != null) {
225-
DebugTool.logError(TAG, "Security Query module internal error: " + receivedHeader.getErrorCode().getName());
224+
if (receivedHeader.getBulkData() != null && receivedHeader.getBulkDataSize() == 1) {
225+
DebugTool.logError(TAG, "Security Query module internal error: " + SecurityQueryErrorCode.valueOf(receivedHeader.getBulkData()[0]).getName());
226226
} else {
227227
DebugTool.logError(TAG, "Security Query module error: No information provided");
228228
}
@@ -263,17 +263,17 @@ protected void processControlService(ProtocolMessage msg) {
263263
jsonData = new byte[0];
264264
}
265265
responseHeader.setJsonData(jsonData);
266-
responseHeader.setBulkData(null);
267-
responseHeader.setErrorCode(SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR);
268-
returnBytes = responseHeader.assembleSecurityQueryPayload(responseHeader.getJsonSize());
266+
byte[] errorCode = new byte[1];
267+
errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue();
268+
responseHeader.setBulkData(errorCode);
269269
} else {
270270
responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
271271
responseHeader.setQueryType(SecurityQueryType.RESPONSE);
272272
responseHeader.setCorrelationID(msg.getCorrID());
273273
responseHeader.setBulkData(dataToRead);
274274
responseHeader.setJsonData(null);
275-
returnBytes = responseHeader.assembleSecurityQueryPayload(iNumBytes);
276275
}
276+
returnBytes = responseHeader.assembleBinaryData();
277277

278278
ProtocolMessage protocolMessage = new ProtocolMessage();
279279
protocolMessage.setSessionType(SessionType.CONTROL);

0 commit comments

Comments
 (0)