Skip to content

Commit

Permalink
Add local shadow cache. Prefer cached shadow state to AWS shadow stat…
Browse files Browse the repository at this point in the history
…e during initialization.
  • Loading branch information
Jeremy Hahn committed Jun 4, 2016
1 parent 1cb5c01 commit 80ca261
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ APP_DIR = src
APP_INCLUDE_DIRS = -I$(top_srcdir)/include -I$(APP_DIR)

ECUTOOLS_SRC_FILES = src/canbus.c src/awsiot_client.c src/mystring.c src/vector.c src/j2534.c src/j2534/apigateway.c
ECUTOOLS_SRC_FILES += src/passthru_shadow.c src/passthru_thing.c src/passthru_shadow_parser.c src/passthru_shadow_router.c
ECUTOOLS_SRC_FILES += src/passthru_shadow.c src/passthru_shadow_state.c src/passthru_thing.c src/passthru_shadow_parser.c src/passthru_shadow_router.c
ECUTOOLS_SRC_FILES += src/passthru_shadow_connection_handler.c src/passthru_shadow_log_handler.c src/passthru_shadow_j2534_handler.c
ECUTOOLS_SRC_FILES += src/canbus_logger.c src/canbus_log.c src/canbus_filelogger.c src/canbus_awsiotlogger.c

Expand Down
1 change: 0 additions & 1 deletion cli/lib/ecutools/awsiot/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def save_certificates(keys_and_cert)
File.write("#{certs_dir}/#{thing_name}.key.pem", keys_and_cert[:key_pair][:private_key])
FileUtils.chmod 0600, "#{certs_dir}/#{thing_name}.key.pem"
FileUtils.cp("#{Ecutools.home}/ecutools/awsiot/ca.crt", "#{certs_dir}/ca.crt") unless File.exist?("#{certs_dir}/ca.crt")
FileUtils.chmod_R owner, group, certs_dir
end

end
Expand Down
5 changes: 5 additions & 0 deletions install-bbb-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ MYUID=ecutune
MYGID=ecutools
LOGDIR=/var/log/ecutools
CERTDIR=/etc/ecutools/certs
CACHEDIR=/var/ecutools/cache
SDCARD=/sdcard

groupadd $MYGID
Expand All @@ -40,6 +41,10 @@ chown -R root.$MYGID $CERTDIR
chmod 775 $CERTDIR
chmod 660 $CERTDIR/*

mkdir -p $CACHEDIR
chown -R root.$MYGID $CACHEDIR
chmod 775 $CACHEDIR

su debian -c "git clone https://github.com/jeremyhahn/ecutools.git"
cd ecutools

Expand Down
5 changes: 5 additions & 0 deletions install-bbb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MYUID=ecutune
MYGID=ecutools
LOGDIR=/var/log/ecutools
CERTDIR=/etc/ecutools/certs
CACHEDIR=/var/ecutools/cache

groupadd $MYGID
useradd -G $MYGID -r $MYUID -s /bin/false
Expand All @@ -29,6 +30,10 @@ chown -R root.$MYGID $CERTDIR
chmod 775 $CERTDIR
chmod 660 $CERTDIR/*

mkdir -p $CACHEDIR
chown -R root.$MYGID $CACHEDIR
chmod 775 $CACHEDIR

git clone https://github.com/jeremyhahn/ecutools.git
cd ecutools

Expand Down
21 changes: 5 additions & 16 deletions src/awsiot_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,11 @@ unsigned int awsiot_client_connect(awsiot_client *awsiot) {
char rootCA[255];
char clientCRT[255];
char clientKey[255];
char etcEcutools[255] = "/etc/ecutools/certs";
char cafileName[] = AWS_IOT_ROOT_CA_FILENAME;
char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME;
char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME;
awsiot->rc = SUCCESS;

if(awsiot->certDir == NULL) {
sprintf(rootCA, "%s/%s", etcEcutools, cafileName);
sprintf(clientCRT, "%s/%s", etcEcutools, clientCRTName);
sprintf(clientKey, "%s/%s", etcEcutools, clientKeyName);
}
else {
sprintf(rootCA, "%s/%s", awsiot->certDir, cafileName);
sprintf(clientCRT, "%s/%s", awsiot->certDir, clientCRTName);
sprintf(clientKey, "%s/%s", awsiot->certDir, clientKeyName);
}
sprintf(rootCA, "%s/%s", awsiot->certDir, AWS_IOT_ROOT_CA_FILENAME);
sprintf(clientCRT, "%s/%s", awsiot->certDir, AWS_IOT_CERTIFICATE_FILENAME);
sprintf(clientKey, "%s/%s", awsiot->certDir, AWS_IOT_PRIVATE_KEY_FILENAME);

syslog(LOG_DEBUG, "rootCA %s", rootCA);
syslog(LOG_DEBUG, "clientCRT %s", clientCRT);
Expand Down Expand Up @@ -98,9 +87,9 @@ bool awsiot_client_isconnected(awsiot_client *awsiot) {
return (awsiot->rc == NETWORK_RECONNECTED || awsiot->rc == SUCCESS);
}

unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic) {
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic, void *pApplicationHandlerData) {
syslog(LOG_DEBUG, "awsiot_client_subscribe: subscribing to topic %s.", topic);
awsiot->rc = aws_iot_mqtt_subscribe(awsiot->client, topic, strlen(topic), QOS0, awsiot->onmessage, NULL);
awsiot->rc = aws_iot_mqtt_subscribe(awsiot->client, topic, strlen(topic), QOS0, awsiot->onmessage, pApplicationHandlerData);
if (SUCCESS != awsiot->rc) {
char errmsg[255];
sprintf(errmsg, "awsiot_client_subscribe: error subscribing to topic %s. IoT_Error_t: %d", topic, awsiot->rc);
Expand Down
2 changes: 1 addition & 1 deletion src/awsiot_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef struct _awsiot_client {

unsigned int awsiot_client_connect(awsiot_client *awsiot);
bool awsiot_client_isconnected();
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic);
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic, void *pApplicationHandlerData);
unsigned int awsiot_client_publish(awsiot_client *awsiot, const char *topic, const char *payload);
void awsiot_client_close(awsiot_client *awsiot);
bool awsiot_client_build_desired_json(char *pJsonDocument, size_t maxSizeOfJsonDocument, const char *pData, uint32_t pDataLen);
Expand Down
2 changes: 2 additions & 0 deletions src/canbus_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "canbus_log.h"

static FILE *canbus_log;

unsigned int canbus_log_open(canbus_logger *logger, const char *mode) {

char datestamp[100];
Expand Down
2 changes: 0 additions & 2 deletions src/canbus_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <time.h>
#include "canbus_logger.h"

FILE *canbus_log;

unsigned int canbus_log_open(canbus_logger *logger, const char *mode);
unsigned int canbus_log_write(char *data);
unsigned int canbus_log_read(canbus_logger *logger);
Expand Down
1 change: 1 addition & 0 deletions src/canbus_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct canbus_logger {
char *logdir;
char *logfile;
char *certDir;
char *cacheDir;
bool isrunning;
unsigned int type;
uint8_t canbus_flags;
Expand Down
27 changes: 22 additions & 5 deletions src/ecutuned.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void print_program_header() {

void parse_args(int argc, char** argv, passthru_thing_params *params) {
int opt;
while((opt = getopt(argc, argv, "n:i:l:c:d")) != -1) {
while((opt = getopt(argc, argv, "n:i:l:s:c:d")) != -1) {
switch(opt) {
case 'n':
if(strlen(optarg) > 80) {
Expand All @@ -121,6 +121,13 @@ void parse_args(int argc, char** argv, passthru_thing_params *params) {
}
params->logdir = MYSTRING_COPY(optarg, strlen(optarg));
break;
case 's':
if(strlen(optarg) > 255) {
printf("ERROR: state directory must not exceed 255 chars");
main_exit(1, params);
}
params->cacheDir = MYSTRING_COPY(optarg, strlen(optarg));
break;
case 'c':
if(strlen(optarg) > 255) {
printf("ERROR: cert directory must not exceed 255 chars");
Expand All @@ -146,6 +153,19 @@ void parse_args(int argc, char** argv, passthru_thing_params *params) {
break;
}
}

if(params->thingName == NULL) {
params->thingName = malloc(sizeof(char) * 8);
strcpy(params->thingName, AWS_IOT_MY_THING_NAME);
}

if(params->certDir == NULL) {
params->certDir = PASSTHRU_CERT_DIR;
}

if(params->cacheDir == NULL) {
params->cacheDir = PASSTHRU_CACHE_DIR;
}
}

int main(int argc, char **argv) {
Expand All @@ -157,11 +177,8 @@ int main(int argc, char **argv) {
params->logdir = NULL;
params->iface = NULL;
params->certDir = NULL;
params->cacheDir = NULL;
parse_args(argc, argv, params);
if(params->thingName == NULL) {
params->thingName = malloc(sizeof(char) * 8);
strcpy(params->thingName, AWS_IOT_MY_THING_NAME);
}

struct sigaction newSigAction;
sigset_t newSigSet;
Expand Down
4 changes: 2 additions & 2 deletions src/j2534.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ unsigned int j2534_publish_state(j2534_client *client, int desired_state) {
snprintf(json, json_len, "{\"state\":{\"desired\":{\"j2534\":%i}}}", desired_state);
json[json_len] = '\0';

if(awsiot_client_subscribe(client->awsiot, j2534_shadow_update_accepted_topic) != 0) {
if(awsiot_client_subscribe(client->awsiot, j2534_shadow_update_accepted_topic, NULL) != 0) {
syslog(LOG_ERR, "j2534_publish_state: failed to subscribe to j2534_shadow_update_accepted_topic %s. rc=%d", j2534_shadow_update_accepted_topic, client->awsiot->rc);
return ERR_DEVICE_NOT_CONNECTED;
}

if(awsiot_client_subscribe(client->awsiot, J2534_ERROR_TOPIC) != 0) {
if(awsiot_client_subscribe(client->awsiot, J2534_ERROR_TOPIC, NULL) != 0) {
syslog(LOG_ERR, "j2534_publish_state: failed to subscribe to J2534_ERROR_TOPIC %s. rc=%d", J2534_ERROR_TOPIC, client->awsiot->rc);
return ERR_DEVICE_NOT_CONNECTED;
}
Expand Down
25 changes: 7 additions & 18 deletions src/passthru_shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,10 @@ int passthru_shadow_connect(passthru_shadow *shadow) {
char rootCA[255];
char clientCRT[255];
char clientKey[255];
char etcEcutools[255] = "/etc/ecutools/certs";
char cafileName[] = AWS_IOT_ROOT_CA_FILENAME;
char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME;
char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME;

if(shadow->certDir == NULL) {
sprintf(rootCA, "%s/%s", etcEcutools, cafileName);
sprintf(clientCRT, "%s/%s", etcEcutools, clientCRTName);
sprintf(clientKey, "%s/%s", etcEcutools, clientKeyName);
}
else {
sprintf(rootCA, "%s/%s", shadow->certDir, cafileName);
sprintf(clientCRT, "%s/%s", shadow->certDir, clientCRTName);
sprintf(clientKey, "%s/%s", shadow->certDir, clientKeyName);
}

sprintf(rootCA, "%s/%s", shadow->certDir, AWS_IOT_ROOT_CA_FILENAME);
sprintf(clientCRT, "%s/%s", shadow->certDir, AWS_IOT_CERTIFICATE_FILENAME);
sprintf(clientKey, "%s/%s", shadow->certDir, AWS_IOT_PRIVATE_KEY_FILENAME);

syslog(LOG_DEBUG, "rootCA %s", rootCA);
syslog(LOG_DEBUG, "clientCRT %s", clientCRT);
Expand Down Expand Up @@ -102,7 +91,7 @@ int passthru_shadow_connect(passthru_shadow *shadow) {

int passthru_shadow_report_delta(passthru_shadow *shadow) {
syslog(LOG_DEBUG, "Sending delta report: %s", DELTA_REPORT);
return passthru_shadow_update(shadow, DELTA_REPORT);
return passthru_shadow_update(shadow, DELTA_REPORT, NULL);
}

void passthru_shadow_get(passthru_shadow *shadow) {
Expand All @@ -115,9 +104,9 @@ void passthru_shadow_get(passthru_shadow *shadow) {
}
}

int passthru_shadow_update(passthru_shadow *shadow, char *message) {
int passthru_shadow_update(passthru_shadow *shadow, char *message, void *pContextData) {
syslog(LOG_DEBUG, "passthru_shadow_update: message=%s", message);
shadow->rc = aws_iot_shadow_update(shadow->mqttClient, shadow->thingName, message, shadow->onupdate, NULL, 2, true);
shadow->rc = aws_iot_shadow_update(shadow->mqttClient, shadow->thingName, message, shadow->onupdate, pContextData, 2, true);
if(shadow->rc != SUCCESS) {
char errmsg[255];
sprintf(errmsg, "aws_iot_shadow_update error rc=%d", shadow->rc);
Expand Down
2 changes: 1 addition & 1 deletion src/passthru_shadow.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int passthru_shadow_connect(passthru_shadow *shadow);
bool passthru_shadow_build_report_json(char *pJsonDocument, size_t maxSizeOfJsonDocument, const char *pReceivedDeltaData, uint32_t lengthDelta);
int passthru_shadow_report_delta(passthru_shadow *shadow);
void passthru_shadow_get(passthru_shadow *shadow);
int passthru_shadow_update(passthru_shadow *shadow, char *message);
int passthru_shadow_update(passthru_shadow *shadow, char *message, void *pContextData);
int passthru_shadow_disconnect(passthru_shadow *shadow);

#endif
32 changes: 16 additions & 16 deletions src/passthru_shadow_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@

#include "passthru_shadow_router.h"

void passthru_shadow_router_print_desired(shadow_desired *desired) {
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->j2534->state=%d, desired->j2534->error=%d", desired->j2534->state, desired->j2534->error);
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->log->type=%d, desired->log->file=%s", desired->log->type, desired->log->file);
}

void passthru_shadow_router_print_reported(shadow_report *reported) {
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->j2534->state=%d, reported->j2534->error=%d", reported->j2534->state, reported->j2534->error);
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->log->type=%d, reported->log->file=%s", reported->log->type, reported->log->file);
}

void passthru_shadow_router_print_message(shadow_message *message) {
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->connection=%i", message->state->reported->connection);
passthru_shadow_router_print_desired(message->state->desired);
passthru_shadow_router_print_reported(message->state->reported);
}

void passthru_shadow_router_route_message(passthru_thing *thing, shadow_message *message) {

if(message == NULL || message->state == NULL) return;
Expand Down Expand Up @@ -49,19 +65,3 @@ void passthru_shadow_router_route_delta(passthru_thing *thing, shadow_desired *d
}
syslog(LOG_DEBUG, "passthru_shadow_router_route_delta: unable to locate delta handler");
}

void passthru_shadow_router_print_message(shadow_message *message) {
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->connection=%i", message->state->reported->connection);
passthru_shadow_router_print_desired(message->state->desired);
passthru_shadow_router_print_reported(message->state->reported);
}

void passthru_shadow_router_print_desired(shadow_desired *desired) {
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->j2534->state=%d, desired->j2534->error=%d", desired->j2534->state, desired->j2534->error);
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->log->type=%d, desired->log->file=%s", desired->log->type, desired->log->file);
}

void passthru_shadow_router_print_reported(shadow_report *reported) {
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->j2534->state=%d, reported->j2534->error=%d", reported->j2534->state, reported->j2534->error);
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->log->type=%d, reported->log->file=%s", reported->log->type, reported->log->file);
}
3 changes: 2 additions & 1 deletion src/passthru_shadow_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "passthru_shadow_log_handler.h"
#include "passthru_shadow_connection_handler.h"

void passthru_shadow_router_route(passthru_thing *thing, shadow_message *message);
void passthru_shadow_router_route_message(passthru_thing *thing, shadow_message *message);
void passthru_shadow_router_route_delta(passthru_thing *thing, shadow_desired *desired);

#endif
Loading

0 comments on commit 80ca261

Please sign in to comment.