summaryrefslogtreecommitdiffstats
path: root/apps/examples/nxconsole/nxcon_main.c
blob: 5a9d20307bdffdc9f073077015b13bc70bb80966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/****************************************************************************
 * examples/nxconsole/nxcon_main.c
 *
 *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <debug.h>

#ifdef CONFIG_NX_LCDDRIVER
#  include <nuttx/lcd/lcd.h>
#else
#  include <nuttx/fb.h>
#endif

#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nxfonts.h>
#include <nuttx/nx/nxconsole.h>

#include <apps/nsh.h>

#include "nxcon_internal.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Public Data
 ****************************************************************************/

/* All example global variables are retained in a structure to minimize
 * the chance of name collisions.
 */

struct nxcon_state_s g_nxcon_vars;

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: nxcon_initialize
 ****************************************************************************/

static int nxcon_initialize(void)
{
  struct sched_param param;
  pthread_t thread;
  pid_t servrid;
  int ret;

  /* Set the client task priority */

  param.sched_priority = CONFIG_EXAMPLES_NXCON_CLIENTPRIO;
  ret = sched_setparam(0, &param);
  if (ret < 0)
    {
      message("nxcon_initialize: sched_setparam failed: %d\n" , ret);
      return ERROR;
    }

  /* Start the server task */

  message("nxcon_initialize: Starting nxcon_server task\n");
  servrid = task_create("NX Server", CONFIG_EXAMPLES_NXCON_SERVERPRIO,
                        CONFIG_EXAMPLES_NXCON_STACKSIZE, nxcon_server, NULL);
  if (servrid < 0)
    {
      message("nxcon_initialize: Failed to create nxcon_server task: %d\n", errno);
      return ERROR;
    }

  /* Wait a bit to let the server get started */

  sleep(1);

  /* Connect to the server */

  g_nxcon_vars.hnx = nx_connect();
  if (g_nxcon_vars.hnx)
    {
       pthread_attr_t attr;

       /* Start a separate thread to listen for server events.  This is probably
        * the least efficient way to do this, but it makes this example flow more
        * smoothly.
        */

       (void)pthread_attr_init(&attr);
       param.sched_priority = CONFIG_EXAMPLES_NXCON_LISTENERPRIO;
       (void)pthread_attr_setschedparam(&attr, &param);
       (void)pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NXCON_STACKSIZE);

       ret = pthread_create(&thread, &attr, nxcon_listener, NULL);
       if (ret != 0)
         {
            printf("nxcon_initialize: pthread_create failed: %d\n", ret);
            return ERROR;
         }

       /* Don't return until we are connected to the server */

       while (!g_nxcon_vars.connected)
         {
           /* Wait for the listener thread to wake us up when we really
            * are connected.
            */

           (void)sem_wait(&g_nxcon_vars.eventsem);
         }
    }
  else
    {
      message("nxcon_initialize: nx_connect failed: %d\n", errno);
      return ERROR;
    }
  return OK;
}

/****************************************************************************
 * Name: nxcon_task
 ****************************************************************************/

static int nxcon_task(int argc, char **argv)
{
  /* If the console front end is selected, then run it on this thread */

#ifdef CONFIG_NSH_CONSOLE
  (void)nsh_consolemain(0, NULL);
#endif

  printf("nxcon_task: Unregister the NX console device\n");
  (void)nxcon_unregister(g_nxcon_vars.hdrvr);

  printf("nxcon_task: Close the window\n");
  (void)nxtk_closewindow(g_nxcon_vars.hwnd);

  /* Disconnect from the server */

  printf("nxcon_task: Disconnect from the server\n");
  nx_disconnect(g_nxcon_vars.hnx);

  return EXIT_SUCCESS;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: user_start
 ****************************************************************************/

int user_start(int argc, char **argv)
{
  nxgl_mxpixel_t color;
  int fd;
  int ret;

  /* General Initialization *************************************************/
  /* Reset all global data */

  message("user_start: Started\n");
  memset(&g_nxcon_vars, 0, sizeof(struct nxcon_state_s));

  /* Call all C++ static constructors */

#if defined(CONFIG_HAVE_CXX) && defined(CONFIG_HAVE_CXXINITIALIZE)
  up_cxxinitialize();
#endif

  /* NSH Initialization *****************************************************/
  /* Initialize the NSH library */

  message("user_start: Initialize NSH\n");
  nsh_initialize();

  /* If the Telnet console is selected as a front-end, then start the
   * Telnet daemon.
   */

#ifdef CONFIG_NSH_TELNET
  ret = nsh_telnetstart();
  if (ret < 0)
    {
     /* The daemon is NOT running.  Report the the error then fail...
      * either with the serial console up or just exiting.
      */

     fprintf(stderr, "ERROR: Failed to start TELNET daemon: %d\n", ret);
   }
#endif
  /* NX Initialization ******************************************************/
  /* Initialize NX */

  message("user_start: Initialize NX\n");
  ret = nxcon_initialize();
  message("user_start: NX handle=%p\n", g_nxcon_vars.hnx);
  if (!g_nxcon_vars.hnx || ret < 0)
    {
      message("user_start: Failed to get NX handle: %d\n", errno);
      goto errout;
    }

  /* Set the background to the configured background color */

  message("user_start: Set background color=%d\n", CONFIG_EXAMPLES_NXCON_BGCOLOR);
  color = CONFIG_EXAMPLES_NXCON_BGCOLOR;
  ret = nx_setbgcolor(g_nxcon_vars.hnx, &color);
  if (ret < 0)
    {
      message("user_start: nx_setbgcolor failed: %d\n", errno);
      goto errout_with_nx;
    }

  /* Window Configuration ***************************************************/
  /* Create a window */

  message("user_start: Create window\n");
  g_nxcon_vars.hwnd = nxtk_openwindow(g_nxcon_vars.hnx, &g_nxconcb, NULL);
  if (!g_nxcon_vars.hwnd)
    {
      message("user_start: nxtk_openwindow failed: %d\n", errno);
      goto errout_with_nx;
    }
  message("user_start: hwnd=%p\n", g_nxcon_vars.hwnd);

  /* Wait until we have the screen resolution.  We'll have this immediately
   * unless we are dealing with the NX server.
   */

  while (!g_nxcon_vars.haveres)
    {
      (void)sem_wait(&g_nxcon_vars.eventsem);
    }
  message("user_start: Screen resolution (%d,%d)\n", g_nxcon_vars.xres, g_nxcon_vars.yres);

  /* Determine the size and position of the window */

  g_nxcon_vars.wndo.wsize.w = g_nxcon_vars.xres / 2 + g_nxcon_vars.xres / 4;
  g_nxcon_vars.wndo.wsize.h = g_nxcon_vars.yres / 2 + g_nxcon_vars.yres / 4;

  g_nxcon_vars.wpos.x       = g_nxcon_vars.xres / 8;
  g_nxcon_vars.wpos.y       = g_nxcon_vars.yres / 8;

  /* Set the window position */

  message("user_start: Set window position to (%d,%d)\n",
          g_nxcon_vars.wpos.x, g_nxcon_vars.wpos.y);

  ret = nxtk_setposition(g_nxcon_vars.hwnd, &g_nxcon_vars.wpos);
  if (ret < 0)
    {
      message("user_start: nxtk_setposition failed: %d\n", errno);
      goto errout_with_hwnd;
    }

  /* Set the window size */

  message("user_start: Set window size to (%d,%d)\n",
          g_nxcon_vars.wndo.wsize.w, g_nxcon_vars.wndo.wsize.h);

  ret = nxtk_setsize(g_nxcon_vars.hwnd, &g_nxcon_vars.wndo.wsize);
  if (ret < 0)
    {
      message("user_start: nxtk_setsize failed: %d\n", errno);
      goto errout_with_hwnd;
    }

  /* Open the toolbar */

  message("user_start: Add toolbar to window\n");
  ret = nxtk_opentoolbar(g_nxcon_vars.hwnd, CONFIG_EXAMPLES_NXCON_TOOLBAR_HEIGHT, &g_nxtoolcb, NULL);
  if (ret < 0)
    {
      message("user_start: nxtk_opentoolbar failed: %d\n", errno);
      goto errout_with_hwnd;
    }

  /* Sleep a little bit to allow the server to catch up */
 
  sleep(2);

  /* NxConsole Configuration ************************************************/
  /* Use the window to create an NX console */

  g_nxcon_vars.wndo.wcolor[0] = CONFIG_EXAMPLES_NXCON_WCOLOR;
  g_nxcon_vars.wndo.fcolor[0] = CONFIG_EXAMPLES_NXCON_FONTCOLOR;
  g_nxcon_vars.wndo.fontid    = CONFIG_EXAMPLES_NXCON_FONTID;

  g_nxcon_vars.hdrvr = nxtk_register(g_nxcon_vars.hwnd, &g_nxcon_vars.wndo, CONFIG_EXAMPLES_NXCON_MINOR);
  if (!g_nxcon_vars.hdrvr)
    {
      message("user_start: nxtk_register failed: %d\n", errno);
      goto errout_with_hwnd;
    }

  /* Open the NxConsole driver */

  fd = open(CONFIG_EXAMPLES_NXCON_DEVNAME, O_WRONLY);
  if (fd < 0)
    {
      message("user_start: open %s read-only failed: %d\n",
              CONFIG_EXAMPLES_NXCON_DEVNAME, errno);
      goto errout_with_driver;
    }

  /* Start Console Task *****************************************************/
  /* Now re-direct stdout and stderr so that they use the NX console driver.
   * Note that stdin is retained (file descriptor 0, probably the the serial console).
    */

   message("user_start: Starting the console task\n");
   msgflush();

  (void)fflush(stdout);
  (void)fflush(stderr);

  (void)fclose(stdout);
  (void)fclose(stderr);

  (void)dup2(fd, 1);
  (void)dup2(fd, 2);

   /* And we can close our original driver file descriptor */

   close(fd);

   /* And start the console task.  It will inherit stdin, stdout, and stderr
    * from this task.
    */
 
   g_nxcon_vars.pid = TASK_CREATE("NxConsole", CONFIG_EXAMPLES_NXCONSOLE_PRIO,
                                  CONFIG_EXAMPLES_NXCONSOLE_STACKSIZE,
                                  nxcon_task, NULL);
   ASSERT(g_nxcon_vars.pid > 0);
   return EXIT_SUCCESS;

  /* Error Exits ************************************************************/

errout_with_driver:
  (void)nxcon_unregister(g_nxcon_vars.hdrvr);

errout_with_hwnd:
  (void)nxtk_closewindow(g_nxcon_vars.hwnd);

errout_with_nx:
  /* Disconnect from the server */

  nx_disconnect(g_nxcon_vars.hnx);
errout:
  return EXIT_FAILURE;
}