1 /*
2  * Copyright (c) 2023 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <pthread.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 
14 #include <zephyr/sys/util.h>
15 #include <zephyr/kernel.h>
16 #include <zephyr/version.h>
17 
18 #ifdef BUILD_VERSION
19 #define VERSION_BUILD STRINGIFY(BUILD_VERSION)
20 #else
21 #define VERSION_BUILD KERNEL_VERSION_STRING
22 #endif
23 
24 #if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_PICOLIBC)
25 /* newlib headers seem to be missing this */
26 int getenv_r(const char *name, char *val, size_t len);
27 #endif
28 
env(void)29 static void env(void)
30 {
31 	extern char **environ;
32 
33 	if (environ != NULL) {
34 		for (char **envp = environ; *envp != NULL; ++envp) {
35 			printf("%s\n", *envp);
36 		}
37 	}
38 }
39 
entry(void * arg)40 static void *entry(void *arg)
41 {
42 	static char alert_msg_buf[42];
43 
44 	setenv("BOARD", CONFIG_BOARD, 1);
45 	setenv("BUILD_VERSION", VERSION_BUILD, 1);
46 	setenv("ALERT", "", 1);
47 
48 	env();
49 
50 	while (true) {
51 		sleep(1);
52 		if (getenv_r("ALERT", alert_msg_buf, sizeof(alert_msg_buf) - 1) < 0 ||
53 		    strlen(alert_msg_buf) == 0) {
54 			continue;
55 		}
56 		printf("ALERT=%s\n", alert_msg_buf);
57 		unsetenv("ALERT");
58 	}
59 
60 	return NULL;
61 }
62 
main(void)63 int main(void)
64 {
65 	pthread_t th;
66 
67 	/* create a separate thread so that the shell can start */
68 	return pthread_create(&th, NULL, entry, NULL);
69 }
70