Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5aa0c7b89f | ||
|
|
de95821d6b | ||
|
|
e839c7f93a | ||
|
|
34cec2c4d0 | ||
|
|
07f80f9e4f | ||
|
|
13491b809b | ||
|
|
089c1ce25f | ||
|
|
b157063f92 | ||
|
|
ab0ae0671c | ||
|
|
611f97242a | ||
|
|
176d8ead1e | ||
|
|
8d1a74fc8d | ||
|
|
1a24d0eb64 | ||
|
|
de292e5c38 | ||
|
|
17c1fc392b | ||
|
|
80f3c05170 | ||
|
|
800efb2a3f | ||
|
|
c64393e72c | ||
|
|
d34f2bed05 | ||
|
|
afe577a0cb | ||
|
|
b76f80f6b7 | ||
|
|
c70252be4b | ||
|
|
4bbd2c435f | ||
|
|
3e0ed17d26 | ||
|
|
1e8dd93ed6 | ||
|
|
b7212ee4a7 | ||
|
|
53eedfc10b | ||
|
|
24689cd83e | ||
|
|
69ccfb0b68 | ||
|
|
5f274e7cc7 | ||
|
|
12137089fe | ||
|
|
91ae304ee9 | ||
|
|
5c2f6c2367 | ||
|
|
c84acef8e0 | ||
|
|
d8b5a00376 | ||
|
|
bbac6c3a08 | ||
|
|
f8869ac5fe | ||
|
|
b6d16bd871 | ||
|
|
0d6b6b8dad | ||
|
|
5e2ff7f912 | ||
|
|
949aacdcbd | ||
|
|
65c8e01300 | ||
|
|
a6a017ed80 |
12
Makefile
12
Makefile
@@ -1,8 +1,14 @@
|
||||
CC = gcc
|
||||
CFLAGS = -DVERSION=\"$(GIT_VERSION)\" -Werror -Wall -Wextra -pedantic -Wcast-align -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wredundant-decls -Wshadow -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option -Wconversion
|
||||
LDFLAGS = -std=c11 -lcurl -lconfuse -ljson-c -lpthread -lrt
|
||||
CFLAGS = -DVERSION=\"$(GIT_VERSION)\" -Werror -Wall -Wextra -pedantic -Wcast-align\
|
||||
-Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs\
|
||||
-Wredundant-decls -Wshadow -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-variadic-macros\
|
||||
-Wno-parentheses -fdiagnostics-show-option -Wconversion -Wswitch-default -Wvla -Wlong-long -Wredundant-decls\
|
||||
-Wunreachable-code -Wduplicated-cond -Wnull-dereference -Wjump-misses-init -Wdouble-promotion\
|
||||
-Wimplicit-fallthrough -Wduplicated-branches -Wrestrict
|
||||
|
||||
GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags)
|
||||
LDFLAGS = -std=gnu11 -lcurl -lconfuse -ljson-c -lpthread -lrt
|
||||
|
||||
GIT_VERSION := $(shell git describe --dirty --always --tags)
|
||||
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
@@ -25,7 +25,4 @@ relay = 1
|
||||
#draait schakelfunctie van het relais om
|
||||
relayinvert = false
|
||||
|
||||
#schakeltijd in seconden
|
||||
switchtime = 5
|
||||
|
||||
|
||||
|
||||
10
config.c
10
config.c
@@ -14,17 +14,10 @@ int get_device_id(char *device){
|
||||
int fd;
|
||||
ssize_t count;
|
||||
|
||||
#ifdef TEST
|
||||
if((fd = open("/sys/class/net/enxb827eb66e7b5/address", O_RDONLY)) < 0){
|
||||
writesyslog(LOG_ERR, "error opening /sys/class/net/enxb827eb66e7b5/address");
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if((fd = open("/sys/class/net/eth0/address", O_RDONLY)) < 0){
|
||||
writesyslog(LOG_ERR, "error opening /sys/class/net/eth0/address");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
char mac[18];
|
||||
count = read(fd, &mac, sizeof(mac));
|
||||
@@ -66,7 +59,6 @@ int config_read(struct config_t *config, char *filepath){
|
||||
config->inputlevel = 0;
|
||||
config->relay = 0;
|
||||
config->relayinvert = cfg_false;
|
||||
config->switchtime = 0;
|
||||
|
||||
cfg_opt_t opts[] = {
|
||||
//CFG_SIMPLE_BOOL("verbose", &verbose),
|
||||
@@ -84,7 +76,6 @@ int config_read(struct config_t *config, char *filepath){
|
||||
CFG_SIMPLE_INT("inputlevel", &config->inputlevel),
|
||||
CFG_SIMPLE_INT("relay", &config->relay),
|
||||
CFG_SIMPLE_BOOL("relayinvert", &config->relayinvert),
|
||||
CFG_SIMPLE_INT("switchtime", &config->switchtime),
|
||||
CFG_END()
|
||||
};
|
||||
cfg_t *cfg;
|
||||
@@ -125,6 +116,5 @@ void config_print(struct config_t *config, char *filepath){
|
||||
printf("inputlevel: %ld\n", config->inputlevel);
|
||||
printf("relay: %ld\n", config->relay);
|
||||
printf("relayinvert: %s\n", config->relayinvert ? "true" : "false");
|
||||
printf("switchtime: %ld\n", config->switchtime);
|
||||
printf("-----------------------------------\n\n");
|
||||
}
|
||||
|
||||
1
config.h
1
config.h
@@ -13,7 +13,6 @@ struct config_t {
|
||||
long int inputlevel;
|
||||
long int relay;
|
||||
cfg_bool_t relayinvert;
|
||||
long int switchtime;
|
||||
};
|
||||
|
||||
int config_read(struct config_t *config, char *filepath);
|
||||
|
||||
85
display.c
Normal file
85
display.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <syslog.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
#include "config.h"
|
||||
#include "web.h"
|
||||
#include "log.h"
|
||||
#include "jsontypes.h"
|
||||
#include "display.h"
|
||||
|
||||
#define CONTENT_LENGTH 21
|
||||
#define MENU_LENGTH 8
|
||||
#define MENU_SIZE 4
|
||||
#define MENU_OFFSET 80
|
||||
|
||||
static int line[] = {
|
||||
0, 8, 16, 24,
|
||||
};
|
||||
|
||||
//write display line to h1502display char device
|
||||
int write_device(char *ln, size_t len){
|
||||
int fd = open("/dev/h1502display", O_WRONLY);
|
||||
if (fd == -1) {
|
||||
writesyslog(LOG_ERR, "open /dev/h1502display error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t ret = write(fd, ln, len);
|
||||
if(ret < 0){
|
||||
writesyslog(LOG_ERR, "write /dev/h1502display error");
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//write data to de h1502display device
|
||||
int write_line(int y, int x, int invert, char *txt, int32_t time){
|
||||
size_t len = strlen(txt);
|
||||
if(len <= 0 ){
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *buf = malloc((len + 12) * sizeof(char));
|
||||
if(buf == NULL){
|
||||
writesyslog(LOG_ERR, "write_line: out off memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int length = sprintf(buf, "%03d%03d%03d%d%s", x, y, time, invert, txt);
|
||||
|
||||
int ret = 0;
|
||||
if(length > 0){
|
||||
#ifdef TEST
|
||||
ret = printf("Display write ->%s<-\n", buf);
|
||||
#else
|
||||
ret = write_device(buf, ((size_t)length + 1));
|
||||
#endif
|
||||
}
|
||||
|
||||
free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void clear_display(void){
|
||||
write_line(line[0], MENU_OFFSET, 0, " ", 0);
|
||||
write_line(line[1], 0, 0, " ", 0);
|
||||
write_line(line[2], 0, 0, " ", 0);
|
||||
write_line(line[3], MENU_OFFSET, 0, " ", 0);
|
||||
}
|
||||
|
||||
void write_display(int ln, int pos, int invert, char *txt, int32_t time){
|
||||
if(time <= 0){
|
||||
time = 3;
|
||||
}
|
||||
write_line(line[ln], pos, invert, txt, time);
|
||||
}
|
||||
8
display.h
Normal file
8
display.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef DISPLAY_FILE
|
||||
#define DISPLAY_FILE
|
||||
|
||||
void clear_display(void);
|
||||
void write_display(int ln, int pos, int invert, char *txt, int time);
|
||||
void display_timer(long int time);
|
||||
|
||||
#endif
|
||||
11
global.h
Normal file
11
global.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef FLAGS_FILE
|
||||
#define FLAGS_FILE
|
||||
|
||||
#define ACS_TRUE 1
|
||||
#define ACS_FALSE 0
|
||||
|
||||
#define DISPLAY_TIME 60
|
||||
|
||||
volatile int doorlockedflag;
|
||||
|
||||
#endif
|
||||
81
gpio.c
81
gpio.c
@@ -181,7 +181,7 @@ int GPIOWrite(unsigned int pin, unsigned int value){
|
||||
|
||||
#ifdef TEST
|
||||
unsigned int relaysysfs[9] = {0, 17, 17, 17, 17, 17, 17, 17, 17}; //SYSFS export number
|
||||
unsigned int relaypin[9] = {0, 17, 17, 17, 17, 17, 17, 17, 17}; //PortC number
|
||||
unsigned int relaypin[9] = {0, 17, 17, 17, 17, 17, 17, 17, 17}; //Port number
|
||||
#else
|
||||
unsigned int relaysysfs[9] = {0, 81, 80, 79, 78, 83, 84, 76, 77}; //SYSFS export number
|
||||
unsigned int relaypin[9] = {0, 17, 16, 15, 14, 19, 20, 12, 13}; //PortC number
|
||||
@@ -200,7 +200,7 @@ int gpio_initrelay(unsigned int relay, cfg_bool_t relayinvert){
|
||||
free(log);
|
||||
return(-1);
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
for (int i = 1; i<9; i++ ) {
|
||||
if(GPIOExport(relaysysfs[i]) < 0){
|
||||
return(-1);
|
||||
@@ -209,6 +209,7 @@ int gpio_initrelay(unsigned int relay, cfg_bool_t relayinvert){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if(relayinvert == cfg_true){
|
||||
if(GPIOWrite(relaypin[relay], HIGH) < 0){
|
||||
@@ -224,77 +225,9 @@ int gpio_initrelay(unsigned int relay, cfg_bool_t relayinvert){
|
||||
}
|
||||
|
||||
|
||||
//void timerhandler(int signal){
|
||||
// if (signal == SIGALRM) {
|
||||
//relayswitchflag = 1;
|
||||
//displaytime = 0;
|
||||
// printf("Timer handler\n");
|
||||
|
||||
// }else{
|
||||
// char *log;
|
||||
// if(asprintf(&log, "Timerhandler: wrong signal returned: %d\n", signal) > 0 ){
|
||||
// writesyslog(LOG_ERR, log);
|
||||
// }
|
||||
// free(log);
|
||||
// }
|
||||
//}
|
||||
|
||||
//void starttimer(unsigned int switchtime){
|
||||
// if(switchtime > 0){
|
||||
// if (signal(SIGALRM, timerhandler) == SIG_ERR){
|
||||
// writesyslog(LOG_ERR, "starttimer - can't catch SIGALRM\n");
|
||||
// }
|
||||
// alarm(switchtime);
|
||||
// }else{
|
||||
//relayswitchflag = 1;
|
||||
// }
|
||||
//}
|
||||
|
||||
//int gpio_switchrelay(int state, unsigned int relay, cfg_bool_t relayinvert){
|
||||
// if(state == RELAY_ON){
|
||||
// if (relayinvert == cfg_false){
|
||||
//#ifdef TEST
|
||||
// printf("Relay %d - HIGH\n", relay);
|
||||
//#endif
|
||||
// if(GPIOWrite(relaypin[relay], HIGH) < 0){
|
||||
// return(-1);
|
||||
// }
|
||||
// }
|
||||
// else{
|
||||
//#ifdef TEST
|
||||
// printf("Relay %d - LOW\n", relay);
|
||||
//#endif
|
||||
// if(GPIOWrite(relaypin[relay], LOW) < 0){
|
||||
// return(-1);
|
||||
// }
|
||||
// }//
|
||||
// }
|
||||
// else{
|
||||
// if (relayinvert == cfg_false){
|
||||
//#ifdef TEST
|
||||
// printf("Relay %d - LOW\n", relay);
|
||||
//#endif
|
||||
// if(GPIOWrite(relaypin[relay], LOW) < 0){
|
||||
// return(-1);
|
||||
// }
|
||||
// }
|
||||
// else{
|
||||
//#ifdef TEST
|
||||
// printf("Relay %d - HIGH\n", relay);
|
||||
//#endif
|
||||
// if(GPIOWrite(relaypin[relay], HIGH) < 0){
|
||||
// return(-1);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return(0);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
#ifdef TEST
|
||||
unsigned int inputsysfs[9] = {0, 2, 2, 2, 2, 2, 2, 2, 2}; //SYSFS export number
|
||||
unsigned int inputpin[9] = {0, 2, 2, 2, 2, 2, 2, 2, 2}; //PortA number
|
||||
unsigned int inputsysfs[9] = {0, 22, 22, 22, 22, 22, 22, 22, 22}; //SYSFS export number
|
||||
unsigned int inputpin[9] = {0, 22, 22, 22, 22, 22, 22, 22, 22}; //Port number
|
||||
#else
|
||||
unsigned int inputsysfs[9] = {0, 29, 28, 27, 26, 25, 24, 23, 22}; //SYSFS export number
|
||||
unsigned int inputpin[9] = {0, 29, 28, 27, 26, 25, 24, 23, 22}; //PortA number
|
||||
@@ -313,7 +246,8 @@ int gpio_initinput(unsigned int input){
|
||||
free(log);
|
||||
return(-1);
|
||||
}
|
||||
//input is pioA
|
||||
|
||||
#ifdef TEST
|
||||
for (int i = 1; i<9; i++ ) {
|
||||
if(GPIOExport(inputsysfs[i]) < 0){
|
||||
return(-1);
|
||||
@@ -322,5 +256,6 @@ int gpio_initinput(unsigned int input){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return(0);
|
||||
}
|
||||
|
||||
3
gpio.h
3
gpio.h
@@ -16,9 +16,6 @@ int GPIOWrite(unsigned int pin, unsigned int value);
|
||||
|
||||
unsigned int get_relaypin(unsigned int pin);
|
||||
int gpio_initrelay(unsigned int relay, cfg_bool_t relayinvert);
|
||||
//void starttimer(unsigned int switchtime);
|
||||
//unsigned int get_relaypin(int pin);
|
||||
//int gpio_switchrelay(int state, unsigned int relay, cfg_bool_t relayinvert);
|
||||
|
||||
unsigned int get_inputpin(int pin);
|
||||
int gpio_initinput(unsigned int input);
|
||||
|
||||
53
inputs.c
53
inputs.c
@@ -17,35 +17,35 @@
|
||||
#include "log.h"
|
||||
#include "web.h"
|
||||
#include "relay.h"
|
||||
|
||||
#include "global.h"
|
||||
#include "display.h"
|
||||
|
||||
int input_fd_open(long int input){
|
||||
char *fle, *flv;
|
||||
#ifdef TEST
|
||||
if(asprintf(&fle, "/sys/class/gpio/gpio%d/edge", get_inputpin(input)) < 0 ){
|
||||
if(asprintf(&fle, "/sys/class/gpio/gpio%d/edge", get_inputpin((int)input)) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
if(asprintf(&flv, "/sys/class/gpio/gpio%d/value", get_inputpin(input)) < 0 ){
|
||||
if(asprintf(&flv, "/sys/class/gpio/gpio%d/value", get_inputpin((int)input)) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
#else
|
||||
if(asprintf(&flve, "/sys/class/gpio/pioA%d/edge", get_inputpin(input)) < 0 ){
|
||||
if(asprintf(&fle, "/sys/class/gpio/pioA%d/edge", get_inputpin((int)input)) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
if(asprintf(&flv, "/sys/class/gpio/pioA%d/value", get_inputpin(input)) < 0 ){
|
||||
if(asprintf(&flv, "/sys/class/gpio/pioA%d/value", get_inputpin((int)input)) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
#endif
|
||||
|
||||
int fd = open(fle, O_WRONLY);
|
||||
write(fd, "both", 4);
|
||||
ssize_t ret = write(fd, "both", 4);
|
||||
close(fd);
|
||||
|
||||
//fd = open(flv, O_RDONLY | O_NONBLOCK );
|
||||
fd = open(flv, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
writesyslog(LOG_ERR, "failed to open gpio value for manualinput");
|
||||
@@ -66,7 +66,7 @@ void *readinputs(void *ptr){
|
||||
struct pollfd pfd;
|
||||
pfd.fd = input_fd_open(config->manualinput);;
|
||||
if(pfd.fd < 0){
|
||||
printf("manualinput failed\n");
|
||||
writesyslog(LOG_ERR, "manualinput failed\n");
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
pfd.events = POLLPRI;
|
||||
@@ -92,26 +92,27 @@ void *readinputs(void *ptr){
|
||||
free(log);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if((val == '0' && config->inputlevel == 1) || (val == '1' && config->inputlevel == 0)){
|
||||
if(relay_switch(RELAY_ON, config->switchtime, config) == ERROR){
|
||||
if(doorlockedflag == ACS_TRUE){
|
||||
if((val == '0' && config->inputlevel == 1) || (val == '1' && config->inputlevel == 0)){
|
||||
int32_t switchtime = server_get_opentime(config);
|
||||
if(relay_switch(RELAY_ON, switchtime, config) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
clear_display();
|
||||
char *ln1;
|
||||
if(asprintf(&ln1, "Manual open - %d sec", switchtime) > 0 ){
|
||||
write_display(1, 0, 0, ln1, switchtime);
|
||||
}
|
||||
char *ln2;
|
||||
if(asprintf(&ln2, "In: %ld - Relay: %ld", config->manualinput, config->relay) > 0 ){
|
||||
write_display(2, 0, 0, ln2, switchtime);
|
||||
}
|
||||
free(ln2);
|
||||
|
||||
writesyslog(LOG_INFO, ln1);
|
||||
server_send_log(config, ln1);
|
||||
free(ln1);
|
||||
}
|
||||
|
||||
clear_display();
|
||||
write_display(ln[1], 0, 0, "Manual open");
|
||||
char *ln2;
|
||||
asprintf(&ln2, "In: %ld - Relay: %ld",config->manualinput, config->relay);
|
||||
write_display(ln[2], 0, 0, ln2);
|
||||
free(ln2);
|
||||
|
||||
writesyslog(LOG_INFO, "Manual open");
|
||||
server_send_log(config, "Manual open");
|
||||
|
||||
//wait
|
||||
//sleep((unsigned int)config->switchtime);
|
||||
|
||||
//clear_display();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
40
jsontypes.h
40
jsontypes.h
@@ -2,33 +2,33 @@
|
||||
#define JSONTYPES_FILE
|
||||
|
||||
typedef enum{
|
||||
UNKNOWNERROR = 0,
|
||||
ACCESSGRANTED = 1,
|
||||
ACCESSDENIED = 2,
|
||||
UNKNOWNERROR = 0,
|
||||
|
||||
LOGOK = 3,
|
||||
LOGERROR = 4,
|
||||
ACCESSGRANTED = 1,
|
||||
ACCESSDENIED = 2,
|
||||
|
||||
CONNECTIONTIMEOUT = 5,
|
||||
RESOLVEERROR = 6,
|
||||
SERVERERROR = 7,
|
||||
LOGOK = 3,
|
||||
LOGERROR = 4,
|
||||
|
||||
CREDENTIALERROR = 8,
|
||||
CONNECTIONTIMEOUT = 5,
|
||||
RESOLVEERROR = 6,
|
||||
SERVERERROR = 7,
|
||||
|
||||
LOCKSTATEOK = 9,
|
||||
LOCKSTATEERROR = 10,
|
||||
CREDENTIALERROR = 8,
|
||||
|
||||
LOCKSTATEOK = 9,
|
||||
LOCKSTATEERROR = 10,
|
||||
|
||||
STATUSOK = 11,
|
||||
STATUSERROR = 12,
|
||||
|
||||
OPENTIMEOK = 13,
|
||||
OPENTIMEERROR = 14,
|
||||
|
||||
DEVICEINACTIVE = 15,
|
||||
} returnstate_t;
|
||||
|
||||
|
||||
struct returnjson_t{
|
||||
char device[7];
|
||||
char uid[11];
|
||||
int cardnumber;
|
||||
char sectorkey[36];
|
||||
int opentime;
|
||||
returnstate_t state;
|
||||
};
|
||||
|
||||
typedef enum{
|
||||
UNKNOWN = 0,
|
||||
UNLOCKED = 1,
|
||||
|
||||
132
log.c
132
log.c
@@ -1,22 +1,6 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <syslog.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <json-c/json.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include "config.h"
|
||||
#include "web.h"
|
||||
#include "log.h"
|
||||
#include "jsontypes.h"
|
||||
|
||||
#define CONTENT_LENGTH 21
|
||||
#define MENU_LENGTH 8
|
||||
#define MENU_SIZE 4
|
||||
#define MENU_OFFSET 80
|
||||
|
||||
void writesyslog(int priority, const char *logtext){
|
||||
#ifdef TEST
|
||||
@@ -27,119 +11,3 @@ void writesyslog(int priority, const char *logtext){
|
||||
syslog(priority, "%s", logtext);
|
||||
closelog();
|
||||
}
|
||||
|
||||
|
||||
//write display line to h1502display char device
|
||||
int write_device(char *line){
|
||||
int fd;
|
||||
ssize_t ret;
|
||||
|
||||
fd = open("/dev/h1502display", O_WRONLY);
|
||||
if (fd == -1) {
|
||||
perror("open /dev/h1502display: ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = write(fd, line, strlen(line));
|
||||
if(ret < (int)strlen(line)){
|
||||
perror("write /dev/h1502display: ");
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//write display line to h1502display char device
|
||||
int fwrite_device(char *line){
|
||||
FILE *fd;
|
||||
|
||||
fd = fopen("/dev/h1502display", "w");
|
||||
if (fd == NULL) {
|
||||
writesyslog(LOG_ERR, "write_device: opening /dev/h1502display failed");
|
||||
return -1;
|
||||
}
|
||||
if(fwrite(line, sizeof(char), strlen(line), fd) != strlen(line)){
|
||||
writesyslog(LOG_ERR, "write_device: writing /dev/h1502display failed");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
fclose(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//write data to de h1502display device
|
||||
int write_line(int y, int x, int invert, char *txt){
|
||||
char *buf;
|
||||
int ret;
|
||||
|
||||
if(strlen(txt) <= 0 ){
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf = malloc((strlen(txt) + 8) * sizeof(char));
|
||||
if(buf == NULL){
|
||||
writesyslog(LOG_ERR, "write_line: out off memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sprintf(buf, "%03d%03d%d%s", x, y, invert, txt);
|
||||
ret = write_device(buf);
|
||||
|
||||
free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void clear_display(void){
|
||||
#ifndef TEST
|
||||
write_line(ln[0], MENU_OFFSET, 0, " ");
|
||||
write_line(ln[1], 0, 0, " ");
|
||||
write_line(ln[2], 0, 0, " ");
|
||||
write_line(ln[3], MENU_OFFSET, 0, " ");
|
||||
#endif
|
||||
}
|
||||
|
||||
void write_display(int line, int pos, int invert, char *txt){
|
||||
#ifdef TEST
|
||||
printf("Display write : %s\n", txt);
|
||||
#else
|
||||
write_line(line, pos, invert, txt);
|
||||
#endif
|
||||
}
|
||||
|
||||
void display_timer_handler(union sigval arg){
|
||||
printf("display timer handler\n");
|
||||
//struct config_t *config = (struct config_t *)arg.sival_ptr;
|
||||
//printf("relay: %ld\n", config->relay);
|
||||
//printf("relay invert: %d\n", config->relayinvert);
|
||||
//relay_switch(RELAY_OFF, 0, config);
|
||||
clear_display();
|
||||
}
|
||||
|
||||
void display_timer(int time){
|
||||
timer_t timer_id;
|
||||
struct itimerspec ts;
|
||||
struct sigevent se;
|
||||
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
//se.sigev_value.sival_ptr = config;
|
||||
|
||||
se.sigev_notify_function = display_timer_handler;
|
||||
se.sigev_notify_attributes = NULL;
|
||||
|
||||
ts.it_value.tv_sec = time;
|
||||
ts.it_value.tv_nsec = 0;
|
||||
ts.it_interval.tv_sec = 0;
|
||||
ts.it_interval.tv_nsec = 0;
|
||||
|
||||
int status = timer_create(CLOCK_REALTIME, &se, &timer_id);
|
||||
if (status == -1)
|
||||
printf("error create display timer\n");
|
||||
|
||||
status = timer_settime(timer_id, 0, &ts, 0);
|
||||
if (status == -1)
|
||||
printf("error set display timer\n");
|
||||
}
|
||||
|
||||
|
||||
8
log.h
8
log.h
@@ -3,12 +3,4 @@
|
||||
|
||||
void writesyslog(int priority, const char *logtext);
|
||||
|
||||
static int ln[] = {
|
||||
0, 8, 16, 24,
|
||||
};
|
||||
|
||||
void clear_display(void);
|
||||
void write_display(int line, int pos, int invert, char *txt);
|
||||
void display_timer(int time);
|
||||
|
||||
#endif
|
||||
|
||||
138
main.c
138
main.c
@@ -1,16 +1,11 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
#include <string.h>
|
||||
#include <sys/file.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include "runtime.h"
|
||||
#include "config.h"
|
||||
@@ -23,50 +18,19 @@
|
||||
#include "jsontypes.h"
|
||||
#include "inputs.h"
|
||||
#include "reader.h"
|
||||
#include "global.h"
|
||||
#include "display.h"
|
||||
|
||||
#define STARTMSG "Starting acsreader version " VERSION
|
||||
|
||||
//volatile unsigned int dooropenflag = 0;
|
||||
//volatile unsigned int relayswitchflag = 0;
|
||||
//volatile int displaytime;
|
||||
|
||||
//void timerhandler(int signal){
|
||||
// if (signal == SIGALRM) {
|
||||
// relayswitchflag = 1;
|
||||
// displaytime = 0;
|
||||
// }else{
|
||||
// char *log;
|
||||
// if(asprintf(&log, "Timerhandler: wrong signal returned: %d\n", signal) > 0 ){
|
||||
// writesyslog(LOG_ERR, log);
|
||||
// }
|
||||
// free(log);
|
||||
// }
|
||||
//}
|
||||
|
||||
//void starttimer(unsigned int switchtime){
|
||||
// if(switchtime > 0){
|
||||
// if (signal(SIGALRM, timerhandler) == SIG_ERR){
|
||||
// writesyslog(LOG_ERR, "starttimer - can't catch SIGALRM\n");
|
||||
// }
|
||||
// alarm(switchtime);
|
||||
// }else{
|
||||
// relayswitchflag = 1;
|
||||
// }
|
||||
//}
|
||||
|
||||
//void set_displaytime(int time){
|
||||
// displaytime = time;
|
||||
//}
|
||||
#define STARTMSG "Starting ACSreader version " VERSION
|
||||
|
||||
/*********************************/
|
||||
/* MAIN */
|
||||
/**********************************/
|
||||
int main(int argc, char **argv){
|
||||
writesyslog(LOG_INFO, "Starting ACSreader\n");
|
||||
writesyslog(LOG_INFO, STARTMSG);
|
||||
clear_display();
|
||||
write_display(ln[1], 0, 0, "Starting");
|
||||
write_display(ln[2], 0, 0, "ACSreader");
|
||||
//set_displaytime(100);
|
||||
write_display(1, 0, 0, "Starting", 5);
|
||||
write_display(2, 0, 0, "ACSreader", 5);
|
||||
|
||||
createpidfile("/var/run/acsreader.pid");
|
||||
sighandlerinit();
|
||||
@@ -106,44 +70,51 @@ int main(int argc, char **argv){
|
||||
|
||||
struct config_t config;
|
||||
if(config_read(&config, filepath) <= 0){
|
||||
printf("Config error\n");
|
||||
char *log;
|
||||
if(asprintf(&log, "Config erro: file path %s", filepath) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
printf("Config error: file path %s\r\n", filepath);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
config_print(&config, filepath);
|
||||
|
||||
if(gpio_initrelay((unsigned int)config.relay, (unsigned int)config.relayinvert) == ERROR){
|
||||
printf("GPIO relay error\n");
|
||||
char *log;
|
||||
if(asprintf(&log, "GPIO relay init error : relay %ld : invert %d", config.relay, config.relayinvert) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
doorlockedflag = ACS_TRUE;
|
||||
if(gpio_initinput((unsigned int)config.manualinput) == ERROR){
|
||||
printf("GPIO input error\n");
|
||||
char *log;
|
||||
if(asprintf(&log, "GPIO input init error : input %ld : level %ld", config.manualinput, config.inputlevel) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct serialport_t serial1, serial2;
|
||||
if(openserialport(&serial1, config.serialport1) == ERROR){
|
||||
writesyslog(LOG_ERR, "Serialport 1 error");
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
serial1.reader = 1;
|
||||
if(openserialport(&serial2, config.serialport2) == ERROR){
|
||||
writesyslog(LOG_ERR, "Serialport 2 error");
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
serial2.reader = 2;
|
||||
|
||||
if(server_send_log(&config, STARTMSG)){
|
||||
char *log;
|
||||
if(asprintf(&log, "Server error log writing : %s", config.serveraddress) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
printf("Writeserverlog failed :%s \n", config.serveraddress);
|
||||
}else{
|
||||
if(!server_send_log(&config, STARTMSG)){
|
||||
char *log;
|
||||
if(asprintf(&log, "Connection with server : %s", config.serveraddress) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
writesyslog(LOG_INFO, log);
|
||||
}
|
||||
free(log);
|
||||
printf("Connection with server :%s \n", config.serveraddress);
|
||||
}
|
||||
|
||||
//start threads
|
||||
@@ -153,24 +124,38 @@ int main(int argc, char **argv){
|
||||
pthread_mutex_init(&sharedbuffermutex, NULL);
|
||||
pthread_cond_init (&sharedbuffersignal, NULL);
|
||||
|
||||
int ret = pthread_create( &watchthread, NULL, serialdatawatch, (void*) &config);
|
||||
int ret = pthread_create(&watchthread, NULL, serialdatawatch, (void*) &config);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
char *log;
|
||||
if(asprintf(&log, "Error - watchthread pthread_create() return code: %d", ret) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
writesyslog(LOG_ERR, "Error - watchthread pthread_create()");
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
ret = pthread_create( &serialthread1, NULL, readserialport, (void*) &serial1);
|
||||
ret = pthread_create(&serialthread1, NULL, readserialport, (void*) &serial1);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
char *log;
|
||||
if(asprintf(&log, "Error - serialthread1 pthread_create() return code: %d", ret) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
ret = pthread_create( &serialthread2, NULL, readserialport, (void*) &serial2);
|
||||
ret = pthread_create(&serialthread2, NULL, readserialport, (void*) &serial2);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
char *log;
|
||||
if(asprintf(&log, "Error - serialthread2 pthread_create() return code: %d", ret) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
ret = pthread_create( &inputsthread, NULL, readinputs, (void*) &config);
|
||||
ret = pthread_create(&inputsthread, NULL, readinputs, (void*) &config);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
char *log;
|
||||
if(asprintf(&log, "Error - inputsthread pthread_create() return code: %d", ret) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -179,32 +164,7 @@ int main(int argc, char **argv){
|
||||
pthread_join(serialthread2, NULL);
|
||||
pthread_join(inputsthread, NULL);
|
||||
|
||||
//
|
||||
do {
|
||||
printf("can not happen\n");
|
||||
|
||||
//relay switch action
|
||||
//if(relayswitchflag == 1){
|
||||
//if(gpio_switchrelay(RELAY_OFF, (unsigned int)config.relay, config.relayinvert) == ERROR){
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
//}
|
||||
// if(mf700_controlcommand(&serial1.fd, EVERYLEDOFF) == ERROR){
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
// }
|
||||
// if(mf700_controlcommand(&serial2.fd, EVERYLEDOFF) == ERROR){
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
// }
|
||||
// server_send_lockstate(&config, LOCKED);
|
||||
// relayswitchflag = 0;
|
||||
// dooropenflag = 0;
|
||||
//}
|
||||
|
||||
//display timer
|
||||
//if(displaytime == 0){
|
||||
// clear_display();
|
||||
// displaytime = -1;
|
||||
//}else if(displaytime > 0){
|
||||
// displaytime--;
|
||||
//}
|
||||
} while (1);
|
||||
}
|
||||
|
||||
141
mf700.c
141
mf700.c
@@ -1,4 +1,5 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -9,72 +10,112 @@
|
||||
#include "mf700.h"
|
||||
#include "serial.h"
|
||||
#include "log.h"
|
||||
#include "reader.h"
|
||||
|
||||
|
||||
void mf700_timer_handler(union sigval arg){
|
||||
//printf("mf700 timer_handler\n");
|
||||
//printf("mf700 sigval ptr %p\n", arg.sival_ptr);
|
||||
|
||||
int *serial = (int *)arg.sival_ptr;
|
||||
mf700_controlcommand(serial, 0, EVERYLEDOFF);
|
||||
free(serial);
|
||||
}
|
||||
|
||||
|
||||
void mf700_timer(unsigned int i, int *serial){
|
||||
timer_t timer_id;
|
||||
struct itimerspec ts;
|
||||
struct sigevent se;
|
||||
|
||||
int *ptr = malloc(sizeof(int *));
|
||||
*ptr = *serial;
|
||||
|
||||
//printf("serial pointer: %p\n", (void *)serial);
|
||||
//printf("serial data: %d\n", *serial);
|
||||
//printf("ptr pointer: %p\n", (void *)ptr);
|
||||
//printf("ptr data: %d\n", *ptr);
|
||||
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
se.sigev_value.sival_ptr = ptr;
|
||||
se.sigev_notify_function = mf700_timer_handler;
|
||||
se.sigev_notify_attributes = NULL;
|
||||
|
||||
ts.it_value.tv_sec = (long int)i;
|
||||
ts.it_value.tv_nsec = (long int)0;
|
||||
ts.it_interval.tv_sec = 0;
|
||||
ts.it_interval.tv_nsec = 0;
|
||||
|
||||
int status = timer_create(CLOCK_REALTIME, &se, &timer_id);
|
||||
if (status == -1)
|
||||
printf("error create mf700 timer\n");
|
||||
|
||||
status = timer_settime(timer_id, 0, &ts, 0);
|
||||
if (status == -1)
|
||||
printf("error set mf700 timer\n");
|
||||
}
|
||||
|
||||
|
||||
int mf700_controlcommand(int *serial, unsigned int time, mf700controlset command){
|
||||
int mf700_writecommand(struct reader_t *reader, mf700controlset command){
|
||||
char cmd[4];
|
||||
cmd[0] = STX;
|
||||
cmd[1] = 'J';
|
||||
cmd[2] = command;
|
||||
cmd[3] = '\r';
|
||||
|
||||
ssize_t wlen = write(*serial, cmd, 4);
|
||||
ssize_t wlen = write(reader->fd, cmd, 4);
|
||||
|
||||
if (wlen != 4) {
|
||||
char *log;
|
||||
if(asprintf(&log, "Error from write to mf700: %ld : %d", (long int)wlen, errno) > 0 ){
|
||||
if(asprintf(&log, "Error writing to mf700: %ld : %d", (long int)wlen, errno) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
return(-1);
|
||||
}
|
||||
//usleep(5000); //little sleep between commands
|
||||
if(time > 0){
|
||||
mf700_timer(time, serial);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static volatile int reader1timersemaphore = 0;
|
||||
static volatile int reader2timersemaphore = 0;
|
||||
|
||||
void mf700_timer_handler(union sigval arg){
|
||||
struct reader_t *reader = (struct reader_t *)arg.sival_ptr;
|
||||
|
||||
if(reader->reader == 1){
|
||||
reader1timersemaphore -= 1;
|
||||
if(reader1timersemaphore == 0){
|
||||
mf700_writecommand(reader, EVERYLEDOFF);
|
||||
}
|
||||
}
|
||||
if(reader->reader == 2){
|
||||
reader2timersemaphore -= 1;
|
||||
if(reader2timersemaphore == 0){
|
||||
mf700_writecommand(reader, EVERYLEDOFF);
|
||||
}
|
||||
}
|
||||
|
||||
return(0);
|
||||
free(reader);
|
||||
}
|
||||
|
||||
void mf700_timer(long int time, struct reader_t *reader){
|
||||
timer_t timer_id;
|
||||
struct itimerspec ts;
|
||||
struct sigevent se;
|
||||
|
||||
struct reader_t *ptr = malloc(sizeof(struct reader_t));
|
||||
*ptr = *reader;
|
||||
|
||||
memset(&ts, 0, sizeof(struct itimerspec));
|
||||
memset(&se, 0, sizeof(struct sigevent));
|
||||
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
se.sigev_value.sival_ptr = ptr;
|
||||
se.sigev_notify_function = mf700_timer_handler;
|
||||
se.sigev_notify_attributes = NULL;
|
||||
|
||||
ts.it_value.tv_sec = time;
|
||||
ts.it_value.tv_nsec = (long int)0;
|
||||
ts.it_interval.tv_sec = 0;
|
||||
ts.it_interval.tv_nsec = 0;
|
||||
|
||||
int status = timer_create(CLOCK_REALTIME, &se, &timer_id);
|
||||
if (status == -1){
|
||||
char *log;
|
||||
if(asprintf(&log, "Error creating mf700 timer: %d", errno) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
return;
|
||||
}
|
||||
|
||||
status = timer_settime(timer_id, 0, &ts, 0);
|
||||
if (status == -1){
|
||||
char *log;
|
||||
if(asprintf(&log, "Error setting mf700 timer: %d", errno) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
return;
|
||||
}
|
||||
|
||||
if(reader->reader == 1)
|
||||
reader1timersemaphore += 1;
|
||||
if(reader->reader == 2)
|
||||
reader2timersemaphore += 1;
|
||||
}
|
||||
|
||||
|
||||
int mf700_controlcommand(struct reader_t *reader, long int time, mf700controlset command){
|
||||
|
||||
if( mf700_writecommand(reader, command) == -1){
|
||||
return -1;
|
||||
}
|
||||
|
||||
usleep(5000); //little sleep between commands
|
||||
|
||||
if(time <= 0){
|
||||
mf700_timer(1, reader);
|
||||
}else{
|
||||
mf700_timer(time, reader);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
4
mf700.h
4
mf700.h
@@ -1,6 +1,7 @@
|
||||
#ifndef MF700_FILE
|
||||
#define MF700_FILE
|
||||
|
||||
#include "reader.h"
|
||||
|
||||
typedef enum {
|
||||
EVERYLEDOFF = 0x30,
|
||||
@@ -15,7 +16,6 @@ typedef enum {
|
||||
EVERYLEDON = 0x39,
|
||||
}mf700controlset;
|
||||
|
||||
//void mf700_timer(unsigned int i, int *serial);
|
||||
int mf700_controlcommand(int *serial, unsigned int time, mf700controlset command);
|
||||
int mf700_controlcommand(struct reader_t *reader, long int time, mf700controlset command);
|
||||
|
||||
#endif
|
||||
|
||||
89
reader.c
89
reader.c
@@ -19,24 +19,20 @@
|
||||
#include "inputs.h"
|
||||
#include "reader.h"
|
||||
#include "relay.h"
|
||||
#include "display.h"
|
||||
|
||||
void readerrequest(struct reader_t *reader, struct config_t *config){
|
||||
server_credential_request(reader, config);
|
||||
|
||||
#ifdef TEST
|
||||
//printf("retdata Device: %s\n", retdata.device);
|
||||
//printf("retdata UID: %s\n", retdata.uid);
|
||||
printf("reader: %d\n", reader->reader);
|
||||
printf("Cardnumber: %d\n", reader->cardnumber);
|
||||
//printf("retdata Sectorkey: %s\n", retdata.sectorkey);
|
||||
printf("Opentime: %d\n", reader->opentime);
|
||||
printf("State: %d\n", reader->state);
|
||||
printf("server: %s\n", reader->config->serveraddress);
|
||||
#endif
|
||||
|
||||
int state = reader->state;
|
||||
|
||||
if(state == ACCESSGRANTED){
|
||||
if(mf700_controlcommand(&reader->fd, (unsigned int)reader->opentime, GREENLED1BEEP) == ERROR){
|
||||
if(reader->state == ACCESSGRANTED){
|
||||
if(mf700_controlcommand(reader, reader->opentime, GREENLED1BEEP) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
if(relay_switch(RELAY_ON, reader->opentime, config) == ERROR){
|
||||
@@ -44,93 +40,40 @@ void readerrequest(struct reader_t *reader, struct config_t *config){
|
||||
}
|
||||
|
||||
char *log;
|
||||
if(asprintf(&log, "CARD ID %d", reader->cardnumber) > 0){
|
||||
if(asprintf(&log, "CARD ID %d - %d sec", reader->cardnumber, reader->opentime) > 0){
|
||||
clear_display();
|
||||
write_display(ln[1], 0, 0, log);
|
||||
write_display(ln[2], 0, 0, "access granted");
|
||||
display_timer(reader->opentime);
|
||||
write_display(1, 0, 0, log, reader->opentime);
|
||||
write_display(2, 0, 0, "access granted", reader->opentime);
|
||||
}
|
||||
free(log);
|
||||
if(asprintf(&log, "%d - access granted", reader->cardnumber) > 0 ){
|
||||
if(asprintf(&log, "Card %d - access granted - %d sec", reader->cardnumber, reader->opentime) > 0 ){
|
||||
writesyslog(LOG_INFO, log);
|
||||
}
|
||||
free(log);
|
||||
}
|
||||
else if(state == ACCESSDENIED){
|
||||
if(mf700_controlcommand(&reader->fd, 2, REDLED3BEEPS) == ERROR){
|
||||
else if(reader->state == ACCESSDENIED){
|
||||
if(mf700_controlcommand(reader, 2, REDLED3BEEPS) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char *log;
|
||||
if(asprintf(&log, "CARD ID %d", reader->cardnumber) > 0){
|
||||
clear_display();
|
||||
write_display(ln[1], 0, 0, log);
|
||||
write_display(ln[2], 0, 0, "access denied");
|
||||
display_timer(3);
|
||||
write_display(1, 0, 0, log, 3);
|
||||
write_display(2, 0, 0, "access denied", 3);
|
||||
}
|
||||
free(log);
|
||||
if(asprintf(&log, "%d - access denied", reader->cardnumber) > 0 ){
|
||||
if(asprintf(&log, "Card %d - access denied", reader->cardnumber) > 0 ){
|
||||
writesyslog(LOG_INFO, log);
|
||||
}
|
||||
free(log);
|
||||
}
|
||||
else if (state == UNKNOWNERROR || state == CONNECTIONTIMEOUT || state == RESOLVEERROR || state == SERVERERROR || state == CREDENTIALERROR){
|
||||
if(mf700_controlcommand(&reader->fd, 0, EVERYLEDON) == ERROR){
|
||||
else{
|
||||
if(mf700_controlcommand(reader, 0, EVERYLEDON) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
if(mf700_controlcommand(&reader->fd, 5, BUZZER3TIME) == ERROR){
|
||||
if(mf700_controlcommand(reader, 5, BUZZER3TIME) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char *log;
|
||||
char *ln1, *ln2;
|
||||
int len = 0;
|
||||
switch(state){
|
||||
case UNKNOWNERROR:
|
||||
len = asprintf(&log, "Unknown error");
|
||||
asprintf(&ln1, "Unknown error");
|
||||
asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case CONNECTIONTIMEOUT:
|
||||
len = asprintf(&log, "Connection timeout to server: %s", config->serveraddress);
|
||||
asprintf(&ln1, "Connection timout");
|
||||
asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case RESOLVEERROR:
|
||||
len = asprintf(&log, "Can't resolve server address: %s", config->serveraddress);
|
||||
asprintf(&ln1, "Resolve error");
|
||||
asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case SERVERERROR:
|
||||
len = asprintf(&log, "Error in ACS server: %s", config->serveraddress);
|
||||
asprintf(&ln1, "Error in server");
|
||||
asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case CREDENTIALERROR:
|
||||
len = asprintf(&log, "Error in credential: %s", config->serveraddress);
|
||||
asprintf(&ln1, "Error in credential");
|
||||
asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
default:
|
||||
len = asprintf(&log, "switch default - server errors");
|
||||
asprintf(&ln1, "Switch default server");
|
||||
asprintf(&ln2, "cannot happen !");
|
||||
}
|
||||
if(len > 0){
|
||||
clear_display();
|
||||
write_display(ln[1], 0, 0, ln1);
|
||||
write_display(ln[2], 0, 0, ln2);
|
||||
writesyslog(LOG_ERR, log);
|
||||
display_timer(5);
|
||||
}
|
||||
free(ln1);
|
||||
free(ln2);
|
||||
free(log);
|
||||
}else{
|
||||
char *log;
|
||||
if(asprintf(&log, "Unknown state from server: %d\n", state) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
}
|
||||
}
|
||||
|
||||
3
reader.h
3
reader.h
@@ -1,12 +1,13 @@
|
||||
#ifndef READER_FILE
|
||||
#define READER_FILE
|
||||
#include <stdint.h>
|
||||
|
||||
struct reader_t{
|
||||
int fd;
|
||||
unsigned char buff[80];
|
||||
int reader;
|
||||
int state;
|
||||
int opentime;
|
||||
int32_t opentime;
|
||||
int cardnumber;
|
||||
struct config_t *config;
|
||||
};
|
||||
|
||||
136
relay.c
136
relay.c
@@ -9,7 +9,6 @@
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include "gpio.h"
|
||||
#include "log.h"
|
||||
@@ -18,81 +17,112 @@
|
||||
#include "relay.h"
|
||||
#include "jsontypes.h"
|
||||
#include "web.h"
|
||||
#include "global.h"
|
||||
|
||||
|
||||
int relay_set_state(int state, struct config_t *config){
|
||||
if(state == RELAY_ON){
|
||||
doorlockedflag = ACS_FALSE;
|
||||
if (config->relayinvert == cfg_false){
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - HIGH\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), HIGH) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
else{
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - LOW\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), LOW) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
server_send_lockstate(config, UNLOCKED);
|
||||
}
|
||||
else{ //RELAY_OFF
|
||||
if (config->relayinvert == cfg_false){
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - LOW\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), LOW) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
else{
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - HIGH\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), HIGH) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
server_send_lockstate(config, LOCKED);
|
||||
doorlockedflag = ACS_TRUE;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
static volatile int relaytimersemaphore = 0;
|
||||
|
||||
void relay_timer_handler(union sigval arg){
|
||||
struct config_t *config = (struct config_t *)arg.sival_ptr;
|
||||
//printf("relay: %ld\n", config->relay);
|
||||
//printf("relay invert: %d\n", config->relayinvert);
|
||||
relay_switch(RELAY_OFF, 0, config);
|
||||
//server_send_lockstate(config, UNLOCKED);
|
||||
|
||||
relaytimersemaphore -= 1;
|
||||
if(relaytimersemaphore == 0){
|
||||
relay_set_state(RELAY_OFF, config);
|
||||
}
|
||||
}
|
||||
|
||||
void relay_timer(int time, struct config_t *config){
|
||||
void relay_timer(long int time, struct config_t *config){
|
||||
timer_t timer_id;
|
||||
struct itimerspec ts;
|
||||
struct sigevent se;
|
||||
|
||||
memset(&ts, 0, sizeof(struct itimerspec));
|
||||
memset(&se, 0, sizeof(struct sigevent));
|
||||
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
se.sigev_value.sival_ptr = config;
|
||||
|
||||
se.sigev_notify_function = relay_timer_handler;
|
||||
se.sigev_notify_attributes = NULL;
|
||||
|
||||
ts.it_value.tv_sec = time;
|
||||
ts.it_value.tv_nsec = 0;
|
||||
ts.it_value.tv_nsec = (long int)0;
|
||||
ts.it_interval.tv_sec = 0;
|
||||
ts.it_interval.tv_nsec = 0;
|
||||
|
||||
int status = timer_create(CLOCK_REALTIME, &se, &timer_id);
|
||||
if (status == -1)
|
||||
printf("error create relay timer\n");
|
||||
if (status == -1){
|
||||
char *log;
|
||||
if(asprintf(&log, "Error creating relay timer: %d", errno) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
return;
|
||||
}
|
||||
|
||||
status = timer_settime(timer_id, 0, &ts, 0);
|
||||
if (status == -1)
|
||||
printf("error set relay timer\n");
|
||||
if (status == -1){
|
||||
char *log;
|
||||
if(asprintf(&log, "Error setting relay timer: %d", errno) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
return;
|
||||
}
|
||||
|
||||
relaytimersemaphore += 1;
|
||||
}
|
||||
|
||||
int relay_switch(int state, int time, struct config_t *config){
|
||||
if(state == RELAY_ON){
|
||||
server_send_lockstate(config, UNLOCKED);
|
||||
if (config->relayinvert == cfg_false){
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - HIGH\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), HIGH) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
else{
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - LOW\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), LOW) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
int relay_switch(int state, long int time, struct config_t *config){
|
||||
if(time <= 0){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
server_send_lockstate(config, LOCKED);
|
||||
if (config->relayinvert == cfg_false){
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - LOW\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), LOW) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
else{
|
||||
#ifdef TEST
|
||||
printf("Relay %ld - HIGH\n", config->relay);
|
||||
#endif
|
||||
if(GPIOWrite(get_relaypin((unsigned int)config->relay), HIGH) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
if(relay_set_state(state, config) != 0){
|
||||
return -1;
|
||||
}
|
||||
if(time > 0){
|
||||
relay_timer(time, config);
|
||||
}
|
||||
return(0);
|
||||
relay_timer(time, config);
|
||||
return 0;
|
||||
}
|
||||
|
||||
6
relay.h
6
relay.h
@@ -1,10 +1,6 @@
|
||||
#ifndef RELAY_FILE
|
||||
#define RELAY_FILE
|
||||
|
||||
|
||||
|
||||
//void starttimer(unsigned int switchtime);
|
||||
|
||||
int relay_switch(int state, int time, struct config_t *config);
|
||||
int relay_switch(int state, long int time, struct config_t *config);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,7 +17,9 @@ void exitprogram(int status){
|
||||
close(pid_file);
|
||||
|
||||
writesyslog(LOG_INFO, "Goodbye and thanks for all the fish");
|
||||
#ifdef TEST
|
||||
printf("bye....\n");
|
||||
#endif
|
||||
exit(status);
|
||||
}
|
||||
|
||||
@@ -27,7 +29,6 @@ void createpidfile(const char *location){
|
||||
if(fl) {
|
||||
if(EWOULDBLOCK == errno){
|
||||
writesyslog(LOG_ERR, "Attempting to run duplicate instance of acsreader");
|
||||
printf("Attempting to run duplicate instance of acsreader");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ devid=$(cat /sys/class/net/eth0/address | sed 's/://g' | tr a-z A-Z | tail -c 7)
|
||||
case "$(pidof $service | wc -w)" in
|
||||
0) #echo "service stooped case"
|
||||
#Send status that the service is not started.
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":2}' https://$serveraddress/api/status)
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":2}' https://$serveraddress/v2/api/status)
|
||||
|
||||
#Try to restart the service
|
||||
/etc/init.d/S51$service start
|
||||
@@ -22,16 +22,16 @@ case "$(pidof $service | wc -w)" in
|
||||
#Check if restart was successfully
|
||||
case "$(pidof $service | wc -w)" in
|
||||
0) #echo "service not started"
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":4}' https://$serveraddress/api/status)
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":4}' https://$serveraddress/v2/api/status)
|
||||
;;
|
||||
1) #echo "service started"
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":3}' https://$serveraddress/api/status)
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":3}' https://$serveraddress/v2/api/status)
|
||||
;;
|
||||
esac
|
||||
|
||||
;;
|
||||
1) #echo "service running case"
|
||||
#Send status that the service is running.
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":1}' https://$serveraddress/api/status)
|
||||
$(curl -X POST -H "Content-Type: application/json" -d '{"Device":"'"$devid"'","Name":"'"$devicename"'","ProductClass":"'"$productclass"'","Status":1}' https://$serveraddress/v2/api/status)
|
||||
;;
|
||||
esac
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
dateext
|
||||
dateformat -%Y%m%d
|
||||
lastaction
|
||||
/etc/init.d/S01logging restart
|
||||
/etc/init.d/S01syslogd restart
|
||||
/etc/init.d/S51acsreader restart
|
||||
endscript
|
||||
}
|
||||
|
||||
21
serial.c
21
serial.c
@@ -65,7 +65,6 @@ int openserialport(struct serialport_t *serial, char *serialpath){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
printf("Serial port error %s\n", serialpath);
|
||||
return -1;
|
||||
}
|
||||
serial_interfaceattribs(&serial->fd, BAUDRATE);
|
||||
@@ -114,19 +113,6 @@ void *serialdatawatch(void *ptr){
|
||||
//wait for signal from serial read
|
||||
pthread_cond_wait(&sharedbuffersignal, &sharedbuffermutex);
|
||||
|
||||
//printf("buffer watch %s\n", sharedbuffer.buff);
|
||||
//printf("reader %d\n", sharedbuffer.reader);
|
||||
//printf("server = %s\n", config->serveraddress);
|
||||
//procesReaderData(buffer, &config, &serial->fd);
|
||||
|
||||
//struct reader_t *reader = malloc(sizeof(struct reader_t));
|
||||
//reader->fd = sharedbuffer.fd;
|
||||
//reader->reader = sharedbuffer.reader;
|
||||
//reader->config = config;
|
||||
//memcpy(reader->buff, sharedbuffer.buff, sizeof(reader->buff));
|
||||
|
||||
//readerrequest(reader, config);
|
||||
|
||||
struct reader_t reader;
|
||||
reader.fd = sharedbuffer.fd;
|
||||
reader.reader = sharedbuffer.reader;
|
||||
@@ -135,15 +121,8 @@ void *serialdatawatch(void *ptr){
|
||||
|
||||
readerrequest(&reader, config);
|
||||
|
||||
//if(mf700_controlcommand(&sharedbuffer.fd, GREENLED1BEEP) == ERROR){
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
//}
|
||||
//if(gpio_switchrelay(RELAY_ON, (unsigned int)config->relay, config->relayinvert) == ERROR){
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
//}
|
||||
//remove all credential data from buffer
|
||||
memset(&sharedbuffer, 0, sizeof(struct serialport_t));
|
||||
//free(reader);
|
||||
|
||||
pthread_mutex_unlock(&sharedbuffermutex);
|
||||
}
|
||||
|
||||
4
serial.h
4
serial.h
@@ -1,6 +1,8 @@
|
||||
#ifndef SERIAL_FILE
|
||||
#define SERIAL_FILE
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#define STX '\2'
|
||||
#define ETX '\3'
|
||||
#define ACK '\x6'
|
||||
@@ -12,11 +14,9 @@
|
||||
struct serialport_t{
|
||||
int fd;
|
||||
unsigned char buff[80];
|
||||
//unsigned int idx;
|
||||
int reader;
|
||||
};
|
||||
|
||||
//char sharedbuffer[80];
|
||||
pthread_mutex_t sharedbuffermutex;
|
||||
pthread_cond_t sharedbuffersignal;
|
||||
|
||||
|
||||
199
web.c
199
web.c
@@ -11,7 +11,8 @@
|
||||
#include "log.h"
|
||||
#include "jsontypes.h"
|
||||
#include "reader.h"
|
||||
|
||||
#include "display.h"
|
||||
#include "global.h"
|
||||
|
||||
//------------------- Web request Callback -------------------
|
||||
struct memory_t {
|
||||
@@ -66,6 +67,11 @@ void web_request_json(char *url, const char *json, struct json_object **jsonobj)
|
||||
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); //send all data to this function
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); //we pass our 'chunk' struct to the callback function
|
||||
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 5); // complete connection within 5 seconds
|
||||
curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, ACS_TRUE); //fail on error code >= 400
|
||||
char errbuf[CURL_ERROR_SIZE];
|
||||
curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errbuf);
|
||||
errbuf[0] = '\0';
|
||||
|
||||
CURLcode res = curl_easy_perform(curl_handle);
|
||||
|
||||
@@ -74,10 +80,28 @@ void web_request_json(char *url, const char *json, struct json_object **jsonobj)
|
||||
}
|
||||
else{ //curl returned a error
|
||||
char *log;
|
||||
if(asprintf(&log, "web_request: curl failed: %s\n", curl_easy_strerror(res)) > 0 ){
|
||||
if(asprintf(&log, "web_request: curl failed: %d : %s\n", res, curl_easy_strerror(res)) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
writesyslog(LOG_ERR, errbuf);
|
||||
|
||||
if(res == CURLE_COULDNT_RESOLVE_HOST){
|
||||
*jsonobj = json_object_new_object();
|
||||
json_object_object_add(*jsonobj, "State", json_object_new_int(RESOLVEERROR));
|
||||
}else if(res == CURLE_OPERATION_TIMEDOUT){
|
||||
*jsonobj = json_object_new_object();
|
||||
json_object_object_add(*jsonobj, "State", json_object_new_int(CONNECTIONTIMEOUT));
|
||||
}else if(res == CURLE_HTTP_RETURNED_ERROR){
|
||||
*jsonobj = json_object_new_object();
|
||||
json_object_object_add(*jsonobj, "State", json_object_new_int(SERVERERROR));
|
||||
}else if(res == CURLE_PEER_FAILED_VERIFICATION){
|
||||
*jsonobj = json_object_new_object();
|
||||
json_object_object_add(*jsonobj, "State", json_object_new_int(SERVERERROR));
|
||||
}else{
|
||||
*jsonobj = json_object_new_object();
|
||||
json_object_object_add(*jsonobj, "State", json_object_new_int(UNKNOWNERROR));
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup curl stuff */
|
||||
@@ -87,20 +111,90 @@ void web_request_json(char *url, const char *json, struct json_object **jsonobj)
|
||||
/* we're done with libcurl, so clean it up */
|
||||
curl_global_cleanup();
|
||||
}
|
||||
//----------------------- Print Errors --------------------------------------
|
||||
|
||||
void server_display_error(returnstate_t state, struct config_t *config){
|
||||
char *log;
|
||||
char *ln1, *ln2;
|
||||
int len = 0;
|
||||
int ret1, ret2;
|
||||
switch(state){
|
||||
case UNKNOWNERROR:
|
||||
len = asprintf(&log, "Unknown error");
|
||||
ret1 = asprintf(&ln1, "Unknown error");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case CONNECTIONTIMEOUT:
|
||||
len = asprintf(&log, "Connection timeout to server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Connection timout");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case RESOLVEERROR:
|
||||
len = asprintf(&log, "Can't resolve server address: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Resolve error");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case SERVERERROR:
|
||||
len = asprintf(&log, "Error in ACS server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Error in server");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case CREDENTIALERROR:
|
||||
len = asprintf(&log, "Error in credential on server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Error in credential");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case LOGERROR:
|
||||
len = asprintf(&log, "Error in log on server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Error in log");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case LOCKSTATEERROR:
|
||||
len = asprintf(&log, "Error in lockstate on server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Error in lockstate");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case OPENTIMEERROR:
|
||||
len = asprintf(&log, "Error in opentime on server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Error in opentime");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
case DEVICEINACTIVE:
|
||||
len = asprintf(&log, "Device not active on server: %s", config->serveraddress);
|
||||
ret1 = asprintf(&ln1, "Device not active");
|
||||
ret2 = asprintf(&ln2, "%s", config->serveraddress);
|
||||
break;
|
||||
default:
|
||||
len = asprintf(&log, "switch default - server errors");
|
||||
ret1 = asprintf(&ln1, "Switch default server");
|
||||
ret2 = asprintf(&ln2, "cannot happen !");
|
||||
}
|
||||
if(len > 0){
|
||||
clear_display();
|
||||
write_display(1, 0, 0, ln1, DISPLAY_TIME);
|
||||
write_display(2, 0, 0, ln2, DISPLAY_TIME);
|
||||
//display_timer(15);
|
||||
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(ln1);
|
||||
free(ln2);
|
||||
free(log);
|
||||
}
|
||||
|
||||
|
||||
//----------------------- Request functions ----------------------------------
|
||||
|
||||
void server_credential_request(struct reader_t *reader, struct config_t *config){
|
||||
char *url;
|
||||
int len = asprintf(&url, "https://%s/api/CredentialRequest", config->serveraddress);
|
||||
int len = asprintf(&url, "https://%s/v2/api/CredentialRequest", config->serveraddress);
|
||||
|
||||
struct json_object *sendobj;
|
||||
sendobj = json_object_new_object();
|
||||
json_object_object_add(sendobj, "Device", json_object_new_string(config->device));
|
||||
json_object_object_add(sendobj, "Name", json_object_new_string(config->name));
|
||||
json_object_object_add(sendobj, "ReaderData", json_object_new_string((const char *)reader->buff));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int64(config->ProductClass));
|
||||
json_object_object_add(sendobj, "Reader", json_object_new_int64(reader->reader));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int((int32_t)config->ProductClass));
|
||||
json_object_object_add(sendobj, "Reader", json_object_new_int(reader->reader));
|
||||
|
||||
struct json_object *jsonobj = NULL;
|
||||
web_request_json(url, json_object_to_json_string(sendobj), &jsonobj);
|
||||
@@ -108,26 +202,23 @@ void server_credential_request(struct reader_t *reader, struct config_t *config)
|
||||
#ifdef TEST
|
||||
printf("Server answer: %s\n", json_object_to_json_string(jsonobj));
|
||||
#endif
|
||||
|
||||
reader->state = 0;
|
||||
if(jsonobj != NULL){
|
||||
struct json_object *tmpobj;
|
||||
tmpobj = json_object_new_object();
|
||||
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == TRUE && tmpobj != NULL) {
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
reader->state = json_object_get_int(tmpobj);
|
||||
}
|
||||
else{
|
||||
reader->state = UNKNOWNERROR;
|
||||
}
|
||||
|
||||
if(json_object_object_get_ex(jsonobj, "OpenTime", &tmpobj) == TRUE && tmpobj != NULL) {
|
||||
if(json_object_object_get_ex(jsonobj, "OpenTime", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
reader->opentime = json_object_get_int(tmpobj);
|
||||
}
|
||||
else{
|
||||
reader->opentime = 0;
|
||||
}
|
||||
|
||||
if(json_object_object_get_ex(jsonobj, "CardNumber", &tmpobj) == TRUE && tmpobj != NULL) {
|
||||
if(json_object_object_get_ex(jsonobj, "CardNumber", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
reader->cardnumber = json_object_get_int(tmpobj);
|
||||
}
|
||||
else{
|
||||
@@ -136,20 +227,25 @@ void server_credential_request(struct reader_t *reader, struct config_t *config)
|
||||
|
||||
json_object_put(tmpobj); //delete json object
|
||||
}
|
||||
if(reader->state != ACCESSGRANTED && reader->state != ACCESSDENIED){
|
||||
server_display_error(reader->state, config);
|
||||
}
|
||||
|
||||
json_object_put(sendobj); //delete json object
|
||||
free(url);
|
||||
}
|
||||
|
||||
//****************** LockState *******************************
|
||||
void server_send_lockstate(struct config_t *config, lockstates_t lockstate){
|
||||
char *url;
|
||||
int len = asprintf(&url, "https://%s/api/LockState", config->serveraddress);
|
||||
int len = asprintf(&url, "https://%s/v2/api/LockState", config->serveraddress);
|
||||
|
||||
struct json_object *sendobj;
|
||||
sendobj = json_object_new_object();
|
||||
json_object_object_add(sendobj, "Device", json_object_new_string(config->device));
|
||||
json_object_object_add(sendobj, "Name", json_object_new_string(config->name));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int64(config->ProductClass));
|
||||
json_object_object_add(sendobj, "LockState", json_object_new_int64(lockstate));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int((int32_t)config->ProductClass));
|
||||
json_object_object_add(sendobj, "LockState", json_object_new_int(lockstate));
|
||||
|
||||
struct json_object *jsonobj = NULL;
|
||||
web_request_json(url, json_object_to_json_string(sendobj), &jsonobj);
|
||||
@@ -158,44 +254,38 @@ void server_send_lockstate(struct config_t *config, lockstates_t lockstate){
|
||||
printf("Server answer: %s\n", json_object_to_json_string(jsonobj));
|
||||
#endif
|
||||
|
||||
int state;
|
||||
returnstate_t state = 0;
|
||||
if(jsonobj != NULL){
|
||||
struct json_object *tmpobj;
|
||||
tmpobj = json_object_new_object();
|
||||
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == TRUE && tmpobj != NULL) {
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
state = json_object_get_int(tmpobj);
|
||||
}
|
||||
else{
|
||||
state = UNKNOWNERROR;
|
||||
}
|
||||
json_object_put(tmpobj); //delete json object
|
||||
}
|
||||
|
||||
if(state != LOCKSTATEOK){
|
||||
char *log;
|
||||
if(asprintf(&log, "ACS server lockstate writing error - %d", state) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
server_display_error(state, config);
|
||||
}
|
||||
|
||||
json_object_put(sendobj); //delete json object
|
||||
free(url);
|
||||
}
|
||||
|
||||
//****************** Add Log *******************************
|
||||
int server_send_log(struct config_t *config, const char *logtext){
|
||||
int ret = 0;
|
||||
char *url;
|
||||
|
||||
int len = asprintf(&url, "https://%s/api/addlog", config->serveraddress );
|
||||
int len = asprintf(&url, "https://%s/v2/api/addlog", config->serveraddress );
|
||||
|
||||
struct json_object *sendobj;
|
||||
sendobj = json_object_new_object();
|
||||
json_object_object_add(sendobj, "Device", json_object_new_string(config->device));
|
||||
json_object_object_add(sendobj, "Name", json_object_new_string(config->name));
|
||||
json_object_object_add(sendobj, "Message", json_object_new_string(logtext));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int64(config->ProductClass));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int((int32_t)config->ProductClass));
|
||||
|
||||
if(len > 0 ){
|
||||
struct json_object *jsonobj = NULL;
|
||||
@@ -205,26 +295,19 @@ int server_send_log(struct config_t *config, const char *logtext){
|
||||
printf("Server answer: %s\n", json_object_to_json_string(jsonobj));
|
||||
#endif
|
||||
|
||||
int state;
|
||||
returnstate_t state = 0;
|
||||
if(jsonobj != NULL){
|
||||
struct json_object *tmpobj;
|
||||
tmpobj = json_object_new_object();
|
||||
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == TRUE && tmpobj != NULL) {
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
state = json_object_get_int(tmpobj);
|
||||
}
|
||||
else{
|
||||
state = UNKNOWNERROR;
|
||||
}
|
||||
json_object_put(tmpobj); //delete json object
|
||||
}
|
||||
|
||||
if(state != LOGOK){
|
||||
char *log;
|
||||
if(asprintf(&log, "ACS server log writing error - %d", state) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
server_display_error(state, config);
|
||||
ret = -1;
|
||||
}
|
||||
}else{
|
||||
@@ -237,3 +320,43 @@ int server_send_log(struct config_t *config, const char *logtext){
|
||||
return(ret);
|
||||
}
|
||||
|
||||
//****************** get OpenTime *******************************
|
||||
int32_t server_get_opentime(struct config_t *config){
|
||||
char *url;
|
||||
int len = asprintf(&url, "https://%s/v2/api/opentime", config->serveraddress);
|
||||
|
||||
struct json_object *sendobj;
|
||||
sendobj = json_object_new_object();
|
||||
json_object_object_add(sendobj, "Device", json_object_new_string(config->device));
|
||||
json_object_object_add(sendobj, "Name", json_object_new_string(config->name));
|
||||
json_object_object_add(sendobj, "ProductClass", json_object_new_int((int32_t)config->ProductClass));
|
||||
|
||||
struct json_object *jsonobj = NULL;
|
||||
web_request_json(url, json_object_to_json_string(sendobj), &jsonobj);
|
||||
|
||||
#ifdef TEST
|
||||
printf("Server answer: %s\n", json_object_to_json_string(jsonobj));
|
||||
#endif
|
||||
|
||||
int32_t opentime = 0;
|
||||
returnstate_t state = 0;
|
||||
if(jsonobj != NULL){
|
||||
struct json_object *tmpobj;
|
||||
tmpobj = json_object_new_object();
|
||||
|
||||
if(json_object_object_get_ex(jsonobj, "State", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
state = json_object_get_int(tmpobj);
|
||||
}
|
||||
if(json_object_object_get_ex(jsonobj, "OpenTime", &tmpobj) == ACS_TRUE && tmpobj != NULL) {
|
||||
opentime = json_object_get_int(tmpobj);
|
||||
}
|
||||
json_object_put(tmpobj); //delete json object
|
||||
}
|
||||
if(state != OPENTIMEOK){
|
||||
server_display_error(state, config);
|
||||
}
|
||||
|
||||
json_object_put(sendobj); //delete json object
|
||||
free(url);
|
||||
return opentime;
|
||||
}
|
||||
|
||||
1
web.h
1
web.h
@@ -7,5 +7,6 @@
|
||||
void server_credential_request(struct reader_t *reader, struct config_t *config);
|
||||
void server_send_lockstate(struct config_t *config, lockstates_t lockstate);
|
||||
int server_send_log(struct config_t *config, const char *logtext);
|
||||
int32_t server_get_opentime(struct config_t *config);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user