]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/tidep0084.git/blob - tutorials/generic_sensor_tutorial/tutorial/SensorToCloud/example/npi_server2/linux_main.c
Added Support for Generic Sensor Tutorial which provides instructions on how to add...
[apps/tidep0084.git] / tutorials / generic_sensor_tutorial / tutorial / SensorToCloud / example / npi_server2 / linux_main.c
1 /******************************************************************************
2  @file linux_main.c
4  @brief TIMAC 2.0 API Linux "main" for the NPI server application
6  Group: WCS LPC
7  $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350$
9  ******************************************************************************
10  $License: BSD3 2016 $
11   
12    Copyright (c) 2015, Texas Instruments Incorporated
13    All rights reserved.
14   
15    Redistribution and use in source and binary forms, with or without
16    modification, are permitted provided that the following conditions
17    are met:
18   
19    *  Redistributions of source code must retain the above copyright
20       notice, this list of conditions and the following disclaimer.
21   
22    *  Redistributions in binary form must reproduce the above copyright
23       notice, this list of conditions and the following disclaimer in the
24       documentation and/or other materials provided with the distribution.
25   
26    *  Neither the name of Texas Instruments Incorporated nor the names of
27       its contributors may be used to endorse or promote products derived
28       from this software without specific prior written permission.
29   
30    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33    PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37    OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  ******************************************************************************
42  $Release Name: TI-15.4Stack Linux x64 SDK$
43  $Release Date: July 14, 2016 (2.00.00.30)$
44  *****************************************************************************/
46 #include "compiler.h"
47 #include "npi_server2.h"
49 #include "ini_file.h"       /* this reads our ini file */
50 #include "log.h"            /* our logging scheme */
51 #include "mt_msg.h"
52 #include "mt_msg_dbg.h"
53 #include "timer.h"
54 #include "fatal.h"
55 #include "stream_socket.h"  /* we use a socket in our app */
56 #include "stream_uart.h"    /* and a uart. */
58 #include <stdio.h>
59 #include <stdlib.h>
61 const struct ini_flag_name * const log_flag_names[] = {
62     log_builtin_flag_names,
63     mt_msg_log_flags,
64     /* terminate */
65     NULL
66 };
68 /*!
69  * @brief Handle any config file settings for the uart.
70  *
71  * @param pINI - ini file parse info
72  * @param handled - set to true if the item was handled
73  */
74 static int my_UART_INI_settings(struct ini_parser *pINI, bool *handled)
75 {
76     int r;
78     r = 0;
79     if(INI_itemMatches(pINI, "uart-cfg", NULL))
80     {
81         r = UART_INI_settingsOne(pINI, handled, &my_uart_cfg);
82     }
83     return r;
84 }
86 static int my_SOCKET_INI_settings(struct ini_parser *pINI, bool *handled)
87 {
88     int r;
90     r = 0;
91     if(INI_itemMatches(pINI, "socket-cfg", NULL))
92     {
93         r = SOCKET_INI_settingsOne(pINI, handled, &my_socket_cfg);
94     }
95     return r;
96 }
98 static int my_MT_MSG_INI_settings(struct ini_parser *pINI, bool *handled)
99 {
100     int r;
102     r = 0;
103     if(INI_itemMatches(pINI, "socket-interface", NULL))
104     {
105         r = MT_MSG_INI_settings(pINI, handled, &socket_interface_template);
106     }
108     if(INI_itemMatches(pINI, "uart-interface", NULL))
109     {
110         r = MT_MSG_INI_settings(pINI, handled, &common_uart_interface);
111     }
112     return r;
115 static int my_APP_settings(struct ini_parser *pINI, bool *handled)
117     if(!INI_itemMatches(pINI, "application", NULL))
118     {
119         return 0;
120     }
122     if(INI_itemMatches(pINI,NULL,"msg-dbg-data"))
123     {
124         struct mt_msg_dbg **ppDbg;
126         /* append at end of list */
127         ppDbg = &(ALL_MT_MSG_DBG);
128         while( *ppDbg ){
129             ppDbg = &((*ppDbg)->m_pNext);
130         }
131         INI_dequote(pINI);
132         *ppDbg = MT_MSG_dbg_load(pINI->item_value);
133         *handled = true;
134         return 0;
135     }
136     return 0;
139 static ini_rd_callback * const ini_cb_table[] = {
140     LOG_INI_settings,
141     my_UART_INI_settings,
142     my_SOCKET_INI_settings,
143     my_MT_MSG_INI_settings,
144     my_APP_settings,
145     /* Terminate list */
146     NULL
147 };
149 static int cfg_callback(struct ini_parser *pINI, bool *handled)
151     int x;
152     int r;
154     for(x = 0 ; ini_cb_table[x] ; x++)
155     {
156         r = (*(ini_cb_table[x]))(pINI, handled);
157         if(*handled)
158         {
159             return r;
160         }
161     }
162     /* let the system handle it */
163     return 0;
166 int main(int argc, char **argv)
168     int r;
169     const char *cfg_filename;
171     cfg_filename = "npi_server2.cfg";
173     switch(argc)
174     {
175     default:
176         fprintf(stderr, "usage: %s [CONFIGFILE]\n", argv[0]);
177         fprintf(stderr, "\n");
178         fprintf(stderr, "Default CONFIGFILE = %s\n", cfg_filename);
179         exit(1);
180     case 1:
181         /* use default */
182         break;
183     case 2:
184         cfg_filename = argv[1];
185         break;
186     }
188     /* Basic initialization */
189     SOCKET_init();
190     STREAM_init();
191     TIMER_init();
192     LOG_init("/dev/stderr");
193     /* we want these logs to begin with */
194     log_cfg.log_flags = LOG_FATAL | LOG_WARN | LOG_ERROR;
196     APP_defaults();
198     /* Rad our configuration file */
199     r = INI_read(cfg_filename, cfg_callback, 0);
200     if(r != 0)
201     {
202         FATAL_printf("Failed to read cfg file\n");
203     }
205     APP_main();
207     exit(0);
210 /*
211  *  ========================================
212  *  Texas Instruments Micro Controller Style
213  *  ========================================
214  *  Local Variables:
215  *  mode: c
216  *  c-file-style: "bsd"
217  *  tab-width: 4
218  *  c-basic-offset: 4
219  *  indent-tabs-mode: nil
220  *  End:
221  *  vim:set  filetype=c tabstop=4 shiftwidth=4 expandtab=true
222  */