nmxp_base.h 6.21 KB
Newer Older
1 2
/*! \file
 *
Matteo Quintiliani's avatar
Matteo Quintiliani committed
3
 * \brief Base for Nanometrics Protocol Library
4 5 6 7 8 9
 *
 * Author:
 * 	Matteo Quintiliani
 * 	Istituto Nazionale di Geofisica e Vulcanologia - Italy
 *	quintiliani@ingv.it
 *
10
 * $Id: nmxp_base.h,v 1.36 2010-09-15 13:14:33 mtheo Exp $
Matteo Quintiliani's avatar
Matteo Quintiliani committed
11
 *
12 13 14 15 16
 */

#ifndef NMXP_BASE_H
#define NMXP_BASE_H 1

17
#include "nmxp_data.h"
18
#include "nmxp_chan.h"
19
#include "nmxp_log.h"
20

21
/*! Maximum time between connection attempts (seconds). */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
22
#define NMXP_SLEEPMAX 60
23 24 25 26 27 28 29

/*! Return message for succes on socket. */
#define NMXP_SOCKET_OK          0

/*! Return message for error on socket.*/
#define NMXP_SOCKET_ERROR      -1

30 31
/*! Maximum time out for receiving data (seconds). */
#define NMXP_HIGHEST_TIMEOUT 30
32 33 34 35 36

/*! \brief Looks up target host, opens a socket and connects
 *
 *  \param hostname	hostname
 *  \param portNum	port number
37
 *  \param func_cond Pointer to function for exit condition from loop.
38 39 40 41 42
 *
 *  \retval sd A descriptor referencing the socket.
 *  \retval -1 "Empty host name", "Cannot lookup host", ...
 *
 */
43
int nmxp_openSocket(char *hostname, int portNum, int (*func_cond)(void));
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69


/*! \brief Close a socket.
 *
 *  \param isock  A descriptor referencing the socket.
 *
 *  \retval  0 Success
 *  \retval -1 Error
 *
 */
int nmxp_closeSocket(int isock);


/*! \brief Sends a buffer on a socket.
 *
 * \param isock A descriptor referencing the socket.
 * \param buffer Data buffer.
 * \param length Length in bytes.
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
int nmxp_send_ctrl(int isock, void *buffer, int length);


70 71 72 73 74 75 76 77 78 79 80
/*! \brief Receives length bytes in a buffer from a socket.
 *
 * \param isock A descriptor referencing the socket.
 * \param timeoutsec Time-out in seconds
 *
 * \return getsockopt() return value
 *
 */
int nmxp_setsockopt_RCVTIMEO(int isock, int timeoutsec);


81 82
/*! \brief Wrapper to strerror, strerror_r or WSAGetLastErrorMessage
 *
83
 * \return String message of errno_value. It is not static, need to be freed.
84 85 86 87 88
 *
 */
char *nmxp_strerror(int errno_value);


89 90 91 92 93
/*! \brief Receives length bytes in a buffer from a socket.
 *
 * \param isock A descriptor referencing the socket.
 * \param[out] buffer Data buffer.
 * \param length Length in bytes.
94
 * \param timeoutsec Time-out in seconds
Matteo Quintiliani's avatar
Matteo Quintiliani committed
95
 * \param[out] recv_errno errno value after recv()
96 97 98 99 100 101 102
 *
 * \warning Data buffer it has to be allocated before and big enough to contain length bytes!
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
103
int nmxp_recv_ctrl(int isock, void *buffer, int length, int timeoutsec, int *recv_errno );
104 105 106 107 108 109 110 111 112 113 114 115


/*! \brief Sends header of a message.
 *
 * \param isock A descriptor referencing the socket.
 * \param type Type of message within \ref NMXP_MSG_CLIENT.
 * \param length Length in bytes.
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
116
int nmxp_sendHeader(int isock, NMXP_MSG_CLIENT type, int32_t length);
117 118 119 120 121 122 123


/*! \brief Receives header of a message.
 * 
 * \param isock A descriptor referencing the socket.
 * \param[out] type Type of message within \ref NMXP_MSG_CLIENT.
 * \param[out] length Length in bytes.
Matteo Quintiliani's avatar
Matteo Quintiliani committed
124 125
 * \param timeoutsec Time-out in seconds
 * \param[out] recv_errno errno value after recv()
126 127 128 129 130
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
131
int nmxp_receiveHeader(int isock, NMXP_MSG_SERVER *type, int32_t *length, int timeoutsec, int *recv_errno );
132 133 134 135 136 137 138 139 140 141 142 143 144


/*! \brief Sends header and body of a message.
 *
 * \param isock A descriptor referencing the socket.
 * \param type Type of message within \ref NMXP_MSG_CLIENT.
 * \param buffer Data buffer. It could be NULL.
 * \param length Length in bytes. It must be greater or equal to zero.
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
145
int nmxp_sendMessage(int isock, NMXP_MSG_CLIENT type, void *buffer, int32_t length);
146 147 148 149 150 151


/*! \brief Receives header and body of a message.
 *
 * \param isock A descriptor referencing the socket.
 * \param[out] type Type of message within \ref NMXP_MSG_SERVER.
152
 * \param buffer Pointer to the Data buffer.
153
 * \param[out] length Length in bytes.
Matteo Quintiliani's avatar
Matteo Quintiliani committed
154 155
 * \param timeoutsec Time-out in seconds
 * \param[out] recv_errno errno value after recv()
156
 * \param buffer_length Max length of Data buffer.
157 158 159 160 161
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
162
int nmxp_receiveMessage(int isock, NMXP_MSG_SERVER *type, void *buffer, int32_t *length, int timeoutsec, int *recv_errno, int buffer_length);
163

164 165 166 167 168 169

/*! \brief Process Compressed Data message by function func_processData().
 *
 * \param buffer_data Pointer to the data buffer containing Compressed Nanometrics packets.
 * \param length_data Buffer length in bytes.
 * \param channelList Pointer to the Channel List.
170
 * \param network_code_default Value of network code to assign returned structure. It should not be NULL.
171
 * \param location_code_default Value of location code to assign returned structure. It should not be NULL.
172 173 174 175
 *
 * \return Return a pointer to static struct NMXP_DATA_PROCESS.
 *
 */
176
NMXP_DATA_PROCESS *nmxp_processCompressedData(char* buffer_data, int length_data, NMXP_CHAN_LIST_NET *channelList, const char *network_code_default, const char *location_code_default);
177 178 179 180 181 182 183


/*! \brief Process decompressed Data message by function func_processData().
 *
 * \param buffer_data Pointer to the data buffer containing Decompressed Nanometrics packets.
 * \param length_data Buffer length in bytes.
 * \param channelList Pointer to the Channel List.
184
 * \param network_code_default Value of network code to assign returned structure. It should not be NULL.
185
 * \param location_code_default Value of location code to assign returned structure. It should not be NULL.
186 187 188 189
 *
 * \return Return a pointer to static struct NMXP_DATA_PROCESS.
 *
 */
190
NMXP_DATA_PROCESS *nmxp_processDecompressedData(char* buffer_data, int length_data, NMXP_CHAN_LIST_NET *channelList, const char *network_code_default, const char *location_code_default);
191

192 193 194 195 196 197 198 199 200 201

/*! \brief Wrapper for functions sleep on different platforms
 *
 *  \param sleep_time time in seconds
 *
 *  \retval ....
 *
 */
unsigned int nmxp_sleep(unsigned int sleep_time);

Matteo Quintiliani's avatar
Matteo Quintiliani committed
202 203 204 205 206 207 208
/*! \brief Wrapper for functions usleep on different platforms
 *
 *  \param usleep_time time in microseconds
 *
 *  \retval ....
 *
 */
209
unsigned int nmxp_usleep(unsigned int usleep_time);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
210

211 212
#endif