-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy SuplaDataPacket.java because it has extra sulpa tag in proto.h
- Loading branch information
Showing
7 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...col/src/main/java/pl/grzeslowski/jsupla/protocol/api/decoders/SuplaDataPacketDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package pl.grzeslowski.jsupla.protocol.api.decoders; | ||
|
||
import lombok.val; | ||
import pl.grzeslowski.jsupla.protocol.api.structs.SuplaDataPacket; | ||
|
||
import static pl.grzeslowski.jsupla.protocol.api.JavaConsts.BYTE_SIZE; | ||
import static pl.grzeslowski.jsupla.protocol.api.JavaConsts.INT_SIZE; | ||
|
||
@lombok.NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) | ||
public class SuplaDataPacketDecoder implements ProtoWithSizeDecoder<SuplaDataPacket> { | ||
public static final SuplaDataPacketDecoder INSTANCE = new SuplaDataPacketDecoder(); | ||
|
||
@Override | ||
public SuplaDataPacket decode(byte[] bytes, int offset) { | ||
val version = PrimitiveDecoder.INSTANCE.parseUnsignedByte(bytes, offset); | ||
offset += BYTE_SIZE; | ||
|
||
val rrId = PrimitiveDecoder.INSTANCE.parseUnsignedInt(bytes, offset); | ||
offset += INT_SIZE; | ||
|
||
val callId = PrimitiveDecoder.INSTANCE.parseUnsignedInt(bytes, offset); | ||
offset += INT_SIZE; | ||
|
||
val dataSize = PrimitiveDecoder.INSTANCE.parseUnsignedInt(bytes, offset); | ||
offset += INT_SIZE; | ||
|
||
val data = PrimitiveDecoder.INSTANCE.copyOfRangeByte(bytes, offset, offset + (int) dataSize); | ||
offset += dataSize * BYTE_SIZE; | ||
|
||
return new SuplaDataPacket(version, rrId, callId, dataSize, data); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...col/src/main/java/pl/grzeslowski/jsupla/protocol/api/encoders/SuplaDataPacketEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pl.grzeslowski.jsupla.protocol.api.encoders; | ||
|
||
import pl.grzeslowski.jsupla.protocol.api.structs.SuplaDataPacket; | ||
|
||
@lombok.NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) | ||
public class SuplaDataPacketEncoder implements ProtoWithSizeEncoder<SuplaDataPacket> { | ||
public static final SuplaDataPacketEncoder INSTANCE = new SuplaDataPacketEncoder(); | ||
|
||
@Override | ||
public byte[] encode(SuplaDataPacket proto) { | ||
final byte[] bytes = new byte[proto.size()]; | ||
int offset = 0; | ||
|
||
offset += PrimitiveEncoder.INSTANCE.writeUnsignedByte(proto.version, bytes, offset); | ||
offset += PrimitiveEncoder.INSTANCE.writeUnsignedInt(proto.rrId, bytes, offset); | ||
offset += PrimitiveEncoder.INSTANCE.writeUnsignedInt(proto.callId, bytes, offset); | ||
offset += PrimitiveEncoder.INSTANCE.writeUnsignedInt(proto.dataSize, bytes, offset); | ||
offset += PrimitiveEncoder.INSTANCE.writeByteArray(proto.data, bytes, offset); | ||
|
||
return bytes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
protocol/src/main/java/pl/grzeslowski/jsupla/protocol/api/structs/SuplaDataPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package pl.grzeslowski.jsupla.protocol.api.structs; | ||
|
||
import pl.grzeslowski.jsupla.protocol.api.types.ProtoWithSize; | ||
|
||
import static pl.grzeslowski.jsupla.protocol.api.JavaConsts.*; | ||
import static pl.grzeslowski.jsupla.protocol.api.Preconditions.checkArrayLength; | ||
import static pl.grzeslowski.jsupla.protocol.api.Preconditions.unsigned; | ||
|
||
/** | ||
* Original code: | ||
* <pre> | ||
* typedef struct { | ||
* char tag[SUPLA_TAG_SIZE]; | ||
* unsigned char version; | ||
* unsigned _supla_int_t rr_id; // Request/Response ID | ||
* unsigned _supla_int_t call_id; | ||
* unsigned _supla_int_t data_size; | ||
* char data[SUPLA_MAX_DATA_SIZE]; // Last variable in struct! | ||
* } TSuplaDataPacket; | ||
* </pre> | ||
*/ | ||
@lombok.EqualsAndHashCode | ||
@lombok.ToString | ||
public class SuplaDataPacket implements ProtoWithSize { | ||
/** | ||
* unsigned char | ||
*/ | ||
public final short version; | ||
/** | ||
* Request/Response ID | ||
* <p> | ||
* unsigned _supla_int_t | ||
*/ | ||
public final long rrId; | ||
/** | ||
* unsigned _supla_int_t | ||
*/ | ||
public final long callId; | ||
/** | ||
* unsigned _supla_int_t | ||
*/ | ||
public final long dataSize; | ||
/** | ||
* Last variable in struct! | ||
*/ | ||
public final byte[] data; | ||
|
||
public SuplaDataPacket(short version, | ||
long rrId, | ||
long callId, | ||
long dataSize, | ||
byte[] data) { | ||
this.version = unsigned(version); | ||
this.rrId = unsigned(rrId); | ||
this.callId = unsigned(callId); | ||
this.dataSize = unsigned(dataSize); | ||
this.data = checkArrayLength(data, (int) dataSize); | ||
} | ||
|
||
/* no call type */ | ||
|
||
@Override | ||
public int size() { | ||
return CHAR_SIZE // version | ||
+ INT_SIZE // rrId | ||
+ INT_SIZE // callId | ||
+ INT_SIZE // dataSize | ||
+ (int) dataSize * BYTE_SIZE // data | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters