Skip to content

Commit

Permalink
[SOFT-495] Migrate bootloader from protobuf-c to nanopb (#426)
Browse files Browse the repository at this point in the history
Nanopb is a more embedded-friendly protobuf implementation, so we use it instead of protobuf-c.
  • Loading branch information
Kyle02 authored Sep 10, 2021
1 parent ebadd85 commit bb06c5f
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 7 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,7 +87,7 @@ codegen/out/
codegen/genfiles/
codegen/system_can.dbc

# Python generated code
# Python generated code
*.pyc

# Code coverage files
Expand Down
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ MU_PROTOS_DIR := $(MU_DIR)/protos

# Bootloader directory
BOOTLOADER_DIR := $(PROJ_DIR)/bootloader
PYTHONPATHNANO := $(realpath ..)/nanopb

CODECOV_DIR := codecov

Expand Down Expand Up @@ -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)/*'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions projects/bootloader/protogen/flash_application_code.pb.c
Original file line number Diff line number Diff line change
@@ -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)



56 changes: 56 additions & 0 deletions projects/bootloader/protogen/flash_application_code.pb.h
Original file line number Diff line number Diff line change
@@ -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 <pb.h>

#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
12 changes: 12 additions & 0 deletions projects/bootloader/protogen/querying.pb.c
Original file line number Diff line number Diff line change
@@ -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)



59 changes: 59 additions & 0 deletions projects/bootloader/protogen/querying.pb.h
Original file line number Diff line number Diff line change
@@ -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 <pb.h>

#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
12 changes: 12 additions & 0 deletions projects/bootloader/protogen/querying_response.pb.c
Original file line number Diff line number Diff line change
@@ -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)



59 changes: 59 additions & 0 deletions projects/bootloader/protogen/querying_response.pb.h
Original file line number Diff line number Diff line change
@@ -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 <pb.h>

#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
12 changes: 12 additions & 0 deletions projects/bootloader/protogen/update_id.pb.c
Original file line number Diff line number Diff line change
@@ -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)



47 changes: 47 additions & 0 deletions projects/bootloader/protogen/update_id.pb.h
Original file line number Diff line number Diff line change
@@ -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 <pb.h>

#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
12 changes: 12 additions & 0 deletions projects/bootloader/protogen/update_name.pb.c
Original file line number Diff line number Diff line change
@@ -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)



Loading

0 comments on commit bb06c5f

Please sign in to comment.