Skip to content

Commit

Permalink
feat: add DeviceDiscoveryRequest model
Browse files Browse the repository at this point in the history
Signed-off-by: wei <493703217@qq.com>
  • Loading branch information
instpe committed Nov 17, 2024
1 parent 7e1bc04 commit 6342d0b
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 2 deletions.
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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L13

Added line #L13 was not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L15

Added line #L15 was not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L17

Added line #L17 was not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L19-L20

Added lines #L19 - L20 were not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L23-L24

Added lines #L23 - L24 were not covered by tests
}

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/AbstractMessageBody.java#L28-L29

Added lines #L28 - L29 were not covered by tests
}
}
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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeader.java#L29-L36

Added lines #L29 - L36 were not covered by tests
}
}
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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L18-L21

Added lines #L18 - L21 were not covered by tests
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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L23

Added line #L23 was not covered by tests
}
return buffer;

Check warning on line 25 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L25

Added line #L25 was not covered by tests
}

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L29-L32

Added lines #L29 - L32 were not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L34-L35

Added lines #L34 - L35 were not covered by tests
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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L37

Added line #L37 was not covered by tests
}
return request;

Check warning on line 39 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryRequest.java#L39

Added line #L39 was not covered by tests
}
}
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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L33-L38

Added lines #L33 - L38 were not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L41-L42

Added lines #L41 - L42 were not covered by tests
}

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L45-L46

Added lines #L45 - L46 were not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L49

Added line #L49 was not covered by tests
}

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L53-L54

Added lines #L53 - L54 were not covered by tests
}

return buffer;

Check warning on line 57 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DeviceDiscoveryResponse.java#L57

Added line #L57 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package iot.github.protocol.mdtp.common.model;
package io.github.protocol.mdtp.common.model;

import lombok.Getter;

Expand Down
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);

Check warning on line 18 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java#L18

Added line #L18 was not covered by tests
if (securityHeader != null) {
securityHeader.toByteBuf(buffer);

Check warning on line 20 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java#L20

Added line #L20 was not covered by tests
}
if (body != null) {
body.toByteBuf(buffer);

Check warning on line 23 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java#L23

Added line #L23 was not covered by tests
}
return buffer;

Check warning on line 25 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MdtpPacket.java#L25

Added line #L25 was not covered by tests
}
}
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),

Check warning on line 4 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java#L3-L4

Added lines #L3 - L4 were not covered by tests

RESPONSE(1),

Check warning on line 6 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java#L6

Added line #L6 was not covered by tests

NOTIFY(2);

Check warning on line 8 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java#L8

Added line #L8 was not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java#L12-L14

Added lines #L12 - L14 were not covered by tests

public int getCode() {
return code;

Check warning on line 17 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageType.java#L17

Added line #L17 was not covered by tests
}
}
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);

Check warning on line 12 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/SecurityHeader.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/SecurityHeader.java#L12

Added line #L12 was not covered by tests
}
return buffer;

Check warning on line 14 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/SecurityHeader.java

View check run for this annotation

Codecov / codecov/patch

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/SecurityHeader.java#L14

Added line #L14 was not covered by tests
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package iot.github.protocol.mdtp.common.model;
package io.github.protocol.mdtp.common.model;

import lombok.Getter;

Expand Down
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;

}

0 comments on commit 6342d0b

Please sign in to comment.