thermferm/server.c

changeset 325
35b6f6605ae3
parent 313
8448fcf3d799
child 329
517e1719006a
equal deleted inserted replaced
324:940a668e568f 325:35b6f6605ae3
321 } 321 }
322 322
323 if (strcmp(opt, (char *)"GET") == 0) { 323 if (strcmp(opt, (char *)"GET") == 0) {
324 for (device = Config.devices; device; device = device->next) { 324 for (device = Config.devices; device; device = device->next) {
325 if (strcmp(device->uuid, param) == 0) { 325 if (strcmp(device->uuid, param) == 0) {
326 #ifdef HAVE_WIRINGPI_H
327 piLock(LOCK_DEVICES);
328 #endif
329 int my_value = device->value;
330 int my_timestamp = (int)device->timestamp;
331 #ifdef HAVE_WIRINGPI_H
332 piUnlock(LOCK_DEVICES);
333 #endif
326 srv_send((char *)"213 Device record follows:"); 334 srv_send((char *)"213 Device record follows:");
327 srv_send((char *)"TYPE,%s", DEVTYPE[device->type]); 335 srv_send((char *)"TYPE,%s", DEVTYPE[device->type]);
328 srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]); 336 srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]);
329 srv_send((char *)"VALUE,%d", device->value); 337 srv_send((char *)"VALUE,%d", my_value);
330 srv_send((char *)"OFFSET,%d", device->offset); 338 srv_send((char *)"OFFSET,%d", device->offset);
331 srv_send((char *)"PRESENT,%s", DEVPRESENT[device->present]); 339 srv_send((char *)"PRESENT,%s", DEVPRESENT[device->present]);
332 srv_send((char *)"ADDRESS,%s", device->address); 340 srv_send((char *)"ADDRESS,%s", device->address);
333 srv_send((char *)"SUBDEVICE,%d", device->subdevice); 341 srv_send((char *)"SUBDEVICE,%d", device->subdevice);
334 srv_send((char *)"GPIOPIN,%d", device->gpiopin); 342 srv_send((char *)"GPIOPIN,%d", device->gpiopin);
335 srv_send((char *)"DESCRIPTION,%s", device->description); 343 srv_send((char *)"DESCRIPTION,%s", device->description);
336 srv_send((char *)"INUSE,%d", device->inuse); 344 srv_send((char *)"INUSE,%d", device->inuse);
337 srv_send((char *)"COMMENT,%s", device->comment); 345 srv_send((char *)"COMMENT,%s", device->comment);
338 srv_send((char *)"TIMESTAMP,%d", (int)device->timestamp); 346 srv_send((char *)"TIMESTAMP,%d", my_timestamp);
339 srv_send((char *)"."); 347 srv_send((char *)".");
340 return 1; 348 return 1;
341 } 349 }
342 } 350 }
343 srv_send((char *)"440 No such device"); 351 srv_send((char *)"440 No such device");
381 val = strtok(NULL, "\0"); 389 val = strtok(NULL, "\0");
382 if (kwd && val) { 390 if (kwd && val) {
383 if (strcmp(kwd, (char *)"TYPE") == 0) { 391 if (strcmp(kwd, (char *)"TYPE") == 0) {
384 for (i = 0; i < 8; i++) { 392 for (i = 0; i < 8; i++) {
385 if (strcmp(val, DEVTYPE[i]) == 0) { 393 if (strcmp(val, DEVTYPE[i]) == 0) {
394 #ifdef HAVE_WIRINGPI_H
395 piLock(LOCK_DEVICES);
396 #endif
386 if (device->type != i) 397 if (device->type != i)
387 syslog(LOG_NOTICE, "Device %s changed type %s to %s", device->uuid, DEVTYPE[device->type], DEVTYPE[i]); 398 syslog(LOG_NOTICE, "Device %s changed type %s to %s", device->uuid, DEVTYPE[device->type], DEVTYPE[i]);
388 device->type = i; 399 device->type = i;
400 #ifdef HAVE_WIRINGPI_H
401 piUnlock(LOCK_DEVICES);
402 #endif
389 break; 403 break;
390 } 404 }
391 } 405 }
392 406
393 } else if (strcmp(kwd, (char *)"DIRECTION") == 0) { 407 } else if (strcmp(kwd, (char *)"DIRECTION") == 0) {
394 for (i = 0; i < 7; i++) { 408 for (i = 0; i < 7; i++) {
395 if (strcmp(val, DEVDIR[i]) == 0) { 409 if (strcmp(val, DEVDIR[i]) == 0) {
410 #ifdef HAVE_WIRINGPI_H
411 piLock(LOCK_DEVICES);
412 #endif
396 if (device->direction != i) 413 if (device->direction != i)
397 syslog(LOG_NOTICE, "Device %s changed direction %s to %s", device->uuid, DEVDIR[device->type], DEVDIR[i]); 414 syslog(LOG_NOTICE, "Device %s changed direction %s to %s", device->uuid, DEVDIR[device->type], DEVDIR[i]);
398 device->direction = i; 415 device->direction = i;
416 #ifdef HAVE_WIRINGPI_H
417 piUnlock(LOCK_DEVICES);
418 #endif
399 break; 419 break;
400 } 420 }
401 } 421 }
402 422
403 } else if (strcmp(kwd, (char *)"VALUE") == 0) { 423 } else if (strcmp(kwd, (char *)"VALUE") == 0) {
404 if (sscanf(val, "%d", &ival) == 1) { 424 if (sscanf(val, "%d", &ival) == 1) {
405 if (device->value != ival) 425 #ifdef HAVE_WIRINGPI_H
426 piLock(LOCK_DEVICES);
427 #endif
428 if (device->value != ival)
406 syslog(LOG_NOTICE, "Device %s changed value %d to %d", device->uuid, device->value, ival); 429 syslog(LOG_NOTICE, "Device %s changed value %d to %d", device->uuid, device->value, ival);
407 device->value = ival; 430 device->value = ival;
431 #ifdef HAVE_WIRINGPI_H
432 piUnlock(LOCK_DEVICES);
433 #endif
408 } 434 }
409 435
410 } else if (strcmp(kwd, (char *)"OFFSET") == 0) { 436 } else if (strcmp(kwd, (char *)"OFFSET") == 0) {
411 if (sscanf(val, "%d", &ival) == 1) { 437 if (sscanf(val, "%d", &ival) == 1) {
412 if (device->offset != ival) 438 #ifdef HAVE_WIRINGPI_H
439 piLock(LOCK_DEVICES);
440 #endif
441 if (device->offset != ival)
413 syslog(LOG_NOTICE, "Device %s changed offset %d to %d", device->uuid, device->offset, ival); 442 syslog(LOG_NOTICE, "Device %s changed offset %d to %d", device->uuid, device->offset, ival);
414 device->offset = ival; 443 device->offset = ival;
444 #ifdef HAVE_WIRINGPI_H
445 piUnlock(LOCK_DEVICES);
446 #endif
415 } 447 }
416 448
417 } else if (strcmp(kwd, (char *)"PRESENT") == 0) { 449 } else if (strcmp(kwd, (char *)"PRESENT") == 0) {
418 for (i = 0; i < 4; i++) { 450 for (i = 0; i < 4; i++) {
419 if (strcmp(val, DEVPRESENT[i]) == 0) { 451 if (strcmp(val, DEVPRESENT[i]) == 0) {
452 #ifdef HAVE_WIRINGPI_H
453 piLock(LOCK_DEVICES);
454 #endif
420 if (device->present != i) 455 if (device->present != i)
421 syslog(LOG_NOTICE, "Device %s changed present %s to %s", device->uuid, DEVPRESENT[device->present], DEVPRESENT[i]); 456 syslog(LOG_NOTICE, "Device %s changed present %s to %s", device->uuid, DEVPRESENT[device->present], DEVPRESENT[i]);
422 device->present = i; 457 device->present = i;
423 break; 458 #ifdef HAVE_WIRINGPI_H
459 piUnlock(LOCK_DEVICES);
460 #endif
461 break;
424 } 462 }
425 } 463 }
426 464
427 } else if (strcmp(kwd, (char *)"ADDRESS") == 0) { 465 } else if (strcmp(kwd, (char *)"ADDRESS") == 0) {
466 #ifdef HAVE_WIRINGPI_H
467 piLock(LOCK_DEVICES);
468 #endif
428 if (device->address) { 469 if (device->address) {
429 if (strcmp(device->address, val)) 470 if (strcmp(device->address, val))
430 syslog(LOG_NOTICE, "Device %s changed address `%s' to `%s'", device->uuid, device->address, val); 471 syslog(LOG_NOTICE, "Device %s changed address `%s' to `%s'", device->uuid, device->address, val);
431 free(device->address); 472 free(device->address);
432 } 473 }
433 device->address = xstrcpy(val); 474 device->address = xstrcpy(val);
475 #ifdef HAVE_WIRINGPI_H
476 piUnlock(LOCK_DEVICES);
477 #endif
434 478
435 } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) { 479 } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) {
436 if (sscanf(val, "%d", &ival) == 1) { 480 if (sscanf(val, "%d", &ival) == 1) {
481 #ifdef HAVE_WIRINGPI_H
482 piLock(LOCK_DEVICES);
483 #endif
437 if (device->subdevice != ival) 484 if (device->subdevice != ival)
438 syslog(LOG_NOTICE, "Device %s changed subdevice %d to %d", device->uuid, device->subdevice, ival); 485 syslog(LOG_NOTICE, "Device %s changed subdevice %d to %d", device->uuid, device->subdevice, ival);
439 device->subdevice = ival; 486 device->subdevice = ival;
487 #ifdef HAVE_WIRINGPI_H
488 piUnlock(LOCK_DEVICES);
489 #endif
440 } 490 }
441 491
442 } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) { 492 } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) {
443 if (sscanf(val, "%d", &ival) == 1) { 493 if (sscanf(val, "%d", &ival) == 1) {
494 #ifdef HAVE_WIRINGPI_H
495 piLock(LOCK_DEVICES);
496 #endif
444 if (device->gpiopin != ival) 497 if (device->gpiopin != ival)
445 syslog(LOG_NOTICE, "Device %s changed gpiopin %d to %d", device->uuid, device->gpiopin, ival); 498 syslog(LOG_NOTICE, "Device %s changed gpiopin %d to %d", device->uuid, device->gpiopin, ival);
446 device->gpiopin = ival; 499 device->gpiopin = ival;
500 #ifdef HAVE_WIRINGPI_H
501 piUnlock(LOCK_DEVICES);
502 #endif
447 } 503 }
448 504
449 } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) { 505 } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) {
506 #ifdef HAVE_WIRINGPI_H
507 piLock(LOCK_DEVICES);
508 #endif
450 if (device->description) { 509 if (device->description) {
451 if (strcmp(device->description, val)) 510 if (strcmp(device->description, val))
452 syslog(LOG_NOTICE, "Device %s changed description `%s' to `%s'", device->uuid, device->description, val); 511 syslog(LOG_NOTICE, "Device %s changed description `%s' to `%s'", device->uuid, device->description, val);
453 free(device->description); 512 free(device->description);
454 } 513 }
455 device->description = xstrcpy(val); 514 device->description = xstrcpy(val);
515 #ifdef HAVE_WIRINGPI_H
516 piUnlock(LOCK_DEVICES);
517 #endif
456 518
457 } else if (strcmp(kwd, (char *)"COMMENT") == 0) { 519 } else if (strcmp(kwd, (char *)"COMMENT") == 0) {
520 #ifdef HAVE_WIRINGPI_H
521 piLock(LOCK_DEVICES);
522 #endif
458 if (device->comment) { 523 if (device->comment) {
459 if (strcmp(device->comment, val)) 524 if (strcmp(device->comment, val))
460 syslog(LOG_NOTICE, "Device %s changed comment `%s' to `%s'", device->uuid, device->comment, val); 525 syslog(LOG_NOTICE, "Device %s changed comment `%s' to `%s'", device->uuid, device->comment, val);
461 free(device->comment); 526 free(device->comment);
462 } 527 }
463 device->comment = xstrcpy(val); 528 device->comment = xstrcpy(val);
529 #ifdef HAVE_WIRINGPI_H
530 piUnlock(LOCK_DEVICES);
531 #endif
464 532
465 } 533 }
466 } 534 }
467 } 535 }
468 } 536 }

mercurial