Alejandro Acuña
2024-10-25 e51f4a713ed6e744c203c9493165584728a29c52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package art.servers.pvvserver.protocols.dgt.clv;
 
import art.library.utils.common.ArrayUtils;
import java.util.ArrayList;
import java.util.List;
 
 
public class ClvDgtPDU
{
    public int stx;
    public int etx;
    public int crc1;
    public int crc2;
    public int function;
    public int address;
    public boolean ack = false;
    public int[] information = new int[0];
 
    private int index = 0;
 
    public void checkOrder(ClvDgtPDU request) throws Exception
    {
    }
 
    public int readByte() throws Exception
    {
        return information[index++];
    }
 
    public int readShort() throws Exception
    {
        return (readByte() << 8) + readByte();
    }
 
    public int readInt() throws Exception
    {
        return (readByte() << 24) + (readByte() << 16) + (readByte() << 8) + readByte();
    }
 
    public long readLong() throws Exception
    {
        long res = ((long) readByte() << 56);
        res = res + ((long) readByte() << 48);
        res = res + ((long) readByte() << 40);
        res = res + ((long) readByte() << 32);
        res = res + ((long) readByte() << 24);
        res = res + ((long) readByte() << 16);
        res = res + ((long) readByte() << 8);
        res = res + (long) readByte();
        return (res);
    }
 
    public int[] readFully() throws Exception
    {
        int[] resultado = new int[information.length - index];
        System.arraycopy(information, index, resultado, 0, resultado.length);
        return resultado;
    }
 
    public int[] readIntArray(int count) throws Exception
    {
        int[] resultado = new int[count];
        System.arraycopy(information, index, resultado, 0, resultado.length);
        index = index + count;
        return resultado;
    }
 
    public int[] readIntArrayUntilValue(int limit) throws Exception
    {
        List<Integer> list = new ArrayList<Integer>();
        int value = readByte();
        while (value != limit)
        {
            list.add(value);
            value = readByte();
        }
 
        int[] result = new int[list.size()];
        for (int i=0; i<list.size(); i++)
        {
            result[i] = list.get(i).intValue();
        }
 
        return result;
    }
 
    public boolean eof()
    {
        return (index == information.length);
    }
 
    public byte[] createMessage() throws Exception
    {
        int[] result = new int[6 + information.length];
        int index = 0;
        result[index++] = stx;
        result[index++] = address;
        result[index++] = function;
        for (int i = 0; i < information.length; i++)
        {
            result[index++] = information[i];
        }
 
        byte[] crc = checksum(result, 1, index - 1);
        result[index++] = crc[1];
        result[index++] = crc[0];
        result[index++] = etx;
 
        return (ArrayUtils.toByteArray(addStuffing(result)));
    }
 
 
    public byte obtenerByte(int valor)
    {
      try
      {
        switch (valor)
        {
          case 0  :  return((byte)'0');
          case 1  :  return((byte)'1');
          case 2  :  return((byte)'2');
          case 3  :  return((byte)'3');
          case 4  :  return((byte)'4');
          case 5  :  return((byte)'5');
          case 6  :  return((byte)'6');
          case 7  :  return((byte)'7');
          case 8  :  return((byte)'8');
          case 9  :  return((byte)'9');
          case 10 :  return((byte)'A');
          case 11 :  return((byte)'B');
          case 12 :  return((byte)'C');
          case 13 :  return((byte)'D');
          case 14 :  return((byte)'E');
          case 15 :  return((byte)'F');
        }
      }
      catch (Exception e)
      {
      }
 
      return((byte)'0');
    }
 
 
    public byte obtenerByteAscii(byte b)
    {
      try
      {
        switch (b)
        {
          case 0  :  return((byte)'0');
          case 1  :  return((byte)'1');
          case 2  :  return((byte)'2');
          case 3  :  return((byte)'3');
          case 4  :  return((byte)'4');
          case 5  :  return((byte)'5');
          case 6  :  return((byte)'6');
          case 7  :  return((byte)'7');
          case 8  :  return((byte)'8');
          case 9  :  return((byte)'9');
          case 10 :  return((byte)'A');
          case 11 :  return((byte)'B');
          case 12 :  return((byte)'C');
          case 13 :  return((byte)'D');
          case 14 :  return((byte)'E');
          case 15 :  return((byte)'F');
        }
 
      }
      catch (Exception e)
      {
      }
 
      return((byte)'0');
    }
 
 
    public byte[] checksum(int[] trama, int inicio, int fin) throws Exception
    {
        try
        {
          short acum,dato;
          long crc;
          int i,j;
 
          crc=0;
          acum=(short)crc;
          for(i=inicio;i<=fin;i++)
          {
            int valor = trama[i];
            
            dato = (short)valor;
            acum=(short)crc;
 
            dato<<=8;
            for(j=8;j>0;j--)
            {
              if(((dato^acum) & 0x8000) != 0)
                acum =(short)((acum<<1)^0x1021);
              else
                acum<<=1;
 
              dato<<=1;
            }
 
            crc=acum;
          }
 
          long aux = acum;
 
          if (aux < 0)
            aux += (Short.MAX_VALUE*2 + 2);
          crc=aux;
 
          byte[] res = new byte[2];
 
          res[0]=(byte) crc;
          crc=(long)(crc/256);
          res[1]=(byte) crc;
 
          return(res);
        }
        catch (Exception e)
        {
          throw e;
        }
    }
 
 
    public int[] addStuffing(int[] trama) throws Exception
    {
        try
        {
              int[] aux = new int[100096];
              int apuntador = 0;
 
              for (int i=0; i<trama.length; i++)
              {
                if ((i>=1) && (i<trama.length-1))
                {
                  if ((trama[i] == 2) || (trama[i] == 3) ||
                      (trama[i] == 5) || (trama[i] == 6) ||
                      (trama[i] == 16))
                  {
                    aux[apuntador++] = 0x10;
                    aux[apuntador++] = (trama[i]+128);
                  }
                  else
                    aux[apuntador++] = trama[i];
                }
                else
                  aux[apuntador++] = trama[i];
              }
 
              int[] resultado = new int[apuntador];
              System.arraycopy(aux, 0, resultado, 0, apuntador);
 
              return(resultado);
            }
            catch (Exception e)
            {
              throw e;
            }
    }
 
 
    public int[] removeStuffing(int[] trama) throws Exception
    {
        try
        {
          int[] aux = new int[100096];
          int apuntador = 0;
 
          for (int i=0; i<trama.length; i++)
          {
            if (trama[i] == 16)
            {
              aux[apuntador++] = (trama[i+1]-128);
              i++;
            }
            else
              aux[apuntador++] = trama[i];
          }
 
          int[] resultado = new int[apuntador];
          System.arraycopy(aux, 0, resultado, 0, apuntador);
 
          return(resultado);
        }
        catch (Exception e)
        {
          throw e;
        }
    }
 
}