Skip to content

Commit

Permalink
Merge pull request #419 from openxc/protobuf3
Browse files Browse the repository at this point in the history
Protobuf3
  • Loading branch information
pjt0620 committed Oct 29, 2019
2 parents 6fa7a02 + ddb7635 commit 4ed6bae
Show file tree
Hide file tree
Showing 32 changed files with 212 additions and 730 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
[submodule "src/libs/AT-commander"]
path = src/libs/AT-commander
url = https://github.com/openxc/AT-commander
[submodule "src/libs/nanopb"]
path = src/libs/nanopb
url = https://github.com/nanopb/nanopb.git
[submodule "src/libs/openxc-message-format"]
path = src/libs/openxc-message-format
url = https://github.com/openxc/openxc-message-format
Expand Down
8 changes: 4 additions & 4 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ CROSSPLATFORM_C_SRCS += $(wildcard $(LIBS_PATH)/uds-c/deps/isotp-c/src/**/*.c)
CROSSPLATFORM_C_SRCS += $(wildcard $(LIBS_PATH)/uds-c/src/**/*.c)
CROSSPLATFORM_C_SRCS += $(wildcard $(LIBS_PATH)/cmp/cmp.c)
# TODO put this under a "gen" folder separate from objdir
CROSSPLATFORM_C_SRCS += $(LIBS_PATH)/nanopb/pb_encode.c \
$(LIBS_PATH)/nanopb/pb_decode.c \
$(LIBS_PATH)/nanopb/pb_common.c
CROSSPLATFORM_C_SRCS += $(LIBS_PATH)/openxc-message-format/libs/nanopb/pb_encode.c \
$(LIBS_PATH)/openxc-message-format/libs/nanopb/pb_decode.c \
$(LIBS_PATH)/openxc-message-format/libs/nanopb/pb_common.c
CROSSPLATFORM_C_SRCS += $(LIBS_PATH)/openxc-message-format/gen/cpp/openxc.pb.c
CROSSPLATFORM_CPP_SRCS = $(wildcard *.cpp)
CROSSPLATFORM_CPP_SRCS += $(wildcard can/*.cpp)
Expand All @@ -174,7 +174,7 @@ CROSSPLATFORM_CPP_SRCS += $(wildcard payload/*.cpp)

INCLUDE_PATHS = -I. -I$(LIBS_PATH)/cJSON -I$(LIBS_PATH)/emqueue \
-I$(LIBS_PATH)/AT-commander/atcommander \
-I$(LIBS_PATH)/nanopb \
-I$(LIBS_PATH)/openxc-message-format/libs/nanopb \
-I$(LIBS_PATH)/openxc-message-format/gen/cpp \
-I$(LIBS_PATH)/uds-c/deps/isotp-c/deps/bitfield-c/src \
-I$(LIBS_PATH)/uds-c/deps/isotp-c/src \
Expand Down
22 changes: 5 additions & 17 deletions src/can/canread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ openxc_DynamicField openxc::can::read::ignoreDecoder(CanSignal* signal,
CanSignal* signals, int signalCount, Pipeline* pipeline, float value,
bool* send) {
*send = false;
openxc_DynamicField decodedValue = {0};
openxc_DynamicField decodedValue = openxc_DynamicField(); // Zero fill
return decodedValue;
}

openxc_DynamicField openxc::can::read::stateDecoder(CanSignal* signal,
CanSignal* signals, int signalCount, Pipeline* pipeline, float value,
bool* send) {
openxc_DynamicField decodedValue = {0};
decodedValue.has_type = true;
openxc_DynamicField decodedValue = openxc_DynamicField(); // Zero fill
decodedValue.type = openxc_DynamicField_Type_STRING;
decodedValue.has_string_value = true;

const CanSignalState* signalState = lookupSignalState(value, signal);
if(signalState != NULL) {
Expand All @@ -61,27 +59,22 @@ openxc_DynamicField openxc::can::read::stateDecoder(CanSignal* signal,

static void buildBaseSimpleVehicleMessage(openxc_VehicleMessage* message,
const char* name) {
message->has_type = true;
message->type = openxc_VehicleMessage_Type_SIMPLE;
message->has_simple_message = true;
message->simple_message = {0};
message->simple_message.has_name = true;
message->simple_message = openxc_SimpleMessage(); // Zero Fill
strcpy(message->simple_message.name, name);
}

void openxc::can::read::publishVehicleMessage(const char* name,
openxc_DynamicField* value, openxc_DynamicField* event,
openxc::pipeline::Pipeline* pipeline) {
openxc_VehicleMessage message = {0};
openxc_VehicleMessage message = openxc_VehicleMessage(); // Zero fill
buildBaseSimpleVehicleMessage(&message, name);

if(value != NULL) {
message.simple_message.has_value = true;
message.simple_message.value = *value;
}

if(event != NULL) {
message.simple_message.has_event = true;
message.simple_message.event = *event;
}

Expand Down Expand Up @@ -150,16 +143,11 @@ void openxc::can::read::passthroughMessage(CanBus* bus, CanMessage* message,
size_t adjustedSize = message->length == 0 ?
CAN_MESSAGE_SIZE : message->length;
if(send) {
openxc_VehicleMessage vehicleMessage = {0};
vehicleMessage.has_type = true;
openxc_VehicleMessage vehicleMessage = openxc_VehicleMessage(); // Zero fill
vehicleMessage.type = openxc_VehicleMessage_Type_CAN;
vehicleMessage.has_can_message = true;
vehicleMessage.can_message = {0};
vehicleMessage.can_message.has_id = true;
vehicleMessage.can_message.id = message->id;
vehicleMessage.can_message.has_bus = true;
vehicleMessage.can_message.bus = bus->address;
vehicleMessage.can_message.has_data = true;
vehicleMessage.can_message.data.size = adjustedSize;
memcpy(vehicleMessage.can_message.data.bytes, message->data,
adjustedSize);
Expand Down
12 changes: 3 additions & 9 deletions src/can/canwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,23 @@ bool openxc::can::write::encodeAndSendSignal(CanSignal* signal,
}

bool openxc::can::write::encodeAndSendNumericSignal(CanSignal* signal, float value, bool force) {
openxc_DynamicField field = {0};
field.has_type = true;
openxc_DynamicField field = openxc_DynamicField(); // Zero fill
field.type = openxc_DynamicField_Type_NUM;
field.has_numeric_value = true;
field.numeric_value = value;
return encodeAndSendSignal(signal, &field, force);
}

bool openxc::can::write::encodeAndSendStateSignal(CanSignal* signal, const char* value,
bool force) {
openxc_DynamicField field = {0};
field.has_type = true;
openxc_DynamicField field = openxc_DynamicField(); // Zero fill
field.type = openxc_DynamicField_Type_STRING;
field.has_string_value = true;
strcpy(field.string_value, value);
return encodeAndSendSignal(signal, &field, force);
}

bool openxc::can::write::encodeAndSendBooleanSignal(CanSignal* signal, bool value, bool force) {
openxc_DynamicField field = {0};
field.has_type = true;
openxc_DynamicField field = openxc_DynamicField(); // Zero fill
field.type = openxc_DynamicField_Type_BOOL;
field.has_boolean_value = true;
field.boolean_value = value;
return encodeAndSendSignal(signal, &field, force);
}
Expand Down
14 changes: 4 additions & 10 deletions src/commands/af_bypass_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ namespace pipeline = openxc::pipeline;

bool openxc::commands::validateFilterBypassCommand(openxc_VehicleMessage* message) {
bool valid = false;
if(message->has_control_command) {
if(message->type == openxc_VehicleMessage_Type_CONTROL_COMMAND) {
openxc_ControlCommand* command = &message->control_command;
if(command->has_acceptance_filter_bypass_command) {
if(command->type == openxc_ControlCommand_Type_ACCEPTANCE_FILTER_BYPASS) {
openxc_AcceptanceFilterBypassCommand* bypassCommand =
&command->acceptance_filter_bypass_command;
if(bypassCommand->has_bus && bypassCommand->has_bypass) {
if (bypassCommand != NULL) {
valid = true;
}
}
Expand All @@ -31,24 +31,18 @@ bool openxc::commands::validateFilterBypassCommand(openxc_VehicleMessage* messag

bool openxc::commands::handleFilterBypassCommand(openxc_ControlCommand* command) {
bool status = false;
if(command->has_acceptance_filter_bypass_command) {
if(command->type == openxc_ControlCommand_Type_ACCEPTANCE_FILTER_BYPASS) {
openxc_AcceptanceFilterBypassCommand* bypassCommand =
&command->acceptance_filter_bypass_command;
if(bypassCommand->has_bus && bypassCommand->has_bypass) {
CanBus* bus = NULL;
if(bypassCommand->has_bus) {
bus = lookupBus(bypassCommand->bus, getCanBuses(),
getCanBusCount());
} else {
debug("AF bypass command missing bus");
}

if(bus != NULL) {
openxc::can::setAcceptanceFilterStatus(bus,
!bypassCommand->bypass, getCanBuses(), getCanBusCount());
status = true;
}
}
}

sendCommandResponse(openxc_ControlCommand_Type_ACCEPTANCE_FILTER_BYPASS,
Expand Down
23 changes: 8 additions & 15 deletions src/commands/can_message_write_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ namespace interface = openxc::interface;
bool openxc::commands::handleCan(openxc_VehicleMessage* message,
openxc::interface::InterfaceDescriptor* sourceInterfaceDescriptor) {
bool status = true;
if(message->has_can_message) {
if(message->type == openxc_VehicleMessage_Type_CAN) {
openxc_CanMessage* canMessage = &message->can_message;
CanBus* matchingBus = NULL;
if(canMessage->has_bus) {
if(canMessage->bus >= 0) {
matchingBus = lookupBus(canMessage->bus, getCanBuses(), getCanBusCount());
} else if(getCanBusCount() > 0) {
}
if((matchingBus == NULL) && (canMessage->bus == 0) && (getCanBusCount() > 0)) { // Could not find a bus of 0 so use 1st one if one not asked for
matchingBus = &getCanBuses()[0];
debug("No bus specified for write, using the first active: %d", matchingBus->address);
}

if(!sourceInterfaceDescriptor->allowRawWrites) {
debug("Direct CAN message writes not allowed from %s interface",
interface::descriptorToString(sourceInterfaceDescriptor));
Expand All @@ -44,7 +44,7 @@ bool openxc::commands::handleCan(openxc_VehicleMessage* message,
uint8_t size = canMessage->data.size;
CanMessageFormat format;

if(canMessage->has_frame_format) {
if(canMessage->frame_format != openxc_CanMessage_FrameFormat_UNUSED) {
format = canMessage->frame_format ==
openxc_CanMessage_FrameFormat_STANDARD ?
CanMessageFormat::STANDARD :
Expand All @@ -71,21 +71,14 @@ bool openxc::commands::handleCan(openxc_VehicleMessage* message,

bool openxc::commands::validateCan(openxc_VehicleMessage* message) {
bool valid = true;
if(message->has_type && message->type == openxc_VehicleMessage_Type_CAN &&
message->has_can_message) {
if(message->type == openxc_VehicleMessage_Type_CAN) {
openxc_CanMessage* canMessage = &message->can_message;
if(!canMessage->has_id) {
if(canMessage->id == 0) {
valid = false;
debug("Write request is malformed, missing id");
}

if(!canMessage->has_data) {
valid = false;
debug("Raw write request for 0x%02x missing data", canMessage->id);
}

if(canMessage->has_frame_format &&
canMessage->frame_format == openxc_CanMessage_FrameFormat_STANDARD &&
if (canMessage->frame_format == openxc_CanMessage_FrameFormat_STANDARD &&
canMessage->id > 0xff) {
valid = false;
debug("ID in raw write request (0x%02x) is too large "
Expand Down
22 changes: 7 additions & 15 deletions src/commands/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using openxc::interface::InterfaceType;

static bool handleComplexCommand(openxc_VehicleMessage* message) {
bool status = true;
if(message != NULL && message->has_control_command) {
if(message != NULL && message->type == openxc_VehicleMessage_Type_CONTROL_COMMAND) {
openxc_ControlCommand* command = &message->control_command;
switch(command->type) {
case openxc_ControlCommand_Type_DIAGNOSTIC:
Expand Down Expand Up @@ -60,7 +60,7 @@ static bool handleComplexCommand(openxc_VehicleMessage* message) {
break;
case openxc_ControlCommand_Type_RTC_CONFIGURATION:
status = openxc::commands::handleRTCConfigurationCommand(command);
break;
break;
case openxc_ControlCommand_Type_SD_MOUNT_STATUS:
status = openxc::commands::handleSDMountStatusCommand();
default:
Expand All @@ -73,7 +73,7 @@ static bool handleComplexCommand(openxc_VehicleMessage* message) {

size_t openxc::commands::handleIncomingMessage(uint8_t payload[], size_t length,
openxc::interface::InterfaceDescriptor* sourceInterfaceDescriptor) {
openxc_VehicleMessage message = {0};
openxc_VehicleMessage message = openxc_VehicleMessage(); // Zero fill
size_t bytesRead = 0;

#if (DO_NOT_PROCESS_BINARY_UART_PROTOBUFF == 1)
Expand Down Expand Up @@ -121,10 +121,8 @@ size_t openxc::commands::handleIncomingMessage(uint8_t payload[], size_t length,
}

static bool validateControlCommand(openxc_VehicleMessage* message) {
bool valid = message->has_type &&
message->type == openxc_VehicleMessage_Type_CONTROL_COMMAND &&
message->has_control_command &&
message->control_command.has_type;
bool valid = (message->type == openxc_VehicleMessage_Type_CONTROL_COMMAND) &&
(message->control_command.type != openxc_ControlCommand_Type_UNUSED);
if(valid) {
switch(message->control_command.type) {
case openxc_ControlCommand_Type_DIAGNOSTIC:
Expand Down Expand Up @@ -164,7 +162,7 @@ static bool validateControlCommand(openxc_VehicleMessage* message) {

bool openxc::commands::validate(openxc_VehicleMessage* message) {
bool valid = false;
if(message != NULL && message->has_type) {
if(message != NULL) {
switch(message->type) {
case openxc_VehicleMessage_Type_CAN:
valid = validateCan(message);
Expand All @@ -185,17 +183,11 @@ bool openxc::commands::validate(openxc_VehicleMessage* message) {

void openxc::commands::sendCommandResponse(openxc_ControlCommand_Type commandType,
bool status, char* responseMessage, size_t responseMessageLength) {
openxc_VehicleMessage message = {0};
message.has_type = true;
openxc_VehicleMessage message = openxc_VehicleMessage(); // Zero Fill
message.type = openxc_VehicleMessage_Type_COMMAND_RESPONSE;
message.has_command_response = true;
message.command_response.has_type = true;
message.command_response.type = commandType;
message.command_response.has_message = false;
message.command_response.has_status = true;
message.command_response.status = status;
if(responseMessage != NULL && responseMessageLength > 0) {
message.command_response.has_message = true;
strncpy(message.command_response.message, responseMessage,
MIN(sizeof(message.command_response.message),
responseMessageLength));
Expand Down
11 changes: 5 additions & 6 deletions src/commands/diagnostic_request_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,24 @@ bool openxc::commands::handleDiagnosticRequestCommand(openxc_ControlCommand* com

bool openxc::commands::validateDiagnosticRequest(openxc_VehicleMessage* message) {
bool valid = true;
if(message->has_control_command) {
if(message->type == openxc_VehicleMessage_Type_CONTROL_COMMAND) {
openxc_ControlCommand* command = &message->control_command;
if(command->has_type &&
command->type == openxc_ControlCommand_Type_DIAGNOSTIC) {
if(command->type == openxc_ControlCommand_Type_DIAGNOSTIC) {
openxc_DiagnosticControlCommand* diagControlCommand =
&command->diagnostic_request;
openxc_DiagnosticRequest* request = &diagControlCommand->request;

if(!diagControlCommand->has_action) {
if(diagControlCommand->action == openxc_DiagnosticControlCommand_Action_UNUSED) {
valid = false;
debug("Diagnostic request command missing action");
}

if(!request->has_message_id) {
if(request->message_id == 0) {
valid = false;
debug("Diagnostic request missing message ID");
}

if(!request->has_mode) {
if(request->mode == 0) {
valid = false;
debug("Diagnostic request missing mode");
}
Expand Down
12 changes: 5 additions & 7 deletions src/commands/modem_config_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ using openxc::config::getConfiguration;

bool openxc::commands::validateModemConfigurationCommand(openxc_VehicleMessage* message) {
bool valid = false;
if(message->has_control_command) {
if(message->type == openxc_VehicleMessage_Type_CONTROL_COMMAND) {
openxc_ControlCommand* command = &message->control_command;
if(command->has_modem_configuration_command) {
if(command->type == openxc_ControlCommand_Type_MODEM_CONFIGURATION) {
valid = true;
}
}
Expand All @@ -23,28 +23,26 @@ bool openxc::commands::validateModemConfigurationCommand(openxc_VehicleMessage*
bool openxc::commands::handleModemConfigurationCommand(openxc_ControlCommand* command) {
#ifdef TELIT_HE910_SUPPORT
bool status = false;
if(command->has_modem_configuration_command) {
if(command->type == openxc_ControlCommand_Type_MODEM_CONFIGURATION) {
openxc_ModemConfigurationCommand* modemConfigurationCommand =
&command->modem_configuration_command;
// extract configs for each has_<sub-messages>
if(modemConfigurationCommand->has_serverConnectSettings) {
openxc_ServerConnectSettings* serverConnectSettings =
&modemConfigurationCommand->serverConnectSettings;
if(serverConnectSettings->has_host) {
if(strlen(serverConnectSettings->host)>0) {
strcpy(getConfiguration()->telit->config.serverConnectSettings.host,
serverConnectSettings->host);
status = true;
debug("Set server address to %s",
getConfiguration()->telit->config.serverConnectSettings.host);
status = true;
}
if(serverConnectSettings->has_port) {
if(serverConnectSettings->port > 0) {
getConfiguration()->telit->config.serverConnectSettings.port =
serverConnectSettings->port;
debug("Set server port to %u",
getConfiguration()->telit->config.serverConnectSettings.port);
}
}
}

if(status) {
Expand Down
Loading

0 comments on commit 4ed6bae

Please sign in to comment.