-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
600 lines (522 loc) · 12.7 KB
/
main.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
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
/* oscchief - Sends and receives OSC messages.
*
* Copyright (c) 2013 Sebastian Ruml
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <errno.h>
#include <stdbool.h>
#include <lo/lo.h>
#define VERSION "0.2.0"
/**
* Prints the usage text.
*/
void usage()
{
printf("oscchief Version %s\n", VERSION);
printf("Copyright (C) 2013 Sebastian Ruml <sebastian.ruml@gmail.com>\n\n");
printf("usage: oscchief send HOST PORT OSCADDRESS TYPES ARGUMENTS\n");
printf(" oscchief send FILENAME\n");
printf(" oscchief listen PORT\n\n");
printf("positional arguments:\n");
printf(" HOST: IP address of the host where you want to send your OSC message\n");
printf(" PORT: Port number\n");
printf(" OSCADDRESS: OSC address where you want to send your message\n");
printf(" TYPES: OSC type tags. Supported types:\n");
printf(" %c - 32 Bit integer\n", LO_INT32);
printf(" %c - 64 Bit integer\n", LO_INT64);
printf(" %c - 32 Bit float\n", LO_FLOAT);
printf(" %c - 64 Bit double\n", LO_DOUBLE);
printf(" %c - Char\n", LO_CHAR);
printf(" %c - String\n", LO_STRING);
printf(" %c - True (no argument required)\n", LO_TRUE);
printf(" %c - False (no argument required)\n", LO_FALSE);
printf(" %c - Nil (no argument required)\n\n", LO_NIL);
printf("optional arguments:\n");
printf(" -h - Shows this help message\n\n");
printf("Examples:\n");
printf(" oscchief send 192.168.0.10 7028 /osc/address %c%c%c%c%c some integers 10 12 786\n",
LO_STRING, LO_STRING, LO_INT32, LO_INT32, LO_INT32);
printf(" oscchief send 192.168.0.10 7028 /osc/address %c%c%c%c%c 643 98\n",
LO_TRUE, LO_TRUE, LO_INT32, LO_FALSE, LO_INT32);
printf(" oscchief send 192.168.0.10 7028 /osc/address\n");
printf(" oscchief send 192.168.0.10 7028 /osc/address %c%c\n", LO_TRUE, LO_FALSE);
printf(" oscchief listen 7028\n");
}
void errorHandler(int num, const char *msg, const char *where)
{
printf("Server error %d in address %s: %s\n", num, msg, where);
}
/**
* Message handler for incoming OSC messages.
*/
int messageHandler(const char *address, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data)
{
printf("%s %s", address, types);
for (int i = 0; i < argc; i++)
{
printf(" ");
lo_arg_pp((lo_type)types[i], argv[i]);
}
printf("\n");
return 0;
}
/**
* Creates a new OSC message from the arguments and type tags.
*
* \returns The created OSC message.
*/
lo_message create_message(char **argv)
{
char const *types;
lo_message message;
// Arg 2: Hostname
// Arg 3: OSC Port
// Arg 4: OSC Address
// Arg 5: Type Tags
// Arg 6: Arguments
// Build a new message
message = lo_message_new();
// Get the length of the type tags
int numberOfArgs;
if (argv[5] == NULL)
{
// An empty type string is allowed
numberOfArgs = 0;
}
else
{
types = argv[5];
numberOfArgs = strlen(types);
}
// Handle every argument
int index = 6;
char const *arg;
for (int i = 0; i < numberOfArgs; i++)
{
if (types[i] != LO_FALSE && types[i] != LO_TRUE
&& types[i] != LO_NIL)
{
arg = argv[index];
if (arg == NULL)
{
fprintf(stderr, "Argument %d not found.\n", i + 1);
goto EXIT;
}
}
switch(types[i])
{
case LO_INT32:
{
int64_t val;
char *endptr;
errno = 0;
val = strtol(arg, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0))
{
fprintf(stderr, "Could not parse argument %d '%s' as INT32.\n", i+1, arg);
goto EXIT;
}
if (val > INT_MAX || val < INT_MIN)
{
fprintf(stderr, "Could not parse argument %d '%s' as INT32.\n", i+1, arg);
goto EXIT;
}
if (endptr == arg)
{
fprintf(stderr, "Could not parse argument %d '%s' as INT32.\n", i+1, arg);
goto EXIT;
}
if (lo_message_add_int32(message, (int32_t)val) < 0)
{
fprintf(stderr, "Could not add argument %d '%s' to the OSC message.\n", i+1, arg);
goto EXIT;
}
index++;
}
break;
case LO_INT64:
{
int64_t val;
char *endptr;
errno = 0;
val = strtol(arg, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0))
{
fprintf(stderr, "Could not parse argument %d '%s' as INT64.\n", i+1, arg);
goto EXIT;
}
if (val > LONG_MAX || val < LONG_MIN)
{
fprintf(stderr, "Could not parse argument %d '%s' as INT64.\n", i+1, arg);
goto EXIT;
}
if (endptr == arg)
{
fprintf(stderr, "Could not parse argument %d '%s' as INT64.\n", i+1, arg);
goto EXIT;
}
if (lo_message_add_int64(message, val) < 0)
{
fprintf(stderr, "Could not add argument %d '%s' to the OSC message.\n", i+1, arg);
goto EXIT;
}
index++;
}
break;
case LO_FLOAT:
{
float val;
char *endptr;
errno = 0;
val = strtof(arg, &endptr);
if ((errno == ERANGE && val == HUGE_VALF)
|| (errno == ERANGE && val == 0))
{
fprintf(stderr, "Could not parse argument %d '%s' as FLOAT.\n", i+1, arg);
goto EXIT;
}
if (endptr == arg)
{
fprintf(stderr, "Could not parse argument %d '%s' as FLOAT.\n", i+1, arg);
goto EXIT;
}
if (lo_message_add_float(message, val) < 0)
{
fprintf(stderr, "Could not add argument %d '%s' to the OSC message.\n", i+1, arg);
goto EXIT;
}
index++;
}
break;
case LO_DOUBLE:
{
double val;
char *endptr;
errno = 0;
val = strtod(arg, &endptr);
if ((errno == ERANGE && val == HUGE_VAL)
|| (errno == ERANGE && val == 0))
{
fprintf(stderr, "Could not parse argument %d '%s' as DOUBLE.\n", i+1, arg);
goto EXIT;
}
if (endptr == arg)
{
fprintf(stderr, "Could not parse argument %d '%s' as DOUBLE.\n", i+1, arg);
goto EXIT;
}
if (lo_message_add_double(message, val) < 0)
{
fprintf(stderr, "Could not add argument %d '%s' to the OSC message.\n", i+1, arg);
goto EXIT;
}
index++;
}
break;
case LO_CHAR:
{
if (strlen(arg) != 1)
{
fprintf(stderr, "Only single byte char is allowed.\n");
goto EXIT;
}
if (lo_message_add_char(message, arg[0]) < 0)
{
fprintf(stderr, "Could not add argument %d '%s' to the OSC message.\n", i+1, arg);
goto EXIT;
}
index++;
}
break;
case LO_STRING:
{
if (lo_message_add_string(message, arg) < 0)
{
fprintf(stderr, "Could not add argument %d '%s' to the OSC message.\n", i+1, arg);
goto EXIT;
}
}
index++;
break;
case LO_TRUE:
{
if (lo_message_add_true(message) < 0)
{
fprintf(stderr, "Could not add argument %d to the OSC message.\n", i+1);
goto EXIT;
}
}
break;
case LO_FALSE:
{
if (lo_message_add_false(message) < 0)
{
fprintf(stderr, "Could not add argument %d to the OSC message.\n", i+1);
goto EXIT;
}
}
break;
case LO_NIL:
{
if (lo_message_add_nil(message) < 0)
{
fprintf(stderr, "Could not add argument %d to the OSC message.\n", i+1);
goto EXIT;
}
}
break;
default:
fprintf(stderr, "Type '%c' is not supported.\n", types[i]);
goto EXIT;
}
}
return message;
EXIT:
lo_message_free(message);
return NULL;
}
/**
* Sends the OSC messages specified in the given file.
*
* \returns 0, if success. Otherwise, -1.
*/
int process_message_file(char *filename)
{
if (filename == NULL)
{
return -1;
}
FILE *file;
char line_buffer[BUFSIZ];
file = fopen(filename, "r");
if (!file)
{
fprintf(stderr, "Couldn't open file %s for reading.\n", filename);
return -1;
}
// Process every line
while (fgets(line_buffer, sizeof(line_buffer), file))
{
char *argv[100];
// Fill the first two arguments with empty strings, so we can use the
// create_message() function
argv[0] = "";
argv[1] = "";
int i = 2;
char *part = strtok(line_buffer, " ");
while (part != NULL)
{
// TODO: Check if pause is specified
// Remove line endings if any
int len = strlen(part);
if (part[len-1] == '\n' || part[len-1] == '\r')
{
part[len-1] = '\0';
}
argv[i] = part;
// Get the next part
part = strtok(NULL, " ");
i++;
}
// Check if we got enough arguments
if (i < 3)
{
fprintf(stderr, "Error parsing file.\n");
return -1;
}
if (argv[2] == NULL)
{
fprintf(stderr, "No hostname is given.\n");
exit(1);
}
if (argv[3] == NULL)
{
fprintf(stderr, "No port is specified.\n");
exit(1);
}
// Create a new address
lo_address target = lo_address_new(argv[2], argv[3]);
if (target == NULL)
{
fprintf(stderr, "Failed to create %s:%s\n", argv[2], argv[3]);
exit(1);
}
if (argv[4] == NULL)
{
fprintf(stderr, "No OSC address is given\n");
exit(1);
}
printf("%s %s %s", argv[2], argv[3], argv[4]);
char *types = argv[5];
int argc = strlen(types);
for (int i = 0; i < argc; i++)
{
printf(" ");
if (types[i] == LO_TRUE)
{
printf("TRUE");
}
else if (types[i] == LO_FALSE)
{
printf("FALSE");
}
else if (types[i] == LO_NIL)
{
printf("NIL");
}
else
{
printf("%s", argv[i+6]);
}
}
printf("\n");
lo_message message = create_message(argv);
if (message == NULL)
{
fprintf(stderr, "Could not create OSC message.\n");
exit(1);
}
int ret = lo_send_message(target, argv[4], message);
if (ret <= -1)
{
fprintf(stderr, "Error sending message.\n");
exit(1);
}
}
return 0;
}
int main(int argc, char **argv)
{
bool runAsServer = false;
if (argc < 2)
{
usage();
exit(1);
}
// Handle optional arguments
char *firstArg = argv[1];
if (strcmp(firstArg, "-h") == 0)
{
usage();
exit(1);
}
else if (strcmp(firstArg, "send") == 0)
{
runAsServer = false;
}
else if (strcmp(firstArg, "listen") == 0)
{
runAsServer = true;
}
else
{
printf("Invalid argument(s)!\n");
usage();
exit(1);
}
// Run as server and listen for incoming OSC messages
if (runAsServer)
{
if (argc < 3)
{
usage();
exit(1);
}
// Create new server
lo_server server = lo_server_new(argv[2], errorHandler);
if (server == NULL)
{
printf("Could not start a server with port %s", argv[3]);
exit(1);
}
// Add a message handler that matches all OSC addresses
lo_server_add_method(server, NULL, NULL, messageHandler, NULL);
printf("Start listening on port '%s' for incoming OSC messages...\n", argv[2]);
// Loop forever and receive OSC messages
for (;;)
{
lo_server_recv(server);
}
}
else // Run as client
{
if (argc != 3 && argc < 5)
{
usage();
exit(1);
}
// Read the file and send the specified OSC messages
if (argc == 3)
{
printf("Reading messages from \"%s\"\n", argv[2]);
int ret = process_message_file(argv[2]);
if (ret < 0)
{
fprintf(stderr, "Error processing message file.\n");
exit(1);
}
}
else if (argc >= 5)
{
if (argv[2] == NULL)
{
fprintf(stderr, "No hostname is given.\n");
exit(1);
}
if (argv[3] == NULL)
{
fprintf(stderr, "No port is specified.\n");
exit(1);
}
// Create a new address
lo_address target = lo_address_new(argv[2], argv[3]);
if (target == NULL)
{
fprintf(stderr, "Failed to create %s:%s\n", argv[2], argv[3]);
exit(1);
}
if (argv[4] == NULL)
{
fprintf(stderr, "No OSC address is given\n");
exit(1);
}
lo_message message = create_message(argv);
if (message == NULL)
{
fprintf(stderr, "Could not create OSC message.\n");
exit(1);
}
int ret = lo_send_message(target, argv[4], message);
if (ret <= -1)
{
fprintf(stderr, "Error sending message.\n");
exit(1);
}
}
}
}