thermferm/server.c

changeset 174
244de612c572
parent 170
3cb99272b84b
child 175
b73490398368
equal deleted inserted replaced
173:7259ee8778e9 174:244de612c572
36 #endif 36 #endif
37 extern sys_config Config; 37 extern sys_config Config;
38 extern const char UNITMODE[5][8]; 38 extern const char UNITMODE[5][8];
39 extern const char UNITmode[5]; 39 extern const char UNITmode[5];
40 extern const char TEMPSTATE[3][8]; 40 extern const char TEMPSTATE[3][8];
41 extern const char DEVTYPE[7][6];
42 extern const char DEVPRESENT[4][6];
43 extern const char DEVDIR[7][11];
44
41 45
42 int s; /* connected socket */ 46 int s; /* connected socket */
43 int ls; /* listen socket */ 47 int ls; /* listen socket */
44 48
45 struct sockaddr_in myaddr_in; /* for local socket address */ 49 struct sockaddr_in myaddr_in; /* for local socket address */
275 return 1; 279 return 1;
276 } 280 }
277 281
278 282
279 283
284 int delete_Device(char *uuid)
285 {
286 devices_list *current = Config.devices;
287 devices_list *previous = NULL;
288
289 while (current) {
290 if (strcmp(current->uuid, uuid) == 0) {
291 if (previous == NULL) {
292 Config.devices = current->next;
293 free(current->uuid);
294 current->uuid = NULL;
295 free(current->address);
296 current->address = NULL;
297 free(current->description);
298 current->description = NULL;
299 free(current->comment);
300 current->comment = NULL;
301 free(current);
302 return 1;
303 } else {
304 free(current->uuid);
305 current->uuid = NULL;
306 free(current->address);
307 current->address = NULL;
308 free(current->description);
309 current->description = NULL;
310 free(current->comment);
311 current->comment = NULL;
312 previous->next = current->next;
313 free(current);
314 current = previous->next;
315 return 1;
316 }
317 } else {
318 previous = current;
319 current = current->next;
320 }
321 }
322 return 0;
323 }
324
325
326
280 /* 327 /*
281 * GET AIR uuid 328 * DEVICE ADD type
282 * GET BEER uuid 329 * DEVICE DEL uuid
283 * GET LED1 uuid 330 * DEVICE LIST
284 * GET LED2 uuid 331 * DEVICE GET uuid
285 * GET LED3 uuid 332 * DEVICE PUT uuid
286 * GET MODE uuid
287 * GET TARGET uuid
288 */ 333 */
289 int cmd_get(char *buf) 334 int cmd_device(char *buf)
290 { 335 {
291 char *opt, *uuid; 336 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
292 units_list *unit; 337 devices_list *device, *tmpd;
338 socklen_t fromlen;
339 int i, rlen, ival;
340 uuid_t uu;
293 341
294 opt = strtok(buf, " \0"); 342 opt = strtok(buf, " \0");
295 opt = strtok(NULL, " \0"); 343 opt = strtok(NULL, " \0");
296 uuid = strtok(NULL, "\0"); 344
297 345 if (opt == NULL) {
298 if (uuid == NULL) {
299 srv_send((char *)"502 Missing command option"); 346 srv_send((char *)"502 Missing command option");
300 return 1; 347 return 1;
301 } 348 }
302 349 param = strtok(NULL, "\0");
303 for (unit = Config.units; unit; unit = unit->next) { 350
304 if (strcmp(uuid, unit->uuid) == 0) { 351 if (strcmp(opt, (char *)"LIST") == 0) {
305 352 srv_send((char *)"212 bus devices:");
306 if (strcmp(opt, (char *)"AIR") == 0) { 353 for (device = Config.devices; device; device = device->next) {
307 if (unit->air_state == 0) 354 srv_send((char *)"%s,%s,%d,%d,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment);
308 srv_send((char *)"215 AIR,%.3f", unit->air_temperature / 1000.0); 355 }
309 else 356 srv_send((char *)".");
310 srv_send((char *)"215 AIR,NA"); 357 return 0;
311 return 0; 358 }
312 359
313 } else if (strcmp(opt, (char *)"BEER") == 0) { 360 if (param == NULL) {
314 if (unit->beer_state == 0) 361 srv_send((char *)"502 Missing command parameter");
315 srv_send((char *)"215 BEER,%.3f", unit->beer_temperature / 1000.0); 362 return 1;
316 else 363 }
317 srv_send((char *)"215 BEER,NA"); 364
318 return 0; 365 if (strcmp(opt, (char *)"ADD") == 0) {
319 366 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) ||
320 } else if (strcmp(opt, (char *)"LED1") == 0) { 367 (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) {
321 srv_send((char *)"215 HEATER,%d", (unit->heater_available && unit->heater_state) ? 1:0); 368
322 return 0; 369 device = (devices_list *)malloc(sizeof(devices_list));
323 370 device->next = NULL;
324 } else if (strcmp(opt, (char *)"LED2") == 0) { 371 device->version = 1;
325 srv_send((char *)"215 COOLER,%d", (unit->cooler_available && unit->cooler_state) ? 1:0); 372 device->uuid = malloc(37);
326 return 0; 373 uuid_generate(uu);
327 374 uuid_unparse(uu, device->uuid);
328 } else if (strcmp(opt, (char *)"LED3") == 0) { 375 for (i = 0; i < 7; i++) {
329 srv_send((char *)"215 FAN,%d", (unit->fan_available && unit->fan_state) ? 1:0); 376 if (strcmp(param, DEVTYPE[i]) == 0) {
330 return 0; 377 device->type = i;
331 378 break;
332 } else if (strcmp(opt, (char *)"MODE") == 0) { 379 }
333 srv_send((char *)"215 MODE,%s", UNITMODE[unit->mode]); 380 }
334 return 0; 381 device->direction = DEVDIR_UNDEF;
335 382 device->value = device->subdevice = device->inuse = 0;
336 } else if (strcmp(opt, (char *)"TARGET") == 0) { 383 device->present = DEVPRESENT_UNDEF;
337 if (unit->mode == UNITMODE_BEER) { 384 device->address = xstrcpy((char *)"Enter address here");
338 srv_send((char *)"215 TARGET,%.3f", unit->beer_set); 385 device->gpiopin = -1;
339 } else if (unit->mode == UNITMODE_FRIDGE) { 386 device->description = xstrcpy((char *)"Describe me here");
340 srv_send((char *)"215 TARGET,%.3f", unit->fridge_set); 387 device->comment = xstrcpy((char *)"Comment here");
341 } else if (unit->mode == UNITMODE_PROFILE) { 388
342 srv_send((char *)"215 TARGET,%.3f", unit->prof_target); 389 if (Config.devices == NULL) {
343 } else { 390 Config.devices = device;
344 srv_send((char *)"215 TARGET,NA"); 391 } else {
345 } 392 for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) {
346 return 0; 393 if (tmpd->next == NULL) {
347 } 394 tmpd->next = device;
348 395 break;
349 srv_send((char *)"502 Unknown command option"); 396 }
397 }
398 }
399 syslog(LOG_NOTICE, "Device with uuid %s added", device->uuid);
400 srv_send((char *)"211 Device with uuid %s added", device->uuid);
401 return 0;
402
403 } else {
404 srv_send((char *)"506 Wrong device parameter");
350 return 1; 405 return 1;
351 } 406 }
352 } 407 }
353 408
354 srv_send((char *)"440 No such profile"); 409 if (strcmp(opt, (char *)"DEL") == 0) {
410
411 if (delete_Device(param)) {
412 syslog(LOG_NOTICE, "Deleted device with %s", param);
413 srv_send((char *)"211 Device %s deleted", param);
414 return 0;
415 } else {
416 srv_send((char *)"440 Delete Device: No such device %s", param);
417 return 1;
418 }
419 }
420
421 if (strcmp(opt, (char *)"GET") == 0) {
422 for (device = Config.devices; device; device = device->next) {
423 if (strcmp(device->uuid, param) == 0) {
424 srv_send((char *)"213 Device %s record follows:", device->uuid);
425 srv_send((char *)"TYPE,%s", DEVTYPE[device->type]);
426 srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]);
427 srv_send((char *)"VALUE,%d", device->value);
428 srv_send((char *)"PRESENT,%s", DEVPRESENT[device->present]);
429 srv_send((char *)"ADDRESS,%s", device->address);
430 srv_send((char *)"SUBDEVICE,%d", device->subdevice);
431 srv_send((char *)"GPIOPIN,%d", device->gpiopin);
432 srv_send((char *)"DESCRIPTION,%s", device->description);
433 srv_send((char *)"INUSE,%d", device->inuse);
434 srv_send((char *)"COMMENT,%s", device->comment);
435 srv_send((char *)"TIMESTAMP,%d", (int)device->timestamp);
436 srv_send((char *)".");
437 return 1;
438 }
439 }
440 srv_send((char *)"440 No such device");
441 return 1;
442 }
443
444 if (strcmp(opt, (char *)"PUT") == 0) {
445 for (device = Config.devices; device; device = device->next) {
446 if (strcmp(device->uuid, param) == 0) {
447 while (1) {
448 memset((char *)&ibuf, 0, SS_BUFSIZE);
449 fromlen = sizeof(peeraddr_in);
450 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
451 if (rlen == -1) {
452 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
453 srv_send((char *)"518 recfrom(): %s", strerror(errno));
454 return 1;
455 }
456 for (i = 0; i < strlen(ibuf); i++) {
457 if (ibuf[i] == '\n')
458 ibuf[i] = '\0';
459 if (ibuf[i] == '\r')
460 ibuf[i] = '\0';
461 }
462 for (i = strlen(ibuf) -1; i > 0; i--) {
463 if (ibuf[i] == ' ')
464 ibuf[i] = '\0';
465 else
466 break;
467 }
468 if (strlen(ibuf)) {
469 if (debug) {
470 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
471 fprintf(stdout, "recv: \"%s\"\n", ibuf);
472 }
473 if (strcmp(ibuf, (char *)".") == 0) {
474 srv_send((char *)"219 Accepted Device record");
475 return 0;
476 }
477 kwd = strtok(ibuf, ",\0");
478 val = strtok(NULL, "\0");
479 if (kwd && val) {
480 if (strcmp(kwd, (char *)"TYPE") == 0) {
481 for (i = 0; i < 7; i++) {
482 if (strcmp(val, DEVTYPE[i]) == 0) {
483 device->type = i;
484 break;
485 }
486 }
487
488 } else if (strcmp(kwd, (char *)"DIRECTION") == 0) {
489 for (i = 0; i < 7; i++) {
490 if (strcmp(val, DEVDIR[i]) == 0) {
491 device->direction = i;
492 break;
493 }
494 }
495
496 } else if (strcmp(kwd, (char *)"VALUE") == 0) {
497 if (sscanf(val, "%d", &ival) == 1)
498 device->value = ival;
499
500 } else if (strcmp(kwd, (char *)"PRESENT") == 0) {
501 for (i = 0; i < 4; i++) {
502 if (strcmp(val, DEVPRESENT[i]) == 0)
503 device->present = i;
504 break;
505 }
506
507 } else if (strcmp(kwd, (char *)"ADDRESS") == 0) {
508 if (device->address)
509 free(device->address);
510 device->address = xstrcpy(val);
511
512 } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) {
513 if (sscanf(val, "%d", &ival) == 1)
514 device->subdevice = ival;
515
516 } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) {
517 if (sscanf(val, "%d", &ival) == 1)
518 device->gpiopin = ival;
519
520 } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) {
521 if (device->description)
522 free(device->description);
523 device->description = xstrcpy(val);
524
525 } else if (strcmp(kwd, (char *)"COMMENT") == 0) {
526 if (device->comment)
527 free(device->comment);
528 device->comment = xstrcpy(val);
529
530 }
531 }
532 }
533 }
534 }
535 }
536 srv_send((char *)"440 No such device");
537 return 1;
538 }
539
540 srv_send((char *)"502 Unknown command option");
355 return 1; 541 return 1;
356 } 542 }
357 543
358 544
359 545
360 /* 546 /*
361 * LIST 547 * LIST
362 * LIST BUS
363 * LIST LOG 548 * LIST LOG
364 * LIST PROFILES 549 * LIST PROFILES
365 * LIST UNIT 550 * LIST UNIT
366 */ 551 */
367 int cmd_list(char *buf) 552 int cmd_list(char *buf)
368 { 553 {
369 char *opt, *filename, *p, q[2], buffer[256]; 554 char *opt, *filename, *p, q[2], buffer[256];
370 units_list *unit; 555 units_list *unit;
371 profiles_list *profile; 556 profiles_list *profile;
372 prof_step *step; 557 prof_step *step;
373 devices_list *device;
374 int j; 558 int j;
375 FILE *fp; 559 FILE *fp;
376 560
377 opt = strtok(buf, " \0"); 561 opt = strtok(buf, " \0");
378 opt = strtok(NULL, "\0"); 562 opt = strtok(NULL, "\0");
382 * Default, list available units 566 * Default, list available units
383 */ 567 */
384 srv_send((char *)"212 Fermenter list follows:"); 568 srv_send((char *)"212 Fermenter list follows:");
385 for (unit = Config.units; unit; unit = unit->next) { 569 for (unit = Config.units; unit; unit = unit->next) {
386 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 570 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
387 }
388 srv_send((char *)".");
389 return 0;
390
391 } else if (strcmp(opt, (char *)"BUS") == 0) {
392 /*
393 * Bus devices
394 */
395 srv_send((char *)"212 bus devices:");
396 for (device = Config.devices; device; device = device->next) {
397 srv_send((char *)"%s,%s,%d,%d,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment);
398 } 571 }
399 srv_send((char *)"."); 572 srv_send((char *)".");
400 return 0; 573 return 0;
401 574
402 } else if (strcmp(opt, (char *)"LOG") == 0) { 575 } else if (strcmp(opt, (char *)"LOG") == 0) {
1005 if (unit_add(buf) == 0) 1178 if (unit_add(buf) == 0)
1006 wrconfig(); 1179 wrconfig();
1007 } else if (strncmp(buf, "DEL", 3) == 0) { 1180 } else if (strncmp(buf, "DEL", 3) == 0) {
1008 if (cmd_del(buf) == 0) 1181 if (cmd_del(buf) == 0)
1009 wrconfig(); 1182 wrconfig();
1010 } else if (strncmp(buf, "GET", 3) == 0) { 1183 } else if (strncmp(buf, "DEVICE", 6) == 0) {
1011 cmd_get(buf); 1184 if (cmd_device(buf) == 0)
1185 wrconfig();
1012 } else if (strncmp(buf, "HELP", 4) == 0) { 1186 } else if (strncmp(buf, "HELP", 4) == 0) {
1013 srv_send((char *)"100 Help text follows"); 1187 srv_send((char *)"100 Help text follows");
1014 srv_send((char *)"Recognized commands:"); 1188 srv_send((char *)"Recognized commands:");
1015 srv_send((char *)""); 1189 srv_send((char *)"");
1016 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 1190 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
1017 srv_send((char *)"ADD PROFILE name Add a new profile with \"name\""); 1191 srv_send((char *)"ADD PROFILE name Add a new profile with \"name\"");
1018 srv_send((char *)"ADD UNIT name Add a new unit with \"name\""); 1192 srv_send((char *)"ADD UNIT name Add a new unit with \"name\"");
1019 srv_send((char *)"DEL PROFILE uuid Delete profile with uuid"); 1193 srv_send((char *)"DEL PROFILE uuid Delete profile with uuid");
1020 // srv_send((char *)"DEL UNIT uuid Delete unit with uuid"); 1194 // srv_send((char *)"DEL UNIT uuid Delete unit with uuid");
1021 srv_send((char *)"GET AIR uuid Get Air temperature with uuid"); 1195 srv_send((char *)"DEVICE ADD type Add new Device type");
1022 srv_send((char *)"GET BEER uuid Get Beer temperature with uuid"); 1196 // srv_send((char *)"DEVICE DEL uuid Delete Device by uuid");
1023 srv_send((char *)"GET LED1 uuid Get LED 1 (cooler) state"); 1197 srv_send((char *)"DEVICE LIST List Devices");
1024 srv_send((char *)"GET LED2 uuid Get LED 2 (heater) state"); 1198 srv_send((char *)"DEVICE GET uuid Get Device record by uuid");
1025 srv_send((char *)"GET LED3 uuid Get LED 3 (fan) state"); 1199 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid");
1026 srv_send((char *)"GET MODE uuid Get Unit Mode");
1027 // srv_send((char *)"GET SW1 uuid Get Switch 1 (cooler) state");
1028 // srv_send((char *)"GET SW2 uuid Get Switch 2 (heater) state");
1029 // srv_send((char *)"GET SW3 uuid Get Switch 3 (fan) state");
1030 srv_send((char *)"GET TARGET uuid Get Target temperature with uuid");
1031 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); 1200 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)");
1032 srv_send((char *)"LIST List all fermenter units"); 1201 srv_send((char *)"LIST List all fermenter units");
1033 srv_send((char *)"LIST BUS List 1-wire bus");
1034 srv_send((char *)"LIST LOG List logfile data in 1 hour lines"); 1202 srv_send((char *)"LIST LOG List logfile data in 1 hour lines");
1035 srv_send((char *)"LIST PROFILES List available profiles"); 1203 srv_send((char *)"LIST PROFILES List available profiles");
1036 srv_send((char *)"LIST UNIT List fermenter unit"); 1204 srv_send((char *)"LIST UNIT List fermenter unit");
1037 srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE"); 1205 srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE");
1038 // srv_send((char *)"PROFILE Profile status of current unit"); 1206 // srv_send((char *)"PROFILE Profile status of current unit");

mercurial