Hi
I found these in my archive
Remove the .CSV file extensions to restore to correct type
ReadMe.txt.csv (839 Bytes)
timer.c.csv (5.5 KB)
VelservTimer.zip.csv (2.6 KB)
Readme.txt content
correct compile instruction :- gcc -Wall -o veltimeupdate timer.c -lpthread
Usage: ./veltimupdate [-apmhV]
sudo ./veltimeupdate -p 6000 -m 0
-a --address HOST IP address or hostname where to connect to as client
default is 127.0.0.1
-p --port PORT port where to connect
default is 3788
-m --mode MODE mode = 0 --> single update and exit (default mode)
mode = 1 --> update at 12h AM/PM in background
-h --help print this help and exit
-V --version print version information and exit
So you can use it as standalone (mode = 1), in this mode the velbus system will be synced with the computer system time. Or you can use it with crontab when in Mode 0.
In Mode 0, everytime the program starts it will sync Velbus with the system time of the PC.
timer.c content
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <getopt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define VERSION "0.2"
#define IP "127.0.0.1"
char *IP_ADDRESS = IP;
unsigned int PORT = 3788;
int sock;
int i_hour,i_min,i_sec,i_day;
unsigned int mode = 0;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
/////////////////////////
// Time values thread
/////////////////////////
void *time_values()
{
char outstr[200];
time_t td;
struct tm *tmp;
int loop = 1;
while(loop)
{
td = time(NULL);
tmp = localtime(&td);
strftime(outstr, sizeof(outstr), "%H", tmp);
i_hour = atoi(outstr);
strftime(outstr, sizeof(outstr), "%M", tmp);
i_min = atoi(outstr);
strftime(outstr, sizeof(outstr), "%S", tmp);
i_sec = atoi(outstr);
strftime(outstr, sizeof(outstr), "%u", tmp);
i_day = atoi(outstr) - 1;
usleep(100000);
if (mode == 0)
{
loop = 0;
}
}
return 0;
}
////////////////////////////////////////////////
// Velbus functions
////////////////////////////////////////////////
unsigned char checksum(unsigned char*lpData, int nSize)
{
unsigned char c = 0;
int k = 0;
for(; k<(nSize-2); ++k)
c += lpData[k];
return (-c);
}
int send_pakket(unsigned char prio, unsigned char rtr, unsigned char lenght, unsigned char addres, unsigned char*lpData)
{
int k;
unsigned char dest[30];
dest[0]=0xf;
dest[1]=prio;
dest[2]=addres;
dest[3]=(0xf0 & rtr) | (0xf & lenght);
for (k=0; k<lenght; k++)
{
dest[4+k]=lpData[k];
}
dest[lenght+4]=checksum(dest,(lenght + 6));
dest[lenght+5]=0x4;
send(sock,dest,(lenght+6), 0);
return 0;
}
int send_time ()
{
unsigned char prio, rtr, lenght, addres;
unsigned char message[8];
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname(IP_ADDRESS);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr,"Velbus time updater: error creating client socket\n");
return 1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
{
fprintf(stderr,"Velbus time updater: error connecting client to server\n");
return 1;
}
addres = 0x00;
message[0] = 0xD8;
message[1] = i_day;
message[2] = i_hour;
message[3] = i_min;
lenght = 0x4;
rtr = 0x00;
prio = 0xfb;
send_pakket(prio, rtr, lenght, addres, message);
close(sock);
return 0;
}
////////////////////////////////
// Program options control
////////////////////////////////
static void version (FILE *f, int i)
{
fprintf(f, "Velbus time updater %s\n",VERSION);
fprintf(f, "Copyright (C) 2017 Jeroen De Schepper\n");
exit (i);
}
static void usage (FILE *f, char **argv, int i)
{
fprintf(f,"Usage: %s [-apmhV]\n",argv[0]);
fprintf(f,"\n");
fprintf(f,"-a --address HOST IP address or hostname where to connect to as client\n");
fprintf(f," default is 127.0.0.1\n");
fprintf(f,"-p --port PORT port where to connect\n");
fprintf(f," default is 3788\n");
fprintf(f,"-m --mode MODE mode = 0 --> single update and exit (default mode)\n");
fprintf(f," mode = 1 --> update at 12h AM/PM in background\n");
fprintf(f,"-h --help print this help and exit\n");
fprintf(f,"-V --version print version information and exit\n");
fprintf(f,"\n");
exit (i);
}
static struct option long_options[] = {
{ "address", required_argument, NULL, 'a'},
{ "port", required_argument, NULL, 'p' },
{ "mode", required_argument, NULL, 'm' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V'},
{ NULL, 0, NULL, 0 } };
static void parse_params(int argc, char **argv)
{
while(1) {
int c = getopt_long (argc, argv, "a:p:m:hV", long_options, NULL);
if (c == -1)
break;
switch(c) {
case 'a':
IP_ADDRESS = strdup (optarg);
break;
case 'p':
PORT = atoi(strdup (optarg));
break;
case 'm':
mode = atoi(strdup (optarg));
break;
case 'h':
usage (stdout, argv, 0);
case 'V':
version (stdout, 0);
exit (0);
case '?':
usage (stdout, argv, 0);
}
}
}
/////////////
// main
/////////////
int main(int argc, char **argv)
{
pthread_t thread1;
int iret1=0;
pid_t pid, sid;
parse_params (argc, argv);
if (optind > argc)
{
usage (stderr, argv, 1);
}
if (mode)
{
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0)
{
exit(EXIT_FAILURE);
}
if ((chdir("/")) < 0)
{
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
iret1 = pthread_create( &thread1, NULL, time_values, NULL);
sleep(1);
send_time();
while(mode)
{
if ((i_hour == 12 || i_hour == 0) && i_min == 0 && i_sec == 0)
{
send_time();
}
sleep(1);
}
pthread_join( thread1, NULL);
return 0;
}