Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / flashrom.c
blobfcc1a4e424ea6e4759637876bbe04fb37fa745f3
1 /*
2 * Copyright (c) 2007, IRTrans GmbH
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of IRTrans GmbH nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifdef WIN32
31 #include "winsock2.h"
32 #include <windows.h>
33 #endif
35 #ifdef WINCE
36 #include "winsock2.h"
37 #include <windows.h>
38 #endif
41 #ifdef LINUX
42 typedef int SOCKET;
43 typedef int DWORD;
44 typedef int WSAEVENT;
45 #define closesocket close
46 extern int hCom;
47 #include <stdint.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <sys/un.h>
51 #include <arpa/inet.h>
52 #endif
54 #include <stdio.h>
56 #include "remote.h"
57 #include "errcode.h"
58 #include "dbstruct.h"
59 #include "network.h"
60 #include "lowlevel.h"
61 #include "global.h"
62 #include "fileio.h"
64 #ifdef WIN32
65 #include "winio.h"
66 #include "winusbio.h"
67 #endif
70 extern IRREMOTE *rem_pnt;
71 extern int rem_cnt;
72 extern IRCOMMAND *cmd_pnt;
73 extern int cmd_cnt;
74 extern IRTIMING *tim_pnt;
75 extern int tim_cnt;
78 #include "flash.h"
80 #define INITIAL_VALUE 0x92f3
81 #define INITIAL_VALUE_V2 0x143a
84 void set_entry (char entry[],int pos,FLASH_CONTENT *content,byte type,byte remote,byte group,byte shift,byte setup,word sourcemask,word acc_timeout,word acc_repeat,char *version,byte nmflag,char *remname,char *comname,byte rcv_len);
85 word FindIRDBCommand (byte remote[],byte command[],IRDB_HEADER *pnt,byte vers);
86 word FindIRDBRemote (byte remote[],IRDB_HEADER *pnt,byte vers);
87 void ReadTraReceive (char tp[]);
88 void ReadTraConfig ();
89 word CRC (byte *Data, int Length,word init);
92 word flash_data[65536];
93 FILE *fptrans;
95 TRANSLATECOMMAND trans[10000];
96 IRDBCOMMAND irdb[10000];
97 IRDBHEADER irdbheader;
99 int trans_num;
100 extern STATUS_BUFFER remote_statusex[MAX_IR_DEVICES];
102 void StoreDbItem (IRDBCOMMAND *db)
104 char remcmp[100];
105 char cmdcmp[100];
107 trans_num = db->number;
109 if (db->type == IRDB_REM || db->type == IRDB_TYPEACTION) {
111 memset (remcmp,0,100);
112 memset (cmdcmp,0,100);
114 strcpy (remcmp,db->remote);
115 strcpy (cmdcmp,db->command);
116 ConvertLcase (remcmp,(int)strlen (remcmp));
117 ConvertLcase (cmdcmp,(int)strlen (cmdcmp));
120 db->remote_num = DBFindRemote (remcmp);
122 if (db->type == IRDB_TYPEACTION) db->command_num = DBFindCommand (cmdcmp,db->remote_num);
125 if (db->type == IRDB_TYPEHEADER) {
126 memcpy (&irdbheader,db,sizeof (IRDBHEADER));
128 memcpy (&irdb[trans_num],db,sizeof (IRDBCOMMAND));
133 void StoreTransItem (TRANSLATECOMMAND *tr)
135 trans_num = tr->number;
136 memcpy (&trans[trans_num],tr,sizeof (TRANSLATECOMMAND));
141 int SetIRDBEx (byte bus,int iradr,STATUSBUFFER *stat)
143 char st[255];
144 byte vers,calflag;
145 int i,j,flashwordsize,len,lenf,ntim,act_cnt;
146 word adr,cmd_cnt,rem_cnt,com_adr;
147 IRDB_HEADER *pnt;
148 IRDB_REMOTE *rem;
149 IRDB_REMOTE_COMPACT *remc;
150 IRDB_REMOTE_EX *remex;
151 HASH_ENTRY *hash;
152 IRDB_COMMAND *com;
153 IRDB_COMMAND_COMPACT *comc;
154 IRDB_IRCOMMAND *ircom;
155 IRDB_ACTION *act;
156 IRDB_ACTION_EX *actex;
157 word rem_adr_det[4000],cadr;
158 HASH_ENTRY *chash;
159 IRDB_FLASHINFO *finfo;
162 finfo = (IRDB_FLASHINFO *)stat;
163 memset (flash_data,0,sizeof (flash_data));
164 pnt = (IRDB_HEADER *)flash_data;
166 act_cnt = 0;
167 vers = 0;
168 if (strcmp (IRDevices[bus].version+1,"5.04.17") >= 0) vers = 1; // Erste IRDB Command Struktur
169 if (strcmp (IRDevices[bus].version+1,"5.04.28") >= 0) vers = 2; // GeƤnderte IRDB Command Struktur: Compact Structs
170 if (strcmp (IRDevices[bus].version+1,"5.05.04") >= 0) vers = 3; // EX IRDB Struktur (IPs, RS232 separat, Remote Action
171 calflag = (IRDevices[bus].fw_capabilities & FN_CALIBRATE) != 0;
173 pnt->type = FLASH_TYPE_IRDB;
175 adr = pnt->table.remote_adr = IRDB_HEADER_LEN;
177 if (irdb[0].type == IRDB_TYPEHEADER) {
178 memcpy (pnt->table.default_action,irdbheader.default_action,100);
179 memcpy (pnt->table.return_stat_ok,irdbheader.ok_status,20);
180 memcpy (pnt->table.return_stat_err,irdbheader.err_status,20);
182 for (i=0;i <= 3;i++) {
183 pnt->table.adc[i].mode = irdbheader.inputmode[i];
184 pnt->table.adc[i].low_value = irdbheader.min[i];
185 pnt->table.adc[i].high_value = irdbheader.max[i];
186 pnt->table.adc[i].hysteresis = irdbheader.hyst[i];
187 pnt->table.adc[i].low_ledsel = irdbheader.minledsel[i];
188 pnt->table.adc[i].high_ledsel = irdbheader.maxledsel[i];
189 pnt->table.adc[i].low_mask = irdbheader.minmask[i];
190 pnt->table.adc[i].high_mask = irdbheader.maxmask[i];
192 if (!(irdbheader.active & 1)) pnt->type = 0xffff; // Ausschalten der IRDB
193 else {
194 if ((irdbheader.active & 2) && vers >= 3) pnt->type = FLASH_TYPE_IRDBAUX;
195 if ((irdbheader.active & 4) && vers >= 3) pnt->type |= FLASH_MODE_LAN_DB;
196 if ((irdbheader.active & 8) && vers >= 3) pnt->type |= FLASH_MODE_LANRELAIS;
201 rem_cnt = 0;
202 rem = (IRDB_REMOTE *)(flash_data + adr);
203 remc = (IRDB_REMOTE_COMPACT *)(flash_data + adr);
204 remex = (IRDB_REMOTE_EX *)(flash_data + adr);
206 for (i=0;i <= trans_num;i++) {
207 if (irdb[i].type == IRDB_REM) {
208 rem_adr_det[rem_cnt] = adr;
210 if (vers >= 3) {
211 remex->ip = irdb[i].ip;
212 remex->port = irdb[i].port;
213 memcpy (remex->name,irdb[i].remote,80);
214 len = (int)strlen (irdb[i].remote);
215 if (len > 80) len = 80;
216 remex->namelen = len;
218 remex->modeflags = irdb[i].action_type;
220 memcpy (remex->name+len,irdb[i].action,50);
221 lenf = (int)strlen (irdb[i].action);
222 if (lenf > 50) lenf = 50;
223 remex->defactlen = lenf;
225 adr += (word)((IRDB_REMOTE_LEN_EX + len + lenf) / 2);
226 remex = (IRDB_REMOTE_EX *)(flash_data + adr);
228 else if (vers >= 2) {
229 memcpy (remc->name,irdb[i].remote,80);
230 len = (int)strlen (irdb[i].remote);
231 if (len > 80) len = 80;
232 remc->namelen = len;
234 adr += (word)((IRDB_REMOTE_LEN_COMPACT + len) / 2);
235 remc = (IRDB_REMOTE_COMPACT *)(flash_data + adr);
237 else {
238 memcpy (rem->name,irdb[i].remote,80);
239 adr += IRDB_REMOTE_LEN;
240 rem = (IRDB_REMOTE *)(flash_data + adr);
243 rem_cnt++;
247 pnt->table.remote_cnt = rem_cnt;
249 rem_cnt = 0;
250 pnt->table.remote_hash_adr = adr;
252 for (i=0;i <= trans_num;i++) {
253 if (irdb[i].type == IRDB_REM) {
254 hash = (HASH_ENTRY *)(flash_data + adr);
255 len = (int)strlen (irdb[i].remote);
256 if (len > 80) len = 80;
257 hash->hashcode = get_hashcode (irdb[i].remote,(byte)len);
258 hash->adr = rem_adr_det[rem_cnt];
260 adr += HASH_LEN;
261 rem_cnt++;
266 pnt->table.ircommand_hash_adr = adr;
267 hash = (HASH_ENTRY *)(flash_data + adr);
268 cmd_cnt = 0;
270 for (i=0;i <= trans_num;i++) {
271 if (irdb[i].type == IRDB_REM) {
272 j = rem_pnt[irdb[i].remote_num].command_start;
273 while (j < rem_pnt[irdb[i].remote_num].command_end) {
274 hash->hashcode = get_hashcode (cmd_pnt[j].data,(byte)cmd_pnt[j].ir_length);
275 adr += HASH_LEN;
276 hash = (HASH_ENTRY *)(flash_data + adr);
277 cmd_cnt++;
278 j++;
285 for (i=0;i <= trans_num;i++) {
286 if (irdb[i].type == IRDB_REM) {
287 j = rem_pnt[irdb[i].remote_num].timing_start;
289 while (j < rem_pnt[irdb[i].remote_num].timing_end) {
290 if (tim_pnt[j].link_count) {
291 tim_pnt[j].flash_adr = adr;
292 memcpy (flash_data + adr,&tim_pnt[j].ir_length,sizeof (IRDB_TIMING));
294 adr += IRDB_TIMING_LEN;
296 j++;
301 pnt->table.command_cnt = cmd_cnt;
302 com_adr = adr;
303 rem_cnt = 0;
305 for (i=0;i <= trans_num;i++) {
306 if (irdb[i].type == IRDB_REM) {
307 j = rem_pnt[irdb[i].remote_num].command_start;
308 cmd_cnt = 0;
309 while (j < rem_pnt[irdb[i].remote_num].command_end) {
310 com = (IRDB_COMMAND *)(flash_data + adr);
311 comc = (IRDB_COMMAND_COMPACT *)(flash_data + adr);
313 if (vers >= 2) {
314 memset (comc,0,sizeof (IRDB_COMMAND_COMPACT));
315 comc->remote_adr = rem_adr_det[rem_cnt];
316 len = (int)strlen (cmd_pnt[j].name);
317 if (len > 20) len = 20;
318 memcpy (comc->name,cmd_pnt[j].name,len);
320 ntim = rem_pnt[irdb[i].remote_num].timing_start + cmd_pnt[j].timing;
321 if (ntim < rem_pnt[irdb[i].remote_num].timing_end) comc->timing_adr = tim_pnt[ntim].flash_adr;
323 remc = (IRDB_REMOTE_COMPACT *)(flash_data + comc->remote_adr);
324 if (!cmd_cnt) remc->command_adr = adr;
326 comc->toggle_seq = cmd_pnt[j].toggle_seq;
327 comc->namelen = len;
329 adr += (word)((IRDB_COMMAND_LEN_COMPACT + len) / 2);
331 else {
332 com->remote_adr = rem_adr_det[rem_cnt];
333 len = (int)strlen (cmd_pnt[j].name) + 1;
334 if (len > 20) len = 20;
335 memset (com->name,' ',20);
336 memcpy (com->name,cmd_pnt[j].name,len);
338 ntim = rem_pnt[irdb[i].remote_num].timing_start + cmd_pnt[j].timing;
339 if (ntim < rem_pnt[irdb[i].remote_num].timing_end) com->timing_adr = tim_pnt[ntim].flash_adr;
341 rem = (IRDB_REMOTE *)(flash_data + com->remote_adr);
342 if (!cmd_cnt) rem->command_adr = adr;
344 if (vers >= 1) {
345 com->toggle_seq = cmd_pnt[j].toggle_seq;
346 adr += IRDB_COMMAND_LEN;
348 else adr += IRDB_COMMAND_LEN_1;
351 j++;
352 cmd_cnt++;
354 if (vers >= 2) remc->command_cnt = cmd_cnt;
355 else rem->command_cnt = cmd_cnt;
356 rem_cnt++;
361 rem_cnt = 0;
363 for (i=0;i <= trans_num;i++) {
364 if (irdb[i].type == IRDB_REM) {
365 j = rem_pnt[irdb[i].remote_num].command_start;
366 cmd_cnt = 0;
367 rem = (IRDB_REMOTE *)(flash_data + rem_adr_det[rem_cnt]);
368 remc = (IRDB_REMOTE_COMPACT *)(flash_data + rem_adr_det[rem_cnt]);
369 cadr = remc->command_adr;
371 while (j < rem_pnt[irdb[i].remote_num].command_end) {
372 hash = (HASH_ENTRY *)(flash_data + adr);
374 len = (int)strlen (cmd_pnt[j].name);
375 if (len > 20) len = 20;
377 hash->hashcode = get_hashcode (cmd_pnt[j].name,(byte)len);
379 if (vers >= 2) hash->adr = cadr;
380 else if (vers >= 1) hash->adr = rem->command_adr + cmd_cnt * IRDB_COMMAND_LEN;
381 else hash->adr = rem->command_adr + cmd_cnt * IRDB_COMMAND_LEN_1;
384 if (!cmd_cnt) {
385 if (vers >= 2) remc->command_hash = adr;
386 else rem->command_hash = adr;
389 adr += HASH_LEN;
390 j++;
391 cmd_cnt++;
392 cadr += (word)((IRDB_COMMAND_LEN_COMPACT + len) / 2);
394 rem_cnt++;
398 cmd_cnt = 0;
399 rem_cnt = 0;
400 hash = (HASH_ENTRY *)(flash_data + pnt->table.ircommand_hash_adr);
401 pnt->table.ircommand_adr = adr;
402 ircom = (IRDB_IRCOMMAND *)(flash_data + adr);
404 for (i=0;i <= trans_num;i++) {
405 if (irdb[i].type == IRDB_REM) {
406 j = rem_pnt[irdb[i].remote_num].command_start;
407 rem = (IRDB_REMOTE *)(flash_data + rem_adr_det[rem_cnt]);
408 remc = (IRDB_REMOTE_COMPACT *)(flash_data + rem_adr_det[rem_cnt]);
409 rem_cnt++;
410 if (vers >= 2) chash = (HASH_ENTRY *)(flash_data + remc->command_hash);
411 else chash = (HASH_ENTRY *)(flash_data + rem->command_hash);
413 while (j < rem_pnt[irdb[i].remote_num].command_end) {
414 com = (IRDB_COMMAND *)(flash_data + chash->adr);
415 comc = (IRDB_COMMAND_COMPACT *)(flash_data + chash->adr);
417 if (!(cmd_pnt[j].mode & RAW_DATA) && ((calflag && cmd_pnt[j].ir_length_cal > CODE_LEN) || cmd_pnt[j].ir_length > CODE_LEN)) { // Long Codes packen
418 int ii,pp;
419 ii = pp = 0;
421 if (calflag) {
422 while (pp < cmd_pnt[j].ir_length_cal) {
423 if (cmd_pnt[j].data_cal[pp] & 128) ircom->ir_data[ii++] = cmd_pnt[j].data_cal[pp++];
424 else {
425 ircom->ir_data[ii] = (cmd_pnt[j].data_cal[pp++] & 7);
426 if (pp < cmd_pnt[j].ir_length_cal) ircom->ir_data[ii] |= ((cmd_pnt[j].data_cal[pp] & 7) << 4);
427 pp++;
428 ii++;
432 ircom->ir_data[0] |= LONG_CODE_FLAG;
433 if (pp > cmd_pnt[j].ir_length_cal) ircom->ir_data[0] |= LONG_CODE_LEN;
434 ircom->ir_length = ii;
436 else {
437 while (pp < cmd_pnt[j].ir_length) {
438 if (cmd_pnt[j].data[pp] & 128) pp++;
439 else {
440 ircom->ir_data[ii] = (cmd_pnt[j].data[pp++] & 7);
441 if (pp < cmd_pnt[j].ir_length) ircom->ir_data[ii] |= ((cmd_pnt[j].data[pp] & 7) << 4);
442 pp++;
443 ii++;
447 ircom->ir_data[0] |= LONG_CODE_FLAG;
448 if (pp > cmd_pnt[j].ir_length) ircom->ir_data[0] |= LONG_CODE_LEN;
449 ircom->ir_length = ii;
452 else {
453 if (calflag && !(cmd_pnt[j].mode & RAW_DATA)) {
454 memcpy (ircom->ir_data,cmd_pnt[j].data_cal,(byte)cmd_pnt[j].ir_length_cal);
455 ircom->ir_length = (byte)cmd_pnt[j].ir_length_cal;
457 else {
458 memcpy (ircom->ir_data,cmd_pnt[j].data,(byte)cmd_pnt[j].ir_length);
459 ircom->ir_length = (byte)cmd_pnt[j].ir_length;
463 ircom->command_adr = chash->adr;
465 if (vers >= 2) comc->ird_adr = adr;
466 else com->ird_adr = adr;
468 if (cmd_pnt[j].mode & RAW_DATA) {
469 if (vers >= 2) comc->timing_adr = (word)cmd_pnt[j].timing;
470 else com->timing_adr = (word)cmd_pnt[j].timing;
471 ircom->command_adr = 0;
473 else {
474 hash[cmd_cnt].adr = adr;
477 adr += (sizeof (IRDB_IRCOMMAND) + ircom->ir_length) / 2; // +1 steckt bereits in der LƤnge 1 von data in IRDB_IRCOMMAND
478 ircom = (IRDB_IRCOMMAND *)(flash_data + adr);
480 cmd_cnt++;
481 chash++;
482 j++;
489 if (vers >= 3) {
490 actex = (IRDB_ACTION_EX *)(flash_data + adr);
491 for (i=0;i <= trans_num;i++) {
492 if (irdb[i].type == IRDB_TYPEACTION_2) {
493 int n,p;
494 actex->relais = irdb[i-2].relais;
495 i -= 2;
496 p = 0;
497 for (n=0;n <= 2;n++) {
498 actex->action_type[n] = irdb[i+n].action_type;
499 actex->action_len[n] = irdb[i+n].action_len;
501 if (irdb[i+n].ip) {
502 memcpy (actex->action + p,&irdb[i+n].ip,4);
503 p += 4;
504 actex->action_type[n] |= ACTION_IP;
506 if (irdb[i+n].port) {
507 memcpy (actex->action + p,&irdb[i+n].port,2);
508 p += 2;
509 actex->action_type[n] |= ACTION_PORT;
511 memcpy (actex->action + p,irdb[i+n].action,irdb[i+n].action_len);
512 p += irdb[i+n].action_len;
513 if (irdb[i+n].action_len & 1) p++;
516 j = FindIRDBCommand (irdb[i].remote,irdb[i].command,pnt,vers);
517 if (j) {
518 comc = (IRDB_COMMAND_COMPACT *)(flash_data + j);
519 comc->action_adr = adr;
522 adr += (word)((sizeof (IRDB_ACTION_EX) + p) / 2); // +1 steckt bereits in der LƤnge 1 von action in IRDB_ACTION
523 actex = (IRDB_ACTION_EX *)(flash_data + adr);
524 act_cnt++;
525 i += 2;
529 else {
530 act = (IRDB_ACTION *)(flash_data + adr);
531 for (i=0;i <= trans_num;i++) {
532 if (irdb[i].type == IRDB_TYPEACTION) {
533 act->action_len = irdb[i].action_len;
534 act->action_type = irdb[i].action_type;
535 act->relais = irdb[i].relais;
536 memcpy (act->action,irdb[i].action,irdb[i].action_len);
538 j = FindIRDBCommand (irdb[i].remote,irdb[i].command,pnt,vers);
539 if (j) {
540 com = (IRDB_COMMAND *)(flash_data + j);
541 comc = (IRDB_COMMAND_COMPACT *)(flash_data + j);
542 if (vers >= 2) comc->action_adr = adr;
543 else com->action_adr = adr;
546 adr += (sizeof (IRDB_ACTION) + act->action_len) / 2; // +1 steckt bereits in der LƤnge 1 von action in IRDB_ACTION
547 act = (IRDB_ACTION *)(flash_data + adr);
548 act_cnt++;
554 if (irdb[0].type == IRDB_TYPEHEADER) {
555 for (i=0;i <= 3;i++) {
556 if (irdbheader.minremote[i][0] && irdbheader.mincommand[i][0]) {
557 j = FindIRDBRemote (irdbheader.minremote[i],pnt,vers);
558 if (!j) {
559 pnt->table.adc[i].low_remote = 0;
560 sprintf (st,"Minremote %d [%s] not found in Flash IRDB\n",i,irdbheader.minremote[i]);
561 log_print (st,LOG_ERROR);
563 else {
564 pnt->table.adc[i].low_remote = j;
565 memcpy (pnt->table.adc[i].low_commands,irdbheader.mincommand[i],sizeof (irdbheader.mincommand[i]));
568 else pnt->table.adc[i].low_remote = 0;
570 if (irdbheader.maxremote[i][0] && irdbheader.maxcommand[i][0]) {
571 j = FindIRDBRemote (irdbheader.maxremote[i],pnt,vers);
572 if (!j) {
573 pnt->table.adc[i].high_remote = 0;
574 sprintf (st,"Maxremote %d [%s] not found in Flash IRDB\n",i,irdbheader.maxremote[i]);
575 log_print (st,LOG_ERROR);
577 else {
578 pnt->table.adc[i].high_remote = j;
579 memcpy (pnt->table.adc[i].high_commands,irdbheader.maxcommand[i],sizeof (irdbheader.maxcommand[i]));
582 else pnt->table.adc[i].high_remote = 0;
586 pnt->len = adr;
588 if (vers >= 2) pnt->crc = CRC ((byte *)(flash_data + 1),((adr - 1) * 2),INITIAL_VALUE_V2);
589 else pnt->crc = CRC ((byte *)(flash_data + 1),((adr - 1) * 2),INITIAL_VALUE);
592 // FlashgrĆ¶ĆŸe kontrollieren !
594 sprintf (st,"Flash IRDB Size: %d Words (%d KBytes).\n%-3d Remotes\n%-3d Commands\n%-3d Actions\n",adr,adr / 512,pnt->table.remote_cnt,pnt->table.command_cnt,act_cnt);
595 log_print (st,LOG_DEBUG);
597 finfo->statuslen = sizeof (IRDB_FLASHINFO);
598 finfo->statustype = STATUS_IRDBFLASH;
599 sprintf (finfo->memsize,"%d Words (%d KBytes)",adr,adr/512);
600 sprintf (finfo->remotes,"%d",pnt->table.remote_cnt);
601 sprintf (finfo->commands,"%d",pnt->table.command_cnt);
602 sprintf (finfo->actions,"%d",act_cnt);
603 if (IRDevices[bus].fw_capabilities & FN_FLASH128) sprintf (finfo->flashsize,"128K");
604 else sprintf (finfo->flashsize,"64K");
606 flashwordsize = 128;
608 adr = 0;
609 while (adr < pnt->len) {
610 if ((adr + flashwordsize /2) < pnt->len) TransferFlashdataEx (bus,flash_data + adr,adr,flashwordsize,0,iradr);
611 else TransferFlashdataEx (bus,flash_data + adr,adr,flashwordsize,1,iradr);
612 adr += flashwordsize / 2;
615 return (0);
618 word FindIRDBCommand (byte remote[],byte command[],IRDB_HEADER *pnt,byte vers)
620 word i,cnt,adr,len;
621 IRDB_REMOTE *rem;
622 IRDB_COMMAND *com;
623 IRDB_COMMAND_COMPACT *comc;
624 IRDB_REMOTE_COMPACT *remc;
625 IRDB_REMOTE_EX *remex;
627 if (vers >= 3) {
628 adr = pnt->table.remote_adr;
630 len = (word)strlen (remote);
632 for (i=0;i < pnt->table.remote_cnt;i++) {
633 remex = (IRDB_REMOTE_EX *)(flash_data + adr);
634 if (len == remex->namelen && !memcmp (remex->name,remote,len)) break;
635 adr += (IRDB_REMOTE_LEN_EX + remex->namelen) / 2;
638 if (i == pnt->table.remote_cnt) return (0);
640 cnt = remex->command_cnt;
641 adr = remex->command_adr;
643 len = (word)strlen (command);
645 for (i=0;i < cnt;i++) {
646 comc = (IRDB_COMMAND_COMPACT *)(flash_data + adr);
647 if (len == comc->namelen && !memcmp (comc->name,command,len)) return (adr);
649 adr += (IRDB_COMMAND_LEN_COMPACT + comc->namelen) / 2;
653 else if (vers >= 2) {
654 adr = pnt->table.remote_adr;
656 len = (word)strlen (remote);
658 for (i=0;i < pnt->table.remote_cnt;i++) {
659 remc = (IRDB_REMOTE_COMPACT *)(flash_data + adr);
660 if (len == remc->namelen && !memcmp (remc->name,remote,len)) break;
661 adr += (IRDB_REMOTE_LEN_COMPACT + remc->namelen) / 2;
665 if (i == pnt->table.remote_cnt) return (0);
667 cnt = remc->command_cnt;
668 adr = remc->command_adr;
670 len = (word)strlen (command);
672 for (i=0;i < cnt;i++) {
673 comc = (IRDB_COMMAND_COMPACT *)(flash_data + adr);
674 if (len == comc->namelen && !memcmp (comc->name,command,len)) return (adr);
676 adr += (IRDB_COMMAND_LEN_COMPACT + comc->namelen) / 2;
680 else {
681 rem = (IRDB_REMOTE *)(flash_data + pnt->table.remote_adr);
683 for (i=0;i < pnt->table.remote_cnt;i++) if (!memcmp (rem[i].name,remote,80)) break;
685 if (i == pnt->table.remote_cnt) return (0);
687 cnt = rem[i].command_cnt;
688 adr = rem[i].command_adr;
690 for (i=0;i < cnt;i++) {
691 com = (IRDB_COMMAND *)(flash_data + adr);
692 if (!memcmp (com->name,command,20)) return (adr);
694 if (vers >= 1) adr += IRDB_COMMAND_LEN;
695 else adr += IRDB_COMMAND_LEN_1;
699 return (0);
703 word FindIRDBRemote (byte remote[],IRDB_HEADER *pnt,byte vers)
705 word i,adr,len;
706 IRDB_REMOTE *rem;
708 IRDB_REMOTE_COMPACT *remc;
710 if (vers >= 2) {
711 adr = pnt->table.remote_adr;
713 len = (word)strlen (remote);
715 for (i=0;i < pnt->table.remote_cnt;i++) {
716 remc = (IRDB_REMOTE_COMPACT *)(flash_data + adr);
717 if (len == remc->namelen && !memcmp (remc->name,remote,len)) break;
718 adr += (IRDB_REMOTE_LEN_COMPACT + remc->namelen) / 2;
722 if (i == pnt->table.remote_cnt) return (0);
724 return (adr);
727 else {
728 rem = (IRDB_REMOTE *)(flash_data + pnt->table.remote_adr);
730 for (i=0;i < pnt->table.remote_cnt;i++) if (!memcmp (rem[i].name,remote,80)) break;
732 if (i == pnt->table.remote_cnt) return (0);
734 return ((word)((word *)(&rem[i]) - flash_data));
737 return (0);
742 int FileTransData (char nm[],byte dbtype,byte filemode)
744 int i;
745 char st[255];
747 strcpy (st,nm);
748 if (dbtype == 1) strcat (st,".tra");
749 if (dbtype == 2) strcat (st,".irdb");
750 if (filemode == FILE_MODE_SAVEAS) {
751 fptrans = fopen (st,"r");
752 if (fptrans) {
753 fclose (fptrans);
754 return (ERR_OVERWRITE);
758 fptrans = fopen (st,"w");
759 if (fptrans == NULL) return ERR_OPENTRANS;
761 for (i=0;i <= trans_num;i++) {
762 if (dbtype == 2) {
763 if (irdb[i].type == IRDB_TYPEHEADER) {
764 int j;
765 fprintf (fptrans,"[DEFAULTACTION]%s\n",irdbheader.default_action);
766 fprintf (fptrans," [RESULT_ERR]%s\n",irdbheader.err_status);
767 fprintf (fptrans," [RESULT_OK]%s\n",irdbheader.ok_status);
768 fprintf (fptrans," [ACTIVE]%d\n",irdbheader.active);
769 for (j=0;j <= 3;j++) {
770 fprintf (fptrans," [INPUTMODE_%d]%d\n",j,irdbheader.inputmode[j]);
771 fprintf (fptrans," [MIN_%d]%d\n",j,irdbheader.min[j]);
772 fprintf (fptrans," [MAX_%d]%d\n",j,irdbheader.max[j]);
773 fprintf (fptrans," [HYST_%d]%d\n",j,irdbheader.hyst[j]);
774 fprintf (fptrans," [MINREMOTE_%d]%s\n",j,irdbheader.minremote[j]);
775 fprintf (fptrans," [MINCOMMAND_%d]%s\n",j,irdbheader.mincommand[j]);
776 fprintf (fptrans," [MAXREMOTE_%d]%s\n",j,irdbheader.maxremote[j]);
777 fprintf (fptrans," [MAXCOMMAND_%d]%s\n",j,irdbheader.maxcommand[j]);
778 fprintf (fptrans," [MINLED_%d]%d\n",j,irdbheader.minledsel[j]);
779 fprintf (fptrans," [MINMASK_%d]%d\n",j,irdbheader.minmask[j]);
780 fprintf (fptrans," [MAXLED_%d]%d\n",j,irdbheader.maxledsel[j]);
781 fprintf (fptrans," [MAXMASK_%d]%d\n",j,irdbheader.maxmask[j]);
783 fprintf (fptrans,"[END]\n");
785 if (irdb[i].type == IRDB_REM) {
786 fprintf (fptrans,"[DBREMOTE2]%s\n",irdb[i].remote);
788 fprintf (fptrans," [IP]%03d.%03d.%03d.%03d\n",(irdb[i].ip >> 24) & 255,(irdb[i].ip >> 16) & 255,(irdb[i].ip >> 8) & 255,irdb[i].ip & 255);
789 fprintf (fptrans," [PORT]%d\n",irdb[i].port);
790 fprintf (fptrans," [FLAGS]%d\n",irdb[i].action_type);
791 fprintf (fptrans," [RACTION]%s\n",irdb[i].action);
792 fprintf (fptrans,"[END]\n");
794 if (irdb[i].type == IRDB_TYPEACTION || irdb[i].type == IRDB_TYPEACTION_1 || irdb[i].type == IRDB_TYPEACTION_2) {
795 fprintf (fptrans,"[COMACTION_%d]\n [ACREMOTE]%s\n",irdb[i].type - IRDB_TYPEACTION,irdb[i].remote);
796 fprintf (fptrans," [ACCOMMAND]%s\n",irdb[i].command);
797 fprintf (fptrans," [ACFLAGS]%d\n",irdb[i].action_type);
798 if (irdb[i].type == IRDB_TYPEACTION)
799 fprintf (fptrans," [RELAIS]%c%d\n",irdb[i].relais & 125,(irdb[i].relais & 128)>>6 | (irdb[i].relais & 2)>>1);
800 fprintf (fptrans," [ACLEN]%d\n",irdb[i].action_len);
801 fprintf (fptrans," [IP]%03d.%03d.%03d.%03d\n",(irdb[i].ip >> 24) & 255,(irdb[i].ip >> 16) & 255,(irdb[i].ip >> 8) & 255,irdb[i].ip & 255);
802 fprintf (fptrans," [PORT]%d\n",irdb[i].port);
803 fprintf (fptrans," [ACTION]");
804 fwrite (irdb[i].action,1,irdb[i].action_len,fptrans);
805 fprintf (fptrans,"\n[END]\n");
809 if (dbtype == 1) {
810 if (trans[i].type == F_CONFIG) {
811 fprintf (fptrans,"[CONFIG]\n [NAME]%s\n",trans[i].remote);
813 fprintf (fptrans," [SETTINGS]0x%x\n",trans[i].setup);
814 fprintf (fptrans," [SOURCEMASK]0x%x\n",trans[i].source_mask);
815 fprintf (fptrans," [TARGETMASK]0x%x\n",trans[i].target_mask);
816 fprintf (fptrans,"[END]\n");
818 if ((trans[i].type >= F_COMMAND && trans[i].type <= F_VOLUMEMACROD) ||
819 (trans[i].type >= F_TOGGLE && trans[i].type < (F_TOGGLE + 6*4)) || trans[i].type >= F_MACRO) {
820 if (trans[i].type == F_COMMAND) fprintf (fptrans,"[RCVCOMMAND]\n");
821 if (trans[i].type == F_VOLUMEMACRO) fprintf (fptrans,"[VOLUMEMACRO]\n");
822 if (trans[i].type == F_VOLUMEMACROD) fprintf (fptrans,"[VOLUMEMACROD]\n");
823 if (trans[i].type >= F_MACRO) fprintf (fptrans,"[MACRO %d]\n",trans[i].type - F_MACRO);
824 else if (trans[i].type >= F_TOGGLE) fprintf (fptrans,"[TOGGLE %d]\n",trans[i].type - F_TOGGLE);
826 fprintf (fptrans," [REMOTE]%s\n",trans[i].remote);
827 fprintf (fptrans," [COMMAND]%s\n",trans[i].command);
828 fprintf (fptrans," [SETTINGS]0x%x\n",trans[i].setup);
829 fprintf (fptrans," [SOURCEMASK]0x%x\n",trans[i].source_mask);
830 fprintf (fptrans," [ACC_TIMEOUT]%d\n",trans[i].accelerator_timeout);
831 fprintf (fptrans," [ACC_SETUP]0x%x\n",trans[i].accelerator_repeat);
832 fprintf (fptrans," [REMOTE_NUM]%d\n",trans[i].remote_num);
833 fprintf (fptrans," [GROUP_NUM]%d\n",trans[i].group_num);
834 fprintf (fptrans," [SHIFT_NUM]%d\n",trans[i].multi_num);
835 fprintf (fptrans," [INCLUDE_NAMES]%d\n",trans[i].include_names);
836 fprintf (fptrans,"[END]\n");
838 if (trans[i].type == F_SEND) {
839 fprintf (fptrans,"[SEND]\n");
841 fprintf (fptrans," [REMOTE]%s\n",trans[i].remote);
842 fprintf (fptrans," [COMMAND]%s\n",trans[i].command);
843 fprintf (fptrans," [TARGETMASK]0x%x\n",trans[i].target_mask);
844 fprintf (fptrans," [TIMEOUT]%d\n",trans[i].wait_timeout);
845 fprintf (fptrans," [REMOTE_NUM]%d\n",trans[i].remote_num);
846 fprintf (fptrans," [GROUP_NUM]%d\n",trans[i].group_num);
847 fprintf (fptrans," [SHIFT_NUM]%d\n",trans[i].multi_num);
848 fprintf (fptrans,"[END]\n");
850 if (trans[i].type == F_REMOTE) {
851 fprintf (fptrans,"[REMOTE]\n [NAME]%s\n",trans[i].remote);
852 fprintf (fptrans," [SETTINGS]0x%x\n",trans[i].setup);
853 fprintf (fptrans," [SOURCEMASK]0x%x\n",trans[i].source_mask);
854 fprintf (fptrans," [TARGETMASK]0x%x\n",trans[i].target_mask);
855 fprintf (fptrans," [REMOTE_NUM]%d\n",trans[i].remote_num);
856 fprintf (fptrans,"[END]\n");
858 if (trans[i].type == F_ENABLEGROUP) {
859 fprintf (fptrans,"[GROUP]\n");
860 fprintf (fptrans," [REMOTE]%s\n",trans[i].remote);
861 fprintf (fptrans," [COMMAND]%s\n",trans[i].command);
862 fprintf (fptrans," [SETTINGS]0x%x\n",trans[i].setup);
863 fprintf (fptrans," [SOURCEMASK]0x%x\n",trans[i].source_mask);
864 fprintf (fptrans," [REMOTE_NUM]%d\n",trans[i].remote_num);
865 fprintf (fptrans," [GROUP_NUM]%d\n",trans[i].group_num);
866 fprintf (fptrans,"[END]\n");
868 if (trans[i].type == F_PREKEY) {
869 fprintf (fptrans,"[SHIFT]\n");
870 fprintf (fptrans," [REMOTE]%s\n",trans[i].remote);
871 fprintf (fptrans," [COMMAND]%s\n",trans[i].command);
872 fprintf (fptrans," [SETTINGS]0x%x\n",trans[i].setup);
873 fprintf (fptrans," [SOURCEMASK]0x%x\n",trans[i].source_mask);
874 fprintf (fptrans," [REMOTE_NUM]%d\n",trans[i].remote_num);
875 fprintf (fptrans," [GROUP_NUM]%d\n",trans[i].group_num);
876 fprintf (fptrans," [SHIFT_NUM]%d\n",trans[i].multi_num);
877 fprintf (fptrans,"[END]\n");
882 fclose (fptrans);
883 return (0);
886 int LoadIRDB (IRDBBUFFER *db,char nm[],word offset)
888 int i;
889 char ln[255],*st;
891 if (!offset) {
892 IRDBHEADERBUFFER *dbh;
893 dbh = (IRDBHEADERBUFFER *)db;
895 strcpy (ln,nm);
896 strcat (ln,".irdb");
897 fptrans = fopen (ln,"r");
898 if (fptrans == NULL) return ERR_OPENTRANS;
899 trans_num = 0;
900 memset (&irdbheader,0,sizeof(IRDBHEADER));
901 while (st = DBReadString (ln,fptrans,0)) {
902 ReadTraReceive (st);
904 fclose (fptrans);
906 memset (dbh,0,sizeof (IRDBHEADERBUFFER));
907 dbh->statustype = STATUS_IRDB;
908 dbh->statuslen = sizeof (IRDBHEADERBUFFER);
909 dbh->offset = offset;
910 memcpy (&dbh->header,&irdbheader,sizeof (IRDBHEADER));
911 dbh->count_buffer = 1;
912 dbh->count_total = trans_num;
913 dbh->count_remaining = (short)(trans_num - offset);
915 return (0);
918 i = 0;
919 memset (db,0,sizeof (IRDBBUFFER));
920 db->statustype = STATUS_IRDB;
921 db->statuslen = sizeof (IRDBBUFFER);
922 db->offset = offset;
924 while (i < 12 && offset < trans_num) {
925 memcpy (&db->dbdata[i],&irdb[offset],sizeof (IRDBCOMMAND));
926 i++;
927 offset++;
930 db->count_buffer = i;
931 db->count_total = trans_num;
932 if (i == 12) db->count_remaining = (short)(trans_num - offset);
933 else db->count_remaining = 0;
934 return (0);
937 int LoadTranslation (TRANSLATEBUFFER *tb,char nm[],word offset)
939 int i;
940 char ln[255],*st;
942 if (!offset) {
943 strcpy (ln,nm);
944 strcat (ln,".tra");
945 fptrans = fopen (ln,"r");
946 if (fptrans == NULL) return ERR_OPENTRANS;
947 trans_num = 0;
948 while (st = DBReadString (ln,fptrans,0)) {
949 ReadTraReceive (st);
951 fclose (fptrans);
954 i = 0;
955 memset (tb,0,sizeof (TRANSLATEBUFFER));
956 tb->statustype = STATUS_TRANSLATE;
957 tb->statuslen = sizeof (TRANSLATEBUFFER);
958 tb->offset = offset;
960 while (i < 30 && offset < trans_num) {
961 memcpy (&tb->trdata[i],&trans[offset],sizeof (TRANSLATECOMMAND));
962 i++;
963 offset++;
966 tb->count_buffer = i;
967 tb->count_total = trans_num;
968 if (i == 30) tb->count_remaining = (short)(trans_num - offset);
969 else tb->count_remaining = 0;
970 return (0);
974 void ReadTraReceive (char tp[])
976 int pos;
977 char ln[255],*st;
979 memset (&trans[trans_num],0,sizeof (TRANSLATECOMMAND));
980 memset (&irdb[trans_num],0,sizeof (IRDBCOMMAND));
982 irdb[trans_num].number = trans_num;
983 trans[trans_num].number = trans_num;
986 if (!strncmp (tp,"[DBREMOTE]",10)) {
987 irdb[trans_num].type = IRDB_REM;
988 memset (irdb[trans_num].remote,' ',80);
989 memcpy (irdb[trans_num].remote,tp + 10,strlen (tp + 10));
990 irdb[trans_num].action_type = 1;
991 trans_num++;
992 return;
995 if (!strcmp (tp,"[RCVCOMMAND]")) trans[trans_num].type = F_COMMAND;
996 if (!strcmp (tp,"[VOLUMEMACRO]")) trans[trans_num].type = F_VOLUMEMACRO;
997 if (!strcmp (tp,"[VOLUMEMACROD]")) trans[trans_num].type = F_VOLUMEMACROD;
998 if (!strncmp (tp,"[MACRO ",7)) trans[trans_num].type = F_MACRO + (byte)strtoul (tp + 7,0,0);
999 if (!strncmp (tp,"[TOGGLE ",8)) trans[trans_num].type = F_TOGGLE + (byte)strtoul (tp + 8,0,0);
1000 if (!strcmp (tp,"[SEND]")) trans[trans_num].type = F_SEND;
1001 if (!strcmp (tp,"[CONFIG]")) trans[trans_num].type = F_CONFIG;
1002 if (!strcmp (tp,"[REMOTE]")) trans[trans_num].type = F_REMOTE;
1003 if (!strcmp (tp,"[GROUP]")) trans[trans_num].type = F_ENABLEGROUP;
1004 if (!strcmp (tp,"[SHIFT]")) trans[trans_num].type = F_PREKEY;
1005 if (!strcmp (tp,"[COMACTION]")) irdb[trans_num].type = IRDB_TYPEACTION;
1006 if (!strcmp (tp,"[COMACTION_0]")) irdb[trans_num].type = IRDB_TYPEACTION;
1007 if (!strcmp (tp,"[COMACTION_1]")) irdb[trans_num].type = IRDB_TYPEACTION_1;
1008 if (!strcmp (tp,"[COMACTION_2]")) irdb[trans_num].type = IRDB_TYPEACTION_2;
1009 if (!strncmp (tp,"[DBREMOTE2]",11)) {
1010 irdb[trans_num].type = IRDB_REM;
1011 memset (irdb[trans_num].remote,' ',80);
1012 memcpy (irdb[trans_num].remote,tp + 11,strlen (tp + 11));
1014 if (!strncmp (tp,"[DEFAULTACTION]",15)) {
1015 irdb[trans_num].type = IRDB_TYPEHEADER;
1016 irdbheader.type = IRDB_TYPEHEADER;
1017 memset (irdbheader.default_action ,' ',100);
1018 tp[115] = 0;
1019 memcpy (irdbheader.default_action,tp + 15,strlen (tp + 15));
1021 pos = ftell (fptrans);
1022 while ((st = DBReadString (ln,fptrans,0)) && strcmp (st,"[END]")) {
1023 if (!strncmp (st,"[NAME]",6)) {
1024 memset (trans[trans_num].remote,' ',80);
1025 memcpy (trans[trans_num].remote,st + 6,strlen (st + 6));
1027 if (!strncmp (st,"[REMOTE]",8)) {
1028 memset (trans[trans_num].remote,' ',80);
1029 memcpy (trans[trans_num].remote,st + 8,strlen (st + 8));
1031 if (!strncmp (st,"[PORT]",6)) irdb[trans_num].port = (word)strtoul (st + 6,0,0);
1032 if (!strncmp (st,"[FLAGS]",7)) irdb[trans_num].action_type = (byte)strtoul (st + 7,0,0);
1033 if (!strncmp (st,"[IP]",4)) {
1034 st[7] = 0;
1035 irdb[trans_num].ip = (byte)strtoul (st + 4,0,10) << 24;
1036 st[11] = 0;
1037 irdb[trans_num].ip |= (byte)strtoul (st + 8,0,10) << 16;
1038 st[15] = 0;
1039 irdb[trans_num].ip |= (byte)strtoul (st + 12,0,10) << 8;
1040 st[19] = 0;
1041 irdb[trans_num].ip |= (byte)strtoul (st + 16,0,10);
1043 if (!strncmp (st,"[RACTION]",9)) {
1044 memset (irdb[trans_num].action ,' ',256);
1045 st[59] = 0;
1046 memcpy (irdb[trans_num].action,st + 9,strlen (st + 9));
1049 if (!strncmp (st,"[COMMAND]",9)) {
1050 memset (trans[trans_num].command ,' ',20);
1051 memcpy (trans[trans_num].command ,st + 9,strlen (st + 9));
1053 if (!strncmp (st,"[RESULT_ERR]",12)) {
1054 strcpy (irdbheader.err_status,st + 12);
1056 if (!strncmp (st,"[RESULT_OK]",11)) {
1057 strcpy (irdbheader.ok_status,st + 11);
1059 if (!strncmp (st,"[ACREMOTE]",10)) {
1060 memset (irdb[trans_num].remote,' ',80);
1061 memcpy (irdb[trans_num].remote,st + 10,strlen (st + 10));
1063 if (!strncmp (st,"[ACCOMMAND]",11)) {
1064 memset (irdb[trans_num].command ,' ',20);
1065 memcpy (irdb[trans_num].command ,st + 11,strlen (st + 11));
1067 if (!strncmp (st,"[ACTION]",8)) {
1068 fseek (fptrans,pos+10,SEEK_SET);
1069 fread (irdb[trans_num].action,1,irdb[trans_num].action_len,fptrans);
1071 if (!strncmp (st,"[RELAIS]",8)) irdb[trans_num].relais = st[8] + ((st[9] & 2) << 6) + ((st[9] & 1) << 1);
1072 if (!strncmp (st,"[ACTYPE]",8)) {
1073 if (st[8] == 'A') irdb[trans_num].action_type = 130;
1074 if (st[8] == 'B') irdb[trans_num].action_type = 131;
1076 if (!strncmp (st,"[ACFLAGS]",9)) irdb[trans_num].action_type = atoi (st+9);
1077 if (!strncmp (st,"[ACLEN]",7)) irdb[trans_num].action_len = (byte)strtoul (st + 7,0,0);
1079 if (!strncmp (st,"[INPUTMODE_0]",13)) irdbheader.inputmode[0] = (byte)strtoul (st + 13,0,0);
1080 if (!strncmp (st,"[INPUTMODE_1]",13)) irdbheader.inputmode[1] = (byte)strtoul (st + 13,0,0);
1081 if (!strncmp (st,"[INPUTMODE_2]",13)) irdbheader.inputmode[2] = (byte)strtoul (st + 13,0,0);
1082 if (!strncmp (st,"[INPUTMODE_3]",13)) irdbheader.inputmode[3] = (byte)strtoul (st + 13,0,0);
1083 if (!strncmp (st,"[MIN_0]",7)) irdbheader.min[0] = (byte)strtoul (st + 7,0,0);
1084 if (!strncmp (st,"[MIN_1]",7)) irdbheader.min[1] = (byte)strtoul (st + 7,0,0);
1085 if (!strncmp (st,"[MIN_2]",7)) irdbheader.min[2] = (byte)strtoul (st + 7,0,0);
1086 if (!strncmp (st,"[MIN_3]",7)) irdbheader.min[3] = (byte)strtoul (st + 7,0,0);
1087 if (!strncmp (st,"[MAX_0]",7)) irdbheader.max[0] = (byte)strtoul (st + 7,0,0);
1088 if (!strncmp (st,"[MAX_1]",7)) irdbheader.max[1] = (byte)strtoul (st + 7,0,0);
1089 if (!strncmp (st,"[MAX_2]",7)) irdbheader.max[2] = (byte)strtoul (st + 7,0,0);
1090 if (!strncmp (st,"[MAX_3]",7)) irdbheader.max[3] = (byte)strtoul (st + 7,0,0);
1091 if (!strncmp (st,"[HYST_0]",8)) irdbheader.hyst[0] = (byte)strtoul (st + 8,0,0);
1092 if (!strncmp (st,"[HYST_1]",8)) irdbheader.hyst[1] = (byte)strtoul (st + 8,0,0);
1093 if (!strncmp (st,"[HYST_2]",8)) irdbheader.hyst[2] = (byte)strtoul (st + 8,0,0);
1094 if (!strncmp (st,"[HYST_3]",8)) irdbheader.hyst[3] = (byte)strtoul (st + 8,0,0);
1095 if (!strncmp (st,"[MINLED_0]",10)) irdbheader.minledsel[0] = (byte)strtoul (st + 10,0,0);
1096 if (!strncmp (st,"[MINLED_1]",10)) irdbheader.minledsel[1] = (byte)strtoul (st + 10,0,0);
1097 if (!strncmp (st,"[MINLED_2]",10)) irdbheader.minledsel[2] = (byte)strtoul (st + 10,0,0);
1098 if (!strncmp (st,"[MINLED_3]",10)) irdbheader.minledsel[3] = (byte)strtoul (st + 10,0,0);
1099 if (!strncmp (st,"[MAXLED_0]",10)) irdbheader.maxledsel[0] = (byte)strtoul (st + 10,0,0);
1100 if (!strncmp (st,"[MAXLED_1]",10)) irdbheader.maxledsel[1] = (byte)strtoul (st + 10,0,0);
1101 if (!strncmp (st,"[MAXLED_2]",10)) irdbheader.maxledsel[2] = (byte)strtoul (st + 10,0,0);
1102 if (!strncmp (st,"[MAXLED_3]",10)) irdbheader.maxledsel[3] = (byte)strtoul (st + 10,0,0);
1103 if (!strncmp (st,"[MINMASK_0]",11)) irdbheader.minmask[0] = strtoul (st + 11,0,0);
1104 if (!strncmp (st,"[MINMASK_1]",11)) irdbheader.minmask[1] = strtoul (st + 11,0,0);
1105 if (!strncmp (st,"[MINMASK_2]",11)) irdbheader.minmask[2] = strtoul (st + 11,0,0);
1106 if (!strncmp (st,"[MINMASK_3]",11)) irdbheader.minmask[3] = strtoul (st + 11,0,0);
1107 if (!strncmp (st,"[MAXMASK_0]",11)) irdbheader.maxmask[0] = strtoul (st + 11,0,0);
1108 if (!strncmp (st,"[MAXMASK_1]",11)) irdbheader.maxmask[1] = strtoul (st + 11,0,0);
1109 if (!strncmp (st,"[MAXMASK_2]",11)) irdbheader.maxmask[2] = strtoul (st + 11,0,0);
1110 if (!strncmp (st,"[MAXMASK_3]",11)) irdbheader.maxmask[3] = strtoul (st + 11,0,0);
1111 if (!strncmp (st,"[MINREMOTE_",11)) {
1112 st[13 + 80] = 0;
1113 memset (irdbheader.minremote[st[11] - '0'],' ',80);
1114 memcpy (irdbheader.minremote[st[11] - '0'],st + 13,strlen (st + 13));
1116 if (!strncmp (st,"[MAXREMOTE_",11)) {
1117 st[13 + 80] = 0;
1118 memset (irdbheader.maxremote[st[11] - '0'],' ',80);
1119 memcpy (irdbheader.maxremote[st[11] - '0'],st + 13,strlen (st + 13));
1121 if (!strncmp (st,"[MINCOMMAND_",12)) {
1122 st[14 + 50] = 0;
1123 memset (irdbheader.mincommand[st[12] - '0'],' ',50);
1124 memcpy (irdbheader.mincommand[st[12] - '0'],st + 14,strlen (st + 14));
1126 if (!strncmp (st,"[MAXCOMMAND_",11)) {
1127 st[14 + 50] = 0;
1128 memset (irdbheader.maxcommand[st[12] - '0'],' ',50);
1129 memcpy (irdbheader.maxcommand[st[12] - '0'],st + 14,strlen (st + 14));
1131 if (!strncmp (st,"[ACTIVE]",8)) irdbheader.active = (byte)strtoul (st + 8,0,0);
1132 if (!strncmp (st,"[SETTINGS]",10)) trans[trans_num].setup = (byte)strtoul (st + 10,0,0);
1133 if (!strncmp (st,"[SOURCEMASK]",12)) trans[trans_num].source_mask = strtoul (st + 12,0,0);
1134 if (!strncmp (st,"[TARGETMASK]",12)) trans[trans_num].target_mask = strtoul (st + 12,0,0);
1135 if (!strncmp (st,"[REMOTE_NUM]",12)) trans[trans_num].remote_num = (byte)strtoul (st + 12,0,0);
1136 if (!strncmp (st,"[GROUP_NUM]",11)) trans[trans_num].group_num = (byte)strtoul (st + 11,0,0);
1137 if (!strncmp (st,"[SHIFT_NUM]",11)) trans[trans_num].multi_num = (byte)strtoul (st + 11,0,0);
1138 if (!strncmp (st,"[ACC_TIMEOUT]",13)) trans[trans_num].accelerator_timeout = (byte)strtoul (st + 13,0,0);
1139 if (!strncmp (st,"[ACC_SETUP]",11)) trans[trans_num].accelerator_repeat = (byte)strtoul (st + 11,0,0);
1140 if (!strncmp (st,"[TIMEOUT]",9)) trans[trans_num].wait_timeout = (word)strtoul (st + 9,0,0);
1141 if (!strncmp (st,"[INCLUDE_NAMES]",15)) trans[trans_num].include_names = (byte)strtoul (st + 15,0,0);
1142 pos = ftell (fptrans);
1144 trans_num++;
1147 int SetFlashdataEx (byte bus,int iradr)
1149 byte cal;
1150 char remname[100];
1151 int adr,i,j,flashwordsize,nrem;
1152 word val;
1153 IRDATA ird;
1154 FLASH_CONTENT *pnt;
1155 int cmd_count = 0;
1156 int cmd_num = 0;
1158 cal = (IRDevices[bus].fw_capabilities & FN_CALIBRATE) != 0;
1160 for (i=0;i <= trans_num;i++) if ((trans[i].type >= F_COMMAND && trans[i].type <= F_VOLUMEMACROD) ||
1161 (trans[i].type >= F_TOGGLE && trans[i].type < (F_TOGGLE + 6*4)) ||
1162 trans[i].type >= F_MACRO || trans[i].type == F_ENABLEGROUP || trans[i].type == F_PREKEY) {
1163 if (DBFindRemoteCommandEx (trans[i].remote,trans[i].command,&ird,cal)) trans[i].type = F_ERROR;
1164 else cmd_count++;
1167 memset (flash_data,0,sizeof (flash_data));
1168 pnt = (FLASH_CONTENT *)flash_data;
1169 pnt->dir_cnt = cmd_count;
1170 pnt->magic = F_MAGIC;
1171 pnt->checksum = 0;
1173 pnt->rcv_len = 0;
1174 for (i=0;i < REMOTE_CNT;i++) pnt->group_flags[i] = 1;
1175 pnt->trans_setup[0] = trans[0].setup;
1176 pnt->source_mask[0] = (word)trans[0].source_mask;
1177 pnt->target_mask[0] = (word)trans[0].target_mask;
1178 pnt->end_pnt = pnt->data_pnt = CONTENT_LEN;
1179 cmd_num = 0;
1181 for (i=0;i <= trans_num;i++) if ((trans[i].type >= F_COMMAND && trans[i].type <= F_VOLUMEMACROD) ||
1182 (trans[i].type >= F_TOGGLE && trans[i].type < (F_TOGGLE + 6*4)) ||
1183 trans[i].type >= F_MACRO || trans[i].type == F_ENABLEGROUP || trans[i].type == F_PREKEY) {
1184 memset (remname,0,sizeof(remname));
1185 strcpy (remname,trans[i].remote);
1186 nrem = DBFindRemote (remname);
1187 if (nrem >= 0) if (rem_pnt[nrem].rcv_len && (!pnt->rcv_len || rem_pnt[nrem].rcv_len < pnt->rcv_len)) pnt->rcv_len = rem_pnt[nrem].rcv_len;
1191 for (i=0;i <= trans_num;i++) if ((trans[i].type >= F_COMMAND && trans[i].type <= F_VOLUMEMACROD) ||
1192 (trans[i].type >= F_TOGGLE && trans[i].type < (F_TOGGLE + 6*4)) ||
1193 trans[i].type >= F_MACRO || trans[i].type == F_ENABLEGROUP || trans[i].type == F_PREKEY) {
1194 DBFindRemoteCommandEx (trans[i].remote,trans[i].command,&ird,cal);
1195 ird.data[ird.ir_length] = 0;
1196 set_entry (ird.data,cmd_num,pnt,trans[i].type,trans[i].remote_num,trans[i].group_num,trans[i].multi_num,trans[i].setup,
1197 (word)trans[i].source_mask,trans[i].accelerator_timeout,trans[i].accelerator_repeat,remote_statusex[bus].stat[iradr].version+1,trans[i].include_names,trans[i].remote,trans[i].command,pnt->rcv_len);
1198 cmd_num++;
1201 cmd_num = 0;
1202 for (i=0;i <= trans_num;i++) {
1203 if (trans[i].type == F_REMOTE) {
1204 pnt->trans_setup[trans[i].remote_num] = trans[i].setup;
1205 pnt->target_mask[trans[i].remote_num] = (word)trans[i].target_mask;
1206 pnt->source_mask[trans[i].remote_num] = (word)trans[i].source_mask;
1208 if (trans[i].type == F_ENABLEGROUP) cmd_num++;
1210 if (trans[i].type == F_PREKEY) cmd_num++;
1212 if (trans[i].type >= F_COMMAND && trans[i].type <= F_VOLUMEMACROD) {
1213 if (trans[i+1].type == F_SEND) {
1214 DBFindRemoteCommandEx (trans[i+1].remote,trans[i+1].command,&ird,cal);
1215 ird.target_mask = (word)trans[i+1].target_mask;
1216 set_commanddata (cmd_num,&ird);
1217 if (trans[i].type == F_VOLUMEMACROD) {
1218 if (trans[i+2].type == F_SEND) {
1219 DBFindRemoteCommandEx (trans[i+2].remote,trans[i+2].command,&ird,cal);
1220 ird.target_mask = (word)trans[i+2].target_mask;
1221 set_commanddata (-1,&ird);
1225 cmd_num++;
1227 if (trans[i].type >= F_MACRO) {
1228 for (j=0;j < trans[i].type - F_MACRO && trans[i+j+1].type == F_SEND;j++) {
1229 DBFindRemoteCommandEx (trans[i+j+1].remote,trans[i+j+1].command,&ird,cal);
1230 ird.target_mask = (word)trans[i+j+1].target_mask;
1231 ird.address = trans[i+j+1].wait_timeout / 16;
1232 if (j) set_commanddata (-1,&ird);
1233 else set_commanddata (cmd_num,&ird);
1235 cmd_num++;
1237 if (trans[i].type >= F_TOGGLE && trans[i].type < (F_TOGGLE + 6*4)) {
1238 for (j=0;j < ((trans[i].type - F_TOGGLE) % 6) && trans[i+j+1].type == F_SEND;j++) {
1239 DBFindRemoteCommandEx (trans[i+j+1].remote,trans[i+j+1].command,&ird,cal);
1240 ird.target_mask = (word)trans[i+j+1].target_mask;
1241 ird.address = trans[i+j+1].wait_timeout / 16;
1242 if (j) set_commanddata (-1,&ird);
1243 else set_commanddata (cmd_num,&ird);
1245 cmd_num++;
1249 val = 0;
1250 adr = CONTENT_LEN;
1251 while (adr < pnt->end_pnt) val += flash_data[adr++];
1252 pnt->checksum = val;
1254 // cap = iradr >> 8;
1255 // if (!(cap & FN_TRANSL)) flashwordsize = 64;
1256 // else
1258 flashwordsize = 128;
1260 adr = 0;
1261 while (adr < pnt->end_pnt) {
1262 if ((adr + flashwordsize /2) < pnt->end_pnt) TransferFlashdataEx (bus,flash_data + adr,adr,flashwordsize,0,iradr);
1263 else TransferFlashdataEx (bus,flash_data + adr,adr,flashwordsize,1,iradr);
1264 adr += flashwordsize / 2;
1266 return (0);
1271 void set_commanddata (int pos,IRDATA *irpnt)
1273 HASH_ENTRY *hash_table;
1274 FLASH_ENTRY_ORG *fentry;
1275 FLASH_CONTENT *content;
1277 content = (FLASH_CONTENT *)flash_data;
1278 hash_table = (HASH_ENTRY *)(flash_data + CONTENT_LEN);
1279 fentry = (FLASH_ENTRY_ORG *)(flash_data + hash_table[pos].adr);
1281 irpnt -> command = HOST_SEND;
1282 if (irpnt -> mode & RAW_DATA)
1283 irpnt -> len = (sizeof (IRDATA) - (CODE_LEN + (RAW_EXTRA))) + irpnt -> ir_length;
1284 else
1285 irpnt -> len = (sizeof (IRDATA) - CODE_LEN) + irpnt -> ir_length;
1287 if (time_len != TIME_LEN) {
1288 if (irpnt->mode & RAW_DATA) {
1289 if (irpnt->ir_length > OLD_LENRAW) return;
1291 else {
1292 if (irpnt->time_cnt > 6) return;
1293 ConvertToIRTRANS3 (irpnt);
1298 // irpnt->target_mask = 0xffff;
1300 if (pos >= 0) fentry->flash_adr = content->end_pnt;
1302 memcpy ((void *)(flash_data + content->end_pnt),(void *)irpnt,irpnt->len);
1303 content->end_pnt += (irpnt->len + 1) / 2;
1307 void set_entry (char entry[],int pos,FLASH_CONTENT *content,byte type,byte remote,byte group,byte shift,byte setup,word sourcemask,word acc_timeout,word acc_repeat,char *version,byte nmflag,char *remname,char *comname,byte rcv_len)
1309 HASH_ENTRY *hash_table;
1310 FLASH_ENTRY_ORG *fentry;
1312 hash_table = (HASH_ENTRY *)(flash_data + CONTENT_LEN);
1314 if (!pos) content->data_pnt = CONTENT_LEN + content->dir_cnt * (sizeof (HASH_ENTRY) / 2);
1316 if (rcv_len) entry[rcv_len] = 0;
1319 hash_table[pos].hashcode = get_hashcode (entry,(byte)strlen (entry));
1320 hash_table[pos].adr = content->data_pnt;
1322 fentry = (FLASH_ENTRY_ORG *)(flash_data + content->data_pnt);
1323 fentry->type = type;
1324 fentry->flash_adr = 0;
1325 if (group) fentry->group = group - 1;
1326 else fentry->group = 0;
1327 fentry->remote = remote;
1328 fentry->source_mask = sourcemask;
1329 fentry->accelerator_timeout = (byte)acc_timeout;
1330 fentry->accelerator_repeat = (byte)acc_repeat;
1331 fentry->trans_setup = setup;
1332 fentry->len = (byte)strlen (entry);
1334 if (strcmp (version,"5.04.05") >= 0) {
1335 byte stlen = 0;
1336 char nmstr[256];
1337 if (nmflag) {
1338 sprintf (nmstr,"%s,%s\r\n",remname,comname);
1339 stlen = (byte)strlen (nmstr);
1341 fentry->cdata[0] = shift;
1342 fentry->cdata[1] = stlen;
1343 strcpy (fentry->cdata+2,entry);
1344 strcpy (fentry->cdata+2+fentry->len,nmstr);
1346 content->end_pnt = content->data_pnt = (13 + fentry->len + stlen + 1) / 2 + hash_table[pos].adr;
1348 else if (strcmp (version,"4.04.27") >= 0) {
1349 fentry->cdata[0] = shift;
1350 strcpy (fentry->cdata+1,entry);
1351 content->end_pnt = content->data_pnt = (12 + fentry->len + 1) / 2 + hash_table[pos].adr;
1353 else {
1354 strcpy (fentry->cdata,entry);
1355 content->end_pnt = content->data_pnt = (11 + fentry->len + 1) / 2 + hash_table[pos].adr;
1362 void read_flashdata (byte *pnt,word adr,word cnt)
1364 set_flashadr (adr);
1365 read_nextflashdata (pnt,cnt);
1368 int last_adr;
1370 void set_flashadr (word adr)
1372 last_adr = adr;
1375 void read_nextflashdata (byte *pnt,word cnt)
1377 memcpy (pnt,flash_data + last_adr,cnt);
1378 last_adr += cnt / 2;
1383 word get_hashcode (byte data[],byte len)
1385 byte i;
1386 word h = 0;
1388 for (i=0;i < len;i++) h += (data[i] & 7) << ((i * 2) & 15);
1390 return (h);
1394 /*crc-16 standard root*/
1395 #define POLYNOMIAL 0x8005
1398 #pragma pack(1)
1400 typedef union {
1401 uint32_t Whole;
1402 struct
1404 byte Data;
1405 word Remainder;
1406 byte Head;
1407 } Part;
1408 } CRC_BUFFER;
1411 void PutCRC(byte b,CRC_BUFFER *crcb)
1413 byte i;
1414 crcb->Part.Data = b;
1415 for (i=0; i<8; i++)
1417 crcb->Whole = crcb->Whole << 1;
1418 if (crcb->Part.Head & 0x01)
1419 crcb->Part.Remainder ^= POLYNOMIAL;
1424 word CRC (byte *Data, int Length,word init)
1426 CRC_BUFFER CRC_buffer;
1428 CRC_buffer.Part.Remainder = init;
1429 while (Length-- > 0)
1430 PutCRC(*Data++,&CRC_buffer);
1431 PutCRC(0,&CRC_buffer);
1432 PutCRC(0,&CRC_buffer);
1433 return CRC_buffer.Part.Remainder;