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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
|
/*
* Changes for use with CM11A copyright 1996, 1997 Daniel B. Suthers,
* Pleasanton Ca, 94588 USA
* E-mail dbs@tanj.com
*/
/*
* Copyright 1986 by Larry Campbell, 73 Concord Street, Maynard MA 01754 USA
* (maynard!campbell). You may freely copy, use, and distribute this software
* subject to the following restrictions:
*
* 1) You may not charge money for it.
* 2) You may not remove or alter this copyright notice.
* 3) You may not claim you wrote it.
* 4) If you make improvements (or other changes), you are requested
* to send them to me, so there's a focal point for distributing
* improved versions.
*
* John Chmielewski (tesla!jlc until 9/1/86, then rogue!jlc) assisted
* by doing the System V port and adding some nice features. Thanks!
*
*/
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef SYSV
#include <string.h>
#else
#include <strings.h>
#endif
#include <time.h>
#include "x10.h"
#include "version.h"
#include <syslog.h>
#ifdef __GLIBC__
/* msf - added for glibc/rh 5.0 */
#include <sys/types.h>
#endif
/* I'm not sure why I had these (time and localtime) in here. Was there
a system where time.h wasn't enough?
extern long time();
extern struct tm *localtime();
*/
extern int tty;
extern int sptty;
extern char x10_tty[];
extern int usage();
extern int start_relay(),setup_sp_tty(), xread(), unit2int();
extern void error(), quit();
int x10_housecode;
int verbose;
int i_am_relay = 0;
void sigtimer(), err();
char hc2char();
void init();
int check4poll();
char
syncmsg[SYNCN], flag;
char latitude[20];
char longitude[20];
struct hstruct /* table to map housecodes into letters */
housetab[] =
{
{HC_A, 'a'},
{HC_B, 'b'},
{HC_C, 'c'},
{HC_D, 'd'},
{HC_E, 'e'},
{HC_F, 'f'},
{HC_G, 'g'},
{HC_H, 'h'},
{HC_I, 'i'},
{HC_J, 'j'},
{HC_K, 'k'},
{HC_L, 'l'},
{HC_M, 'm'},
{HC_N, 'n'},
{HC_O, 'o'},
{HC_P, 'p'}
};
char *wdays[] =
{"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "<day not set>"};
/* Table for mapping units (1-16) or house codes (a-p) to 4 bit nibbles.
* element 0 equates to unit 1 or code a
*/
unsigned char cm11map[]=
{ 06, 016, 02, 012, 01, 011, 05, 015,
07, 017, 03, 013, 0, 010, 04, 014
};
/* Table for mapping an x10 format bitmap to a unit number. It's
* the reverse of the one above.
* Note that the unit numbers are 1-16, but the array is 0-15.
*/
unsigned char map2cm11[]=
{
13, 5, 3, 11, 15, 7, 1, 9, 14, 6, 4, 12, 16, 8, 2, 10
};
int timeout = TIMEOUT, Irev, Iminutes, Iseconds, Ihours, Idays, Ijday;
int Istatdim, Istatmon, Iaddmon;
int newformat; /* for the output format */
unsigned char Ihcode;
extern int
c_data(), c_date(), c_delete(), c_diagnostic(), c_dump(), c_fdump(),
c_finfo(), c_fload(), c_info(), c_monitor(), c_reset(), c_schedule(),
c_setclock(), c_turn(), c_dbs(), c_stop(), c_erase(), c_preset();
extern int c_load_macro(), c_qerase();
int c_help(), c_version();
struct cmdentry {
char *cmd_name;
int (*cmd_routine) ();
} cmdtab[] = {
{"date", c_date} ,
{"erase", c_erase},
{"help", c_help},
{"info", c_info},
{"macro", c_load_macro},
{"monitor", c_monitor},
{"preset", c_preset},
{"reset", c_reset},
{"setclock", c_setclock},
{"status", c_turn},
{"stop", c_stop},
{"turn", c_turn},
{"upload", c_load_macro},
{"version", c_version},
{"qerase", c_qerase}, /* hidden feature */
{"", NULL }
};
char *argptr;
int main(argc, argv)
int argc;
char *argv[];
{
register int i;
int (*rtn) ();
struct cmdentry *c;
struct stat statb;
char RCSID[]= "@(#) $Id: x10.c,v 1.26 1999/12/26 20:37:17 dbs Exp $\n";
void read_config();
display(RCSID);
rtn = NULL;
verbose = 1; /* for the error message */
if( argc > 1)
{
if (strcmp(argv[1], "-v") == 0 )
{
verbose = 1;
for (i = 1; i < argc; i++ )
{
argv[i] = argv[i+1];
}
argc --;
}
else
verbose = 0;
}
if( verbose )
printf( "Version:%4s\n", VERSION );
if (argc < 2)
usage(E_NOCMD);
for (c = cmdtab; c->cmd_routine != NULL; c++) {
if (strcmp(argv[1], c->cmd_name) == 0) {
rtn = c->cmd_routine;
break;
}
}
if (rtn == NULL)
usage(E_INVCN);
if( stat(SPOOLDIR, &statb) != 0 )
{
fprintf( stderr, "Please build the %s directory with permissions 1777\n", SPOOLDIR);
exit(3);
}
if( (statb.st_mode & S_IRWXO) != S_IRWXO )
{
fprintf( stderr, "Please change the %s directory's permissions to 2777\n", SPOOLDIR);
exit(3);
}
read_config();
if( ( strcmp("stop", c->cmd_name) == 0 ) ||
(strcmp("version", c->cmd_name) == 0) ||
(strcmp("help", c->cmd_name) == 0) )
{
(*rtn)(); /* exits */
return(0);
}
argptr = argv[0];
/* Future: A command line of 'relay' directs this copy to start the relay
* process. If that's the case, don't check for relay.
*/
if( strcmp("relay", c->cmd_name) != 0 )
{
start_relay(x10_tty);
if( ! i_am_relay )
{
setup_sp_tty();
}
}
#ifdef MINIEXCH
mxconnect(MINIXPORT);
#endif
init(c->cmd_name);
(*rtn) (argc, argv);
return 0;
}
/*
* Convert X10-style day of week (bit map, bit 0=monday, 6=sunday)
* to UNIX localtime(3) style day of week (integer, 0=sunday)
*/
/* DBS NOTE: This is valid for CP290, not for CM11A.
* The CM11A uses unix style.
*/
int dowX2U(b)
register char b;
{
register int n;
for (n = 1; (!(b & 1)) && n < 8; n++, b = b >> 1)
;
if (n == 7)
n = 0;
if (n == 8)
n = 7;
return (n);
}
int dowU2X(d)
int d;
{
if (d == 0)
d = 7;
return (1 << (d - 1));
}
void init(cmd_name)
char * cmd_name;
{
char *bits2day();
int get_status();
timeout = TIMEOUT;
if( strcmp(cmd_name, "monitor") == 0 )
{
check4poll(1,2);
}
else
check4poll(0,0);
/* Now check the status of the interface. */
/* if( get_status() < 1)
error("could not get the interface status");
*/
}
/*
* chksum is used to check the CM11A's acknowledgment to the
* data we send it. It's a simple addition of all bytes.
*/
int chksum(buf, size)
unsigned char *buf;
int size;
{
register int i, sum;
for (i = 0, sum = 0; i < size ; i++)
sum += buf[i];
return (sum & 0xFF);
}
char hc2char(code)
unsigned code;
{
register int i;
for (i = 0; i < 16; i++)
if (housetab[i].h_code == code)
return i + 'a';
return ('?');
}
int char2hc(hletter)
int hletter;
{
if (isupper(hletter))
hletter = tolower(hletter);
if (! ('a' <= hletter && hletter <= 'p') )
error("invalid house code");
return (housetab[hletter - 'a'].h_code);
}
/*
* Parse string of comma-separated unit numbers and return bitmap
* (big-endian) of units specified. '*' means "all units".
*/
unsigned int getunits(p)
register char *p;
{
#define DIGBUFN 80 /* maximum line length */
unsigned bitmsk, n, unit, lastunit, range;
char digbuf[DIGBUFN];
bitmsk = 0;
lastunit = 0;
range = 0;
while (*p) {
if (*p == '*') {
bitmsk = 0xFFFF;
break;
}
for (n = 0; n < DIGBUFN && isdigit(*p); n++, p++)
digbuf[n] = *p;
digbuf[n] = '\0';
if ((unit = atoi(digbuf)) < 1 || unit > 16)
error("bad unit code, must be between 1 and 16");
bitmsk |= (0x01 << (unit-1)) ;
if( range == 1)
{
for( n = lastunit; n <= unit; n++)
bitmsk |= (0x01 << (n-1)) ;
}
if (*p)
{
if (*p == '-')
{
lastunit = unit;
range = 1;
p++;
}
else
{
if (*p == ',')
{
lastunit = 0;
range=0;
p++;
}
else
{
error("bad unit separator, use comma or dash please");
}
}
}
}
return (bitmsk);
}
int dimstate(p, level)
register char *p, *level;
{
unsigned levelnum;
unsigned dim;
dim = -1;
if (strcmp(p, "alloff") == 0)
return (0);
if (strcmp(p, "lightson") == 0)
return (1);
if (strcmp(p, "on") == 0)
return (2);
if (strcmp(p, "off") == 0)
return (3);
if (strcmp(p, "dim") == 0)
dim = 4;
if (strcmp(p, "bright") == 0)
dim = 5;
if (strcmp(p, "lightsoff") == 0)
return(6);
if (dim == -1)
error("bad state keyword");
if (sscanf(level, "%d", &levelnum) == 0)
error("dim value must be numeric");
if (levelnum > 23)
error("dim value out of range, must be between 0 and 23");
timeout = DTIMEOUT;
return ((levelnum << 3) | dim);
}
struct nstruct dtab[] =
{
{"monday", 0x01},
{"tuesday", 0x02},
{"wednesday", 0x04},
{"thursday", 0x08},
{"friday", 0x10},
{"saturday", 0x20},
{"sunday", 0x40},
{"everyday", 0x7f},
{"weekdays", 0x1f},
{"weekends", 0x60},
{"", 0x00 }
};
int day2bits(p)
char *p;
{
int n, mask, length;
if (strcasecmp(p,"Today") == 0)
return (Idays);
length = strlen(p);
mask = 0;
for (n = 0; dtab[n].n_name[0] != 0; n++) {
if (strncasecmp(dtab[n].n_name, p, length) == 0) {
if (mask != 0)
error("ambiguous day abbreviation");
mask = dtab[n].n_code;
}
}
if (mask == 0)
error("bad day keyword");
return (mask);
}
#define MODULES 255
#define NAME_LEN 20
#define UNIT_LEN 50
struct x10_mod {
char name[NAME_LEN+1];
char hc;
char un[UNIT_LEN];
} x10_modules[MODULES];
void read_config()
{
char line[256];
char alias[50];
char housecode[50];
char unit[UNIT_LEN];
char *configfile;
char * home;
struct stat file_buf;
static FILE *f = NULL;
int i = 0;
int n;
extern char *getenv();
if (f) return;
configfile = getenv("X10CONFIG");
if (configfile == NULL)
{
home=getenv("HOME");
if( home == NULL )
{
home = ".";
}
(void) strcat(strcpy(line, home), ALIASFILE);
configfile = line;
}
if( stat(configfile,&file_buf) != 0 )
configfile = "/etc/x10.config";
if( verbose )
fprintf(stderr,
"Using the config file %s\n", configfile);
f = fopen(configfile,"r");
if (!f) {
err("couldn't open configfile %s: is $X10CONFIG set?\n See the x10config man page.",
configfile);
exit(1);
}
clearerr(f);
for (;;)
{
line [0] = '\0';
fgets(line, 255, f);
if ((line[0] == '\0') || (line[0] == 0x0a) )
{
if( feof(f) != 0 ) /* end of file */
{
break;
}
else
{
continue; /* blank line */
}
}
if (i >= MODULES)
{
err("out of table space for config", NULL);
break;
}
n = sscanf(line, "%s %s %s", alias, housecode, unit);
if (!n || alias[0] == '#')
continue;
switch(n)
{
case 3:
case 2:
if (strcmp(alias, "TTY") == 0)
{
/* special case */
strcpy(x10_tty, housecode);
break;
}
if (strcmp(alias, "LATITUDE") == 0)
{
/* another special case */
strncpy(latitude, housecode,
sizeof(latitude));
continue;
}
if (strcmp(alias, "LONGITUDE") == 0)
{
/* another special case */
strncpy(longitude, housecode,
sizeof(longitude));
continue;
}
if (strcmp(alias, "HOUSECODE") == 0)
{
/* another special case */
x10_housecode = char2hc(housecode[0]);
continue;
}
housecode[1] = '\0';
if (isupper(housecode[0]))
housecode[0] = tolower(housecode[0]);
if (housecode[0] < 'a' || housecode[0] > 'p') {
err("bad housecode in config, alias %s",
alias);
continue;
}
x10_modules[i].hc = housecode[0];
strncpy(x10_modules[i].un,unit,UNIT_LEN);
strncpy(x10_modules[i].name, alias, NAME_LEN);
++i;
break;
case 1:
if (strcmp(alias, "NEWFORMAT") == 0)
{
/* another special case */
newformat = 1;
continue;
}
else
err("warning: short field in line in config:\n %s",line);
break;
default:
err("warning: too many fields in line in config:\n %s",line);
break;
}
}
x10_modules[i].hc = hc2char(x10_housecode);
strcpy(x10_modules[i].un,"*");
strcpy(x10_modules[i].name, "all");
fclose (f);
}
struct x10_mod *
xmod_lookup(name)
char *name;
{
int i = 0;
struct x10_mod *xm;
xm = x10_modules;
while (i < MODULES) {
if (!xm->name[0])
break;
if (strcmp(name, xm->name) == 0)
return xm;
i++;
xm++;
}
if (!i) {
err("no config at all", NULL);
}
return NULL;
}
char *
xmod_name(hl,unit)
char *unit;
{
int i = 0;
struct x10_mod *xm;
xm = x10_modules;
if (isupper(hl))
hl = tolower(hl);
while (i < MODULES) {
if (!xm->name[0])
break;
if (hl == xm->hc && strcmp(unit,xm->un) == 0)
return xm->name;
i++;
xm++;
}
return NULL;
}
void parse_unit(name,hcp,unitp)
char *name;
int *hcp;
char **unitp;
{
struct x10_mod *x;
int hletter;
if (isalpha(name[0]) && (isdigit(name[1]) || name[1] == '*')) {
hletter = name[0];
if (isupper(hletter))
hletter = tolower(hletter);
*unitp = &name[1];
} else {
x = xmod_lookup(name);
if (!x) {
err("bad alias %s",name);
quit();
}
hletter = x->hc;
*unitp = x->un;
}
*hcp = char2hc(hletter);
}
void err(m,s)
char *m, *s;
{
fprintf(stderr, "heyu: ");
fprintf(stderr, m, s);
fprintf(stderr, "\n");
}
#ifdef DEBUG
/* dump config is a debugging tool used to see if the 10_mod link list is ok */
dump_config()
{
int i = 0;
struct x10_mod *xm = x10_modules;
extern char x10_tty[];
while (i < MODULES) {
if (!xm->name[0])
break;
printf("%d: name: %s, housecode is %c, unit %s\n",
i, xm->name, xm->hc, xm->un);
i++;
xm++;
}
if (!i)
err("no config at all", NULL);
if (x10_tty[0])
printf("tty is %s\n", x10_tty);
}
#endif
/*
* The CM11A sends data back to the computer whenever it sees a command
* come in over the AC buss. This should (theoretically) allow the compuer
* to track the status of all modules. Upon startup, this program should
* check for a poll before anything else.
*
* Check for a poll (0x5a) from the CM11A, If we get one within a
* second, we should send 0xc3 to tell it that we are ready to read
* it's output.
*
* If the showdata flag is set, we print. Otherwise we just eat the output.
*/
int check4poll(showdata, timeout)
int showdata;
int timeout;
{
int n, i;
int to_read;
char hc;
char *nl;
int unit;
int macro_report;
char *func;
int funcbits;
static int wasflag = 0;
unsigned char buf[128];
char statstr[80];
extern char *funcmap[];
extern char *b2s();
time_t timestore;
struct tm *tp;
off_t f_offset;
unit = -1;
hc = '\0';
func = "" ;
if( showdata )
{
timestore = 0;
time(×tore);
tp = localtime(×tore);
if( newformat == 1 )
{
sprintf(statstr, "%d/%d at %02d:%02d:%02d ",
tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
}
else
{
sprintf(statstr, "%s at %02d:%02d:%02d ",
wdays[tp->tm_wday], tp->tm_hour, tp->tm_min, tp->tm_sec);
}
}
if( timeout == 0 ) /* only do a read if there is data */
{
if( sptty < 0 )
return(0);
f_offset = lseek(sptty, 0, SEEK_CUR);
if( f_offset == lseek(sptty, 0, SEEK_END) )
return(0);
lseek(sptty, f_offset, SEEK_SET);
timeout = 1;
}
n = xread(sptty, buf, 1, timeout);
if( n != 0 )
{
while( buf[0] == 0xff ) /* CM11A has polling info for me */
{
if( ++wasflag == 3)
{
wasflag = 0;
n = xread(sptty, buf, 1, timeout); /* length of xmit */
if( n != 1 )
return(0);
n = buf[0];
if( showdata )
printf("%s %s ...", statstr,
(n < 127)? "Transmitted data" : "Response to transmision");
if( n > 127)
n -= 127;
n = xread(sptty, buf, n, timeout); /* read entire transmision */
for( i = 0; i < n; i++)
printf( "%02x%s", buf[i], i == (n-1) ? "\n" : " ");
return(0);
}
else
n = xread(sptty, buf, 1, timeout);
if( n != 1 )
return(0);
}
if( (buf[0] != 0xff) && (wasflag > 0))
{
n = buf[0];
for(i = 0; i < wasflag;i++)
buf[i] = 0xff;
buf[i] = n;
wasflag = 0;
}
macro_report = 0;
if( buf[0] == 0x5a ) /* CM11A has polling info for me */
{
/* The xread is executed a second time on failure because the
dim commands may be tieing up the CM11.
*/
n = xread(sptty, buf, 1, 2); /* get the buffer size */
if ( n == 0)
n = xread(sptty, buf, 1, 5); /* get the buffer size */
/* Greater than 0 means we have a byte count */
if ( n > 0 )
{
to_read = buf[0]; /* number of bytes to read */
if( to_read == 0x5a)
{
timeout = 2;
return(check4poll(showdata, timeout));
}
else if (to_read == 0x5b)
{
to_read = 2;
macro_report = 1;
}
if( to_read > sizeof(buf) )
{
if( verbose )
fprintf(stderr, "Polling read size exceeds buffer");
return(-1);
}
n = xread(sptty, buf, to_read , timeout);
if ( n != to_read )
{
fprintf( stderr, "Poll only got %d of %d bytes\n",
n, to_read);
return(0);
}
if( showdata > 0 && (verbose != 0) )
fprintf( stderr, "I received a poll %d bytes long.\n", n);
/* DEBUG
fprintf( stderr, "byte 0 = %0x (%s)\n", buf[0], b2s( buf[0]) );
DEBUG */
if( macro_report == 1 ) /* CM11A is reporting a macro execution.*/
{
if( showdata )
{
if( newformat == 1 )
fprintf( stderr, "%s Triggered macro : macro executed at eeprom address %d\n",
statstr, ((buf[0] & 0x07) << 8) + buf[1] );
else
fprintf( stderr, "%s executed macro : macro executed at eeprom address %d\n",
statstr, ((buf[0] & 0x07) << 8) + buf[1] );
}
}
else
{
for ( i = 1; i < to_read; i++)
{
/* DEBUG
fprintf( stderr, "byte %d = %0x\n", i, buf[i]);
DEBUG */
if( strcmp( func, "Dim") == 0)
{
if( showdata )
fprintf( stderr, "dimmed by %%%02.0f\n",
(float)buf[i]/(float)210 * 100);
func = "";
}
else if( strcmp( func, "Bright") == 0 )
{
if( showdata )
fprintf( stderr, "brightened by %%%02.0f\n",
(float)buf[i]/(float)210 * 100 );
func = "";
}
else if( (buf[0] & (0x01 << (i-1))) != 0) /* a function */
{
hc = hc2char( ((buf[i] & 0xF0) >> 4) );
funcbits = buf[i] & 0x0F;
func = funcmap[ buf[i] & 0x0F];
/* continue the line if function is dim or bright */
if( funcbits == DIM || funcbits == BRIGHT )
nl = ", ";
else
nl = "\n";
if( showdata )
fprintf( stderr, "%s function %6s : housecode %c%s",
statstr, func, hc, nl);
}
else /* an address */
{
hc = hc2char( ((buf[i] & 0xF0) >> 4) );
unit = unit2int( buf[i] & 0x0F) ;
if( showdata )
fprintf( stderr, "%s address unit %3d : housecode %c\n",
statstr, unit, hc);
}
}
}
}
else
{
if (verbose)
sprintf( statstr, "Bytes received = %d,", n);
fprintf( stderr,
"%s The interface didn't answer a getinfo response.\n",
statstr);
}
}
else if( (buf[0] == 0xa5) )
{ /* CM11A is asking for time update */
fprintf(stderr, "Module experienced a power failure. %s",
"It requested a time update.\n");
}
else if( buf[0] == 0x5b)
{
to_read = 2;
n = xread(sptty, buf, to_read, timeout);
if(showdata)
{
/* format changed with version 1.28c */
if( newformat == 1)
fprintf(stderr, "%s Timer executed : macro at eeprom address %d\n",
statstr, (((buf[0] & 0x07)<<8) + buf[1]));
else
fprintf(stderr, "%s executed macro at EEPROM address %d\n",
statstr, (((buf[0] & 0x07)<<8) + buf[1]));
/* TEST CODE
fprintf(stderr, "( EEPROM byte 0 = %d, Byte 1 = %d)\n",
(buf[0] & 0x07), (buf[1]));
*/
}
}
else
fprintf( stderr,
"%s Poll received unknown value (%d bytes), leading byte = %0x\n",
statstr, n, buf[0]);
}
return(0);
}
/* Takes the bits in CM11A format (as used in the reply to a status)
* and returns a day string.
* The b parameter is for bitmap */
char *bits2day(b)
int b;
{
switch(b)
{
case 1:
return("Sun");
case 2:
return("Mon");
case 4:
return("Tue");
case 8:
return("Wed");
case 16:
return("Thu");
case 32:
return("Fri");
case 64:
return("Sat");
}
return("Error");
}
int c_version()
{
printf( "Version:%4s\n", VERSION );
return(0);
}
int c_help()
{
usage(NULL);
return(0);
}
void display(RCCS)
char *RCCS;
{
/* This is not used much so far */
if( verbose > 1)
printf( "%s\n", RCCS);
}
/* take the simple bitmap produced by getunits() and turn it into the
* cm11 style, where bit 0 represents unit 13, bit 1 represents unit 5, etc
*/
unsigned int cm11bitmap(bits)
unsigned int bits;
{
unsigned int cm11bits;
int x;
cm11bits=0;
for(x=0; x < 16; x++)
{
if( ( bits & (1 << x ) ) != 0 )
cm11bits |= (1 << (cm11map[x]) );
}
return(cm11bits);
}
|