-
Notifications
You must be signed in to change notification settings - Fork 15
Building a Package
Currently, the console doesn't have the ability to create packages automatically. This functionality is on the way. In the mean time, we will have to create the directory structure of a package ourselves.
A package is a collection of functions, like a dynamic library, that live on the device. The platform library, libflipper, knows about the functions that live in packages and can execute them remotely from any programming language.
First, create a directory to house the files that will be built into a package for the device. We will call the package that we're creating in this example my_package
.
mkdir my_package
cd my_package
Once inside, we need to create the necessary files so that Flipper's build system knows how to build the package. The directory tree for a package is below.
.
├── src
│ └── my_package.c
├── host
│ └── main.c
├── makefile
└── include
└── my_package.h
mkdir src host include
touch src/my_package.c host/main.c include/my_package.h makefile
# This sets the name of the Flipper package being built
MODULE := my_package
# This next line *MUST* come after MODULE has been set
include flipper.mk
Next, we need to create a function in the src
directory that will live on the device. We use a special macro called LF_FUNC
to tell the flipper tools that this function should be marked as callable from outside the device. This macro only needs to be placed in front of the function's declaration, not the function's definition. That is to say, only place it in front of the function in the .c
file.
include/main.h
#ifndef __my_package_h__
#define __my_package_h__
int my_package_function(void);
#endif
src/main.c
#include <my_package.h>
LF_FUNC int my_package_function(void) {
return 1234;
}
A package isn't very useful unless we create a program that can call the functions inside of this. We use a folder called host
to tell the flipper build system that code located inside of it should be build for the host platform. We can create a normal C program with a main function in this folder to test the execution of our my_package_function
.
host/main.c
#include <flipper.h>
#include <my_package.h>
int main(int argc, char *argv[]) {
flipper_attach();
int value = my_package_function();
printf("Got value %i from the package.\n", value);
return 0;
}
To build the package, simply
make
To install the package onto the attached hardware
make install
The built products are placed in the build
folder. Run the application by
./build/host/my_package
If all is well, you should see the following message printed on the command line.
./build/host/my_package
Got value 1234 from the package.
We can see that the value 1234
was retrieved as a result of calling the function on the device, obtaining its return value, and printing it.