Skip to content

Commit 80ca261

Browse files
author
Jeremy Hahn
committed
Add local shadow cache. Prefer cached shadow state to AWS shadow state during initialization.
1 parent 1cb5c01 commit 80ca261

19 files changed

+303
-144
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ APP_DIR = src
44
APP_INCLUDE_DIRS = -I$(top_srcdir)/include -I$(APP_DIR)
55

66
ECUTOOLS_SRC_FILES = src/canbus.c src/awsiot_client.c src/mystring.c src/vector.c src/j2534.c src/j2534/apigateway.c
7-
ECUTOOLS_SRC_FILES += src/passthru_shadow.c src/passthru_thing.c src/passthru_shadow_parser.c src/passthru_shadow_router.c
7+
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
88
ECUTOOLS_SRC_FILES += src/passthru_shadow_connection_handler.c src/passthru_shadow_log_handler.c src/passthru_shadow_j2534_handler.c
99
ECUTOOLS_SRC_FILES += src/canbus_logger.c src/canbus_log.c src/canbus_filelogger.c src/canbus_awsiotlogger.c
1010

cli/lib/ecutools/awsiot/service.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ def save_certificates(keys_and_cert)
142142
File.write("#{certs_dir}/#{thing_name}.key.pem", keys_and_cert[:key_pair][:private_key])
143143
FileUtils.chmod 0600, "#{certs_dir}/#{thing_name}.key.pem"
144144
FileUtils.cp("#{Ecutools.home}/ecutools/awsiot/ca.crt", "#{certs_dir}/ca.crt") unless File.exist?("#{certs_dir}/ca.crt")
145-
FileUtils.chmod_R owner, group, certs_dir
146145
end
147146

148147
end

install-bbb-dev.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ MYUID=ecutune
2626
MYGID=ecutools
2727
LOGDIR=/var/log/ecutools
2828
CERTDIR=/etc/ecutools/certs
29+
CACHEDIR=/var/ecutools/cache
2930
SDCARD=/sdcard
3031

3132
groupadd $MYGID
@@ -40,6 +41,10 @@ chown -R root.$MYGID $CERTDIR
4041
chmod 775 $CERTDIR
4142
chmod 660 $CERTDIR/*
4243

44+
mkdir -p $CACHEDIR
45+
chown -R root.$MYGID $CACHEDIR
46+
chmod 775 $CACHEDIR
47+
4348
su debian -c "git clone https://github.com/jeremyhahn/ecutools.git"
4449
cd ecutools
4550

install-bbb.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ MYUID=ecutune
1616
MYGID=ecutools
1717
LOGDIR=/var/log/ecutools
1818
CERTDIR=/etc/ecutools/certs
19+
CACHEDIR=/var/ecutools/cache
1920

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

33+
mkdir -p $CACHEDIR
34+
chown -R root.$MYGID $CACHEDIR
35+
chmod 775 $CACHEDIR
36+
3237
git clone https://github.com/jeremyhahn/ecutools.git
3338
cd ecutools
3439

src/awsiot_client.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,11 @@ unsigned int awsiot_client_connect(awsiot_client *awsiot) {
2424
char rootCA[255];
2525
char clientCRT[255];
2626
char clientKey[255];
27-
char etcEcutools[255] = "/etc/ecutools/certs";
28-
char cafileName[] = AWS_IOT_ROOT_CA_FILENAME;
29-
char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME;
30-
char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME;
3127
awsiot->rc = SUCCESS;
3228

33-
if(awsiot->certDir == NULL) {
34-
sprintf(rootCA, "%s/%s", etcEcutools, cafileName);
35-
sprintf(clientCRT, "%s/%s", etcEcutools, clientCRTName);
36-
sprintf(clientKey, "%s/%s", etcEcutools, clientKeyName);
37-
}
38-
else {
39-
sprintf(rootCA, "%s/%s", awsiot->certDir, cafileName);
40-
sprintf(clientCRT, "%s/%s", awsiot->certDir, clientCRTName);
41-
sprintf(clientKey, "%s/%s", awsiot->certDir, clientKeyName);
42-
}
29+
sprintf(rootCA, "%s/%s", awsiot->certDir, AWS_IOT_ROOT_CA_FILENAME);
30+
sprintf(clientCRT, "%s/%s", awsiot->certDir, AWS_IOT_CERTIFICATE_FILENAME);
31+
sprintf(clientKey, "%s/%s", awsiot->certDir, AWS_IOT_PRIVATE_KEY_FILENAME);
4332

4433
syslog(LOG_DEBUG, "rootCA %s", rootCA);
4534
syslog(LOG_DEBUG, "clientCRT %s", clientCRT);
@@ -98,9 +87,9 @@ bool awsiot_client_isconnected(awsiot_client *awsiot) {
9887
return (awsiot->rc == NETWORK_RECONNECTED || awsiot->rc == SUCCESS);
9988
}
10089

101-
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic) {
90+
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic, void *pApplicationHandlerData) {
10291
syslog(LOG_DEBUG, "awsiot_client_subscribe: subscribing to topic %s.", topic);
103-
awsiot->rc = aws_iot_mqtt_subscribe(awsiot->client, topic, strlen(topic), QOS0, awsiot->onmessage, NULL);
92+
awsiot->rc = aws_iot_mqtt_subscribe(awsiot->client, topic, strlen(topic), QOS0, awsiot->onmessage, pApplicationHandlerData);
10493
if (SUCCESS != awsiot->rc) {
10594
char errmsg[255];
10695
sprintf(errmsg, "awsiot_client_subscribe: error subscribing to topic %s. IoT_Error_t: %d", topic, awsiot->rc);

src/awsiot_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ typedef struct _awsiot_client {
4747

4848
unsigned int awsiot_client_connect(awsiot_client *awsiot);
4949
bool awsiot_client_isconnected();
50-
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic);
50+
unsigned int awsiot_client_subscribe(awsiot_client *awsiot, const char *topic, void *pApplicationHandlerData);
5151
unsigned int awsiot_client_publish(awsiot_client *awsiot, const char *topic, const char *payload);
5252
void awsiot_client_close(awsiot_client *awsiot);
5353
bool awsiot_client_build_desired_json(char *pJsonDocument, size_t maxSizeOfJsonDocument, const char *pData, uint32_t pDataLen);

src/canbus_log.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "canbus_log.h"
2020

21+
static FILE *canbus_log;
22+
2123
unsigned int canbus_log_open(canbus_logger *logger, const char *mode) {
2224

2325
char datestamp[100];

src/canbus_log.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include <time.h>
2525
#include "canbus_logger.h"
2626

27-
FILE *canbus_log;
28-
2927
unsigned int canbus_log_open(canbus_logger *logger, const char *mode);
3028
unsigned int canbus_log_write(char *data);
3129
unsigned int canbus_log_read(canbus_logger *logger);

src/canbus_logger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct canbus_logger {
3838
char *logdir;
3939
char *logfile;
4040
char *certDir;
41+
char *cacheDir;
4142
bool isrunning;
4243
unsigned int type;
4344
uint8_t canbus_flags;

src/ecutuned.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void print_program_header() {
9898

9999
void parse_args(int argc, char** argv, passthru_thing_params *params) {
100100
int opt;
101-
while((opt = getopt(argc, argv, "n:i:l:c:d")) != -1) {
101+
while((opt = getopt(argc, argv, "n:i:l:s:c:d")) != -1) {
102102
switch(opt) {
103103
case 'n':
104104
if(strlen(optarg) > 80) {
@@ -121,6 +121,13 @@ void parse_args(int argc, char** argv, passthru_thing_params *params) {
121121
}
122122
params->logdir = MYSTRING_COPY(optarg, strlen(optarg));
123123
break;
124+
case 's':
125+
if(strlen(optarg) > 255) {
126+
printf("ERROR: state directory must not exceed 255 chars");
127+
main_exit(1, params);
128+
}
129+
params->cacheDir = MYSTRING_COPY(optarg, strlen(optarg));
130+
break;
124131
case 'c':
125132
if(strlen(optarg) > 255) {
126133
printf("ERROR: cert directory must not exceed 255 chars");
@@ -146,6 +153,19 @@ void parse_args(int argc, char** argv, passthru_thing_params *params) {
146153
break;
147154
}
148155
}
156+
157+
if(params->thingName == NULL) {
158+
params->thingName = malloc(sizeof(char) * 8);
159+
strcpy(params->thingName, AWS_IOT_MY_THING_NAME);
160+
}
161+
162+
if(params->certDir == NULL) {
163+
params->certDir = PASSTHRU_CERT_DIR;
164+
}
165+
166+
if(params->cacheDir == NULL) {
167+
params->cacheDir = PASSTHRU_CACHE_DIR;
168+
}
149169
}
150170

151171
int main(int argc, char **argv) {
@@ -157,11 +177,8 @@ int main(int argc, char **argv) {
157177
params->logdir = NULL;
158178
params->iface = NULL;
159179
params->certDir = NULL;
180+
params->cacheDir = NULL;
160181
parse_args(argc, argv, params);
161-
if(params->thingName == NULL) {
162-
params->thingName = malloc(sizeof(char) * 8);
163-
strcpy(params->thingName, AWS_IOT_MY_THING_NAME);
164-
}
165182

166183
struct sigaction newSigAction;
167184
sigset_t newSigSet;

src/j2534.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ unsigned int j2534_publish_state(j2534_client *client, int desired_state) {
104104
snprintf(json, json_len, "{\"state\":{\"desired\":{\"j2534\":%i}}}", desired_state);
105105
json[json_len] = '\0';
106106

107-
if(awsiot_client_subscribe(client->awsiot, j2534_shadow_update_accepted_topic) != 0) {
107+
if(awsiot_client_subscribe(client->awsiot, j2534_shadow_update_accepted_topic, NULL) != 0) {
108108
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);
109109
return ERR_DEVICE_NOT_CONNECTED;
110110
}
111111

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

src/passthru_shadow.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,10 @@ int passthru_shadow_connect(passthru_shadow *shadow) {
2929
char rootCA[255];
3030
char clientCRT[255];
3131
char clientKey[255];
32-
char etcEcutools[255] = "/etc/ecutools/certs";
33-
char cafileName[] = AWS_IOT_ROOT_CA_FILENAME;
34-
char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME;
35-
char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME;
36-
37-
if(shadow->certDir == NULL) {
38-
sprintf(rootCA, "%s/%s", etcEcutools, cafileName);
39-
sprintf(clientCRT, "%s/%s", etcEcutools, clientCRTName);
40-
sprintf(clientKey, "%s/%s", etcEcutools, clientKeyName);
41-
}
42-
else {
43-
sprintf(rootCA, "%s/%s", shadow->certDir, cafileName);
44-
sprintf(clientCRT, "%s/%s", shadow->certDir, clientCRTName);
45-
sprintf(clientKey, "%s/%s", shadow->certDir, clientKeyName);
46-
}
32+
33+
sprintf(rootCA, "%s/%s", shadow->certDir, AWS_IOT_ROOT_CA_FILENAME);
34+
sprintf(clientCRT, "%s/%s", shadow->certDir, AWS_IOT_CERTIFICATE_FILENAME);
35+
sprintf(clientKey, "%s/%s", shadow->certDir, AWS_IOT_PRIVATE_KEY_FILENAME);
4736

4837
syslog(LOG_DEBUG, "rootCA %s", rootCA);
4938
syslog(LOG_DEBUG, "clientCRT %s", clientCRT);
@@ -102,7 +91,7 @@ int passthru_shadow_connect(passthru_shadow *shadow) {
10291

10392
int passthru_shadow_report_delta(passthru_shadow *shadow) {
10493
syslog(LOG_DEBUG, "Sending delta report: %s", DELTA_REPORT);
105-
return passthru_shadow_update(shadow, DELTA_REPORT);
94+
return passthru_shadow_update(shadow, DELTA_REPORT, NULL);
10695
}
10796

10897
void passthru_shadow_get(passthru_shadow *shadow) {
@@ -115,9 +104,9 @@ void passthru_shadow_get(passthru_shadow *shadow) {
115104
}
116105
}
117106

118-
int passthru_shadow_update(passthru_shadow *shadow, char *message) {
107+
int passthru_shadow_update(passthru_shadow *shadow, char *message, void *pContextData) {
119108
syslog(LOG_DEBUG, "passthru_shadow_update: message=%s", message);
120-
shadow->rc = aws_iot_shadow_update(shadow->mqttClient, shadow->thingName, message, shadow->onupdate, NULL, 2, true);
109+
shadow->rc = aws_iot_shadow_update(shadow->mqttClient, shadow->thingName, message, shadow->onupdate, pContextData, 2, true);
121110
if(shadow->rc != SUCCESS) {
122111
char errmsg[255];
123112
sprintf(errmsg, "aws_iot_shadow_update error rc=%d", shadow->rc);

src/passthru_shadow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int passthru_shadow_connect(passthru_shadow *shadow);
100100
bool passthru_shadow_build_report_json(char *pJsonDocument, size_t maxSizeOfJsonDocument, const char *pReceivedDeltaData, uint32_t lengthDelta);
101101
int passthru_shadow_report_delta(passthru_shadow *shadow);
102102
void passthru_shadow_get(passthru_shadow *shadow);
103-
int passthru_shadow_update(passthru_shadow *shadow, char *message);
103+
int passthru_shadow_update(passthru_shadow *shadow, char *message, void *pContextData);
104104
int passthru_shadow_disconnect(passthru_shadow *shadow);
105105

106106
#endif

src/passthru_shadow_router.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818

1919
#include "passthru_shadow_router.h"
2020

21+
void passthru_shadow_router_print_desired(shadow_desired *desired) {
22+
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->j2534->state=%d, desired->j2534->error=%d", desired->j2534->state, desired->j2534->error);
23+
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->log->type=%d, desired->log->file=%s", desired->log->type, desired->log->file);
24+
}
25+
26+
void passthru_shadow_router_print_reported(shadow_report *reported) {
27+
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->j2534->state=%d, reported->j2534->error=%d", reported->j2534->state, reported->j2534->error);
28+
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->log->type=%d, reported->log->file=%s", reported->log->type, reported->log->file);
29+
}
30+
31+
void passthru_shadow_router_print_message(shadow_message *message) {
32+
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->connection=%i", message->state->reported->connection);
33+
passthru_shadow_router_print_desired(message->state->desired);
34+
passthru_shadow_router_print_reported(message->state->reported);
35+
}
36+
2137
void passthru_shadow_router_route_message(passthru_thing *thing, shadow_message *message) {
2238

2339
if(message == NULL || message->state == NULL) return;
@@ -49,19 +65,3 @@ void passthru_shadow_router_route_delta(passthru_thing *thing, shadow_desired *d
4965
}
5066
syslog(LOG_DEBUG, "passthru_shadow_router_route_delta: unable to locate delta handler");
5167
}
52-
53-
void passthru_shadow_router_print_message(shadow_message *message) {
54-
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->connection=%i", message->state->reported->connection);
55-
passthru_shadow_router_print_desired(message->state->desired);
56-
passthru_shadow_router_print_reported(message->state->reported);
57-
}
58-
59-
void passthru_shadow_router_print_desired(shadow_desired *desired) {
60-
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->j2534->state=%d, desired->j2534->error=%d", desired->j2534->state, desired->j2534->error);
61-
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: desired->log->type=%d, desired->log->file=%s", desired->log->type, desired->log->file);
62-
}
63-
64-
void passthru_shadow_router_print_reported(shadow_report *reported) {
65-
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->j2534->state=%d, reported->j2534->error=%d", reported->j2534->state, reported->j2534->error);
66-
syslog(LOG_DEBUG, "passthru_shadow_router_print_message: reported->log->type=%d, reported->log->file=%s", reported->log->type, reported->log->file);
67-
}

src/passthru_shadow_router.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "passthru_shadow_log_handler.h"
2626
#include "passthru_shadow_connection_handler.h"
2727

28-
void passthru_shadow_router_route(passthru_thing *thing, shadow_message *message);
28+
void passthru_shadow_router_route_message(passthru_thing *thing, shadow_message *message);
29+
void passthru_shadow_router_route_delta(passthru_thing *thing, shadow_desired *desired);
2930

3031
#endif

0 commit comments

Comments
 (0)