Skip to content
Browse files

Added first stab at some protocol prediction code

  • Loading branch information...
1 parent c334a35 commit 009d4abf9ac21c0bc84eb8628d1517c79d87c499 @theterg theterg committed
View
3 com.buglabs.xbee/com/buglabs/xbee/XBeeController.java
@@ -2,6 +2,7 @@
import java.util.Map;
+import com.buglabs.xbee.protocol.BaseXBeeProtocol;
import com.buglabs.xbee.protocol.XBeeProtocol;
import com.rapplogic.xbee.api.XBee;
@@ -11,4 +12,6 @@
public XBee getXBee();
public Map<String,Object> getResponse();
public Map<String,Object> getResponse(int[] addr);
+ public void addPredictive(Class proto);
+ public void removeAll(Class proto);
}
View
67 com.buglabs.xbee/com/buglabs/xbee/XBeeMonitor.java
@@ -1,7 +1,9 @@
package com.buglabs.xbee;
+import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -12,6 +14,7 @@
import org.osgi.util.tracker.ServiceTracker;
import com.buglabs.application.ServiceTrackerHelper.ManagedRunnable;
+import com.buglabs.xbee.protocol.BaseXBeeProtocol;
import com.buglabs.xbee.protocol.XBeeProtocol;
import com.rapplogic.xbee.api.ApiId;
import com.rapplogic.xbee.api.AtCommand;
@@ -42,6 +45,7 @@
private String devnode = "/dev/ttyUSB0";
private int baud = 9600;
private Map<XBeeAddress, XBeeProtocol> protocols;
+ private Set<Class> expectedProtocols;
private BundleContext _context;
private ServiceTracker tracker;
@@ -59,6 +63,41 @@ public void processResponse(XBeeResponse res) {
whiteboardNotify(ret);
}
+ private Map<String,Object> predictiveParse(XBeeResponse res){
+ Map<String,Object> ret = null;
+ if (res.getApiId() == ApiId.AT_RESPONSE)
+ return null;
+ boolean found = false;
+ for (int i=0;i<VALID_APIID.length;i++)
+ if (res.getApiId() == VALID_APIID[i])
+ found = true;
+ if (!found){
+ return null;
+ }
+ RxBaseResponse pkt = (RxBaseResponse)res;
+ for (Class proto:expectedProtocols){
+ XBeeProtocol candidate = null;
+ try {
+ candidate = (XBeeProtocol) proto.newInstance();
+ } catch (InstantiationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ if (candidate == null){
+ continue;
+ }
+ if (candidate.parseable(res)){
+ candidate.setAddr(pkt.getSourceAddress());
+ addListener(candidate);
+ return candidate.parse(res);
+ }
+ }
+ return null;
+ }
+
private Map<String,Object> parseResponse(XBeeResponse res){
//Shortcut - AT_Responses should only be synchronous
//Also, ATMY is used as a ping, so we need to filter it out
@@ -90,9 +129,15 @@ public void processResponse(XBeeResponse res) {
//Send the raw (but still unescaped) packet bytes.
} else {
if (tracker.size() > 0){
- ret = new HashMap<String,Object>();
- ret.put("raw", pkt.getProcessedPacketBytes());
- ret.put("address", pkt.getSourceAddress().getAddress());
+ //If we have listeners, try to guess the protocol for them.
+ dlog("Attempting to guess the protocol...");
+ ret = predictiveParse(res);
+ if (ret != null){
+ dlog("prediction success: "+protocols.get(pkt.getSourceAddress()).getClass().getName());
+ ret.put("protocol", protocols.get(pkt.getSourceAddress()));
+ ret.put("address", pkt.getSourceAddress().getAddress());
+ }
+ return ret;
} else {
dlog("data(no consumers):"+ByteUtils.toString(pkt.getProcessedPacketBytes()));
}
@@ -166,10 +211,26 @@ public XBeeMonitor(BundleContext context){
}
@Override
+ public void addPredictive(Class proto) {
+ expectedProtocols.add(proto);
+ }
+
+ @Override
+ public void removeAll(Class proto) {
+ expectedProtocols.remove(proto);
+ for (Map.Entry<XBeeAddress, XBeeProtocol> entry : protocols.entrySet()){
+ if (entry.getValue().getClass() == proto){
+ protocols.remove(entry.getKey());
+ }
+ }
+ }
+
+ @Override
public void run(Map<Object, Object> services) {
// TODO Auto-generated method stub
ls = (LogService) services.get(LogService.class.getName());
protocols = new HashMap<XBeeAddress, XBeeProtocol>();
+ expectedProtocols = new HashSet<Class>();
t = new Thread(this, "XBee Monitor");
t.start();
_context.registerService(XBeeController.class.getName(), this, null);
View
8 com.buglabs.xbee/com/buglabs/xbee/protocol/BaseXBeeProtocol.java
@@ -12,6 +12,9 @@
protected Map<String, Object> last = null;
protected XBeeAddress addr;
protected XBeeController con;
+
+ public BaseXBeeProtocol(){
+ }
public BaseXBeeProtocol(int[] address, XBeeController controller){
if (address.length > 2)
@@ -50,4 +53,9 @@ public boolean parseable(XBeeResponse res) {
return false;
}
+ @Override
+ public void setAddr(XBeeAddress address) {
+ addr = address;
+ }
+
}
View
1 com.buglabs.xbee/com/buglabs/xbee/protocol/XBeeProtocol.java
@@ -9,6 +9,7 @@
public Map<String, Object> parse(XBeeResponse res);
public Map<String, Object> lastSample();
public XBeeAddress getAddr();
+ public void setAddr(XBeeAddress address);
public String toString(Map<String, Object> data);
public boolean parseable(XBeeResponse res);
};

0 comments on commit 009d4ab

Please sign in to comment.
Something went wrong with that request. Please try again.