In general, Contiki-NG adopts the C programming language.
Creating a Project
The Contiki-NG program does not provide project templates to build a program. If you want to create a new project, you start by creating a new folder and a Makefile file. Notice that we also can use program samples from the Contiki project.
Contiki-NG Basic Programming Language
Contiki-NG adopts the C general programming language for implementation. Contiki-NG programming is similar to C as far as declaring variables and using conditional statements and looping.
1 | Declare variables -> Assign variables -> Looping -> If-conditional -> Comment codes |
1 | int alpha; |
Review of Protothreads
Protothreads are lightweight threads designed for memory-constrained systems, such as small embedded systems or WSN nodes. The implementations for Contiki-NG located in the <contiki_root>/os/sys/pt.h. The following is a list of function descriptions based on the pt.h header:
PT_INIT(pt)function is used to initialize aprotothread.PT_THREAD(name_args)is a macro that is used to declare aprotothread.PT_BEGIN(pt)is used to declare the starting point of aprotothread.PT_END(pt)is used to end aprotothread.PT_WAIT_UNTIL(pt, condition)is used for blocking theprotothreaduntil the specified condition is true.PT_WAIT_WHILE(pt, cond)is used for blocking and waiting while the condition is true.PT_WAIT_THREAD(pt, thread)is used to schedule a childprotothread. The currentprotothreadwill block until the childprotothreadcompletes.PT_SPAWN(pt, child, thread)is used to spawn a childprotothreadand waits until it exits.PT_RESTART(pt)will block and cause the runningprotothreadto restart its execution at the place of thePT_BEGIN()call.PT_EXIT(pt)is used to exit from aprotothread.PT_SCHEDULE(f)is used to schedule aprotothread. The return value of the function is non-zero if theprotothreadis running or zero if theprotothreadhas exited.PT_YIELD(pt)yields from the currentprotothread.PT_YIELD_UNTIL(pt, cond)yields from theprotothreaduntil a condition occurs.
Extending the Contiki-NG Library
Contiki-NG OS consists of several libraries and apps that can extend based for specific cases. To extend Contiki-NG OS functionalities, we can follow the same approach used while building C programs. We can add additional libraries by adding a C header and source code. Then, our Contiki-NG program will consume our libraries.
Sample Library
sample.h- Define the header.
1 |
|
sample.c- Implement the header.
1 |
|
- Next, we will implement our extended library and use it in a Contiki-NG program. To access the extended library, we declare that header file. Then, we call library functions from our code.
1 |
|