[IOT-1616] CSM bug fixed in the cloud interface 95/16095/6
authoryeonghun.nam <yeonghun.nam@samsung.com>
Wed, 14 Dec 2016 06:34:43 +0000 (15:34 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Thu, 19 Jan 2017 02:03:46 +0000 (02:03 +0000)
 - CSM bug fixed to support the device which does not support CSM

Change-Id: Iad570aa5e6776c58842927b95bb86581ab703848
Signed-off-by: yeonghun.nam <yeonghun.nam@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15585
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
(cherry picked from commit 4d94c16126d47892e4c434641ee7ac390430e5d1)
Reviewed-on: https://gerrit.iotivity.org/gerrit/16095
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java
cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapSignaling.java

index cc449e0..0a86fc5 100644 (file)
@@ -437,23 +437,18 @@ public class DeviceServerSystem extends ServerSystem {
             ctx.fireChannelInactive();
         }
 
-        @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:
index f7e4a2a..0f2de1f 100644 (file)
@@ -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;
     }