/* Written by Matt Wright, The Center for New Music and Audio Technologies, University of California, Berkeley. Copyright (c) 1996,97,98,99,2000,01,02,03 The Regents of the University of California (Regents). Permission to use, copy, modify, distribute, and distribute modified versions of this software and its documentation without fee and without a signed licensing agreement, is hereby granted, provided that the above copyright notice, this paragraph and the following two paragraphs appear in all copies, modifications, and distributions. IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. The OSC webpage is http://cnmat.cnmat.berkeley.edu/OpenSoundControl */ /* sendOSC.c Matt Wright, 6/3/97 based on sendSC.c, which was based on a version by Adrian Freed Text-based OpenSoundControl client. User can enter messages via command line arguments or standard input. Version 0.1: "play" feature Version 0.2: Message type tags. */ #define VERSION "http://cnmat.berkeley.edu/OpenSoundControl/sendOSC-0.1.html" /* compiling: cc -o sendOSC sendOSC.c htmsocket.c OpenSoundControl.c OSC_timeTag.c */ #include "OSC-client.h" #include "htmsocket.h" #include #include #include #include #include #include #include #include /* #include */ #include typedef struct { enum {INT, FLOAT, STRING} type; union { int i; float f; char *s; } datum; } typedArg; void CommandLineMode(int argc, char *argv[], void *htmsocket); int WriteMessage(OSCbuf *buf, char *messageName, int number); void SendBuffer(void *htmsocket, OSCbuf *buf); void SendData(void *htmsocket, int size, char *data); void fatal_error(char *s); void complain(char *s, ...); int serialport_init(const char* serialport, int baud); int serialport_read_until(int fd, char* buf, char until); /* Exit status codes: 0: successful 2: Message(s) dropped because of buffer overflow 3: Socket error 4: Usage error 5: Internal error */ static int exitStatus = 0; int fd; static int useTypeTags = 1; main(int argc, char *argv[]) { int portnumber; char *hostname = 0; void *htmsocket; fd=serialport_init("/dev/ttyUSB0", 9600); argc--; argv++; if (argc == 0) { goto usageerror; } if (argc >= 1 && (strncmp(*argv, "-notypetags", 2) == 0)) { useTypeTags = 0; argv++; // argc--; } if (argc >= 2 && (strncmp(*argv, "-r", 2) == 0)) { hostname = getenv("REMOTE_ADDR"); if (hostname == NULL) { complain("sendSC -r: REMOTE_ADDR not in environment\n"); exit(4); } argv++; // argc--; } if (argc >= 3 && (strncmp(*argv, "-h", 2) == 0)) { hostname = argv[1]; argv += 2; argc -= 1; } portnumber = atoi(*argv); argv++; argc--; htmsocket = OpenHTMSocket(hostname, portnumber); if (!htmsocket) { perror("Couldn't open socket: "); exit(3); } if (argc > 0) { printf("host %s, port %d, %s\n", hostname, portnumber, useTypeTags ? "use type tags" : "don't use type tags"); CommandLineMode(argc, argv, htmsocket); } else { printf("sSC version " VERSION "\n"); printf("by Matt Wright. Copyright (c) 1996, 1997 Regents of the University of California.\n"); printf("host %s, port %d, %s\n", hostname, portnumber, useTypeTags ? "use type tags" : "don't use type tags"); // InteractiveMode(htmsocket); } CloseHTMSocket(htmsocket); exit(exitStatus); usageerror: complain("usage: %s [-notypetags] [-r] [-h target_host_name] port_number [message...]\n", argv[-1]); exit(4); } #define MAX_ARGS 2000 #define SC_BUFFER_SIZE 32000 static char bufferForOSCbuf[SC_BUFFER_SIZE]; void CommandLineMode(int argc, char *argv[], void *htmsocket) { char messageName[10]; int number; // typedArg args[MAX_ARGS]; int i,j, numArgs; OSCbuf buf[1]; char buff[256]; while (1) { //messageName is last number of local IP??? OSC_initBuffer(buf, SC_BUFFER_SIZE, bufferForOSCbuf); // OSC_openBundle(buf, OSCTT_Immediately()); char messageName[]="/test"; // read from serial serialport_read_until(fd, buff, '\n'); //atoi number=atoi(buff); WriteMessage(buf, messageName, number); SendBuffer(htmsocket, buf); // OSC_closeBundle(buf); // sleep(1); } } #define MAXMESG 2048 int WriteMessage(OSCbuf *buf, char *messageName, int number) { int j, returnVal; returnVal = 0; // printf("%s ", number); returnVal = OSC_writeAddress(buf, messageName); returnVal = OSC_writeIntArg(buf, number); return returnVal; } void SendBuffer(void *htmsocket, OSCbuf *buf) { #ifdef DEBUG printf("Sending buffer...\n"); #endif if (OSC_isBufferEmpty(buf)) return; if (!OSC_isBufferDone(buf)) { fatal_error("SendBuffer() called but buffer not ready!"); exit(5); } SendData(htmsocket, OSC_packetSize(buf), OSC_getPacket(buf)); } void SendData(void *htmsocket, int size, char *data) { if (!SendHTMSocket(htmsocket, size, data)) { perror("Couldn't send out socket: "); CloseHTMSocket(htmsocket); exit(3); } } void fatal_error(char *s) { fprintf(stderr, "%s\n", s); exit(4); } #include void complain(char *s, ...) { va_list ap; va_start(ap, s); vfprintf(stderr, s, ap); va_end(ap); } int serialport_init(const char* serialport, int baud) { struct termios toptions; int fd; //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n", // serialport,baud); fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("init_serialport: Unable to open port "); return -1; } if (tcgetattr(fd, &toptions) < 0) { perror("init_serialport: Couldn't get term attributes"); return -1; } speed_t brate = baud; // let you override switch below if needed switch(baud) { case 4800: brate=B4800; break; case 9600: brate=B9600; break; // if you want these speeds, uncomment these and set #defines if Linux //#ifndef OSNAME_LINUX // case 14400: brate=B14400; break; //#endif case 19200: brate=B19200; break; //#ifndef OSNAME_LINUX // case 28800: brate=B28800; break; //#endif case 38400: brate=B38400; break; case 57600: brate=B57600; break; case 115200: brate=B115200; break; } cfsetispeed(&toptions, brate); cfsetospeed(&toptions, brate); // 8N1 toptions.c_cflag &= ~PARENB; toptions.c_cflag &= ~CSTOPB; toptions.c_cflag &= ~CSIZE; toptions.c_cflag |= CS8; // no flow control toptions.c_cflag &= ~CRTSCTS; toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw toptions.c_oflag &= ~OPOST; // make raw // see: http://unixwiz.net/techtips/termios-vmin-vtime.html toptions.c_cc[VMIN] = 0; toptions.c_cc[VTIME] = 20; if( tcsetattr(fd, TCSANOW, &toptions) < 0) { perror("init_serialport: Couldn't set term attributes"); return -1; } return fd; } int serialport_read_until(int fd, char* buf, char until) { char b[1]; int i=0; do { int n = read(fd, b, 1); // read a char at a time if( n==-1) return -1; // couldn't read if( n==0 ) { usleep( 10 * 1000 ); // wait 10 msec try again continue; } buf[i] = b[0]; i++; } while( b[0] != until ); buf[i] = 0; // null terminate the string return 0; }