]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/tidep0084.git/blob - components/common/src/stream_socket_ini.c
Updated TI Linux Sensor To Cloud to the latest TI 15.4-Stack v2.4, now with CC13x2...
[apps/tidep0084.git] / components / common / src / stream_socket_ini.c
1 /******************************************************************************
2  @file stream_socket_ini.c
4  @brief TIMAC 2.0 API Parse INI files to socket interfaces
6  Group: WCS LPC
7  $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350, CC1352$
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: Sept 27, 2017 (2.04.00.13)$
44  *****************************************************************************/
46 #include "compiler.h"
48 #include "stream.h"
49 #include "ini_file.h"
50 #include "stream_socket.h"
52 #include <string.h>
54 struct socket_cfg ALL_INI_SOCKETS[ INI_MAX_SOCKETS ];
56 static const struct ini_flag_name socket_types[] = {
57     { .name = "server", .value = 's' },
58     { .name = "client", .value = 'c' },
59     { .name = NULL }
60 };
62 /*
63  * Parse socket configuration information from an ini file
64  * Handles both server and client sockets
65  *
66  * Public function defined in stream_socket.h
67  */
69 int SOCKET_INI_settingsNth(struct ini_parser *pINI, bool *handled)
70 {
71     unsigned nth;
73     if(pINI->item_name == NULL)
74     {
75         /* this is the section name, we don't care about it. */
76         return (0);
77     }
79     /* is this a SOCKET section? */
80     if(!INI_isNth(pINI, "socket-", &nth))
81     {
82         /* Nope, then leave */
83         return (0);
84     }
86     /* Is the number value? */
87     if(nth>= INI_MAX_SOCKETS)
88     {
89         INI_syntaxError(pINI, "invalid-socket-index\n");
90         return (-1);
91     }
93     return (SOCKET_INI_settingsOne(pINI, handled, &(ALL_INI_SOCKETS[nth])));
94 }
96 /*
97   handle INI file callbacks for a socket interface.
99   Public function in stream_socket.h
100  */
101 int SOCKET_INI_settingsOne(struct ini_parser *pINI,
102                             bool *handled,
103                             struct socket_cfg *pCFG)
105     bool is_not;
106     const struct ini_flag_name *pF;
107     int r;
109     if(pINI->item_name == NULL)
110     {
111         return (0);
112     }
114     r = -1;
115     is_not = false;
117     /* match name? */
118     if(INI_itemMatches(pINI, NULL, "type"))
119     {
120         r = 0;
121         INI_dequote(pINI);
122         pF = INI_flagLookup(socket_types, pINI->item_value, &is_not);
123         if(pF == NULL)
124         {
125             INI_syntaxError(pINI, "unknown flag: %s\n", pINI->item_value);
126         }
127         else
128         {
129             pCFG->ascp = (int)(pF->value);
130         }
131     }
133     if(INI_itemMatches(pINI, NULL, "host"))
134     {
135         pCFG->host = INI_itemValue_strdup(pINI);
136         r = 0;
137     }
139     if(INI_itemMatches(pINI, NULL, "service"))
140     {
141         pCFG->service = INI_itemValue_strdup(pINI);
142         r = 0;
143     }
145     if(INI_itemMatches(pINI, NULL, "devicename"))
146     {
147         pCFG->device_binding = INI_itemValue_strdup(pINI);
148         r = 0;
149     }
151     if(INI_itemMatches(pINI, NULL, "server_backlog"))
152     {
153         pCFG->server_backlog = (unsigned)INI_valueAsU64(pINI);
154         r = 0;
155     }
157     if(INI_itemMatches(pINI, NULL, "inet"))
158     {
159         r = INI_isValueInt(pINI, &(pCFG->inet_4or6));
160         if(r)
161         {
162             if((pCFG->inet_4or6 == 4) || (pCFG->inet_4or6 == 6))
163             {
164                 r = 0;
165                 goto done;
166             }
167             else
168             {
169                 goto bad_inet46;
170             }
171         }
172         else
173         {
174             if(0 == strcmp(pINI->item_value, "any"))
175             {
176                 r = 0;
177                 pCFG->inet_4or6 = 0;
178                 goto done;
179             }
180             else
181             {
182             bad_inet46:
183                 INI_syntaxError(pINI,
184                                 "inet must be 4, 6 or any, not: %s\n",
185                                 pINI->item_value);
186                 return (-1);
187             }
188         }
189     }
190 done:
191     if(r == 0)
192     {
193         /* We handle it */
194         *handled = true;
195     }
196     else
197     {
198         INI_syntaxError(pINI, "unknown socket item\n");
199     }
200     return (r);
203 /*
204  *  ========================================
205  *  Texas Instruments Micro Controller Style
206  *  ========================================
207  *  Local Variables:
208  *  mode: c
209  *  c-file-style: "bsd"
210  *  tab-width: 4
211  *  c-basic-offset: 4
212  *  indent-tabs-mode: nil
213  *  End:
214  *  vim:set  filetype=c tabstop=4 shiftwidth=4 expandtab=true
215  */