summaryrefslogtreecommitdiff
path: root/Libraries/OSC
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/OSC')
-rw-r--r--Libraries/OSC/OSCDecoder.cpp118
-rw-r--r--Libraries/OSC/OSCDecoder.h31
-rw-r--r--Libraries/OSC/OSCMessage.cpp386
-rw-r--r--Libraries/OSC/OSCMessage.h104
-rw-r--r--Libraries/OSC/OSCcommon.h60
5 files changed, 699 insertions, 0 deletions
diff --git a/Libraries/OSC/OSCDecoder.cpp b/Libraries/OSC/OSCDecoder.cpp
new file mode 100644
index 0000000..0784851
--- /dev/null
+++ b/Libraries/OSC/OSCDecoder.cpp
@@ -0,0 +1,118 @@
+/*
+
+ ArdOSC - OSC Library for Arduino.
+
+ -------- Lisence -----------------------------------------------------------
+
+ ArdOSC
+
+ The MIT License
+
+ Copyright (c) 2009 - 2010 recotana( http://recotana.com ) All right reserved
+
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "OSCcommon.h"
+#include "OSCDecoder.h"
+
+
+
+int16_t OSCDecoder::decode( OSCMessage::OSCMessage *mes ,const uint8_t *recData ){
+
+
+//=========== BIN -> OSC Address(String) Decode ===========
+
+ const uint8_t *packStartPtr=recData;
+
+ mes->setOSCAddress((char*)packStartPtr);
+
+ packStartPtr += mes->oscAdrPacSize;
+
+
+
+//=========== BIN -> TypeTag (String) Decode ===========
+
+ mes->setTypeTags((char*)(packStartPtr+1));
+
+ packStartPtr += mes->typeTagPacSize;
+
+
+//=========== BIN -> Auguments Decode ===========
+
+
+ if (mes->arguments!=NULL) free(mes->arguments);
+ mes->arguments=(void**)calloc(mes->argsNum, sizeof(void*));
+
+ mes->argsPacSize=0;
+
+ uint8_t *tmpPtr;
+ uint16_t tmpSize;
+
+
+
+ for (uint16_t i=0; i< mes->argsNum ; i++) {
+
+ switch (mes->typeTag[i]) {
+
+ case 'i':
+
+ tmpPtr=(uint8_t*)calloc(1, 4);
+
+ *(tmpPtr+3)=*(uint8_t*)packStartPtr++;
+ *(tmpPtr+2)=*(uint8_t*)packStartPtr++;
+ *(tmpPtr+1)=*(uint8_t*)packStartPtr++;
+ *tmpPtr=*(uint8_t*)packStartPtr++;
+
+ mes->arguments[i]=(uint32_t*)tmpPtr;
+ mes->argsPacSize += 4;
+ break;
+
+#ifdef _USE_FLOAT_
+ case 'f':
+
+ tmpPtr=(uint8_t*)calloc(1, 4);
+
+ *(tmpPtr+3)=*(uint8_t*)packStartPtr++;
+ *(tmpPtr+2)=*(uint8_t*)packStartPtr++;
+ *(tmpPtr+1)=*(uint8_t*)packStartPtr++;
+ *tmpPtr=*(uint8_t*)packStartPtr++;
+
+ mes->arguments[i]=(float*)tmpPtr;
+ mes->argsPacSize += 4;
+
+ break;
+#endif
+
+
+#ifdef _USE_STRING_
+ case 's':
+
+ tmpSize=strlen((char*)packStartPtr);
+
+ if(tmpSize > kMaxStringCharactor){
+
+ DBG_LOGLN("decode max str err");
+ return 1;
+ }
+
+
+ tmpPtr=(uint8_t*)calloc(tmpSize+1, 1);
+ strcpy((char*)tmpPtr,(char*)packStartPtr);
+
+ mes->arguments[i]=tmpPtr;
+
+
+ tmpSize = mes->getPackSize(tmpSize);
+ packStartPtr += tmpSize;
+ mes->argsPacSize += tmpSize;
+
+ break;
+#endif
+ }
+ }
+
+ return 0;
+}
diff --git a/Libraries/OSC/OSCDecoder.h b/Libraries/OSC/OSCDecoder.h
new file mode 100644
index 0000000..0c47a9b
--- /dev/null
+++ b/Libraries/OSC/OSCDecoder.h
@@ -0,0 +1,31 @@
+/*
+
+ ArdOSC - OSC Library for Arduino.
+
+ -------- Lisence -----------------------------------------------------------
+
+ ArdOSC
+
+ The MIT License
+
+ Copyright (c) 2009 - 2010 recotana( http://recotana.com ) All right reserved
+
+ */
+
+#ifndef OSCDecoder_h
+#define OSCDecoder_h
+
+#include "OSCMessage.h"
+
+class OSCDecoder{
+
+private:
+
+
+public:
+
+ int16_t decode( OSCMessage::OSCMessage *mes ,const uint8_t *recData );
+
+};
+
+#endif \ No newline at end of file
diff --git a/Libraries/OSC/OSCMessage.cpp b/Libraries/OSC/OSCMessage.cpp
new file mode 100644
index 0000000..5fb4d87
--- /dev/null
+++ b/Libraries/OSC/OSCMessage.cpp
@@ -0,0 +1,386 @@
+/*
+
+ ArdOSC - OSC Library for Arduino.
+
+ -------- Lisence -----------------------------------------------------------
+
+ ArdOSC
+
+ The MIT License
+
+ Copyright (c) 2009 - 2010 recotana( http://recotana.com ) All right reserved
+
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "OSCcommon.h"
+#include "OSCMessage.h"
+
+
+
+
+
+
+OSCMessage::OSCMessage()
+{
+ DBG_LOGLN("construct message");
+
+ oscAddress=NULL;
+ typeTag=NULL;
+ arguments=NULL;
+
+ flush();
+
+}
+
+OSCMessage::OSCMessage(const char *_oscAddress)
+{
+ DBG_LOGLN("construct message with Adr");
+ oscAddress=NULL;
+ typeTag=NULL;
+ arguments=NULL;
+
+ flush();
+
+ setOSCAddress(_oscAddress);
+
+}
+
+OSCMessage::~OSCMessage()
+{
+ DBG_LOGLN("destruct message");
+ flush();
+}
+
+
+void OSCMessage::flush(){
+
+ if(oscAddress!=NULL) free(oscAddress);
+
+ oscAddress=NULL;
+ oscAdrSize=0;
+ oscAdrPacSize=0;
+
+ if(typeTag!=NULL) free(typeTag);
+
+ typeTag=NULL;
+ typeTagSize=0;
+ typeTagPacSize=0;
+
+ if(arguments!=NULL){
+
+ for (uint16_t i=0; i< argsNum; i++) {
+ free((void*)arguments[i]);
+ arguments[i]=NULL;
+ }
+ free(arguments);
+ }
+ argsNum=0;
+ argsPacSize=0;
+
+ arguments=NULL;
+
+ setIpAddress(0,0,0,0);
+ portNumber=0;
+
+ DBG_LOGLN("flush message obj");
+
+}
+
+void OSCMessage::setAddress(uint8_t *_ip , uint16_t _port){
+
+ setIpAddress(_ip);
+
+ portNumber=_port;
+
+}
+void OSCMessage::setIpAddress(uint8_t *_ip){
+
+ ipAddress[0] = _ip[0];
+ ipAddress[1] = _ip[1];
+ ipAddress[2] = _ip[2];
+ ipAddress[3] = _ip[3];
+
+ DBG_LOG("set IP Adr with IParray:");
+ DBG_LOG((int)ipAddress[0]);
+ DBG_LOG(".");
+ DBG_LOG((int)ipAddress[1]);
+ DBG_LOG(".");
+ DBG_LOG((int)ipAddress[2]);
+ DBG_LOG(".");
+ DBG_LOGLN((int)ipAddress[3]);
+
+}
+
+void OSCMessage::setIpAddress( uint8_t _ip1 ,uint8_t _ip2 ,uint8_t _ip3 ,uint8_t _ip4 ){
+
+ ipAddress[0] = _ip1;
+ ipAddress[1] = _ip2;
+ ipAddress[2] = _ip3;
+ ipAddress[3] = _ip4;
+
+ DBG_LOG("set IP Adr with IP nums:");
+ DBG_LOG((int)ipAddress[0]);
+ DBG_LOG(".");
+ DBG_LOG((int)ipAddress[1]);
+ DBG_LOG(".");
+ DBG_LOG((int)ipAddress[2]);
+ DBG_LOG(".");
+ DBG_LOGLN((int)ipAddress[3]);
+
+}
+
+uint8_t *OSCMessage::getIpAddress(){
+ return ipAddress;
+}
+
+void OSCMessage::setPortNumber(uint16_t _port){
+ portNumber=_port;
+}
+
+uint16_t OSCMessage::getPortNumber(){
+ return portNumber;
+}
+
+
+
+
+
+int16_t OSCMessage::setOSCMessage( const char *_address ,char *types , ... ){
+
+ va_list argsList;
+
+ DBG_LOG("set OSCMessage:");
+
+
+ if( setOSCAddress(_address) > 0) return 1;
+ if( setTypeTags(types) > 0) return 1;
+
+
+
+
+ //-------------------------------------------------
+
+ if (arguments!=NULL) free(arguments);
+ arguments=(void**)calloc(argsNum, sizeof(void*));
+
+
+
+ argsPacSize=0;
+
+ void *tmpPtr;
+ char *tmpStr;
+ uint16_t strSize;
+
+ va_start( argsList, types );
+
+ for(uint8_t i=0 ; i < argsNum ; i++){
+
+ typeTag[i]=types[i];
+
+
+ switch(types[i]){
+
+ case 'i':
+ tmpPtr=calloc(1, 4);
+
+ memcpy(tmpPtr,(int32_t *)va_arg(argsList, int32_t *),4);
+ arguments[i]=(int32_t *)tmpPtr;
+ argsPacSize += 4;
+
+ DBG_LOG(" int(4byte):");
+ DBG_LOG(*(int32_t *)tmpPtr);
+
+ break;
+
+
+#ifdef _USE_FLOAT_
+ case 'f':
+ tmpPtr=calloc(1, 4);
+
+ memcpy(tmpPtr,(float *)va_arg(argsList, float *),4);
+ arguments[i]=(float *)tmpPtr;
+
+ argsPacSize += 4;
+
+ DBG_LOG(" float(4byte):");
+ DBG_LOG(*(float *)tmpPtr);
+
+ break;
+#endif
+
+
+#ifdef _USE_STRING_
+ case 's':
+ tmpStr=(char *)va_arg(argsList, char *);
+ strSize=strlen(tmpStr);
+ DBG_LOGLN(strSize);
+ if(strSize > kMaxStringCharactor){
+
+ DBG_LOGLN("set args message max str err");
+ return 1;
+ }
+
+ tmpPtr=calloc(strSize+1, 1);
+ strcpy((char*)tmpPtr, tmpStr);
+
+ arguments[i]=tmpPtr;
+ argsPacSize += getPackSize( strSize );
+
+ DBG_LOG(" String,size:");
+ DBG_LOG(strSize);
+ DBG_LOG(" packsize:");
+ DBG_LOG(getPackSize( strSize ));
+ DBG_LOG(" :");
+ DBG_LOG((char *)tmpPtr);
+
+ break;
+#endif
+
+ }
+
+ }
+ DBG_LOGLN("");
+ DBG_LOGLN("");
+ return 0;
+}
+
+
+
+
+int16_t OSCMessage::setOSCAddress(const char *_address){
+
+ oscAdrSize=(uint16_t)strlen(_address);
+
+ if( oscAdrSize > kMaxOSCAdrCharactor ){
+ DBG_LOGLN("set OSC max OSC Adr err");
+ flush();
+ return 1;
+ }
+
+ if(oscAddress!=NULL) free(oscAddress);
+
+ oscAddress=(char*)calloc(1,oscAdrSize+1);
+ strcpy(oscAddress,_address);
+
+ oscAdrPacSize=getPackSize(oscAdrSize);
+
+ DBG_LOG("set OSC Adr:");
+ DBG_LOG(oscAddress);
+ DBG_LOG(" size:");
+ DBG_LOG(oscAdrSize);
+ DBG_LOG(" packsize:");
+ DBG_LOGLN(oscAdrPacSize);
+
+ return 0;
+}
+char *OSCMessage::getOSCAddress(){
+ return oscAddress;
+}
+
+
+
+int16_t OSCMessage::setTypeTags(const char *_tags ){
+
+ uint8_t maxErr=0;
+
+ argsNum=(uint16_t)strlen(_tags);
+
+ if(argsNum > kMaxAugument){
+ DBG_LOGLN("set tags max arg err");
+ flush();
+ return 1;
+ }
+
+ typeTagSize = argsNum+1;
+
+ if(typeTag!=NULL) free(typeTag);
+ typeTag=(char*)calloc(1,argsNum+1);
+
+ strcpy(typeTag,_tags);
+
+ typeTagPacSize=getPackSize(typeTagSize);
+
+ argsNum=typeTagSize;
+
+
+ DBG_LOG("set TypeTag:");
+ DBG_LOG(typeTag);
+ DBG_LOG(" size:");
+ DBG_LOG(typeTagSize);
+ DBG_LOG(" packsize:");
+ DBG_LOGLN(typeTagPacSize);
+
+ return 0;
+}
+
+char * OSCMessage::getTypeTags(){
+ return typeTag;
+}
+
+char OSCMessage::getTypeTag(uint16_t _index){
+ if(_index>argsNum) _index=argsNum-1;
+ return typeTag[_index];
+}
+
+
+int16_t OSCMessage::getArgsNum(){
+ return argsNum;
+}
+
+
+int32_t OSCMessage::getInteger32(uint16_t _index){
+
+ int32_t *value;
+ if(_index > argsNum) _index=argsNum-1;
+ value = (int32_t *)arguments[_index];
+
+ return *value;
+
+}
+
+#ifdef _USE_FLOAT_
+float OSCMessage::getFloat(uint16_t _index){
+
+ float *value;
+ if(_index > argsNum) _index=argsNum-1;
+ value = (float *)arguments[_index];
+
+ return *value;
+
+
+}
+#endif
+
+#ifdef _USE_STRING_
+char * OSCMessage::getString(uint16_t _index){
+
+ if(_index > argsNum) _index=argsNum-1;
+
+ return (char *)arguments[_index];
+
+}
+#endif
+
+uint16_t OSCMessage::getAllPackSize(){
+
+ return oscAdrPacSize+typeTagPacSize+argsPacSize;
+}
+
+
+uint16_t OSCMessage::getStrPackSize(const char* data){
+
+ return getPackSize(strlen(data));
+}
+
+uint16_t OSCMessage::getPackSize(uint16_t size){
+
+ return (size+4)&0xfffc;
+}
+
+
+
+
diff --git a/Libraries/OSC/OSCMessage.h b/Libraries/OSC/OSCMessage.h
new file mode 100644
index 0000000..2660b91
--- /dev/null
+++ b/Libraries/OSC/OSCMessage.h
@@ -0,0 +1,104 @@
+/*
+
+ ArdOSC - OSC Library for Arduino.
+
+ -------- Lisence -----------------------------------------------------------
+
+ ArdOSC
+
+ The MIT License
+
+ Copyright (c) 2009 - 2010 recotana( http://recotana.com ) All right reserved
+
+ */
+
+
+
+
+#ifndef OSCMessage_h
+#define OSCMessage_h
+
+
+#include "OSCcommon.h"
+
+
+class OSCMessage{
+
+private:
+
+ uint16_t allPackSize; //
+ char *oscAddress; // "/ard"
+ uint16_t oscAdrSize; // "/ard" ->4
+ uint16_t oscAdrPacSize; // "/ard\0\0\0\0" ->8
+
+ char *typeTag; // "iif"
+ uint16_t typeTagSize; // ",iif" -> 4
+ uint16_t typeTagPacSize; // ",iif\0\0\0\0" -> 8
+
+
+ void **arguments;
+ uint16_t argsNum; // "iif" -> 3
+ uint16_t argsPacSize; // "iif" -> 4 + 4 + 4 = 12
+
+ uint8_t ipAddress[4]; //{192,168,0,10}
+ uint16_t portNumber; //10000
+
+public:
+
+
+
+ OSCMessage();
+ OSCMessage(const char *_oscAddress);
+ ~OSCMessage();
+
+ void flush();
+
+
+ uint16_t getAllPackSize();
+ uint16_t getStrPackSize(const char* data);
+ uint16_t getPackSize(uint16_t size);
+
+
+
+ void setAddress(uint8_t *_ip , uint16_t _port);
+
+ void setIpAddress( uint8_t *_ip );
+ void setIpAddress( uint8_t _ip1, uint8_t _ip2, uint8_t _ip3, uint8_t _ip4);
+
+ uint8_t *getIpAddress();
+ void setPortNumber(uint16_t _port);
+ uint16_t getPortNumber();
+
+ int16_t setOSCMessage( const char *_address ,char *types , ... );
+ int16_t getArgsNum();
+
+ int16_t setOSCAddress(const char *_address);
+ char *getOSCAddress();
+
+ int16_t setTypeTags(const char *_tags);
+ char *getTypeTags();
+ char getTypeTag(uint16_t _index);
+
+
+
+ int32_t getInteger32(uint16_t _index);
+
+#ifdef _USE_FLOAT_
+ float getFloat(uint16_t _index);
+#endif
+
+#ifdef _USE_STRING_
+ char *getString(uint16_t _index);
+#endif
+
+
+
+ friend class OSCServer;
+ friend class OSCClient;
+ friend class OSCDecoder;
+ friend class OSCEncoder;
+
+};
+
+
+#endif
diff --git a/Libraries/OSC/OSCcommon.h b/Libraries/OSC/OSCcommon.h
new file mode 100644
index 0000000..443cf86
--- /dev/null
+++ b/Libraries/OSC/OSCcommon.h
@@ -0,0 +1,60 @@
+/*
+
+ ArdOSC - OSC Library for Arduino.
+
+ -------- Lisence -----------------------------------------------------------
+
+ ArdOSC
+
+ The MIT License
+
+ Copyright (c) 2009 - 2010 recotana( http://recotana.com ) All right reserved
+
+ */
+
+
+
+#ifndef OSCcommon_h
+#define OSCcommon_h
+
+//======== user define ==============
+
+//#define _DEBUG_
+
+#define _USE_FLOAT_
+#define _USE_STRING_
+
+
+
+//======== user define end ========
+
+#define kMaxAugument 16
+
+#define kMaxRecieveData 100
+#define kMaxOSCAdrCharactor 255
+#define kMaxStringCharactor 255
+
+
+
+extern "C" {
+#include <inttypes.h>
+}
+
+
+#ifdef _DEBUG_
+#include "HardwareSerial.h"
+#endif
+
+#ifdef _DEBUG_
+#define DBG_LOGLN(...) Serial.println(__VA_ARGS__)
+#define DBG_LOG(...) Serial.print(__VA_ARGS__)
+
+#else
+#define DBG_LOGLN
+#define DBG_LOG
+#endif
+
+
+
+
+#endif \ No newline at end of file