-
Notifications
You must be signed in to change notification settings - Fork 3
/
CodeplugComms.cs
392 lines (351 loc) · 12.1 KB
/
CodeplugComms.cs
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using GD77_FlashManager;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using UsbLibrary;
internal class CodeplugComms
{
public delegate void Delegate1(object sender, FirmwareUpdateProgressEventArgs e);
private const int HEAD_LEN = 4;
private const int MAX_COMM_LEN = 32;
private const byte CMD_WRITE = 87;
private const byte CMD_READ = 82;
private const byte CMD_CMD = 67;
private const byte CMD_BASE = 66;
private const int MaxReadTimeout = 5000;
private const int MaxWriteTimeout = 1000;
private const int MaxBuf = 160;
private const float IndexListPercent = 5f;
private const int HID_VID = 0x15A2;
private const int HID_PID = 0x0073;
private static readonly byte[] CMD_ENDR = Encoding.ASCII.GetBytes("ENDR");
private static readonly byte[] CMD_ENDW = Encoding.ASCII.GetBytes("ENDW");
private static readonly byte[] CMD_ACK= new byte[1] {65};
private static readonly byte[] CMD_PRG = new byte[7] { 2, (byte)'P', (byte)'R', (byte)'O', (byte)'G', (byte)'R', (byte)'A' };// 80,82,79,71,82,65}
private static readonly byte[] CMD_PRG2 = new byte[2] {77,2};
public int[] START_ADDR;
public int[] END_ADDR;
private Thread thread;
private Delegate1 OnFirmwareUpdateProgress;
bool _CancelComm;
[CompilerGenerated]
public bool getCancelComm()
{
return this._CancelComm;
}
[CompilerGenerated]
public void setCancelComm(bool bool_0)
{
this._CancelComm = bool_0;
}
bool _IsRead;
[CompilerGenerated]
public bool setIsRead()
{
return this._IsRead;
}
[CompilerGenerated]
public void setIsRead(bool bool_0)
{
this._IsRead = bool_0;
}
public bool isThreadAlive()
{
if (this.thread != null)
{
return this.thread.IsAlive;
}
return false;
}
public void joinThreadIfAlive()
{
if (this.isThreadAlive())
{
this.thread.Join();
}
}
public void startCodeplugReadOrWriteInNewThread()
{
if (this.setIsRead())
{
this.thread = new Thread(this.readData);
}
else
{
this.thread = new Thread(this.writeData);
}
this.thread.Start();
}
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
// Test function added by Roger Clark to read all 1Mb from the external Flash chip on the GD-77.
// It outputs the data dummp to the file c:\\gd-77_datadump.bin
// However depending on the mode in which the GD-77 is booted, addresses 0x000000 - 0x01FFFF (for codeplug mode)
// or addresses 0x030000 - 0x03FFFF for DMR-ID mode are returned with anything other than 0x00 in them
// Its strange that address 0x020000 - 0x02FFFF does not seem to be accessible
// Note. Valid transfer lengths seem to be 8,16 or 32 bytes. 64 bytes does not work, as it just returns 0x00 in addresses after 32 bytes
public void readData()
{
byte[] usbBuf = new byte[160];// buffer for individual transfers
int blockLength = 0;
int addr32 = 0;
int addr16 = 0;
int pageAddr = 0;
SpecifiedDevice specifiedDevice = null;
try
{
specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);//0x152A HID_PID
if (specifiedDevice == null)
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Device not found", true, true));
}
}
else
{
while (true)
{
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.SendData(CodeplugComms.CMD_PRG);// Send PROGRA command to initiate comms
specifiedDevice.ReceiveData(usbBuf);// Wait for response
if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
{
break;// Exit if not ack
}
specifiedDevice.SendData(CodeplugComms.CMD_PRG2);// Send second half of comms init sequence
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.ReceiveData(usbBuf);// GD77 send back device information
byte[] array3 = new byte[8];
Buffer.BlockCopy(usbBuf, 0, array3, 0, 8);// Extract the first 8 bytes from the response
// REMOVED CURRENT MODEL CHECK !!! if (array3.smethod_4(Settings.CUR_MODEL))
{
// its the correct model number
specifiedDevice.SendData(CodeplugComms.CMD_ACK);// send ACK
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.ReceiveData(usbBuf);// Wait for response (of ACK)
if (usbBuf[0] == CodeplugComms.CMD_ACK[0])
{
// --------------- removed the password checking
blockLength = 32;// Max transfer length is 32 bytes
int currentPage = 0;
int bankSize = 65536;
int numBlocks = MainForm.transferLength / blockLength;
int startBlock = MainForm.startAddress / blockLength;
for (int block = startBlock; block < (startBlock + numBlocks); block++)
{
if (currentPage != (block * blockLength) / bankSize)
{
currentPage = (block * blockLength) / bankSize;
addr32 = blockLength * block;
byte[] array4 = new byte[8] { (byte)'C', (byte)'W', (byte)'B', 4, 0, 0, 0, 0 };
pageAddr = addr32 >> 16 << 16;
array4[4] = (byte)(pageAddr >> 24);
array4[5] = (byte)(pageAddr >> 16);
array4[6] = (byte)(pageAddr >> 8);
array4[7] = (byte)pageAddr;
Console.WriteLine(SpecifiedDevice.ByteArrayToString(array4));
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.SendData(array4, 0, array4.Length);
specifiedDevice.ReceiveData(usbBuf);
if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
{
goto end_IL_02a2;
}
}
addr16 = (block * blockLength) & 0xffff;
// Send request for dcata
byte[] data2 = new byte[4] { 82, (byte)(addr16 >> 8), (byte)addr16, (byte)blockLength };
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.SendData(data2, 0, 4);
if (!specifiedDevice.ReceiveData(usbBuf))
{
goto end_IL_02a2;
}
Buffer.BlockCopy(usbBuf, 4, MainForm.eeprom, (block * blockLength), blockLength);// Extract the first 8 bytes from the response
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(block+1-startBlock) *100 / (float)numBlocks, "", false, false));
}
}
// SEND END OF READ
specifiedDevice.SendData(CodeplugComms.CMD_ENDR);
specifiedDevice.ReceiveData(usbBuf);
}
break;
}
return;
end_IL_02a2:
break;
}
}
}
catch (TimeoutException ex)
{
Console.WriteLine(ex.Message);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Comms error", false, false));
}
}
finally
{
if (specifiedDevice != null)
{
specifiedDevice.Dispose();
}
}
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
}
}
public void writeData()
{
//byte[] MainForm.eeprom;// no need to allocate this as its read in from file. = new byte[128 * 1024];// whole of the codeplug
byte[] usbBuf = new byte[160];// buffer for individual usb transfers
int blockLength = 0;
int addr32 = 0;
int addr16 = 0;
int pageAddr = 0;
SpecifiedDevice specifiedDevice = null;
try
{
specifiedDevice = SpecifiedDevice.FindSpecifiedDevice(HID_VID, HID_PID);//0x152A HID_PID
if (specifiedDevice == null)
{
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Device not found", true, true));
}
}
else
{
while (true)
{
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.SendData(CodeplugComms.CMD_PRG);// Send PROGRA command to initiate comms
//Console.WriteLine("Send PRG1");
specifiedDevice.ReceiveData(usbBuf);// Wait for response
if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
{
break;// Exit if not ack
}
specifiedDevice.SendData(CodeplugComms.CMD_PRG2);// Send second half of comms init sequence
//Console.WriteLine("Send PRG2");
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.ReceiveData(usbBuf);// GD77 send back device information
byte[] array3 = new byte[8];
Buffer.BlockCopy(usbBuf, 0, array3, 0, 8);// Extract the first 8 bytes from the response
{
// its the correct model number
specifiedDevice.SendData(CodeplugComms.CMD_ACK);// send ACK
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.ReceiveData(usbBuf);// Wait for response (of ACK)
if (usbBuf[0] == CodeplugComms.CMD_ACK[0])
{
//Console.WriteLine("Got ACK");
// --------------- removed the password checking
blockLength = 32;// Max transfer length is 32 bytes
int currentPage = 0;
int bankSize = 65536;
int numBlocks = MainForm.transferLength / blockLength;
int startBlock = MainForm.startAddress / blockLength;
for (int block = startBlock; block < (startBlock + numBlocks); block++)
{
//Console.Write("Processing block " + block + " end block " + (startBlock + numBlocks));
if (currentPage != (block * blockLength) / bankSize)
{
currentPage = (block * blockLength) / bankSize;
addr32 = blockLength * block;
byte[] array4 = new byte[8] { (byte)'C', (byte)'W', (byte)'B', 4, 0, 0, 0, 0 };
pageAddr = addr32 >> 16 << 16;
array4[4] = (byte)(pageAddr >> 24);
array4[5] = (byte)(pageAddr >> 16);
array4[6] = (byte)(pageAddr >> 8);
array4[7] = (byte)pageAddr;
//Console.WriteLine("Send address changed to 0x" + pageAddr.ToString("X"));
Array.Clear(usbBuf, 0, usbBuf.Length);
specifiedDevice.SendData(array4, 0, array4.Length);
specifiedDevice.ReceiveData(usbBuf);
if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
{
goto end_IL_02a2;
}
}
addr16 = (block * blockLength) & 0xffff;
// Send request for dcata
byte[] data2 = new byte[4+32] { 87, (byte)(addr16 >> 8), (byte)addr16, (byte)blockLength,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Array.Clear(usbBuf, 0, usbBuf.Length);
Buffer.BlockCopy(MainForm.eeprom, (block * blockLength), data2, 4, blockLength);
specifiedDevice.SendData(data2, 0, 4+32);
//Console.WriteLine("Send Data to address 0x"+addr16.ToString("X"));
specifiedDevice.ReceiveData(usbBuf);
if (usbBuf[0] != CodeplugComms.CMD_ACK[0])
{
goto end_IL_02a2;
}
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs((float)(block + 1 - startBlock) * 100 / (float)numBlocks, "", false, false));
}
}
// SEND END OF WRITE
specifiedDevice.SendData(CodeplugComms.CMD_ENDW);
specifiedDevice.ReceiveData(usbBuf);
}
break;
}
return;
end_IL_02a2:
// SEND END OF WRITE
specifiedDevice.SendData(CodeplugComms.CMD_ENDW);
specifiedDevice.ReceiveData(usbBuf);
break;
}
}
}
catch (TimeoutException ex)
{
Console.WriteLine(ex.Message);
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(0f, "Error", false, false));
}
}
finally
{
if (specifiedDevice != null)
{
specifiedDevice.Dispose();
}
}
if (this.OnFirmwareUpdateProgress != null)
{
this.OnFirmwareUpdateProgress(this, new FirmwareUpdateProgressEventArgs(100f, "", false, true));
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void method_9(Delegate1 delegate1_0)
{
this.OnFirmwareUpdateProgress = (Delegate1)Delegate.Combine(this.OnFirmwareUpdateProgress, delegate1_0);
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void method_10(Delegate1 delegate1_0)
{
this.OnFirmwareUpdateProgress = (Delegate1)Delegate.Remove(this.OnFirmwareUpdateProgress, delegate1_0);
}
public CodeplugComms()
{
this.START_ADDR = new int[0];
this.END_ADDR = new int[0];
}
}