read inputs from thread
This commit is contained in:
31
api.c
Normal file
31
api.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
#include <inttypes.h>
|
||||
#include <curl/curl.h>
|
||||
#include <json-c/json.h>
|
||||
#include "config.h"
|
||||
#include "web.h"
|
||||
#include "log.h"
|
||||
#include "jsontypes.h"
|
||||
|
||||
|
||||
//void credentialrequest(unsigned char *readerdata, struct config_t *config, struct json_object *jsonobj){
|
||||
//char *url;
|
||||
//int len = asprintf(&url, "https://%s/api/CredentialRequest", config->serveraddress);
|
||||
|
||||
//struct json_object *sendobj;
|
||||
//jsonobj = 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 *)readerdata));
|
||||
//json_object_object_add(sendobj, "ProductClass", json_object_new_int64(config->ProductClass));
|
||||
|
||||
//web_request_json(url, json_object_to_json_string(sendobj), retobj);
|
||||
|
||||
//json_object_put(sendobj); //delete json object
|
||||
//free(url);
|
||||
//}
|
||||
|
||||
6
global.h
Normal file
6
global.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef GLOBAL_FILE
|
||||
#define GLOBAL_FILE
|
||||
|
||||
#define ERROR -1
|
||||
|
||||
#endif
|
||||
31
gpio.c
31
gpio.c
@@ -281,32 +281,47 @@ int gpio_initinput(unsigned int input){
|
||||
if(GPIOExport(inputsysfs[i]) < 0){
|
||||
return(-1);
|
||||
}
|
||||
//if(GPIODirection(inputpin[i], IN) < 0){
|
||||
// return(-1);
|
||||
//}
|
||||
if(GPIODirection(inputpin[i], IN) < 0){
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
int input_fd_open(long int input){
|
||||
char *fl;
|
||||
char *fle, *flv;
|
||||
#ifdef TEST
|
||||
if(asprintf(&fl, "/sys/class/gpio/gpio%d/value", inputpin[input]) < 0 ){
|
||||
if(asprintf(&fle, "/sys/class/gpio/gpio%d/edge", inputpin[input]) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
if(asprintf(&flv, "/sys/class/gpio/gpio%d/value", inputpin[input]) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
#else
|
||||
if(asprintf(&fl, "/sys/class/gpio/pioA%d/value", inputpin[input]) < 0 ){
|
||||
if(asprintf(&flve, "/sys/class/gpio/pioA%d/edge", inputpin[input]) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
if(asprintf(&flv, "/sys/class/gpio/pioA%d/value", inputpin[input]) < 0 ){
|
||||
writesyslog(LOG_ERR, "asprintf failed");
|
||||
return(-1);
|
||||
}
|
||||
#endif
|
||||
|
||||
int fd = open(fl, O_RDONLY | O_NONBLOCK );
|
||||
int fd = open(fle, O_WRONLY);
|
||||
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");
|
||||
return(-1);
|
||||
}
|
||||
free(fl);
|
||||
free(fle);
|
||||
free(flv);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
82
inputs.c
Normal file
82
inputs.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
#include <string.h>
|
||||
#include <sys/file.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include "global.h"
|
||||
#include "runtime.h"
|
||||
#include "config.h"
|
||||
#include "gpio.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
void *readinputs(void *ptr){
|
||||
struct config_t *config;
|
||||
config = (struct config_t *) ptr;
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = input_fd_open(config->manualinput);;
|
||||
if(pfd.fd < 0){
|
||||
printf("manualinput failed\n");
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
pfd.events = POLLPRI;
|
||||
|
||||
while(1){
|
||||
lseek(pfd.fd, 0, SEEK_SET);
|
||||
int rc = poll(&pfd, 1, -1);
|
||||
if (rc < 0){
|
||||
if(errno != EINTR){
|
||||
char *log;
|
||||
if(asprintf(&log, "inputpoll failed: %s", strerror(errno)) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
}else if(rc > 0){
|
||||
unsigned char val;
|
||||
if(read(pfd.fd, &val, 1) < 0){
|
||||
char *log;
|
||||
if(asprintf(&log, "input read failed: %s", strerror(errno)) != 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if((val == '0' && config->inputlevel == 1) || (val == '1' && config->inputlevel == 0)){
|
||||
|
||||
//printf("switch\n");
|
||||
|
||||
if(gpio_switchrelay(RELAY_ON, (unsigned int)config->relay, config->relayinvert) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
// starttimer((unsigned int)config.switchtime);
|
||||
// set_displaytime(100);
|
||||
|
||||
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);
|
||||
|
||||
// if(dooropenflag == 0){
|
||||
//write one time to log
|
||||
// writesyslog(LOG_INFO, "Manual open");
|
||||
|
||||
// server_send_lockstate(&config, UNLOCKED);
|
||||
// writeserverlog(&config, "Manual open");
|
||||
// }
|
||||
// dooropenflag = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
inputs.h
Normal file
6
inputs.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef INPUTS_FILE
|
||||
#define INPUTS_FILE
|
||||
|
||||
void *readinputs(void *ptr);
|
||||
|
||||
#endif
|
||||
187
main.c
187
main.c
@@ -12,6 +12,7 @@
|
||||
#include <sys/file.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include "global.h"
|
||||
#include "runtime.h"
|
||||
#include "config.h"
|
||||
#include "web.h"
|
||||
@@ -21,8 +22,9 @@
|
||||
#include "log.h"
|
||||
#include "info.h"
|
||||
#include "jsontypes.h"
|
||||
#include "inputs.h"
|
||||
|
||||
#define ERROR -1
|
||||
//#define ERROR -1
|
||||
|
||||
#define STARTMSG "Starting acsreader version " VERSION
|
||||
|
||||
@@ -188,60 +190,6 @@ void procesReaderData(unsigned char *readerdata, struct config_t *config, int *s
|
||||
|
||||
struct serialport_t serial1, serial2;
|
||||
|
||||
char sharedbuffer[80];
|
||||
pthread_mutex_t sharedbuffermutex;
|
||||
pthread_cond_t sharedbuffersignal;
|
||||
|
||||
|
||||
void *watch_count(void *ptr){
|
||||
printf("Watch tread started\n");
|
||||
|
||||
struct config_t *config;
|
||||
config = (struct config_t *) ptr;
|
||||
|
||||
while(1) {
|
||||
pthread_mutex_lock(&sharedbuffermutex);
|
||||
|
||||
pthread_cond_wait(&sharedbuffersignal, &sharedbuffermutex);
|
||||
|
||||
printf("buffer watch %s\n", sharedbuffer);
|
||||
printf("server = %s\n", config->serveraddress);
|
||||
|
||||
//procesReaderData(buffer, &config, &serial->fd);
|
||||
|
||||
pthread_mutex_unlock(&sharedbuffermutex);
|
||||
}
|
||||
}
|
||||
|
||||
void *readserialport(void *ptr){
|
||||
unsigned char chr;
|
||||
struct serialport_t *serial;
|
||||
serial = (struct serialport_t *) ptr;
|
||||
while(1){
|
||||
ssize_t len1 = read(serial->fd, &chr, sizeof(chr));
|
||||
if (len1 > 0){
|
||||
if ((chr == '\r' || chr == '\n') && serial->idx == 72){
|
||||
serial->buff[serial->idx] = '\0';
|
||||
|
||||
pthread_mutex_lock(&sharedbuffermutex);
|
||||
memcpy(sharedbuffer, serial->buff, serial->idx);
|
||||
pthread_cond_signal(&sharedbuffersignal);
|
||||
pthread_mutex_unlock(&sharedbuffermutex);
|
||||
|
||||
serial->idx = 0;
|
||||
}else if (serial->idx > 72){
|
||||
serial->idx = 0;
|
||||
}else if (chr == ACK || chr == NACK ){
|
||||
serial->idx = 0;
|
||||
}else {
|
||||
serial->buff[serial->idx] = chr;
|
||||
serial->idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*********************************/
|
||||
/* MAIN */
|
||||
/**********************************/
|
||||
@@ -304,7 +252,6 @@ int main(int argc, char **argv){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
if(openserialport(&serial1, config.serialport1) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
@@ -329,114 +276,108 @@ int main(int argc, char **argv){
|
||||
}
|
||||
|
||||
//setup input poll
|
||||
struct pollfd fdset[1];
|
||||
fdset[0].fd = input_fd_open(config.manualinput);;
|
||||
if(fdset[0].fd < 0){
|
||||
printf("manualinput failed\n");
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
fdset[0].events = POLLPRI;
|
||||
//struct pollfd fdset[1];
|
||||
//fdset[0].fd = input_fd_open(config.manualinput);;
|
||||
//if(fdset[0].fd < 0){
|
||||
// printf("manualinput failed\n");
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
//}
|
||||
//fdset[0].events = POLLPRI;
|
||||
|
||||
serial1.idx = 0;
|
||||
serial2.idx = 0;
|
||||
|
||||
//start threads
|
||||
|
||||
pthread_t serialthread1, serialthread2, watchthread;
|
||||
pthread_t serialthread1, serialthread2, watchthread, inputsthread;
|
||||
|
||||
/* Initialize mutex and condition variable objects */
|
||||
pthread_mutex_init(&sharedbuffermutex, NULL);
|
||||
pthread_cond_init (&sharedbuffersignal, NULL);
|
||||
|
||||
int ret = pthread_create( &watchthread, NULL, watch_count, (void*) &config);
|
||||
int ret = pthread_create( &watchthread, NULL, serialdatawatch, (void*) &config);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
//printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
|
||||
ret = pthread_create( &serialthread1, NULL, readserialport, (void*) &serial1);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
//printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
|
||||
ret = pthread_create( &serialthread2, NULL, readserialport, (void*) &serial2);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
|
||||
|
||||
|
||||
//printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
|
||||
ret = pthread_create( &inputsthread, NULL, readinputs, (void*) &config);
|
||||
if(ret){
|
||||
printf("Error - pthread_create() return code: %d\n",ret);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
//printf("pthread_create() for thread 1 returns: %d\n",ret);
|
||||
|
||||
pthread_join(watchthread, NULL);
|
||||
pthread_join(serialthread1, NULL);
|
||||
pthread_join(serialthread2, NULL);
|
||||
pthread_join(inputsthread, NULL);
|
||||
|
||||
|
||||
//
|
||||
do {
|
||||
//serial read from mf700 reader on serialport1
|
||||
//readserialport(&serial1, &config);
|
||||
|
||||
//serial read from mf700 reader on serialport2
|
||||
//readserialport(&serial2, &config);
|
||||
|
||||
//int bytes;
|
||||
//ioctl(serial2.fd, FIONREAD, &bytes);
|
||||
//printf("bytes %d\n", bytes);
|
||||
|
||||
printf("loop\n");
|
||||
|
||||
//input poll
|
||||
int rc = poll(fdset, 1, 0);
|
||||
if (rc < 0){
|
||||
if(errno != EINTR){
|
||||
char *log;
|
||||
if(asprintf(&log, "inputpoll failed: %s", strerror(errno)) > 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
}else if(rc > 0){
|
||||
if (fdset[0].revents & POLLPRI) {
|
||||
lseek(fdset[0].fd, 0, SEEK_SET);
|
||||
unsigned char buf[3];
|
||||
if(read(fdset[0].fd, buf, 3) < 0){
|
||||
char *log;
|
||||
if(asprintf(&log, "input read failed: %s", strerror(errno)) != 0 ){
|
||||
writesyslog(LOG_ERR, log);
|
||||
}
|
||||
free(log);
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
if((buf[0] == '0' && config.inputlevel == 1) || (buf[0] == '1' && config.inputlevel == 0)){
|
||||
if(gpio_switchrelay(RELAY_ON, (unsigned int)config.relay, config.relayinvert) == ERROR){
|
||||
exitprogram(EXIT_FAILURE);
|
||||
}
|
||||
starttimer((unsigned int)config.switchtime);
|
||||
set_displaytime(100);
|
||||
//int rc = poll(fdset, 1, 0);
|
||||
//if (rc < 0){
|
||||
// if(errno != EINTR){
|
||||
// char *log;
|
||||
// if(asprintf(&log, "inputpoll failed: %s", strerror(errno)) > 0 ){
|
||||
// writesyslog(LOG_ERR, log);
|
||||
// }
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
// }
|
||||
//}else if(rc > 0){
|
||||
// if (fdset[0].revents & POLLPRI) {
|
||||
// lseek(fdset[0].fd, 0, SEEK_SET);
|
||||
// unsigned char buf[3];
|
||||
// if(read(fdset[0].fd, buf, 3) < 0){
|
||||
// char *log;
|
||||
// if(asprintf(&log, "input read failed: %s", strerror(errno)) != 0 ){
|
||||
// writesyslog(LOG_ERR, log);
|
||||
// }
|
||||
// free(log);
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
// }
|
||||
// if((buf[0] == '0' && config.inputlevel == 1) || (buf[0] == '1' && config.inputlevel == 0)){
|
||||
// if(gpio_switchrelay(RELAY_ON, (unsigned int)config.relay, config.relayinvert) == ERROR){
|
||||
// exitprogram(EXIT_FAILURE);
|
||||
// }
|
||||
// starttimer((unsigned int)config.switchtime);
|
||||
// set_displaytime(100);
|
||||
|
||||
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);
|
||||
// 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);
|
||||
|
||||
if(dooropenflag == 0){
|
||||
// if(dooropenflag == 0){
|
||||
//write one time to log
|
||||
writesyslog(LOG_INFO, "Manual open");
|
||||
// writesyslog(LOG_INFO, "Manual open");
|
||||
|
||||
server_send_lockstate(&config, UNLOCKED);
|
||||
writeserverlog(&config, "Manual open");
|
||||
}
|
||||
dooropenflag = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// server_send_lockstate(&config, UNLOCKED);
|
||||
// writeserverlog(&config, "Manual open");
|
||||
// }
|
||||
// dooropenflag = 1;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//relay switch action
|
||||
if(relayswitchflag == 1){
|
||||
|
||||
49
serial.c
49
serial.c
@@ -7,8 +7,10 @@
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
#include <pthread.h>
|
||||
#include "serial.h"
|
||||
#include "log.h"
|
||||
#include "config.h"
|
||||
|
||||
int serial_interfaceattribs(int *fd, int speed){
|
||||
struct termios tty;
|
||||
@@ -67,3 +69,50 @@ int openserialport(struct serialport_t *serial, char *serialpath){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//read serial port thread
|
||||
void *readserialport(void *ptr){
|
||||
unsigned char chr;
|
||||
struct serialport_t *serial;
|
||||
serial = (struct serialport_t *) ptr;
|
||||
while(1){
|
||||
ssize_t len1 = read(serial->fd, &chr, sizeof(chr));
|
||||
if (len1 > 0){
|
||||
if ((chr == '\r' || chr == '\n') && serial->idx == 72){
|
||||
serial->buff[serial->idx] = '\0';
|
||||
|
||||
pthread_mutex_lock(&sharedbuffermutex);
|
||||
memcpy(sharedbuffer, serial->buff, serial->idx);
|
||||
pthread_cond_signal(&sharedbuffersignal);
|
||||
pthread_mutex_unlock(&sharedbuffermutex);
|
||||
|
||||
serial->idx = 0;
|
||||
}else if (serial->idx > 72){
|
||||
serial->idx = 0;
|
||||
}else if (chr == ACK || chr == NACK ){
|
||||
serial->idx = 0;
|
||||
}else {
|
||||
serial->buff[serial->idx] = chr;
|
||||
serial->idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void *serialdatawatch(void *ptr){
|
||||
struct config_t *config;
|
||||
config = (struct config_t *) ptr;
|
||||
|
||||
while(1) {
|
||||
pthread_mutex_lock(&sharedbuffermutex);
|
||||
//wait for signal from serial read
|
||||
pthread_cond_wait(&sharedbuffersignal, &sharedbuffermutex);
|
||||
|
||||
printf("buffer watch %s\n", sharedbuffer);
|
||||
printf("server = %s\n", config->serveraddress);
|
||||
//procesReaderData(buffer, &config, &serial->fd);
|
||||
|
||||
memset(sharedbuffer, '\0', sizeof(sharedbuffer));
|
||||
pthread_mutex_unlock(&sharedbuffermutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
6
serial.h
6
serial.h
@@ -15,7 +15,13 @@ struct serialport_t{
|
||||
unsigned int idx;
|
||||
};
|
||||
|
||||
char sharedbuffer[80];
|
||||
pthread_mutex_t sharedbuffermutex;
|
||||
pthread_cond_t sharedbuffersignal;
|
||||
|
||||
int serial_interfaceattribs(int *fd, int speed);
|
||||
int openserialport(struct serialport_t *serial, char *serialpath);
|
||||
void *readserialport(void *ptr);
|
||||
void *serialdatawatch(void *ptr);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user