1 /*
2 * Copyright (c) 2013, Texas Instruments Incorporated
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 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33 /*
34 * ======== Global.c ========
35 */
37 /* This define must precede inclusion of any xdc header files */
38 #define Registry_CURDESC ti_sdo_ce_osal_Global_desc
40 #include <xdc/std.h>
41 #include <xdc/runtime/Assert.h>
42 #include <xdc/runtime/Diags.h>
43 #include <xdc/runtime/Log.h>
44 #include <xdc/runtime/System.h>
45 #include <xdc/runtime/Memory.h>
46 #include <xdc/runtime/Registry.h>
48 #include <ti/sysbios/utils/Load.h>
50 #include <ti/sdo/ce/global/CESettings.h>
52 #include <stdlib.h>
53 #include <string.h> /* for strcmp */
55 #include <ti/sdo/ce/osal/Global.h>
58 /* list of functions to be called at exit */
60 typedef struct ExitFxnElem {
61 Fxn fxn;
62 struct ExitFxnElem *next;
63 } ExitFxnElem;
65 /* REMINDER: if you add an initialized static var, reinitialize it at cleanup */
67 static Bool curInit = FALSE;
69 static ExitFxnElem *exitFxnList = NULL;
70 static Bool doRegisterAtExit = TRUE;
72 extern String Global_buildInfo[]; /* generated by Global.xdt */
74 Registry_Desc ti_sdo_ce_osal_Global_desc;
76 static Int regInit = 0; /* Registry_addModule() called */
78 #include "Global_BIOS.h"
80 /*
81 * ======== Global_abort ========
82 * TODO: (jeh) This can be removed
83 */
84 Void Global_abort(String fmt, ...)
85 {
86 va_list vargs;
88 System_printf("\n======== Global_abort =============\n");
90 va_start(vargs, fmt);
91 System_vprintf(fmt, vargs);
92 va_end(vargs);
94 System_exit(1);
95 }
97 /*
98 * ======== Global_atexit ========
99 * TODO: (jeh) Use System_atexit(System_AtexitHandler fxn), where
100 * typedef Void (*System_AtexitHandler)(Int);
101 */
102 Bool Global_atexit(Fxn fxn)
103 {
104 ExitFxnElem *exitFxnElem;
107 Log_print1(Diags_ENTRY, "[+E] Global_atexit> enter (fxn=0x%x)", (IArg)fxn);
109 exitFxnElem = (ExitFxnElem *)Memory_alloc(NULL, sizeof(ExitFxnElem), 0,
110 NULL);
112 if (exitFxnElem == NULL) {
113 return (FALSE);
114 }
116 exitFxnElem->fxn = fxn;
117 exitFxnElem->next = exitFxnList;
118 exitFxnList = exitFxnElem;
120 /* register with the real system exit only once in the lifetime */
121 if (doRegisterAtExit == TRUE) {
122 System_atexit((System_AtexitHandler)Global_exit );
123 doRegisterAtExit = FALSE;
124 }
126 return (TRUE);
127 }
129 /*
130 * ======== Global_exit ========
131 * calls cleanup functions for all the modules that
132 * scheduled it, then cleans up itself
133 */
134 Void Global_exit(Void)
135 {
136 ExitFxnElem *old;
138 if (curInit == FALSE) {
139 return;
140 }
142 Log_print0(Diags_ENTRY, "[+E] Global_exit> enter" );
144 while( exitFxnList != NULL ) {
145 Log_print1(Diags_USER2, "[+2] Global_exit> "
146 "calling function *0x%x()...", (IArg)(exitFxnList->fxn ));
147 exitFxnList->fxn();
148 old = exitFxnList;
149 exitFxnList = exitFxnList->next;
150 Memory_free(NULL, old, sizeof(ExitFxnElem));
151 }
153 /* if the tracing module has its exit function, it must have been
154 * the last one, so don't use trace from this point on
155 */
157 /* do the cleanup of this module */
158 curInit = FALSE;
159 exitFxnList = NULL; /* unnecessary, NULL already */
161 /* we do NOT reset doRegisterAtExit, as we don't want endless pairs of
162 * CERuntime_init/exit to keep adding this func to the process' atexit
163 * table. Autoexit is a curtesy feature only for those who call
164 * CERuntime_init once.
165 */
166 /* doRegisterAtExit = TRUE; */
167 }
169 /*
170 * ======== Global_getCpuLoad ========
171 */
172 Int Global_getCpuLoad(Void)
173 {
174 return (Load_getCPULoad());
175 }
177 /*
178 * ======== Global_getenv ========
179 */
180 String Global_getenv(String name)
181 {
182 String retVal = NULL;
184 /* Currently only support a few hard-coded vars */
185 if (!strcmp(name, "CE_DEBUG")) {
186 if (ti_sdo_ce_osal_bios_Global_CE_DEBUG[0] != '\0') {
187 retVal = ti_sdo_ce_osal_bios_Global_CE_DEBUG;
188 }
189 }
190 else if (!strcmp(name, "CE_CHECK")) {
191 if (ti_sdo_ce_osal_bios_Global_CE_CHECK[0] != '\0') {
192 retVal = ti_sdo_ce_osal_bios_Global_CE_CHECK;
193 }
194 }
196 return (retVal);
197 }
199 /*
200 * ======== Global_init ========
201 */
202 Void Global_init(Void)
203 {
204 Int i;
205 Registry_Result result;
207 /*
208 * No need to reference count for Registry_addModule(), since there
209 * is no way to remove the module.
210 */
211 if (regInit == 0) {
212 /* Register this module for logging */
213 result = Registry_addModule(&ti_sdo_ce_osal_Global_desc,
214 Global_MODNAME);
215 Assert_isTrue(result == Registry_SUCCESS, (Assert_Id)NULL);
217 if (result == Registry_SUCCESS) {
218 /* Set the diags mask to the CE default */
219 CESettings_init();
220 CESettings_setDiags(Global_MODNAME);
221 }
222 regInit = 1;
223 }
225 if (curInit != TRUE) {
226 curInit = TRUE;
228 Log_print0(Diags_USER4, "[+4] Global_init> "
229 "This program was built with the following packages:" );
230 for (i = 0; Global_buildInfo[i] != NULL; i++) {
231 Log_print1(Diags_USER4, "[+4] %s", (IArg)(Global_buildInfo[i]));
232 }
233 }
234 }
236 /*
237 * ======== Global_setSpecialTrace ========
238 */
239 Void Global_setSpecialTrace( String mask )
240 {
241 }