-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyModulePyCommon.cpp
664 lines (598 loc) · 21.6 KB
/
PyModulePyCommon.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
* PyModulePyCommon.cpp
* PyRIDE
*
* Created by Xun Wang on 16/12/13.
* Copyright 2013 Galaxy Network. All rights reserved.
*
*/
#include <arpa/inet.h>
#ifdef DEFINE_COMMON_PYMODULE_METHODS
{ "declare", (PyCFunction)PyModule_declare, METH_NOARGS,
"Declare the robot to all available PyRIDE remote clients." },
{ "disconnect", (PyCFunction)PyModule_disconnect, METH_VARARGS,
"Disconnect PyRIDE one or all consoles." },
{ "takeCameraSnapshot", (PyCFunction)PyModule_TakeCameraSnapshot, METH_VARARGS,
"Use robot camera to take a snapshot." },
{ "getMyIPAddress", (PyCFunction)PyModule_GetMyIPAddress, METH_NOARGS,
"Get the current robot IP address." },
{ "updateOperationalStatus", (PyCFunction)PyModule_UpdateOperationalStatus, METH_VARARGS,
"Dispatch robot operational data to PyRIDE consoles." },
{ "updateRobotTelemetry", (PyCFunction)PyModule_UpdateRobotTelemetry, METH_VARARGS,
"Dispatch robot telemetry data." },
{ "listCurrentUsers", (PyCFunction)PyModule_ListCurrentUsers, METH_NOARGS,
"List all users who are currently log on the robot." },
{ "listAllUsers", (PyCFunction)PyModule_ListAllUsers, METH_NOARGS,
"List all users on the robot." },
{ "blockRemoteExclusiveControl", (PyCFunction)PyModule_BlockRemoteExclusiveControl, METH_VARARGS,
"Block or unblock remote client exclusive control of the robot." },
{ "saveConfiguration", (PyCFunction)PyModule_SaveConfiguration, METH_NOARGS,
"Save the current PyRIDE configuration." },
{ "addUser", (PyCFunction)PyModule_AddUser, METH_VARARGS,
"Add a new user on the robot." },
{ "removeUser", (PyCFunction)PyModule_RemoveUser, METH_VARARGS,
"Remove an existing user on the robot." },
{ "changeUserPassword", (PyCFunction)PyModule_ChangeUserPassword, METH_VARARGS,
"Change the password of an existing user on the robot." },
{ "addTimer", (PyCFunction)PyModule_AddTimer, METH_VARARGS,
"Add a new timer object." },
{ "isTimerRunning", (PyCFunction)PyModule_IsTimerRunning, METH_VARARGS,
"Check a timer with ID is still running." },
{ "isTimerExecuting", (PyCFunction)PyModule_IsTimerExecuting, METH_VARARGS,
"Check a timer with ID is still executing." },
{ "removeTimer", (PyCFunction)PyModule_RemoveTimer, METH_VARARGS,
"Remove a timer object with ID." },
{ "removeAllTimers", (PyCFunction)PyModule_RemoveAllTimers, METH_NOARGS,
"Remove all timer objects." },
{ "listActiveVideoObjects", (PyCFunction)PyModule_ActiveVideoObjects, METH_NOARGS,
"List the ID of all active video objects." },
{ "dispatchVideoTo", (PyCFunction)PyModule_DispatchVideoData, METH_VARARGS|METH_KEYWORDS,
"Send image data from a video object to a specific remote host." },
{ "dispatchAudioTo", (PyCFunction)PyModule_DispatchAudioData, METH_VARARGS|METH_KEYWORDS,
"Send image data from audio object to a specific remote host." },
#undef DEFINE_COMMON_PYMODULE_METHODS
#endif
#ifdef INCLUDE_COMMON_PYMODULE_MEHTODS
#ifndef PYRIDE_ROBOT_MODEL
#error You must define PYRIDE_ROBOT_MODEL macro first!
#endif
/*
* Common static methods in PyRIDE Python robot extension module
*
*/
/** @name Remote Client Access Functions
*
*/
/**@{*/
static PyObject * PyModule_declare( PyObject * self )
{
ServerDataProcessor::instance()->discoverConsoles();
Py_RETURN_NONE;
}
/*! \typedef disconnect( username )
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Disconnect all remote clients.
* \param str username. Optional name of an online user to be disconnected. If no username is provided. All connected clients will be disconnected.
* \return None
*/
static PyObject * PyModule_disconnect( PyObject * self, PyObject * args )
{
char * username = NULL;
if (!PyArg_ParseTuple( args, "|s", &username )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
SOCKET_T fd = INVALID_SOCKET;
if (username) {
if (AppConfigManager::instance()->getOnlineUserClientFD( username, fd )) {
ServerDataProcessor::instance()->disconnectConsole( fd );
}
else {
PyErr_Format( PyExc_ValueError, "Py%s.disconnect: user '%s' is not online!",
PYRIDE_ROBOT_MODEL, username );
return NULL;
}
}
else {
ServerDataProcessor::instance()->disconnectConsoles();
}
Py_RETURN_NONE;
}
/*! \typedef blockRemoteExclusiveControl( toblock )
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Block or unblock exclusive control of connected remote clients.
*
* Allow or block the exclusive control right of remote clients. If a client
* has already taken the exclusive control, the control right will be revoked
* when the input is set to True.
* \param bool toblock. True for block; False for unblock.
* \return None
*/
static PyObject * PyModule_BlockRemoteExclusiveControl( PyObject * self, PyObject * args )
{
PyObject * isYesObj = NULL;
if (!PyArg_ParseTuple( args, "O", &isYesObj )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (!PyBool_Check( isYesObj )) {
PyErr_Format( PyExc_ValueError, "Py%s.blockRemoteExclusiveControl: the parameter must be a boolean!", PYRIDE_ROBOT_MODEL );
return NULL;
}
ServerDataProcessor::instance()->blockRemoteExclusiveControl( PyObject_IsTrue( isYesObj ) );
Py_RETURN_NONE;
}
/*! \typedef updateOperationalStatus(op_state, op_text)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Send robot operational status notification to all connected remote clients.
* \param int op_state. Must be a positive integer representing the operational status.
* \param str op_text. Optional text for the operational status.
* \return None.
* \note For pre allocated operational status ID, check constants.py.
*/
/**@}*/
static PyObject * PyModule_UpdateOperationalStatus( PyObject * self, PyObject * args )
{
int state;
char * dataStr = NULL;
if (!PyArg_ParseTuple( args, "i|s", &state, &dataStr )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (state < 0 || state > 4) {
PyErr_Format( PyExc_ValueError, "Py%s.updateOperationalStatus: invalid status %d!", PYRIDE_ROBOT_MODEL,
state );
return NULL;
}
if (dataStr) {
ServerDataProcessor::instance()->updateOperationalStatus( (RobotOperationalState)state,
dataStr,
(int)strlen( dataStr ) );
}
else {
ServerDataProcessor::instance()->updateOperationalStatus( (RobotOperationalState)state );
}
Py_RETURN_NONE;
}
/** @name Miscellaneous Functions
*
*/
/**@{*/
/*! \typedef takeCameraSnapshot( all_camera )
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Take a snapshot for the robot camera(s)
* \param bool all_camera. Take a snapshot from all available cameras.
* \return None
* \note Image(s) taken from the robot camera(s) will be saved in <b>{robot home directory}/recordings/cameras</b>
* directory on the ROBOT_MODEL_DOXYGEN.
*/
static PyObject * PyModule_TakeCameraSnapshot( PyObject * self, PyObject * args )
{
PyObject * takeAllObj = NULL;
if (!PyArg_ParseTuple( args, "|O", &takeAllObj )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (takeAllObj && !PyBool_Check( takeAllObj )) {
PyErr_Format( PyExc_ValueError, "Py%s.takeCameraSnapshot: the parameter must be a boolean!", PYRIDE_ROBOT_MODEL );
return NULL;
}
ServerDataProcessor::instance()->takeCameraSnapshot( (takeAllObj && PyObject_IsTrue( takeAllObj )) );
Py_RETURN_NONE;
}
/*! \typedef getMyIPAddress()
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Get the IP address of the robot.
* \return The robot IP address in text.
*/
/**@}*/
static PyObject * PyModule_GetMyIPAddress( PyObject * self )
{
struct in_addr myaddr;
myaddr.s_addr = ntohl( ServerDataProcessor::instance()->getMyIPAddress() );
if (myaddr.s_addr) {
return Py_BuildValue( "s", inet_ntoa( myaddr ) );
}
else {
Py_RETURN_NONE;
}
}
static PyObject * PyModule_UpdateRobotTelemetry( PyObject * self, PyObject * args )
{
float pos_x, pos_y, pos_theta;
if (!PyArg_ParseTuple( args, "fff", &pos_x, &pos_y, &pos_theta )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
ServerDataProcessor::instance()->updateRobotTelemetry( pos_x, pos_y, pos_theta );
Py_RETURN_NONE;
}
/** @name Remote Client User Management Functions
*
*/
/**@{*/
/*! \typedef listCurrentUsers()
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Return a list of users who are currently logged in through remote clients.
*
* \return A list of user names.
*/
static PyObject * PyModule_ListCurrentUsers( PyObject * self )
{
std::vector<std::string> userList;
int nofusers = AppConfigManager::instance()->listCurrentUsers( userList );
PyObject * retList = PyList_New( nofusers );
PyObject * item = NULL;
for (int i = 0; i < nofusers; i++) {
item = Py_BuildValue( "s", userList[i].c_str() );
PyList_SetItem( retList, i, item );
}
return retList;
}
/*! \typedef listAllUsers()
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Return a list of users who can log in remotely.
*
* \return A list of user names.
*/
static PyObject * PyModule_ListAllUsers( PyObject * self )
{
std::vector<std::string> userList;
int nofusers = AppConfigManager::instance()->listAllUsers( userList );
PyObject * retList = PyList_New( nofusers );
PyObject * item = NULL;
for (int i = 0; i < nofusers; i++) {
item = Py_BuildValue( "s", userList[i].c_str() );
PyList_SetItem( retList, i, item );
}
return retList;
}
/*! \typedef saveConfiguration()
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Save the current user access configuration.
*
* \return None.
*/
static PyObject * PyModule_SaveConfiguration( PyObject * self )
{
AppConfigManager::instance()->saveConfig();
Py_RETURN_NONE;
}
/*! \typedef addUser(user_name, user_password)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Add a new remote user into the system.
* \param str user_name.
* \param str user_password.
* \return None.
*/
static PyObject * PyModule_AddUser( PyObject * self, PyObject * args )
{
char * nameStr = NULL;
char * passwordStr = NULL;
if (!PyArg_ParseTuple( args, "ss", &nameStr, &passwordStr )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (!AppConfigManager::instance()->addUser( nameStr, passwordStr )) {
PyErr_Format( PyExc_ValueError, "Py%s.addUser: invalid user name and password combination."
"Choose a different name or password, make sure password is longer than four characters.", PYRIDE_ROBOT_MODEL );
return NULL;
}
Py_RETURN_NONE;
}
/*! \typedef removeUser(user_name)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Remove an existing remote user from the system.
* \param str user_name name.
* \return None.
*/
static PyObject * PyModule_RemoveUser( PyObject * self, PyObject * args )
{
char * nameStr = NULL;
if (!PyArg_ParseTuple( args, "s", &nameStr )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (!AppConfigManager::instance()->delUser( nameStr )) {
PyErr_Format( PyExc_ValueError, "Py%s.delUser: user '%s' does not exist.", PYRIDE_ROBOT_MODEL, nameStr );
return NULL;
}
Py_RETURN_NONE;
}
/*! \typedef changeUserPassword(user_name, old_password, new_password)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Change an existing remote user's password.
* \param str user_name name.
* \param str old_password.
* \param str new_password.
* \return None.
*/
/**@}*/
static PyObject * PyModule_ChangeUserPassword( PyObject * self, PyObject * args )
{
char * nameStr = NULL;
char * newPasswordPtr = NULL;
char * oldPasswordPtr = NULL;
if (!PyArg_ParseTuple( args, "sss", &nameStr, &oldPasswordPtr, &newPasswordPtr )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (!AppConfigManager::instance()->changeUserPassword( nameStr, oldPasswordPtr, newPasswordPtr )) {
PyErr_Format( PyExc_ValueError, "Py%s.changeUserPassword: invalid user name and password combination."
"Make sure you have correct user name and old password, new password is longer than four characters.", PYRIDE_ROBOT_MODEL );
return NULL;
}
Py_RETURN_NONE;
}
/** @name Timer Management Functions
*
*/
/**@{*/
/*! \typedef addTimer(init_start, repeats, interval)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Add a timer object
* \param float init_start. Initial start time (in seconds, precision to 10th of a second) after the method is called.
* \param long repeats. A number of times the timer shall be called. -1 means infinite.
* \param float interval. Time interval (in seconds, precision to 10th of a second) between timer callbacks.
* \return long The ID of the timer object.
*/
static PyObject * PyModule_AddTimer( PyObject * self, PyObject * args )
{
float initTime;
long repeats = 0;
float interval = 1.0;
if (!PyArg_ParseTuple( args, "f|lf", &initTime, &repeats, &interval )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (initTime <= 0) {
PyErr_Format( PyExc_ValueError, "Py%s.addTimer: invalid initial invoking time."
"The first argument must be greater than zero.", PYRIDE_ROBOT_MODEL );
return NULL;
}
long timerID = ServerDataProcessor::instance()->addTimer( initTime, repeats, interval );
g_PyModuleTimerList.push_back( timerID );
return Py_BuildValue( "l", timerID );
}
/*! \typedef removeTimer(timer_id)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Remove a timer object.
* \param long timer_id. ID of the timer object pending for removal.
* \return None.
*/
static PyObject * PyModule_RemoveTimer( PyObject * self, PyObject * args )
{
long timerID;
if (!PyArg_ParseTuple( args, "l", &timerID )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (timerID <= 0) {
PyErr_Format( PyExc_ValueError, "Py%s.removeTimer: invalid timer ID."
"The ID must be greater than zero.", PYRIDE_ROBOT_MODEL );
return NULL;
}
bool found = false;
std::vector<long>::iterator iter;
for (iter = g_PyModuleTimerList.begin(); iter != g_PyModuleTimerList.end(); iter++) {
if (*iter == timerID) {
found = true;
break;
}
}
if (!found) {
PyErr_Format( PyExc_ValueError, "Py%s.removeTimer: unknown timer ID %d.", PYRIDE_ROBOT_MODEL, (int)timerID );
return NULL;
}
g_PyModuleTimerList.erase( iter );
ServerDataProcessor::instance()->delTimer( timerID );
Py_RETURN_NONE;
}
/*! \typedef isTimerRunning(timer_id)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Check whether a timer object is still alive.
* \param long timer_id. ID of the timer object.
* \return True = timer is alive; False = timer is dead.
*/
static PyObject * PyModule_IsTimerRunning( PyObject * self, PyObject * args )
{
long timerID;
if (!PyArg_ParseTuple( args, "l", &timerID )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (timerID <= 0) {
PyErr_Format( PyExc_ValueError, "Py%s.isTimerRunning: invalid timer ID."
"The ID must be greater than zero.", PYRIDE_ROBOT_MODEL );
return NULL;
}
if (ServerDataProcessor::instance()->isTimerRunning( timerID )) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}
/*! \typedef isTimerExecuting(timer_id)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Check whether a timer object is in execution mode.
* \param long timer_id. ID of the timer object.
* \return True = timer is executing code; False = timer is in hibernation.
*/
static PyObject * PyModule_IsTimerExecuting( PyObject * self, PyObject * args )
{
long timerID;
if (!PyArg_ParseTuple( args, "l", &timerID )) {
// PyArg_ParseTuple will set the error status.
return NULL;
}
if (timerID <= 0) {
PyErr_Format( PyExc_ValueError, "Py%s.isTimerExecuting: invalid timer ID."
"The ID must be greater than zero.", PYRIDE_ROBOT_MODEL );
return NULL;
}
if (ServerDataProcessor::instance()->isTimerExecuting( timerID )) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}
/*! \typedef removeAllTimers()
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Remove all timer objects in the system.
* \return None.
*/
/**@}*/
static PyObject * PyModule_RemoveAllTimers( PyObject * self )
{
for (size_t i = 0; i < g_PyModuleTimerList.size(); i++) {
ServerDataProcessor::instance()->delTimer( g_PyModuleTimerList[i] );
}
g_PyModuleTimerList.clear();
Py_RETURN_NONE;
}
static PyObject * PyModule_ActiveVideoObjects( PyObject * self )
{
std::vector<std::string> deviceList;
int nofdevice = ServerDataProcessor::instance()->activeVideoObjectList( deviceList );
PyObject * retList = PyList_New( nofdevice );
PyObject * item = NULL;
for (int i = 0; i < nofdevice; i++) {
item = Py_BuildValue( "s", deviceList[i].c_str() );
PyList_SetItem( retList, i, item );
}
return retList;
}
static const char *kDispatchVideoKWlist[] = { "devidx", "host", "port", "todispatch", NULL };
static const char *kDispatchAudioKWlist[] = { "host", "port", "todispatch", NULL };
static PyObject * PyModule_DispatchVideoData( PyObject * self, PyObject * args, PyObject * keywds )
{
int devid = -1;
char * hostname = NULL;
int port = 0;
PyObject * isYesObj = NULL;
if (!PyArg_ParseTupleAndKeywords( args, keywds, "isiO", (char**)kDispatchVideoKWlist, &devid, &hostname, &port, &isYesObj ) ||
!PyBool_Check( isYesObj )) { // PyArg_ParseTuple will set the error status.
PyErr_Format( PyExc_ValueError, "Py%s.dispatchVideoTo: invalid keyworded input parameters.", PYRIDE_ROBOT_MODEL );
return NULL;
}
if (devid < 0) {
PyErr_Format( PyExc_ValueError, "Py%s.dispatchVideoTo: selected device index must be a non-negative number.", PYRIDE_ROBOT_MODEL );
return NULL;
}
unsigned long saddr = 0;
struct hostent * hostInfo = gethostbyname( hostname ); // try resolve name first
if (!hostInfo) {
#ifdef WIN32
saddr = inet_addr( host );
if (saddr == INADDR_NONE) {
#else
if (inet_pton( AF_INET, hostname, &saddr ) != 1) {
#endif
PyErr_Format( PyExc_ValueError, "Py%s.dispatchVideoTo: unable to resove host %s.", PYRIDE_ROBOT_MODEL, hostname );
return NULL;
}
}
struct sockaddr_in cAddr;
cAddr.sin_family = AF_INET;
if (hostInfo) {
memcpy( (char *)&cAddr.sin_addr, hostInfo->h_addr, hostInfo->h_length );
}
else {
#ifdef WIN32
cAddr.sin_addr.s_addr = saddr;
#else
cAddr.sin_addr.s_addr = (in_addr_t)saddr;
#endif
}
if (port < 20000 || port > 55000) {
PyErr_Format( PyExc_ValueError, "Py%s.dispatchVideoTo: remote host port must be greater than 20000.", PYRIDE_ROBOT_MODEL );
return NULL;
}
if (ServerDataProcessor::instance()->dispatchVideoDataTo( devid, cAddr, (short)port, PyObject_IsTrue( isYesObj ) )) {
Py_RETURN_NONE;
}
else {
PyErr_Format( PyExc_SystemError, "Py%s.dispatchVideoTo: %s dispatching video data to %s failed.",
PYRIDE_ROBOT_MODEL, PyObject_IsTrue( isYesObj ) ? "start" : "stop", hostname );
return NULL;
}
}
static PyObject * PyModule_DispatchAudioData( PyObject * self, PyObject * args, PyObject * keywds )
{
char * hostname = NULL;
int port = 0;
PyObject * isYesObj = NULL;
if (!PyArg_ParseTupleAndKeywords( args, keywds, "siO", (char**)kDispatchAudioKWlist, &hostname, &port, &isYesObj ) ||
!PyBool_Check( isYesObj )) { // PyArg_ParseTuple will set the error status.
PyErr_Format( PyExc_ValueError, "Py%s.dispatchAudioTo: invalid keyworded input parameters.", PYRIDE_ROBOT_MODEL );
return NULL;
}
unsigned long saddr = 0;
struct hostent * hostInfo = gethostbyname( hostname ); // try resolve name first
if (!hostInfo) {
#ifdef WIN32
saddr = inet_addr( host );
if (saddr == INADDR_NONE) {
#else
if (inet_pton( AF_INET, hostname, &saddr ) != 1) {
#endif
PyErr_Format( PyExc_ValueError, "Py%s.dispatchAudioTo: unable to resolve host %s.", PYRIDE_ROBOT_MODEL, hostname );
return NULL;
}
}
struct sockaddr_in cAddr;
cAddr.sin_family = AF_INET;
if (hostInfo) {
memcpy( (char *)&cAddr.sin_addr, hostInfo->h_addr, hostInfo->h_length );
}
else {
#ifdef WIN32
cAddr.sin_addr.s_addr = saddr;
#else
cAddr.sin_addr.s_addr = (in_addr_t)saddr;
#endif
}
if (port < 20000 || port > 55000) {
PyErr_Format( PyExc_ValueError, "Py%s.dispatchAudioTo: remote host port must be greater than 20000.", PYRIDE_ROBOT_MODEL );
return NULL;
}
if (ServerDataProcessor::instance()->dispatchAudioDataTo( cAddr, (short)port, PyObject_IsTrue( isYesObj ) )) {
Py_RETURN_NONE;
}
else {
PyErr_Format( PyExc_SystemError, "Py%s.dispatchAudioTo: %s dispatching video data to %s failed.",
PYRIDE_ROBOT_MODEL, PyObject_IsTrue( isYesObj ) ? "start" : "stop", hostname );
return NULL;
}
}
/*! \typedef setTeamMemberID(member_id, team_id)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Sets the ID number of the robot.
*
* The robot can be assigned as a member
* of blue team (number 1) or pink team (number 2). This facilitates robot to
* robot communication.
* \note This function is inherited from robot soccer competition
* code for NAO.
* \param int member_id. Must be a non-negative integer.
* \param int team_id. Must be either 1 (blue team) or 2 (pink team).
* \return None.
*/
/*! \typedef say(text)
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Use Text-to-Speech system to say the input text.
* \param str text. Text to be spoken by the robot.
* \return None.
*/
/*! \typedef sendTeamMessage( message )
* \memberof ROBOT_MODEL_DOXYGEN.
* \brief Broadcast a message to other robots in the same team (colour).
*
* \param str message. A text based message.
* \return None
* \note (legacy) This is based on the team colour assignment defined in pyrideconfig.xml
*/
#undef INCLUDE_COMMON_PYMODULE_MEHTODS
#endif