[zb4osgi-changeset] [scm] ZigBee 4 OSGi repository change: r1094 - in /projects/zb4osgi/trunk: zigbee.CC2530.driver/src/main/java/it/cnr/isti/zigbee/dongle/CC2530/impl/ zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/ zigbee.ez430-rf2480.driver/src/main/java/it/cnr/isti/zigbee/dongle/ez430rf2480/impl/

scm-notify at zb4osgi.aaloa.org scm-notify at zb4osgi.aaloa.org
Wed Nov 12 20:35:13 CET 2014


Author: stefano.lenzi
Date: Wed Nov 12 20:35:13 2014
New Revision: 1094

Log:
Extracted WaitForAList and WaitForCommand as class so that code reasue among different driver is easier ( refs #275 )



Added:
    projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/
    projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForAList.java   (with props)
    projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForCommand.java   (with props)
Modified:
    projects/zb4osgi/trunk/zigbee.CC2530.driver/src/main/java/it/cnr/isti/zigbee/dongle/CC2530/impl/DriverCC2530.java
    projects/zb4osgi/trunk/zigbee.ez430-rf2480.driver/src/main/java/it/cnr/isti/zigbee/dongle/ez430rf2480/impl/DriverEZ430_RF2480.java

Modified: projects/zb4osgi/trunk/zigbee.CC2530.driver/src/main/java/it/cnr/isti/zigbee/dongle/CC2530/impl/DriverCC2530.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.CC2530.driver/src/main/java/it/cnr/isti/zigbee/dongle/CC2530/impl/DriverCC2530.java	(original)
+++ projects/zb4osgi/trunk/zigbee.CC2530.driver/src/main/java/it/cnr/isti/zigbee/dongle/CC2530/impl/DriverCC2530.java	Wed Nov 12 20:35:13 2014
@@ -27,9 +27,9 @@
 import it.cnr.isti.cc2480.high.SynchrounsCommandListner;
 import it.cnr.isti.cc2480.low.HWLowLevelDriver;
 import it.cnr.isti.cc2480.low.PacketListener;
+import it.cnr.isti.cc2480.zic.WaitForCommand;
 import it.cnr.isti.primitvetypes.util.Integers;
 import it.cnr.isti.zigbee.dongle.api.AFMessageListner;
-import it.cnr.isti.zigbee.dongle.api.AnnounceListner;
 import it.cnr.isti.zigbee.dongle.api.AnnounceListner;
 import it.cnr.isti.zigbee.dongle.api.ConfigurationProperties;
 import it.cnr.isti.zigbee.dongle.api.DriverStatus;
@@ -109,10 +109,7 @@
  */
 public class DriverCC2530 implements Runnable, SimpleDriver {
 
-    private final static Logger logger = LoggerFactory
-            .getLogger(DriverCC2530.class);
-    private final static Logger logger4Waiter = LoggerFactory
-            .getLogger(WaitForCommand.class);
+    private final static Logger logger = LoggerFactory.getLogger(DriverCC2530.class);
 
     public static final int RESEND_TIMEOUT_DEFAULT = 1000;
     public static final String RESEND_TIMEOUT_KEY = "zigbee.driver.cc2530.resend.timeout";
@@ -765,166 +762,12 @@
         }
     }
 
-    private class WaitForCommand implements AsynchrounsCommandListener {
-
-        final ZToolPacket[] result = new ZToolPacket[]{null};
-        final int waitFor;
-        final HWHighLevelDriver driver;
-
-        public WaitForCommand(int waitFor, HWHighLevelDriver driver) {
-            this.waitFor = waitFor;
-            this.driver = driver;
-            driver.addAsynchrounsCommandListener(this);
-        }
-
-        public void receivedAsynchrounsCommand(ZToolPacket packet) {
-            logger4Waiter.info("received a packet {} and waiting for {}",
-                    packet.getCMD(), waitFor);
-            logger4Waiter.debug("received {} {}", packet.getClass(),
-                    packet.toString());
-            if (packet.isError())
-                return;
-            if (packet.getCMD().get16BitValue() != waitFor)
-                return;
-            synchronized (result) {
-                result[0] = packet;
-                logger4Waiter.info("received packet that we were waiting for");
-                cleanup();
-            }
-        }
-
-        public ZToolPacket getCommand(final long timeout) {
-            synchronized (result) {
-                final long wakeUpTime = System.currentTimeMillis() + timeout;
-                while (result[0] == null
-                        && wakeUpTime > System.currentTimeMillis()) {
-                    try {
-                        result.wait(wakeUpTime - System.currentTimeMillis());
-                    } catch (InterruptedException ignored) {
-                    }
-
-                }
-            }
-            if (result[0] == null) {
-                logger4Waiter.warn(
-                        "Timeout {} expired and no packet with {} received",
-                        timeout, waitFor);
-            }
-            cleanup();
-            return result[0];
-        }
-
-        public void cleanup() {
-            synchronized (result) {
-                driver.removeAsynchrounsCommandListener(this);
-                result.notify();
-            }
-        }
-    }
-
-    private class WaitForAList implements PacketListener {
-
-        final int[] waitingList;
-        final ZToolPacket[] packetHistory;
-        final HWLowLevelDriver hwDriver;
-        int idx = 0;
-
-/**
-             * This constructor uses the {@link WaitForAList#WaitForAList(int[], ZToolPacket[], HWLowLevelDriver) constructor<br>
-             * with the following parameters <code>WaitForAList(list,null,null)</code>
-             *
-             * @see WaitForAList#WaitForAList(int[], ZToolPacket[], HWLowLevelDriver)
-             * @param list
-             */
-        public WaitForAList(final int[] list) {
-            this(list, null, null);
-        }
-
-        /**
-         * Create a {@link WaitForAList} that wait for a specified list<br>
-         * of packet to be received. Furthermore, the {@link WaitForAList}<br>
-         * while record all the matching packet received, and it will handle<br>
-         * by itself the registration and unregistration as
-         * {@link PacketListener}<br>
-         * to the specified {@link HWLowLevelDriver}.<br>
-         * <br>
-         * <b>NOTE:</b>If the {@link ZToolPacket} array is null no packet will
-         * be recorded<br>
-         * <b>NOTE:</b>If the {@link HWLowLevelDriver} array is null
-         * registration and unregistration as<br>
-         * {@link PacketListener} must be handled by the invoker<br>
-         *
-         * @param list
-         *            the array of <code>int</code> to listen to
-         * @param packets
-         *            {@link ZToolPacket} the array of the same size as
-         *            <code>list</code> to record the received packet
-         * @param lowDriver
-         *            {@link HWLowLevelDriver} to register to, if
-         *            <code>null</code> to handle registration/unregistration
-         *            manually
-         */
-        public WaitForAList(final int[] list, final ZToolPacket[] packets,
-                final HWLowLevelDriver lowDriver) {
-            this.waitingList = list;
-            this.packetHistory = packets;
-            this.hwDriver = lowDriver;
-            if (hwDriver != null) {
-                hwDriver.addPacketListener(this);
-            }
-        }
-
-        public void waitForAll(long timeout) {
-            long wakeUpTime = System.currentTimeMillis() + timeout;
-            synchronized (waitingList) {
-                while (idx < waitingList.length
-                        && wakeUpTime > System.currentTimeMillis()) {
-                    try {
-                        waitingList.wait(wakeUpTime
-                                - System.currentTimeMillis());
-                    } catch (InterruptedException ignored) {
-                    }
-                }
-            }
-        }
-
-        public void packetReceived(ZToolPacket packet) {
-            if (packet.isError())
-                return;
-            if ((packet.getCommandId() & 0xFFFF) == waitingList[idx]) {
-                logger.info("Received packet that was waiting for increasing waitingList");
-                synchronized (waitingList) {
-                    if (packetHistory != null) {
-                        packetHistory[idx] = packet;
-                    }
-                    idx = idx + 1;
-                    if (idx == waitingList.length) {
-                        if (low != null) {
-                            low.removePacketListener(this);
-                        }
-                        waitingList.notifyAll();
-                        return;
-                    }
-                }
-            }
-        }
-    }
-
     private boolean dongleReset() {
         if (waitForHardware() == false)
             return false;
 
         final WaitForCommand waiter = new WaitForCommand(
                 ZToolCMD.SYS_RESET_RESPONSE, high);
-
-        // WaitForAList waiting = new WaitForAList(new int[]{
-        // ZToolCMD.SYS_RESET_RESPONSE,
-        // ZToolCMD.ZB_APP_REGISTER_RSP,
-        // ZToolCMD.ZB_WRITE_CONFIGURATION_RSP,
-        // ZToolCMD.ZB_WRITE_CONFIGURATION_RSP
-        // });
-
-        // low.addPacketListener(waiting);
 
         try {
             high.sendAsynchrounsCommand(new SYS_RESET(
@@ -937,8 +780,6 @@
         SYS_RESET_RESPONSE response = (SYS_RESET_RESPONSE) waiter
                 .getCommand(TIMEOUT);
 
-        // waiting.waitForAll(TIMEOUT*2);
-        // low.removePacketListener(waiting);
         return response != null;
     }
 

Added: projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForAList.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForAList.java	(added)
+++ projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForAList.java	Wed Nov 12 20:35:13 2014
@@ -0,0 +1,128 @@
+/*
+   Copyright 2014-2014 CNR-ISTI, http://isti.cnr.it
+   Institute of Information Science and Technologies
+   of the Italian National Research Council
+
+
+   See the NOTICE file distributed with this work for additional
+   information regarding copyright ownership
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ */
+package it.cnr.isti.cc2480.zic;
+
+import it.cnr.isti.cc2480.low.HWLowLevelDriver;
+import it.cnr.isti.cc2480.low.PacketListener;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.itaca.ztool.api.ZToolPacket;
+
+/**
+*
+* @author <a href="mailto:stefano.lenzi at isti.cnr.it">Stefano "Kismet" Lenzi</a>
+* @version $LastChangedRevision$ ($LastChangedDate$)
+* @since 0.9.0
+*
+*/
+public class WaitForAList implements PacketListener {
+
+    private final static Logger logger = LoggerFactory.getLogger(WaitForAList.class);
+	
+	
+    final int[] waitingList;
+    final ZToolPacket[] packetHistory;
+    final HWLowLevelDriver hwDriver;
+    int idx = 0;
+    
+    /**
+     * This constructor uses the {@link WaitForAList#WaitForAList(int[], ZToolPacket[], HWLowLevelDriver) constructor<br>
+     * with the following parameters <code>WaitForAList(list,null,null)</code>
+     *
+     * @see WaitForAList#WaitForAList(int[], ZToolPacket[], HWLowLevelDriver)
+     * @param list
+     */
+	public WaitForAList(final int[] list) {
+		this(list, null, null);
+	}
+
+    /**
+     * Create a {@link WaitForAList} that wait for a specified list<br>
+     * of packet to be received. Furthermore, the {@link WaitForAList}<br>
+     * while record all the matching packet received, and it will handle<br>
+     * by itself the registration and unregistration as
+     * {@link PacketListener}<br>
+     * to the specified {@link HWLowLevelDriver}.<br>
+     * <br>
+     * <b>NOTE:</b>If the {@link ZToolPacket} array is null no packet will
+     * be recorded<br>
+     * <b>NOTE:</b>If the {@link HWLowLevelDriver} array is null
+     * registration and unregistration as<br>
+     * {@link PacketListener} must be handled by the invoker<br>
+     *
+     * @param list
+     *            the array of <code>int</code> to listen to
+     * @param packets
+     *            {@link ZToolPacket} the array of the same size as
+     *            <code>list</code> to record the received packet
+     * @param lowDriver
+     *            {@link HWLowLevelDriver} to register to, if
+     *            <code>null</code> to handle registration/unregistration
+     *            manually
+     */
+    public WaitForAList(final int[] list, final ZToolPacket[] packets,
+            final HWLowLevelDriver lowDriver) {
+        this.waitingList = list;
+        this.packetHistory = packets;
+        this.hwDriver = lowDriver;
+        if (hwDriver != null) {
+            hwDriver.addPacketListener(this);
+        }
+    }
+
+    public void waitForAll(long timeout) {
+        long wakeUpTime = System.currentTimeMillis() + timeout;
+        synchronized (waitingList) {
+            while (idx < waitingList.length
+                    && wakeUpTime > System.currentTimeMillis()) {
+                try {
+                    waitingList.wait(wakeUpTime
+                            - System.currentTimeMillis());
+                } catch (InterruptedException ignored) {
+                }
+            }
+        }
+    }
+
+    public void packetReceived(ZToolPacket packet) {
+        if (packet.isError())
+            return;
+        if ((packet.getCommandId() & 0xFFFF) == waitingList[idx]) {
+            logger.info("Received packet that was waiting for increasing waitingList");
+            synchronized (waitingList) {
+                if (packetHistory != null) {
+                    packetHistory[idx] = packet;
+                }
+                idx = idx + 1;
+                if (idx == waitingList.length) {
+                    if (hwDriver != null) {
+                        hwDriver.removePacketListener(this);
+                    }
+                    waitingList.notifyAll();
+                    return;
+                }
+            }
+        }
+    }
+}

Propchange: projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForAList.java
------------------------------------------------------------------------------
    svn:executable = *

Added: projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForCommand.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForCommand.java	(added)
+++ projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForCommand.java	Wed Nov 12 20:35:13 2014
@@ -0,0 +1,102 @@
+/*
+   Copyright 2014-2014 CNR-ISTI, http://isti.cnr.it
+   Institute of Information Science and Technologies
+   of the Italian National Research Council
+
+
+   See the NOTICE file distributed with this work for additional
+   information regarding copyright ownership
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ */
+package it.cnr.isti.cc2480.zic;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import it.cnr.isti.cc2480.high.AsynchrounsCommandListener;
+import it.cnr.isti.cc2480.high.HWHighLevelDriver;
+
+import com.itaca.ztool.api.ZToolPacket;
+
+/**
+*
+* @author <a href="mailto:stefano.lenzi at isti.cnr.it">Stefano "Kismet" Lenzi</a>
+* @version $LastChangedRevision$ ($LastChangedDate$)
+* @since 0.9.0
+*
+*/
+public class WaitForCommand implements AsynchrounsCommandListener {
+
+    private final static Logger logger = LoggerFactory.getLogger(WaitForCommand.class);
+	
+    final ZToolPacket[] result = new ZToolPacket[]{null};
+    final int waitFor;
+    final HWHighLevelDriver driver;
+
+    public WaitForCommand(int waitFor, HWHighLevelDriver driver) {
+        this.waitFor = waitFor;
+        this.driver = driver;
+        driver.addAsynchrounsCommandListener(this);
+    }
+
+    public void receivedAsynchrounsCommand(ZToolPacket packet) {
+        logger.info("Received a packet {} and waiting for {}", packet.getCMD(), waitFor);
+        logger.debug("Received {} {}", packet.getClass(), packet.toString());
+        
+        if (packet.isError()){
+            logger.debug("SKIPPED ZToolPacket: it was error packet");            	
+            return;
+        }
+        
+        final int cmdId = packet.getCMD().get16BitValue();
+        if (cmdId != waitFor) {
+            logger.debug("SKIPPED ZToolPacket: it was error unmatching command {} != {} ", waitFor, cmdId);            	
+            return;
+        }
+        
+        synchronized (result) {
+            result[0] = packet;
+            logger.info("Received the packet that we were waiting for");
+            cleanup();
+        }
+    }
+
+    public ZToolPacket getCommand(final long timeout) {
+        synchronized (result) {
+            final long wakeUpTime = System.currentTimeMillis() + timeout;
+            while (result[0] == null
+                    && wakeUpTime > System.currentTimeMillis()) {
+                try {
+                    result.wait(wakeUpTime - System.currentTimeMillis());
+                } catch (InterruptedException ignored) {
+                }
+
+            }
+        }
+        if (result[0] == null) {
+            logger.warn(
+                    "Timeout {} expired and no packet with {} received",
+                    timeout, waitFor);
+        }
+        cleanup();
+        return result[0];
+    }
+
+    public void cleanup() {
+        synchronized (result) {
+            driver.removeAsynchrounsCommandListener(this);
+            result.notify();
+        }
+    }
+}

Propchange: projects/zb4osgi/trunk/zigbee.cc2480.datalink/src/main/java/it/cnr/isti/cc2480/zic/WaitForCommand.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: projects/zb4osgi/trunk/zigbee.ez430-rf2480.driver/src/main/java/it/cnr/isti/zigbee/dongle/ez430rf2480/impl/DriverEZ430_RF2480.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.ez430-rf2480.driver/src/main/java/it/cnr/isti/zigbee/dongle/ez430rf2480/impl/DriverEZ430_RF2480.java	(original)
+++ projects/zb4osgi/trunk/zigbee.ez430-rf2480.driver/src/main/java/it/cnr/isti/zigbee/dongle/ez430rf2480/impl/DriverEZ430_RF2480.java	Wed Nov 12 20:35:13 2014
@@ -27,6 +27,7 @@
 import it.cnr.isti.cc2480.high.SynchrounsCommandListner;
 import it.cnr.isti.cc2480.low.HWLowLevelDriver;
 import it.cnr.isti.cc2480.low.PacketListener;
+import it.cnr.isti.cc2480.zic.WaitForCommand;
 import it.cnr.isti.primitvetypes.util.Integers;
 import it.cnr.isti.zigbee.dongle.api.AFMessageListner;
 import it.cnr.isti.zigbee.dongle.api.AnnounceListner;
@@ -618,58 +619,6 @@
         }
     }
 
-    private class WaitForCommand implements AsynchrounsCommandListener{
-
-        final ZToolPacket[] result = new ZToolPacket[]{null};
-        final int waitFor;
-        final HWHighLevelDriver driver;
-
-        public WaitForCommand(int waitFor, HWHighLevelDriver driver) {
-            this.waitFor = waitFor;
-            this.driver = driver;
-            driver.addAsynchrounsCommandListener(this);
-        }
-
-
-
-        public void receivedAsynchrounsCommand(ZToolPacket packet) {
-            logger4Waiter.info("Recieved a packet {} and waiting for {}", packet.getCMD(), waitFor);
-            logger4Waiter.debug("Recieved {} {}", packet.getClass(), packet.toString());
-            if( packet.isError() ) return;
-            if( packet.getCMD().get16BitValue() != waitFor) return;
-            synchronized (result) {
-                result[0]=packet;
-                logger4Waiter.info("Recieved packet that we were waiting for");
-                cleanup();
-            }
-        }
-
-        public ZToolPacket getCommand(final long timeout){
-            synchronized (result) {
-                final long wakeUpTime = System.currentTimeMillis() + timeout;
-                while(result[0] == null && wakeUpTime > System.currentTimeMillis()){
-                    try {
-                        result.wait(wakeUpTime-System.currentTimeMillis());
-                    } catch (InterruptedException ignored) {
-                    }
-
-                }
-            }
-            if( result[0] == null ){
-                logger4Waiter.warn("Timeout {} expired and not packet with {} recieved", timeout, waitFor);
-            }
-            cleanup();
-            return result[0];
-        }
-
-        public void cleanup(){
-            synchronized (result) {
-                driver.removeAsynchrounsCommandListener(this);
-                result.notify();
-            }
-        }
-    }
-
     private class WaitForAList implements PacketListener{
 
         final int[] waitingList;




More information about the Commit mailing list