2015-07-02 04:47:13 +08:00
|
|
|
/** @file
|
2015-10-21 00:42:33 +08:00
|
|
|
* @brief Simple socket API
|
|
|
|
*
|
|
|
|
* Simple socket API for applications to connection establishment and
|
|
|
|
* disconnection.
|
2015-04-23 16:20:42 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-04-23 16:20:42 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-23 16:20:42 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-23 16:20:42 +08:00
|
|
|
*/
|
|
|
|
|
2015-06-29 19:58:12 +08:00
|
|
|
#ifndef __NET_SOCKET_H
|
|
|
|
#define __NET_SOCKET_H
|
|
|
|
|
2015-04-23 16:20:42 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <net/net_ip.h>
|
2015-10-30 23:42:03 +08:00
|
|
|
#include <net/buf.h>
|
2015-04-23 16:20:42 +08:00
|
|
|
|
2016-01-23 01:38:49 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/**
|
2015-04-23 16:20:42 +08:00
|
|
|
* @brief Get network context.
|
|
|
|
*
|
|
|
|
* @details Network context is used to define the connection
|
|
|
|
* 5-tuple (protocol, remote address, remote port, source
|
|
|
|
* address and source port).
|
|
|
|
*
|
2016-02-12 02:25:27 +08:00
|
|
|
* @param ip_proto Protocol to use. Currently only UDP is supported.
|
2015-04-23 16:20:42 +08:00
|
|
|
* @param remote_addr Remote IPv6/IPv4 address.
|
|
|
|
* @param remote_port Remote UDP/TCP port.
|
2016-03-11 21:15:22 +08:00
|
|
|
* @param local_addr Local IPv6/IPv4 address. If the local address is
|
|
|
|
* set to be anyaddr (all zeros), the IP stack will use the link
|
2015-04-23 16:20:42 +08:00
|
|
|
* local address defined for the system.
|
|
|
|
* @param local_port Local UDP/TCP port. If the local port is 0,
|
|
|
|
* then a random port will be allocated.
|
|
|
|
*
|
|
|
|
* @return Network context if successful, NULL otherwise.
|
|
|
|
*/
|
|
|
|
struct net_context *net_context_get(enum ip_protocol ip_proto,
|
|
|
|
const struct net_addr *remote_addr,
|
|
|
|
uint16_t remote_port,
|
2016-03-11 21:15:22 +08:00
|
|
|
struct net_addr *local_addr,
|
2015-04-23 16:20:42 +08:00
|
|
|
uint16_t local_port);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/**
|
2015-04-23 16:20:42 +08:00
|
|
|
* @brief Get network tuple.
|
|
|
|
*
|
|
|
|
* @details This function returns the used connection tuple.
|
|
|
|
*
|
|
|
|
* @param context Network context.
|
|
|
|
*
|
|
|
|
* @return Network tuple if successful, NULL otherwise.
|
|
|
|
*/
|
|
|
|
struct net_tuple *net_context_get_tuple(struct net_context *context);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/**
|
2015-04-23 16:20:42 +08:00
|
|
|
* @brief Release network context.
|
|
|
|
*
|
|
|
|
* @details Free the resources allocated for the context.
|
|
|
|
* All network listeners tied to this context are removed.
|
|
|
|
*
|
|
|
|
* @param context Network context.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void net_context_put(struct net_context *context);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/**
|
2015-04-23 16:20:42 +08:00
|
|
|
* @brief Send data to network.
|
|
|
|
*
|
|
|
|
* @details Send user specified data to network. This
|
|
|
|
* requires that net_buf is tied to context. This means
|
|
|
|
* that the net_buf was allocated using net_buf_get().
|
|
|
|
*
|
|
|
|
* @param buf Network buffer.
|
|
|
|
*
|
|
|
|
* @return 0 if ok, <0 if error.
|
|
|
|
*/
|
|
|
|
int net_send(struct net_buf *buf);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/**
|
2015-04-23 16:20:42 +08:00
|
|
|
* @brief Receive data from network.
|
|
|
|
*
|
|
|
|
* @details Application uses this to get data from network
|
2015-08-03 15:40:08 +08:00
|
|
|
* connection. Caller can specify a timeout, if there is no
|
|
|
|
* data to return after a timeout, a NULL will be returned.
|
|
|
|
* Caller is responsible to release the returned net_buf.
|
2015-04-23 16:20:42 +08:00
|
|
|
*
|
|
|
|
* @param context Network context.
|
2015-07-31 20:50:42 +08:00
|
|
|
* @param timeout Timeout to wait. The value is in ticks.
|
|
|
|
* If TICKS_UNLIMITED (-1), wait forever.
|
|
|
|
* If TICKS_NONE (0), do not wait.
|
|
|
|
* If > 0, wait amount of ticks.
|
|
|
|
* The timeout is only available if kernel is compiled
|
|
|
|
* with CONFIG_NANO_TIMEOUTS. If CONFIG_NANO_TIMEOUT is not
|
2015-08-03 15:40:08 +08:00
|
|
|
* defined, then value > 0 means not to wait.
|
2015-04-23 16:20:42 +08:00
|
|
|
*
|
|
|
|
* @return Network buffer if successful, NULL otherwise.
|
|
|
|
*/
|
2015-07-31 20:50:42 +08:00
|
|
|
struct net_buf *net_receive(struct net_context *context,
|
|
|
|
int32_t timeout);
|
2015-04-23 16:20:42 +08:00
|
|
|
|
2015-08-12 19:09:30 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the UDP connection pointer from net_context.
|
|
|
|
*
|
|
|
|
* @details This is only needed in special occacions when
|
|
|
|
* there is an existing UDP connection that application
|
|
|
|
* wants to attach to. Normally the connection information
|
|
|
|
* is not needed by application.
|
|
|
|
*
|
|
|
|
* @return UDP connection if it exists, NULL otherwise.
|
|
|
|
*/
|
|
|
|
struct simple_udp_connection *
|
|
|
|
net_context_get_udp_connection(struct net_context *context);
|
|
|
|
|
2016-01-23 01:38:49 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-23 16:20:42 +08:00
|
|
|
#endif /* __NET_SOCKET_H */
|