Skip to content

Commit

Permalink
Add remaining PassThruStartMsgFilter implementation to passthru device
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Hahn committed Oct 29, 2016
1 parent 0d171ea commit abe63f4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 23 deletions.
2 changes: 0 additions & 2 deletions bindings/ruby/lib/ecutools/j2534.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def PassThruStopPeriodicMsg(channelId, messageId)
end

# long PassThruStartMsgFilter(unsigned long ChannelID, unsigned long FilterType, PASSTHRU_MSG *pMaskMsg, PASSTHRU_MSG *pPatternMsg, unsigned long *pFilterID);

# attach_function :PassThruStartMsgFilter, [ :ulong, :ulong, Ecutools::J2534::Structs::PASSTHRU_MSG.by_ref, Ecutools::J2534::Structs::PASSTHRU_MSG.by_ref, :pointer ], :long

def PassThruStartMsgFilter(channelId, filterType, maskMsg, patternMsg, filterId)
pMaskMsg = map_model_to_passthru_msg(maskMsg, Ecutools::J2534::Structs::PASSTHRU_MSG.new)
pPatternMsg = map_model_to_passthru_msg(patternMsg, Ecutools::J2534::Structs::PASSTHRU_MSG.new)
Expand Down
1 change: 0 additions & 1 deletion src/canbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ int canbus_filter(canbus_client *canbus, struct can_filter *filters, unsigned in
syslog(LOG_ERR, "canbus_write: CAN socket not connected");
return 1;
}
syslog(LOG_DEBUG, "canbus_filter: applying filters");
int i;
for(i=0; i<filter_len; i++) {
syslog(LOG_DEBUG, "canbus_filter: can_id=%x, can_mask=%x", filters[i].can_id, filters[i].can_mask);
Expand Down
17 changes: 6 additions & 11 deletions src/j2534.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ void j2534_onerror(awsiot_client *awsiot, const char *message) {

char *filter_json(j2534_client *client) {

syslog(LOG_DEBUG, "filter_json: client->filters->count=%d", client->filters->count);

unsigned int json_len = client->filters->count * 27;
j2534_canfilter *canfilter = NULL;
char *json = malloc(sizeof(char) * json_len);
Expand Down Expand Up @@ -143,20 +141,21 @@ syslog(LOG_DEBUG, "filter_json: client->filters->count=%d", client->filters->cou
}
strcat(json, "]");

syslog(LOG_DEBUG, "filter_json: json=%s", json);

return json;
}

unsigned int j2534_publish_state(j2534_client *client, int desired_state) {

char json_format[255] = "{\"state\":{\"desired\":{\"j2534\":{\"deviceId\":%i,\"state\":%i}}}}";
char *msgfilters = filter_json(client);

char json_format[255] = "{\"state\":{\"desired\":{\"j2534\":{\"deviceId\":%i,\"state\":%i,\"filters\":%s}}}}";
unsigned int json_format_len = strlen(json_format) - 4;
unsigned int json_len = json_format_len + MYINT_LEN(desired_state) + MYINT_LEN(client->deviceId);
unsigned int json_len = json_format_len + MYINT_LEN(desired_state) + MYINT_LEN(client->deviceId) + strlen(msgfilters);

char json[json_len+1];
snprintf(json, json_len+1, json_format, client->deviceId, desired_state);
snprintf(json, json_len+1, json_format, client->deviceId, desired_state, msgfilters);
json[json_len+1] = '\0';
free(msgfilters);

if(awsiot_client_publish(client->awsiot, client->shadow_update_topic, (const char *)json) != 0) {
syslog(LOG_ERR, "j2534_publish_state: failed to publish. topic=%s, rc=%d", client->shadow_update_topic, client->awsiot->rc);
Expand Down Expand Up @@ -1520,10 +1519,6 @@ long PassThruStartMsgFilter(unsigned long ChannelID, unsigned long FilterType, P
filter->can_mask = pMaskMsg->DataBuffer;
vector_add(client->filters, filter);

filter_json(client);

return STATUS_NOERROR;

return unless_concurrent_call(
j2534_publish_state(client, J2534_PassThruStartMsgFilter),
J2534_PassThruStartMsgFilter
Expand Down
11 changes: 10 additions & 1 deletion src/passthru_shadow.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <syslog.h>
#include <pthread.h>
#include <errno.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include "vector.h"
#include "aws_iot_src/include/aws_iot_log.h"
#include "aws_iot_src/include/aws_iot_version.h"
#include "aws_iot_src/include/aws_iot_mqtt_client_interface.h"
Expand All @@ -43,10 +46,16 @@ typedef struct {
} shadow_log;

typedef struct {
canid_t can_id;
canid_t can_mask;
} shadow_j2534_filter;

typedef struct {
int *deviceId;
int *state;
int *error;
int *deviceId;
char *data;
vector *filters;
} shadow_j2534;

typedef struct {
Expand Down
15 changes: 9 additions & 6 deletions src/passthru_shadow_j2534_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,19 @@ void passthru_shadow_j2534_handler_desired_startMsgFilter(passthru_thing *thing,

client->state = J2534_PassThruStartMsgFilter;

struct can_filter *filters = malloc(sizeof(struct can_filter) * client->filters->size);
syslog(LOG_DEBUG, "passthru_shadow_j2534_handler_desired_startMsgFilter: j2534->filters->count=%i", j2534->filters->count);

for(i=0; i<client->filters->count; i++) {
struct can_filter *filters = malloc(sizeof(struct can_filter) * j2534->filters->count);
for(i=0; i<j2534->filters->count; i++) {

j2534_canfilter *canfilter = (j2534_canfilter *)vector_get(client->filters, i);
filters[i].can_id = canfilter->can_id;
filters[i].can_mask = canfilter->can_mask;
shadow_j2534_filter *shadow_filter = vector_get(j2534->filters, i);
filters[i].can_id = shadow_filter->can_id;
filters[i].can_mask = shadow_filter->can_mask;
}

canbus_filter(client->canbus, filters, client->filters->size);
if(j2534->filters->count) {
canbus_filter(client->canbus, filters, j2534->filters->count);
}

passthru_shadow_j2534_handler_send_report(J2534_PassThruStartMsgFilter);
}
Expand Down
53 changes: 52 additions & 1 deletion src/passthru_shadow_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ shadow_message* passthru_shadow_parser_parse_state(const char *json) {
message->state->desired->j2534->error = 0;
message->state->desired->j2534->data = NULL;
message->state->desired->j2534->deviceId = NULL;
message->state->desired->j2534->filters = malloc(sizeof(vector));
vector_init(message->state->desired->j2534->filters);
message->state->desired->connection = NULL;

root = json_loads(json, 0, &error);
Expand Down Expand Up @@ -85,6 +87,8 @@ shadow_message* passthru_shadow_parser_parse_state(const char *json) {

shadow_desired* passthru_shadow_parser_parse_delta(const char *json) {

syslog(LOG_DEBUG, "passthru_shadow_parser_parse_delta: json=%s", json);

json_t *root;
json_error_t error;

Expand All @@ -97,6 +101,8 @@ shadow_desired* passthru_shadow_parser_parse_delta(const char *json) {
desired->j2534->state = NULL;
desired->j2534->error = NULL;
desired->j2534->data = NULL;
desired->j2534->filters = malloc(sizeof(vector));
vector_init(desired->j2534->filters);
desired->connection = NULL;

root = json_loads(json, 0, &error);
Expand All @@ -122,14 +128,52 @@ shadow_desired* passthru_shadow_parser_parse_delta(const char *json) {

json_t *j2534 = json_object_get(root, "j2534");
if(json_is_object(j2534)) {

json_t *state = json_object_get(j2534, "state");
json_t *error = json_object_get(j2534, "error");
json_t *data = json_object_get(j2534, "data");
json_t *deviceId = json_object_get(j2534, "deviceId");
json_t *filters = json_object_get(j2534, "filters");
desired->j2534->state = json_integer_value(state);
desired->j2534->error = json_string_value(error);
desired->j2534->data = json_string_value(data);
desired->j2534->deviceId = json_integer_value(deviceId);

if(!json_is_array(filters)) {
syslog(LOG_ERR, "passthru_shadow_parser_parse_delta: J2534 filters is not an array");
return desired;
}

long j2534_filter_count = (unsigned long) json_array_size(filters);

int i;
for(i=0; i<j2534_filter_count; i++) {

json_t *filter, *filterId, *filterMask;
filter = json_array_get(filters, i);

if(!json_is_object(filter)) {
syslog(LOG_ERR, "passthru_shadow_parser_parse_delta: J2534 filter element is not an object");
return desired;
}

filterId = json_object_get(filter, "id");
if(!json_is_string(filterId)) {
syslog(LOG_ERR, "passthru_shadow_parser_parse_delta: filter id is not a string");
return desired;
}

filterMask = json_object_get(filter, "mask");
if(!json_is_string(filterMask)) {
syslog(LOG_ERR, "passthru_shadow_parser_parse_delta: filter mask is not a string");
return desired;
}

shadow_j2534_filter *j2534_filter = malloc(sizeof(shadow_j2534_filter));
j2534_filter->can_id = strtoul(json_string_value(filterId), NULL, 16);
j2534_filter->can_mask = strtoul(json_string_value(filterMask), NULL, 16);
vector_add(desired->j2534->filters, j2534_filter);
}
}

return desired;
Expand Down Expand Up @@ -207,7 +251,6 @@ void passthru_shadow_parser_parse_desired(json_t *obj, shadow_message *message)
message->state->desired->j2534->data = json_string_value(data);
message->state->desired->j2534->deviceId = json_integer_value(deviceId);
}

}
}

Expand All @@ -218,6 +261,14 @@ void passthru_shadow_parser_free_desired(shadow_desired *desired) {
desired->log = NULL;
}
if(desired->j2534 != NULL) {
if(desired->j2534->filters != NULL) {
int i;
for(i=0; i<desired->j2534->filters->count; i++) {
free(vector_get(desired->j2534->filters, i));
}
vector_free(desired->j2534->filters);
desired->j2534->filters = NULL;
}
free(desired->j2534);
desired->j2534 = NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion src/passthru_shadow_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#include "passthru_shadow_router.h"

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

Expand Down

0 comments on commit abe63f4

Please sign in to comment.