2014-04-07 05:09:21 +00:00
|
|
|
/*
|
2014-05-19 07:56:54 +00:00
|
|
|
Mesh Stream Protocol (MSP) API
|
2014-04-07 05:09:21 +00:00
|
|
|
Copyright (C) 2013-2014 Serval Project Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SERVAL_DNA__MSP_CLIENT_H
|
|
|
|
#define __SERVAL_DNA__MSP_CLIENT_H
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-23 08:19:00 +00:00
|
|
|
#include "constants.h" // for MDP_MTU
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
#ifndef __MSP_CLIENT_INLINE
|
|
|
|
# if __GNUC__ && !__GNUC_STDC_INLINE__
|
|
|
|
# define __MSP_CLIENT_INLINE extern inline
|
|
|
|
# else
|
|
|
|
# define __MSP_CLIENT_INLINE inline
|
|
|
|
# endif
|
|
|
|
#endif
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
typedef uint16_t msp_state_t;
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
struct msp_sock;
|
|
|
|
struct msp_handle {
|
|
|
|
struct msp_sock *ptr;
|
|
|
|
unsigned salt;
|
|
|
|
};
|
|
|
|
typedef struct msp_handle MSP_SOCKET;
|
|
|
|
#define MSP_SOCKET_NULL ((MSP_SOCKET){.ptr=NULL,.salt=0})
|
|
|
|
|
|
|
|
__MSP_CLIENT_INLINE int msp_socket_is_null(MSP_SOCKET sock) {
|
|
|
|
return sock.ptr == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msp_socket_is_valid(MSP_SOCKET);
|
|
|
|
|
|
|
|
// socket lifecycle
|
|
|
|
msp_state_t msp_get_state(MSP_SOCKET sock);
|
2016-03-15 00:18:10 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
int msp_socket_is_initialising(MSP_SOCKET);
|
|
|
|
int msp_socket_is_open(MSP_SOCKET);
|
|
|
|
int msp_socket_is_closed(MSP_SOCKET);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
int msp_socket_is_listening(MSP_SOCKET);
|
|
|
|
int msp_socket_is_data(MSP_SOCKET);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
int msp_socket_is_connected(MSP_SOCKET);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
int msp_socket_is_shutdown_local(MSP_SOCKET);
|
|
|
|
int msp_socket_is_shutdown_remote(MSP_SOCKET);
|
2013-12-19 04:15:58 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
unsigned msp_socket_count(void);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
// allocate a new socket
|
|
|
|
MSP_SOCKET msp_socket(int mdp_sock, int flags);
|
|
|
|
// initialise a socket
|
|
|
|
void msp_set_local(MSP_SOCKET sock, const struct mdp_sockaddr *local);
|
|
|
|
void msp_connect(MSP_SOCKET sock, const struct mdp_sockaddr *remote);
|
|
|
|
int msp_listen(MSP_SOCKET sock);
|
|
|
|
|
|
|
|
// close socket(s)
|
|
|
|
int msp_shutdown(MSP_SOCKET sock);
|
2014-05-21 07:40:00 +00:00
|
|
|
void msp_stop(MSP_SOCKET sock);
|
2014-05-07 05:33:44 +00:00
|
|
|
void msp_close_all(int mdp_sock);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
void msp_debug(void);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
typedef size_t MSP_HANDLER(MSP_SOCKET sock, msp_state_t state, const uint8_t *payload, size_t len, void *context);
|
|
|
|
void msp_set_handler(MSP_SOCKET sock, MSP_HANDLER *handler, void *context);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
int msp_get_mdp_socket(MSP_SOCKET); // returns arg passed to msp_socket() if MSP_SOCKET is valid
|
|
|
|
void msp_get_local(MSP_SOCKET sock, struct mdp_sockaddr *addr);
|
|
|
|
void msp_get_remote(MSP_SOCKET sock, struct mdp_sockaddr *addr);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2014-05-07 05:33:44 +00:00
|
|
|
// bind, send data, and potentially shutdown this end of the connection
|
|
|
|
ssize_t msp_send(MSP_SOCKET sock, const uint8_t *payload, size_t len);
|
2013-11-28 06:23:12 +00:00
|
|
|
// receive and process an incoming packet
|
|
|
|
int msp_recv(int mdp_sock);
|
|
|
|
// next_action indicates the next time that msp_processing should be called
|
|
|
|
int msp_processing(time_ms_t *next_action);
|
|
|
|
|
2014-04-07 05:09:21 +00:00
|
|
|
#endif //__SERVAL_DNA__MSP_CLIENT_H
|