Commit cee89a52 authored by Sven Robertz's avatar Sven Robertz
Browse files

added a small comment on the packing + added untested code to the csharp lib +...

added a small comment on the packing + added untested code to the csharp lib + changed BASE_UID to 0x60
parent 9e27c274
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
/* /*
* Start index for user defined types * Start index for user defined types
*/ */
#define LABCOMM_USER 0x80 #define LABCOMM_USER 0x60
/* /*
* Semi private decoder declarations * Semi private decoder declarations
...@@ -269,6 +269,13 @@ LABCOMM_ENCODE(long, long long) ...@@ -269,6 +269,13 @@ LABCOMM_ENCODE(long, long long)
LABCOMM_ENCODE(float, float) LABCOMM_ENCODE(float, float)
LABCOMM_ENCODE(double, double) LABCOMM_ENCODE(double, double)
/*
* Pack the 32 bit number data as a sequence of 7 bit chunks, represented in bytes
* with the high bit meaning that more data is to come.
*
* The chunks are sent "little endian": each 7 bit chunk is more significant than
* the previous.
*/
static inline void labcomm_pack32(labcomm_writer_t *w, unsigned int data) static inline void labcomm_pack32(labcomm_writer_t *w, unsigned int data)
{ {
unsigned int tmp, i; unsigned int tmp, i;
......
...@@ -25,7 +25,7 @@ namespace se.lth.control.labcomm { ...@@ -25,7 +25,7 @@ namespace se.lth.control.labcomm {
/* /*
* start of user defined types * start of user defined types
*/ */
public const int FIRST_USER_INDEX = 0x80; public const int FIRST_USER_INDEX = 0x60;
} }
......
...@@ -18,11 +18,11 @@ namespace se.lth.control.labcomm { ...@@ -18,11 +18,11 @@ namespace se.lth.control.labcomm {
public void runOne() { public void runOne() {
bool done = false; bool done = false;
while (!done) { while (!done) {
int tag = decodeInt(); int tag = decodePacked32();
switch (tag) { switch (tag) {
case LabComm.TYPEDEF: case LabComm.TYPEDEF:
case LabComm.SAMPLE: { case LabComm.SAMPLE: {
int index = decodeInt(); int index = decodePacked32();
String name = decodeString(); String name = decodeString();
MemoryStream signature = new MemoryStream(); MemoryStream signature = new MemoryStream();
collectFlatSignature(new LabCommEncoderChannel(signature)); collectFlatSignature(new LabCommEncoderChannel(signature));
...@@ -55,19 +55,19 @@ namespace se.lth.control.labcomm { ...@@ -55,19 +55,19 @@ namespace se.lth.control.labcomm {
} }
private void collectFlatSignature(LabCommEncoder e) { private void collectFlatSignature(LabCommEncoder e) {
int type = decodeInt(); int type = decodePacked32();
e.encodeInt(type); e.encodeInt(type);
switch (type) { switch (type) {
case LabComm.ARRAY: { case LabComm.ARRAY: {
int dimensions = decodeInt(); int dimensions = decodePacked32();
e.encodeInt(dimensions); e.encodePacked32(dimensions);
for (int i = 0 ; i < dimensions ; i++) { for (int i = 0 ; i < dimensions ; i++) {
e.encodeInt(decodeInt()); e.encodePacked32(decodePacked32());
} }
collectFlatSignature(e); collectFlatSignature(e);
} break; } break;
case LabComm.STRUCT: { case LabComm.STRUCT: {
int fields = decodeInt(); int fields = decodePacked32();
e.encodeInt(fields); e.encodeInt(fields);
for (int i = 0 ; i < fields ; i++) { for (int i = 0 ; i < fields ; i++) {
e.encodeString(decodeString()); e.encodeString(decodeString());
...@@ -155,12 +155,26 @@ namespace se.lth.control.labcomm { ...@@ -155,12 +155,26 @@ namespace se.lth.control.labcomm {
} }
public String decodeString() { public String decodeString() {
int length = (int)ReadInt(4); //int length = (int)ReadInt(4);
int length = decodePacked32();
byte[] buf = new byte[length]; byte[] buf = new byte[length];
ReadBytes(buf, length); ReadBytes(buf, length);
return Encoding.UTF8.GetString(buf); return Encoding.UTF8.GetString(buf);
} }
} public int decodePacked32() {
Int64 res = 0;
byte i = 0;
bool cont = true;
do {
byte c = decodeByte();
res |= (c & 0x7f) << 7*i;
cont = (c & 0x80) != 0;
i++;
} while(cont);
return (int) (res & 0xffffffff);
}
}
} }
...@@ -18,8 +18,8 @@ namespace se.lth.control.labcomm { ...@@ -18,8 +18,8 @@ namespace se.lth.control.labcomm {
public void register(LabCommDispatcher dispatcher) { public void register(LabCommDispatcher dispatcher) {
int index = registry.add(dispatcher); int index = registry.add(dispatcher);
encodeInt(LabComm.SAMPLE); encodePacked32(LabComm.SAMPLE);
encodeInt(index); encodePacked32(index);
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature(); byte[] signature = dispatcher.getSignature();
for (int i = 0 ; i < signature.Length ; i++) { for (int i = 0 ; i < signature.Length ; i++) {
...@@ -29,7 +29,7 @@ namespace se.lth.control.labcomm { ...@@ -29,7 +29,7 @@ namespace se.lth.control.labcomm {
} }
public void begin(Type c) { public void begin(Type c) {
encodeInt(registry.getTag(c)); encodePacked32(registry.getTag(c));
} }
public void end(Type c) { public void end(Type c) {
...@@ -84,9 +84,18 @@ namespace se.lth.control.labcomm { ...@@ -84,9 +84,18 @@ namespace se.lth.control.labcomm {
public void encodeString(String value) { public void encodeString(String value) {
byte[] buf = Encoding.UTF8.GetBytes(value); byte[] buf = Encoding.UTF8.GetBytes(value);
WriteInt(buf.Length, 4); EncodePacked32(buf.Length, 4);
bytes.Write(buf, 0, buf.Length); bytes.Write(buf, 0, buf.Length);
} }
public void encodePacked32(Int64 value) {
Int64 tmp = value;
while(tmp >= 0x80) {
encodeByte( (byte) ((tmp & 0x7f) | 0x80 ) );
tmp >>= 7;
}
encodeByte( (byte) (tmp & 0x7f) );
}
} }
} }
\ No newline at end of file
...@@ -25,6 +25,6 @@ public class LabComm { ...@@ -25,6 +25,6 @@ public class LabComm {
/* /*
* Start of * Start of
*/ */
public static final int FIRST_USER_INDEX = 0x80; public static final int FIRST_USER_INDEX = 0x60;
} }
\ No newline at end of file
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
# User data: # User data:
# #
# +----+----+----+----+ # +----+----+----+----+
# | id >= 0x00000080 | # | id >= 0x00000060 |
# +----+----+----+----+ # +----+----+----+----+
# | user data # | user data
# | ... # | ...
...@@ -107,7 +107,7 @@ i_FLOAT = 0x25 ...@@ -107,7 +107,7 @@ i_FLOAT = 0x25
i_DOUBLE = 0x26 i_DOUBLE = 0x26
i_STRING = 0x27 i_STRING = 0x27
i_USER = 0x80 i_USER = 0x60
def indent(i, s): def indent(i, s):
return ("\n%s" % (" " * i)).join(s.split("\n")) return ("\n%s" % (" " * i)).join(s.split("\n"))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment