Quellcode durchsuchen

Internal pairwised device discovery & caching logic was changed to keep outdated devices with timestamp. There isn no clear list of pairwised devices except clicking refresh button on BridgeActivity.

Trishia vor 4 Jahren
Ursprung
Commit
25beb3f183

BIN
app/libs/iotivity-lite.jar


+ 3 - 3
app/src/main/aidl/org/iotivity/bridge/homeserver/IBridgeService.aidl

@@ -35,7 +35,7 @@ interface IBridgeService {
     void unregisterBridgeServiceCallback(IBridgeServiceCallback cb);
 
     /**
-     * retrieve all on-line device list in BridgeService
+     * retrieve all on-line/off-line device list in BridgeService
      * @return devices list data with json array format as String
      * [{"name":"Light Switch","di":"15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","piid":"2b32e152-3756-4fb6-b3f2-d8db7aafe39f","icv":"ocf.2.0.0","dmv":"ocf.res.1.3.0,ocf.sh.1.3.0"},
      * {"name":"Television","di":"4e7bf95f-1658-4a43-7f78-44522db5255f","piid":"2cd8e585-ef21-4fd1-7958-6ca7e54adcce","icv":"ocf.2.0.0","dmv":"ocf.res.1.3.0, ocf.sh.1.3.0"}]
@@ -46,8 +46,8 @@ interface IBridgeService {
      * retrieve all resources of the pair-wised deivce in BridgeService
      * @param device_uuid : unique identity of ocf device (eg. 15ba3c25-8cdd-41f8-6f9e-fe49105dce0a)
      * @return resource list data with json array format as String
-     * [{"anchor":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","uri”:”/light","rt":"[oic.r.switch.binary]","eps":"[coaps://192.168.0.3:43370]","if":"BASELINE | RW","pm":"DISCOVERABLE | OBSERVABLE"},
-     * {"anchor":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","uri":"/dimming","rt":"[oic.r.light.dimming]","eps":"[coaps://192.168.0.3:43370]","if":"BASELINE | RW","pm":"DISCOVERABLE | OBSERVABLE"}]
+     * [{"anchor":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","uri”:”/light","rt":"[oic.r.switch.binary]","eps":"[coaps://192.168.0.3:43370]","if":"BASELINE | RW","pm":"DISCOVERABLE | OBSERVABLE", "status":"online"},
+     * {"anchor":"ocf://15ba3c25-8cdd-41f8-6f9e-fe49105dce0a","uri":"/dimming","rt":"[oic.r.light.dimming]","eps":"[coaps://192.168.0.3:43370]","if":"BASELINE | RW","pm":"DISCOVERABLE | OBSERVABLE", "status":"offline"}]
      */
     String retrieveAllResources(in String device_uuid) ;
 

+ 63 - 8
app/src/main/java/org/iotivity/bridge/homeserver/BridgeServer.java

@@ -1,5 +1,6 @@
 package org.iotivity.bridge.homeserver;
 
+import android.provider.Settings;
 import android.util.Log;
 
 import org.iotivity.OCBridge;
@@ -43,6 +44,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Properties;
 
 public class BridgeServer {
@@ -63,7 +66,7 @@ public class BridgeServer {
     private Bridge bridge = null ;
     private Light light = null ;
 
-    private ArrayList<OcRemoteDevice> pairwised_devices = new ArrayList<OcRemoteDevice>();
+    private HashMap<OcRemoteDevice, Long> pairwised_devices = new HashMap<OcRemoteDevice, Long>();
     private ArrayList<OCUuid> pairwised_credential_uuids = new ArrayList<OCUuid>();
 
     public BridgeServer(BridgeService service) {
@@ -279,7 +282,9 @@ public class BridgeServer {
     public VirtualOCFDevice addVirtualOCFDevice(String device_name, String virtual_di, String device_type, String eco_name) {
 
         // Add Virtual Device for Light
+        Log.d(TAG, "virtual_di = " + virtual_di);
         OCUuid uuid = OCUuidUtil.stringToUuid(virtual_di);
+        Log.d(TAG, "virtual_di = " + OCUuidUtil.uuidToString(uuid));
         int vod_index = OCBridge.addVirtualDevice(uuid.getId(),
                 eco_name, "/oic/d", device_type,  // "oic.d.virtual" automatically added by internal api implements
                 device_name, "ocf.2.2.0", "ocf.res.1.3.0,ocf.sh.1.3.0");
@@ -570,7 +575,12 @@ public class BridgeServer {
 
     /* These APIs are related to online pairwised devices */
     public ArrayList<OcRemoteDevice> getPairwisedDevices() {
-        return pairwised_devices;
+        ArrayList<OcRemoteDevice> pDevices = new ArrayList<>();
+        Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
+        while(it.hasNext()){
+            pDevices.add(it.next());
+        }
+        return pDevices.size() > 0 ? pDevices : null;
     }
 
     public void clearPairwisedDevices() {
@@ -578,18 +588,32 @@ public class BridgeServer {
     }
 
     public boolean addOrReplacePairwisedDevice(OcRemoteDevice new_dev) {
-        for(OcRemoteDevice dev : pairwised_devices) {
+
+        Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
+        while(it.hasNext()){
+            OcRemoteDevice dev = it.next();
             if(OCUuidUtil.isEqual(dev.getDeviceId(), new_dev.getDeviceId())) {
-                return true ;
+                removePairwisedDevices(dev);
+                return this.pairwised_devices.put(new_dev, Long.valueOf(System.currentTimeMillis())) == null ? false : true ;
+            }
+        }
+        return this.pairwised_devices.put(new_dev, Long.valueOf(System.currentTimeMillis())) == null ? false : true ;
+
+        /*ArrayList<OcRemoteDevice> clones = (ArrayList<OcRemoteDevice>)pairwised_devices.clone();
+        for(OcRemoteDevice dev : clones) {
+            if(OCUuidUtil.isEqual(dev.getDeviceId(), new_dev.getDeviceId())) {
+                removePairwisedDevices(dev);
+                return this.pairwised_devices.add(new_dev) ;
             }
         }
-        return this.pairwised_devices.add(new_dev);
+        return this.pairwised_devices.add(new_dev);*/
     }
 
     public boolean removePairwisedDevices(OcRemoteDevice dev) {
-        return this.pairwised_devices.remove(dev);
+        return this.pairwised_devices.remove(dev) > 0;
     }
 
+/*
     public boolean removePairwisedDevices(int index) {
         return pairwised_devices.remove(pairwised_devices.get(index));
     }
@@ -597,17 +621,48 @@ public class BridgeServer {
     public OcRemoteDevice getPairwisedDevice(int index) {
         return pairwised_devices.get(index);
     }
+*/
 
     public OcRemoteDevice getPairwisedDevice(String device_uuid) {
         OCUuid uuid = OCUuidUtil.stringToUuid(device_uuid);
-        for(OcRemoteDevice device : pairwised_devices) {
+        Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
+        while(it.hasNext()){
+            OcRemoteDevice dev = it.next();
+            if(OCUuidUtil.isEqual(dev.getDeviceId(), uuid)) {
+                return dev ;
+            }
+        }
+        /*for(OcRemoteDevice device : pairwised_devices) {
             if(OCUuidUtil.isEqual(device.getDeviceId(), uuid)) {
                 return device ;
             }
-        }
+        }*/
         return null ;
     }
 
+    public long getPairwisedDeviceTimeStamp(String device_uuid) {
+        OCUuid uuid = OCUuidUtil.stringToUuid(device_uuid);
+        Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
+        while(it.hasNext()){
+            OcRemoteDevice dev = it.next();
+            if(OCUuidUtil.isEqual(dev.getDeviceId(), uuid)) {
+                return pairwised_devices.get(dev) ;
+            }
+        }
+        return 0 ;
+    }
+
+    public boolean isPairwisedDeviceOnlineStatus(String device_uuid) {
+        long current_time = System.currentTimeMillis() ;
+        long timestamp = getPairwisedDeviceTimeStamp(device_uuid);
+        Log.d(TAG, "isPairwisedDeviceOnlineStatus() : System.currentTimeMillis() = " + current_time + ", latest discovered time = " + timestamp);
+        if((current_time - timestamp)
+                < (BridgeService.PAIRWISED_DISCOVERY_PEROID * 1.8) * 1000L) {
+            return true;
+        }
+        return false;
+    }
+
     public OcRemoteResource getPairwisedResource(String device_uuid, String resource_uri) {
         OcRemoteDevice device = getPairwisedDevice(device_uuid);
         for(OcRemoteResource resource : device.getResources()) {

+ 21 - 5
app/src/main/java/org/iotivity/bridge/homeserver/BridgeService.java

@@ -104,6 +104,9 @@ public class BridgeService extends IntentService {
         return bridgeServer ;
     }
 
+    public static final int PAIRWISED_DISCOVERY_PEROID = 90 ;
+    public static final int LIGHTCONTROL_DISCOVERY_PEROID = 180 ;
+
     @Override
     public void onCreate() {
         super.onCreate();
@@ -861,7 +864,7 @@ public class BridgeService extends IntentService {
                 // load pair-wised device UUIDs from Bridge Credentials
                 loadPairwisedDeviceUUIDs();
                 // clear all pair-wised device list discovered pre-time
-                clearPairwisedDevices();
+                //clearPairwisedDevices();
 
                 if(OCMain.isOwnedDevice(Bridge.BRIDGE_INDEX)) {
                     // discover all pair-wised device except Bridge and OBT
@@ -870,10 +873,21 @@ public class BridgeService extends IntentService {
                         final String msg = "Failed to discover devices";
                         Log.d(TAG, msg);
                     }
-                    Log.d(TAG, "DeviceDiscovery ScheduledTask per 90sec was called.");
+                    Log.d(TAG, "DeviceDiscovery ScheduledTask per "+PAIRWISED_DISCOVERY_PEROID+"sec was called.");
                 }
+
+            }
+        }, 10, PAIRWISED_DISCOVERY_PEROID, TimeUnit.SECONDS); // 180 sec interval after 10sec delay
+
+        executorPairwised.scheduleAtFixedRate(new Runnable() {
+            @Override
+            public void run() {
+
+                Log.d(TAG, "Pairwised Device Status check ScheduledTask per "+PAIRWISED_DISCOVERY_PEROID+"sec was called.");
+                Log.d(TAG, "getAllDiscoveredDevices() : "+getAllDiscoveredDevices());
+
             }
-        }, 10, 90, TimeUnit.SECONDS); // 90 sec interval after 10sec delay
+        }, 20, PAIRWISED_DISCOVERY_PEROID, TimeUnit.SECONDS); // 180 sec interval after 10sec delay
     }
 
     public void stopScheduledTaskForPairwised() {
@@ -894,14 +908,14 @@ public class BridgeService extends IntentService {
             @Override
             public void run() {
                 // Getting Properites of Light Control Service for resource monitoring
-                Log.d(TAG, "LightControl ScheduledTask per 30sec was called.");
+                Log.d(TAG, "LightControl ScheduledTask per "+LIGHTCONTROL_DISCOVERY_PEROID+"sec was called.");
                 if(lightControlBinder==null) {
                     bindLightControlService();
                 }
                 discoverAllLightControlDevices();
                 doGetLightControlResources();
             }
-        }, 30, 30, TimeUnit.SECONDS); // 180sec interval after 30sec delay
+        }, 30, LIGHTCONTROL_DISCOVERY_PEROID, TimeUnit.SECONDS); // 180sec interval after 30sec delay
     }
 
     public void stopScheduledTaskForLightControl() {
@@ -954,6 +968,8 @@ public class BridgeService extends IntentService {
                     obj.put("piid", device.getProtocolIndependentId());
                     obj.put("icv", device.getSpecVersion());
                     obj.put("dmv", device.getDataModelVersion());
+                    boolean online = bridgeServer.isPairwisedDeviceOnlineStatus(OCUuidUtil.uuidToString(device.getDeviceId()));
+                    obj.put("status", online ? "online" : "offline");
                     array.put(obj);
                 }
                 return array.toString().replace("\\", "");

+ 4 - 2
app/src/main/java/org/iotivity/bridge/homeserver/handler/FactoryPresetsHandler.java

@@ -6,8 +6,10 @@ import android.util.Log;
 import org.iotivity.OCFactoryPresetsHandler;
 import org.iotivity.OCPki;
 import org.iotivity.OCSpTypesMask;
+import org.iotivity.bridge.homeserver.BridgeServer;
 import org.iotivity.bridge.homeserver.BridgeServerActivity;
 import org.iotivity.bridge.homeserver.BridgeService;
+import org.iotivity.bridge.homeserver.model.Bridge;
 import org.iotivity.bridge.homeserver.utils.Utils;
 
 import java.io.ByteArrayOutputStream;
@@ -29,8 +31,8 @@ public class FactoryPresetsHandler implements OCFactoryPresetsHandler {
     public void handler(long deviceIndex) {
         Log.d(TAG, "inside the FactoryPresetsHandler, deviceIndex  = " + deviceIndex);
 
-        setPKISecurityMfgCert(deviceIndex);
-
+        if(deviceIndex == Bridge.BRIDGE_INDEX)
+            setPKISecurityMfgCert(deviceIndex);
     }
 
     private void setPKISecurityMfgCert(long deviceIndex) {

+ 27 - 21
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/DeviceDiscoveryHandler.java

@@ -26,33 +26,39 @@ public class DeviceDiscoveryHandler implements OcDeviceDiscoveryHandler {
     }
 
     @Override
-    public void discoveredDevice(OcRemoteDevice remoteDevice) {
+    public void discoveredDevice(final OcRemoteDevice remoteDevice) {
         Log.d(TAG, "Remote Device Discovery Handler: " + OCUuidUtil.uuidToString(remoteDevice.getDeviceId()) + ", " + remoteDevice.getName());
 
-        /* Only pairwised devices except Bridge itself and OBT will be added in the pairwised device list */
-        if(!OCUuidUtil.isEqual(remoteDevice.getDeviceId(), self_di) && !Utils.isObt(remoteDevice)) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                /* Only pairwised devices except Bridge itself and OBT will be added in the pairwised device list */
+                if(!OCUuidUtil.isEqual(remoteDevice.getDeviceId(), self_di) && !Utils.isObt(remoteDevice)) {
 
-            /* check if Bridge has the discovered device's pairwised credential */
-            if(service.hasPairwisedDeviceUUIDs(remoteDevice.getDeviceId())) {
-                service.addPairwisedDevice(remoteDevice);
-                //Log.d(TAG, "getAllResources : " + service.getDeviceResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                    /* check if Bridge has the discovered device's pairwised credential */
+                    if(service.hasPairwisedDeviceUUIDs(remoteDevice.getDeviceId())) {
+                        service.addPairwisedDevice(remoteDevice);
+                        //Log.d(TAG, "getAllResources : " + service.getDeviceResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
 
-                //Log.d(TAG, "getDeviceSpecificResources: " + service.doGetDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
-                //Log.d(TAG, "StopObserveAll : " + service.stopObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
-                Log.d(TAG, "ObserveAll : " + service.doObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
-            }
+                        //Log.d(TAG, "getDeviceSpecificResources: " + service.doGetDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                        //Log.d(TAG, "StopObserveAll : " + service.stopObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                        Log.d(TAG, "ObserveAll : " + service.doObserveDeviceSpecificResources(OCUuidUtil.uuidToString(remoteDevice.getDeviceId())));
+                    }
 
-            /* check if the device is accessible via coaps get /oic/d or not
-             * because no pairwised client can get STATUS_OK in secured channel(coaps)
-            OcRemoteResource[] resources = remoteDevice.getResources();
-            for(OcRemoteResource resource : resources) {
-                if(Utils.containsList(resource.getTypes(), "oic.wk.d")){
-                * //GetDoxmResponseHandler.RT_DOXM)) {
-                    GetDeviceSecuredResponseHandler responseHandler = new GetDeviceSecuredResponseHandler(service, remoteDevice, resource);
-                    OcUtils.doGet(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, responseHandler, OCQos.LOW_QOS);
+                    /* check if the device is accessible via coaps get /oic/d or not
+                     * because no pairwised client can get STATUS_OK in secured channel(coaps)
+                    OcRemoteResource[] resources = remoteDevice.getResources();
+                    for(OcRemoteResource resource : resources) {
+                        if(Utils.containsList(resource.getTypes(), "oic.wk.d")){
+                        * //GetDoxmResponseHandler.RT_DOXM)) {
+                            GetDeviceSecuredResponseHandler responseHandler = new GetDeviceSecuredResponseHandler(service, remoteDevice, resource);
+                            OcUtils.doGet(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, responseHandler, OCQos.LOW_QOS);
+                        }
+                    }
+                    */
                 }
             }
-            */
-        }
+        }).start();
+
     }
 }

+ 1 - 1
app/src/main/java/org/iotivity/bridge/homeserver/handler/client/GetDeviceSecuredResponseHandler.java

@@ -69,7 +69,7 @@ public class GetDeviceSecuredResponseHandler implements OCResponseHandler {
         service.printLine();
         Log.d(TAG, "Properties : " + Utils.propertiesToString(props));
 
-        service.addPairwisedDevice(device);
+        //service.addPairwisedDevice(device);
 
     }
 }