1 /*
2  * Copyright (c) 2009 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <stdio.h>
24 #include <app.h>
25 #include <kernel/thread.h>
26 
27 extern const struct app_descriptor __apps_start[];
28 extern const struct app_descriptor __apps_end[];
29 
30 static void start_app(const struct app_descriptor *app);
31 
32 /* one time setup */
apps_init(void)33 void apps_init(void)
34 {
35     const struct app_descriptor *app;
36 
37     /* call all the init routines */
38     for (app = __apps_start; app != __apps_end; app++) {
39         if (app->init)
40             app->init(app);
41     }
42 
43     /* start any that want to start on boot */
44     for (app = __apps_start; app != __apps_end; app++) {
45         if (app->entry && (app->flags & APP_FLAG_DONT_START_ON_BOOT) == 0) {
46             start_app(app);
47         }
48     }
49 }
50 
app_thread_entry(void * arg)51 static int app_thread_entry(void *arg)
52 {
53     const struct app_descriptor *app = (const struct app_descriptor *)arg;
54 
55     app->entry(app, NULL);
56 
57     return 0;
58 }
59 
start_app(const struct app_descriptor * app)60 static void start_app(const struct app_descriptor *app)
61 {
62     uint32_t stack_size = (app->flags & APP_FLAG_CUSTOM_STACK_SIZE) ? app->stack_size : DEFAULT_STACK_SIZE;
63 
64     printf("starting app %s\n", app->name);
65     thread_t *t = thread_create(app->name, &app_thread_entry, (void *)app, DEFAULT_PRIORITY, stack_size);
66     if (!t) {
67         panic("start_app thread creation failed");
68     }
69     thread_detach(t);
70     thread_resume(t);
71 }
72 
73