File: oscpacketizer.cpp

package info (click to toggle)
qlcplus 4.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,644 kB
  • sloc: cpp: 182,867; javascript: 7,764; xml: 2,453; ansic: 2,120; sh: 1,716; python: 634; ruby: 606; makefile: 23
file content (280 lines) | stat: -rw-r--r-- 8,943 bytes parent folder | download | duplicates (2)
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
/*
  Q Light Controller Plus
  oscpacketizer.cpp

  Copyright (c) Massimo Callegari

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "oscpacketizer.h"

#include <QStringList>
#include <QDebug>

OSCPacketizer::OSCPacketizer()
{
}

OSCPacketizer::~OSCPacketizer()
{
}

/*********************************************************************
 * Sender functions
 *********************************************************************/

void OSCPacketizer::setupOSCDmx(QByteArray &data, quint32 universe, quint32 channel, uchar value)
{
    data.clear();
    QString path = QString("/%1/dmx/%2").arg(universe).arg(channel);
    data.append(path.toUtf8());

    // add trailing zeros to reach a multiple of 4
    int zeroNumber = 4 - (path.length() % 4);
    if (zeroNumber > 0)
        data.append(QByteArray(zeroNumber, 0x00));

    data.append(",f");
    data.append((char)0x00);
    data.append((char)0x00);
    // Output value
    float fVal = (float)value / 255.f;
    data.append(*(((char *)&fVal) + 3));
    data.append(*(((char *)&fVal) + 2));
    data.append(*(((char *)&fVal) + 1));
    data.append(*(((char *)&fVal) + 0));
}

void OSCPacketizer::setupOSCGeneric(QByteArray &data, QString &path, QString types, QByteArray &values)
{
    data.clear();
    if (path.isEmpty())
    {
        qDebug() << Q_FUNC_INFO << "Empty path, can't create packet";
        return;
    }

    data.append(path.toUtf8());
    // add trailing zeros to reach a multiple of 4
    int zeroNumber = 4 - (path.length() % 4);
    if (zeroNumber > 0)
        data.append(QByteArray(zeroNumber, 0x00));

    data.append(",");
    data.append(types.toUtf8());

    zeroNumber = 4 - ((types.length() + 1) % 4);
    if (zeroNumber > 0)
        data.append(QByteArray(zeroNumber, 0x00));

    for (int i = 0; i < types.length() && i < values.length(); i++)
    {
        if (types.at(i) == 'f')
        {
            uchar val = (uchar)values.at(i);
            float fVal = (float)val / 255.f;
            data.append(*(((char *)&fVal) + 3));
            data.append(*(((char *)&fVal) + 2));
            data.append(*(((char *)&fVal) + 1));
            data.append(*(((char *)&fVal) + 0));
        }
    }
}

/*********************************************************************
 * Receiver functions
 *********************************************************************/
bool OSCPacketizer::parseMessage(QByteArray const& data, QString& path, QByteArray& values)
{
    path.clear();
    values.clear();

    QList<TagType> typeArray;
    bool tagsEnded = false;

    //qDebug() << "[OSC] data:" << data;

    // first of all look for a comma
    int commaPos = data.indexOf(0x2C);
    if (commaPos == -1)
        return false;

    path = QString(data.mid(0, commaPos));
    // remove possible trailing zeroes used for padding
    path.remove(QChar(0x00));
    //qDebug() << "[OSC] path extracted:" << path;

    int currPos = commaPos + 1;
    while (tagsEnded == false)
    {
        switch (data.at(currPos))
        {
            case '\0': tagsEnded = true; break;
            case 'b': typeArray.append(BlobTag); break;
            case 'f': typeArray.append(FloatTag); break;
            case 'd': typeArray.append(DoubleTag); break;
            case 'i': typeArray.append(IntegerTag); break;
            case 's': typeArray.append(StringTag); break;
            case 't': typeArray.append(TimeTag); break;
            default: break;
        }
        currPos++;
    }
    // round current position to 4
    int left = (typeArray.count() + 1) % 4;
    currPos += 3 - left;

    //qDebug () << "[OSC] Tags found:" << typeArray.count() << "currpos at" << currPos;

    foreach (TagType tag, typeArray)
    {
        switch (tag)
        {
            case IntegerTag:
            {
                if (currPos + 4 > data.size())
                    break;
                quint32 iVal;

                *((uchar*)(&iVal) + 3) = data.at(currPos);
                *((uchar*)(&iVal) + 2) = data.at(currPos + 1);
                *((uchar*)(&iVal) + 1) = data.at(currPos + 2);
                *((uchar*)(&iVal) + 0) = data.at(currPos + 3);

                if (iVal < 256)
                    values.append((char)iVal);
                else
                    values.append((char)(iVal / 0xFFFFFF));

                qDebug() << "[OSC] iVal:" << iVal;
                currPos += 4;
            }
            break;
            case FloatTag:
            {
                if (currPos + 4 > data.size())
                    break;
                float fVal;

                *((uchar*)(&fVal) + 3) = data.at(currPos);
                *((uchar*)(&fVal) + 2) = data.at(currPos + 1);
                *((uchar*)(&fVal) + 1) = data.at(currPos + 2);
                *((uchar*)(&fVal) + 0) = data.at(currPos + 3);

                values.append((char)(255.0 * fVal));

                qDebug() << "[OSC] fVal:" << fVal;

                currPos += 4;
            }
            break;
            case DoubleTag:
            {
                if (currPos + 8 > data.size())
                    break;

                double dVal;
                *((uchar*)(&dVal) + 7) = data.at(currPos);
                *((uchar*)(&dVal) + 6) = data.at(currPos + 1);
                *((uchar*)(&dVal) + 5) = data.at(currPos + 2);
                *((uchar*)(&dVal) + 4) = data.at(currPos + 3);
                *((uchar*)(&dVal) + 3) = data.at(currPos + 4);
                *((uchar*)(&dVal) + 2) = data.at(currPos + 5);
                *((uchar*)(&dVal) + 1) = data.at(currPos + 6);
                *((uchar*)(&dVal) + 0) = data.at(currPos + 7);

                values.append((char)(255.0 * dVal));

                qDebug() << "[OSC] dVal:" << dVal;
                currPos += 8;
            }
            break;
            case StringTag:
            {
                int firstZeroPos = data.indexOf('\0', currPos);
                QString str = QString(data.mid(currPos, firstZeroPos - currPos));
                qDebug() << "[OSC] string:" << str;
                // align current position to a multiple of 4
                int zeroNumber = 4 - (str.length() % 4);
                currPos = firstZeroPos + zeroNumber;
            }
            break;
            case TimeTag:
            {
                // A OSC timestamp would be helpful to defer
                // value changes, but since QLC+ plugins don't support
                // such thing, let's skip it.
                currPos += 8;
            }
            break;
            default: break;
        }
    }

    return true;
}

QList<QPair<QString, QByteArray> > OSCPacketizer::parsePacket(QByteArray const& data)
{
    int bufPos = 0;
    QList<QPair<QString, QByteArray> > messages;

    while (bufPos < data.size())
    {
        QString path;
        QByteArray values;

        // check wether we need to parse a bundle or a single message
        if (data.at(bufPos) == '#')
        {
            if (data.size() < 20 || data.startsWith("#bundle") == false)
            {
                qWarning() << "[OSC] Found an unsupported message type!" << data;
                return messages;
            }
            // 8 bytes for '#bundle\0' and 8 bytes for a timestamp that we don't handle
            bufPos += 16;

            // now, a bundle can contain another bundle or a message, starting with the message size
            // Check where we are here
            while (bufPos < data.size() && data.at(bufPos) != '#')
            {
                quint32 msgSize = (uchar(data.at(bufPos)) << 24) + (uchar(data.at(bufPos + 1)) << 16) + (uchar(data.at(bufPos + 2)) << 8) + uchar(data.at(bufPos + 3));
                qDebug() << "[OSC] Bundle message size:" << msgSize;
                bufPos += 4;

                if (data.size() >= bufPos + (int)msgSize)
                {
                    QByteArray msgData = data.mid(bufPos, msgSize);
                    if (parseMessage(msgData, path, values) == true)
                        messages.append(QPair<QString, QByteArray>(path, values));
                }
                bufPos += msgSize;
            }
            //qDebug() << "Buf pos=" << bufPos << "data size=" << data.size();
        }
        else
        {
            if (parseMessage(data, path, values) == true)
                messages.append(QPair<QString, QByteArray>(path, values));

            bufPos += data.size();
        }
    }

    return messages;
}