From a74828b828a36fbdd1e07fea91aeccdaf581591d Mon Sep 17 00:00:00 2001 From: "yeonghun.nam" Date: Wed, 14 Dec 2016 15:34:43 +0900 Subject: [PATCH] [IOT-1616] CSM bug fixed in the cloud interface - CSM bug fixed to support the device which does not support CSM Change-Id: Iad570aa5e6776c58842927b95bb86581ab703848 Signed-off-by: yeonghun.nam Reviewed-on: https://gerrit.iotivity.org/gerrit/15585 Tested-by: jenkins-iotivity Reviewed-by: Jee Hyeok Kim (cherry picked from commit 4d94c16126d47892e4c434641ee7ac390430e5d1) Reviewed-on: https://gerrit.iotivity.org/gerrit/16095 Tested-by: jenkins-iotivity --- .../cloud/ciserver/DeviceServerSystem.java | 21 ++++++++------------- .../cloud/base/protocols/coap/CoapSignaling.java | 22 ++++++++++++++-------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java index cc449e0..0a86fc5 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java @@ -438,22 +438,17 @@ public class DeviceServerSystem extends ServerSystem { } @Override - public void channelActive(ChannelHandlerContext ctx) { - Device device = ctx.channel().attr(keyDevice).get(); - mDevicePool.addDevice(device); - device.onConnected(); - // Authenticated device connected - // Actual channel active should decided after authentication. - CoapSignaling signaling = (CoapSignaling) MessageBuilder - .createSignaling(SignalingMethod.CSM); - signaling.setCsmMaxMessageSize(4294967295L); - ctx.writeAndFlush(signaling); - } - - @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { try { if (msg instanceof CoapSignaling) { + if (mCsmMap.get(ctx) == null) { + // In the server, the CSM message is sent to the device + // once + CoapSignaling inicialCsm = (CoapSignaling) MessageBuilder + .createSignaling(SignalingMethod.CSM); + inicialCsm.setCsmMaxMessageSize(4294967295L); + ctx.writeAndFlush(inicialCsm); + } CoapSignaling signaling = (CoapSignaling) msg; switch (signaling.getSignalingMethod()) { case CSM: diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapSignaling.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapSignaling.java index f7e4a2a..0f2de1f 100644 --- a/cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapSignaling.java +++ b/cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapSignaling.java @@ -204,12 +204,16 @@ public class CoapSignaling extends CoapMessage { } public void setCsmServerName(String serverName) { - addOption(1, serverName.getBytes(StandardCharsets.UTF_8)); + if (serverName != null && !serverName.isEmpty()) { + addOption(1, serverName.getBytes(StandardCharsets.UTF_8)); + } } public void setCsmMaxMessageSize(long maxMessageSize) { - ByteBuffer buf = ByteBuffer.wrap(new byte[4]); - max_message_size = buf.putInt(0, (int) maxMessageSize).array(); + if (maxMessageSize != 0) { + ByteBuffer buf = ByteBuffer.wrap(new byte[4]); + max_message_size = buf.putInt(0, (int) maxMessageSize).array(); + } } public void setCsmBlockWiseTransfer(boolean blockWiseTransferOption) { @@ -218,7 +222,7 @@ public class CoapSignaling extends CoapMessage { public String getCsmServerName() { if (server_name == null) - return ""; + return new String(); return new String(server_name, Charset.forName("UTF-8")); } @@ -315,10 +319,12 @@ public class CoapSignaling extends CoapMessage { } private static final long unsignedIntToLong(byte[] b) { - long value = 0; - for (byte data : b) { - value <<= 8; - value += data & 0xFF; + long value = 0L; + if (b != null) { + for (byte data : b) { + value <<= 8; + value += data & 0xFF; + } } return value; } -- 2.7.4