Subversion Repositories heizung

Rev

Rev 9 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9 Rev 10
Line 78... Line 78...
78
int readHeizPlanName(char *fname);
78
int readHeizPlanName(char *fname);
79
int writeHeizPlan(void);
79
int writeHeizPlan(void);
80
int writeHeizPlanName(char *fname);
80
int writeHeizPlanName(char *fname);
81
void readConf(void);
81
void readConf(void);
82
 
82
 
-
 
83
HEIZINDEX *allocMem(HEIZINDEX *first);
-
 
84
int sortTable(void);
-
 
85
 
83
char *readLine(int fd, char *buf, int bufLen);
86
char *readLine(int fd, char *buf, int bufLen);
84
char *trim(char *str);
87
char *trim(char *str);
85
char *remove_string(char *str, char *search, char *ret);
88
char *remove_string(char *str, char *search, char *ret);
86
 
89
 
87
static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
90
//static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
88
static pthread_mutex_t fastmutex_ser = PTHREAD_MUTEX_INITIALIZER;
91
static pthread_mutex_t fastmutex_ser = PTHREAD_MUTEX_INITIALIZER;
89
static pthread_mutex_t fastmutex_proc = PTHREAD_MUTEX_INITIALIZER;
92
static pthread_mutex_t fastmutex_proc = PTHREAD_MUTEX_INITIALIZER;
90
 
93
 
91
/*
94
/*
92
 * The main program, initializing everything and start the daemon.
95
 * The main program, initializing everything and start the daemon.
Line 130... Line 133...
130
 * Detach application from console and make it a daemon.
133
 * Detach application from console and make it a daemon.
131
 */
134
 */
132
void daemon_start (int ignsigcld)
135
void daemon_start (int ignsigcld)
133
{
136
{
134
int childpid, fd;
137
int childpid, fd;
-
 
138
char hv0[64];
135
 
139
 
136
        if (getpid () == 1)
140
        if (getpid () == 1)
137
           goto out;
141
           goto out;
138
 
142
 
139
#ifdef SIGTTOU
143
#ifdef SIGTTOU
Line 176... Line 180...
176
//           signal (SIGCLD, sig_child);
180
//           signal (SIGCLD, sig_child);
177
//#else
181
//#else
178
           signal (SIGCLD, SIG_IGN);
182
           signal (SIGCLD, SIG_IGN);
179
//#endif
183
//#endif
180
        }
184
        }
-
 
185
 
-
 
186
	// Create PID file
-
 
187
	if ((fd = open(configs.Pidfile, O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
-
 
188
	{
-
 
189
	   syslog(LOG_WARNING,"Can't create PID file %s: %s",configs.Pidfile, strerror(errno));
-
 
190
	   return;
-
 
191
	}
-
 
192
	
-
 
193
	sprintf(&hv0[0],"%d", getpid());
-
 
194
	write(fd, hv0, strlen(hv0));
-
 
195
	close(fd);
181
}
196
}
182
 
197
 
183
void sig_child ()
198
void sig_child ()
184
{
199
{
185
#if defined(BSD) && !defined(sinix) && !defined(Linux)
200
#if defined(BSD) && !defined(sinix) && !defined(Linux)
Line 424... Line 439...
424
	length = sizeof(client1);
439
	length = sizeof(client1);
425
        syslog (LOG_DEBUG, "Server ready: %d",s);
440
        syslog (LOG_DEBUG, "Server ready: %d",s);
426
 
441
 
427
	while (1)
442
	while (1)
428
	{
443
	{
429
	   pthread_mutex_lock (&fastmutex);
444
//	   pthread_mutex_lock (&fastmutex);
430
	   // Read time table at every loop, to be sure to have the latest
445
	   // Read time table at every loop, to be sure to have the latest
431
	   // version (User may change it at every time)
446
	   // version (User may change it at every time)
432
	   if (!readHeizPlan())
447
	   if (!readHeizPlan())
433
	   {
448
	   {
434
	      if (!HeizFirst)
449
	      if (!HeizFirst)
Line 438... Line 453...
438
	      }
453
	      }
439
	      else
454
	      else
440
		 syslog(LOG_DAEMON, "Error reading table with plan! Will work with old one.");
455
		 syslog(LOG_DAEMON, "Error reading table with plan! Will work with old one.");
441
	   }
456
	   }
442
 
457
 
443
	   pthread_mutex_unlock(&fastmutex);
458
//	   pthread_mutex_unlock(&fastmutex);
444
 
459
 
445
	   if ((s1 = accept (s, (struct sockaddr *)&client1, &length)) < 0)
460
	   if ((s1 = accept (s, (struct sockaddr *)&client1, &length)) < 0)
446
	   {
461
	   {
447
	      syslog (LOG_DAEMON, "Error in accept: %d: %s", s1, strerror(errno));
462
	      syslog (LOG_DAEMON, "Error in accept: %d: %s", s1, strerror(errno));
448
	      continue;
463
	      continue;
Line 979... Line 994...
979
	strcpy(configs.User,"nobody");
994
	strcpy(configs.User,"nobody");
980
	strcpy(configs.Grp,"nobody");
995
	strcpy(configs.Grp,"nobody");
981
	strcpy(configs.HeizPath, "/var/www/.HeizPlan.conf");
996
	strcpy(configs.HeizPath, "/var/www/.HeizPlan.conf");
982
	strcpy(configs.Werte, "/var/log/werte.dat");
997
	strcpy(configs.Werte, "/var/log/werte.dat");
983
	strcpy(configs.Device, "/dev/ttyS1");
998
	strcpy(configs.Device, "/dev/ttyS1");
-
 
999
	strcpy(configs.Pidfile, "/var/run/heizung.pid");
984
	configs.port = 11001;
1000
	configs.port = 11001;
985
 
1001
 
986
	if (!confFile[0] || (fd = open(confFile,O_RDONLY)) == -1)
1002
	if (!confFile[0] || (fd = open(confFile,O_RDONLY)) == -1)
987
	{
1003
	{
988
	   if (confFile[0])
1004
	   if (confFile[0])
Line 1058... Line 1074...
1058
	   {
1074
	   {
1059
	      syslog(LOG_INFO,"Found \"Device\": %s", hv1);
1075
	      syslog(LOG_INFO,"Found \"Device\": %s", hv1);
1060
	      strncpy (configs.Device, hv1, sizeof(configs.Device));
1076
	      strncpy (configs.Device, hv1, sizeof(configs.Device));
1061
	   }
1077
	   }
1062
 
1078
 
-
 
1079
	   if (!strcasecmp(hv0, "Pidfile"))
-
 
1080
	   {
-
 
1081
	      syslog(LOG_INFO,"Found \"Pidfile\": %s", hv1);
-
 
1082
	      strncpy (configs.Pidfile, hv1, sizeof(configs.Pidfile));
-
 
1083
	   }
-
 
1084
 
1063
	   if (!strcasecmp(hv0, "VID"))
1085
	   if (!strcasecmp(hv0, "VID"))
1064
	   {
1086
	   {
1065
	      syslog(LOG_INFO,"Found VendorID: %04x", atoi(hv1));
1087
	      syslog(LOG_INFO,"Found VendorID: %04x", atoi(hv1));
1066
	      configs.VID = atoi(hv1);
1088
	      configs.VID = atoi(hv1);
1067
	   }
1089
	   }
Line 1267... Line 1289...
1267
	return writeHeizPlanName(configs.HeizPath);
1289
	return writeHeizPlanName(configs.HeizPath);
1268
}
1290
}
1269
 
1291
 
1270
int writeHeizPlanName(char *fname)
1292
int writeHeizPlanName(char *fname)
1271
{
1293
{
1272
int fd;
1294
int fd, wday;
-
 
1295
ulong lastEnd;
1273
char hv0[512], path[512];
1296
char hv0[128], path[512];
1274
HEIZINDEX *act;
1297
HEIZINDEX *act;
1275
 
1298
 
1276
	fd = -1;
1299
	fd = -1;
1277
 
1300
 
1278
	if (makeFileName(&path[0], fname, sizeof(path)) == NULL)
1301
	if (makeFileName(&path[0], fname, sizeof(path)) == NULL)
1279
	{
1302
	{
1280
	   syslog(LOG_DAEMON,"No or invalid path %s", fname);
1303
	   syslog(LOG_DAEMON,"No or invalid path %s", fname);
1281
	   return 0;
1304
	   return 0;
1282
	}
1305
	}
1283
 
1306
 
-
 
1307
	if (!sortTable())
-
 
1308
	   return 0;
-
 
1309
 
1284
	if ((fd = open(path, O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
1310
	if ((fd = open(path, O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
1285
	{
1311
	{
1286
	   syslog(LOG_DAEMON, "Error opening/creating file %s: %s", path, strerror(errno));
1312
	   syslog(LOG_DAEMON, "Error opening/creating file %s: %s", path, strerror(errno));
1287
	   return 0;
1313
	   return 0;
1288
	}
1314
	}
1289
 
1315
 
1290
	act = HeizFirst;
1316
	// Sort table for week days
1291
 
-
 
1292
	while(act)
1317
	for (wday = 1; wday <= 7; wday++)
1293
	{
1318
	{
-
 
1319
	   lastEnd = 0L;
1294
	   if (act->heizung->wday)
1320
	   act = HeizFirst;
-
 
1321
 
-
 
1322
	   while(act)
1295
	   {
1323
	   {
-
 
1324
	      if (act->heizung->wday == wday)
-
 
1325
	      {
1296
	      int hour, min;
1326
	      int hour, min;
1297
 
1327
 
1298
	      hour = act->heizung->end / 3600;
1328
		 hour = act->heizung->end / 3600;
1299
	      min = (act->heizung->end - (hour * 3600)) / 60;
1329
		 min = (act->heizung->end - (hour * 3600)) / 60;
-
 
1330
 
-
 
1331
		 if (hour > 23)
-
 
1332
		 {
-
 
1333
		    hour = 23;
-
 
1334
		    min = 59;
-
 
1335
		 }
-
 
1336
 
-
 
1337
		 sprintf(&hv0[0], "%d,%02d:%02d,%.1f\n",
-
 
1338
		     act->heizung->wday, hour, min,
-
 
1339
		     act->heizung->temp);
-
 
1340
		 write(fd, &hv0[0], strlen(hv0));
-
 
1341
	      }
-
 
1342
 
-
 
1343
	      act = act->next;
-
 
1344
	   }
-
 
1345
	}
-
 
1346
 
-
 
1347
	close(fd);
-
 
1348
	return 1;
-
 
1349
}
-
 
1350
 
-
 
1351
/*
-
 
1352
 * Allocate the memory for the actual heizung plan.
-
 
1353
 * This function appends an element to the end of the chain.
-
 
1354
 */
-
 
1355
HEIZINDEX *allocMem(HEIZINDEX *first)
-
 
1356
{
-
 
1357
HEIZINDEX *act, *last;
-
 
1358
 
-
 
1359
	if (!first)
-
 
1360
	{
-
 
1361
	   first = malloc(sizeof(HEIZINDEX));
-
 
1362
 
-
 
1363
	   if (first)
-
 
1364
	   {
-
 
1365
	      first->heizung = malloc(sizeof(HEIZUNG));
-
 
1366
	      first->next = NULL;
-
 
1367
	   }
-
 
1368
	   else
-
 
1369
	      return NULL;
-
 
1370
 
-
 
1371
	   return first;
-
 
1372
	}
-
 
1373
	else
-
 
1374
	{
-
 
1375
	   // Find last element
-
 
1376
	   last = first;
-
 
1377
 
-
 
1378
	   while(last->next)
-
 
1379
	      last = last->next;
-
 
1380
 
-
 
1381
	   act = malloc(sizeof(HEIZINDEX));
-
 
1382
 
-
 
1383
	   if (act)
-
 
1384
	   {
-
 
1385
	      act->heizung = malloc(sizeof(HEIZUNG));
-
 
1386
	      act->next = NULL;
-
 
1387
	      last->next = act;
-
 
1388
	   }
-
 
1389
	   else
-
 
1390
	      return NULL;
-
 
1391
 
-
 
1392
	   return act;
-
 
1393
	}
-
 
1394
 
-
 
1395
	return NULL;
-
 
1396
}
-
 
1397
 
-
 
1398
// This function sorts the table for week days and every into increasing
-
 
1399
// time stamps
-
 
1400
int sortTable()
-
 
1401
{
-
 
1402
int count, actElement, wday, flag;
-
 
1403
ulong end;
-
 
1404
HEIZINDEX *first, *act, *p;
-
 
1405
 
-
 
1406
	if (!HeizFirst)
-
 
1407
	   return 0;
-
 
1408
 
-
 
1409
	p = HeizFirst;
-
 
1410
	// Count the number of elements
-
 
1411
	count = 0;
-
 
1412
 
-
 
1413
	while (p)
-
 
1414
	{
-
 
1415
	   if (p->heizung->wday)
-
 
1416
	      count++;
-
 
1417
 
-
 
1418
	   p = p->next;
-
 
1419
	}
-
 
1420
 
-
 
1421
	wday = 1;
-
 
1422
	act = first = NULL;
-
 
1423
	actElement = 0;
-
 
1424
 
-
 
1425
	// First loop sorts for week days
-
 
1426
	while (actElement < count && wday <= 7)
-
 
1427
	{
-
 
1428
	int cnt, pos;
1300
 
1429
 
-
 
1430
	   p = HeizFirst;
-
 
1431
	   flag = 0;
-
 
1432
	   // find number of entries for actual week day
-
 
1433
	   cnt = 0;
-
 
1434
	   act = HeizFirst;
-
 
1435
 
-
 
1436
	   while(act)
-
 
1437
	   {
-
 
1438
	      if (act->heizung->wday == wday)
-
 
1439
		 cnt++;
-
 
1440
 
1301
	      if (hour > 23)
1441
	      act = act->next;
-
 
1442
	   }
-
 
1443
 
-
 
1444
	   pos = 0;
-
 
1445
	   end = 0L;
-
 
1446
 
-
 
1447
	   // Second loop sorts the actual week day for end times
-
 
1448
	   while (p)
-
 
1449
	   {
-
 
1450
	      if (p->heizung->wday == wday && p->heizung->start == end && p->heizung->end > end)
1302
	      {
1451
	      {
-
 
1452
		 actElement++;
1303
		 hour = 23;
1453
		 flag = 1;
-
 
1454
		 act = allocMem(first);
-
 
1455
 
-
 
1456
		 if (!first)
-
 
1457
		    first = act;
-
 
1458
 
-
 
1459
		 // Check for valid memory block
1304
		 min = 59;
1460
		 if (act)
-
 
1461
		 {
-
 
1462
		    memmove(act->heizung, p->heizung, sizeof(HEIZUNG));
-
 
1463
		    end = p->heizung->end;
-
 
1464
		    pos++;
-
 
1465
		 }
-
 
1466
		 else
-
 
1467
		 {
-
 
1468
		    syslog(LOG_DAEMON,"Error allocationg memory for sorting table: %s", strerror(errno));
-
 
1469
		    // Free allocated memory
-
 
1470
		    act = first;
-
 
1471
 
-
 
1472
		    while(act)
-
 
1473
		    {
-
 
1474
		       p = act->next;
-
 
1475
		       free(act->heizung);
-
 
1476
		       free(act);
-
 
1477
		       act = p;
-
 
1478
		    }
-
 
1479
 
-
 
1480
		    return 0;
-
 
1481
		 }
1305
	      }
1482
	      }
1306
 
1483
 
-
 
1484
	      p = p->next;
-
 
1485
 
1307
	      sprintf(&hv0[0], "%d,%02d:%02d,%.1f\n",
1486
	      // If there are still members left, reset pointer and restart loop
1308
		   act->heizung->wday, hour, min,
1487
	      if (!p && pos < (cnt - 1) && flag)
-
 
1488
	      {
1309
		   act->heizung->temp);
1489
		 p = HeizFirst;
1310
	      write(fd, &hv0[0], strlen(hv0));
1490
		 flag = 0;
-
 
1491
	      }
1311
	   }
1492
	   }
1312
 
1493
 
1313
	   act = act->next;
1494
	   wday++;
1314
	}
1495
	}
1315
 
1496
 
1316
	close(fd);
1497
	freeMemory();
-
 
1498
	HeizFirst = first;
1317
	return 1;
1499
	return 1;
1318
}
1500
}
1319
 
1501
 
1320
char *readLine(int fd, char *buf, int bufLen)
1502
char *readLine(int fd, char *buf, int bufLen)
1321
{
1503
{