{%- if msg_defs_header | length -%} #include #include "{{ msg_defs_header }}" {%- endif %} {%- if cpp_namespace is defined and cpp_namespace is not none %} namespace {{ cpp_namespace }} { {%- endif %} {%- for (msg_id_value, msg) in msg_cpp.data.items() %} {%- if proto is defined %} // {{ msg['msg_id'] }} ({{ msg['msg_id_hex_string'] }}) // payload: {{ msg_cpp.field_list(msg_id_value) | join(", ") }} void {{ msg['msg_name'] }}Payload::fromProtobuf([[maybe_unused]] const messages::{{ msg['msg_name'] }} &src) { {%- for field in msg['payload'] %} {%- if field['type'] != "union" %} // {{ field['type'] ~ "-" ~ field['name'] }} {{ field['name'] }}.value = src.{{ field['name'].lower() }}(); {%- else %} // TODO: {{ field['type'] ~ "-" ~ field['name'] }} {%- endif %} {%- endfor %} } {%- endif %} // {{ msg['msg_id'] }} ({{ msg['msg_id_hex_string'] }}) // payload: {{ msg_cpp.field_list(msg_id_value) | join(", ") }} bool {{ msg['msg_name'] }}Payload::fromQByteArray([[maybe_unused]] const QByteArray &src) { {%- if msg['payload'] | length %} int offset = 0; return ( {%- set fields = namespace(count = 0) %} {%- for field in msg['payload'] %} {%- if field['type'] == "union" %} // TODO: {{ field['type'] ~ "-" ~ field['name'] }} {%- if fields.count < msg['payload'] | length - 1 %} true && {%- else %} true {%- endif %} {%- else %} // {{ field['type'] ~ "-" ~ field['name'] }} {%- if fields.count < msg['payload'] | length - 1 %} Types::getValue<>(src, offset, {{ field['name'] }}, QT_STRINGIFY({{ field['name'] }})) && {%- else %} Types::getValue<>(src, offset, {{ field['name'] }}, QT_STRINGIFY({{ field['name'] }})) {%- endif %} {%- endif %} {%- set fields.count = fields.count + 1 %} {%- endfor %} ); {%- else %} return true; {%- endif %} } {%- if proto is defined %} // {{ msg['msg_id'] }} ({{ msg['msg_id_hex_string'] }}) // payload: {{ msg_cpp.field_list(msg_id_value) | join(", ") }} messages::{{ msg['msg_name'] }} {{ msg['msg_name'] }}Payload::toProtobuf() const { messages::{{ msg['msg_name'] }} protoMsg; {%- for field in msg['payload'] %} {%- if field['type'] != "union" %} // {{ field['type'] ~ "-" ~ field['name'] }} protoMsg.set_{{ field['name'].lower() }}({{ field['name'] }}.value); {%- else %} // TODO: {{ field['type'] ~ "-" ~ field['name'] }} {%- endif %} {%- endfor %} return protoMsg; } {%- endif %} // {{ msg['msg_id'] }} ({{ msg['msg_id_hex_string'] }}) // payload: {{ msg_cpp.field_list(msg_id_value) | join(", ") }} void {{ msg['msg_name'] }}Payload::toQByteArray([[maybe_unused]] QByteArray &dst) const { {%- if msg['payload'] | length %} {%- for field in msg['payload'] %} {%- if field['type'] == "union" %} // TODO: {{ field['type'] ~ "-" ~ field['name'] }} {%- else %} // {{ field['type'] ~ "-" ~ field['name'] }} Types::setValue<>({{ field['name'] }}, dst); {%- endif %} {%- endfor %} {%- endif %} } void {{ msg['msg_name'] }}Payload::dump() const { QStringList params; {%- for field in msg['payload'] %} {%- if field['type'] != "union" %} params << QString("{{ field['name'] }}=%1").arg({{ field['name'] }}.value); {%- endif %} {%- endfor %} qDebug().noquote() << QString("{{ msg['msg_name'] }}Payload: %1").arg(params.count() ? params.join(", ") : ""); } {%- endfor %} QByteArray canMessageToProtobufByteArray(const QDateTime ×tamp, const QString &deviceSerialNum, const Can::Message &msg) { static const auto updateHeader = [&](messages::Header *header) { if (header) { const auto msecs = timestamp.toMSecsSinceEpoch(); header->set_deviceserialnum(deviceSerialNum.toStdString()); auto proto_timestamp = header->mutable_timestamp(); if (proto_timestamp) { proto_timestamp->set_seconds(msecs / 1000); proto_timestamp->set_nanos((msecs % 1000) * 1000000); } header->set_msgid(msg.msgId); header->set_sequence(msg.sequence); } }; switch (msg.msgId) { {%- for (msg_id_value, msg) in msg_cpp.data.items() %} case {{ msg['msg_id'] }}: { {{ msg['msg_name'] }}Payload payload; if (payload.fromQByteArray(msg.data) == false) { qDebug().noquote() << "ERROR: could not convert CAN message with MsgId={{ msg['msg_name'] }} to struct"; } payload.dump(); auto proto = payload.toProtobuf(); updateHeader(proto.mutable_header()); std::ostringstream out; proto.SerializeToOstream(&out); return out.str().c_str(); break; } {%- endfor %} default: qDebug().noquote() << QString("WARNING: MsgId=0x%1 not handled").arg(msg.msgId, 4, 16, QChar('0')); break; } return QByteArray(); } {%- if cpp_namespace is defined and cpp_namespace is not none %} } // namespace {{ cpp_namespace }} {%- endif %}