-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEOMtheEMSsocket.c
388 lines (279 loc) · 11.2 KB
/
EOMtheEMSsocket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
* Author: Marco Accame
* email: marco.accame@iit.it
* website: www.robotcub.org
* Permission is granted to copy, distribute, and/or modify this program
* under the terms of the GNU General Public License, version 2 or any
* later version published by the Free Software Foundation.
*
* A copy of the license can be found at
* http://www.robotcub.org/icub/license/gpl.txt
*
* 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
*/
// --------------------------------------------------------------------------------------------------------------------
// - external dependencies
// --------------------------------------------------------------------------------------------------------------------
#include "stdlib.h"
#include "string.h"
#include "EoCommon.h"
#include "EOtheMemoryPool.h"
#include "EOtheErrorManager.h"
#include "EOMtheIPnet.h"
#include "EOsocketDatagram.h"
#include "EOMmutex.h"
// --------------------------------------------------------------------------------------------------------------------
// - declaration of extern public interface
// --------------------------------------------------------------------------------------------------------------------
#include "EOMtheEMSsocket.h"
// --------------------------------------------------------------------------------------------------------------------
// - declaration of extern hidden interface
// --------------------------------------------------------------------------------------------------------------------
#include "EOMtheEMSsocket_hid.h"
// --------------------------------------------------------------------------------------------------------------------
// - #define with internal scope
// --------------------------------------------------------------------------------------------------------------------
// empty-section
// --------------------------------------------------------------------------------------------------------------------
// - definition (and initialisation) of extern variables, but better using _get(), _set()
// --------------------------------------------------------------------------------------------------------------------
const eOemssocket_cfg_t eom_emssocket_DefaultCfg =
{
EO_INIT(.inpdatagramnumber) 3,
EO_INIT(.outdatagramnumber) 2,
EO_INIT(.inpdatagramsizeof) 768,
EO_INIT(.outdatagramsizeof) 1024,
EO_INIT(.localport) 12345,
EO_INIT(.usemutex) eobool_true
};
// --------------------------------------------------------------------------------------------------------------------
// - typedef with internal scope
// --------------------------------------------------------------------------------------------------------------------
// empty-section
// --------------------------------------------------------------------------------------------------------------------
// - declaration of static functions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// - definition (and initialisation) of static variables
// --------------------------------------------------------------------------------------------------------------------
static const char s_eobj_ownname[] = "EOMtheEMSsocket";
static EOMtheEMSsocket s_emssocket_singleton =
{
EO_INIT(.socket) NULL,
EO_INIT(.cfg)
{
EO_INIT(.inpdatagramnumber) 3,
EO_INIT(.outdatagramnumber) 1,
EO_INIT(.inpdatagramsizeof) 768,
EO_INIT(.outdatagramsizeof) 1024,
EO_INIT(.localport) 12345,
EO_INIT(.usemutex) eobool_true
},
EO_INIT(.rxpkt) NULL,
EO_INIT(.txpkt) NULL,
EO_INIT(.hostaddress) EO_COMMON_IPV4ADDR_LOCALHOST,
EO_INIT(.connected2host) eobool_false,
EO_INIT(.active) eobool_false,
EO_INIT(.ontxrequest) NULL
};
// --------------------------------------------------------------------------------------------------------------------
// - definition of extern public functions
// --------------------------------------------------------------------------------------------------------------------
extern EOMtheEMSsocket * eom_emssocket_Initialise(const eOemssocket_cfg_t *cfg)
{
if(NULL != s_emssocket_singleton.socket)
{
return(&s_emssocket_singleton);
}
if(NULL == cfg)
{
cfg = &eom_emssocket_DefaultCfg;
}
memcpy(&s_emssocket_singleton.cfg, cfg, sizeof(eOemssocket_cfg_t));
if(NULL == eom_ipnet_GetHandle())
{
eo_errman_Error(eo_errman_GetHandle(), eo_errortype_fatal, "the EOMtheIPnet has not started yet", s_eobj_ownname, &eo_errman_DescrRuntimeErrorLocal);
}
s_emssocket_singleton.socket = eo_socketdtg_New( cfg->inpdatagramnumber, cfg->inpdatagramsizeof, (eobool_true == cfg->usemutex) ? (eom_mutex_New()) : (NULL),
cfg->outdatagramnumber, cfg->outdatagramsizeof, (eobool_true == cfg->usemutex) ? (eom_mutex_New()) : (NULL)
);
s_emssocket_singleton.rxpkt = eo_packet_New(cfg->inpdatagramsizeof);
//s_emssocket_singleton.txpkt = eo_packet_New(cfg->outdatagramsizeof);
s_emssocket_singleton.active = eobool_false;
s_emssocket_singleton.ontxrequest = eo_action_New();
eo_action_Clear(s_emssocket_singleton.ontxrequest);
return(&s_emssocket_singleton);
}
extern EOMtheEMSsocket* eom_emssocket_GetHandle(void)
{
if(NULL != s_emssocket_singleton.socket)
{
return(&s_emssocket_singleton);
}
else
{
return(NULL);
}
}
extern eOresult_t eom_emssocket_Open(EOMtheEMSsocket *p, EOaction* withactiononrx, EOaction* withactionontx, EOaction* ontxrequest)
{
eOresult_t res;
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
if(eobool_true == p->active)
{
res = eo_socketdtg_SetActions(p->socket, withactiononrx, withactionontx);
}
else
{
res = eo_socketdtg_Open(p->socket, p->cfg.localport, eo_sktdir_TXRX, eobool_false,
NULL, withactiononrx, withactionontx);
if(eores_OK == res)
{
p->active = eobool_true;
}
}
if(NULL == ontxrequest)
{
eo_action_Clear(p->ontxrequest);
}
else
{
eo_action_Copy(p->ontxrequest, ontxrequest);
}
return(res);
}
extern eOresult_t eom_emssocket_Close(EOMtheEMSsocket *p)
{
eOresult_t res;
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
if(eobool_false == p->active)
{
return(eores_OK);
}
res = eo_socketdtg_Close(p->socket);
p->active = eobool_false;
return(res);
}
extern eOsizecntnr_t eom_emssocket_NumberOfReceivedPackets(EOMtheEMSsocket *p)
{
eOsizecntnr_t numberof = 0;
if(NULL == p)
{
return(numberof);
}
if(eobool_true == p->active)
{
eo_socketdtg_Received_NumberOf(p->socket, &numberof);
}
return(numberof);
}
extern eOresult_t eom_emssocket_Receive(EOMtheEMSsocket *p, EOpacket** rxpkt, eOsizecntnr_t* remaining)
{
eOresult_t res;
if(NULL != remaining)
{
*remaining = 0;
}
if((NULL == p) || (NULL == rxpkt))
{
return(eores_NOK_nullpointer);
}
if(eobool_false == p->active)
{
return(eores_NOK_generic);
}
// eok_reltimeZERO is on read blocking mode ... it waits until a packet is received.
res = eo_socketdtg_Get(p->socket, p->rxpkt, eok_reltimeZERO);
if(eores_OK != res)
{
eo_packet_Size_Set(p->rxpkt, 0);
eo_socketdtg_Received_NumberOf(p->socket, remaining);
}
else
{
eo_socketdtg_Received_NumberOf(p->socket, remaining);
}
*rxpkt = p->rxpkt;
return(res);
}
extern eOresult_t eom_emssocket_Connect(EOMtheEMSsocket *p, eOipv4addr_t remaddr, eOreltime_t timeout)
{
eOresult_t res = eores_OK;
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
if((eobool_false == p->connected2host) || (remaddr != p->hostaddress))
{
p->connected2host = eobool_false;
p->hostaddress = remaddr;
res = eom_ipnet_ResolveIP(eom_ipnet_GetHandle(), remaddr, timeout);
if(eores_OK != res)
{
return(res);
}
p->connected2host = eobool_true;
}
return(eores_OK);
}
extern eOresult_t eom_emssocket_Transmit(EOMtheEMSsocket *p, EOpacket* txpkt, eOreltime_t timeout)
{
eOresult_t res;
eOipv4addr_t remaddr;
eOipv4port_t remport;
if((NULL == p) || (NULL == txpkt))
{
return(eores_NOK_nullpointer);
}
if(eobool_false == p->active)
{
return(eores_NOK_generic);
}
eo_packet_Addressing_Get(txpkt, &remaddr, &remport);
if((eobool_false == p->connected2host) || (remaddr != p->hostaddress))
{
res = eom_emssocket_Connect(p, remaddr, timeout);
if(eores_OK != res)
{
return(res);
}
}
res = eo_socketdtg_Put(p->socket, txpkt);
return(res);
}
extern eOresult_t eom_emssocket_TransmissionRequest(EOMtheEMSsocket *p)
{
eOresult_t res;
if(NULL == p)
{
return(eores_NOK_nullpointer);
}
if(eobool_false == p->active)
{
return(eores_NOK_generic);
}
if(eobool_true == eo_action_Isvalid(p->ontxrequest))
{
eo_action_Execute(p->ontxrequest, eok_reltimeINFINITE);
}
return(res);
}
// --------------------------------------------------------------------------------------------------------------------
// - definition of extern hidden functions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// - definition of static functions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// - end-of-file (leave a blank line after)
// --------------------------------------------------------------------------------------------------------------------