Skip to content

Commit

Permalink
Merge pull request #464 from openxc/combineprotomessages
Browse files Browse the repository at this point in the history
Combineprotomessages
  • Loading branch information
GenoJAFord committed Nov 10, 2020
2 parents 7cff489 + adaa900 commit 953e027
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 107 deletions.
72 changes: 18 additions & 54 deletions src/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,47 +294,47 @@ 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;
message.type = openxc_VehicleMessage_Type_DIAGNOSTIC;
message.diagnostic_response = {0};
message.diagnostic_response.bus = bus->address;

if(request->arbitration_id != OBD2_FUNCTIONAL_BROADCAST_ID) {
message.diagnostic_stitch_response.message_id = response->arbitration_id
message.diagnostic_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_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 =
message.diagnostic_response.mode = response->mode;
message.diagnostic_response.pid = response->pid;
message.diagnostic_response.success = response->success;
message.diagnostic_response.negative_response_code =
response->negative_response_code;

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

if(response->payload_length > 0) {
if (request->decoder != NULL) {
message.diagnostic_stitch_response.value = value;
message.diagnostic_response.value = value;
} else {
memcpy(message.diagnostic_stitch_response.payload.bytes, response->payload,
memcpy(message.diagnostic_response.payload.bytes, response->payload,
response->payload_length);
message.diagnostic_stitch_response.payload.size = response->payload_length;
message.diagnostic_response.payload.size = response->payload_length;
message.diagnostic_response.total_size = response->payload_length;
}
}
return message;
Expand Down Expand Up @@ -405,38 +405,6 @@ static void sendPartialMessage(long timestamp,
(uint8_t*)messageBuffer, messageLen, MessageClass::SIMPLE);
}


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

static void relayPartialFrame(DiagnosticsManager* manager, // Only need for the callback
ActiveDiagnosticRequest* request,
const DiagnosticResponse* response,
Pipeline* pipeline) {

int frame = prevFrame + 1;
if (response->completed) {
frame = -1; // Marks the last frame in the response
}
prevFrame = frame;

// see wrapDiagnosticResponseWithSabot
sendPartialMessage(00, // long timestamp,
frame, // int frame
response->arbitration_id, // int message_id,
request->bus->address, // int bus,
0, // int total_size,
response->mode, // int mode,
response->pid, // int pid,
0, // int value, - when the payload is a bitfield or numeric - parsed value
response->negative_response_code, // int negative_response_code
(char *)response->payload, // char *payload
response->payload_length,
pipeline);

if (response->completed && (request->callback != NULL)) {
request->callback(manager, request, response, diagnostic_payload_to_integer(response));
}
}
#endif

static void relayDiagnosticResponse(DiagnosticsManager* manager,
Expand Down Expand Up @@ -514,11 +482,7 @@ static void receiveCanMessage(DiagnosticsManager* manager,

if (response.multi_frame) {
#if (MULTIFRAME != 0)
if (originalStitchAlgo) {
relayPartialFrame(manager, entry, &response, pipeline);
} else {
relayDiagnosticResponse(manager, entry, &response, pipeline, true); // Added 6/11/2020
}
relayDiagnosticResponse(manager, entry, &response, pipeline, true); // Added 6/11/2020
#endif
if (!response.completed) {
time::tick(&entry->timeoutClock);
Expand Down
59 changes: 8 additions & 51 deletions src/payload/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ static bool serializeDiagnostic(openxc_VehicleMessage* message, cJSON* root) {
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_PID_FIELD_NAME,
message->diagnostic_response.pid);

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

if(message->diagnostic_response.negative_response_code != 0) {
cJSON_AddNumberToObject(root, payload::json::DIAGNOSTIC_NRC_FIELD_NAME,
message->diagnostic_response.negative_response_code);
Expand Down Expand Up @@ -97,55 +105,6 @@ 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 @@ -632,8 +591,6 @@ 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
1 change: 0 additions & 1 deletion src/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ 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 953e027

Please sign in to comment.