Addition of message routing
BlueMesh now has functionality, through a modified "write" command, to route messages to a target instead of to all devices. While it still effectively floods all devices, it is ignored by all except the target device.
Changes
--- BlueMesh/src/blue/mesh/BlueMeshService.java 10a15a8d0eb0e7d96c87afa679e7aef85529b78a
+++ BlueMesh/src/blue/mesh/BlueMeshService.java a317db949e45bdc1f0a99bdeaf16f337207b391d
@@ -3,6 +3,7 @@
+import android.bluetooth.BluetoothDevice;
@@ -52,7 +53,7 @@
- router = new RouterObject();
+ router = new RouterObject(adapter.getAddress());
@@ -86,10 +87,15 @@
- // function that writes message to devices
+ // function that writes message to all devices
- router.write(buffer, Constants.BYTE_LEVEL_USER);
+ router.write(buffer, Constants.MESSAGE_ALL, null);
+ }
+
+ public int write(byte[] buffer, BluetoothDevice target) {+ router.write(buffer, Constants.MESSAGE_TARGET, target); //TODO: Insert Handling in router for alternate messages.
+ return Constants.SUCCESS;
--- BlueMesh/src/blue/mesh/Constants.java 10a15a8d0eb0e7d96c87afa679e7aef85529b78a
+++ BlueMesh/src/blue/mesh/Constants.java a317db949e45bdc1f0a99bdeaf16f337207b391d
@@ -15,6 +15,9 @@
+
+ // Length of a Target Identifier (Length of address taken in characters)
+ public static final int TARGET_ID_LEN = 8;
@@ -29,10 +32,13 @@
- // Used to determine at what lever a message originated
- // No longer used in this implementation, but kept for compatibility.
- public static final byte BYTE_LEVEL_USER = 1;
- public static final byte BYTE_LEVEL_SYSTEM = 2;
+ // Used to determine at what level a message originated (Deprecated)
+ //public static final byte BYTE_LEVEL_USER = 1;
+ //public static final byte BYTE_LEVEL_SYSTEM = 2;
+ // The slot previously used for that (first byte of message packet) is now used
+ // to differentiate between a message to all and a message to one machine.
+ public static final byte MESSAGE_ALL = 1;
+ public static final byte MESSAGE_TARGET = 2;
--- BlueMesh/src/blue/mesh/RouterObject.java 10a15a8d0eb0e7d96c87afa679e7aef85529b78a
+++ BlueMesh/src/blue/mesh/RouterObject.java a317db949e45bdc1f0a99bdeaf16f337207b391d
@@ -19,12 +19,18 @@
-
- protected RouterObject() {+ private byte[] address; //This device's address, as it appears on incoming messages (Generated & Truncated)
+
+ protected RouterObject(String m_address) {+ address = new byte[Constants.TARGET_ID_LEN];
+ byte[] temp = m_address.getBytes();
+ for(int i=0; i<Constants.TARGET_ID_LEN; i++){+ address[i] = temp[i];
+ }
@@ -73,9 +79,7 @@
- // TODO: Change parsing of message below so this byte isn't allocated.
- // TODO: Change message creation function so this byte isn't allocated.
- // byte messageLevel = buffer[0];
+ byte messageLevel = buffer[0];
@@ -101,6 +105,33 @@
+ if (messageLevel == Constants.MESSAGE_ALL){ + // If I am not the sender of the message
+ // add it to the message queue
+ if (source != Constants.SRC_ME) {+ // Add message to message queue
+ synchronized (this.messages) {+ byte message[] = new byte[buffer.length
+ - Constants.MESSAGE_ID_LEN - 1];
+ for (int i = Constants.MESSAGE_ID_LEN + 1; i < buffer.length; i++) {+ message[i - Constants.MESSAGE_ID_LEN - 1] = buffer[i];
+ }
+ messages.add(message);
+ }
+ }
+ }
+
+ else if (messageLevel == Constants.MESSAGE_TARGET){+ byte target[] = new byte[Constants.TARGET_ID_LEN];
+ for (int i=0; i<Constants.TARGET_ID_LEN; i++){+ target[i] = buffer[i + Constants.MESSAGE_ID_LEN + 1];
+ }
+ if (target == this.address) {+ //Add to the message queue
+ return Constants.SUCCESS; //DO NOT send to all threads.
+ }
+ }
+
@@ -109,26 +140,9 @@
-
- // In this implementation, all messages are user-level.
-
- // If I am not the sender of the message
- // add it to the message queue
- if (source != Constants.SRC_ME) {- // Add message to message queue
- synchronized (this.messages) {- byte message[] = new byte[buffer.length
- - Constants.MESSAGE_ID_LEN - 1];
- for (int i = Constants.MESSAGE_ID_LEN + 1; i < buffer.length; i++) {- message[i - Constants.MESSAGE_ID_LEN - 1] = buffer[i];
- }
- messages.add(message);
- }
- }
- return Constants.SUCCESS;
- }
-
- // Completed: Clean out this code and references to System Messages, as well as their related constants, elsewhere.
+
+ return Constants.SUCCESS;
+ }
@@ -196,8 +210,7 @@
- protected int write(byte[] buffer, byte messageLevel) {-
+ protected int write(byte[] buffer, byte messageLevel, BluetoothDevice target) {@@ -217,18 +230,42 @@
-
- byte new_buffer[] = new byte[Constants.MESSAGE_ID_LEN + buffer.length
+
+ int middle_field_length = 0;
+ byte middle_field[] = null;
+
+ if (messageLevel == Constants.MESSAGE_ALL){+ middle_field_length = Constants.MESSAGE_ID_LEN;
+ middle_field = messageID;
+ }
+ else if (messageLevel == Constants.MESSAGE_TARGET){+ middle_field_length = (Constants.MESSAGE_ID_LEN + Constants.TARGET_ID_LEN);
+ byte[] targetID = target.toString().getBytes(); //May be longer than Constants.TARGET_ID_LEN
+ //TODO: Write what happens for targeted messages.
+ middle_field = new byte[Constants.MESSAGE_ID_LEN + Constants.TARGET_ID_LEN];
+ for(int i=0; i<Constants.MESSAGE_ID_LEN; i++){+ middle_field[i] = messageID[i];
+ }
+ for(int i=0; i<Constants.TARGET_ID_LEN; i++){+ middle_field[i+Constants.MESSAGE_ID_LEN] = targetID[i];
+ }
+ }
+ else {+ Log.e(TAG, "Message Level is invalid");
+ }
+
+ //Construct the message in new_buffer and route it out.
+ byte new_buffer[] = new byte[middle_field_length + buffer.length
- for (int i = 0; i < Constants.MESSAGE_ID_LEN; i++) {- new_buffer[i + 1] = messageID[i];
+ for (int i = 0; i < middle_field_length; i++) {+ new_buffer[i + 1] = middle_field[i];
- new_buffer[Constants.MESSAGE_ID_LEN + i + 1] = buffer[i];
+ new_buffer[middle_field_length + i + 1] = buffer[i];

