Package: Hip

Hip

nameinstructionbranchcomplexitylinemethod
Hip(List)
M: 173 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
Hip(byte[])
M: 150 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 28 C: 0
0%
M: 1 C: 0
0%
getHIPmessage()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMessageID()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPayload()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isError()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011, 2018 Eurotech and/or its affiliates
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech
11: *******************************************************************************/
12: package org.eclipse.kura.net.admin.modem.sierra;
13:
14: import java.util.ArrayList;
15: import java.util.List;
16:
17: /**
18: * Describes HIP (Host Interface Protocol) object.
19: * <p>
20: * The Host Interface Protocol is a multiplexing layer designed to allow
21: * several types of message streams to share the same physical link layer. CnS is
22: * one of the supported streams.
23: * HIP is used to carry control and status data between the modem and the host to
24: * provide management of the modem device. This management consists of:
25: * <p>
26: * � Non-volatile configuration of the device
27: * <p>
28: * � Run-time configuration of the device
29: * <p>
30: * � Status reporting and monitoring of the device
31: * <p>
32: * This protocol does not include checksums. It relies on a high-reliability physical
33: * interface between the host and the modem to reduce the processing burden on the
34: * microprocessor.
35: * <p>
36: * HIP format:
37: * <table border="1">
38: * <th>Byte offset</th>
39: * <th>Content</th>
40: * <tr>
41: * <td>0</td>
42: * <td>Framing character (0x7E)</td>
43: * </tr>
44: * <tr>
45: * <td>1-2</td>
46: * <td>Length of ooptional payload</td>
47: * </tr>
48: * <tr>
49: * <td>3</td>
50: * <td>Message ID</td>
51: * </tr>
52: * <tr>
53: * <td>4</td>
54: * <td>Parameter</td>
55: * </tr>
56: * <tr>
57: * <td>5-n</td>
58: * <td>Optional payload</td>
59: * </tr>
60: * <tr>
61: * <td>n+1</td>
62: * <td>Framing character (0x7E)</td>
63: * </tr>
64: * </table>
65: * <p>
66: * The minimum length of a HIP packet is six bytes, for a packet without the
67: * optional payload. Note that such a packet would have the first field set
68: * to 0x0000�no payload.
69: * <p>
70: *
71: */
72: public class Hip {
73:
74: /**
75: * Indicates host to modem message direction.
76: */
77: public static final byte MSGID_CNS_HOST2MODEM = (byte) 0x2B;
78:
79: /**
80: * Indicates modem to host message direction.
81: */
82: public static final byte MSGID_CNS_MODEM2HOST = (byte) 0x6B;
83:
84: /**
85: * Minimum message size.
86: */
87: public static final int MIN_MSG_SIZE = 6;
88:
89: /**
90: * Frame byte delimiter.
91: */
92: public static final byte FRAME_BYTE = (byte) 0x7e;
93:
94: private static final byte ESCAPE_BYTE = (byte) 0x7d;
95: private static final byte MASK_BYTE = (byte) 0x20;
96:
97: private static final int MESSAGE_ID_OFFSET = 2;
98: private static final int PARAMETER_OFFSET = 3;
99: private static final int PAYLOAD_OFFSET = 4;
100:
101: private int payloadlength = 0;
102: private byte messageId = 0;
103: private byte parameter = 0;
104:
105: private byte[] hipmsg = null;
106: private byte[] payload = null;
107:
108: private boolean is_error = false;
109:
110: /**
111: * HIP request constructor
112: *
113: * @param payload
114: * - HIP payload
115: */
116: public Hip(byte[] payload) {
117:
118: this.messageId = MSGID_CNS_HOST2MODEM;
119: this.payloadlength = payload.length;
120:
121: List<Byte> alMsg = new ArrayList<>();
122:
123: // form HIP message
124: alMsg.add(Byte.valueOf((byte) (this.payloadlength >> 8 & 0xff)));
125: alMsg.add(Byte.valueOf((byte) (this.payloadlength & 0xff)));
126: alMsg.add(Byte.valueOf(this.messageId));
127: alMsg.add(Byte.valueOf(this.parameter));
128:• for (int i = 0; i < this.payloadlength; i++) {
129: alMsg.add(Byte.valueOf(payload[i]));
130: }
131:
132: // perform escape character processing
133:• for (int i = 0; i < alMsg.size(); i++) {
134: byte b = alMsg.get(i).byteValue();
135:• if (b == FRAME_BYTE || b == ESCAPE_BYTE) {
136: alMsg.remove(i);
137: alMsg.add(i++, Byte.valueOf(ESCAPE_BYTE));
138: alMsg.add(i, Byte.valueOf((byte) (b ^ MASK_BYTE)));
139: }
140: }
141:
142: // add start and end framing bytes
143: alMsg.add(0, Byte.valueOf(FRAME_BYTE));
144: alMsg.add(Byte.valueOf(FRAME_BYTE));
145:
146: // convert message to array of bytes
147: this.hipmsg = new byte[alMsg.size()];
148:• for (int i = 0; i < this.hipmsg.length; i++) {
149: this.hipmsg[i] = alMsg.get(i).byteValue();
150: }
151: }
152:
153: /**
154: * HIP reply constructor
155: *
156: * @param alMsg
157: * - HIP reply
158: */
159: public Hip(List<Byte> alMsg) {
160:
161: // convert supplied message to array of bytes
162: this.hipmsg = new byte[alMsg.size()];
163:• for (int i = 0; i < this.hipmsg.length; i++) {
164: this.hipmsg[i] = alMsg.get(i).byteValue();
165: }
166:
167: // remove starting and ending flags
168: try {
169:• if (alMsg.get(alMsg.size() - 1).byteValue() == FRAME_BYTE) {
170: alMsg.remove(alMsg.size() - 1);
171: }
172:• if (alMsg.get(0).byteValue() == FRAME_BYTE) {
173: alMsg.remove(0);
174: }
175:
176: // perform escape character processing
177:• for (int i = 0; i < alMsg.size(); i++) {
178: byte b = alMsg.get(i).byteValue();
179:• if (b == ESCAPE_BYTE) {
180: alMsg.remove(i);
181: b = alMsg.get(i).byteValue();
182: alMsg.remove(i);
183: alMsg.add(i, Byte.valueOf((byte) (b ^ MASK_BYTE)));
184: }
185: }
186:
187: // get HIP header fields
188:
189: this.payloadlength = alMsg.get(0).byteValue() << 8 & 0x0ffff | alMsg.get(1).byteValue() & 0x0ff;
190:
191: this.messageId = alMsg.get(MESSAGE_ID_OFFSET).byteValue();
192: this.parameter = alMsg.get(PARAMETER_OFFSET).byteValue();
193:
194: // get HIP payload
195:
196: this.payload = new byte[this.payloadlength];
197:• for (int i = 0; i < this.payloadlength; i++) {
198: this.payload[i] = alMsg.get(PAYLOAD_OFFSET + i).byteValue();
199: }
200: } catch (Exception e) {
201: this.is_error = true;
202: }
203: }
204:
205: /**
206: * Reports HIP message
207: *
208: * @return byte [] - message
209: */
210: public byte[] getHIPmessage() {
211: return this.hipmsg;
212: }
213:
214: /**
215: * Reports HIP payload
216: *
217: * @return HIP payload
218: */
219: public byte[] getPayload() {
220: return this.payload;
221: }
222:
223: /**
224: * Reports message ID
225: *
226: * @return Message ID
227: */
228: public byte getMessageID() {
229: return this.messageId;
230: }
231:
232: /**
233: * Reports if there is an error in processing HIP reply
234: *
235: * @return boolean <br>
236: * true - error <br>
237: * false - no error
238: */
239: public boolean isError() {
240: return this.is_error;
241: }
242: }