Contiki-NG
Loading...
Searching...
No Matches
contiki-main.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, George Oikonomou - http://www.spd.gr
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*---------------------------------------------------------------------------*/
32/**
33 * \addtogroup main
34 * @{
35 */
36/*---------------------------------------------------------------------------*/
37/**
38 * \file
39 *
40 * Implementation of \os's main routine
41 */
42/*---------------------------------------------------------------------------*/
43#include "contiki.h"
44#include "contiki-net.h"
45#include "lib/ecc.h"
46#include "sys/node-id.h"
47#include "sys/platform.h"
48#include "sys/energest.h"
49#include "sys/stack-check.h"
50#include "dev/watchdog.h"
51
52#include "net/queuebuf.h"
55#include "services/rpl-border-router/rpl-border-router.h"
60
61#include <stdio.h>
62#include <stdint.h>
63/*---------------------------------------------------------------------------*/
64/* Log configuration */
65#include "sys/log.h"
66#define LOG_MODULE "Main"
67#define LOG_LEVEL LOG_LEVEL_MAIN
68
69#if PLATFORM_MAIN_ACCEPTS_ARGS
70/*---------------------------------------------------------------------------*/
71int contiki_argc;
72char **contiki_argv;
73/*---------------------------------------------------------------------------*/
74#include "lib/list.h"
75
76LIST(contiki_options);
77static const char *prog;
78static const char *help_usage;
79static const char *help_suffix;
80/*---------------------------------------------------------------------------*/
81void
82contiki_set_usage(const char *msg)
83{
84 help_usage = msg;
85}
86/*---------------------------------------------------------------------------*/
87void
88contiki_set_extra_help(const char *msg)
89{
90 help_suffix = msg;
91}
92/*---------------------------------------------------------------------------*/
93void
94contiki_add_option(struct contiki_callback_option *option)
95{
96 static bool initialized = false;
97 if(!initialized) {
98 list_init(contiki_options);
99 initialized = true;
100 }
101 list_add(contiki_options, option);
102}
103/*---------------------------------------------------------------------------*/
104static void
105print_help(void)
106{
107 printf("usage: %s [options]%s", prog, help_usage ? help_usage : "\n");
108 printf("Options are:\n");
109 for(struct contiki_callback_option *r = list_head(contiki_options);
110 r != NULL; r = r->next) {
111 if(!r->help) {
112 continue;
113 }
114 int short_len = r->opt_struct.flag == NULL && r->opt_struct.val ? 6 : 0;
115 if(short_len) {
116 printf(" -%c, ", (char)r->opt_struct.val);
117 }
118 int has_arg = r->opt_struct.has_arg;
119 const char *arg_desc = has_arg == no_argument ? "" :
120 has_arg == optional_argument ? " [value]" : " value ";
121 printf(" %c-%s%s%s\t%s", strlen(r->opt_struct.name) == 1 ? ' ' : '-',
122 r->opt_struct.name, arg_desc,
123 /* Insert extra tab for short option names. */
124 short_len + 3 + strlen(r->opt_struct.name) + strlen(arg_desc) < 8
125 ? "\t" : "",
126 r->help);
127 }
128 if(help_suffix) {
129 printf("%s", help_suffix);
130 }
131}
132/*---------------------------------------------------------------------------*/
133CC_NORETURN static int
134help_callback(const char *optarg)
135{
136 print_help();
137 exit(0);
138}
139CONTIKI_OPTION(CONTIKI_MAX_INIT_PRIO + 1, { "help", no_argument, NULL, 'h' },
140 help_callback, "display this help and exit\n");
141/*---------------------------------------------------------------------------*/
142static int
143parse_argv(int *argc, char ***argv)
144{
145 prog = *argv[0];
146 const int num_options = list_length(contiki_options);
147 struct contiki_callback_option options[num_options];
148 struct option long_options[num_options + 1];
149
150 int i = 0;
151 for(struct contiki_callback_option *r = list_head(contiki_options);
152 r != NULL; ++i, r = r->next) {
153 memcpy(&long_options[i], &r->opt_struct, sizeof(struct option));
154 memcpy(&options[i], r, sizeof(struct contiki_callback_option));
155 }
156 /* Null terminate options. */
157 memset(&long_options[i], 0, sizeof(struct option));
158
159 while(1) {
160 int ix = 0;
161 int c = getopt_long_only(*argc, *argv, "", long_options, &ix);
162 if(c == -1) { /* Processed all options. */
163 break;
164 }
165 if(c == '?') { /* Unknown option, print help and return error. */
166 print_help();
167 return 1;
168 }
169 if(options[ix].callback) { /* Option has a callback, call with optarg. */
170 int rv;
171 if((rv = options[ix].callback(optarg)) != 0) {
172 return rv;
173 }
174 }
175 }
176 *argc -= optind - 1;
177 *argv += optind - 1;
178 return 0;
179}
180/*---------------------------------------------------------------------------*/
181int
182main(int argc, char **argv)
183{
184 int rv;
185 if((rv = parse_argv(&argc, &argv)) != 0) {
186 return rv;
187 }
188 /* Remember argc/argv after command line options. */
189 contiki_argc = argc;
190 contiki_argv = argv;
191#else
192int
193main(void)
194{
195#endif /* PLATFORM_MAIN_ACCEPTS_ARGS */
197
198 clock_init();
199 rtimer_init();
200 process_init();
201 process_start(&etimer_process, NULL);
202 ctimer_init();
204
205 energest_init();
206
207#if STACK_CHECK_ENABLED
209#endif
210
212
213#if QUEUEBUF_ENABLED
214 queuebuf_init();
215#endif /* QUEUEBUF_ENABLED */
216 netstack_init();
217 node_id_init();
218#if ECC_ENABLED
219 ecc_init();
220#endif /* ECC_ENABLED */
221
222 LOG_INFO("Starting " CONTIKI_VERSION_STRING "\n");
223 LOG_DBG("TARGET=%s", CONTIKI_TARGET_STRING);
224#ifdef CONTIKI_BOARD_STRING
225 LOG_DBG_(", BOARD=%s", CONTIKI_BOARD_STRING);
226#endif
227 LOG_DBG_("\n");
228 LOG_INFO("- Routing: %s\n", NETSTACK_ROUTING.name);
229 LOG_INFO("- Net: %s\n", NETSTACK_NETWORK.name);
230 LOG_INFO("- MAC: %s\n", NETSTACK_MAC.name);
231 LOG_INFO("- 802.15.4 PANID: 0x%04x\n", IEEE802154_PANID);
232#if MAC_CONF_WITH_TSCH
233 LOG_INFO("- 802.15.4 TSCH default hopping sequence length: %u\n", (unsigned)sizeof(TSCH_DEFAULT_HOPPING_SEQUENCE));
234#else /* MAC_CONF_WITH_TSCH */
235 LOG_INFO("- 802.15.4 Default channel: %u\n", IEEE802154_DEFAULT_CHANNEL);
236#endif /* MAC_CONF_WITH_TSCH */
237
238 LOG_INFO("Node ID: %u\n", node_id);
239 LOG_INFO("Link-layer address: ");
240 LOG_INFO_LLADDR(&linkaddr_node_addr);
241 LOG_INFO_("\n");
242
243#if NETSTACK_CONF_WITH_IPV6
244 {
245 uip_ds6_addr_t *lladdr;
246 memcpy(&uip_lladdr.addr, &linkaddr_node_addr, sizeof(uip_lladdr.addr));
247 process_start(&tcpip_process, NULL);
248
249 lladdr = uip_ds6_get_link_local(-1);
250 LOG_INFO("Tentative link-local IPv6 address: ");
251 LOG_INFO_6ADDR(lladdr != NULL ? &lladdr->ipaddr : NULL);
252 LOG_INFO_("\n");
253 }
254#endif /* NETSTACK_CONF_WITH_IPV6 */
255
257
258#if BUILD_WITH_RPL_BORDER_ROUTER
259 rpl_border_router_init();
260 LOG_DBG("With RPL Border Router\n");
261#endif /* BUILD_WITH_RPL_BORDER_ROUTER */
262
263#if BUILD_WITH_ORCHESTRA
264 orchestra_init();
265 LOG_DBG("With Orchestra\n");
266#endif /* BUILD_WITH_ORCHESTRA */
267
268#if BUILD_WITH_SHELL
270 LOG_DBG("With Shell\n");
271#endif /* BUILD_WITH_SHELL */
272
273#if BUILD_WITH_COAP
274 coap_engine_init();
275 LOG_DBG("With CoAP\n");
276#endif /* BUILD_WITH_COAP */
277
278#if BUILD_WITH_SNMP
279 snmp_init();
280 LOG_DBG("With SNMP\n");
281#endif /* BUILD_WITH_SNMP */
282
283#if BUILD_WITH_SIMPLE_ENERGEST
285#endif /* BUILD_WITH_SIMPLE_ENERGEST */
286
287#if BUILD_WITH_TSCH_CS
288 /* Initialize the channel selection module */
290#endif /* BUILD_WITH_TSCH_CS */
291
292 autostart_start(autostart_processes);
293
295
296#if PLATFORM_PROVIDES_MAIN_LOOP
298#else
299 while(1) {
300 process_num_events_t r;
301 do {
302 r = process_run();
304 } while(r > 0);
305
307 }
308#endif
309
310 return 0;
311}
312/*---------------------------------------------------------------------------*/
313/**
314 * @}
315 */
void serial_shell_init(void)
Initializes Serial Shell module.
#define CC_NORETURN
Configure if the C compiler supports functions that are not meant to return e.g.
Definition cc.h:99
CoAP engine implementation.
Header file of ECC.
Header file for the energy estimation mechanism.
void snmp_init()
Initializes the SNMP engine.
Definition snmp.c:57
void clock_init(void)
Arch-specific implementation of clock_init for the cc2538.
Definition clock.c:93
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition watchdog.c:72
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition watchdog.c:85
void watchdog_init(void)
Initialisation function for the WDT.
Definition watchdog.c:63
void platform_init_stage_three()
Final stage of platform driver initialisation.
Definition platform.c:169
void platform_init_stage_one(void)
Basic (Stage 1) platform driver initialisation.
Definition platform.c:114
void platform_idle()
The platform's idle/sleep function.
Definition platform.c:185
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
Definition platform.c:123
void ctimer_init(void)
Initialize the callback timer library.
Definition ctimer.c:86
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
static void list_init(list_t list)
Initialize a list.
Definition list.h:152
#define LIST(name)
Declare a linked list.
Definition list.h:90
int list_length(const_list_t list)
Get the length of a list.
Definition list.c:160
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition list.c:71
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition list.h:169
void platform_main_loop(void)
The platform's main loop, if provided.
Definition tz-normal.c:95
#define CONTIKI_OPTION(prio,...)
Add a command line option when the compilation unit is present.
Definition platform.h:153
static void node_id_init(void)
Initialize the node ID.
Definition node-id.h:61
process_num_events_t process_run(void)
Run the system once - call poll handlers and process one event.
Definition process.c:305
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
void process_init(void)
Initialize the process module.
Definition process.c:228
#define rtimer_init()
Initialize the real-time scheduler.
Definition rtimer.h:127
void simple_energest_init(void)
Initialize the deployment module.
void stack_check_init(void)
Initialize the stack area with a known pattern.
Definition stack-check.c:74
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
Linked list manipulation routines.
Header file for the logging system.
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Definition mac.h:52
Node-id (simple 16-bit identifiers) handling.
Orchestra header file.
Header file for the Contiki-NG main routine.
Header file for the Packet queue buffer management.
A shell back-end for the serial port.
A process that periodically prints out the time spent in radio tx, radio rx, total time and duty cycl...
SNMP Implementation of the process.
Stack checker library header file.
Unicast address structure.
Definition uip-ds6.h:205
void tsch_cs_adaptations_init(void)
Initializes the TSCH hopping sequence selection module.
Definition tsch-cs.c:116
Header file for TSCH adaptive channel selection.