1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * Network Interface Sequential API module
4195972f6Sopenharmony_ci *
5195972f6Sopenharmony_ci * @defgroup netifapi NETIF API
6195972f6Sopenharmony_ci * @ingroup sequential_api
7195972f6Sopenharmony_ci * Thread-safe functions to be called from non-TCPIP threads
8195972f6Sopenharmony_ci *
9195972f6Sopenharmony_ci * @defgroup netifapi_netif NETIF related
10195972f6Sopenharmony_ci * @ingroup netifapi
11195972f6Sopenharmony_ci * To be called from non-TCPIP threads
12195972f6Sopenharmony_ci */
13195972f6Sopenharmony_ci
14195972f6Sopenharmony_ci/*
15195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
16195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
17195972f6Sopenharmony_ci *
18195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
19195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
20195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
21195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
22195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
23195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
24195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
25195972f6Sopenharmony_ci *
26195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35195972f6Sopenharmony_ci * OF SUCH DAMAGE.
36195972f6Sopenharmony_ci *
37195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
38195972f6Sopenharmony_ci *
39195972f6Sopenharmony_ci */
40195972f6Sopenharmony_ci
41195972f6Sopenharmony_ci#include "lwip/opt.h"
42195972f6Sopenharmony_ci
43195972f6Sopenharmony_ci#if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */
44195972f6Sopenharmony_ci
45195972f6Sopenharmony_ci#include "lwip/etharp.h"
46195972f6Sopenharmony_ci#include "lwip/netifapi.h"
47195972f6Sopenharmony_ci#include "lwip/memp.h"
48195972f6Sopenharmony_ci#include "lwip/priv/tcpip_priv.h"
49195972f6Sopenharmony_ci
50195972f6Sopenharmony_ci#include <string.h> /* strncpy */
51195972f6Sopenharmony_ci
52195972f6Sopenharmony_ci#define NETIFAPI_VAR_REF(name)      API_VAR_REF(name)
53195972f6Sopenharmony_ci#define NETIFAPI_VAR_DECLARE(name)  API_VAR_DECLARE(struct netifapi_msg, name)
54195972f6Sopenharmony_ci#define NETIFAPI_VAR_ALLOC(name)    API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM)
55195972f6Sopenharmony_ci#define NETIFAPI_VAR_FREE(name)     API_VAR_FREE(MEMP_NETIFAPI_MSG, name)
56195972f6Sopenharmony_ci
57195972f6Sopenharmony_ci/**
58195972f6Sopenharmony_ci * Call netif_add() inside the tcpip_thread context.
59195972f6Sopenharmony_ci */
60195972f6Sopenharmony_cistatic err_t
61195972f6Sopenharmony_cinetifapi_do_netif_add(struct tcpip_api_call_data *m)
62195972f6Sopenharmony_ci{
63195972f6Sopenharmony_ci  /* cast through void* to silence alignment warnings.
64195972f6Sopenharmony_ci   * We know it works because the structs have been instantiated as struct netifapi_msg */
65195972f6Sopenharmony_ci  struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
66195972f6Sopenharmony_ci#ifdef LOSCFG_NET_CONTAINER
67195972f6Sopenharmony_ci  if (!netif_add( msg->netif, get_curr_process_net_group(),
68195972f6Sopenharmony_ci#else
69195972f6Sopenharmony_ci  if (!netif_add( msg->netif,
70195972f6Sopenharmony_ci#endif
71195972f6Sopenharmony_ci#if LWIP_IPV4
72195972f6Sopenharmony_ci                  API_EXPR_REF(msg->msg.add.ipaddr),
73195972f6Sopenharmony_ci                  API_EXPR_REF(msg->msg.add.netmask),
74195972f6Sopenharmony_ci                  API_EXPR_REF(msg->msg.add.gw),
75195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */
76195972f6Sopenharmony_ci                  msg->msg.add.state,
77195972f6Sopenharmony_ci                  msg->msg.add.init,
78195972f6Sopenharmony_ci                  msg->msg.add.input)) {
79195972f6Sopenharmony_ci    return ERR_IF;
80195972f6Sopenharmony_ci  } else {
81195972f6Sopenharmony_ci    return ERR_OK;
82195972f6Sopenharmony_ci  }
83195972f6Sopenharmony_ci}
84195972f6Sopenharmony_ci
85195972f6Sopenharmony_ci#if LWIP_IPV4
86195972f6Sopenharmony_ci/**
87195972f6Sopenharmony_ci * Call netif_set_addr() inside the tcpip_thread context.
88195972f6Sopenharmony_ci */
89195972f6Sopenharmony_cistatic err_t
90195972f6Sopenharmony_cinetifapi_do_netif_set_addr(struct tcpip_api_call_data *m)
91195972f6Sopenharmony_ci{
92195972f6Sopenharmony_ci  /* cast through void* to silence alignment warnings.
93195972f6Sopenharmony_ci   * We know it works because the structs have been instantiated as struct netifapi_msg */
94195972f6Sopenharmony_ci  struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
95195972f6Sopenharmony_ci
96195972f6Sopenharmony_ci  netif_set_addr( msg->netif,
97195972f6Sopenharmony_ci                  API_EXPR_REF(msg->msg.add.ipaddr),
98195972f6Sopenharmony_ci                  API_EXPR_REF(msg->msg.add.netmask),
99195972f6Sopenharmony_ci                  API_EXPR_REF(msg->msg.add.gw));
100195972f6Sopenharmony_ci  return ERR_OK;
101195972f6Sopenharmony_ci}
102195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */
103195972f6Sopenharmony_ci
104195972f6Sopenharmony_ci/**
105195972f6Sopenharmony_ci* Call netif_name_to_index() inside the tcpip_thread context.
106195972f6Sopenharmony_ci*/
107195972f6Sopenharmony_cistatic err_t
108195972f6Sopenharmony_cinetifapi_do_name_to_index(struct tcpip_api_call_data *m)
109195972f6Sopenharmony_ci{
110195972f6Sopenharmony_ci  /* cast through void* to silence alignment warnings.
111195972f6Sopenharmony_ci   * We know it works because the structs have been instantiated as struct netifapi_msg */
112195972f6Sopenharmony_ci  struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
113195972f6Sopenharmony_ci
114195972f6Sopenharmony_ci  msg->msg.ifs.index = netif_name_to_index(msg->msg.ifs.name);
115195972f6Sopenharmony_ci  return ERR_OK;
116195972f6Sopenharmony_ci}
117195972f6Sopenharmony_ci
118195972f6Sopenharmony_ci/**
119195972f6Sopenharmony_ci* Call netif_index_to_name() inside the tcpip_thread context.
120195972f6Sopenharmony_ci*/
121195972f6Sopenharmony_cistatic err_t
122195972f6Sopenharmony_cinetifapi_do_index_to_name(struct tcpip_api_call_data *m)
123195972f6Sopenharmony_ci{
124195972f6Sopenharmony_ci  /* cast through void* to silence alignment warnings.
125195972f6Sopenharmony_ci   * We know it works because the structs have been instantiated as struct netifapi_msg */
126195972f6Sopenharmony_ci  struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
127195972f6Sopenharmony_ci
128195972f6Sopenharmony_ci#ifdef LOSCFG_NET_CONTAINER
129195972f6Sopenharmony_ci  if (!netif_index_to_name(msg->msg.ifs.index, msg->msg.ifs.name, get_curr_process_net_group())) {
130195972f6Sopenharmony_ci#else
131195972f6Sopenharmony_ci  if (!netif_index_to_name(msg->msg.ifs.index, msg->msg.ifs.name)) {
132195972f6Sopenharmony_ci#endif
133195972f6Sopenharmony_ci    /* return failure via empty name */
134195972f6Sopenharmony_ci    msg->msg.ifs.name[0] = '\0';
135195972f6Sopenharmony_ci  }
136195972f6Sopenharmony_ci  return ERR_OK;
137195972f6Sopenharmony_ci}
138195972f6Sopenharmony_ci
139195972f6Sopenharmony_ci/**
140195972f6Sopenharmony_ci * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the
141195972f6Sopenharmony_ci * tcpip_thread context.
142195972f6Sopenharmony_ci */
143195972f6Sopenharmony_cistatic err_t
144195972f6Sopenharmony_cinetifapi_do_netif_common(struct tcpip_api_call_data *m)
145195972f6Sopenharmony_ci{
146195972f6Sopenharmony_ci  /* cast through void* to silence alignment warnings.
147195972f6Sopenharmony_ci   * We know it works because the structs have been instantiated as struct netifapi_msg */
148195972f6Sopenharmony_ci  struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
149195972f6Sopenharmony_ci
150195972f6Sopenharmony_ci  if (msg->msg.common.errtfunc != NULL) {
151195972f6Sopenharmony_ci    return msg->msg.common.errtfunc(msg->netif);
152195972f6Sopenharmony_ci  } else {
153195972f6Sopenharmony_ci    msg->msg.common.voidfunc(msg->netif);
154195972f6Sopenharmony_ci    return ERR_OK;
155195972f6Sopenharmony_ci  }
156195972f6Sopenharmony_ci}
157195972f6Sopenharmony_ci
158195972f6Sopenharmony_ci#if LWIP_ARP && LWIP_IPV4
159195972f6Sopenharmony_ci/**
160195972f6Sopenharmony_ci * @ingroup netifapi_arp
161195972f6Sopenharmony_ci * Add or update an entry in the ARP cache.
162195972f6Sopenharmony_ci * For an update, ipaddr is used to find the cache entry.
163195972f6Sopenharmony_ci *
164195972f6Sopenharmony_ci * @param ipaddr IPv4 address of cache entry
165195972f6Sopenharmony_ci * @param ethaddr hardware address mapped to ipaddr
166195972f6Sopenharmony_ci * @param type type of ARP cache entry
167195972f6Sopenharmony_ci * @return ERR_OK: entry added/updated, else error from err_t
168195972f6Sopenharmony_ci */
169195972f6Sopenharmony_cierr_t
170195972f6Sopenharmony_cinetifapi_arp_add(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, enum netifapi_arp_entry type)
171195972f6Sopenharmony_ci{
172195972f6Sopenharmony_ci  err_t err;
173195972f6Sopenharmony_ci
174195972f6Sopenharmony_ci  /* We only support permanent entries currently */
175195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(type);
176195972f6Sopenharmony_ci
177195972f6Sopenharmony_ci#if ETHARP_SUPPORT_STATIC_ENTRIES && LWIP_TCPIP_CORE_LOCKING
178195972f6Sopenharmony_ci  LOCK_TCPIP_CORE();
179195972f6Sopenharmony_ci  err = etharp_add_static_entry(ipaddr, ethaddr);
180195972f6Sopenharmony_ci  UNLOCK_TCPIP_CORE();
181195972f6Sopenharmony_ci#else
182195972f6Sopenharmony_ci  /* @todo add new vars to struct netifapi_msg and create a 'do' func */
183195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(ipaddr);
184195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(ethaddr);
185195972f6Sopenharmony_ci  err = ERR_VAL;
186195972f6Sopenharmony_ci#endif /* ETHARP_SUPPORT_STATIC_ENTRIES && LWIP_TCPIP_CORE_LOCKING */
187195972f6Sopenharmony_ci
188195972f6Sopenharmony_ci  return err;
189195972f6Sopenharmony_ci}
190195972f6Sopenharmony_ci
191195972f6Sopenharmony_ci/**
192195972f6Sopenharmony_ci * @ingroup netifapi_arp
193195972f6Sopenharmony_ci * Remove an entry in the ARP cache identified by ipaddr
194195972f6Sopenharmony_ci *
195195972f6Sopenharmony_ci * @param ipaddr IPv4 address of cache entry
196195972f6Sopenharmony_ci * @param type type of ARP cache entry
197195972f6Sopenharmony_ci * @return ERR_OK: entry removed, else error from err_t
198195972f6Sopenharmony_ci */
199195972f6Sopenharmony_cierr_t
200195972f6Sopenharmony_cinetifapi_arp_remove(const ip4_addr_t *ipaddr, enum netifapi_arp_entry type)
201195972f6Sopenharmony_ci{
202195972f6Sopenharmony_ci  err_t err;
203195972f6Sopenharmony_ci
204195972f6Sopenharmony_ci  /* We only support permanent entries currently */
205195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(type);
206195972f6Sopenharmony_ci
207195972f6Sopenharmony_ci#if ETHARP_SUPPORT_STATIC_ENTRIES && LWIP_TCPIP_CORE_LOCKING
208195972f6Sopenharmony_ci  LOCK_TCPIP_CORE();
209195972f6Sopenharmony_ci  err = etharp_remove_static_entry(ipaddr);
210195972f6Sopenharmony_ci  UNLOCK_TCPIP_CORE();
211195972f6Sopenharmony_ci#else
212195972f6Sopenharmony_ci  /* @todo add new vars to struct netifapi_msg and create a 'do' func */
213195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(ipaddr);
214195972f6Sopenharmony_ci  err = ERR_VAL;
215195972f6Sopenharmony_ci#endif /* ETHARP_SUPPORT_STATIC_ENTRIES && LWIP_TCPIP_CORE_LOCKING */
216195972f6Sopenharmony_ci
217195972f6Sopenharmony_ci  return err;
218195972f6Sopenharmony_ci}
219195972f6Sopenharmony_ci#endif /* LWIP_ARP && LWIP_IPV4 */
220195972f6Sopenharmony_ci
221195972f6Sopenharmony_ci/**
222195972f6Sopenharmony_ci * @ingroup netifapi_netif
223195972f6Sopenharmony_ci * Call netif_add() in a thread-safe way by running that function inside the
224195972f6Sopenharmony_ci * tcpip_thread context.
225195972f6Sopenharmony_ci *
226195972f6Sopenharmony_ci * @note for params @see netif_add()
227195972f6Sopenharmony_ci */
228195972f6Sopenharmony_cierr_t
229195972f6Sopenharmony_cinetifapi_netif_add(struct netif *netif,
230195972f6Sopenharmony_ci#if LWIP_IPV4
231195972f6Sopenharmony_ci                   const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
232195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */
233195972f6Sopenharmony_ci                   void *state, netif_init_fn init, netif_input_fn input)
234195972f6Sopenharmony_ci{
235195972f6Sopenharmony_ci  err_t err;
236195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
237195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
238195972f6Sopenharmony_ci
239195972f6Sopenharmony_ci#if LWIP_IPV4
240195972f6Sopenharmony_ci  if (ipaddr == NULL) {
241195972f6Sopenharmony_ci    ipaddr = IP4_ADDR_ANY4;
242195972f6Sopenharmony_ci  }
243195972f6Sopenharmony_ci  if (netmask == NULL) {
244195972f6Sopenharmony_ci    netmask = IP4_ADDR_ANY4;
245195972f6Sopenharmony_ci  }
246195972f6Sopenharmony_ci  if (gw == NULL) {
247195972f6Sopenharmony_ci    gw = IP4_ADDR_ANY4;
248195972f6Sopenharmony_ci  }
249195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */
250195972f6Sopenharmony_ci
251195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).netif = netif;
252195972f6Sopenharmony_ci#if LWIP_IPV4
253195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.ipaddr  = NETIFAPI_VAR_REF(ipaddr);
254195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
255195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.gw      = NETIFAPI_VAR_REF(gw);
256195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */
257195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.state   = state;
258195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.init    = init;
259195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.input   = input;
260195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_netif_add, &API_VAR_REF(msg).call);
261195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
262195972f6Sopenharmony_ci  return err;
263195972f6Sopenharmony_ci}
264195972f6Sopenharmony_ci
265195972f6Sopenharmony_ci#if LWIP_IPV4
266195972f6Sopenharmony_ci/**
267195972f6Sopenharmony_ci * @ingroup netifapi_netif
268195972f6Sopenharmony_ci * Call netif_set_addr() in a thread-safe way by running that function inside the
269195972f6Sopenharmony_ci * tcpip_thread context.
270195972f6Sopenharmony_ci *
271195972f6Sopenharmony_ci * @note for params @see netif_set_addr()
272195972f6Sopenharmony_ci */
273195972f6Sopenharmony_cierr_t
274195972f6Sopenharmony_cinetifapi_netif_set_addr(struct netif *netif,
275195972f6Sopenharmony_ci                        const ip4_addr_t *ipaddr,
276195972f6Sopenharmony_ci                        const ip4_addr_t *netmask,
277195972f6Sopenharmony_ci                        const ip4_addr_t *gw)
278195972f6Sopenharmony_ci{
279195972f6Sopenharmony_ci  err_t err;
280195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
281195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
282195972f6Sopenharmony_ci
283195972f6Sopenharmony_ci  if (ipaddr == NULL) {
284195972f6Sopenharmony_ci    ipaddr = IP4_ADDR_ANY4;
285195972f6Sopenharmony_ci  }
286195972f6Sopenharmony_ci  if (netmask == NULL) {
287195972f6Sopenharmony_ci    netmask = IP4_ADDR_ANY4;
288195972f6Sopenharmony_ci  }
289195972f6Sopenharmony_ci  if (gw == NULL) {
290195972f6Sopenharmony_ci    gw = IP4_ADDR_ANY4;
291195972f6Sopenharmony_ci  }
292195972f6Sopenharmony_ci
293195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).netif = netif;
294195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.ipaddr  = NETIFAPI_VAR_REF(ipaddr);
295195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
296195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.add.gw      = NETIFAPI_VAR_REF(gw);
297195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_netif_set_addr, &API_VAR_REF(msg).call);
298195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
299195972f6Sopenharmony_ci  return err;
300195972f6Sopenharmony_ci}
301195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */
302195972f6Sopenharmony_ci
303195972f6Sopenharmony_ci/**
304195972f6Sopenharmony_ci * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe
305195972f6Sopenharmony_ci * way by running that function inside the tcpip_thread context.
306195972f6Sopenharmony_ci *
307195972f6Sopenharmony_ci * @note use only for functions where there is only "netif" parameter.
308195972f6Sopenharmony_ci */
309195972f6Sopenharmony_cierr_t
310195972f6Sopenharmony_cinetifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
311195972f6Sopenharmony_ci                      netifapi_errt_fn errtfunc)
312195972f6Sopenharmony_ci{
313195972f6Sopenharmony_ci  err_t err;
314195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
315195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
316195972f6Sopenharmony_ci
317195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).netif = netif;
318195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.common.voidfunc = voidfunc;
319195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.common.errtfunc = errtfunc;
320195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_netif_common, &API_VAR_REF(msg).call);
321195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
322195972f6Sopenharmony_ci  return err;
323195972f6Sopenharmony_ci}
324195972f6Sopenharmony_ci
325195972f6Sopenharmony_ci/**
326195972f6Sopenharmony_ci* @ingroup netifapi_netif
327195972f6Sopenharmony_ci* Call netif_name_to_index() in a thread-safe way by running that function inside the
328195972f6Sopenharmony_ci* tcpip_thread context.
329195972f6Sopenharmony_ci*
330195972f6Sopenharmony_ci* @param name the interface name of the netif
331195972f6Sopenharmony_ci* @param idx output index of the found netif
332195972f6Sopenharmony_ci*/
333195972f6Sopenharmony_cierr_t
334195972f6Sopenharmony_cinetifapi_netif_name_to_index(const char *name, u8_t *idx)
335195972f6Sopenharmony_ci{
336195972f6Sopenharmony_ci  err_t err;
337195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
338195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
339195972f6Sopenharmony_ci
340195972f6Sopenharmony_ci  *idx = 0;
341195972f6Sopenharmony_ci
342195972f6Sopenharmony_ci#if LWIP_MPU_COMPATIBLE
343195972f6Sopenharmony_ci  strncpy(NETIFAPI_VAR_REF(msg).msg.ifs.name, name, NETIF_NAMESIZE - 1);
344195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.ifs.name[NETIF_NAMESIZE - 1] = '\0';
345195972f6Sopenharmony_ci#else
346195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.ifs.name = LWIP_CONST_CAST(char *, name);
347195972f6Sopenharmony_ci#endif /* LWIP_MPU_COMPATIBLE */
348195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_name_to_index, &API_VAR_REF(msg).call);
349195972f6Sopenharmony_ci  if (!err) {
350195972f6Sopenharmony_ci    *idx = NETIFAPI_VAR_REF(msg).msg.ifs.index;
351195972f6Sopenharmony_ci  }
352195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
353195972f6Sopenharmony_ci  return err;
354195972f6Sopenharmony_ci}
355195972f6Sopenharmony_ci
356195972f6Sopenharmony_ci/**
357195972f6Sopenharmony_ci* @ingroup netifapi_netif
358195972f6Sopenharmony_ci* Call netif_index_to_name() in a thread-safe way by running that function inside the
359195972f6Sopenharmony_ci* tcpip_thread context.
360195972f6Sopenharmony_ci*
361195972f6Sopenharmony_ci* @param idx the interface index of the netif
362195972f6Sopenharmony_ci* @param name output name of the found netif, empty '\0' string if netif not found.
363195972f6Sopenharmony_ci*             name should be of at least NETIF_NAMESIZE bytes
364195972f6Sopenharmony_ci*/
365195972f6Sopenharmony_cierr_t
366195972f6Sopenharmony_cinetifapi_netif_index_to_name(u8_t idx, char *name)
367195972f6Sopenharmony_ci{
368195972f6Sopenharmony_ci  err_t err;
369195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
370195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
371195972f6Sopenharmony_ci
372195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.ifs.index = idx;
373195972f6Sopenharmony_ci#if !LWIP_MPU_COMPATIBLE
374195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.ifs.name = name;
375195972f6Sopenharmony_ci#endif /* LWIP_MPU_COMPATIBLE */
376195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_index_to_name, &API_VAR_REF(msg).call);
377195972f6Sopenharmony_ci#if LWIP_MPU_COMPATIBLE
378195972f6Sopenharmony_ci  if (!err) {
379195972f6Sopenharmony_ci    strncpy(name, NETIFAPI_VAR_REF(msg).msg.ifs.name, NETIF_NAMESIZE - 1);
380195972f6Sopenharmony_ci    name[NETIF_NAMESIZE - 1] = '\0';
381195972f6Sopenharmony_ci  }
382195972f6Sopenharmony_ci#endif /* LWIP_MPU_COMPATIBLE */
383195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
384195972f6Sopenharmony_ci  return err;
385195972f6Sopenharmony_ci}
386195972f6Sopenharmony_ci
387195972f6Sopenharmony_ci#if LWIP_LOWPOWER
388195972f6Sopenharmony_cistatic err_t
389195972f6Sopenharmony_cinetifapi_do_set_lowpower_mod(struct tcpip_api_call_data *m)
390195972f6Sopenharmony_ci{
391195972f6Sopenharmony_ci  struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m;
392195972f6Sopenharmony_ci  enum lowpower_mod mod = msg->msg.lp.mod;
393195972f6Sopenharmony_ci  set_lowpower_mod(mod);
394195972f6Sopenharmony_ci  return ERR_OK;
395195972f6Sopenharmony_ci}
396195972f6Sopenharmony_ci
397195972f6Sopenharmony_cierr_t
398195972f6Sopenharmony_cinetifapi_enable_lowpower(void)
399195972f6Sopenharmony_ci{
400195972f6Sopenharmony_ci  err_t err;
401195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
402195972f6Sopenharmony_ci
403195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
404195972f6Sopenharmony_ci
405195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.lp.mod = LOW_TMR_LOWPOWER_MOD;
406195972f6Sopenharmony_ci
407195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_set_lowpower_mod, &API_VAR_REF(msg).call);
408195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
409195972f6Sopenharmony_ci  return err;
410195972f6Sopenharmony_ci}
411195972f6Sopenharmony_ci
412195972f6Sopenharmony_cierr_t
413195972f6Sopenharmony_cinetifapi_disable_lowpower(void)
414195972f6Sopenharmony_ci{
415195972f6Sopenharmony_ci  err_t err;
416195972f6Sopenharmony_ci  NETIFAPI_VAR_DECLARE(msg);
417195972f6Sopenharmony_ci
418195972f6Sopenharmony_ci  NETIFAPI_VAR_ALLOC(msg);
419195972f6Sopenharmony_ci
420195972f6Sopenharmony_ci  NETIFAPI_VAR_REF(msg).msg.lp.mod = LOW_TMR_NORMAL_MOD;
421195972f6Sopenharmony_ci
422195972f6Sopenharmony_ci  err = tcpip_api_call(netifapi_do_set_lowpower_mod, &API_VAR_REF(msg).call);
423195972f6Sopenharmony_ci  NETIFAPI_VAR_FREE(msg);
424195972f6Sopenharmony_ci  return err;
425195972f6Sopenharmony_ci}
426195972f6Sopenharmony_ci#endif /* LWIP_LOWPOWER */
427195972f6Sopenharmony_ci
428195972f6Sopenharmony_ci#endif /* LWIP_NETIF_API */
429