|
| 1 | +/* |
| 2 | + * Below is the copyright agreement for IMCJava. |
| 3 | + * |
| 4 | + * Copyright (c) 2010-2025, Laboratório de Sistemas e Tecnologia Subaquática |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * - Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * - Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * - Neither the names of IMC, LSTS, IMCJava nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL LABORATORIO DE SISTEMAS E TECNOLOGIA SUBAQUATICA |
| 22 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 24 | + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + */ |
| 30 | +package pt.lsts.imc; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * IMC Message Message Fragment Control (878)<br/> |
| 35 | + * This message is used by the receiver of MessageParts messages<br/> |
| 36 | + * to inform the sender of the status of the reception of a message<br/> |
| 37 | + * in fragments.<br/> |
| 38 | + * The sender can then use this information to determine which<br/> |
| 39 | + * fragments were received and which ones were not.<br/> |
| 40 | + * This message is sent in response to a MessagePart message.<br/> |
| 41 | + */ |
| 42 | + |
| 43 | +public class MessagePartControl extends IMCMessage { |
| 44 | + |
| 45 | + public enum OP { |
| 46 | + STATUS_RECEIVED(0), |
| 47 | + REQUEST_RETRANSMIT(1); |
| 48 | + |
| 49 | + protected long value; |
| 50 | + |
| 51 | + public long value() { |
| 52 | + return value; |
| 53 | + } |
| 54 | + |
| 55 | + OP(long value) { |
| 56 | + this.value = value; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static final int ID_STATIC = 878; |
| 61 | + |
| 62 | + public MessagePartControl() { |
| 63 | + super(ID_STATIC); |
| 64 | + } |
| 65 | + |
| 66 | + public MessagePartControl(IMCMessage msg) { |
| 67 | + super(ID_STATIC); |
| 68 | + try{ |
| 69 | + copyFrom(msg); |
| 70 | + } |
| 71 | + catch (Exception e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public MessagePartControl(IMCDefinition defs) { |
| 77 | + super(defs, ID_STATIC); |
| 78 | + } |
| 79 | + |
| 80 | + public static MessagePartControl create(Object... values) { |
| 81 | + MessagePartControl m = new MessagePartControl(); |
| 82 | + for (int i = 0; i < values.length-1; i+= 2) |
| 83 | + m.setValue(values[i].toString(), values[i+1]); |
| 84 | + return m; |
| 85 | + } |
| 86 | + |
| 87 | + public static MessagePartControl clone(IMCMessage msg) throws Exception { |
| 88 | + |
| 89 | + MessagePartControl m = new MessagePartControl(); |
| 90 | + if (msg == null) |
| 91 | + return m; |
| 92 | + if(msg.definitions != m.definitions){ |
| 93 | + msg = msg.cloneMessage(); |
| 94 | + IMCUtil.updateMessage(msg, m.definitions); |
| 95 | + } |
| 96 | + else if (msg.getMgid()!=m.getMgid()) |
| 97 | + throw new Exception("Argument "+msg.getAbbrev()+" is incompatible with message "+m.getAbbrev()); |
| 98 | + |
| 99 | + m.getHeader().values.putAll(msg.getHeader().values); |
| 100 | + m.values.putAll(msg.values); |
| 101 | + return m; |
| 102 | + } |
| 103 | + |
| 104 | + public MessagePartControl(short uid, OP op, String frag_ids) { |
| 105 | + super(ID_STATIC); |
| 106 | + setUid(uid); |
| 107 | + setOp(op); |
| 108 | + if (frag_ids != null) |
| 109 | + setFragIds(frag_ids); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return Transmission Unique Id - uint8_t |
| 114 | + */ |
| 115 | + public short getUid() { |
| 116 | + return (short) getInteger("uid"); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param uid Transmission Unique Id |
| 121 | + */ |
| 122 | + public MessagePartControl setUid(short uid) { |
| 123 | + values.put("uid", uid); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return Operation (enumerated) - uint8_t |
| 129 | + */ |
| 130 | + public OP getOp() { |
| 131 | + try { |
| 132 | + OP o = OP.valueOf(getMessageType().getFieldPossibleValues("op").get(getLong("op"))); |
| 133 | + return o; |
| 134 | + } |
| 135 | + catch (Exception e) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public String getOpStr() { |
| 141 | + return getString("op"); |
| 142 | + } |
| 143 | + |
| 144 | + public short getOpVal() { |
| 145 | + return (short) getInteger("op"); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param op Operation (enumerated) |
| 150 | + */ |
| 151 | + public MessagePartControl setOp(OP op) { |
| 152 | + values.put("op", op.value()); |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param op Operation (as a String) |
| 158 | + */ |
| 159 | + public MessagePartControl setOpStr(String op) { |
| 160 | + setValue("op", op); |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param op Operation (integer value) |
| 166 | + */ |
| 167 | + public MessagePartControl setOpVal(short op) { |
| 168 | + setValue("op", op); |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @return Fragments IDs - plaintext |
| 174 | + */ |
| 175 | + public String getFragIds() { |
| 176 | + return getString("frag_ids"); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @param frag_ids Fragments IDs |
| 181 | + */ |
| 182 | + public MessagePartControl setFragIds(String frag_ids) { |
| 183 | + values.put("frag_ids", frag_ids); |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments