184ffd7f767a4a0b25d56811f42000a57d3d3fa2
[owSlave2.git] / tools_cmd / rwOW / main.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1
2 extern "C" {
3 #include "ownet.h"
4 }
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 FILE *fio;
10 char s[100], fname[254];
11
12
13 SMALLINT ReadDS2430(int portnum, uchar SerialNum[8]) {
14         uchar rt = FALSE;
15         uchar send_block[145];
16         uchar send_cnt = 0, i;
17         int address;
18         char s[100];
19         FILE *f;
20         sprintf(s, "%02X%02X%02X%02X%02X%02X%02X%02X.bin", SerialNum[7], SerialNum[6], SerialNum[5], SerialNum[4], SerialNum[3], SerialNum[2], SerialNum[1], SerialNum[0]);
21         fprintf(fio, ">14\n");
22         send_cnt = 0;
23         printf("Read Data MEM  \n");
24         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
25         send_block[send_cnt++] = 0xF0;
26         send_block[send_cnt++] = 0x00;
27         for (i = 0; i<32; i++) send_block[send_cnt++] = 0xFF;
28         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
29         for (i = 2; i<send_cnt; i++) { printf("%02X ", send_block[i]); fprintf(fio,"%02X ", send_block[i]); }
30         printf("\n");
31         fprintf(fio, "\n");
32         send_cnt = 0;
33         printf("Read App MEM  \n");
34         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
35         send_block[send_cnt++] = 0xC3;
36         send_block[send_cnt++] = 0x00;
37         for (i = 0; i<8; i++) send_block[send_cnt++] = 0xFF;
38         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
39         for (i = 2; i<send_cnt; i++) { printf("%02X ", send_block[i]); fprintf(fio, "%02X ", send_block[i]); }
40         printf("\n");
41         fprintf(fio, "\n");
42
43         send_cnt = 0;
44         printf("Read App State  \n");
45         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
46         send_block[send_cnt++] = 0x66;
47         for (i = 0; i<1; i++) send_block[send_cnt++] = 0xFF;
48         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
49         for (i = 1; i<send_cnt; i++) { printf("%02X ", send_block[i]);  fprintf(fio, "%02X ", send_block[i]); }
50         printf("\n");
51         fprintf(fio, "\n");
52 }
53
54
55 SMALLINT ReadDS2433(int portnum, uchar SerialNum[8]) {
56         uchar rt = FALSE;
57         uchar send_block[145];
58         uchar send_cnt = 0, i, j;
59         int address;
60         send_cnt = 0;
61         printf("-----> TEST DS2433 \n");
62         fprintf(fio, ">23\n");
63
64         send_cnt = 0;
65         printf("\nRead MEM ALL \n");
66         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
67         send_block[send_cnt++] = 0xF0;
68         send_block[send_cnt++] = 0x00;
69         send_block[send_cnt++] = 0x00;
70         for (j = 0; j<16; j++) {
71                 for (i = 0; i<32; i++) send_block[send_cnt++] = 0xFF;
72                 if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
73                 for (i = j?0:3; i < send_cnt; i++) { printf("%02X ", send_block[i]); fprintf(fio, "%02X ", send_block[i]); }
74                 send_cnt = 0;
75                 printf("\n");
76                 fprintf(fio, "\n");
77         }
78
79 }
80
81
82 int findDev(uchar code) {
83         int find = 0;
84         fio = fopen(fname, "r");
85         while (!feof(fio)) {
86                 s[0] = fgetc(fio);
87                 if (s[0] == '>') {
88                         s[0] = fgetc(fio);
89                         s[1] = fgetc(fio);
90                         s[2] = 0;
91                         uchar w = strtol(s, NULL, 16);
92                         if (code ==w) return 1;
93                 }
94         }
95         fclose(fio);
96         return 0;
97 }
98
99
100
101 SMALLINT WriteDS2433(int portnum, uchar SerialNum[8]) {
102         uchar rt = FALSE;
103         uchar send_block[145];
104         uchar send_cnt = 0, i, j;
105         int address;
106         send_cnt = 0;
107         printf("-----> TEST DS2433 \n");
108         if (findDev(0x23)) {
109                 for (j = 0; j < 16; j++) {
110                         for (i = 0; i < 32; i++) {
111                                 fscanf(fio, "%02X", &send_block[i + 3]);
112                         }
113                         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
114                         send_block[0] = 0x0F;
115                         send_block[1] = j << 5;
116                         send_block[2] = j >> 3;
117                         if (!owBlock(portnum, FALSE, send_block, 32 + 3))  { printf("ERROR RUN COMMAND \n"); return 1; }
118                         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
119                         send_cnt = 0;
120                         send_block[send_cnt++] = 0xAA;
121                         for (i = 0; i < 32 + 3; i++) send_block[send_cnt++] = 0xFF;
122                         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
123                         for (i = 0; i < send_cnt; i++) { printf("%02X ", send_block[i]); }
124                         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
125                         send_cnt = 0;
126                         send_block[send_cnt++] = 0x55;
127                         send_cnt += 3;
128                         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
129                         msDelay(100);
130                         printf("\n");
131                 }
132         
133         }
134         fclose(fio);
135         return 0;
136
137
138 }
139
140
141 SMALLINT WriteDS2430(int portnum, uchar SerialNum[8]) {
142         uchar rt = FALSE;
143         uchar send_block[145];
144         uchar send_cnt = 0, i, j;
145         int address;
146         send_cnt = 0;
147         printf("-----> TEST DS2430 \n");
148         if (findDev(0x14)) {
149                 for (i = 0; i < 32; i++) {
150                         fscanf(fio, "%02X", &send_block[i + 2]);
151                 }
152                 if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
153                 send_block[0] = 0x0F;
154                 send_block[1] = 0;
155                 if (!owBlock(portnum, FALSE, send_block, 32 + 2))  { printf("ERROR RUN COMMAND \n"); return 1; }
156                 if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
157                 send_cnt = 0;
158                 send_block[send_cnt++] = 0xAA;
159                 send_block[send_cnt++] = 0x00;
160                 for (i = 0; i < 32 + 2; i++) send_block[send_cnt++] = 0xFF;
161                 if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
162                 for (i = 0; i < send_cnt; i++) { printf("%02X ", send_block[i]); }
163                 if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
164                 send_cnt = 0;
165                 send_block[send_cnt++] = 0x55;
166                 send_block[send_cnt++] = 0xA5;
167                 if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
168                 msDelay(100);
169                 printf("\n");
170         }
171         fclose(fio);
172
173 }
174 /*
175 SMALLINT WriteDS2430(int portnum, uchar SerialNum[8], const char *fname) {
176         uchar rt = FALSE;
177         uchar send_block[145];
178         uchar send_cnt = 0, i;
179         int address;
180         uchar readbl[64];
181         FILE *f;
182         f = fopen(fname, "rb");
183         fread(readbl, 1, 32 + 16, f);
184         fclose(f);
185         ResetDS2430(portnum, SerialNum);
186         msDelay(1000);
187         send_cnt = 0;
188         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
189         send_block[send_cnt++] = 0x0F;
190         send_block[send_cnt++] = 0x00;
191         for (i = 0; i<32; i++) send_block[send_cnt++] = readbl[i + 8];
192         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
193         send_cnt = 0;
194         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
195         send_block[send_cnt++] = 0xAA;
196         send_block[send_cnt++] = 0x00;
197         for (i = 0; i<32; i++) send_block[send_cnt++] = 0xFF;
198         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
199         for (i = 0; i<send_cnt; i++) { printf("%02X ", send_block[i]); if ((i - 1) % 16 == 0) printf("\n"); }
200
201         send_cnt = 0;
202         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
203         send_block[send_cnt++] = 0x55;
204         send_block[send_cnt++] = 0xA5;
205         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
206         msDelay(10);
207
208         send_cnt = 0;
209         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
210         send_block[send_cnt++] = 0xF0;
211         send_block[send_cnt++] = 0x00;
212         for (i = 0; i<32; i++) send_block[send_cnt++] = 0xFF;
213         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
214         for (i = 0; i<send_cnt; i++) { printf("%02X ", send_block[i]); if ((i - 1) % 16 == 0) printf("\n"); }
215
216         send_cnt = 0;
217         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
218         send_block[send_cnt++] = 0x99;
219         send_block[send_cnt++] = 0x00;
220         for (i = 0; i<8; i++) send_block[send_cnt++] = readbl[i + 8 + 32];
221         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
222
223         send_cnt = 0;
224         if (!owAccess(portnum)) { printf("ERROR MATCH ROM \n"); return 1; }
225         send_block[send_cnt++] = 0x5A;
226         send_block[send_cnt++] = 0xA5;
227         if (!owBlock(portnum, FALSE, send_block, send_cnt))  { printf("ERROR RUN COMMAND \n"); return 1; }
228         msDelay(10);
229         SetIDDS2430(portnum, SerialNum, readbl);
230
231
232 }
233 */
234
235
236
237 unsigned char conf2(const char *s) {
238         unsigned char b = 0;
239         for (int i = 0; i < 2; i++) {
240                 b = b * 16;
241                 if ((s[i] >= '0') && (s[i] <= '9')) {
242                         b += s[i] - '0';
243                 } else
244                 if ((s[i] >= 'A') && (s[i] <= 'F')) {
245                         b += s[i] - 'A'+10;
246                 } else
247                 if ((s[i] >= 'a') && (s[i] <= 'f')) {
248                         b += s[i] - 'a'+10;
249                 }
250                 
251         }
252         return b;
253 }
254
255 void strcpys(char *s, const char* d,bool up) {
256         if (strlen(d) > 7990) {
257                 strncpy(s, d, 7990);
258                 s[200] = 0;
259         }
260         else strcpy(s, d);
261         if (up) {
262                 for (int i = 0; i<strlen(s); i++)
263                         if ((s[i] >= 'a') && (s[i] <= 'z'))
264                                 s[i] += 'A' - 'a';
265         }
266 }
267
268 int main(int argc, char *argv[]) {
269         uchar SNum[8];
270         int rslt, cnt, i;
271         uchar send_block[256];
272         uchar send_cnt = 0;
273         int portnum;
274         char adapter[20];
275         int found = 0;
276         int rw = 0;
277         int err = 0;
278         char s[8000];
279         int adt = 0;
280         int mod = 0;
281         if (argc >= 2) {
282                 strcpys(s, argv[1],true);
283                 
284                 
285                 if (strncmp(s,"USB",3)==0) {adt = 6;}
286                 if (strncmp(s, "COM", 3) == 0) { adt = 5; }
287                 if (adt != 0) {
288                         strcpy(s, argv[1] + 3);
289                         sprintf(adapter, "{%s,%i}\0",s,adt);
290                         printf("Open Port %s\n", argv[1]);
291                         if ((portnum = owAcquireEx(adapter)) < 0) {
292                                 printf("ERROR \n", adt);
293                         }
294                         else mod = 1;
295
296                 } 
297                 if ((argc == 2) && (mod == 1)) mod = 2;
298                 if (argc > 2) {
299                         strcpys(s, argv[2],true);
300                         if (strncmp(s, "DC", 2) == 0) {
301                                 if (argc > 3) {
302                                         if (strlen(argv[3])>=16)
303                                         for (int i = 0; i < 16; i += 2) {
304                                                 SNum[7 - (i / 2)] = conf2(argv[3] + i);
305
306                                         }
307                                         printf("Select: %02X%02X%02X%02X%02X%02X%02X%02X\n", SNum[7], SNum[6], SNum[5], SNum[4], SNum[3], SNum[2], SNum[1], SNum[0]);
308                                         mod = 3;
309                                 }
310
311                         }
312                 }
313                 if ((mod == 3) && (argc>4)) {
314                         strcpys(s, argv[4], true);
315                         mod = 4;
316                 }
317         
318         }
319
320         if (mod == 2) {
321                 rslt = owFirst(portnum, TRUE, FALSE);
322                 cnt = 0;
323                 while (rslt) {
324                         cnt++;
325                         owSerialNum(portnum, &SNum[0], TRUE);
326                         printf("%02X%02X%02X%02X%02X%02X%02X%02X\n", SNum[7], SNum[6], SNum[5], SNum[4], SNum[3], SNum[2], SNum[1], SNum[0]);
327                         //                      fprintf(ft,"%02X%02X%02X%02X%02X%02X%02X%02X\n",SNum[7],SNum[6],SNum[5],SNum[4],SNum[3],SNum[2],SNum[1],SNum[0]);
328 /*                      switch (SNum[0]) {
329
330
331                                 case 0x14: {
332                                         //TestDS2430(portnum,&SNum[0]);
333                                         //scanf("%i",&i);
334                                         //ResetDS2430(portnum,&SNum[0]);
335                                         //uchar id[]={0x14,0xF5 ,0xEA ,0x24 ,0x06 ,0x00 ,0x00 ,0x5E};
336                                         //SetIDDS2430(portnum,&SNum[0],id);
337                                         if (rw) WriteDS2430(portnum, &SNum[0]); else ReadDS2430(portnum, &SNum[0]);
338                                         //WriteDS2430(portnum,&SNum[0],"5E00000624EAF514.bin");
339                                         break;
340                                 }
341                                 case 0x23: {
342                                         //TestDS2433(portnum,&SNum[0]);
343                                         //TestDS2433_reset(portnum,&SNum[0]);
344                                         //scanf("%i",&i);
345                                         if (rw) WriteDS2433(portnum, &SNum[0]); else ReadDS2433(portnum, &SNum[0]);
346                                         break;
347                                 }
348                                 }*/
349                         rslt = owNext(portnum, TRUE, FALSE);
350                 }
351         }
352         if (mod == 4) {
353                 owSerialNum(portnum, SNum, FALSE);
354                 strcat(s, "_R");
355                 int len = strlen(s);
356                 int i = 0;
357                 while (i < len) {
358                         if (s[i] == '_') {
359                                 if (s[i + 1] == 'R') {
360                                         if (owAccess(portnum)) {
361                                                 if (owBlock(portnum, FALSE, send_block, send_cnt)) {
362                                                         for (int j = 0; j < send_cnt; j++) {
363                                                                 printf("%02X", send_block[j]);
364                                                         }
365                                                         printf("\n");
366                                                 }
367                                                 else printf("ERROR RESET\n");
368                                         }
369                                         else printf("ERROR SEND\n");
370                                         send_cnt = 0;
371                                 }
372                                 if (s[i + 1] == 'P') {
373                                         if (i + 2 < len) {
374                                                 i += 2;
375                                                 msDelay(conf2(s + i));
376                                                 
377                                         }
378                                 }
379                         }
380                         else {
381                                 send_block[send_cnt++] = conf2(s + i);
382                         }
383                         i += 2;
384                 
385                         
386                 }
387         }
388         if (mod>0) owRelease(portnum);
389         
390         return 0;
391 }