lwm2m
representation from it. See OMA-LWM2M
14 | * specification, chapter 6.1 for the resource model and chapter 6.3.3 for
15 | * the OMA-TLV specification.
16 | */
17 | public class TLVDeserializer {
18 |
19 | private static class TypeIdLength {
20 | private byte[] tlv;
21 | private int offset;
22 | private int type;
23 | private int id;
24 | private int length;
25 |
26 | private static TypeIdLength createTypeIdLength(byte[] tlv, int offset) {
27 | TypeIdLength til = new TypeIdLength();
28 | til.tlv = tlv;
29 | til.offset = offset;
30 | til.type = tlv[offset] & (byte) 0b11_000000;
31 | til.id = -1;
32 | til.length = 0;
33 | return til;
34 | }
35 |
36 | private TypeIdLength deserialize() {
37 | try {
38 | int idLength = tlv[offset] & TLV.ID16;
39 | int lengthType = tlv[offset] & TLV.LENGTH24;
40 | if (lengthType == 0) {
41 | length = tlv[offset] & (byte) 0b00000_111;
42 | }
43 | offset++;
44 |
45 | deserialiseID(idLength);
46 | deserialiseLength(lengthType);
47 | } catch (IndexOutOfBoundsException exception) {
48 | throw new IllegalArgumentException("Premature end of content...", exception);
49 | }
50 |
51 | return this;
52 | }
53 |
54 | private void deserialiseID (int idLength) {
55 | id = tlv[offset++] & 0xFF;
56 | if (idLength == TLV.ID16) {
57 | id = (id << 8) + (tlv[offset++] & 0xFF);
58 | }
59 | }
60 |
61 | private void deserialiseLength (int lengthType) {
62 | if (lengthType > 0) {
63 | length = tlv[offset++] & 0xFF;
64 | }
65 | if (lengthType > TLV.LENGTH8) {
66 | length = (length << 8) + (tlv[offset++] & 0xFF);
67 | }
68 | if (lengthType > TLV.LENGTH16) {
69 | length = (length << 8) + (tlv[offset++] & 0xFF);
70 | }
71 | }
72 |
73 | }
74 |
75 | /**
76 | * This method checks whether the given binary encodes an object instance
77 | * or something else. It returns true
if bits 7-6 of the first
78 | * byte is "00".
79 | * @param tlv Binary to be checked as LWM2M object instance
80 | * @return true
or false
.
81 | */
82 | public static boolean isObjectInstance (byte[] tlv) {
83 | return isObjectInstance(tlv, 0);
84 | }
85 |
86 | /**
87 | * This method checks whether the given binary encodes a resource or
88 | * something else. It returns true
if bits 7-6 of the first
89 | * byte is "11".
90 | * @param tlv Binary to be checked as LWM2M resource.
91 | * @return true
or false
.
92 | */
93 | public static boolean isResource (byte[] tlv) {
94 | return isResource(tlv, 0);
95 | }
96 |
97 | /**
98 | * This method checks whether the given binary encodes a multiple resource
99 | * or something else. It returns true
if bits 7-6 of the first
100 | * byte is "10".
101 | * @param tlv Binary to be checked as LWM2M multiple resource.
102 | * @return true
or false
.
103 | */
104 | public static boolean isMultipleResource (byte[] tlv) {
105 | return isMultipleResource(tlv, 0);
106 | }
107 |
108 | /**
109 | * This method checks whether the given binary encodes a resource instance
110 | * or something else. It returns true
if bits 7-6 of the first
111 | * byte is "01".
112 | * @param tlv Binary to be checked as LWM2M resource instance.
113 | * @return true
or false
.
114 | */
115 | public static boolean isResourceInstance (byte[] tlv) {
116 | return isResourceInstance(tlv, 0);
117 | }
118 |
119 | /**
120 | * Deserialises the given binary that must encode object instances. Binary
121 | * array can be checked before invoking this method with
122 | * {@link #isObjectInstance(byte[])}.
123 | * @param tlv Binary in OMA-TLV format
124 | * @return List of LWM2MObjectInstance
objects.
125 | * @throws IllegalArgumentException if given binary is not a valid OMA-TLV
126 | * or it encodes a structure other than object instances.
127 | * @see #deserializeResources(byte[])
128 | */
129 | public static ListLWM2MObjectInstance
objects.
142 | * @throws IllegalArgumentException if given binary is not a valid OMA-TLV
143 | * or it encodes a structure other than object instances.
144 | * @see #deserializeResources(byte[])
145 | */
146 | public static ListName | 97 |Type | 98 |
---|---|
{{x.name }} | 105 |{{ x.type }} | 106 |