Skip to content

Commit 873945b

Browse files
authored
feat: add device discover request (#11)
Signed-off-by: wei <[email protected]>
1 parent cf32c2c commit 873945b

32 files changed

+504
-91
lines changed

mdtp-client/src/main/java/io/github/protocol/mdtp/client/MdtpClient.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.protocol.mdtp.client;
22

3+
import io.github.protocol.mdtp.common.model.CDATHeader;
4+
import io.github.protocol.mdtp.common.model.DeviceDiscoveryRequest;
5+
import io.github.protocol.mdtp.common.model.MdtpPacket;
6+
import io.github.protocol.mdtp.common.model.MessageBodyHeader;
37
import io.netty.bootstrap.Bootstrap;
48
import io.netty.channel.ChannelFuture;
59
import io.netty.channel.ChannelInitializer;
@@ -18,6 +22,9 @@ public class MdtpClient implements Closeable {
1822

1923
private EventLoopGroup group;
2024

25+
private ChannelFuture channelFuture;
26+
27+
2128
public MdtpClient(MdtpClientConfig config) {
2229
this.config = config;
2330
}
@@ -37,7 +44,7 @@ public void start() throws Exception {
3744
protected void initChannel(SocketChannel ch) throws Exception {
3845
}
3946
});
40-
ChannelFuture channelFuture = bootstrap.connect().sync();
47+
this.channelFuture = bootstrap.connect().sync();
4148
if (channelFuture.isSuccess()) {
4249
log.info("mdtp client started");
4350
} else {
@@ -55,4 +62,39 @@ public void close() throws IOException {
5562
this.group.shutdownGracefully();
5663
log.info("mdtp client closed");
5764
}
65+
66+
public void sendDeviceDiscoveryRequest(int[] deviceTypes) {
67+
log.info("start to send device discovery request.");
68+
DeviceDiscoveryRequest request = new DeviceDiscoveryRequest();
69+
request.setMessageBodyHeader(MessageBodyHeader.DEVICE_DISCOVERY_REQUEST);
70+
request.setRequestId(request.generateRequestId());
71+
72+
if (deviceTypes == null) {
73+
request.setDeviceTypeCount((byte) 0);
74+
}
75+
76+
if (deviceTypes != null && deviceTypes.length > 0) {
77+
request.setMask((byte) 1);
78+
request.setDeviceTypeCount((byte) deviceTypes.length);
79+
request.setDeviceTypes(deviceTypes);
80+
}
81+
82+
CDATHeader cdatHeader = new CDATHeader();
83+
cdatHeader.setFormatType((byte) 0x02);
84+
cdatHeader.setProtocolVersion((byte) 1);
85+
cdatHeader.setMessageLength((short) 0);
86+
cdatHeader.setTimestamp(System.currentTimeMillis());
87+
cdatHeader.setFlags((byte) 0b01100000);
88+
cdatHeader.setSequenceNumber(0);
89+
cdatHeader.setLogicalChannelId(0);
90+
91+
MdtpPacket packet = new MdtpPacket();
92+
packet.setHeader(cdatHeader);
93+
packet.setSecurityHeader(null);
94+
packet.setBody(request);
95+
packet.setSignature(null);
96+
97+
this.channelFuture.channel().writeAndFlush(packet.toByteBuf());
98+
log.info("send device discovery request success: " + packet);
99+
}
58100
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.protocol.mdtp.common.codec;
2+
3+
import io.github.protocol.mdtp.common.model.AbstractMessageBody;
4+
import io.github.protocol.mdtp.common.model.DeviceDiscoveryRequest;
5+
import io.netty.buffer.ByteBuf;
6+
7+
public class DeviceDiscoveryRequestDecoder implements MessageBodyDecoder {
8+
@Override
9+
public AbstractMessageBody handle(ByteBuf in) {
10+
return DeviceDiscoveryRequest.fromByteBuf(in);
11+
}
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.protocol.mdtp.common.codec;
2+
3+
4+
import io.github.protocol.mdtp.common.model.AbstractMessageBody;
5+
import io.github.protocol.mdtp.common.model.CDATHeader;
6+
import io.github.protocol.mdtp.common.model.MdtpPacket;
7+
import io.github.protocol.mdtp.common.model.MessageBodyHeader;
8+
import io.netty.buffer.ByteBuf;
9+
import io.netty.channel.ChannelHandlerContext;
10+
import io.netty.handler.codec.ByteToMessageDecoder;
11+
import lombok.extern.slf4j.Slf4j;
12+
13+
import java.util.List;
14+
15+
@Slf4j
16+
public class MdtpDecoder extends ByteToMessageDecoder {
17+
18+
@Override
19+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
20+
21+
MdtpPacket mdtpPacket = new MdtpPacket();
22+
CDATHeader header = CDATHeader.readByteBuf(in);
23+
24+
MessageBodyHeader messageBodyHeader = MessageBodyHeader.readByteBuf(in);
25+
MessageBodyDecoder messageDecode = MessageDecoderFactory.getDecoder(messageBodyHeader);
26+
AbstractMessageBody messageBody = messageDecode.handle(in);
27+
messageBody.setMessageBodyHeader(messageBodyHeader);
28+
29+
mdtpPacket.setHeader(header);
30+
mdtpPacket.setBody(messageBody);
31+
32+
out.add(mdtpPacket);
33+
}
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.protocol.mdtp.common.codec;
2+
3+
import io.github.protocol.mdtp.common.model.AbstractMessageBody;
4+
import io.netty.buffer.ByteBuf;
5+
6+
public interface MessageBodyDecoder {
7+
AbstractMessageBody handle(ByteBuf in);
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.protocol.mdtp.common.codec;
2+
3+
import io.github.protocol.mdtp.common.model.MessageBodyHeader;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class MessageDecoderFactory {
9+
10+
private static final Map<Short, MessageBodyDecoder> decoders = new HashMap<>();
11+
12+
static {
13+
decoders.put(MessageBodyHeader.DEVICE_DISCOVERY_REQUEST.toShort(), new DeviceDiscoveryRequestDecoder());
14+
}
15+
16+
public static MessageBodyDecoder getDecoder(MessageBodyHeader header) {
17+
return decoders.get(header.toShort());
18+
}
19+
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,14 @@
77

88
@Data
99
public abstract class AbstractMessageBody {
10-
private short messageBodyHeader;
11-
12-
public void setMessageBodyHeader(MessageType messageType, ServiceGroup serviceGroup, DiscoveryServiceCode serviceCode) {
13-
this.messageBodyHeader = 0;
14-
this.messageBodyHeader |= (short) (messageType.getCode() & 0b111);
15-
this.messageBodyHeader |= (short) ((serviceGroup.getCode() & 0b1111111) << 3);
16-
this.messageBodyHeader |= (short) ((serviceCode.getCode() & 0b111111) << 10);
17-
}
10+
private MessageBodyHeader messageBodyHeader;
1811

1912
public short generateRequestId() {
2013
UUID uuid = UUID.randomUUID();
2114
return (short) (uuid.getLeastSignificantBits() & 0xFFFF);
2215
}
2316

24-
public ByteBuf toByteBuf(ByteBuf buffer) {
25-
buffer.writeShort(messageBodyHeader);
26-
return buffer;
17+
public void writeByteBuf(ByteBuf buffer) {
18+
messageBodyHeader.writeByteBuf(buffer);
2719
}
2820
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,27 @@ public class CDATHeader {
2525

2626
private Integer logicalChannelId;
2727

28-
public ByteBuf toByteBuf(ByteBuf buffer) {
28+
public void writeByteBuf(ByteBuf buffer) {
2929
buffer.writeByte(formatType);
3030
buffer.writeByte(protocolVersion);
3131
buffer.writeShort(messageLength);
3232
buffer.writeLong(timestamp);
3333
buffer.writeByte(flags);
3434
buffer.writeInt(sequenceNumber);
3535
buffer.writeInt(logicalChannelId);
36-
return buffer;
36+
}
37+
38+
public static CDATHeader readByteBuf(ByteBuf buffer) {
39+
CDATHeader header = new CDATHeader();
40+
41+
header.setFormatType(buffer.readByte());
42+
header.setProtocolVersion(buffer.readByte());
43+
header.setMessageLength(buffer.readShort());
44+
header.setTimestamp(buffer.readLong());
45+
header.setFlags(buffer.readByte());
46+
header.setSequenceNumber(buffer.readInt());
47+
header.setLogicalChannelId(buffer.readInt());
48+
49+
return header;
3750
}
3851
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
@Data
8-
public class DeviceDiscoveryRequest extends AbstractMessageBody {
8+
public class DeviceDiscoveryRequest extends AbstractMessageBody{
9+
910
private short requestId;
1011

1112
private byte mask;
@@ -14,15 +15,16 @@ public class DeviceDiscoveryRequest extends AbstractMessageBody {
1415

1516
private int[] deviceTypes;
1617

17-
public ByteBuf toByteBuf(ByteBuf buffer) {
18-
super.toByteBuf(buffer);
18+
public void writeByteBuf(ByteBuf buffer) {
19+
super.writeByteBuf(buffer);
1920
buffer.writeShort(requestId);
2021
buffer.writeByte(mask);
2122
buffer.writeByte(deviceTypeCount);
22-
for (int deviceType : deviceTypes) {
23-
buffer.writeInt(deviceType);
23+
if (deviceTypeCount > 0) {
24+
for (int deviceType : deviceTypes) {
25+
buffer.writeInt(deviceType);
26+
}
2427
}
25-
return buffer;
2628
}
2729

2830
public static DeviceDiscoveryRequest fromByteBuf(ByteBuf data) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class DeviceDiscoveryResponse {
2929

3030
private String deviceName;
3131

32-
public ByteBuf toByteBuf(ByteBuf buffer) {
32+
public void writeByteBuf(ByteBuf buffer) {
3333
buffer.writeShort(messageHeader);
3434
buffer.writeShort(requestId);
3535
buffer.writeShort(responseId);
@@ -53,7 +53,5 @@ public ByteBuf toByteBuf(ByteBuf buffer) {
5353
byte[] nameBytes = deviceName.getBytes(StandardCharsets.UTF_8);
5454
buffer.writeBytes(nameBytes);
5555
}
56-
57-
return buffer;
5856
}
5957
}

mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/DiscoveryServiceCode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ public enum DiscoveryServiceCode {
1616
this.description = description;
1717
}
1818

19+
public static DiscoveryServiceCode fromCode(int value) {
20+
for (DiscoveryServiceCode serviceCode : DiscoveryServiceCode.values()) {
21+
if (serviceCode.getCode() == value) {
22+
return serviceCode;
23+
}
24+
}
25+
throw new IllegalArgumentException("Invalid DiscoveryServiceCode: " + value);
26+
}
1927
}

0 commit comments

Comments
 (0)