From bb06c5f88ded611923123f04a6b0315c3dc9db09 Mon Sep 17 00:00:00 2001 From: Kyle Chan <64808590+Kyle02@users.noreply.github.com> Date: Thu, 9 Sep 2021 20:45:16 -0400 Subject: [PATCH] [SOFT-495] Migrate bootloader from protobuf-c to nanopb (#426) Nanopb is a more embedded-friendly protobuf implementation, so we use it instead of protobuf-c. --- .gitignore | 4 +- Makefile | 16 +++-- .../protogen/flash_application_code.pb.c | 12 ++++ .../protogen/flash_application_code.pb.h | 56 ++++++++++++++++++ projects/bootloader/protogen/querying.pb.c | 12 ++++ projects/bootloader/protogen/querying.pb.h | 59 +++++++++++++++++++ .../protogen/querying_response.pb.c | 12 ++++ .../protogen/querying_response.pb.h | 59 +++++++++++++++++++ projects/bootloader/protogen/update_id.pb.c | 12 ++++ projects/bootloader/protogen/update_id.pb.h | 47 +++++++++++++++ projects/bootloader/protogen/update_name.pb.c | 12 ++++ projects/bootloader/protogen/update_name.pb.h | 47 +++++++++++++++ ...esponse_.proto => querying_response.proto} | 0 13 files changed, 341 insertions(+), 7 deletions(-) create mode 100644 projects/bootloader/protogen/flash_application_code.pb.c create mode 100644 projects/bootloader/protogen/flash_application_code.pb.h create mode 100644 projects/bootloader/protogen/querying.pb.c create mode 100644 projects/bootloader/protogen/querying.pb.h create mode 100644 projects/bootloader/protogen/querying_response.pb.c create mode 100644 projects/bootloader/protogen/querying_response.pb.h create mode 100644 projects/bootloader/protogen/update_id.pb.c create mode 100644 projects/bootloader/protogen/update_id.pb.h create mode 100644 projects/bootloader/protogen/update_name.pb.c create mode 100644 projects/bootloader/protogen/update_name.pb.h rename projects/bootloader/protos/{querying_response_.proto => querying_response.proto} (100%) diff --git a/.gitignore b/.gitignore index 45c044edb..459634da6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,6 @@ libraries/version/inc/git_version_impl.h libraries/version/inc/git_version_impl.h.tmp # Autogenerated Protobuf code -*pb-c.h -*pb-c.c *_pb2.py # Flash file @@ -89,7 +87,7 @@ codegen/out/ codegen/genfiles/ codegen/system_can.dbc -# Python generated code +# Python generated code *.pyc # Code coverage files diff --git a/Makefile b/Makefile index 96f56f00e..384860c30 100644 --- a/Makefile +++ b/Makefile @@ -108,6 +108,7 @@ MU_PROTOS_DIR := $(MU_DIR)/protos # Bootloader directory BOOTLOADER_DIR := $(PROJ_DIR)/bootloader +PYTHONPATHNANO := $(realpath ..)/nanopb CODECOV_DIR := codecov @@ -225,7 +226,7 @@ FIND_PATHS := $(addprefix -o -path $(LIB_DIR)/,$(IGNORE_CLEANUP_LIBS)) $(addpref FIND := find $(PROJECT_DIR) $(LIBRARY_DIR) \ \( $(wordlist 2,$(words $(FIND_PATHS)),$(FIND_PATHS)) \) -prune -o \ -iname "*.[ch]" -print -FIND_MOD_NEW := git diff origin/master --name-only --diff-filter=ACMRT -- '*.c' '*.h' ':(exclude)*.mako.*' +FIND_MOD_NEW := git diff origin/master --name-only --diff-filter=ACMRT -- '*.c' '*.h' ':(exclude)*.mako.*' ':(exclude)projects/bootloader/protogen' # ignore MU since it has a different pylint FIND_MOD_NEW_PY := git diff origin/master --name-only --diff-filter=ACMRT -- '*.py' ':(exclude)mu/*.py' ':(exclude)$(VENV_DIR)/*' FIND_MOD_NEW_MU_PY := git diff origin/master --name-only --diff-filter=ACMRT -- 'mu/*.py' ':(exclude)$(VENV_DIR)/*' @@ -300,7 +301,12 @@ pyformat: bootloader_protos: @echo "Compiling protos..." @mkdir -p $(BOOTLOADER_DIR)/protogen - @protoc -I=$(BOOTLOADER_DIR)/protos --c_out=$(BOOTLOADER_DIR)/protogen $(BOOTLOADER_DIR)/protos/*.proto + @for i in $$(ls $(BOOTLOADER_DIR)/protos); do \ + protoc -I=$(BOOTLOADER_DIR)/protos -ocommand.pb $$i; \ + python $(PYTHONPATHNANO)/generator/nanopb_generator.py -I=$(BOOTLOADER_DIR)/protos command.pb; \ + done + @mv *.pb *.pb.c *.pb.h $(BOOTLOADER_DIR)/protogen + @cd $(BOOTLOADER_DIR)/protogen && rm command.pb # Note: build.py relies on a lot of relative paths so it would be easier to just cd and execute command .PHONY: codegen @@ -373,8 +379,6 @@ clean: @rm -f $(LIB_DIR)/mu-gen/inc/*.pb-c.h @rm -f $(LIB_DIR)/mu-gen/src/*.pb-c.c @rm -f $(MU_DIR)/protogen/*_pb2.py - @rm -f $(BOOTLOADER_DIR)/protogen/*.pb-c.h - @rm -f $(BOOTLOADER_DIR)/protogen/*.pb-c.c .PHONY: mock_can_data mock_can_data: socketcan @@ -405,6 +409,10 @@ install_requirements: @virtualenv $(VENV_DIR) @. $(VENV_DIR)/bin/activate; \ pip install -r requirements.txt + @if [ ! -d "$(PYTHONPATHNANO)" ]; then \ + cd $(PYTHONPATHNANO)/.. && git clone https://github.com/nanopb/nanopb.git \ + && cd $(PYTHONPATHNANO)/generator/proto && make; \ + fi .PHONY: codecov codecov: diff --git a/projects/bootloader/protogen/flash_application_code.pb.c b/projects/bootloader/protogen/flash_application_code.pb.c new file mode 100644 index 000000000..20a4389c4 --- /dev/null +++ b/projects/bootloader/protogen/flash_application_code.pb.c @@ -0,0 +1,12 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.6-dev */ + +#include "flash_application_code.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(FlashApplicationCode, FlashApplicationCode, AUTO) + + + diff --git a/projects/bootloader/protogen/flash_application_code.pb.h b/projects/bootloader/protogen/flash_application_code.pb.h new file mode 100644 index 000000000..ffded412a --- /dev/null +++ b/projects/bootloader/protogen/flash_application_code.pb.h @@ -0,0 +1,56 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.6-dev */ + +#ifndef PB_FLASH_APPLICATION_CODE_PB_H_INCLUDED +#define PB_FLASH_APPLICATION_CODE_PB_H_INCLUDED +#include + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +typedef struct _FlashApplicationCode { + pb_callback_t name; + pb_callback_t git_version; + uint32_t application_crc; + uint32_t size; +} FlashApplicationCode; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define FlashApplicationCode_init_default {{{NULL}, NULL}, {{NULL}, NULL}, 0, 0} +#define FlashApplicationCode_init_zero {{{NULL}, NULL}, {{NULL}, NULL}, 0, 0} + +/* Field tags (for use in manual encoding/decoding) */ +#define FlashApplicationCode_name_tag 1 +#define FlashApplicationCode_git_version_tag 2 +#define FlashApplicationCode_application_crc_tag 3 +#define FlashApplicationCode_size_tag 4 + +/* Struct field encoding specification for nanopb */ +#define FlashApplicationCode_FIELDLIST(X, a) \ +X(a, CALLBACK, SINGULAR, STRING, name, 1) \ +X(a, CALLBACK, SINGULAR, STRING, git_version, 2) \ +X(a, STATIC, SINGULAR, UINT32, application_crc, 3) \ +X(a, STATIC, SINGULAR, UINT32, size, 4) +#define FlashApplicationCode_CALLBACK pb_default_field_callback +#define FlashApplicationCode_DEFAULT NULL + +extern const pb_msgdesc_t FlashApplicationCode_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define FlashApplicationCode_fields &FlashApplicationCode_msg + +/* Maximum encoded size of messages (where known) */ +/* FlashApplicationCode_size depends on runtime parameters */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/projects/bootloader/protogen/querying.pb.c b/projects/bootloader/protogen/querying.pb.c new file mode 100644 index 000000000..3018e4209 --- /dev/null +++ b/projects/bootloader/protogen/querying.pb.c @@ -0,0 +1,12 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.6-dev */ + +#include "querying.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(Querying, Querying, AUTO) + + + diff --git a/projects/bootloader/protogen/querying.pb.h b/projects/bootloader/protogen/querying.pb.h new file mode 100644 index 000000000..39d72dd71 --- /dev/null +++ b/projects/bootloader/protogen/querying.pb.h @@ -0,0 +1,59 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.6-dev */ + +#ifndef PB_QUERYING_PB_H_INCLUDED +#define PB_QUERYING_PB_H_INCLUDED +#include + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +typedef struct _Querying { + pb_callback_t id; + pb_callback_t name; + pb_callback_t current_project; + pb_callback_t project_info; + pb_callback_t git_version; +} Querying; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define Querying_init_default {{{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}} +#define Querying_init_zero {{{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}} + +/* Field tags (for use in manual encoding/decoding) */ +#define Querying_id_tag 1 +#define Querying_name_tag 2 +#define Querying_current_project_tag 3 +#define Querying_project_info_tag 4 +#define Querying_git_version_tag 5 + +/* Struct field encoding specification for nanopb */ +#define Querying_FIELDLIST(X, a) \ +X(a, CALLBACK, REPEATED, UINT32, id, 1) \ +X(a, CALLBACK, REPEATED, STRING, name, 2) \ +X(a, CALLBACK, REPEATED, STRING, current_project, 3) \ +X(a, CALLBACK, REPEATED, STRING, project_info, 4) \ +X(a, CALLBACK, REPEATED, STRING, git_version, 5) +#define Querying_CALLBACK pb_default_field_callback +#define Querying_DEFAULT NULL + +extern const pb_msgdesc_t Querying_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define Querying_fields &Querying_msg + +/* Maximum encoded size of messages (where known) */ +/* Querying_size depends on runtime parameters */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/projects/bootloader/protogen/querying_response.pb.c b/projects/bootloader/protogen/querying_response.pb.c new file mode 100644 index 000000000..3af593dbf --- /dev/null +++ b/projects/bootloader/protogen/querying_response.pb.c @@ -0,0 +1,12 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.6-dev */ + +#include "querying_response.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(QueryingResponse, QueryingResponse, AUTO) + + + diff --git a/projects/bootloader/protogen/querying_response.pb.h b/projects/bootloader/protogen/querying_response.pb.h new file mode 100644 index 000000000..96b133c14 --- /dev/null +++ b/projects/bootloader/protogen/querying_response.pb.h @@ -0,0 +1,59 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.6-dev */ + +#ifndef PB_QUERYING_RESPONSE_PB_H_INCLUDED +#define PB_QUERYING_RESPONSE_PB_H_INCLUDED +#include + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +typedef struct _QueryingResponse { + uint32_t id; + pb_callback_t name; + pb_callback_t current_project; + pb_callback_t project_info; + pb_callback_t git_version; +} QueryingResponse; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define QueryingResponse_init_default {0, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}} +#define QueryingResponse_init_zero {0, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}} + +/* Field tags (for use in manual encoding/decoding) */ +#define QueryingResponse_id_tag 1 +#define QueryingResponse_name_tag 2 +#define QueryingResponse_current_project_tag 3 +#define QueryingResponse_project_info_tag 4 +#define QueryingResponse_git_version_tag 5 + +/* Struct field encoding specification for nanopb */ +#define QueryingResponse_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, UINT32, id, 1) \ +X(a, CALLBACK, SINGULAR, STRING, name, 2) \ +X(a, CALLBACK, SINGULAR, STRING, current_project, 3) \ +X(a, CALLBACK, SINGULAR, STRING, project_info, 4) \ +X(a, CALLBACK, SINGULAR, STRING, git_version, 5) +#define QueryingResponse_CALLBACK pb_default_field_callback +#define QueryingResponse_DEFAULT NULL + +extern const pb_msgdesc_t QueryingResponse_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define QueryingResponse_fields &QueryingResponse_msg + +/* Maximum encoded size of messages (where known) */ +/* QueryingResponse_size depends on runtime parameters */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/projects/bootloader/protogen/update_id.pb.c b/projects/bootloader/protogen/update_id.pb.c new file mode 100644 index 000000000..5888bdc43 --- /dev/null +++ b/projects/bootloader/protogen/update_id.pb.c @@ -0,0 +1,12 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.6-dev */ + +#include "update_id.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(UpdateId, UpdateId, AUTO) + + + diff --git a/projects/bootloader/protogen/update_id.pb.h b/projects/bootloader/protogen/update_id.pb.h new file mode 100644 index 000000000..d29c6094e --- /dev/null +++ b/projects/bootloader/protogen/update_id.pb.h @@ -0,0 +1,47 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.6-dev */ + +#ifndef PB_UPDATE_ID_PB_H_INCLUDED +#define PB_UPDATE_ID_PB_H_INCLUDED +#include + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +typedef struct _UpdateId { + uint32_t new_id; +} UpdateId; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define UpdateId_init_default {0} +#define UpdateId_init_zero {0} + +/* Field tags (for use in manual encoding/decoding) */ +#define UpdateId_new_id_tag 1 + +/* Struct field encoding specification for nanopb */ +#define UpdateId_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, UINT32, new_id, 1) +#define UpdateId_CALLBACK NULL +#define UpdateId_DEFAULT NULL + +extern const pb_msgdesc_t UpdateId_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define UpdateId_fields &UpdateId_msg + +/* Maximum encoded size of messages (where known) */ +#define UpdateId_size 6 + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/projects/bootloader/protogen/update_name.pb.c b/projects/bootloader/protogen/update_name.pb.c new file mode 100644 index 000000000..d274eafc7 --- /dev/null +++ b/projects/bootloader/protogen/update_name.pb.c @@ -0,0 +1,12 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.6-dev */ + +#include "update_name.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(UpdateName, UpdateName, AUTO) + + + diff --git a/projects/bootloader/protogen/update_name.pb.h b/projects/bootloader/protogen/update_name.pb.h new file mode 100644 index 000000000..2f0af6dae --- /dev/null +++ b/projects/bootloader/protogen/update_name.pb.h @@ -0,0 +1,47 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.6-dev */ + +#ifndef PB_UPDATE_NAME_PB_H_INCLUDED +#define PB_UPDATE_NAME_PB_H_INCLUDED +#include + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +typedef struct _UpdateName { + pb_callback_t new_name; +} UpdateName; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define UpdateName_init_default {{{NULL}, NULL}} +#define UpdateName_init_zero {{{NULL}, NULL}} + +/* Field tags (for use in manual encoding/decoding) */ +#define UpdateName_new_name_tag 1 + +/* Struct field encoding specification for nanopb */ +#define UpdateName_FIELDLIST(X, a) \ +X(a, CALLBACK, SINGULAR, STRING, new_name, 1) +#define UpdateName_CALLBACK pb_default_field_callback +#define UpdateName_DEFAULT NULL + +extern const pb_msgdesc_t UpdateName_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define UpdateName_fields &UpdateName_msg + +/* Maximum encoded size of messages (where known) */ +/* UpdateName_size depends on runtime parameters */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/projects/bootloader/protos/querying_response_.proto b/projects/bootloader/protos/querying_response.proto similarity index 100% rename from projects/bootloader/protos/querying_response_.proto rename to projects/bootloader/protos/querying_response.proto