Skip to content

Commit

Permalink
Add canbus interface and log directory cmd line arguments, add jansso…
Browse files Browse the repository at this point in the history
…n and curl to configure checks. file logger clean up
  • Loading branch information
Jeremy Hahn committed May 15, 2016
1 parent 37782a4 commit a7409b4
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
certs/*
*.log
logs/*
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ AC_PROG_INSTALL
AC_SEARCH_LIBS([floor], [m])
AC_SEARCH_LIBS([timer_create], [rt])
AC_SEARCH_LIBS([pthread_create], [pthread])
AC_SEARCH_LIBS([curl_easy_perform], [curl])
AC_SEARCH_LIBS([json_loads], [jansson])

# Remember externally set CFLAGS
EXTERNAL_CFLAGS="$CFLAGS"
Expand Down
14 changes: 14 additions & 0 deletions install-bbb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# install build dependencies
sudo apt-get install libjansson-dev libcurl4-openssl-dev can-utils

# compile mbedTLS
cd src/aws_iot_src/external_libs/mbedTLS
make
cd -

# build ecutools
git clone https://github.com/jeremyhahn/ecutools.git
cd ecutools
./autogen.sh && ./configure && make && sudo make install

echo "All done. Create certs directory and update aws_iot_config.h."
3 changes: 2 additions & 1 deletion src/awsiot_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ void awsiot_client_publish(awsiot_client *awsiot, const char *topic, const char
}

void awsiot_client_close(awsiot_client *awsiot, const char *topic, const char *payload) {
if(payload != NULL) {
syslog(LOG_DEBUG, "awsiot_client_close: closing connection");
if(topic != NULL && payload != NULL) {
awsiot_client_publish(awsiot, topic, payload);
}
awsiot->rc = aws_iot_mqtt_disconnect(&awsiot->client);
Expand Down
10 changes: 8 additions & 2 deletions src/canbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ int canbus_init(canbus_client *canbus) {
canbus->socket = 0;
canbus->state = CANBUS_STATE_CLOSED;
canbus->flags = 0;
if(canbus->iface == NULL) {
canbus->iface = malloc(6);
strcpy(canbus->iface, "vcan0");
}
}

int canbus_connect(canbus_client *canbus) {
Expand Down Expand Up @@ -87,9 +91,9 @@ int canbus_connect(canbus_client *canbus) {
return -1;
}

strcpy(ifr.ifr_name, CAN_IFACE);
strcpy(ifr.ifr_name, canbus->iface);
if(ioctl(canbus->socket, SIOCGIFINDEX, &ifr) < 0) {
syslog(LOG_CRIT, "canbus_connect: unable to find CAN interface %s", CAN_IFACE);
syslog(LOG_CRIT, "canbus_connect: unable to find CAN interface %s", canbus->iface);
exit(1);
}

Expand Down Expand Up @@ -184,4 +188,6 @@ void canbus_close(canbus_client *canbus) {
pthread_mutex_lock(&canbus->lock);
canbus->state = CANBUS_STATE_CLOSED;
pthread_mutex_unlock(&canbus->lock);

free(canbus->iface);
}
5 changes: 1 addition & 4 deletions src/canbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
#include <linux/can.h>
#include <linux/can/raw.h>

#ifndef CAN_IFACE
#define CAN_IFACE "vcan0"
#endif

#define CANBUS_STATE_CONNECTING (1 << 0)
#define CANBUS_STATE_CONNECTED (1 << 1)
#define CANBUS_STATE_CLOSING (1 << 2)
Expand All @@ -47,6 +43,7 @@
#define CANBUS_FLAG_RECV_OWN_MSGS 0 // 0 = disable, 1 = enable

typedef struct {
char *iface;
unsigned int socket;
uint8_t state;
uint8_t flags;
Expand Down
1 change: 1 addition & 0 deletions src/canbus_awsiotlogger.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void *canbus_awsiotlogger_thread(void *ptr) {
awsiot_client_publish(&iotlogger, "ecutools/datalogger", data);
}

awsiot_client_close(&iotlogger, NULL, NULL);
canbus_close(pLogger->canbus);
syslog(LOG_DEBUG, "canbus_awsiotlogger_thread: stopping");
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/canbus_awsiotlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef CANBUSawsiotlogger_H
#define CANBUSawsiotlogger_H

#include "canbus_logger_interface.h"
#include "canbus_logger.h"
#include "awsiot_client.h"

unsigned int canbus_awsiotlogger_run(canbus_logger *logger);
Expand Down
8 changes: 4 additions & 4 deletions src/canbus_filelogger.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@

void *canbus_filelogger_thread(void *ptr) {

syslog(LOG_DEBUG, "canbus_filelogger_thread: running");

canbus_log_open();

canbus_logger *pLogger = (canbus_logger *)ptr;
if(canbus_log_open(pLogger) != 0) return NULL;

syslog(LOG_DEBUG, "canbus_filelogger_thread: running");

int can_frame_len = sizeof(struct can_frame);
struct can_frame frame;
Expand All @@ -47,6 +46,7 @@ void *canbus_filelogger_thread(void *ptr) {
canbus_log_write(data);
}

canbus_log_close();
canbus_close(pLogger->canbus);
syslog(LOG_DEBUG, "canbus_filelogger_thread: stopping");
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/canbus_filelogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef CANBUSFILELOGGER_H
#define CANBUSFILELOGGER_H

#include "canbus_logger_interface.h"
#include "canbus_logger.h"

unsigned int canbus_filelogger_run(canbus_logger *logger);
unsigned int canbus_filelogger_stop(canbus_logger *logger);
Expand Down
27 changes: 18 additions & 9 deletions src/canbus_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@
#include "canbus_log.h"

char* canbus_log_datestamp() {
char buf[1000];
char buf[100];
time_t now = time(0);
struct tm tm = *gmtime(&now);
strftime(buf, sizeof buf, "%a, %d %b %Y %H:%M:%S %Z", &tm);
strftime(buf, sizeof buf, "ecutuned_%m%d%Y_%H%M%S_%Z", &tm);
return buf;
}

void canbus_log_open() {
char filename[1050];
snprintf(filename, 1000, canbus_log_datestamp());
unsigned int canbus_log_open(canbus_logger *logger) {
char filename[360];
if(logger->logdir != NULL) {
strcpy(filename, logger->logdir);
if(filename[strlen(filename)] != '/') {
strcat(filename, "/");
}
}
strcat(filename, canbus_log_datestamp());
strcat(filename, ".log");
syslog(LOG_DEBUG, "canbus_log_open: filename=%s", filename);
canbus_log = fopen(filename, "w");
if(canbus_log == NULL) {
syslog(LOG_ERR, "canbus_log_open: Unable to open log file: %s", filename);
return;
syslog(LOG_ERR, "canbus_log_open: Unable to open %s. error=%s", filename, strerror(errno));
return errno;
}
return 0;
}

int canbus_log_write(char *data) {
unsigned canbus_log_write(char *data) {
if(strlen(data) > 255) {
syslog(LOG_ERR, "canbus_log_write: data is larger than 255 bytes");
syslog(LOG_ERR, "canbus_log_write: data must not be larger than 255 chars");
return 1;
}
char d[257];
strncpy(d, data, strlen(data)+1);
syslog(LOG_DEBUG, "canbus_log_write: %s", d);
strcat(d, "\n");
return fprintf(canbus_log, d);
}
Expand Down
5 changes: 3 additions & 2 deletions src/canbus_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include "canbus_logger.h"

FILE *canbus_log;

void canbus_log_open();
int canbus_log_write(char *data);
unsigned int canbus_log_open();
unsigned int canbus_log_write(char *data);
void canbus_log_close();

#endif
2 changes: 1 addition & 1 deletion src/canbus_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ unsigned int canbus_logger_stop(canbus_logger *logger) {
if(logger->canbus_thread != NULL) {
logger->isrunning = false;
while(canbus_isconnected(logger->canbus)) {
syslog(LOG_DEBUG, "canbus_awsiotlogger_stop: waiting for canbus connection to close");
syslog(LOG_DEBUG, "canbus_logger_stop: waiting for canbus connection to close");
sleep(1);
}
logger->canbus_thread = NULL;
Expand Down
19 changes: 18 additions & 1 deletion src/canbus_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@
#ifndef CANBUSLOGGER_H
#define CANBUSLOGGER_H

#include "canbus_logger_interface.h"
#define CANBUS_LOGTYPE_FILE (1 << 0)
#define CANBUS_LOGTYPE_AWSIOT (1 << 1)

#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include <syslog.h>
#include "canbus.h"

typedef struct {
uint8_t canbus_flags;
pthread_t canbus_thread;
struct canbus_filter *filters[10];
canbus_client *canbus;
bool isrunning;
unsigned int type;
char *logdir;
} canbus_logger;

unsigned int canbus_logger_run(canbus_logger *logger);
unsigned int canbus_logger_stop(canbus_logger *logger);
Expand Down
39 changes: 0 additions & 39 deletions src/canbus_logger_interface.h

This file was deleted.

65 changes: 59 additions & 6 deletions src/ecutuned.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@
#include <string.h>
#include "passthru_thing.h"

int main_exit(int exit_status) {
syslog(LOG_DEBUG, "exiting ecutuned");
int main_exit(int exit_status, thing_init_params *params) {
syslog(LOG_DEBUG, "exiting %s", params->thingId);
closelog();
if(params->iface != NULL) {
free(params->iface);
}
if(params->logdir != NULL) {
free(params->logdir);
}
free(params->thingId);
free(params);
return exit_status;
}

Expand Down Expand Up @@ -77,10 +85,54 @@ void print_program_header() {
fprintf(stdout, "\n");
}

void parse_args(int argc, char** argv, thing_init_params *params) {
int opt;
while((opt = getopt(argc, argv, "i:l:")) != -1) {
switch(opt) {
case 'i':
if(strlen(optarg) > 10) {
printf("ERROR: interface value must not exceed 10 chars");
main_exit(1, params);
}
params->iface = malloc(sizeof(char) * (strlen(optarg)+1));
strcpy(params->iface, optarg);
break;
case 'l':
if(strlen(optarg) > 255) {
printf("ERROR: log directory value must not exceed 255 chars");
main_exit(1, params);
}
params->logdir = malloc(sizeof(char) * (strlen(optarg)+1));
strcpy(params->logdir, optarg);
break;
case '?':
if(isprint(optopt)) {
syslog(LOG_WARNING, "Unknown option '-%c'.", optopt);
}
else {
syslog(LOG_WARNING, "Unknown option character '\\x%x'.", optopt);
}
main_exit(1, params);
break;
default:
syslog(LOG_ERR, "parse_args: error parsing command line arguments");
main_exit(1, params);
break;
}
}
}

int main(int argc, char **argv) {

print_program_header();

thing_init_params *params = malloc(sizeof(thing_init_params));
params->thingId = malloc(sizeof(char) * 9);
params->logdir = NULL;
params->iface = NULL;
parse_args(argc, argv, params);
strcpy(params->thingId, "ecutuned");

struct sigaction newSigAction;
sigset_t newSigSet;

Expand All @@ -100,12 +152,13 @@ int main(int argc, char **argv) {
sigaction(SIGINT, &newSigAction, NULL);

setlogmask(LOG_UPTO(LOG_DEBUG));
openlog("ecutuned", LOG_CONS | LOG_PERROR, LOG_USER);
syslog(LOG_DEBUG, "starting ecutuned");
openlog(params->thingId, LOG_CONS | LOG_PERROR, LOG_USER);
syslog(LOG_DEBUG, "starting %s", params->thingId);

passthru_thing_init("ecutuned");
passthru_thing_init(params);
passthru_thing_run();
passthru_thing_close();
passthru_thing_destroy();

return main_exit(EXIT_SUCCESS);
return main_exit(EXIT_SUCCESS, params);
}
Loading

0 comments on commit a7409b4

Please sign in to comment.