diff options
Diffstat (limited to 'osc')
-rwxr-xr-x | osc/packOSC-help.pd | 30 | ||||
-rwxr-xr-x | osc/packOSC.c | 1000 | ||||
-rwxr-xr-x | osc/routeOSC-help.pd | 35 | ||||
-rwxr-xr-x | osc/routeOSC.c | 510 | ||||
-rwxr-xr-x | osc/unpackOSC.c | 604 |
5 files changed, 2179 insertions, 0 deletions
diff --git a/osc/packOSC-help.pd b/osc/packOSC-help.pd new file mode 100755 index 0000000..8518642 --- /dev/null +++ b/osc/packOSC-help.pd @@ -0,0 +1,30 @@ +#N canvas 611 6 488 317 12;
+#X obj 171 184 udpsend;
+#X msg 177 158 disconnect;
+#X msg 171 96 connect 127.0.0.1 9997;
+#X obj 71 96 packOSC;
+#X obj 71 128 prepend send;
+#X obj 171 207 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 1
+1;
+#X msg 14 18 send /test/one/two/three zz 88 T;
+#X msg 71 65 send /test 1 2 3;
+#X msg 90 43 send /west 35;
+#X msg 201 41 send /*/left 22;
+#X msg 296 24 send /?est/ 1;
+#X msg 246 66 send /left one two;
+#X msg 339 43 send /right 88;
+#X text 4 226 packOSC is like sendOSC except that it outputs a list
+of floats instead of directly connecting to the network;
+#X text 158 276 2006/04/25 Martin Peach;
+#X connect 0 0 5 0;
+#X connect 1 0 0 0;
+#X connect 2 0 0 0;
+#X connect 3 0 4 0;
+#X connect 4 0 0 0;
+#X connect 6 0 3 0;
+#X connect 7 0 3 0;
+#X connect 8 0 3 0;
+#X connect 9 0 3 0;
+#X connect 10 0 3 0;
+#X connect 11 0 3 0;
+#X connect 12 0 3 0;
diff --git a/osc/packOSC.c b/osc/packOSC.c new file mode 100755 index 0000000..eed5f0a --- /dev/null +++ b/osc/packOSC.c @@ -0,0 +1,1000 @@ +/* packOSC is like sendOSC but outputs a list of floats which are the bytes making up the OSC packet. */ +/* This allows for the separation of the protocol and its transport. */ +/* Started by Martin Peach 20060403 */ +/* 20060425 version independent of libOSC */ +/* packOSC.c makes extensive use of code from OSC-client.c and sendOSC.c */ +/* as well as some from OSC-timetag.c. These files have the following header: */ +/* +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 +*/ + +#define MAX_ARGS 2000 +#define SC_BUFFER_SIZE 64000 + +#include "m_pd.h" +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#ifdef MSW +#include <winsock2.h> +#else +#include <ctype.h> +#endif + +#ifdef unix +#include <netinet/in.h> +#endif +/* This is from OSC-client.h :*/ +/* + + OSC-client.h: library for constructing OpenSoundControl messages. + Derived from SynthControl.h + Author: Matt Wright + Version 0.1: 6/13/97 + Version 0.2: 7/21/2000: Support for type-tagged messages + + + General notes: + + This library abstracts away the data format for the OpenSoundControl + protocol. Users of this library can construct OpenSoundControl packets + with a function call interface instead of knowing how to lay out the bits. + + All issues of memory allocation are deferred to the user of this library. + There are two data structures that the user must allocate. The first + is the actual buffer that the message will be written into. This buffer + can be any size, but if it's too small there's a possibility that it + will become overfull. The other data structure is called an OSCbuf, + and it holds all the state used by the library as it's constructing + a buffer. + + All procedures that have the possibility of an error condition return int, + with 0 indicating no error and nonzero indicating an error. The variable + OSC_errorMessage will be set to point to a string containing an error + message explaining what the problem is. + +*/ +/* + OSC_timeTag.h: library for manipulating OSC time tags + Matt Wright, 5/29/97 + + Time tags in OSC have the same format as in NTP: 64 bit fixed point, with the + top 32 bits giving number of seconds sinve midnight 1/1/1900 and the bottom + 32 bits giving fractional parts of a second. We represent this by an 8-byte + unsigned long if possible, or else a struct. + + NB: On many architectures with 8-byte ints, it's illegal (like maybe a bus error) + to dereference a pointer to an 8 byte int that's not 8-byte aligned. +*/ + +/* You may have to redefine this typedef if ints on your system + aren't 4 bytes. */ +typedef unsigned int uint4; +typedef struct +{ + uint4 seconds; + uint4 fraction; +} OSCTimeTag; + +/* Return the time tag 0x0000000000000001, indicating to the receiving device + that it should process the message immediately. */ +static OSCTimeTag OSCTT_Immediately(void); + +/* The int4byte type has to be a 4-byte integer. You may have to + change this to long or something else on your system. */ +typedef int int4byte; + +/* The maximum depth of bundles within bundles within bundles within... + This is the size of a static array. If you exceed this limit you'll + get an error message. */ +#define MAX_BUNDLE_NESTING 32 + +/* Don't ever manipulate the data in the OSCbuf struct directly. (It's + declared here in the header file only so your program will be able to + declare variables of type OSCbuf and have the right amount of memory + be allocated.) */ + +typedef struct OSCbuf_struct +{ + char *buffer; /* The buffer to hold the OSC packet */ + int size; /* Size of the buffer */ + char *bufptr; /* Current position as we fill the buffer */ + int state; /* State of partially-constructed message */ + int4byte *thisMsgSize; /* Pointer to count field before */ + /* currently-being-written message */ + int4byte *prevCounts[MAX_BUNDLE_NESTING]; /* Pointers to count */ + /* field before each currently open bundle */ + int bundleDepth; /* How many sub-sub-bundles are we in now? */ + char *typeStringPtr; /* This pointer advances through the type */ + /* tag string as you add arguments. */ + int gettingFirstUntypedArg; /* nonzero if this message doesn't have */ + /* a type tag and we're waiting for the 1st arg */ +} OSCbuf; + +/* Here are the possible values of the state field: */ + +#define EMPTY 0 /* Nothing written to packet yet */ +#define ONE_MSG_ARGS 1 /* Packet has a single message; gathering arguments */ +#define NEED_COUNT 2 /* Just opened a bundle; must write message name or */ + /* open another bundle */ +#define GET_ARGS 3 /* Getting arguments to a message. If we see a message */ + /* name or a bundle open/close then the current message */ + /* will end. */ +#define DONE 4 /* All open bundles have been closed, so can't write */ + /* anything else */ + + +static int OSC_strlen(char *s); +static int OSC_padString(char *dest, char *str); +static int OSC_padStringWithAnExtraStupidComma(char *dest, char *str); +static int OSC_WritePadding(char *dest, int i); +static int CheckTypeTag(OSCbuf *buf, char expectedType); + +/* Initialize the given OSCbuf. The user of this module must pass in the + block of memory that this OSCbuf will use for a buffer, and the number of + bytes in that block. (It's the user's job to allocate the memory because + you do it differently in different systems.) */ +static void OSC_initBuffer(OSCbuf *buf, int size, char *byteArray); + + +/* Reset the given OSCbuf. Do this after you send out the contents of + the buffer and want to start writing new data into it. */ +static void OSC_resetBuffer(OSCbuf *buf); + + +/* Is the buffer empty? (I.e., would it be stupid to send the buffer + contents to the synth?) */ +static int OSC_isBufferEmpty(OSCbuf *buf); + + +/* How much space is left in the buffer? */ +static int OSC_freeSpaceInBuffer(OSCbuf *buf); + +/* Does the buffer contain a valid OSC packet? (Returns nonzero if yes.) */ +static int OSC_isBufferDone(OSCbuf *buf); + +/* When you're ready to send out the buffer (i.e., when OSC_isBufferDone() + returns true), call these two procedures to get the OSC packet that's been + assembled and its size in bytes. (And then call OSC_resetBuffer() if you + want to re-use this OSCbuf for the next packet.) */ +static char *OSC_getPacket(OSCbuf *buf); +static int OSC_packetSize(OSCbuf *buf); +static int OSC_CheckOverflow(OSCbuf *buf, int bytesNeeded); + +/* Here's the basic model for building up OSC messages in an OSCbuf: + - Make sure the OSCbuf has been initialized with OSC_initBuffer(). + - To open a bundle, call OSC_openBundle(). You can then write + messages or open new bundles within the bundle you opened. + Call OSC_closeBundle() to close the bundle. Note that a packet + does not have to have a bundle; it can instead consist of just a + single message. + - For each message you want to send: + - Call OSC_writeAddress() with the name of your message. (In + addition to writing your message name into the buffer, this + procedure will also leave space for the size count of this message.) + - Alternately, call OSC_writeAddressAndTypes() with the name of + your message and with a type string listing the types of all the + arguments you will be putting in this message. + - Now write each of the arguments into the buffer, by calling one of: + OSC_writeFloatArg() + OSC_writeIntArg() + OSC_writeStringArg() + - Now your message is complete; you can send out the buffer or you can + add another message to it. +*/ + +static int OSC_openBundle(OSCbuf *buf, OSCTimeTag tt); +static int OSC_closeBundle(OSCbuf *buf); +static int OSC_writeAddress(OSCbuf *buf, char *name); +static int OSC_writeAddressAndTypes(OSCbuf *buf, char *name, char *types); +static int OSC_writeFloatArg(OSCbuf *buf, float arg); +static int OSC_writeIntArg(OSCbuf *buf, int4byte arg); +static int OSC_writeStringArg(OSCbuf *buf, char *arg); + +/* How many bytes will be needed in the OSC format to hold the given + string? The length of the string, plus the null char, plus any padding + needed for 4-byte alignment. */ +static int OSC_effectiveStringLength(char *string); + +typedef struct +{ + enum {INT_osc, FLOAT_osc, STRING_osc, NOTYPE_osc} type; + union + { + int i; + float f; + char *s; + } datum; +} typedArg; + +static int useTypeTags = 0; +static char bufferForOSCbuf[SC_BUFFER_SIZE]; +static t_atom bufferForOSClist[SC_BUFFER_SIZE]; + +static t_class *packOSC_class; + +typedef struct _packOSC +{ + t_object x_obj; + t_int x_typetags; // typetag flag + int x_bundle; // bundle open flag + OSCbuf x_oscbuf[1]; // OSCbuffer + t_atom *x_osclist; + t_outlet *x_bdpthout; // bundle-depth floatoutlet + t_outlet *x_listout; // OSC packet list ouput +} t_packOSC; + +static void *packOSC_new(t_floatarg udpflag); +static void packOSC_openbundle(t_packOSC *x); +static void packOSC_closebundle(t_packOSC *x); +static void packOSC_settypetags(t_packOSC *x, t_float *f); +static void packOSC_sendtyped(t_packOSC *x, t_symbol *s, int argc, t_atom *argv); +static void packOSC_send(t_packOSC *x, t_symbol *s, int argc, t_atom *argv); +static void packOSC_free(t_packOSC *x); +#ifdef MSW +__declspec(dllexport) +#endif +void packOSC_setup(void); +static typedArg packOSC_parseatom(t_atom *a); +static int packOSC_writemessage(OSCbuf *buf, char *messageName, int numArgs, typedArg *args); +static void packOSC_sendbuffer(t_packOSC *x); + +static void *packOSC_new(t_floatarg udpflag) +{ + t_packOSC *x = (t_packOSC *)pd_new(packOSC_class); + // set typetags to 1 by default + x->x_typetags = 1; + // bundle is closed + x->x_bundle = 0; + OSC_initBuffer(x->x_oscbuf, SC_BUFFER_SIZE, bufferForOSCbuf); + x->x_osclist = bufferForOSClist; + x->x_listout = outlet_new(&x->x_obj, &s_list); + x->x_bdpthout = outlet_new(&x->x_obj, &s_float); + return (x); +} + + +static void packOSC_openbundle(t_packOSC *x) +{ + if (x->x_oscbuf->bundleDepth + 1 >= MAX_BUNDLE_NESTING || + OSC_openBundle(x->x_oscbuf, OSCTT_Immediately())) + { + post("packOSC: Problem opening bundle."); + return; + } + x->x_bundle = 1; + outlet_float(x->x_bdpthout, (float)x->x_oscbuf->bundleDepth); +} + +static void packOSC_closebundle(t_packOSC *x) +{ + if (OSC_closeBundle(x->x_oscbuf)) + { + post("packOSC: Problem closing bundle."); + return; + } + outlet_float(x->x_bdpthout, (float)x->x_oscbuf->bundleDepth); + // in bundle mode we send when bundle is closed? + if(!OSC_isBufferEmpty(x->x_oscbuf) > 0 && OSC_isBufferDone(x->x_oscbuf)) + { + packOSC_sendbuffer(x); + OSC_initBuffer(x->x_oscbuf, SC_BUFFER_SIZE, bufferForOSCbuf); + x->x_bundle = 0; + return; + } +} + +static void packOSC_settypetags(t_packOSC *x, t_float *f) +{ + x->x_typetags = (f != 0)?1:0; + post("packOSC: setting typetags %d",x->x_typetags); +} + + +////////////////////////////////////////////////////////////////////// +// this is the real and only sending routine now, for both typed and +// undtyped mode. + +static void packOSC_sendtyped(t_packOSC *x, t_symbol *s, int argc, t_atom *argv) +{ + char messageName[MAXPDSTRING]; + typedArg args[MAX_ARGS]; + int i; + + messageName[0] = '\0'; // empty + + if(argc>MAX_ARGS) + { + post ("packOSC: too many arguments! (max: %d)", MAX_ARGS); + return; + } + +#ifdef DEBUG + post ("packOSC: type tags? %d", useTypeTags); +#endif + + atom_string(&argv[0], messageName, MAXPDSTRING); + for (i = 0; i < argc-1; i++) + { + args[i] = packOSC_parseatom(&argv[i+1]); +#ifdef DEBUG + switch (args[i].type) + { + case INT_osc: + post("packOSC: cell-cont: %d\n", args[i].datum.i); + break; + case FLOAT_osc: + post("packOSC: cell-cont: %f\n", args[i].datum.f); + break; + case STRING_osc: + post("packOSC: cell-cont: %s\n", args[i].datum.s); + break; + case NOTYPE_osc: + post("packOSC: unknown type\n"); + break; + } + post("packOSC: type-id: %d\n", args[i].type); +#endif + } + + if(packOSC_writemessage(x->x_oscbuf, messageName, i, args)) + { + post("packOSC: usage error, write-msg failed."); + return; + } + + if(!x->x_bundle) + { + packOSC_sendbuffer(x); + OSC_initBuffer(x->x_oscbuf, SC_BUFFER_SIZE, bufferForOSCbuf); + } +} + +static void packOSC_send(t_packOSC *x, t_symbol *s, int argc, t_atom *argv) +{ + if(!argc) + { + post("packOSC: not sending empty message."); + return; + } + if(x->x_typetags) + { + useTypeTags = 1; + packOSC_sendtyped(x, s, argc, argv); + useTypeTags = 0; + } + else + { + packOSC_sendtyped(x, s, argc, argv); + } +} + +static void packOSC_free(t_packOSC *x) +{ +} + +#ifdef MSW +__declspec(dllexport) +#endif +void packOSC_setup(void) +{ + packOSC_class = class_new(gensym("packOSC"), (t_newmethod)packOSC_new, + (t_method)packOSC_free, + sizeof(t_packOSC), 0, A_DEFFLOAT, 0); + class_addmethod(packOSC_class, (t_method)packOSC_settypetags, + gensym("typetags"), A_FLOAT, 0); + class_addmethod(packOSC_class, (t_method)packOSC_send, + gensym("send"), A_GIMME, 0); + class_addmethod(packOSC_class, (t_method)packOSC_send, + gensym("senduntyped"), A_GIMME, 0); + class_addmethod(packOSC_class, (t_method)packOSC_send, + gensym("sendtyped"), A_GIMME, 0); + class_addmethod(packOSC_class, (t_method)packOSC_openbundle, + gensym("["), 0, 0); + class_addmethod(packOSC_class, (t_method)packOSC_closebundle, + gensym("]"), 0, 0); +} + +static typedArg packOSC_parseatom(t_atom *a) +{ + typedArg returnVal; + t_float f; + t_int i; + t_symbol s; + char buf[MAXPDSTRING]; + + atom_string(a, buf, MAXPDSTRING); +#ifdef DEBUG + post("packOSC: atom type %d (%s)", a->a_type, buf); +#endif + /* It might be an int, a float, or a string */ + switch (a->a_type) + { + case A_FLOAT: + f = atom_getfloat(a); + i = atom_getint(a); + if (f == (t_float)i) + { // assume that if the int and float are the same, it's an int + returnVal.type = INT_osc; + returnVal.datum.i = i; + } + else + { + returnVal.type = FLOAT_osc; + returnVal.datum.f = f; + } + return returnVal; + case A_SYMBOL: + s = *atom_getsymbol(a); + returnVal.type = STRING_osc; + returnVal.datum.s = s.s_name; + return returnVal; + default: + atom_string(a, buf, MAXPDSTRING); + error("packOSC: atom type %d not implemented (%s)", a->a_type, buf); + returnVal.type = NOTYPE_osc; + returnVal.datum.s = NULL; + return returnVal; + } +} + +static int packOSC_writemessage(OSCbuf *buf, char *messageName, int numArgs, typedArg *args) +{ + int j, returnVal; + + returnVal = 0; +#ifdef DEBUG + post("packOSC: packOSC_writemessage: %s ", messageName); + + for (j = 0; j < numArgs; j++) + { + switch (args[j].type) + { + case INT_osc: + post("packOSC: %d ", args[j].datum.i); + break; + case FLOAT_osc: + post("packOSC: %f ", args[j].datum.f); + break; + case STRING_osc: + post("packOSC: %s ", args[j].datum.s); + break; + default: + post("packOSC: Unrecognized arg type %d", args[j].type); + break; + } + } +#endif + + if (!useTypeTags) + { + returnVal = OSC_writeAddress(buf, messageName); + if (returnVal) + { + post("packOSC: Problem writing address."); + } + } + else + { + /* First figure out the type tags */ + char typeTags[MAX_ARGS+2]; + + typeTags[0] = ','; + + for (j = 0; j < numArgs; ++j) + { + switch (args[j].type) + { + case INT_osc: + typeTags[j+1] = 'i'; + break; + case FLOAT_osc: + typeTags[j+1] = 'f'; + break; + case STRING_osc: + typeTags[j+1] = 's'; + break; + default: + post("packOSC: arg %d type is unrecognized(%d)", j, args[j].type); + break; + } + } + typeTags[j+1] = '\0'; + returnVal = OSC_writeAddressAndTypes(buf, messageName, typeTags); + if (returnVal) + { + post("packOSC: Problem writing address."); + } + } + + for (j = 0; j < numArgs; j++) + { + switch (args[j].type) + { + case INT_osc: + if ((returnVal = OSC_writeIntArg(buf, args[j].datum.i)) != 0) + { + return returnVal; + } + break; + case FLOAT_osc: + if ((returnVal = OSC_writeFloatArg(buf, args[j].datum.f)) != 0) + { + return returnVal; + } + break; + case STRING_osc: + if ((returnVal = OSC_writeStringArg(buf, args[j].datum.s)) != 0) + { + return returnVal; + } + break; + default: + break; // just skip bad types (which we won't get anyway unless this code is buggy) + } + } + return returnVal; +} + +static void packOSC_sendbuffer(t_packOSC *x) +{ + int i; + int length; + char *buf; +#ifdef DEBUG + post("packOSC_sendbuffer: Sending buffer...\n"); +#endif + if (OSC_isBufferEmpty(x->x_oscbuf)) + { + post("packOSC_sendbuffer() called but buffer empty"); + return; + } + if (!OSC_isBufferDone(x->x_oscbuf)) + { + post("packOSC_sendbuffer() called but buffer not ready!, not exiting"); + return; //{{raf}} + } + length = OSC_packetSize(x->x_oscbuf); + buf = OSC_getPacket(x->x_oscbuf); +#ifdef DEBUG + post ("packOSC_sendbuffer: length: %lu", length); +#endif + /* convert the bytes in the buffer to floats in a list */ + for (i = 0; i < length; ++i) SETFLOAT(&x->x_osclist[i], buf[i]); + /* send the list out the outlet */ + outlet_list(x->x_listout, &s_list, length, x->x_osclist); +} + +/* The next part is copied and morphed from OSC-client.c. */ +/* + Author: Matt Wright + Version 2.2: Calls htonl in the right places 20000620 + Version 2.3: Gets typed messages right. + */ + +/* + pd + ------------- + + raf@interaccess.com: + rev. for Win32 build (verified under Win-2ooo) 11-April-2002 + + -- changed licence part (20040820) jdl + -- Version 2.4 changes not in here (20040820) jdl + +*/ + + +static void OSC_initBuffer(OSCbuf *buf, int size, char *byteArray) +{ + buf->buffer = byteArray; + buf->size = size; + OSC_resetBuffer(buf); +} + +static void OSC_resetBuffer(OSCbuf *buf) +{ + buf->bufptr = buf->buffer; + buf->state = EMPTY; + buf->bundleDepth = 0; + buf->prevCounts[0] = 0; + buf->gettingFirstUntypedArg = 0; + buf->typeStringPtr = 0; +} + +static int OSC_isBufferEmpty(OSCbuf *buf) +{ + return buf->bufptr == buf->buffer; +} + +static int OSC_freeSpaceInBuffer(OSCbuf *buf) +{ + return buf->size - (buf->bufptr - buf->buffer); +} + +static int OSC_isBufferDone(OSCbuf *buf) +{ + return (buf->state == DONE || buf->state == ONE_MSG_ARGS); +} + +static char *OSC_getPacket(OSCbuf *buf) +{ + return buf->buffer; +} + +static int OSC_packetSize(OSCbuf *buf) +{ + return (buf->bufptr - buf->buffer); +} + +static int OSC_CheckOverflow(OSCbuf *buf, int bytesNeeded) +{ + if ((bytesNeeded) > OSC_freeSpaceInBuffer(buf)) + { + post("packOSC: buffer overflow"); + return 1; + } + return 0; +} + +static void PatchMessageSize(OSCbuf *buf) +{ + int4byte size = buf->bufptr - ((char *) buf->thisMsgSize) - 4; + *(buf->thisMsgSize) = htonl(size); +} + +static int OSC_openBundle(OSCbuf *buf, OSCTimeTag tt) +{ + if (buf->state == ONE_MSG_ARGS) + { + post("packOSC: Can't open a bundle in a one-message packet"); + return 3; + } + + if (buf->state == DONE) + { + post("packOSC: This packet is finished; can't open a new bundle"); + return 4; + } + + if (++(buf->bundleDepth) >= MAX_BUNDLE_NESTING) + { + post("packOSC: Bundles nested too deeply; change MAX_BUNDLE_NESTING"); + return 2; + } + + if (CheckTypeTag(buf, '\0')) return 9; + + if (buf->state == GET_ARGS) + { + PatchMessageSize(buf); + } + + if (buf->state == EMPTY) + { + /* Need 16 bytes for "#bundle" and time tag */ + if(OSC_CheckOverflow(buf, 16)) return 1; + } + else + { + /* This bundle is inside another bundle, so we need to leave + a blank size count for the size of this current bundle. */ + if(OSC_CheckOverflow(buf, 20))return 1; + *((int4byte *)buf->bufptr) = 0xaaaaaaaa; + buf->prevCounts[buf->bundleDepth] = (int4byte *)buf->bufptr; + + buf->bufptr += 4; + } + + buf->bufptr += OSC_padString(buf->bufptr, "#bundle"); + + + *((OSCTimeTag *) buf->bufptr) = tt; + + if (htonl(1) != 1) + { + /* Byte swap the 8-byte integer time tag */ + int4byte *intp = (int4byte *)buf->bufptr; + intp[0] = htonl(intp[0]); + intp[1] = htonl(intp[1]); + + /* tt is a struct of two 32-bit words, and even though + each word was wrong-endian, they were in the right order + in the struct.) */ + } + + buf->bufptr += sizeof(OSCTimeTag); + + buf->state = NEED_COUNT; + + buf->gettingFirstUntypedArg = 0; + buf->typeStringPtr = 0; + return 0; +} + + +static int OSC_closeBundle(OSCbuf *buf) +{ + if (buf->bundleDepth == 0) + { + /* This handles EMPTY, ONE_MSG, ARGS, and DONE */ + post("packOSC: Can't close bundle; no bundle is open!"); + return 5; + } + + if (CheckTypeTag(buf, '\0')) return 9; + + if (buf->state == GET_ARGS) + { + PatchMessageSize(buf); + } + + if (buf->bundleDepth == 1) + { + /* Closing the last bundle: No bundle size to patch */ + buf->state = DONE; + } + else + { + /* Closing a sub-bundle: patch bundle size */ + int size = buf->bufptr - ((char *) buf->prevCounts[buf->bundleDepth]) - 4; + *(buf->prevCounts[buf->bundleDepth]) = htonl(size); + buf->state = NEED_COUNT; + } + + --buf->bundleDepth; + buf->gettingFirstUntypedArg = 0; + buf->typeStringPtr = 0; + return 0; +} + +static int OSC_writeAddress(OSCbuf *buf, char *name) +{ + int4byte paddedLength; + + if (buf->state == ONE_MSG_ARGS) + { + post("packOSC: This packet is not a bundle, so you can't write another address"); + return 7; + } + + if (buf->state == DONE) + { + post("packOSC: This packet is finished; can't write another address"); + return 8; + } + + if (CheckTypeTag(buf, '\0')) return 9; + + paddedLength = OSC_effectiveStringLength(name); + + if (buf->state == EMPTY) + { + /* This will be a one-message packet, so no sizes to worry about */ + if(OSC_CheckOverflow(buf, paddedLength))return 1; + buf->state = ONE_MSG_ARGS; + } + else + { + /* GET_ARGS or NEED_COUNT */ + if(OSC_CheckOverflow(buf, 4+paddedLength))return 1; + if (buf->state == GET_ARGS) + { + /* Close the old message */ + PatchMessageSize(buf); + } + buf->thisMsgSize = (int4byte *)buf->bufptr; + *(buf->thisMsgSize) = 0xbbbbbbbb; + buf->bufptr += 4; + buf->state = GET_ARGS; + } + + /* Now write the name */ + buf->bufptr += OSC_padString(buf->bufptr, name); + buf->typeStringPtr = 0; + buf->gettingFirstUntypedArg = 1; + + return 0; +} + +static int OSC_writeAddressAndTypes(OSCbuf *buf, char *name, char *types) +{ + int result; + int4byte paddedLength; + + if (CheckTypeTag(buf, '\0')) return 9; + + result = OSC_writeAddress(buf, name); + + if (result) return result; + + paddedLength = OSC_effectiveStringLength(types); + + if(OSC_CheckOverflow(buf, paddedLength))return 1; + + buf->typeStringPtr = buf->bufptr + 1; /* skip comma */ + buf->bufptr += OSC_padString(buf->bufptr, types); + + buf->gettingFirstUntypedArg = 0; + return 0; +} + +static int CheckTypeTag(OSCbuf *buf, char expectedType) +{ + if (buf->typeStringPtr) + { + if (*(buf->typeStringPtr) != expectedType) + { + if (expectedType == '\0') + { + post("packOSC: According to the type tag I expected more arguments."); + } + else if (*(buf->typeStringPtr) == '\0') + { + post("packOSC: According to the type tag I didn't expect any more arguments."); + } + else + { + post("packOSC: According to the type tag I expected an argument of a different type."); + post("* Expected %c, string now %s\n", expectedType, buf->typeStringPtr); + } + return 9; + } + ++(buf->typeStringPtr); + } + return 0; +} + +static int OSC_writeFloatArg(OSCbuf *buf, float arg) +{ + int4byte *intp; + + if(OSC_CheckOverflow(buf, 4))return 1; + + if (CheckTypeTag(buf, 'f')) return 9; + + /* Pretend arg is a long int so we can use htonl() */ + intp = ((int4byte *) &arg); + *((int4byte *) buf->bufptr) = htonl(*intp); + + buf->bufptr += 4; + + buf->gettingFirstUntypedArg = 0; + return 0; +} + +static int OSC_writeIntArg(OSCbuf *buf, int4byte arg) +{ + if(OSC_CheckOverflow(buf, 4))return 1; + if (CheckTypeTag(buf, 'i')) return 9; + + *((int4byte *) buf->bufptr) = htonl(arg); + buf->bufptr += 4; + + buf->gettingFirstUntypedArg = 0; + return 0; +} + +static int OSC_writeStringArg(OSCbuf *buf, char *arg) +{ + int len; + + if (CheckTypeTag(buf, 's')) return 9; + + len = OSC_effectiveStringLength(arg); + + if (buf->gettingFirstUntypedArg && arg[0] == ',') + { + /* This un-type-tagged message starts with a string + that starts with a comma, so we have to escape it + (with a double comma) so it won't look like a type + tag string. */ + + if(OSC_CheckOverflow(buf, len+4))return 1; /* Too conservative */ + buf->bufptr += + OSC_padStringWithAnExtraStupidComma(buf->bufptr, arg); + + } + else + { + if(OSC_CheckOverflow(buf, len))return 1; + buf->bufptr += OSC_padString(buf->bufptr, arg); + } + + buf->gettingFirstUntypedArg = 0; + return 0; + +} + +/* String utilities */ + +static int OSC_strlen(char *s) +{ + int i; + for (i = 0; s[i] != '\0'; i++) /* Do nothing */ ; + return i; +} + +#define STRING_ALIGN_PAD 4 +static int OSC_effectiveStringLength(char *string) +{ + int len = OSC_strlen(string) + 1; /* We need space for the null char. */ + + /* Round up len to next multiple of STRING_ALIGN_PAD to account for alignment padding */ + if ((len % STRING_ALIGN_PAD) != 0) + { + len += STRING_ALIGN_PAD - (len % STRING_ALIGN_PAD); + } + return len; +} + +static int OSC_padString(char *dest, char *str) +{ + int i; + + for (i = 0; str[i] != '\0'; i++) dest[i] = str[i]; + + return OSC_WritePadding(dest, i); +} + +static int OSC_padStringWithAnExtraStupidComma(char *dest, char *str) +{ + int i; + + dest[0] = ','; + for (i = 0; str[i] != '\0'; i++) dest[i+1] = str[i]; + + return OSC_WritePadding(dest, i+1); +} + +static int OSC_WritePadding(char *dest, int i) +{ + dest[i] = '\0'; + i++; + + for (; (i % STRING_ALIGN_PAD) != 0; i++) dest[i] = '\0'; + + return i; +} + +/* The next bit is modified from OSC-timetag.c. */ +/* + + OSC_timeTag.c: library for manipulating OSC time tags + Matt Wright, 5/29/97 + + Version 0.2 (9/11/98): cleaned up so no explicit type names in the .c file. + +*/ + +static OSCTimeTag OSCTT_Immediately(void) +{ + OSCTimeTag tt; + tt.fraction = 1; + tt.seconds = 0; + return tt; +} +/* end packOSC.c*/ diff --git a/osc/routeOSC-help.pd b/osc/routeOSC-help.pd new file mode 100755 index 0000000..080ce50 --- /dev/null +++ b/osc/routeOSC-help.pd @@ -0,0 +1,35 @@ +#N canvas 0 0 574 322 12;
+#X obj 58 82 udpreceive 9997;
+#X obj 173 106 unpack 0 0 0 0;
+#X floatatom 173 129 3 0 0 0 - - -;
+#X floatatom 208 129 3 0 0 0 - - -;
+#X floatatom 244 129 3 0 0 0 - - -;
+#X floatatom 280 129 3 0 0 0 - - -;
+#X text 137 128 from;
+#X obj 58 114 unpackOSC;
+#X obj 56 158 print;
+#X obj 70 206 routeOSC /test /west;
+#X obj 70 241 print a;
+#X obj 147 241 print b;
+#X obj 225 241 print c;
+#X msg 203 171 set /left;
+#X msg 294 171 set /left /right;
+#X text 10 7 routeOSC;
+#X text 10 25 accepts lists of floats that are interpreted as OSC packets
+;
+#X text 10 43 set message reassigns outputs;
+#X text 244 206 arguments are OSC addresses to route;
+#X text 296 284 2006/04/25 Martin Peach;
+#X connect 0 0 7 0;
+#X connect 0 1 1 0;
+#X connect 1 0 2 0;
+#X connect 1 1 3 0;
+#X connect 1 2 4 0;
+#X connect 1 3 5 0;
+#X connect 7 0 8 0;
+#X connect 7 0 9 0;
+#X connect 9 0 10 0;
+#X connect 9 1 11 0;
+#X connect 9 2 12 0;
+#X connect 13 0 9 0;
+#X connect 14 0 9 0;
diff --git a/osc/routeOSC.c b/osc/routeOSC.c new file mode 100755 index 0000000..50c7872 --- /dev/null +++ b/osc/routeOSC.c @@ -0,0 +1,510 @@ +/* routeOSC.c 20060424 by Martin Peach, based on OSCroute and OSC-pattern-match.c. */ +/* 20060425 cleaned up some more */ +/* OSCroute.c header follows: */ +/* +Written by Adrian Freed, The Center for New Music and Audio Technologies, +University of California, Berkeley. Copyright (c) 1992,93,94,95,96,97,98,99,2000,01,02,03,04 +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 +*/ + +/* OSC-route.c + Max object for OSC-style dispatching + + To-do: + + Match a pattern against a pattern? + [Done: Only Slash-Star is allowed, see MyPatternMatch.] + Declare outlet types / distinguish leaf nodes from other children + More sophisticated (2-pass?) allmessages scheme + set message? + + + pd + ------------- + -- tweaks for Win32 www.zeggz.com/raf 13-April-2002 + +*/ +/* OSC-pattern-match.c header follows: */ +/* +Copyright © 1998. The Regents of the University of California (Regents). +All Rights Reserved. + +Written by Matt Wright, The Center for New Music and Audio Technologies, +University of California, Berkeley. + +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 OpenSound Control WWW page is + http://www.cnmat.berkeley.edu/OpenSoundControl +*/ + + + +/* + OSC-pattern-match.c + Matt Wright, 3/16/98 + Adapted from oscpattern.c, by Matt Wright and Amar Chaudhury +*/ + +#if HAVE_CONFIG_H +#include <config.h> +#endif + +/* the required include files */ +#include "m_pd.h" + +#define MAX_NUM 128 // maximum number of paths (prefixes) we can route + +typedef struct _routeOSC +{ + t_object x_obj; // required header + t_int x_num; // Number of prefixes we store + char *x_prefixes[MAX_NUM]; + void *x_outlets[MAX_NUM+1]; // one for each prefix plus one for everything else +} t_routeOSC; + +/* prototypes */ + +#ifdef MSW +__declspec(dllexport) +#endif +void routeOSC_setup(void); +static int MyPatternMatch (const char *pattern, const char *test); +static void routeOSC_doanything(t_routeOSC *x, t_symbol *s, int argc, t_atom *argv); +static void routeOSC_list(t_routeOSC *x, t_symbol *s, int argc, t_atom *argv); +static void *routeOSC_new(t_symbol *s, int argc, t_atom *argv); +static void routeOSC_set(t_routeOSC *x, t_symbol *s, int argc, t_atom *argv); +static char *NextSlashOrNull(char *p); +static void StrCopyUntilSlash(char *target, const char *source); + +/* from + OSC-pattern-match.c +*/ +static const char *theWholePattern; /* Just for warning messages */ +static int MatchBrackets (const char *pattern, const char *test); +static int MatchList (const char *pattern, const char *test); +static int PatternMatch (const char * pattern, const char * test); + +static t_class *routeOSC_class; +t_symbol *ps_list, *ps_complain, *ps_emptySymbol; + +static int MyPatternMatch (const char *pattern, const char *test) +{ + // This allows the special case of "routeOSC /* " to be an outlet that + // matches anything; i.e., it always outputs the input with the first level + // of the address stripped off. + + if (test[0] == '*' && test[1] == '\0') return 1; + else return PatternMatch(pattern, test); +} + +static void routeOSC_free(t_routeOSC *x) +{ +} + +/* initialization routine */ +// setup +#ifdef MSW +__declspec(dllexport) +#endif +void routeOSC_setup(void) +{ + routeOSC_class = class_new(gensym("routeOSC"), (t_newmethod)routeOSC_new, + (t_method)routeOSC_free,sizeof(t_routeOSC), 0, A_GIMME, 0); + class_addlist(routeOSC_class, routeOSC_list); + class_addanything(routeOSC_class, routeOSC_doanything); + class_addmethod(routeOSC_class, (t_method)routeOSC_set, gensym("set"), A_GIMME, 0); + class_sethelpsymbol(routeOSC_class, gensym("routeOSC-help.pd")); + + ps_emptySymbol = gensym(""); + + post("routeOSC object version 1.0 by Martin Peach, based on OSCroute by Matt Wright. pd: jdl Win32 raf."); + post("OSCroute Copyright © 1999 Regents of the Univ. of California. All Rights Reserved."); +} + +/* instance creation routine */ +static void *routeOSC_new(t_symbol *s, int argc, t_atom *argv) +{ + + t_routeOSC *x = (t_routeOSC *)pd_new(routeOSC_class); // get memory for a new object & initialize + int i; + + if (argc > MAX_NUM) + { + post("* routeOSC: too many arguments: %ld (max %ld)", argc, MAX_NUM); + return 0; + } + x->x_num = 0; + for (i = 0; i < argc; ++i) + { + if (argv[i].a_type == A_SYMBOL) + { + if (argv[i].a_w.w_symbol->s_name[0] == '/') + { /* Now that's a nice prefix */ + x->x_prefixes[i] = argv[i].a_w.w_symbol->s_name; + ++(x->x_num); + } +#if 0 +/* this doesn't make sense to me MP20060425 */ + else if (argv[i].a_w.w_symbol->s_name[0] == '#' && + argv[i].a_w.w_symbol->s_name[1] >= '1' && + argv[i].a_w.w_symbol->s_name[1] <= '9') + { + /* The Max programmer is trying to make a patch that will be + a subpatch with arguments. We have to make an outlet for this + argument. */ + x->x_prefixes[i] = "dummy"; + ++(x->x_num); + } +#endif /* 0 */ + } + else if (argv[i].a_type == A_FLOAT) + { + post("* routeOSC: float arguments are not OK."); + return 0; + } + else + { + post("* routeOSC: unrecognized argument type!"); + return 0; + } + } + /* Have to create the outlets in reverse order */ + /* well, not in pd ? */ + for (i = 0; i <= x->x_num; i++) + { + x->x_outlets[i] = outlet_new(&x->x_obj, &s_list); + } + return (x); +} + +static void routeOSC_set(t_routeOSC *x, t_symbol *s, int argc, t_atom *argv) +{ + int i; + + if (argc > x->x_num) + { + post ("routeOSC: too many paths"); + return; + } + for (i = 0; i < argc; ++i) + { + if (argv[i].a_type != A_SYMBOL) + { + post ("routeOSC: path %d not a symbol", i); + return; + } + if (argv[i].a_w.w_symbol->s_name[0] != '/') + { + post ("routeOSC: path %d doesn't start with /", i); + return; + } + } + for (i = 0; i < argc; ++i) + { + if (argv[i].a_w.w_symbol->s_name[0] == '/') + { /* Now that's a nice prefix */ + x->x_prefixes[i] = argv[i].a_w.w_symbol->s_name; + } + } +} + +static void routeOSC_list(t_routeOSC *x, t_symbol *s, int argc, t_atom *argv) +{ + if (argc > 0 && argv[0].a_type == A_SYMBOL) + { + /* Ignore the fact that this is a "list" */ + routeOSC_doanything(x, argv[0].a_w.w_symbol, argc-1, argv+1); + } + else + { + // post("* OSC-route: invalid list beginning with a number"); + // output on unmatched outlet jdl 20020908 + if (argv[0].a_type == A_FLOAT) + { + outlet_float(x->x_outlets[x->x_num], argv[0].a_w.w_float); + } + else + { + post("* routeOSC: unrecognized atom type!"); + } + } +} + +static void routeOSC_doanything(t_routeOSC *x, t_symbol *s, int argc, t_atom *argv) +{ + char *pattern, *nextSlash; + int i; + int matchedAnything; + // post("*** routeOSC_anything(s %s, argc %ld)", s->s_name, (long) argc); + + pattern = s->s_name; + if (pattern[0] != '/') + { + post("* routeOSC: invalid message pattern %s does not begin with /", s->s_name); + outlet_anything(x->x_outlets[x->x_num], s, argc, argv); + return; + } + matchedAnything = 0; + + nextSlash = NextSlashOrNull(pattern+1); + if (*nextSlash == '\0') + { + /* last level of the address, so we'll output the argument list */ + + for (i = 0; i < x->x_num; ++i) + { + if (MyPatternMatch(pattern+1, x->x_prefixes[i]+1)) + { + ++matchedAnything; + // I hate stupid Max lists with a special first element + if (argc == 0) + { + outlet_bang(x->x_outlets[i]); + } + else if (argv[0].a_type == A_SYMBOL) + { + // Promote the symbol that was argv[0] to the special symbol + outlet_anything(x->x_outlets[i], argv[0].a_w.w_symbol, argc-1, argv+1); + } + else if (argc > 1) + { + // Multiple arguments starting with a number, so naturally we have + // to use a special function to output this "list", since it's what + // Max originally meant by "list". + outlet_list(x->x_outlets[i], 0L, argc, argv); + } + else + { + // There was only one argument, and it was a number, so we output it + // not as a list + if (argv[0].a_type == A_FLOAT) + { + outlet_float(x->x_outlets[i], argv[0].a_w.w_float); + } + else + { + post("* routeOSC: unrecognized atom type!"); + } + } + } + } + } + else + { + /* There's more address after this part, so our output list will begin with + the next slash. */ + t_symbol *restOfPattern = 0; /* avoid the gensym unless we have to output */ + char patternBegin[1000]; + + /* Get the first level of the incoming pattern to match against all our prefixes */ + StrCopyUntilSlash(patternBegin, pattern+1); + + for (i = 0; i < x->x_num; ++i) + { + if (MyPatternMatch(patternBegin, x->x_prefixes[i]+1)) + { + ++matchedAnything; + if (restOfPattern == 0) restOfPattern = gensym(nextSlash); + outlet_anything(x->x_outlets[i], restOfPattern, argc, argv); + } + } + } + + if (!matchedAnything) + { + // output unmatched data on rightmost outlet a la normal 'route' object, jdl 20020908 + outlet_anything(x->x_outlets[x->x_num], s, argc, argv); + } +} + +static char *NextSlashOrNull(char *p) +{ + while (*p != '/' && *p != '\0') p++; + return p; +} + +static void StrCopyUntilSlash(char *target, const char *source) +{ + while (*source != '/' && *source != '\0') + { + *target = *source; + ++target; + ++source; + } + *target = 0; +} + +/* from + OSC-pattern-match.c + Matt Wright, 3/16/98 + Adapted from oscpattern.c, by Matt Wright and Amar Chaudhury +*/ + +static int PatternMatch (const char * pattern, const char * test) +{ + theWholePattern = pattern; + + if (pattern == 0 || pattern[0] == 0) return test[0] == 0; + + if (test[0] == 0) + { + if (pattern[0] == '*') return PatternMatch (pattern+1, test); + return 0; + } + + switch (pattern[0]) + { + case 0: + return test[0] == 0; + case '?': + return PatternMatch (pattern+1, test+1); + case '*': + if (PatternMatch (pattern+1, test)) return 1; + return PatternMatch (pattern, test+1); + case ']': + case '}': + post("routeOSC: Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern); + return 0; + case '[': + return MatchBrackets (pattern,test); + case '{': + return MatchList (pattern,test); + case '\\': + if (pattern[1] == 0) return test[0] == 0; + if (pattern[1] == test[0]) return PatternMatch (pattern+2,test+1); + return 0; + default: + if (pattern[0] == test[0]) return PatternMatch (pattern+1,test+1); + return 0; + } +} + +/* we know that pattern[0] == '[' and test[0] != 0 */ + +static int MatchBrackets (const char *pattern, const char *test) +{ + int result; + int negated = 0; + const char *p = pattern; + + if (pattern[1] == 0) + { + post("routeOSC: Unterminated [ in pattern \".../%s/...\"", theWholePattern); + return 0; + } + if (pattern[1] == '!') + { + negated = 1; + p++; + } + while (*p != ']') + { + if (*p == 0) + { + post("Unterminated [ in pattern \".../%s/...\"", theWholePattern); + return 0; + } + if (p[1] == '-' && p[2] != 0) + { + if (test[0] >= p[0] && test[0] <= p[2]) + { + result = !negated; + goto advance; + } + } + if (p[0] == test[0]) + { + result = !negated; + goto advance; + } + p++; + } + result = negated; +advance: + if (!result) return 0; + while (*p != ']') + { + if (*p == 0) + { + post("Unterminated [ in pattern \".../%s/...\"", theWholePattern); + return 0; + } + p++; + } + return PatternMatch (p+1,test+1); +} + +static int MatchList (const char *pattern, const char *test) +{ + const char *restOfPattern, *tp = test; + + for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) + { + if (*restOfPattern == 0) + { + post("Unterminated { in pattern \".../%s/...\"", theWholePattern); + return 0; + } + } + restOfPattern++; /* skip close curly brace */ + pattern++; /* skip open curly brace */ + while (1) + { + if (*pattern == ',') + { + if (PatternMatch (restOfPattern, tp)) return 1; + tp = test; + ++pattern; + } + else if (*pattern == '}') return PatternMatch (restOfPattern, tp); + else if (*pattern == *tp) + { + ++pattern; + ++tp; + } + else + { + tp = test; + while (*pattern != ',' && *pattern != '}') pattern++; + if (*pattern == ',') pattern++; + } + } +} + +/* end of routeOSC.c */ diff --git a/osc/unpackOSC.c b/osc/unpackOSC.c new file mode 100755 index 0000000..acdec07 --- /dev/null +++ b/osc/unpackOSC.c @@ -0,0 +1,604 @@ +/* unpackOSC is like dumpOSC but outputs two lists: a list of symbols for the path */ +/* and a list of floats and/or symbols for the data */ +/* This allows for the separation of the protocol and its transport. */ +/* Started by Martin Peach 20060420 */ +/* This version tries to be standalone from LIBOSC MP 20060425 */ +/* MP 20060505 fixed a bug (line 209) where bytes are wrongly interpreted as negative */ +/* dumpOSC.c header follows: */ +/* +Written by Matt Wright and Adrian Freed, The Center for New Music and +Audio Technologies, University of California, Berkeley. Copyright (c) +1992,93,94,95,96,97,98,99,2000,01,02,03,04 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 +*/ + + + /* + + dumpOSC.c + server that displays OpenSoundControl messages sent to it + for debugging client udp and UNIX protocol + + by Matt Wright, 6/3/97 + modified from dumpSC.c, by Matt Wright and Adrian Freed + + version 0.2: Added "-silent" option a.k.a. "-quiet" + + version 0.3: Incorporated patches from Nicola Bernardini to make + things Linux-friendly. Also added ntohl() in the right places + to support little-endian architectures. + + + + compile: + cc -o dumpOSC dumpOSC.c + + to-do: + + More robustness in saying exactly what's wrong with ill-formed + messages. (If they don't make sense, show exactly what was + received.) + + Time-based features: print time-received for each packet + + Clean up to separate OSC parsing code from socket/select stuff + + pd: branched from http://www.cnmat.berkeley.edu/OpenSoundControl/src/dumpOSC/dumpOSC.c + ------------- + -- added pd functions + -- socket is made differently than original via pd mechanisms + -- tweaks for Win32 www.zeggz.com/raf 13-April-2002 + -- the OSX changes from cnmat didnt make it here yet but this compiles + on OSX anyway. + +*/ + +#if HAVE_CONFIG_H +#include <config.h> +#endif + +#include "m_pd.h" + +/* declarations */ + + +#ifdef _WIN32 + #ifdef _MSC_VER +// #include "OSC-common.h" + #endif /* _MSC_VER */ + #include <string.h> + #include <stdlib.h> + #include <winsock2.h> + #include <stdio.h> +#else + #include <stdio.h> + #include <string.h> + #include <stdlib.h> +// #include <unistd.h> +// #include <fcntl.h> + #include <sys/types.h> +// #include <sys/stat.h> + #include <netinet/in.h> +// #include <rpc/rpc.h> +// #include <sys/socket.h> +// #include <sys/un.h> +// #include <sys/times.h> +// #include <sys/param.h> +// #include <sys/time.h> +// #include <sys/ioctl.h> + #include <ctype.h> +// #include <arpa/inet.h> +// #include <netdb.h> +// #include <pwd.h> +// #include <signal.h> +// #include <grp.h> +// #include <sys/file.h> + //#include <sys/prctl.h> + +// #ifdef NEED_SCHEDCTL_AND_LOCK +// #include <sys/schedctl.h> +// #include <sys/lock.h> +// #endif +#endif /* _WIN32 */ + +/* Declarations */ +#ifdef WIN32 + typedef unsigned __int64 osc_time_t; +#else + typedef unsigned long long osc_time_t; +#endif + +#define MAX_MESG 65536 // was 32768 MP: make same as MAX_UDP_PACKET + +/* ----------------------------- was dumpOSC ------------------------- */ + +#define MAX_PATH_AT 50 // maximum nuber of elements in OSC path + +static t_class *unpackOSC_class; + +typedef struct _unpackOSC +{ + t_object x_obj; + t_outlet *x_data_out; + t_atom x_data_at[MAX_MESG];// symbols making up the path + payload + int x_data_atc;// number of symbols to be output + char x_raw[MAX_MESG];// bytes making up the entire OSC message + int x_raw_c;// number of bytes in OSC message +} t_unpackOSC; + +#ifdef MSW +__declspec(dllexport) +#endif +void unpackOSC_setup(void); +static void *unpackOSC_new(void); +static void unpackOSC_free(t_unpackOSC *x); +void unpackOSC_setup(void); +static void unpackOSC_list(t_unpackOSC *x, t_symbol *s, int argc, t_atom *argv); +static int unpackOSC_path(t_unpackOSC *x, char *path); +static void unpackOSC_Smessage(t_unpackOSC *x, void *v, int n); +static void unpackOSC_PrintTypeTaggedArgs(t_unpackOSC *x, void *v, int n); +static void unpackOSC_PrintHeuristicallyTypeGuessedArgs(t_unpackOSC *x, void *v, int n, int skipComma); +static char *unpackOSC_DataAfterAlignedString(char *string, char *boundary); +static int unpackOSC_IsNiceString(char *string, char *boundary); + +static void *unpackOSC_new(void) +{ + t_unpackOSC *x; + + x = (t_unpackOSC *)pd_new(unpackOSC_class); + x->x_data_out = outlet_new(&x->x_obj, &s_list); + x->x_raw_c = x->x_data_atc = 0; + return (x); +} + +static void unpackOSC_free(t_unpackOSC *x) +{ +} + +#ifdef MSW +__declspec(dllexport) +#endif +void unpackOSC_setup(void) +{ + unpackOSC_class = class_new(gensym("unpackOSC"), + (t_newmethod)unpackOSC_new, (t_method)unpackOSC_free, + sizeof(t_unpackOSC), 0, 0); + class_addlist(unpackOSC_class, (t_method)unpackOSC_list); +} + +/* unpackOSC_list expects an OSC packet in the form of a list of floats on [0..255] */ +static void unpackOSC_list(t_unpackOSC *x, t_symbol *s, int argc, t_atom *argv) +{ + int size, messageLen, i, j; + char *messageName, *args, *buf; + + if ((argc%4) != 0) + { + post("unpackOSC: packet size (%d) not a multiple of 4 bytes: dropping packet", argc); + return; + } + /* copy the list to a byte buffer, checking for bytes only */ + for (i = 0; i < argc; ++i) + { + if (argv[i].a_type == A_FLOAT) + { + j = (int)argv[i].a_w.w_float; +// if ((j == argv[i].a_w.w_float) && (j >= 0) && (j <= 255)) +// this can miss bytes between 128 and 255 because they are interpreted somewhere as negative +// , so change to this: + if ((j == argv[i].a_w.w_float) && (j >= -128) && (j <= 255)) + { + x->x_raw[i] = (char)j; + } + else + { + post("unpackOSC: data out of range (%d), dropping packet", argv[i].a_w.w_float); + return; + } + } + else + { + post("unpackOSC: data not float, dropping packet"); + return; + } + } + x->x_raw_c = argc; + buf = x->x_raw; + + if ((argc >= 8) && (strncmp(buf, "#bundle", 8) == 0)) + { /* This is a bundle message. */ +#ifdef DEBUG + post("unpackOSC: bundle msg: bundles not yet supported\n"); +#endif + + if (argc < 16) + { + post("unpackOSC: Bundle message too small (%d bytes) for time tag", argc); + return; + } + + /* Print the time tag */ +#ifdef DEBUG + printf("unpackOSC: [ %lx%08lx\n", ntohl(*((unsigned long *)(buf+8))), + ntohl(*((unsigned long *)(buf+12)))); +#endif + + /* Note: if we wanted to actually use the time tag as a little-endian + 64-bit int, we'd have to word-swap the two 32-bit halves of it */ + + i = 16; /* Skip "#group\0" and time tag */ + + while(i < argc) + { + size = ntohl(*((int *) (buf + i))); + if ((size % 4) != 0) + { + post("unpackOSC: Bad size count %d in bundle (not a multiple of 4)", size); + return; + } + if ((size + i + 4) > argc) + { + post("unpackOSC: Bad size count %d in bundle (only %d bytes left in entire bundle)", + size, argc-i-4); + return; + } + + /* Recursively handle element of bundle */ + unpackOSC_list(x, s, size, &argv[i+4]); + i += 4 + size; + } + + if (i != argc) + { + post("unpackOSC: This can't happen"); + } +#ifdef DEBUG + printf("]\n"); +#endif + + } + else if ((argc == 24) && (strcmp(buf, "#time") == 0)) + { + post("unpackOSC: Time message: %s\n :).\n", buf); + return; + } + else + { /* This is not a bundle message or a time message */ + + messageName = buf; + args = unpackOSC_DataAfterAlignedString(messageName, buf+x->x_raw_c); + if (args == 0) + { + post("unpackOSC: Bad message name string: (%s) Dropping entire message.", + messageName); + return; + } +#ifdef DEBUG + post("unpackOSC: message name string: %s", messageName); +#endif + messageLen = args-messageName; + /* put the OSC path into a single symbol */ + x->x_data_atc = unpackOSC_path(x, messageName); + if (x->x_data_atc == 1) unpackOSC_Smessage(x, (void *)args, x->x_raw_c-messageLen); + } + if (x->x_data_atc >= 1) outlet_list(x->x_data_out, &s_list, x->x_data_atc, x->x_data_at); + x->x_data_atc = 0; +} + +static int unpackOSC_path(t_unpackOSC *x, char *path) +{ + int i; + + if (path[0] != '/') + { + post("unpackOSC: bad path (%s)", path); + return 0; + } + for (i = 1; i < MAX_MESG; ++i) + { + if (path[i] == '\0') + { /* the end of the path: turn path into a symbol */ + SETSYMBOL(&x->x_data_at[0],gensym(path)); + return 1; + } + } + post("unpackOSC: path too long"); + return 0; +} +#define SMALLEST_POSITIVE_FLOAT 0.000001f + +static void unpackOSC_Smessage(t_unpackOSC *x, void *v, int n) +{ + char *chars = v; + + if (n != 0) + { + if (chars[0] == ',') + { + if (chars[1] != ',') + { + /* This message begins with a type-tag string */ + unpackOSC_PrintTypeTaggedArgs(x, v, n); + } + else + { + /* Double comma means an escaped real comma, not a type string */ + unpackOSC_PrintHeuristicallyTypeGuessedArgs(x, v, n, 1); + } + } + else + { + unpackOSC_PrintHeuristicallyTypeGuessedArgs(x, v, n, 0); + } + } +} + +static void unpackOSC_PrintTypeTaggedArgs(t_unpackOSC *x, void *v, int n) +{ + char *typeTags, *thisType, *p; + int myargc = x->x_data_atc; + t_atom *mya = x->x_data_at; + + typeTags = v; + + if (!unpackOSC_IsNiceString(typeTags, typeTags+n)) + { + /* No null-termination, so maybe it wasn't a type tag + string after all */ + unpackOSC_PrintHeuristicallyTypeGuessedArgs(x, v, n, 0); + return; + } + + p = unpackOSC_DataAfterAlignedString(typeTags, typeTags+n); + + for (thisType = typeTags + 1; *thisType != 0; ++thisType) + { + switch (*thisType) + { + case 'i': case 'r': case 'm': case 'c': +#ifdef DEBUG + post("integer: %d", ntohl(*((int *) p))); +#endif + SETFLOAT(mya+myargc,(signed)ntohl(*((int *) p))); + myargc++; + p += 4; + break; + case 'f': + { + int i = ntohl(*((int *) p)); + float *floatp = ((float *) (&i)); +#ifdef DEBUG + post("float: %f", *floatp); +#endif + SETFLOAT(mya+myargc,*floatp); + myargc++; + p += 4; + break; + } + case 'h': case 't': +#ifdef DEBUG + printf("[A 64-bit int] "); +#endif + post("[A 64-bit int] not implemented"); + p += 8; + break; + case 'd': +#ifdef DEBUG + printf("[A 64-bit float] "); +#endif + post("[A 64-bit float] not implemented"); + p += 8; + break; + case 's': case 'S': + if (!unpackOSC_IsNiceString(p, typeTags+n)) + { + post("Type tag said this arg is a string but it's not!\n"); + return; + } + else + { +#ifdef DEBUG + post("string: \"%s\"", p); +#endif + SETSYMBOL(mya+myargc,gensym(p)); + myargc++; + p = unpackOSC_DataAfterAlignedString(p, typeTags+n); + + } + break; + case 'T': +#ifdef DEBUG + printf("[True] "); +#endif + SETFLOAT(mya+myargc,1.); + myargc++; + break; + case 'F': +#ifdef DEBUG + printf("[False] "); +#endif + SETFLOAT(mya+myargc,0.); + myargc++; + break; + case 'N': +#ifdef DEBUG + printf("[Nil]"); +#endif + post("sendOSC: [Nil] not implemented"); + break; + case 'I': +#ifdef DEBUG + printf("[Infinitum]"); +#endif + post("sendOSC: [Infinitum] not implemented"); + break; + default: + post("sendOSC: [Unrecognized type tag %c]", *thisType); + } + } + x->x_data_atc = myargc; +} + +static void unpackOSC_PrintHeuristicallyTypeGuessedArgs(t_unpackOSC *x, void *v, int n, int skipComma) +{ + int i, thisi; + int *ints; + float thisf; + char *chars, *string, *nextString; + int myargc= x->x_data_atc; + t_atom* mya = x->x_data_at; + + + /* Go through the arguments 32 bits at a time */ + ints = v; + chars = v; + + for (i = 0; i < n/4; ) + { + string = &chars[i*4]; + thisi = ntohl(ints[i]); + /* Reinterpret the (potentially byte-reversed) thisi as a float */ + thisf = *(((float *) (&thisi))); + + if (thisi >= -1000 && thisi <= 1000000) + { +#ifdef DEBUG + printf("%d ", thisi); +#endif + SETFLOAT(mya+myargc,(t_float) (thisi)); + myargc++; + i++; + } + else if (thisf >= -1000.f && thisf <= 1000000.f && + (thisf <=0.0f || thisf >= SMALLEST_POSITIVE_FLOAT)) + { +#ifdef DEBUG + printf("%f ", thisf); +#endif + SETFLOAT(mya+myargc,thisf); + myargc++; + i++; + } + else if (unpackOSC_IsNiceString(string, chars+n)) + { + nextString = unpackOSC_DataAfterAlignedString(string, chars+n); +#ifdef DEBUG + printf("\"%s\" ", (i == 0 && skipComma) ? string +1 : string); +#endif + SETSYMBOL(mya+myargc,gensym(string)); + myargc++; + i += (nextString-string) / 4; + } + else + { + // unhandled .. ;) +#ifdef DEBUG + post("unpackOSC: indeterminate type: 0x%x xx", ints[i]); +#endif + i++; + } + x->x_data_atc = myargc; + } +} + +#define STRING_ALIGN_PAD 4 + +static char *unpackOSC_DataAfterAlignedString(char *string, char *boundary) +{ + /* The argument is a block of data beginning with a string. The + string has (presumably) been padded with extra null characters + so that the overall length is a multiple of STRING_ALIGN_PAD + bytes. Return a pointer to the next byte after the null + byte(s). The boundary argument points to the character after + the last valid character in the buffer---if the string hasn't + ended by there, something's wrong. + + If the data looks wrong, return 0, and set htm_error_string */ + + int i; + + if ((boundary - string) %4 != 0) + { + post("unpackOSC: DataAfterAlignedString: bad boundary"); + return 0; + } + + for (i = 0; string[i] != '\0'; i++) + { + if (string + i >= boundary) + { + post("unpackOSC: DataAfterAlignedString: Unreasonably long string"); + return 0; + } + } + + /* Now string[i] is the first null character */ + i++; + + for (; (i % STRING_ALIGN_PAD) != 0; i++) + { + if (string + i >= boundary) + { + post("unpackOSC: DataAfterAlignedString: Unreasonably long string"); + return 0; + } + if (string[i] != '\0') + { + post("unpackOSC:DataAfterAlignedString: Incorrectly padded string"); + return 0; + } + } + + return string+i; +} + +static int unpackOSC_IsNiceString(char *string, char *boundary) +{ + /* Arguments same as DataAfterAlignedString(). Is the given "string" + really a string? I.e., is it a sequence of isprint() characters + terminated with 1-4 null characters to align on a 4-byte boundary? + Returns 1 if true, else 0. */ + + int i; + + if ((boundary - string) %4 != 0) + { + fprintf(stderr, "Internal error: IsNiceString: bad boundary\n"); + return 0; + } + + for (i = 0; string[i] != '\0'; i++) + if ((!isprint(string[i])) || (string + i >= boundary)) return 0; + + /* If we made it this far, it's a null-terminated sequence of printing characters + in the given boundary. Now we just make sure it's null padded... */ + + /* Now string[i] is the first null character */ + i++; + for (; (i % STRING_ALIGN_PAD) != 0; i++) + if (string[i] != '\0') return 0; + + return 1; +} + +/* end of unpackOSC.c */ |