nmxp_base.h 5.84 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.35 2009-08-31 12:16:41 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
30
31
32
33
34

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

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


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


/*! \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);


68
69
70
71
72
73
74
75
76
77
78
/*! \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);


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


87
88
89
90
91
/*! \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.
Matteo Quintiliani's avatar
Matteo Quintiliani committed
92
 * \param timeoutsec Time-out in seconds
Matteo Quintiliani's avatar
Matteo Quintiliani committed
93
 * \param[out] recv_errno errno value after recv()
94
95
96
97
98
99
100
 *
 * \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
 *
 */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
101
int nmxp_recv_ctrl(int isock, void *buffer, int length, int timeoutsec, int *recv_errno );
102
103
104
105
106
107
108
109
110
111
112
113


/*! \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
 *
 */
114
int nmxp_sendHeader(int isock, NMXP_MSG_CLIENT type, int32_t length);
115
116
117
118
119
120
121


/*! \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
122
123
 * \param timeoutsec Time-out in seconds
 * \param[out] recv_errno errno value after recv()
124
125
126
127
128
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
129
int nmxp_receiveHeader(int isock, NMXP_MSG_SERVER *type, int32_t *length, int timeoutsec, int *recv_errno );
130
131
132
133
134
135
136
137
138
139
140
141
142


/*! \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
 *
 */
143
int nmxp_sendMessage(int isock, NMXP_MSG_CLIENT type, void *buffer, int32_t length);
144
145
146
147
148
149


/*! \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.
150
 * \param buffer Pointer to the Data buffer.
151
 * \param[out] length Length in bytes.
Matteo Quintiliani's avatar
Matteo Quintiliani committed
152
153
 * \param timeoutsec Time-out in seconds
 * \param[out] recv_errno errno value after recv()
154
 * \param buffer_length Max length of Data buffer.
155
156
157
158
159
 *
 * \retval NMXP_SOCKET_OK on success
 * \retval NMXP_SOCKET_ERROR on error
 *
 */
160
int nmxp_receiveMessage(int isock, NMXP_MSG_SERVER *type, void *buffer, int32_t *length, int timeoutsec, int *recv_errno, int buffer_length);
161

162
163
164
165
166
167

/*! \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.
168
 * \param network_code_default Value of network code to assign returned structure. It should not be NULL.
169
170
171
172
 *
 * \return Return a pointer to static struct NMXP_DATA_PROCESS.
 *
 */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
173
NMXP_DATA_PROCESS *nmxp_processCompressedData(char* buffer_data, int length_data, NMXP_CHAN_LIST_NET *channelList, const char *network_code_default);
174
175
176
177
178
179
180


/*! \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.
181
 * \param network_code_default Value of network code to assign returned structure. It should not be NULL.
182
183
184
185
 *
 * \return Return a pointer to static struct NMXP_DATA_PROCESS.
 *
 */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
186
NMXP_DATA_PROCESS *nmxp_processDecompressedData(char* buffer_data, int length_data, NMXP_CHAN_LIST_NET *channelList, const char *network_code_default);
187

188
189
190
191
192
193
194
195
196
197

/*! \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
198
199
200
201
202
203
204
/*! \brief Wrapper for functions usleep on different platforms
 *
 *  \param usleep_time time in microseconds
 *
 *  \retval ....
 *
 */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
205
unsigned int nmxp_usleep(unsigned int usleep_time);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
206

207
208
#endif