1
- /****************************************************************************
2
- * xedge_main.c
1
+ /***************************************************************************
2
+ * apps/examples/xedge/ xedge_main.c
3
3
*
4
4
* Copyright (C) 2025. All rights reserved.
5
5
* Author: Real Time Logic
32
32
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
33
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
34
*
35
- **************************************************************************** /
35
+ ***************************************************************************/
36
36
37
37
/* Xedge NuttX Startup Code (may need adjustments)
38
38
*
44
44
* prepare.sh to clone and prepare the build env.
45
45
*/
46
46
47
+ /***************************************************************************
48
+ * Included Files
49
+ ***************************************************************************/
50
+
47
51
#include <nuttx/config.h>
48
52
#include <stdio.h>
49
53
#include <stdlib.h>
56
60
#include <BaServerLib.h>
57
61
#include "BAS/examples/xedge/src/xedge.h"
58
62
59
- extern void barracuda (); /* BAS/examples/xedge/src/xedge.c */
60
- extern void init_dlmalloc (char * heapstart , char * heapend ); /* dlmalloc.c */
61
- extern int (* platformInitDiskIo )(DiskIo * ); /* xedge.c */
63
+ /***************************************************************************
64
+ * Private Data
65
+ ***************************************************************************/
66
+
67
+ static int running ; /* Server running mode */
68
+
69
+ /* BAS is configured to use dlmalloc for NuttX. This is the pool.
70
+ * 2M : recommended minimum
71
+ */
72
+
73
+ static char poolbuf [2 * 1024 * 1024 ];
62
74
63
- /* The LThreadMgr configured in xedge.c */
64
- extern LThreadMgr ltMgr ;
75
+ extern LThreadMgr ltMgr ; /* The LThreadMgr configured in xedge.c */
65
76
66
- /* BAS is configured to use dlmalloc for NuttX. This is the pool. */
67
- static char poolBuf [2 * 1024 * 1024 ]; /* 2M : recommended minimum */
77
+ /***************************************************************************
78
+ * External Function Prototypes
79
+ ***************************************************************************/
80
+
81
+ /* barracuda(): BAS/examples/xedge/src/xedge.c */
82
+
83
+ extern void barracuda (void );
84
+ extern void init_dlmalloc (char * heapstart , char * heapend ); /* dlmalloc.c */
85
+ extern int (* platformInitDiskIo )(DiskIo * io ); /* xedge.c */
86
+
87
+ /***************************************************************************
88
+ * Private Functions
89
+ ***************************************************************************/
68
90
69
91
/* The following two functions are copied from the example:
70
92
* https://github.com/RealTimeLogic/BAS/blob/main/examples/xedge/src/led.c
@@ -77,7 +99,8 @@ static char poolBuf[2 * 1024 * 1024]; /* 2M : recommended minimum */
77
99
* attempts to find the global Lua function '_XedgeEvent', and if the
78
100
* function is found, it will be executed as follows: _XedgeEvent("sntp")
79
101
*/
80
- static void executeXedgeEvent (ThreadJob * job , int msgh , LThreadMgr * mgr )
102
+
103
+ static void execevent (ThreadJob * job , int msgh , LThreadMgr * mgr )
81
104
{
82
105
lua_State * L = job -> Lt ;
83
106
lua_pushglobaltable (L );
@@ -91,9 +114,10 @@ static void executeXedgeEvent(ThreadJob *job, int msgh, LThreadMgr *mgr)
91
114
}
92
115
93
116
/* Thread started by xedgeOpenAUX() */
117
+
94
118
static void * checkTimeThread (void * arg )
95
119
{
96
- ThreadMutex * soDispMutex = HttpServer_getMutex (ltMgr .server );
120
+ ThreadMutex * dm = HttpServer_getMutex (ltMgr .server );
97
121
const char * d = __DATE__ ;
98
122
char buf [50 ];
99
123
@@ -102,28 +126,66 @@ static void *checkTimeThread(void *arg)
102
126
if (!(basnprintf (buf , sizeof (buf ), "Mon, %c%c %c%c%c %s %s" ,
103
127
d [4 ], d [5 ], d [0 ], d [1 ], d [2 ], d + 7 , __TIME__ ) < 0 ))
104
128
{
105
- BaTime compileT = baParseDate (buf );
106
- if (compileT )
129
+ BaTime t = baParseDate (buf );
130
+ if (t )
107
131
{
108
- compileT -= 24 * 60 * 60 ;
109
- while (baGetUnixTime () < compileT )
132
+ t -= 24 * 60 * 60 ;
133
+ while (baGetUnixTime () < t )
110
134
{
111
135
Thread_sleep (500 );
112
136
}
113
137
114
- ThreadJob * job = ThreadJob_lcreate (sizeof (ThreadJob ), executeXedgeEvent );
115
- ThreadMutex_set (soDispMutex );
138
+ ThreadJob * job = ThreadJob_lcreate (sizeof (ThreadJob ), execevent );
139
+ ThreadMutex_set (dm );
116
140
LThreadMgr_run (& ltMgr , job );
117
- ThreadMutex_release (soDispMutex );
141
+ ThreadMutex_release (dm );
118
142
}
119
143
}
120
144
121
145
return NULL ;
122
146
}
123
147
148
+ static void panic (BaFatalErrorCodes ecode1 ,
149
+ unsigned int ecode2 ,
150
+ const char * file ,
151
+ int line )
152
+ {
153
+ syslog (LOG_ERR , "Fatal error in Barracuda %d %d %s %d\n" ,
154
+ ecode1 , ecode2 , file , line );
155
+ exit (1 );
156
+ }
157
+
158
+ /* Redirect server's HttpTrace to syslog.
159
+ * https://realtimelogic.com/ba/doc/en/C/reference/html/structHttpTrace.html
160
+ */
161
+
162
+ static void flushtrace (char * buf , int bufLen )
163
+ {
164
+ buf [bufLen ] = 0 ;
165
+ syslog (LOG_INFO , "%s" , buf );
166
+ }
167
+
168
+ static void sighandler (int signo )
169
+ {
170
+ if (running )
171
+ {
172
+ printf ("\nGot SIGTERM; exiting...\n" );
173
+ setDispExit ();
174
+
175
+ /* NuttX feature: Must wait for socket select() to return */
176
+
177
+ Thread_sleep (2000 );
178
+ }
179
+ }
180
+
181
+ /***************************************************************************
182
+ * Public Functions
183
+ ***************************************************************************/
184
+
124
185
/* xedge.c calls this to initialize the IO.
125
186
* Change "/mnt/lfs" to your preference.
126
187
*/
188
+
127
189
int xedgeInitDiskIo (DiskIo * io )
128
190
{
129
191
if (DiskIo_setRootDir (io , "/mnt/lfs" ))
@@ -135,10 +197,10 @@ int xedgeInitDiskIo(DiskIo *io)
135
197
return 0 ;
136
198
}
137
199
138
-
139
200
/* xedge.c calls this; include your Lua bindings here.
140
201
* Tutorial: https://tutorial.realtimelogic.com/Lua-Bindings.lsp
141
- */
202
+ */
203
+
142
204
int xedgeOpenAUX (XedgeOpenAUX * aux )
143
205
{
144
206
pthread_t thread ;
@@ -156,51 +218,19 @@ int xedgeOpenAUX(XedgeOpenAUX *aux)
156
218
return 0 ;
157
219
}
158
220
159
- static void myErrHandler (BaFatalErrorCodes ecode1 ,
160
- unsigned int ecode2 ,
161
- const char * file ,
162
- int line )
163
- {
164
- syslog (LOG_ERR , "Fatal error in Barracuda %d %d %s %d\n" ,
165
- ecode1 , ecode2 , file , line );
166
- exit (1 );
167
- }
168
-
169
-
170
- /* Redirect server's HttpTrace to syslog.
171
- * https://realtimelogic.com/ba/doc/en/C/reference/html/structHttpTrace.html
172
- */
173
- static void flushTrace (char * buf , int bufLen )
174
- {
175
- buf [bufLen ] = 0 ;
176
- syslog (LOG_INFO , "%s" , buf );
177
- }
178
-
179
- static BaBool isRunning ;
180
-
181
- static void sigHandler (int signo )
182
- {
183
- if (isRunning )
184
- {
185
- isRunning = FALSE;
186
- printf ("\nGot SIGTERM; exiting...\n" );
187
- setDispExit ();
188
- Thread_sleep (2000 );
189
- }
190
- }
191
-
192
221
int main (int argc , FAR char * argv [])
193
222
{
194
- signal (SIGINT , sigHandler );
195
- signal (SIGTERM , sigHandler );
223
+ signal (SIGINT , sighandler );
224
+ signal (SIGTERM , sighandler );
196
225
197
226
ntpc_start ();
198
- init_dlmalloc (poolBuf , poolBuf + sizeof (poolBuf ));
199
- HttpTrace_setFLushCallback (flushTrace );
200
- HttpServer_setErrHnd (myErrHandler );
227
+ init_dlmalloc (poolbuf , poolbuf + sizeof (poolbuf ));
228
+ HttpTrace_setFLushCallback (flushtrace );
229
+ HttpServer_setErrHnd (panic );
201
230
202
- isRunning = TRUE;
231
+ running = TRUE;
203
232
barracuda ();
233
+ running = FALSE;
204
234
205
235
printf ("Exiting Xedge\n" );
206
236
return 0 ;
0 commit comments