Skip to content

Commit

Permalink
Refactor canbus_iotbridge to passthru_iotbridge. Replace awsiot_clien…
Browse files Browse the repository at this point in the history
…t with passthru_shadow. Add file logger.
  • Loading branch information
Jeremy Hahn committed May 4, 2016
1 parent d666d3f commit 25c75c4
Show file tree
Hide file tree
Showing 19 changed files with 573 additions and 201 deletions.
8 changes: 5 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ ACLOCAL_AMFLAGS = -I m4
APP_DIR = src
APP_INCLUDE_DIRS = -I$(top_srcdir)/include -I$(APP_DIR)

ECUTOOLS_SRC_FILES = src/canbus.c src/awsiot_client.c src/canbus_iotbridge.c src/apigateway.c
ECUTOOLS_SRC_FILES = src/canbus.c src/canbus_logger.c src/canbus_filelogger.c src/canbus_log.c src/apigateway.c src/passthru_shadow.c src/passthru_iotbridge.c
ECUTOOLS_TEST_FILES = tests/check_j2534.c

# AWS IoT client directory
IOT_CLIENT_DIR = src/aws_iot_src
IOT_INCLUDE_DIRS =
IOT_INCLUDE_DIRS = -I $(IOT_CLIENT_DIR)/shadow
IOT_INCLUDE_DIRS += -I $(IOT_CLIENT_DIR)/protocol/mqtt
IOT_INCLUDE_DIRS += -I $(IOT_CLIENT_DIR)/protocol/mqtt/aws_iot_embedded_client_wrapper
IOT_INCLUDE_DIRS += -I $(IOT_CLIENT_DIR)/protocol/mqtt/aws_iot_embedded_client_wrapper/platform_linux
Expand All @@ -18,7 +18,9 @@ IOT_INCLUDE_DIRS += -I $(IOT_CLIENT_DIR)/utils

PLATFORM_DIR = $(IOT_CLIENT_DIR)/protocol/mqtt/aws_iot_embedded_client_wrapper/platform_linux/openssl
PLATFORM_COMMON_DIR = $(IOT_CLIENT_DIR)/protocol/mqtt/aws_iot_embedded_client_wrapper/platform_linux/common
IOT_SRC_FILES =

IOT_SRC_FILES = $(IOT_CLIENT_DIR)/utils/jsmn.c $(IOT_CLIENT_DIR)/utils/aws_iot_json_utils.c
IOT_SRC_FILES += $(IOT_CLIENT_DIR)/shadow/aws_iot_shadow.c $(IOT_CLIENT_DIR)/shadow/aws_iot_shadow_actions.c $(IOT_CLIENT_DIR)/shadow/aws_iot_shadow_json.c $(IOT_CLIENT_DIR)/shadow/aws_iot_shadow_records.c
IOT_SRC_FILES += $(IOT_CLIENT_DIR)/protocol/mqtt/aws_iot_embedded_client_wrapper/aws_iot_mqtt_embedded_client_wrapper.c
IOT_SRC_FILES += $(PLATFORM_DIR)/network_openssl_wrapper.c $(PLATFORM_DIR)/openssl_hostname_validation.c $(PLATFORM_DIR)/hostname_compare.c $(PLATFORM_DIR)/rawstr.c
IOT_SRC_FILES += $(PLATFORM_COMMON_DIR)/timer.c
Expand Down
4 changes: 4 additions & 0 deletions src/awsiot_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ void awsiot_client_connect(awsiot_client *awsiot) {
if(awsiot->rc != NONE_ERROR) {
sprintf(errmsg, "Error(%d) connecting to %s:%d", awsiot->rc, connectParams.pHostURL, connectParams.port);
awsiot->onerror(awsiot, errmsg);
return NULL;
}

awsiot->rc = aws_iot_mqtt_autoreconnect_set_status(true);
if(NONE_ERROR != awsiot->rc) {
sprintf(errmsg, "Unable to set Auto Reconnect to true. IoT_Error_t=%d", awsiot->rc);
awsiot->onerror(awsiot, errmsg);
return NULL;
}

awsiot->onopen(awsiot);
Expand All @@ -90,6 +92,7 @@ void awsiot_client_subscribe(awsiot_client *awsiot, const char *topic) {
char errmsg[255];
sprintf(errmsg, "awsiot_client_subscribe: error subscribing to topic %s. IoT_Error_t: %d", subParams.pTopic, awsiot->rc);
awsiot->onerror(awsiot, errmsg);
return NULL;
}
}
}
Expand All @@ -114,6 +117,7 @@ void awsiot_client_publish(awsiot_client *awsiot, const char *topic, const char
char errmsg[255];
sprintf(errmsg, "awsiot_client_publish: error publishing to topic %s. IoT_Error_t: %d", Params.pTopic, awsiot->rc);
awsiot->onerror(awsiot, errmsg);
return NULL;
}
}

Expand Down
70 changes: 70 additions & 0 deletions src/canbus_filelogger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "canbus_filelogger.h"

void *canbus_filelogger_thread(void *ptr) {

syslog(LOG_DEBUG, "canbus_filelogger_thread: running");

canbus_log_open();

logger *pLogger = (logger *)ptr;

int can_frame_len = sizeof(struct can_frame);
struct can_frame frame;
memset(&frame, 0, can_frame_len);

int data_len = can_frame_len + 25;
char data[data_len];
memset(data, 0, data_len);

while((pLogger->canbus->state & CANBUS_STATE_CONNECTED) &&
canbus_read(pLogger->canbus, &frame) > 0) {

memset(data, 0, data_len);
canbus_framecpy(&frame, data);

if(frame.can_id & CAN_ERR_FLAG) {
syslog(LOG_ERR, "canbus_filelogger_thread: CAN ERROR: %s", data);
continue;
}

canbus_log_write(data);
}

canbus_log_close();
syslog(LOG_DEBUG, "canbus_filelogger_thread: stopping");
return NULL;
}

unsigned int canbus_filelogger_run(logger *logger) {
canbus_connect(logger->canbus);
if(!canbus_isconnected(logger->canbus)) {
syslog(LOG_CRIT, "canbus_filelogger_run: unable to connect to CAN");
return 1;
}
pthread_create(&logger->canbus_thread, NULL, canbus_filelogger_thread, (void *)logger);
pthread_join(logger->canbus_thread, NULL);
syslog(LOG_DEBUG, "canbus_filelogger_run: logger closed");
return 0;
}

unsigned int canbus_filelogger_cancel(logger *logger) {
return pthread_cancel(logger->canbus_thread);
}
27 changes: 27 additions & 0 deletions src/canbus_filelogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef CANBUSFILELOGGER_H
#define CANBUSFILELOGGER_H

#include "canbus_logger_interface.h"

unsigned int canbus_filelogger_run(logger *logger);
unsigned int canbus_filelogger_cancel(logger *logger);

#endif
48 changes: 48 additions & 0 deletions src/canbus_log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "canbus_log.h"

//FILE *canbus_log;

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

void canbus_log_open() {
char filename[1050];
snprintf(filename, 1000, canbus_log_datestamp());
strcat(filename, ".log");
canbus_log = fopen(filename, "w");
if(canbus_log == NULL) {
syslog(LOG_ERR, "canbus_log_open: Unable to open log file: %s", filename);
return;
}
}

int canbus_log_write(char *data) {
return fprintf(canbus_log, data);
}

void canbus_log_close() {
fclose(canbus_log);
}
32 changes: 32 additions & 0 deletions src/canbus_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef CANBUSLOG_H
#define CANBUSLOG_H

#include <stdio.h>
#include <syslog.h>
#include <time.h>

FILE *canbus_log;

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

#endif
27 changes: 27 additions & 0 deletions src/canbus_logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "canbus_logger.h"

unsigned int canbus_logger_run(logger *logger) {
canbus_filelogger_run(logger);
}

unsigned int canbus_logger_cancel(logger *logger) {
canbus_filelogger_run(logger);
}
27 changes: 27 additions & 0 deletions src/canbus_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef CANBUSLOGGER_H
#define CANBUSLOGGER_H

#include "canbus_logger_interface.h"

unsigned int canbus_logger_run(logger *logger);
unsigned int canbus_logger_cancel(logger *logger);

#endif
33 changes: 33 additions & 0 deletions src/canbus_logger_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* ecutools: IoT Automotive Tuning, Diagnostics & Analytics
* Copyright (C) 2014 Jeremy Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef CANBUSLOGGERINTERFACE_H
#define CANBUSLOGGERINTERFACE_H

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

typedef struct _logger {
uint8_t canbus_flags;
pthread_t canbus_thread;
struct canbus_filter *filters[10];
canbus_client *canbus;
} logger;

#endif
2 changes: 1 addition & 1 deletion src/ecutuned.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "canbus_iotbridge.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <syslog.h>
#include <string.h>
#include "passthru_iotbridge.h"

int main_exit(int exit_status) {
syslog(LOG_DEBUG, "exiting ecutune");
Expand Down
Loading

0 comments on commit 25c75c4

Please sign in to comment.