-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Signed-off-by: wei <493703217@qq.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.Data; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
public abstract class AbstractMessageBody { | ||
private short messageBodyHeader; | ||
|
||
public void setMessageBodyHeader(MessageType messageType, ServiceGroup serviceGroup, DiscoveryServiceCode serviceCode) { | ||
this.messageBodyHeader = 0; | ||
Check warning on line 13 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L13
|
||
|
||
this.messageBodyHeader |= (short) (messageType.getCode() & 0b111); | ||
Check warning on line 15 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L15
|
||
|
||
this.messageBodyHeader |= (short) ((serviceGroup.getCode() & 0b1111111) << 3); | ||
Check warning on line 17 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L17
|
||
|
||
this.messageBodyHeader |= (short) ((serviceCode.getCode() & 0b111111) << 10); | ||
} | ||
Check warning on line 20 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L19-L20
|
||
|
||
public short generateRequestId() { | ||
UUID uuid = UUID.randomUUID(); | ||
return (short) (uuid.getLeastSignificantBits() & 0xFFFF); | ||
Check warning on line 24 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L23-L24
|
||
} | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
buffer.writeShort(messageBodyHeader); | ||
return buffer; | ||
Check warning on line 29 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L28-L29
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CDATHeader { | ||
|
||
private byte formatType; | ||
|
||
private byte protocolVersion; | ||
|
||
private short messageLength; | ||
|
||
private long timestamp; | ||
|
||
private byte flags; | ||
|
||
private Integer sequenceNumber; | ||
|
||
private Integer logicalChannelId; | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
buffer.writeByte(formatType); | ||
buffer.writeByte(protocolVersion); | ||
buffer.writeShort(messageLength); | ||
buffer.writeLong(timestamp); | ||
buffer.writeByte(flags); | ||
buffer.writeInt(sequenceNumber); | ||
buffer.writeInt(logicalChannelId); | ||
return buffer; | ||
Check warning on line 36 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeader.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeader.java#L29-L36
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.Data; | ||
|
||
|
||
@Data | ||
public class DeviceDiscoveryRequest extends AbstractMessageBody { | ||
private short requestId; | ||
|
||
private byte mask; | ||
|
||
private byte deviceTypeCount; | ||
|
||
private int[] deviceTypes; | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
super.toByteBuf(buffer); | ||
buffer.writeShort(requestId); | ||
buffer.writeByte(mask); | ||
buffer.writeByte(deviceTypeCount); | ||
Check warning on line 21 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L18-L21
|
||
for (int deviceType : deviceTypes) { | ||
buffer.writeInt(deviceType); | ||
Check warning on line 23 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L23
|
||
} | ||
return buffer; | ||
Check warning on line 25 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L25
|
||
} | ||
|
||
public static DeviceDiscoveryRequest fromByteBuf(ByteBuf data) { | ||
DeviceDiscoveryRequest request = new DeviceDiscoveryRequest(); | ||
request.requestId = data.readShort(); | ||
request.mask = data.readByte(); | ||
request.deviceTypeCount = data.readByte(); | ||
Check warning on line 32 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L29-L32
|
||
|
||
int length = request.deviceTypeCount; | ||
request.deviceTypes = new int[length]; | ||
Check warning on line 35 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L34-L35
|
||
for (int i = 0; i < length; i++) { | ||
request.deviceTypes[i] = data.readInt(); | ||
Check warning on line 37 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L37
|
||
} | ||
return request; | ||
Check warning on line 39 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L39
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.Data; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
@Data | ||
public class DeviceDiscoveryResponse { | ||
private short messageHeader; | ||
|
||
private short requestId; | ||
|
||
private short responseId; | ||
|
||
private byte mask; | ||
|
||
private byte deviceStatus; | ||
|
||
private byte addressCount; | ||
|
||
private String[] addresses; | ||
|
||
private short port; | ||
|
||
private int deviceType; | ||
|
||
private byte[] uniqueId; | ||
|
||
private String deviceName; | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
buffer.writeShort(messageHeader); | ||
buffer.writeShort(requestId); | ||
buffer.writeShort(responseId); | ||
buffer.writeByte(mask); | ||
buffer.writeByte(deviceStatus); | ||
buffer.writeByte(addressCount); | ||
Check warning on line 38 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L33-L38
|
||
|
||
for (String address : addresses) { | ||
byte[] addressBytes = address.getBytes(StandardCharsets.UTF_8); | ||
buffer.writeBytes(addressBytes); | ||
Check warning on line 42 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L41-L42
|
||
} | ||
|
||
buffer.writeShort(port); | ||
buffer.writeInt(deviceType); | ||
Check warning on line 46 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L45-L46
|
||
|
||
if (uniqueId != null) { | ||
buffer.writeBytes(uniqueId); | ||
Check warning on line 49 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L49
|
||
} | ||
|
||
if (deviceName != null) { | ||
byte[] nameBytes = deviceName.getBytes(StandardCharsets.UTF_8); | ||
buffer.writeBytes(nameBytes); | ||
Check warning on line 54 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L53-L54
|
||
} | ||
|
||
return buffer; | ||
Check warning on line 57 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L57
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class MdtpPacket { | ||
|
||
private CDATHeader header; | ||
|
||
private SecurityHeader securityHeader; | ||
|
||
private AbstractMessageBody body; | ||
|
||
private Signature signature; | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
header.toByteBuf(buffer); | ||
if (securityHeader != null) { | ||
securityHeader.toByteBuf(buffer); | ||
} | ||
if (body != null) { | ||
body.toByteBuf(buffer); | ||
} | ||
return buffer; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
public enum MessageType { | ||
REQUEST(0), | ||
|
||
RESPONSE(1), | ||
|
||
NOTIFY(2); | ||
|
||
private final int code; | ||
|
||
MessageType(int code) { | ||
this.code = code; | ||
} | ||
Check warning on line 14 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java#L12-L14
|
||
|
||
public int getCode() { | ||
return code; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class SecurityHeader { | ||
private byte[] encryptionData; | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
for (int data : encryptionData) { | ||
buffer.writeInt(data); | ||
} | ||
return buffer; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Signature { | ||
private byte[] signatureData; | ||
|
||
} |