Skip to content

Components

DelVecchioAndrea edited this page Jan 18, 2020 · 2 revisions

Extend the project by defining a new component

Adding a new component to the project requires few steps:

  1. Create a directory called <component_name> in /components/bemesh. The component name must be unique so that it won't interfere with other component's names during the registration step.

  2. Fill <component_name> directory with some .cpp or .c code files. Those files will define the functions "offered" by the component. In the same directory create a new directory called include (don't use another name). The include directory will contain all the header files of the component source code, but not other components' header files.

  3. Create a CMakeList.txt inside <component_name>. This CMake will define all components dependencies and requirements as well as flags used to compile the component source code. The CMakeLists.txt must be organized as follow. Define a list variable that contains the names of all the components required by this component. For example, a component that need master, slave, kernel, mid-tier, common will have :

    list(APPEND <component_list_name> "master" "slave" "kernel" "mid-tier" "common")

Define a list variable that keeps track of the component sources. For example:

list(APPEND SOURCES "bemesh_messages_v2.cpp" "message_handler_v2.cpp" "routing.cpp" "rtable.cpp")

Once the component is registered, all the .cpp file mentioned above will be compiled. Paste this code int the following lines of the CMake to build all the include paths for the component:

set(BASE_PATH_TO_INCLUDE "../")
list(APPEND INCLUDE_LIST )
foreach(item ${<component list name>})
  set(tmp "${BASE_PATH_TO_INCLUDE}${item}/include")
  list(APPEND INCLUDE_LIST ${tmp})
endforeach()

Add the include directory created at step 3 to the include paths

list(APPEND INCLUDE_LIST "include")

Then register the component with

idf_component_register(SRCS ${SOURCES}
                    INCLUDE_DIRS ${INCLUDE_LIST} 
                    REQUIRES <required_components_name>)

If you need to add extra component dependencies all you need to do is to insert the component name in the <component_list_name> and to add that name in the REQUIRES list of idf_component_register command:

list(APPEND <component_list_name> "master" "slave" "kernel" "mid-tier" "common" "new_component")

And then

 idf_component_register(SRCS ${SOURCES}
                    INCLUDE_DIRS ${INCLUDE_LIST} 
                    REQUIRES <required_components_name> new_component)

Whenever a new source file is added to the <component_name> directory just add it to the SOURCES list and the toolchain will automatically compile it

list(APPEND SOURCES "bemesh_messages_v2.cpp" "message_handler_v2.cpp" "routing.cpp" "rtable.cpp" "new_source.cpp/.c")

Do the opposite to remove component files or dependencies.

  1. Go to the root CMake. From the <component_name> directory it can be reached with ../../../CMakeLists.txt. Add the component name to the PROJ_COMPS variable

    set(PROJ_COMPS "kernel" "common" "services" "mid-tier" "callbacks" "slave" "master" "new_component")

  2. (optional) If the new component is required by other components, for each of those components:

Open the CMakeLists.txt file and add <component_name> to the REQUIRED_COMPS list variable

list(APPEND REQUIRED_COMPS "master" "slave" "kernel" "mid-tier" "common" "<component_name>")

Do the same to the REQUIRES section in idf_component_register

idf_component_register(SRCS ${SOURCES}
                    INCLUDE_DIRS ${INCLUDE_LIST} 
                    REQUIRES <required_components_name> <component_name>)

Do this also for the main directory if it is necessary.

  1. Check if everything is okay, running idf.py build

A component CMakeLists.txt should look like this

list(APPEND REQUIRED_COMPS "master" "slave" "kernel" "mid-tier" "common")
#In this variable we keep track of source files needed by the component.
list(APPEND SOURCES "callbacks.cpp")
#All header files will be located inside the "include" directory of the component "component" reachable from      "../"
set(BASE_PATH_TO_INCLUDE "../")
list(APPEND INCLUDE_LIST )
#For each required component we build the string "../component/include" that will be added to
#the INCLUDE_LIST variable
foreach(item ${REQUIRED_COMPS})
  set(tmp "${BASE_PATH_TO_INCLUDE}${item}/include")
  list(APPEND INCLUDE_LIST ${tmp})
endforeach()
#We append to the INCLUDE_LIST the include directory of this component
list(APPEND INCLUDE_LIST "include")
#Finally we register the component using the values stored in the variables above
idf_component_register(SRCS ${SOURCES}
                    INCLUDE_DIRS ${INCLUDE_LIST} 
                    REQUIRES nvs_flash bt slave master kernel mid-tier common)
Clone this wiki locally