Skip to content

Commit

Permalink
Merge pull request #451 from openxc/protobufmessageupdate
Browse files Browse the repository at this point in the history
Added Stitch Proto message for protobuf mode
  • Loading branch information
GenoJAFord committed Jun 18, 2020
2 parents f44d9ba + 98a7b29 commit a079b3a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 8 deletions.
74 changes: 69 additions & 5 deletions src/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,58 @@ static openxc_VehicleMessage wrapDiagnosticResponseWithSabot(CanBus* bus,
return message;
}

const bool originalStitchAlgo = false;
static int prevFrame = -1;
static openxc_VehicleMessage wrapDiagnosticStitchResponseWithSabot(CanBus* bus,
const ActiveDiagnosticRequest* request,
const DiagnosticResponse* response, openxc_DynamicField value) {
openxc_VehicleMessage message = openxc_VehicleMessage(); // Zero fill
message.type = openxc_VehicleMessage_Type_DIAGNOSTIC_STITCH;
message.diagnostic_stitch_response = {0};
message.diagnostic_stitch_response.bus = bus->address;

if(request->arbitration_id != OBD2_FUNCTIONAL_BROADCAST_ID) {
message.diagnostic_stitch_response.message_id = response->arbitration_id
- DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET;
} else {
// must preserve responding arb ID for responses to functional broadcast
// requests, as they are the actual module address and not just arb ID +
// 8.
message.diagnostic_stitch_response.message_id = response->arbitration_id;
}

message.diagnostic_stitch_response.mode = response->mode;
message.diagnostic_stitch_response.pid = response->pid;
message.diagnostic_stitch_response.success = response->success;
message.diagnostic_stitch_response.negative_response_code =
response->negative_response_code;

message.diagnostic_stitch_response.frame = prevFrame + 1;
if (response->completed) {
message.diagnostic_stitch_response.frame = -1; // Marks the last frame in the response
} else {
message.diagnostic_stitch_response.success = true;
}
prevFrame = message.diagnostic_stitch_response.frame;

if(response->payload_length > 0) {
if (request->decoder != NULL) {
message.diagnostic_stitch_response.value = value;
} else {
memcpy(message.diagnostic_stitch_response.payload.bytes, response->payload,
response->payload_length);
message.diagnostic_stitch_response.payload.size = response->payload_length;
}
}
return message;
}

#if (MULTIFRAME != 0)
// The next 2 methods are the origial way that multi-frame
// stitching was achieved. It has been replaced with a new
// openxc.proto file and uses a new message type.
// These are here for reference until we know the new messages are working
// 6/15/2020
const int MAX_MULTI_FRAME_MESSAGE_SIZE = 300;

static void sendPartialMessage(long timestamp,
Expand Down Expand Up @@ -357,7 +408,6 @@ static void sendPartialMessage(long timestamp,

// relayPartialFrame - Send the partial frame to the mobile device/web

static int prevFrame = -1;
static void relayPartialFrame(DiagnosticsManager* manager, // Only need for the callback
ActiveDiagnosticRequest* request,
const DiagnosticResponse* response,
Expand Down Expand Up @@ -391,7 +441,8 @@ static void relayPartialFrame(DiagnosticsManager* manager, // Only need for the

static void relayDiagnosticResponse(DiagnosticsManager* manager,
ActiveDiagnosticRequest* request,
const DiagnosticResponse* response, Pipeline* pipeline) {
const DiagnosticResponse* response, Pipeline* pipeline,
bool stitchMessage) {
float parsed_value = diagnostic_payload_to_integer(response);

uint8_t buf_size = response->multi_frame ? response->payload_length + 1 : 20;
Expand Down Expand Up @@ -425,6 +476,15 @@ static void relayDiagnosticResponse(DiagnosticsManager* manager,
} else {
publishNumericalMessage(request->name, field.numeric_value, pipeline);
}
} else if (stitchMessage) {
openxc_VehicleMessage message = wrapDiagnosticStitchResponseWithSabot(
request->bus, request, response, field);
pipeline::publish(&message, pipeline);

if (!(response->completed)) {
return; // Only return if this is not the last partial
// Otherwise if this is last we want to invoke the callback
}
} else {
// If no name, send full details of response but still include 'value'
// instead of 'payload' if they provided a decoder. The one case you
Expand Down Expand Up @@ -454,21 +514,25 @@ static void receiveCanMessage(DiagnosticsManager* manager,

if (response.multi_frame) {
#if (MULTIFRAME != 0)
relayPartialFrame(manager, entry, &response, pipeline);
if (originalStitchAlgo) {
relayPartialFrame(manager, entry, &response, pipeline);
} else {
relayDiagnosticResponse(manager, entry, &response, pipeline, true); // Added 6/11/2020
}
#endif
if (!response.completed) {
time::tick(&entry->timeoutClock);
} else {
#if (MULTIFRAME == 0)
// This is the pre 2020 Way of sending a Diagnostic Response
// (all at once)
relayDiagnosticResponse(manager, entry, &response, pipeline);
relayDiagnosticResponse(manager, entry, &response, pipeline, false);
#endif
}
} else if (response.completed && entry->handle.completed) {
if(entry->handle.success) {
// Handle Single frame messages here!
relayDiagnosticResponse(manager, entry, &response, pipeline);
relayDiagnosticResponse(manager, entry, &response, pipeline, false);
} else {
debug("Fatal error sending or receiving diagnostic request");
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/openxc-message-format
2 changes: 1 addition & 1 deletion src/libs/uds-c
54 changes: 54 additions & 0 deletions src/payload/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const char openxc::payload::json::DIAGNOSTIC_SUCCESS_FIELD_NAME[] = "success";
const char openxc::payload::json::DIAGNOSTIC_NRC_FIELD_NAME[] = "negative_response_code";
const char openxc::payload::json::DIAGNOSTIC_PAYLOAD_FIELD_NAME[] = "payload";
const char openxc::payload::json::DIAGNOSTIC_VALUE_FIELD_NAME[] = "value";
const char openxc::payload::json::DIAGNOSTIC_FRAME_FIELD_NAME[] = "frame";
const char openxc::payload::json::DIAGNOSTIC_TOTAL_SIZE_FIELD_NAME[] = "total_size";


static bool serializeDiagnostic(openxc_VehicleMessage* message, cJSON* root) {
cJSON_AddNumberToObject(root, payload::json::BUS_FIELD_NAME,
Expand Down Expand Up @@ -95,6 +98,55 @@ static bool serializeDiagnostic(openxc_VehicleMessage* message, cJSON* root) {
return true;
}

static bool serializeStitchDiagnostic(openxc_VehicleMessage* message, cJSON* root) {
cJSON_AddNumberToObject(root, payload::json::BUS_FIELD_NAME,
message->diagnostic_stitch_response.bus);
cJSON_AddNumberToObject(root, payload::json::ID_FIELD_NAME,
message->diagnostic_stitch_response.message_id);
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_MODE_FIELD_NAME,
message->diagnostic_stitch_response.mode);
cJSON_AddBoolToObject(root, payload::json::DIAGNOSTIC_SUCCESS_FIELD_NAME,
message->diagnostic_stitch_response.success);
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_PID_FIELD_NAME,
message->diagnostic_stitch_response.pid);

// These next 2 fields are only in a stitched message frame
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_FRAME_FIELD_NAME,
message->diagnostic_stitch_response.frame);
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_TOTAL_SIZE_FIELD_NAME,
message->diagnostic_stitch_response.total_size);

if(message->diagnostic_stitch_response.negative_response_code != 0) {
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_NRC_FIELD_NAME,
message->diagnostic_stitch_response.negative_response_code);
}

if(message->diagnostic_stitch_response.value.type != openxc_DynamicField_Type_UNUSED) {
if (message->diagnostic_stitch_response.value.type == openxc_DynamicField_Type_NUM) {
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_VALUE_FIELD_NAME,
message->diagnostic_stitch_response.value.numeric_value);
} else {
cJSON_AddStringToObject(root, payload::json::DIAGNOSTIC_VALUE_FIELD_NAME,
message->diagnostic_stitch_response.value.string_value);
}
} else if(message->diagnostic_stitch_response.payload.size > 0) {
char encodedData[MAX_DIAGNOSTIC_PAYLOAD_SIZE];
const char* maxAddress = encodedData + sizeof(encodedData);
char* encodedDataIndex = encodedData;
encodedDataIndex += sprintf(encodedDataIndex, "0x");
for(uint8_t i = 0; i < message->diagnostic_stitch_response.payload.size &&
encodedDataIndex < maxAddress; i++) {
encodedDataIndex += snprintf(encodedDataIndex,
maxAddress - encodedDataIndex,
"%02x",
message->diagnostic_stitch_response.payload.bytes[i]);
}
cJSON_AddStringToObject(root, payload::json::DIAGNOSTIC_PAYLOAD_FIELD_NAME,
encodedData);
}
return true;
}

static bool serializeCommandResponse(openxc_VehicleMessage* message,
cJSON* root) {
const char* typeString = NULL;
Expand Down Expand Up @@ -581,6 +633,8 @@ int openxc::payload::json::serialize(openxc_VehicleMessage* message,
status = serializeCan(message, root);
} else if(message->type == openxc_VehicleMessage_Type_DIAGNOSTIC) {
status = serializeDiagnostic(message, root);
} else if(message->type == openxc_VehicleMessage_Type_DIAGNOSTIC_STITCH) {
status =serializeStitchDiagnostic(message, root);
} else if(message->type == openxc_VehicleMessage_Type_COMMAND_RESPONSE) {
status = serializeCommandResponse(message, root);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/payload/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ extern const char DIAGNOSTIC_SUCCESS_FIELD_NAME[];
extern const char DIAGNOSTIC_NRC_FIELD_NAME[];
extern const char DIAGNOSTIC_PAYLOAD_FIELD_NAME[];
extern const char DIAGNOSTIC_VALUE_FIELD_NAME[];
extern const char DIAGNOSTIC_FRAME_FIELD_NAME[];
extern const char DIAGNOSTIC_TOTAL_SIZE_FIELD_NAME[];

extern const char MODEM_CONFIGURATION_COMMAND_NAME[];
extern const char RTC_CONFIGURATION_COMMAND_NAME[];
Expand Down
2 changes: 1 addition & 1 deletion src/payload/payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void dumpPayload(unsigned char *payload, size_t length) {
}
}

static void dumpNum(int value) {
void dumpNum(int value) {
char buf[10];
sprintf(buf,"%d",value);
debug(buf);
Expand Down
1 change: 1 addition & 0 deletions src/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void openxc::pipeline::publish(openxc_VehicleMessage* message,
matched = true;
break;
case openxc_VehicleMessage_Type_DIAGNOSTIC:
case openxc_VehicleMessage_Type_DIAGNOSTIC_STITCH:
messageClass = MessageClass::DIAGNOSTIC;
matched = true;
break;
Expand Down

0 comments on commit a079b3a

Please sign in to comment.