Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 andreas 1
/*
86 andreas 2
 * Copyright (C) 2018 to 2022 by Andreas Theofilu <andreas@theosys.at>
11 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <sys/utsname.h>
147 andreas 20
 
11 andreas 21
#include <functional>
22
#include <iostream>
23
#include <fstream>
24
#include <cstring>
25
#include <string>
26
#include <chrono>
27
#include <thread>
169 andreas 28
#include <mutex>
15 andreas 29
#include <map>
11 andreas 30
 
31
#include <sys/stat.h>
32
#include <sys/types.h>
33
#include <unistd.h>
34
 
35
#include "tamxnet.h"
36
#include "terror.h"
37
#include "tconfig.h"
38
#include "tdirectory.h"
39
#include "tresources.h"
40
#include "texpand.h"
86 andreas 41
#include "tsocket.h"
89 andreas 42
#include "texcept.h"
142 andreas 43
#include "tpagemanager.h"
11 andreas 44
 
45
using namespace amx;
46
using namespace std;
169 andreas 47
using std::mutex;
11 andreas 48
 
49
using placeholders::_1;
50
using placeholders::_2;
51
 
52
string cmdList[] =
53
{
54
    "@WLD-", "@AFP-", "@GCE-", "@APG-", "@CPG-", "@DPG-", "@PDR-", "@PHE-",
55
    "@PHP-", "@PHT-", "@PPA-", "@PPF-", "@PPG-", "@PPK-", "@PPM-", "@PPN-",
56
    "@PPT-", "@PPX", "@PSE-", "@PSP-", "@PST-", "PAGE-", "PPOF-", "PPOG-",
57
    "PPON-", "^ANI-", "^APF-", "^BAT-", "^BAU-", "^BCB-", "^BCF-", "^BCT-",
58
    "^BDO-", "^BFB-", "^BIM-", "^BLN-", "^BMC-", "^BMF-", "^BMI-", "^BML-",
59
    "^BMP-", "^BNC-", "^BNN-", "^BNT-", "^BOP-", "^BOR-", "^BOS-", "^BPP-",
60
    "^BRD-", "^BSF-", "^BSP-", "^BSM-", "^BSO-", "^BVL-", "^BVN-", "^BVP-",
61
    "^BVT-", "^BWW-", "^CPF-", "^DLD-", "^DPF-", "^ENA-", "^FON-", "^GDI-",
62
    "^GIV-", "^GLH-", "^GLL-", "^GRD-", "^GRU-", "^GSC-", "^GSN-", "^ICO-",
63
    "^IRM-", "^JSB-", "^JSI-", "^JST-", "^MBT-", "^MDC-", "^SHO-", "^TEC-",
64
    "^TEF-", "^TOP-", "^TXT-", "^UNI-", "^LPC-", "^LPR-", "^LPS-", "?BCB-",
65
    "?BCF-", "?BCT-", "?BMP-", "?BOP-", "?BRD-", "?BWW-", "?FON-", "?ICO-",
66
    "?JSB-", "?JSI-", "?JST-", "?TEC-", "?TEF-", "?TXT-", "ABEEP", "ADBEEP",
67
    "@AKB-", "AKEYB-", "AKEYP-", "AKEYR-", "@AKP-", "@AKR", "BEEP", "BRIT-",
68
    "@BRT-", "DBEEP", "@EKP-", "PKEYP-", "@PKP-", "SETUP", "SHUTDOWN", "SLEEP",
69
    "@SOU-", "@TKP-", "TPAGEON", "TPAGEOFF", "@VKB", "WAKE", "^CAL", "^KPS-",
70
    "^VKS-", "@PWD-", "^PWD-", "^BBR-", "^RAF-", "^RFR-", "^RMF-", "^RSR-",
71
    "^MODEL?", "^ICS-", "^ICE-", "^ICM-", "^PHN-", "?PHN-", "LEVON", "RXON",
300 andreas 72
    "BLINK", "TPCCMD", "TPCACC", "^EPR", "^SCE", "^SDR", "^SHD", "^SSH",
73
    "^STG",
104 andreas 74
    // G5 commands
233 andreas 75
    "^ABP-", "^ADB", "^SOU-", "^STP", "^TKP-", "^PGE-", "^PPA-", "^PPF-",
76
    "^PPG-", "^PPK-", "^PPM-", "^PPN-", "^PPT-", "^PPX", "^UTF-", "^LVC-",
77
    "^LVD-", "^LVE-", "^LVF-", "^LVL-", "^LVM-", "^LVN-", "^LVR-", "^LVS-",
104 andreas 78
    "\0"
11 andreas 79
};
80
 
81
#define NUMBER_CMDS     144
82
 
129 andreas 83
std::atomic<bool> killed{false};
84
std::atomic<bool> _netRunning{false};
14 andreas 85
amx::TAmxNet *gAmxNet = nullptr;
92 andreas 86
static bool __CommValid = false;
87 andreas 87
std::map<ulong, FUNC_NETWORK_t> mFuncsNetwork;
88
std::map<ulong, FUNC_TIMER_t> mFuncsTimer;
89
 
169 andreas 90
//std::vector<ANET_COMMAND> TAmxNet::comStack; // commands to answer
142 andreas 91
extern TPageManager *gPageManager;
92
 
169 andreas 93
//mutex read_mutex;
94
 
11 andreas 95
TAmxNet::TAmxNet()
96
{
97
    DECL_TRACER("TAmxNet::TAmxNet()");
92 andreas 98
 
11 andreas 99
    init();
100
}
101
 
102
TAmxNet::TAmxNet(const string& sn)
86 andreas 103
    : serNum(sn)
11 andreas 104
{
105
    DECL_TRACER("TAmxNet::TAmxNet(const string& sn)");
92 andreas 106
 
11 andreas 107
    init();
108
}
109
 
110
TAmxNet::TAmxNet(const string& sn, const string& nm)
86 andreas 111
    : panName(nm),
11 andreas 112
      serNum(sn)
113
{
114
    DECL_TRACER("TAmxNet::TAmxNet(const string& sn)");
92 andreas 115
 
11 andreas 116
    size_t pos = nm.find(" (TPC)");
117
 
118
    if (pos != string::npos)
119
    {
120
        panName = nm.substr(0, pos) + "i";
121
        MSG_TRACE("Converted TP name: " << panName);
122
    }
123
 
124
    init();
125
}
126
 
127
TAmxNet::~TAmxNet()
128
{
129
    DECL_TRACER("TAmxNet::~TAmxNet()");
92 andreas 130
 
11 andreas 131
    callback = 0;
169 andreas 132
    write_busy = false;
11 andreas 133
    stop();
92 andreas 134
 
135
    if (mSocket)
136
    {
137
        delete mSocket;
138
        mSocket = nullptr;
139
        __CommValid = false;
140
    }
141
 
70 andreas 142
    gAmxNet = nullptr;
11 andreas 143
}
144
 
92 andreas 145
void TAmxNet::init()
11 andreas 146
{
147
    DECL_TRACER("TAmxNet::init()");
148
 
149
    sendCounter = 0;
150
    initSend = false;
151
    ready = false;
89 andreas 152
 
92 andreas 153
    callback = 0;
154
    stopped_ = false;
155
    write_busy = false;
156
    gAmxNet = this;
157
 
158
    if (!mSocket)
89 andreas 159
    {
92 andreas 160
        mSocket = new TSocket;
161
        __CommValid = true;
89 andreas 162
    }
163
 
11 andreas 164
    string version = "v2.01.00";        // A version > 2.0 is needed for file transfer!
165
    int devID = 0x0163, fwID = 0x0290;
166
 
167
    if (TConfig::getPanelType().length() > 0)
168
        panName = TConfig::getPanelType();
169
    else if (panName.empty())
170
        panName.assign("TheoSys");
171
 
172
    if (panName.find("MVP") != string::npos && panName.find("5200") != string::npos)
173
    {
174
        devID = 0x0149;
175
        fwID = 0x0310;
176
    }
169 andreas 177
    else if (panName.find("NX-CV7") != string::npos)
178
    {
179
        devID = 0x0123;
180
        fwID = 0x0135;
181
    }
11 andreas 182
 
183
    // Initialize the devive info structure
184
    DEVICE_INFO di;
185
    // Answer to MC = 0x0017 --> MC = 0x0097
186
    di.objectID = 0;
187
    di.parentID = 0;
188
    di.manufacturerID = 1;
189
    di.deviceID = devID;
169 andreas 190
    memset(di.serialNum, 0, sizeof(di.serialNum));
11 andreas 191
 
192
    if (!serNum.empty())
193
        memcpy(di.serialNum, serNum.c_str(), serNum.length());
194
 
195
    di.firmwareID = fwID;
196
    memset(di.versionInfo, 0, sizeof(di.versionInfo));
197
    strncpy(di.versionInfo, version.c_str(), version.length());
198
    memset(di.deviceInfo, 0, sizeof(di.deviceInfo));
199
    strncpy(di.deviceInfo, panName.c_str(), min(panName.length(), sizeof(di.deviceInfo) - 1));
200
    memset(di.manufacturerInfo, 0, sizeof(di.manufacturerInfo));
201
    strncpy(di.manufacturerInfo, "TheoSys", 7);
202
    di.format = 2;
203
    di.len = 4;
204
    memset(di.addr, 0, sizeof(di.addr));
205
    devInfo.push_back(di);
206
    // Kernel info
207
    di.objectID = 2;
208
    di.firmwareID = fwID + 1;
209
    memset(di.serialNum, 0x20, sizeof(di.serialNum));
210
    memcpy(di.serialNum, "N/A", 3);
211
    memset(di.deviceInfo, 0, sizeof(di.deviceInfo));
212
    strncpy(di.deviceInfo, "Kernel", 6);
213
    memset(di.versionInfo, 0, sizeof(di.versionInfo));
214
#ifdef __linux__
215
    struct utsname kinfo;
216
    uname(&kinfo);
217
    strncpy(di.versionInfo, kinfo.release, sizeof(di.versionInfo));
134 andreas 218
    char *ppos = strnstr(di.versionInfo, "-", sizeof(di.versionInfo));
219
 
220
    if (ppos && (ppos - di.versionInfo) < 16)
221
        *ppos = 0;
11 andreas 222
#else
223
    strncpy(di.versionInfo, "4.00.00", 7);
224
#endif
225
    devInfo.push_back(di);
226
}
227
 
15 andreas 228
void TAmxNet::registerNetworkState(function<void (int)> registerNetwork, ulong handle)
229
{
230
    DECL_TRACER("TAmxNet::registerNetworkState(function<void (int)> registerNetwork, ulong handle)");
231
 
232
    map<ulong, FUNC_NETWORK_t>::iterator iter = mFuncsNetwork.find(handle);
233
 
83 andreas 234
    if (mFuncsNetwork.size() == 0 || iter == mFuncsNetwork.end())
15 andreas 235
    {
236
        FUNC_NETWORK_t fn;
237
        fn.handle = handle;
238
        fn.func = registerNetwork;
239
        mFuncsNetwork.insert(pair<ulong, FUNC_NETWORK_t>(handle, fn));
240
    }
241
 
88 andreas 242
    registerNetwork((isRunning() ? NSTATE_ONLINE : NSTATE_OFFLINE));
15 andreas 243
}
244
 
245
void TAmxNet::registerTimer(function<void (const ANET_BLINK &)> registerBlink, ulong handle)
246
{
247
    DECL_TRACER("TAmxNet::registerTimer(function<void (const ANET_BLINK &)> registerBlink, ulong handle)");
248
 
249
    map<ulong, FUNC_TIMER_t>::iterator iter = mFuncsTimer.find(handle);
250
 
83 andreas 251
    if (mFuncsTimer.size() == 0 || iter == mFuncsTimer.end())
15 andreas 252
    {
253
        FUNC_TIMER_t ft;
254
        ft.handle = handle;
255
        ft.func = registerBlink;
256
        mFuncsTimer.insert(pair<ulong, FUNC_TIMER_t>(handle, ft));
257
    }
258
}
259
 
260
void TAmxNet::deregNetworkState(ulong handle)
261
{
262
    DECL_TRACER("TAmxNet::deregNetworkState(ulong handle)");
263
 
83 andreas 264
    if (mFuncsNetwork.size() == 0)
265
        return;
266
 
15 andreas 267
    map<ulong, FUNC_NETWORK_t>::iterator iter = mFuncsNetwork.find(handle);
268
 
17 andreas 269
    if (iter != mFuncsNetwork.end())
15 andreas 270
        mFuncsNetwork.erase(iter);
271
}
272
 
273
void TAmxNet::deregTimer(ulong handle)
274
{
275
    DECL_TRACER("TAmxNet::deregTimer(ulong handle)");
276
 
83 andreas 277
    if (mFuncsTimer.size() == 0)
278
        return;
279
 
15 andreas 280
    map<ulong, FUNC_TIMER_t>::iterator iter = mFuncsTimer.find(handle);
281
 
17 andreas 282
    if (iter != mFuncsTimer.end())
15 andreas 283
        mFuncsTimer.erase(iter);
284
}
285
 
88 andreas 286
void TAmxNet::stop(bool soft)
11 andreas 287
{
86 andreas 288
    DECL_TRACER("TAmxNet::stop: Stopping the client...");
83 andreas 289
 
86 andreas 290
    if (stopped_)
83 andreas 291
        return;
292
 
88 andreas 293
    if (!soft)
294
        stopped_ = true;
295
 
92 andreas 296
    if (mSocket)
297
        mSocket->close();
11 andreas 298
}
299
 
93 andreas 300
bool TAmxNet::reconnect()
301
{
302
    DECL_TRACER("TAmxNet::reconnect()");
303
 
304
    if (!mSocket || !__CommValid)
305
        return false;
306
 
307
    mSocket->close();
308
    initSend = false;
309
    ready = false;
310
    return true;
311
}
312
 
88 andreas 313
bool TAmxNet::isNetRun()
314
{
315
    return _netRunning;
316
}
317
 
86 andreas 318
void TAmxNet::Run()
11 andreas 319
{
86 andreas 320
    DECL_TRACER("TAmxNet::Run()");
14 andreas 321
 
92 andreas 322
    if (_netRunning || !__CommValid)
11 andreas 323
        return;
324
 
88 andreas 325
    _netRunning = true;
86 andreas 326
    stopped_ = false;
327
    _retry = false;
11 andreas 328
 
329
    try
330
    {
86 andreas 331
        mThread = std::thread([=] { this->start(); });
332
        mThread.detach();
11 andreas 333
    }
334
    catch (std::exception& e)
335
    {
86 andreas 336
        MSG_ERROR("Error connecting to " << TConfig::getController() << ":" << to_string(TConfig::getPort()) << " [" << e.what() << "]");
337
        _netRunning = false;
11 andreas 338
    }
339
}
340
 
88 andreas 341
/*
342
 * This function is running as a thread. It is the main method connecting to
343
 * a controller and it handles all the communication with the controller.
344
 */
86 andreas 345
void TAmxNet::start()
11 andreas 346
{
86 andreas 347
    DECL_TRACER("TAmxNet::start()");
11 andreas 348
 
86 andreas 349
    sendAllFuncNetwork(NSTATE_CONNECTING);
44 andreas 350
 
92 andreas 351
    while (__CommValid && mSocket && isRunning())
11 andreas 352
    {
93 andreas 353
        initSend = false;
354
        ready = false;
355
 
147 andreas 356
        if (__CommValid && TConfig::getController() == "0.0.0.0")
357
        {
150 andreas 358
            string controller = TConfig::getController();
359
            MSG_INFO("Refusing to connect to invalid controller " << controller);
147 andreas 360
            sendAllFuncNetwork(NSTATE_OFFLINE);
361
            std::this_thread::sleep_for(std::chrono::seconds(10));  // Wait 10 seconds before next try
362
            continue;
363
        }
364
 
92 andreas 365
        if (__CommValid && mSocket && !mSocket->connect(TConfig::getController(), TConfig::getPort()))
11 andreas 366
        {
87 andreas 367
            MSG_DEBUG("Connection failed. Retrying ...");
86 andreas 368
            sendAllFuncNetwork(NSTATE_OFFLINE);
93 andreas 369
            std::this_thread::sleep_for(std::chrono::seconds(mWaitTime));
370
            setWaitTime(WAIT_RESET);
86 andreas 371
            continue;
11 andreas 372
        }
92 andreas 373
        else if (!mSocket)
374
            break;
11 andreas 375
 
86 andreas 376
        sendAllFuncNetwork(NSTATE_ONLINE);
142 andreas 377
 
378
        if (__CommValid && isRunning() && gPageManager && gPageManager->getRepaintWindows())
379
            gPageManager->getRepaintWindows()();
380
 
86 andreas 381
        handle_connect();
93 andreas 382
        std::this_thread::sleep_for(std::chrono::seconds(mWaitTime));
383
        setWaitTime(WAIT_RESET);
384
        MSG_INFO("Network will be reestablished ...");
11 andreas 385
    }
386
 
86 andreas 387
    _netRunning = false;
11 andreas 388
}
389
 
86 andreas 390
void TAmxNet::handle_connect()
11 andreas 391
{
86 andreas 392
    DECL_TRACER("TAmxNet::handle_connect()");
11 andreas 393
 
86 andreas 394
    try
11 andreas 395
    {
92 andreas 396
        while (__CommValid && mSocket && isRunning() && mSocket->isConnected())
86 andreas 397
        {
398
            // Start the input actor.
399
            start_read();
11 andreas 400
 
86 andreas 401
            // Start the output actor.
169 andreas 402
            if (isRunning() && !write_busy)
403
                runWrite();
86 andreas 404
        }
11 andreas 405
 
86 andreas 406
        if (!stopped_ && (killed || prg_stopped))
407
            stop();
11 andreas 408
    }
86 andreas 409
    catch (std::exception& e)
11 andreas 410
    {
86 andreas 411
        MSG_ERROR("Error: " << e.what());
92 andreas 412
 
413
        if (mSocket)
414
            mSocket->close();
11 andreas 415
    }
93 andreas 416
    catch (TXceptComm& e)
89 andreas 417
    {
92 andreas 418
        if (mSocket)
419
            mSocket->close();
89 andreas 420
    }
93 andreas 421
 
422
    sendAllFuncNetwork(NSTATE_CONNECTING);
423
    setWaitTime(WAIT_RECONNECT);
11 andreas 424
}
425
 
426
void TAmxNet::start_read()
427
{
428
    DECL_TRACER("TAmxNet::start_read()");
429
 
92 andreas 430
    if (!__CommValid || !mSocket || !isRunning() || !mSocket->isConnected())
11 andreas 431
        return;
432
 
88 andreas 433
    string _message = "TAmxNet::start_read(): Invalid argument received!";
11 andreas 434
    protError = false;
435
    comm.clear();
436
 
89 andreas 437
    try
88 andreas 438
    {
89 andreas 439
        // Read the first byte. It should be 0x02
92 andreas 440
        if (mSocket->readAbsolut(buff_, 1) == 1)
89 andreas 441
            handle_read(1, RT_ID);
92 andreas 442
        else if (mSocket->isConnected())
89 andreas 443
        {
444
            _message.append(" [RT_ID]");
445
            XCEPTCOMM(_message);
446
        }
447
        else
93 andreas 448
        {
449
            setWaitTime(WAIT_RECONNECT);
89 andreas 450
            return;
93 andreas 451
        }
11 andreas 452
 
92 andreas 453
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 454
            handle_read(2, RT_LEN);
92 andreas 455
        else if (mSocket->isConnected())
89 andreas 456
        {
457
            _message.append(" [RT_LEN]");
458
            XCEPTCOMM(_message);
459
        }
460
        else
93 andreas 461
        {
462
            setWaitTime(WAIT_RECONNECT);
89 andreas 463
            return;
93 andreas 464
        }
11 andreas 465
 
92 andreas 466
        if (mSocket->readAbsolut(buff_, 1) == 1)
89 andreas 467
            handle_read(1, RT_SEP1);
92 andreas 468
        else if (mSocket->isConnected())
89 andreas 469
        {
470
            _message.append(" [RT_SEP1]");
471
            XCEPTCOMM(_message);
472
        }
473
        else
93 andreas 474
        {
475
            setWaitTime(WAIT_RECONNECT);
89 andreas 476
            return;
93 andreas 477
        }
11 andreas 478
 
92 andreas 479
        if (mSocket->readAbsolut(buff_, 1) == 1)
89 andreas 480
            handle_read(1, RT_TYPE);
92 andreas 481
        else if (mSocket->isConnected())
89 andreas 482
        {
483
            _message.append(" [RT_TYPE]");
484
            XCEPTCOMM(_message);
485
        }
486
        else
93 andreas 487
        {
488
            setWaitTime(WAIT_RECONNECT);
89 andreas 489
            return;
93 andreas 490
        }
11 andreas 491
 
92 andreas 492
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 493
            handle_read(2, RT_WORD1);
92 andreas 494
        else if (mSocket->isConnected())
89 andreas 495
        {
496
            _message.append(" [RT_WORD1]");
497
            XCEPTCOMM(_message);
498
        }
499
        else
93 andreas 500
        {
501
            setWaitTime(30);
89 andreas 502
            return;
93 andreas 503
        }
11 andreas 504
 
92 andreas 505
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 506
            handle_read(2, RT_DEVICE);
92 andreas 507
        else if (mSocket->isConnected())
89 andreas 508
        {
509
            _message.append(" [RT_DEVIVE]");
510
            XCEPTCOMM(_message);
511
        }
512
        else
93 andreas 513
        {
514
            setWaitTime(WAIT_RECONNECT);
89 andreas 515
            return;
93 andreas 516
        }
11 andreas 517
 
92 andreas 518
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 519
            handle_read(2, RT_WORD2);
92 andreas 520
        else if (mSocket->isConnected())
89 andreas 521
        {
522
            _message.append(" [RT_WORD2]");
523
            XCEPTCOMM(_message);
524
        }
525
        else
93 andreas 526
        {
527
            setWaitTime(WAIT_RECONNECT);
89 andreas 528
            return;
93 andreas 529
        }
11 andreas 530
 
92 andreas 531
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 532
            handle_read(2, RT_WORD3);
92 andreas 533
        else if (mSocket->isConnected())
89 andreas 534
        {
535
            _message.append(" [RT_WORD3]");
536
            XCEPTCOMM(_message);
537
        }
538
        else
93 andreas 539
        {
540
            setWaitTime(WAIT_RECONNECT);
89 andreas 541
            return;
93 andreas 542
        }
11 andreas 543
 
92 andreas 544
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 545
            handle_read(2, RT_WORD4);
92 andreas 546
        else if (mSocket->isConnected())
89 andreas 547
        {
548
            _message.append(" [RT_WORD4]");
549
            XCEPTCOMM(_message);
550
        }
551
        else
93 andreas 552
        {
553
            setWaitTime(WAIT_RECONNECT);
89 andreas 554
            return;
93 andreas 555
        }
11 andreas 556
 
92 andreas 557
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 558
            handle_read(2, RT_WORD5);
92 andreas 559
        else if (mSocket->isConnected())
89 andreas 560
        {
561
            _message.append(" [RT_WORD5]");
562
            XCEPTCOMM(_message);
563
        }
564
        else
93 andreas 565
        {
566
            setWaitTime(WAIT_RECONNECT);
89 andreas 567
            return;
93 andreas 568
        }
11 andreas 569
 
92 andreas 570
        if (mSocket->readAbsolut(buff_, 1) == 1)
89 andreas 571
            handle_read(1, RT_SEP2);
92 andreas 572
        else if (mSocket->isConnected())
89 andreas 573
        {
574
            _message.append(" [RT_SEP2]");
575
            XCEPTCOMM(_message);
576
        }
577
        else
93 andreas 578
        {
579
            setWaitTime(WAIT_RECONNECT);
89 andreas 580
            return;
93 andreas 581
        }
11 andreas 582
 
92 andreas 583
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 584
            handle_read(2, RT_COUNT);
92 andreas 585
        else if (mSocket->isConnected())
89 andreas 586
        {
587
            _message.append(" [RT_COUNT]");
588
            XCEPTCOMM(_message);
589
        }
590
        else
93 andreas 591
        {
592
            setWaitTime(WAIT_RECONNECT);
89 andreas 593
            return;
93 andreas 594
        }
11 andreas 595
 
92 andreas 596
        if (mSocket->readAbsolut(buff_, 2) == 2)
89 andreas 597
            handle_read(2, RT_MC);
92 andreas 598
        else if (mSocket->isConnected())
89 andreas 599
        {
600
            _message.append(" [RT_MC]");
601
            XCEPTCOMM(_message);
602
        }
603
        else
93 andreas 604
        {
605
            setWaitTime(WAIT_RECONNECT);
89 andreas 606
            return;
93 andreas 607
        }
11 andreas 608
 
89 andreas 609
        // Calculate the length of the data block. This is the rest of the total length
610
        size_t len = (comm.hlen + 3) - 0x0015;
11 andreas 611
 
92 andreas 612
        if (mSocket->isConnected() && len > BUF_SIZE)
89 andreas 613
        {
614
            _message = "Length to read is " + to_string(len) + " bytes, but the buffer is only " + to_string(BUF_SIZE) + " bytes!";
615
            XCEPTCOMM(_message);
616
        }
92 andreas 617
        else if (!mSocket->isConnected())
93 andreas 618
        {
619
            setWaitTime(WAIT_RECONNECT);
89 andreas 620
            return;
93 andreas 621
        }
89 andreas 622
 
92 andreas 623
        if (mSocket->readAbsolut(buff_, len) == len)
89 andreas 624
            handle_read(len, RT_DATA);
92 andreas 625
        else if (mSocket->isConnected())
89 andreas 626
        {
627
            _message.append(" [RT_DATA]");
628
            XCEPTCOMM(_message);
629
        }
11 andreas 630
    }
89 andreas 631
    catch (TXceptNetwork& e)
88 andreas 632
    {
93 andreas 633
        setWaitTime(WAIT_RECONNECT);
88 andreas 634
    }
11 andreas 635
}
636
 
86 andreas 637
void TAmxNet::handle_read(size_t n, R_TOKEN tk)
11 andreas 638
{
639
    DECL_TRACER("TAmxNet::handle_read(const error_code& error, size_t n, R_TOKEN tk)");
640
 
92 andreas 641
    if (stopped_ || !__CommValid || !mSocket || !mSocket->isConnected())
11 andreas 642
        return;
643
 
21 andreas 644
    if (killed || prg_stopped)
11 andreas 645
    {
646
        stop();
647
        return;
648
    }
649
 
650
    uint32_t dw;
651
    int val, pos;
652
    size_t len;
653
    ANET_SEND s;        // Used to answer system requests
654
    string cmd;
655
 
86 andreas 656
    len = (n < BUF_SIZE) ? n : BUF_SIZE - 1;
657
    input_buffer_.assign((char *)&buff_[0], len);
11 andreas 658
 
86 andreas 659
    MSG_DEBUG("Token: " << tk << ", " << len << " bytes");
11 andreas 660
 
86 andreas 661
    switch (tk)
662
    {
663
        case RT_ID:
664
            if (buff_[0] != 0x02)
665
                protError = true;
666
            else
667
                comm.ID = buff_[0];
668
        break;
11 andreas 669
 
86 andreas 670
        case RT_LEN:    comm.hlen = makeWord(buff_[0], buff_[1]); break;
11 andreas 671
 
86 andreas 672
        case RT_SEP1:
673
            if (buff_[0] != 0x02)
674
                protError = true;
675
            else
676
                comm.sep1 = buff_[0];
677
        break;
11 andreas 678
 
86 andreas 679
        case RT_TYPE:   comm.type = buff_[0]; break;
680
        case RT_WORD1:  comm.unk1 = makeWord(buff_[0], buff_[1]); break;
681
        case RT_DEVICE: comm.device1 = makeWord(buff_[0], buff_[1]); break;
682
        case RT_WORD2:  comm.port1 = makeWord(buff_[0], buff_[1]); break;
683
        case RT_WORD3:  comm.system = makeWord(buff_[0], buff_[1]); break;
684
        case RT_WORD4:  comm.device2 = makeWord(buff_[0], buff_[1]); break;
685
        case RT_WORD5:  comm.port2 = makeWord(buff_[0], buff_[1]); break;
11 andreas 686
 
86 andreas 687
        case RT_SEP2:
688
            if (buff_[0] != 0x0f)
689
                protError = true;
690
            else
691
                comm.unk6 = buff_[0];
692
        break;
11 andreas 693
 
86 andreas 694
        case RT_COUNT:  comm.count = makeWord(buff_[0], buff_[1]); break;
695
        case RT_MC:     comm.MC = makeWord(buff_[0], buff_[1]); break;
11 andreas 696
 
86 andreas 697
        case RT_DATA:
698
            if (protError || !isRunning())
699
                break;
11 andreas 700
 
93 andreas 701
            MSG_DEBUG("Received message type: 0x" << std::setw(4) << std::setfill('0') << std::hex << comm.MC);
11 andreas 702
 
86 andreas 703
            switch (comm.MC)
704
            {
705
                case 0x0001:    // ACK
706
                case 0x0002:    // NAK
707
                    comm.checksum = buff_[0];
708
                break;
11 andreas 709
 
86 andreas 710
                case 0x0084:    // input channel ON
711
                case 0x0085:    // input channel OFF
712
                case 0x0006:    // output channel ON
713
                case 0x0086:    // output channel ON status
714
                case 0x0007:    // output channel OFF
715
                case 0x0087:    // output channel OFF status
716
                case 0x0088:    // input/output channel ON status
717
                case 0x0089:    // input/output channel OFF status
718
                case 0x0018:    // feedback channel ON
719
                case 0x0019:    // feedback channel OFF
720
                    comm.data.chan_state.device = makeWord(buff_[0], buff_[1]);
721
                    comm.data.chan_state.port = makeWord(buff_[2], buff_[3]);
722
                    comm.data.chan_state.system = makeWord(buff_[4], buff_[5]);
723
                    comm.data.chan_state.channel = makeWord(buff_[6], buff_[7]);
724
                    comm.checksum = buff_[8];
11 andreas 725
 
86 andreas 726
                    s.channel = comm.data.chan_state.channel;
727
                    s.level = 0;
728
                    s.port = comm.data.chan_state.port;
729
                    s.value = 0;
11 andreas 730
 
86 andreas 731
                    switch (comm.MC)
732
                    {
733
                        case 0x0006: s.MC = 0x0086; break;
734
                        case 0x0007: s.MC = 0x0087; break;
735
                    }
11 andreas 736
 
86 andreas 737
                    if (comm.MC < 0x0020)
738
                    {
11 andreas 739
                        if (callback)
740
                            callback(comm);
13 andreas 741
                        else
742
                            MSG_WARNING("Missing callback function!");
86 andreas 743
                    }
744
                    else
745
                        sendCommand(s);
746
                break;
11 andreas 747
 
86 andreas 748
                case 0x000a:    // level value change
749
                case 0x008a:
750
                    comm.data.message_value.device = makeWord(buff_[0], buff_[1]);
751
                    comm.data.message_value.port = makeWord(buff_[2], buff_[3]);
752
                    comm.data.message_value.system = makeWord(buff_[4], buff_[5]);
753
                    comm.data.message_value.value = makeWord(buff_[6], buff_[7]);
754
                    comm.data.message_value.type = buff_[8];
755
                    val = (int)buff_[8];
11 andreas 756
 
86 andreas 757
                    switch (val)
758
                    {
759
                        case 0x010: comm.data.message_value.content.byte = buff_[9]; comm.checksum = buff_[10]; break;
760
                        case 0x011: comm.data.message_value.content.ch = buff_[9]; comm.checksum = buff_[10]; break;
761
                        case 0x020: comm.data.message_value.content.integer = makeWord(buff_[9], buff_[10]); comm.checksum = buff_[11]; break;
762
                        case 0x021: comm.data.message_value.content.sinteger = makeWord(buff_[9], buff_[10]); comm.checksum = buff_[11]; break;
763
                        case 0x040: comm.data.message_value.content.dword = makeDWord(buff_[9], buff_[10], buff_[11], buff_[12]); comm.checksum = buff_[13]; break;
764
                        case 0x041: comm.data.message_value.content.sdword = makeDWord(buff_[9], buff_[10], buff_[11], buff_[12]); comm.checksum = buff_[13]; break;
11 andreas 765
 
86 andreas 766
                        case 0x04f:
767
                            dw = makeDWord(buff_[9], buff_[10], buff_[11], buff_[12]);
768
                            memcpy(&comm.data.message_value.content.fvalue, &dw, 4);
769
                            comm.checksum = buff_[13];
770
                        break;
11 andreas 771
 
86 andreas 772
                        case 0x08f:
773
                            memcpy(&comm.data.message_value.content.dvalue, &buff_[9], 8);  // FIXME: wrong byte order on Intel CPU?
774
                            comm.checksum = buff_[17];
775
                        break;
776
                    }
11 andreas 777
 
86 andreas 778
                    if (callback)
779
                        callback(comm);
780
                    else
781
                        MSG_WARNING("Missing callback function!");
782
                break;
11 andreas 783
 
86 andreas 784
                case 0x000b:    // string value change
785
                case 0x008b:
786
                case 0x000c:    // command string
787
                case 0x008c:
788
                    comm.data.message_string.device = makeWord(buff_[0], buff_[1]);
789
                    comm.data.message_string.port = makeWord(buff_[2], buff_[3]);
790
                    comm.data.message_string.system = makeWord(buff_[4], buff_[5]);
791
                    comm.data.message_string.type = buff_[6];
792
                    comm.data.message_string.length = makeWord(buff_[7], buff_[8]);
793
                    memset(&comm.data.message_string.content[0], 0, sizeof(comm.data.message_string.content));
794
                    len = (buff_[6] == 0x01) ? comm.data.message_string.length : comm.data.message_string.length * 2;
11 andreas 795
 
86 andreas 796
                    if (len >= sizeof(comm.data.message_string.content))
797
                    {
798
                        len = sizeof(comm.data.message_string.content) - 1;
799
                        comm.data.message_string.length = (buff_[6] == 0x01) ? len : len / 2;
800
                    }
11 andreas 801
 
86 andreas 802
                    memcpy(&comm.data.message_string.content[0], &buff_[9], len);
803
                    pos = (int)(len + 10);
804
                    comm.checksum = buff_[pos];
805
                    cmd.assign((char *)&comm.data.message_string.content[0], len);
806
                    MSG_DEBUG("cmd=" << cmd);
11 andreas 807
 
86 andreas 808
                    if (isCommand(cmd))
809
                    {
810
                        MSG_DEBUG("Command found!");
811
                        oldCmd.assign(cmd);
812
                    }
813
                    else
814
                    {
815
                        oldCmd.append(cmd);
816
                        MSG_DEBUG("Concatenated cmd=" << oldCmd);
817
                        memset(&comm.data.message_string.content[0], 0, sizeof(comm.data.message_string.content));
818
                        memcpy(&comm.data.message_string.content[0], oldCmd.c_str(), sizeof(comm.data.message_string.content) - 1);
819
                        comm.data.message_string.length = oldCmd.length();
820
                        oldCmd.clear();
821
                    }
11 andreas 822
 
86 andreas 823
                    if (callback)
824
                        callback(comm);
825
                    else
826
                        MSG_WARNING("Missing callback function!");
827
                break;
11 andreas 828
 
86 andreas 829
                case 0x000e:    // request level value
830
                    comm.data.level.device = makeWord(buff_[0], buff_[1]);
831
                    comm.data.level.port = makeWord(buff_[2], buff_[3]);
832
                    comm.data.level.system = makeWord(buff_[4], buff_[5]);
833
                    comm.data.level.level = makeWord(buff_[6], buff_[7]);
834
                    comm.checksum = buff_[8];
11 andreas 835
 
86 andreas 836
                    if (callback)
837
                        callback(comm);
838
                    else
839
                        MSG_WARNING("Missing callback function!");
840
                break;
11 andreas 841
 
86 andreas 842
                case 0x000f:    // request output channel status
843
                    comm.data.channel.device = makeWord(buff_[0], buff_[1]);
844
                    comm.data.channel.port = makeWord(buff_[2], buff_[3]);
845
                    comm.data.channel.system = makeWord(buff_[4], buff_[5]);
846
                    comm.data.channel.channel = makeWord(buff_[6], buff_[7]);
847
                    comm.checksum = buff_[8];
11 andreas 848
 
86 andreas 849
                    if (callback)
850
                        callback(comm);
851
                    else
852
                        MSG_WARNING("Missing callback function!");
853
                break;
11 andreas 854
 
86 andreas 855
                case 0x0010:    // request port count
856
                case 0x0017:    // request device info
857
                    comm.data.reqPortCount.device = makeWord(buff_[0], buff_[1]);
858
                    comm.data.reqPortCount.system = makeWord(buff_[2], buff_[3]);
859
                    comm.checksum = buff_[4];
860
                    s.channel = false;
861
                    s.level = 0;
862
                    s.port = 0;
863
                    s.value = 0x0015;
864
                    s.MC = (comm.MC == 0x0010) ? 0x0090 : 0x0097;
11 andreas 865
 
86 andreas 866
                    if (s.MC == 0x0097)
867
                    {
868
                        comm.data.srDeviceInfo.device = comm.device2;
869
                        comm.data.srDeviceInfo.system = comm.system;
870
                        comm.data.srDeviceInfo.flag = 0x0000;
871
                        comm.data.srDeviceInfo.parentID = 0;
872
                        comm.data.srDeviceInfo.herstID = 1;
873
                        msg97fill(&comm);
874
                    }
875
                    else
11 andreas 876
                        sendCommand(s);
86 andreas 877
                break;
11 andreas 878
 
86 andreas 879
                case 0x0011:    // request output channel count
880
                case 0x0012:    // request level count
881
                case 0x0013:    // request string size
882
                case 0x0014:    // request command size
883
                    comm.data.reqOutpChannels.device = makeWord(buff_[0], buff_[1]);
884
                    comm.data.reqOutpChannels.port = makeWord(buff_[2], buff_[3]);
885
                    comm.data.reqOutpChannels.system = makeWord(buff_[4], buff_[5]);
886
                    comm.checksum = buff_[6];
887
                    s.channel = false;
888
                    s.level = 0;
889
                    s.port = comm.data.reqOutpChannels.port;
890
                    s.value = 0;
11 andreas 891
 
86 andreas 892
                    switch (comm.MC)
893
                    {
894
                        case 0x0011:
11 andreas 895
                            s.MC = 0x0091;
896
                            s.value = 0x0f75;   // # channels
86 andreas 897
                        break;
898
 
899
                        case 0x0012:
11 andreas 900
                            s.MC = 0x0092;
901
                            s.value = 0x000d;   // # levels
86 andreas 902
                        break;
903
 
904
                        case 0x0013:
11 andreas 905
                            s.MC = 0x0093;
906
                            s.value = 0x00c7;   // string size
86 andreas 907
                        break;
908
 
909
                        case 0x0014:
11 andreas 910
                            s.MC = 0x0094;
911
                            s.value = 0x00c7;   // command size
86 andreas 912
                        break;
913
                    }
11 andreas 914
 
86 andreas 915
                    sendCommand(s);
916
                break;
917
 
918
                case 0x0015:    // request level size
919
                    comm.data.reqLevels.device = makeWord(buff_[0], buff_[1]);
920
                    comm.data.reqLevels.port = makeWord(buff_[2], buff_[3]);
921
                    comm.data.reqLevels.system = makeWord(buff_[4], buff_[5]);
922
                    comm.data.reqLevels.level = makeWord(buff_[6], buff_[7]);
923
                    comm.checksum = buff_[8];
924
                    s.channel = false;
925
                    s.level = comm.data.reqLevels.level;
926
                    s.port = comm.data.reqLevels.port;
927
                    s.value = 0;
928
                    s.MC = 0x0095;
929
                    sendCommand(s);
930
                break;
931
 
932
                case 0x0016:    // request status code
933
                    comm.data.sendStatusCode.device = makeWord(buff_[0], buff_[1]);
934
                    comm.data.sendStatusCode.port = makeWord(buff_[2], buff_[3]);
935
                    comm.data.sendStatusCode.system = makeWord(buff_[4], buff_[5]);
936
 
937
                    if (callback)
938
                        callback(comm);
939
                    else
940
                        MSG_WARNING("Missing callback function!");
941
                break;
942
 
943
                case 0x0097:    // receive device info
944
                    comm.data.srDeviceInfo.device = makeWord(buff_[0], buff_[1]);
945
                    comm.data.srDeviceInfo.system = makeWord(buff_[2], buff_[3]);
946
                    comm.data.srDeviceInfo.flag = makeWord(buff_[4], buff_[5]);
947
                    comm.data.srDeviceInfo.objectID = buff_[6];
948
                    comm.data.srDeviceInfo.parentID = buff_[7];
949
                    comm.data.srDeviceInfo.herstID = makeWord(buff_[8], buff_[9]);
950
                    comm.data.srDeviceInfo.deviceID = makeWord(buff_[10], buff_[11]);
951
                    memcpy(comm.data.srDeviceInfo.serial, &buff_[12], 16);
952
                    comm.data.srDeviceInfo.fwid = makeWord(buff_[28], buff_[29]);
953
                    memset(comm.data.srDeviceInfo.info, 0, sizeof(comm.data.srDeviceInfo.info));
954
                    memcpy(comm.data.srDeviceInfo.info, &buff_[30], comm.hlen - 0x0015 - 29);
955
                    comm.checksum = buff_[comm.hlen + 3];
956
                    // Prepare answer
957
                    s.channel = false;
958
                    s.level = 0;
959
                    s.port = 0;
960
                    s.value = 0;
961
 
962
                    if (!initSend)
963
                    {
964
                        s.MC = 0x0097;
965
                        initSend = true;
966
                    }
967
                    else if (!ready)
968
                    {
969
                        // Send counts
970
                        s.MC = 0x0090;
971
                        s.value = 0x0015;   // # ports
11 andreas 972
                        sendCommand(s);
86 andreas 973
                        s.MC = 0x0091;
974
                        s.value = 0x0f75;   // # channels
975
                        sendCommand(s);
976
                        s.MC = 0x0092;
977
                        s.value = 0x000d;   // # levels
978
                        sendCommand(s);
979
                        s.MC = 0x0093;
980
                        s.value = 0x00c7;   // string size
981
                        sendCommand(s);
982
                        s.MC = 0x0094;
983
                        s.value = 0x00c7;   // command size
984
                        sendCommand(s);
985
                        s.MC = 0x0098;
986
                        ready = true;
987
                    }
988
                    else
989
                        break;
11 andreas 990
 
86 andreas 991
                    sendCommand(s);
11 andreas 992
 
93 andreas 993
                    MSG_DEBUG("S/N: " << comm.data.srDeviceInfo.serial << " | " << comm.data.srDeviceInfo.info);
86 andreas 994
                break;
11 andreas 995
 
86 andreas 996
                case 0x00a1:    // request status
997
                    reqDevStatus = makeWord(buff_[0], buff_[1]);
998
                    comm.checksum = buff_[2];
999
                break;
11 andreas 1000
 
86 andreas 1001
                case 0x0204:    // file transfer
1002
                    s.device = comm.device2;
1003
                    comm.data.filetransfer.ftype = makeWord(buff_[0], buff_[1]);
1004
                    comm.data.filetransfer.function = makeWord(buff_[2], buff_[3]);
1005
                    pos = 4;
1006
 
1007
                    if (comm.data.filetransfer.ftype == 0 && comm.data.filetransfer.function == 0x0105)         // Directory exist?
1008
                    {
1009
                        for (size_t i = 0; i < 0x0104; i++)
11 andreas 1010
                        {
86 andreas 1011
                            comm.data.filetransfer.data[i] = buff_[pos];
1012
                            pos++;
11 andreas 1013
                        }
1014
 
86 andreas 1015
                        comm.data.filetransfer.data[0x0103] = 0;
1016
                        handleFTransfer(s, comm.data.filetransfer);
1017
                    }
1018
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0100)    // Controller have more files
1019
                        handleFTransfer(s, comm.data.filetransfer);
1020
                    else if (comm.data.filetransfer.ftype == 0 && comm.data.filetransfer.function == 0x0100)    // Request directory listing
1021
                    {
1022
                        comm.data.filetransfer.unk = makeWord(buff_[4], buff_[5]);
1023
                        pos = 6;
11 andreas 1024
 
86 andreas 1025
                        for (size_t i = 0; i < 0x0104; i++)
1026
                        {
1027
                            comm.data.filetransfer.data[i] = buff_[pos];
1028
                            pos++;
11 andreas 1029
                        }
1030
 
86 andreas 1031
                        comm.data.filetransfer.data[0x0103] = 0;
1032
                        handleFTransfer(s, comm.data.filetransfer);
1033
                    }
1034
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0102)    // controller will send a file
1035
                    {
1036
                        comm.data.filetransfer.unk = makeDWord(buff_[4], buff_[5], buff_[6], buff_[7]);
1037
                        comm.data.filetransfer.unk1 = makeDWord(buff_[8], buff_[9], buff_[10], buff_[11]);
1038
                        pos = 12;
11 andreas 1039
 
86 andreas 1040
                        for (size_t i = 0; i < 0x0104; i++)
1041
                        {
1042
                            comm.data.filetransfer.data[i] = buff_[pos];
1043
                            pos++;
11 andreas 1044
                        }
1045
 
86 andreas 1046
                        comm.data.filetransfer.data[0x0103] = 0;
1047
                        handleFTransfer(s, comm.data.filetransfer);
1048
                    }
1049
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0103)    // file or part of a file
1050
                    {
1051
                        comm.data.filetransfer.unk = makeWord(buff_[4], buff_[5]);
1052
                        pos = 6;
11 andreas 1053
 
86 andreas 1054
                        for (size_t i = 0; i < comm.data.filetransfer.unk; i++)
1055
                        {
1056
                            comm.data.filetransfer.data[i] = buff_[pos];
1057
                            pos++;
11 andreas 1058
                        }
86 andreas 1059
 
1060
                        handleFTransfer(s, comm.data.filetransfer);
1061
                    }
1062
                    else if (comm.data.filetransfer.ftype == 0 && comm.data.filetransfer.function == 0x0104)    // Does file exist;
1063
                    {
1064
                        for (size_t i = 0; i < 0x0104; i++)
11 andreas 1065
                        {
86 andreas 1066
                            comm.data.filetransfer.data[i] = buff_[pos];
1067
                            pos++;
11 andreas 1068
                        }
1069
 
86 andreas 1070
                        comm.data.filetransfer.data[0x0103] = 0;
1071
                        handleFTransfer(s, comm.data.filetransfer);
1072
                    }
1073
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0104)    // request a file
1074
                    {
1075
                        comm.data.filetransfer.unk = makeWord(buff_[4], buff_[5]);
1076
                        pos = 6;
11 andreas 1077
 
86 andreas 1078
                        for (size_t i = 0; i < 0x0104; i++)
11 andreas 1079
                        {
86 andreas 1080
                            comm.data.filetransfer.data[i] = buff_[pos];
1081
                            pos++;
11 andreas 1082
                        }
1083
 
86 andreas 1084
                        comm.data.filetransfer.data[0x0103] = 0;
1085
                        handleFTransfer(s, comm.data.filetransfer);
1086
                    }
1087
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0106)    // ACK for 0x0105
1088
                    {
1089
                        comm.data.filetransfer.unk = makeDWord(buff_[4], buff_[5], buff_[6], buff_[7]);
1090
                        pos = 8;
1091
                        handleFTransfer(s, comm.data.filetransfer);
1092
                    }
1093
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0002)    // request next part of file
1094
                        handleFTransfer(s, comm.data.filetransfer);
1095
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0003)    // File content from controller
1096
                    {
1097
                        comm.data.filetransfer.unk = makeWord(buff_[4], buff_[5]);  // length of data block
1098
                        pos = 6;
11 andreas 1099
 
86 andreas 1100
                        for (size_t i = 0; i < comm.data.filetransfer.unk; i++)
11 andreas 1101
                        {
86 andreas 1102
                            comm.data.filetransfer.data[i] = buff_[pos];
1103
                            pos++;
11 andreas 1104
                        }
1105
 
86 andreas 1106
                        handleFTransfer(s, comm.data.filetransfer);
1107
                    }
1108
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0004)    // End of file
1109
                        handleFTransfer(s, comm.data.filetransfer);
1110
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0005)    // End of file ACK
1111
                        handleFTransfer(s, comm.data.filetransfer);
1112
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0006)    // End of directory listing ACK
1113
                    {
1114
                        comm.data.filetransfer.unk = makeWord(buff_[4], buff_[5]);  // length of received data block
1115
                        pos = 6;
1116
                        handleFTransfer(s, comm.data.filetransfer);
1117
                    }
1118
                    else if (comm.data.filetransfer.ftype == 4 && comm.data.filetransfer.function == 0x0007)    // End of file transfer
1119
                        handleFTransfer(s, comm.data.filetransfer);
11 andreas 1120
 
86 andreas 1121
                break;
15 andreas 1122
 
86 andreas 1123
                case 0x0501:    // ping
1124
                    comm.data.chan_state.device = makeWord(buff_[0], buff_[1]);
1125
                    comm.data.chan_state.system = makeWord(buff_[2], buff_[3]);
1126
                    s.channel = 0;
1127
                    s.level = 0;
1128
                    s.port = 0;
1129
                    s.value = 0;
1130
                    s.MC = 0x0581;
1131
                    sendCommand(s);
1132
                break;
15 andreas 1133
 
86 andreas 1134
                case 0x0502:    // Date and time
1135
                    comm.data.blinkMessage.heartBeat = buff_[0];
1136
                    comm.data.blinkMessage.LED = buff_[1];
1137
                    comm.data.blinkMessage.month = buff_[2];
1138
                    comm.data.blinkMessage.day = buff_[3];
1139
                    comm.data.blinkMessage.year = makeWord(buff_[4], buff_[5]);
1140
                    comm.data.blinkMessage.hour = buff_[6];
1141
                    comm.data.blinkMessage.minute = buff_[7];
1142
                    comm.data.blinkMessage.second = buff_[8];
1143
                    comm.data.blinkMessage.weekday = buff_[9];
1144
                    comm.data.blinkMessage.extTemp = makeWord(buff_[10], buff_[11]);
1145
                    memset(comm.data.blinkMessage.dateTime, 0, sizeof(comm.data.blinkMessage.dateTime));
1146
                    memcpy(comm.data.blinkMessage.dateTime, &buff_[12], comm.hlen - 0x0015 - 11);
1147
                    comm.checksum = buff_[comm.hlen + 3];
1148
 
1149
                    sendAllFuncTimer(comm.data.blinkMessage);
15 andreas 1150
/*                        if (callback)
86 andreas 1151
                        callback(comm);
1152
                    else
1153
                        MSG_WARNING("Missing callback function!"); */
1154
                break;
1155
            }
1156
        break;
11 andreas 1157
 
86 andreas 1158
        default:        // Every unknown or unsupported command/request should be ignored.
1159
            ignore = true;
11 andreas 1160
    }
1161
}
1162
 
1163
bool TAmxNet::sendCommand(const ANET_SEND& s)
1164
{
1165
    DECL_TRACER("TAmxNet::sendCommand (const ANET_SEND& s)");
1166
 
1167
    size_t len, size;
1168
    ANET_COMMAND com;
1169
    com.clear();
1170
    com.MC = s.MC;
1171
 
1172
    if (s.MC == 0x0204)     // file transfer
1173
        com.device1 = s.device;
1174
    else
1175
        com.device1 = 0;
1176
 
1177
    com.device2 = panelID;
1178
    com.port1 = 1;
1179
    com.system = TConfig::getSystem();
1180
    com.port2 = s.port;
1181
    sendCounter++;
1182
    com.count = sendCounter;
1183
 
1184
    switch (s.MC)
1185
    {
1186
        case 0x0084:        // push button
1187
            com.data.channel.device = com.device2;
1188
            com.data.channel.port = s.port;
1189
            com.data.channel.system = com.system;
1190
            com.data.channel.channel = s.channel;
1191
            com.hlen = 0x0016 - 0x0003 + sizeof(ANET_CHANNEL);
93 andreas 1192
            MSG_DEBUG("SEND: BUTTON PUSH-" << s.channel << ":" << s.port << ":" << com.device2);
11 andreas 1193
            comStack.push_back(com);
14 andreas 1194
            mSendReady = true;
13 andreas 1195
        break;
11 andreas 1196
 
1197
        case 0x0085:        // release button
1198
            com.data.channel.device = com.device2;
1199
            com.data.channel.port = s.port;
1200
            com.data.channel.system = com.system;
1201
            com.data.channel.channel = s.channel;
1202
            com.hlen = 0x0016 - 0x0003 + sizeof(ANET_CHANNEL);
93 andreas 1203
            MSG_DEBUG("SEND: BUTTON RELEASE-" << s.channel << ":" << s.port << ":" << com.device2);
11 andreas 1204
            comStack.push_back(com);
14 andreas 1205
            mSendReady = true;
13 andreas 1206
        break;
11 andreas 1207
 
1208
        case 0x0086:    // output channel on
1209
        case 0x0088:    // feedback/input channel on
1210
            com.data.channel.device = com.device2;
1211
            com.data.channel.port = s.port;
1212
            com.data.channel.system = com.system;
1213
            com.data.channel.channel = s.channel;
1214
            com.hlen = 0x0016 - 0x0003 + sizeof(ANET_CHANNEL);
93 andreas 1215
            MSG_DEBUG("SEND: CHANNEL ON-" << s.channel << ":" << s.port << ":" << com.device2);
11 andreas 1216
            comStack.push_back(com);
14 andreas 1217
            mSendReady = true;
13 andreas 1218
        break;
11 andreas 1219
 
1220
        case 0x0087:    // output channel off
1221
        case 0x0089:    // feedback/input channel off
1222
            com.data.channel.device = com.device2;
1223
            com.data.channel.port = s.port;
1224
            com.data.channel.system = com.system;
1225
            com.data.channel.channel = s.channel;
1226
            com.hlen = 0x0016 - 0x0003 + sizeof(ANET_CHANNEL);
93 andreas 1227
            MSG_DEBUG("SEND: CHANNEL OFF-" << s.channel << ":" << s.port << ":" << com.device2);
11 andreas 1228
            comStack.push_back(com);
14 andreas 1229
            mSendReady = true;
13 andreas 1230
        break;
11 andreas 1231
 
1232
        case 0x008a:        // level value changed
1233
            com.data.message_value.device = com.device2;
1234
            com.data.message_value.port = s.port;
1235
            com.data.message_value.system = com.system;
1236
            com.data.message_value.value = s.level;
1237
            com.data.message_value.type = 0x20;     // unsigned integer
1238
            com.data.message_value.content.integer = s.value;
1239
            com.hlen = 0x0016 - 0x0003 + 11;
93 andreas 1240
            MSG_DEBUG("SEND: LEVEL-" << s.value << "," << s.level << ":" << s.port << ":" << com.device2);
11 andreas 1241
            comStack.push_back(com);
14 andreas 1242
            mSendReady = true;
13 andreas 1243
        break;
11 andreas 1244
 
1245
        case 0x008b:        // string command
1246
        case 0x008c:        // send command string
1247
            com.data.message_string.device = com.device2;
1248
            com.data.message_string.port = s.port;
1249
            com.data.message_string.system = com.system;
1250
            com.data.message_string.type = 0x01;    // char string
1251
 
1252
            if (s.msg.length() >= sizeof(com.data.message_string.content))
1253
                len = sizeof(com.data.message_string.content) - 1;
1254
            else
1255
                len = s.msg.length();
1256
 
1257
            com.data.message_string.length = len;
1258
            strncpy((char *)&com.data.message_string.content[0], s.msg.c_str(), len);
1259
            com.hlen = 0x0016 - 3 + 9 + len;
127 andreas 1260
 
1261
            if (s.MC == 0x008b)
1262
            {
1263
                MSG_DEBUG("SEND: STRING-'" << s.msg << "'," << s.port << ":" << com.device2);
1264
            }
1265
            else
1266
            {
1267
                MSG_DEBUG("SEND: COMMAND-'" << s.msg << "'," << s.port << ":" << com.device2);
1268
            }
1269
 
11 andreas 1270
            comStack.push_back(com);
14 andreas 1271
            mSendReady = true;
13 andreas 1272
        break;
11 andreas 1273
 
1274
        case 0x008d:    // Custom event
1275
            com.data.customEvent.device = com.device2;
1276
            com.data.customEvent.port = s.port;
1277
            com.data.customEvent.system = com.system;
1278
            com.data.customEvent.ID = s.ID;
1279
            com.data.customEvent.type = s.type;
1280
            com.data.customEvent.flag = s.flag;
1281
            com.data.customEvent.value1 = s.value1;
1282
            com.data.customEvent.value2 = s.value2;
1283
            com.data.customEvent.value3 = s.value3;
1284
            com.data.customEvent.dtype = s.dtype;
1285
 
1286
            if (s.msg.length() >= sizeof(com.data.customEvent.data))
1287
                len = sizeof(com.data.customEvent.data) - 1;
1288
            else
1289
                len = s.msg.length();
1290
 
1291
            com.data.customEvent.length = len;
1292
            memset(com.data.customEvent.data, 0, sizeof(com.data.customEvent.data));
110 andreas 1293
 
1294
            if (len > 0)
1295
                memcpy(&com.data.customEvent.data[0], s.msg.c_str(), len);
1296
 
1297
            com.hlen = 0x0016 - 3 + 29 + len;
11 andreas 1298
            comStack.push_back(com);
14 andreas 1299
            mSendReady = true;
13 andreas 1300
        break;
11 andreas 1301
 
1302
        case 0x0090:        // port count
1303
            com.data.sendPortNumber.device = com.device2;
1304
            com.data.sendPortNumber.system = com.system;
1305
            com.data.sendPortNumber.pcount = s.value;
1306
            com.hlen = 0x0016 - 3 + 6;
1307
            comStack.push_back(com);
14 andreas 1308
            mSendReady = true;
13 andreas 1309
        break;
11 andreas 1310
 
1311
        case 0x0091:        // output channel count
1312
        case 0x0092:        // send level count
1313
            com.data.sendOutpChannels.device = com.device2;
1314
            com.data.sendOutpChannels.port = s.port;
1315
            com.data.sendOutpChannels.system = com.system;
1316
            com.data.sendOutpChannels.count = s.value;
1317
            com.hlen = 0x0016 - 3 + 8;
1318
            comStack.push_back(com);
14 andreas 1319
            mSendReady = true;
13 andreas 1320
        break;
11 andreas 1321
 
1322
        case 0x0093:        // string size
1323
        case 0x0094:        // command size
1324
            com.data.sendSize.device = com.device2;
1325
            com.data.sendSize.port = s.port;
1326
            com.data.sendSize.system = com.system;
1327
            com.data.sendSize.type = 0x01;
1328
            com.data.sendSize.length = s.value;
1329
            com.hlen = 0x0016 - 3 + 9;
1330
            comStack.push_back(com);
14 andreas 1331
            mSendReady = true;
13 andreas 1332
        break;
11 andreas 1333
 
1334
        case 0x0095:        // suported level types
1335
            com.data.sendLevSupport.device = com.device2;
1336
            com.data.sendLevSupport.port = s.port;
1337
            com.data.sendLevSupport.system = com.system;
1338
            com.data.sendLevSupport.level = s.level;
1339
            com.data.sendLevSupport.num = 6;
1340
            com.data.sendLevSupport.types[0] = 0x10;
1341
            com.data.sendLevSupport.types[1] = 0x11;
1342
            com.data.sendLevSupport.types[2] = 0x20;
1343
            com.data.sendLevSupport.types[3] = 0x21;
1344
            com.data.sendLevSupport.types[4] = 0x40;
1345
            com.data.sendLevSupport.types[5] = 0x41;
1346
            com.hlen = 0x0016 - 0x0003 + sizeof(ANET_LEVSUPPORT);
1347
            comStack.push_back(com);
14 andreas 1348
            mSendReady = true;
13 andreas 1349
        break;
11 andreas 1350
 
1351
        case 0x0096:        // Status code
1352
            com.data.sendStatusCode.device = com.device2;
1353
            com.data.sendStatusCode.port = s.port;
1354
            com.data.sendStatusCode.system = com.system;
1355
            com.data.sendStatusCode.status = 0;
1356
            com.data.sendStatusCode.type = 0x11;
1357
            com.data.sendStatusCode.length = 2;
1358
            com.data.sendStatusCode.str[0] = 'O';
1359
            com.data.sendStatusCode.str[1] = 'K';
1360
            com.hlen = 0x0016 - 3 + 13;
1361
            comStack.push_back(com);
14 andreas 1362
            mSendReady = true;
13 andreas 1363
        break;
11 andreas 1364
 
1365
        case 0x0097:        // device info
1366
            com.data.srDeviceInfo.device = com.device2;
1367
            com.data.srDeviceInfo.system = com.system;
1368
            com.data.srDeviceInfo.flag = 0x0000;
1369
            com.data.srDeviceInfo.objectID = 0;
1370
            com.data.srDeviceInfo.parentID = 0;
1371
            com.data.srDeviceInfo.herstID = 1;
1372
            msg97fill(&com);
14 andreas 1373
            mSendReady = true;
13 andreas 1374
        break;
11 andreas 1375
 
1376
        case 0x0098:
1377
            com.data.reqPortCount.device = com.device2;
1378
            com.data.reqPortCount.system = com.system;
1379
            com.hlen = 0x0016 - 3 + 4;
1380
            comStack.push_back(com);
14 andreas 1381
            mSendReady = true;
13 andreas 1382
        break;
11 andreas 1383
 
1384
        case 0x0204:        // File transfer
1385
            com.port1 = 0;
1386
            com.port2 = 0;
1387
            com.data.filetransfer.ftype = s.dtype;
1388
            com.data.filetransfer.function = s.type;
1389
            com.data.filetransfer.info1 = s.value;
1390
            com.data.filetransfer.info2 = s.level;
1391
            com.data.filetransfer.unk = s.value1;
1392
            com.data.filetransfer.unk1 = s.value2;
1393
            com.data.filetransfer.unk2 = s.value3;
1394
            size = min(s.msg.length(), sizeof(com.data.filetransfer.data) - 1);
1395
            memcpy(com.data.filetransfer.data, s.msg.c_str(), size);
1396
            com.data.filetransfer.data[size] = 0;
1397
            len = 4;
1398
 
1399
            if (s.dtype == 0)
1400
            {
1401
                switch (s.type)
1402
                {
1403
                    case 0x0001: len += 2; break;
1404
                    case 0x0101: len += 16 + size + 1; break;
1405
                    case 0x0102: len += 19 + size + 1; break;
1406
                }
1407
            }
1408
            else
1409
            {
1410
                switch (s.type)
1411
                {
1412
                    case 0x0003: len += 2 + s.value1; break;
1413
                    case 0x0101: len += 8; break;
1414
                    case 0x0103: len += 6; break;
1415
                    case 0x0105: len += 8; break;
1416
                }
1417
            }
1418
 
1419
            com.hlen = 0x0016 - 3 + len;
1420
            comStack.push_back(com);
14 andreas 1421
            mSendReady = true;
13 andreas 1422
        break;
11 andreas 1423
 
1424
        case 0x0581:        // Pong
1425
            com.data.srDeviceInfo.device = panelID; // Configuration->getAMXChannel();
1426
            com.data.srDeviceInfo.system = TConfig::getSystem();
1427
            com.data.srDeviceInfo.herstID = devInfo[0].manufacturerID;
1428
            com.data.srDeviceInfo.deviceID = devInfo[0].deviceID;
1429
            com.data.srDeviceInfo.info[0] = 2;  // Type: IPv4 address
1430
            com.data.srDeviceInfo.info[1] = 4;  // length of following data
1431
 
1432
            {
92 andreas 1433
                string addr = mSocket->getMyIP();
11 andreas 1434
                vector<string> parts = StrSplit(addr, ".");
1435
 
1436
                for (size_t i = 0; i < parts.size(); i++)
1437
                    com.data.srDeviceInfo.info[i + 2] = (unsigned char)atoi(parts[i].c_str());
1438
            }
1439
 
1440
            com.hlen = 0x0016 - 3 + 14;
1441
            comStack.push_back(com);
14 andreas 1442
            mSendReady = true;
13 andreas 1443
        break;
11 andreas 1444
    }
1445
 
169 andreas 1446
    if (mSendReady && !write_busy)
1447
        runWrite();
11 andreas 1448
 
14 andreas 1449
    return mSendReady;
11 andreas 1450
}
1451
 
1452
void TAmxNet::handleFTransfer(ANET_SEND &s, ANET_FILETRANSFER &ft)
1453
{
1454
    DECL_TRACER("TAmxNet::handleFTransfer (ANET_SEND &s, ANET_FILETRANSFER &ft)");
1455
 
1456
    int len;
1457
    ANET_COMMAND ftr;
1458
    ftr.MC = 0x1000;
1459
    ftr.device1 = s.device;
1460
    ftr.device2 = s.device;
1461
    ftr.port1 = 0;
1462
    ftr.port2 = 0;
1463
    ftr.count = 0;
1464
    ftr.data.filetransfer.ftype = ft.ftype;
1465
    ftr.data.filetransfer.function = ft.function;
1466
    ftr.data.filetransfer.data[0] = 0;
1467
 
1468
    if (ft.ftype == 0 && ft.function == 0x0105)     // Create directory
1469
    {
1470
        s.channel = 0;
1471
        s.level = 0;
1472
        s.port = 0;
1473
        s.value = 0;
1474
        s.MC = 0x0204;
1475
        s.dtype = 0;                // ftype --> function type
1476
        s.type = 0x0001;            // function
1477
        s.value1 = 0;               // 1st data byte 0x00
1478
        s.value2 = 0x10;            // 2nd data byte 0x10
1479
        string f((char *)&ft.data);
93 andreas 1480
        MSG_DEBUG("0x0000/0x0105: Directory " << f << " exist?");
88 andreas 1481
        string prjPath = TConfig::getProjectPath();
1482
        string newPath;
1483
        dir::TDirectory dir;
11 andreas 1484
 
1485
        if (f.compare(0, 8, "AMXPanel") == 0)
1486
        {
1487
            if (f.find("/images") > 0)
88 andreas 1488
            {
1489
                newPath = prjPath + "/images";
1490
                dir.createAllPath(newPath);
1491
            }
11 andreas 1492
            else if (f.find("/sounds") > 0)
88 andreas 1493
            {
1494
                newPath = prjPath + "/sounds";
1495
                dir.createAllPath(newPath);
1496
            }
11 andreas 1497
            else if (f.find("/fonts") > 0)
88 andreas 1498
            {
1499
                newPath = prjPath + "/fonts";
1500
                dir.createAllPath(newPath);
1501
            }
11 andreas 1502
        }
1503
        else if (f.compare(0, 8, "__system") == 0)
1504
        {
88 andreas 1505
            vector<string> subDirs = { "borders", "cursors", "fonts", "images", "sliders", "sounds" };
1506
            vector<string>::iterator iter;
1507
 
1508
            for (iter = subDirs.begin(); iter != subDirs.end(); ++iter)
11 andreas 1509
            {
88 andreas 1510
                newPath = prjPath + "/__system/graphics/" + *iter;
1511
                dir.createAllPath(newPath);
11 andreas 1512
            }
1513
        }
1514
 
1515
        sendCommand(s);
1516
 
1517
        if (!receiveSetup)
1518
        {
1519
            receiveSetup = true;
1520
            ftransfer.maxFiles = countFiles();
1521
 
1522
            if (callback)
1523
                callback(ftr);
13 andreas 1524
            else
1525
                MSG_WARNING("Missing callback function!");
11 andreas 1526
        }
1527
    }
1528
    else if (ft.ftype == 0 && ft.function == 0x0100)    // Request directory
1529
    {
1530
        string fname((char *)&ft.data);
1531
        string amxpath(fname);
1532
        string realPath;
1533
        size_t pos = 0;
1534
        len = 0;
1535
        dir::TDirectory dr;
1536
 
1537
        if (fname.compare("AMXPanel/") == 0)
1538
        {
1539
            realPath.assign(TConfig::getProjectPath());
1540
            amxpath.assign("/opt/amx/user/AMXPanel");
1541
        }
1542
        else if ((pos = fname.find("AMXPanel/")) != string::npos)
1543
        {
1544
            if (pos == 0)
1545
                amxpath = "/opt/amx/user/" + fname;
1546
 
1547
            realPath = dr.stripPath("AMXPanel", fname);
1548
            realPath = TConfig::getProjectPath() + "/" + realPath;
1549
 
1550
            if (dr.isFile(realPath))
1551
                len = dr.getFileSize(realPath);
1552
        }
1553
 
93 andreas 1554
        MSG_DEBUG("0x0000/0x0100: Request directory " << fname);
11 andreas 1555
        snprintf((char *)&ftr.data.filetransfer.data[0], sizeof(ftr.data.filetransfer.data), "Syncing %d files ...", ftransfer.maxFiles);
1556
 
1557
        if (callback)
1558
            callback(ftr);
13 andreas 1559
        else
1560
            MSG_WARNING("Missing callback function!");
14 andreas 1561
 
11 andreas 1562
        s.channel = 0;
1563
        s.level = 0;
1564
        s.port = 0;
1565
        s.value = 0;
1566
        s.MC = 0x0204;
1567
        s.dtype = 0x0000;
1568
        s.type = 0x0101;
1569
        s.value1 = len;     // File length
1570
        s.value2 = 0x0000be42;
1571
        s.value3 = 0x00003e75;
1572
        s.msg = amxpath;
1573
        sendCommand(s);
1574
        // Read the directory tree
1575
        dr.setStripPath(true);
1576
        dr.readDir(realPath);
1577
        amxpath = fname;
1578
 
1579
        if (amxpath.length() > 1 && amxpath.at(amxpath.length() - 1) == '/')
1580
            amxpath = amxpath.substr(0, amxpath.length() - 1);
1581
 
1582
        for (pos = 0; pos < dr.getNumEntries(); pos++)
1583
        {
1584
            dir::DFILES_T df = dr.getEntry(pos);
1585
            s.type = 0x0102;
1586
 
1587
            s.value = (dr.testDirectory(df.attr)) ? 1 : 0;  // Depends on type of entry
1588
            s.level = dr.getNumEntries();       // # entries
1589
            s.value1 = df.count;                // counter
1590
            s.value2 = df.size;                 // Size of file
1591
            s.value3 = df.date;                 // Last modification date (epoch)
1592
            s.msg.assign(amxpath + "/" + df.name);
1593
            sendCommand(s);
1594
        }
23 andreas 1595
 
1596
        if (dr.getNumEntries() == 0)
1597
        {
1598
            s.type = 0x0102;
1599
 
1600
            s.value = 0;
1601
            s.level = 0;       // # entries
1602
            s.value1 = 0;                // counter
1603
            s.value2 = 0;                 // Size of file
1604
            s.value3 = 0;                 // Last modification date (epoch)
1605
            s.msg.assign(amxpath + "/");
1606
            sendCommand(s);
1607
        }
11 andreas 1608
    }
1609
    else if (ft.ftype == 4 && ft.function == 0x0100)    // Have more files to send.
1610
    {
93 andreas 1611
        MSG_DEBUG("0x0004/0x0100: Have more files to send.");
11 andreas 1612
        s.channel = 0;
1613
        s.level = 0;
1614
        s.port = 0;
1615
        s.value = 0;
1616
        s.MC = 0x0204;
1617
        s.dtype = 4;                // ftype --> function type
1618
        s.type = 0x0101;            // function:
1619
        s.value1 = 0x01bb3000;      // ?
1620
        s.value2 = 0;               // ?
1621
        sendCommand(s);
1622
    }
1623
    else if (ft.ftype == 4 && ft.function == 0x0102)    // Controller will send a file
1624
    {
1625
        string f((char*)&ft.data);
1626
        size_t pos;
1627
        rcvFileName.assign(TConfig::getProjectPath());
1628
 
1629
        if (f.find("AMXPanel") != string::npos)
1630
        {
1631
            pos = f.find_first_of("/");
1632
            rcvFileName.append(f.substr(pos));
1633
        }
1634
        else
1635
        {
1636
            rcvFileName.append("/");
1637
            rcvFileName.append((char*)&ft.data);
1638
        }
1639
 
1640
        if (rcvFile != nullptr)
88 andreas 1641
        {
11 andreas 1642
            fclose(rcvFile);
88 andreas 1643
            rcvFile = nullptr;
1644
            isOpenRcv = false;
1645
        }
11 andreas 1646
 
43 andreas 1647
        // The file name is encoded as CP1250 (Windows). Because we use UTF-8 we
1648
        // must convert the file name to get rid of non ASCII characters.
1649
        rcvFileName = cp1250ToUTF8(rcvFileName);
65 andreas 1650
        dir::TDirectory dr;
1651
 
1652
        if (!dr.exists(rcvFileName))
1653
            dr.createAllPath(rcvFileName, true);
1654
        else
1655
            dr.drop(rcvFileName);
1656
 
11 andreas 1657
        rcvFile = fopen(rcvFileName.c_str(), "w+");
1658
 
1659
        if (!rcvFile)
1660
        {
65 andreas 1661
            MSG_ERROR("Error creating file " << rcvFileName << " (" << strerror(errno) << ")");
11 andreas 1662
            isOpenRcv = false;
1663
        }
1664
        else
65 andreas 1665
        {
11 andreas 1666
            isOpenRcv = true;
1667
 
65 andreas 1668
            if (!TStreamError::checkFilter(HLOG_TRACE))
1669
            {
1670
                MSG_INFO("Writing file: " << rcvFileName);
1671
            }
1672
        }
1673
 
93 andreas 1674
        MSG_DEBUG("0x0004/0x0102: Controller will send file " << rcvFileName);
65 andreas 1675
 
11 andreas 1676
        ftransfer.actFileNum++;
1677
        ftransfer.lengthFile = ft.unk;
1678
 
1679
        if (ftransfer.actFileNum > ftransfer.maxFiles)
1680
            ftransfer.maxFiles = ftransfer.actFileNum;
1681
 
1682
        ftransfer.percent = (int)(100.0 / (double)ftransfer.maxFiles * (double)ftransfer.actFileNum);
1683
        pos = rcvFileName.find_last_of("/");
1684
        string shfn;
1685
 
1686
        if (pos != string::npos)
43 andreas 1687
            shfn = rcvFileName.substr(pos + 1);
11 andreas 1688
        else
43 andreas 1689
            shfn = rcvFileName;
11 andreas 1690
 
43 andreas 1691
        snprintf((char*)&ftr.data.filetransfer.data[0], sizeof(ftr.data.filetransfer.data), "[%d/%d] %s", ftransfer.actFileNum, ftransfer.maxFiles, shfn.c_str());
11 andreas 1692
        ftr.count = ftransfer.percent;
1693
        ftr.data.filetransfer.info1 = 0;
1694
 
1695
        if (callback)
1696
            callback(ftr);
13 andreas 1697
        else
1698
            MSG_WARNING("Missing callback function!");
11 andreas 1699
 
1700
        posRcv = 0;
1701
        lenRcv = ft.unk;
1702
        s.channel = 0;
1703
        s.level = 0;
1704
        s.port = 0;
1705
        s.value = 0;
1706
        s.MC = 0x0204;
1707
        s.dtype = 4;                // ftype --> function type
1708
        s.type = 0x0103;            // function: ready for receiving file
1709
        s.value1 = MAX_CHUNK;       // Maximum length of a chunk
1710
        s.value2 = ft.unk1;         // ID?
1711
        sendCommand(s);
1712
    }
1713
    else if (ft.ftype == 0 && ft.function == 0x0104)    // Delete file <name>
1714
    {
1715
        dir::TDirectory dr;
1716
        s.channel = 0;
1717
        s.level = 0;
1718
        s.port = 0;
1719
        s.value = 0;
1720
        s.MC = 0x0204;
1721
        string f((char*)&ft.data[0]);
1722
        size_t pos = 0;
1723
 
1724
        if ((pos = f.find("AMXPanel/")) == string::npos)
1725
            pos = f.find("__system/");
1726
 
93 andreas 1727
        MSG_DEBUG("0x0000/0x0104: Delete file " << f);
11 andreas 1728
 
1729
        if (pos != string::npos)
1730
            f = TConfig::getProjectPath() + "/" + f.substr(pos + 9);
1731
        else
1732
            f = TConfig::getProjectPath() + "/" + f;
1733
 
1734
        if (dr.exists(f))
1735
        {
1736
            s.dtype = 0;                // ftype --> function type
1737
            s.type = 0x0002;            // function: yes file exists
1738
            remove(f.c_str());
1739
        }
1740
        else    // Send: file was deleted although it does not exist.
1741
        {
1742
            MSG_ERROR("[DELETE] File " << f << " not found!");
1743
            s.dtype = 0;                // ftype --> function type
1744
            s.type = 0x0002;            // function: yes file exists
1745
        }
1746
 
1747
        sendCommand(s);
1748
 
1749
        if (ftransfer.actDelFile == 0)
1750
        {
1751
            ftransfer.actDelFile++;
1752
            ftransfer.percent = (int)(100.0 / (double)ftransfer.maxFiles * (double)ftransfer.actDelFile);
1753
            ftr.count = ftransfer.percent;
1754
 
1755
            if (callback)
1756
                callback(ftr);
13 andreas 1757
            else
1758
                MSG_WARNING("Missing callback function!");
11 andreas 1759
        }
1760
        else
1761
        {
1762
            ftransfer.actDelFile++;
1763
            int prc = (int)(100.0 / (double)ftransfer.maxFiles * (double)ftransfer.actDelFile);
1764
 
1765
            if (prc != ftransfer.percent)
1766
            {
1767
                ftransfer.percent = prc;
1768
                ftr.count = prc;
1769
 
1770
                if (callback)
1771
                    callback(ftr);
13 andreas 1772
                else
1773
                    MSG_WARNING("Missing callback function!");
11 andreas 1774
            }
1775
        }
1776
    }
1777
    else if (ft.ftype == 4 && ft.function == 0x0104)    // request a file
1778
    {
1779
        string f((char*)&ft.data);
1780
        size_t pos;
1781
        len = 0;
1782
        sndFileName.assign(TConfig::getProjectPath());
93 andreas 1783
        MSG_DEBUG("0x0004/0x0104: Request file " << f);
11 andreas 1784
 
1785
        if (f.find("AMXPanel") != string::npos)
1786
        {
1787
            pos = f.find_first_of("/");
1788
            sndFileName.append(f.substr(pos));
1789
        }
1790
        else
1791
        {
1792
            sndFileName.append("/");
1793
            sndFileName.append(f);
1794
        }
1795
 
1796
        if (!access(sndFileName.c_str(), R_OK))
1797
        {
1798
            struct stat s;
1799
 
1800
            if (stat(sndFileName.c_str(), &s) == 0)
1801
                len = s.st_size;
1802
            else
1803
                len = 0;
1804
        }
1805
        else if (sndFileName.find("/version.xma") > 0)
1806
            len = 0x0015;
1807
        else
1808
            len = 0;
1809
 
93 andreas 1810
        MSG_DEBUG("0x0004/0x0104: (" << len << ") File: " << sndFileName);
11 andreas 1811
 
1812
        s.channel = 0;
1813
        s.level = 0;
1814
        s.port = 0;
1815
        s.value = 0;
1816
        s.MC = 0x0204;
1817
        s.dtype = 4;                // ftype --> function type
1818
        s.type = 0x0105;            // function
1819
        s.value1 = len;             // length of file to send
1820
        s.value2 = 0x00001388;      // ID for device when sending a file.
1821
        sendCommand(s);
1822
    }
1823
    else if (ft.ftype == 4 && ft.function == 0x0106)    // Controller is ready for receiving file
1824
    {
93 andreas 1825
        MSG_DEBUG("0x0004/0x0106: Controller is ready for receiving file.");
11 andreas 1826
 
1827
        if (!access(sndFileName.c_str(), R_OK))
1828
        {
1829
            struct stat st;
1830
            stat(sndFileName.c_str(), &st);
1831
            len = st.st_size;
1832
            lenSnd = len;
1833
            posSnd = 0;
1834
            sndFile = fopen(sndFileName.c_str(), "r");
1835
 
1836
            if (!sndFile)
1837
            {
1838
                MSG_ERROR("Error reading file " << sndFileName);
1839
                len = 0;
1840
                isOpenSnd = false;
1841
            }
1842
            else
1843
                isOpenSnd = true;
1844
 
1845
            if (isOpenSnd && len <= MAX_CHUNK)
1846
            {
1847
                char *buf = new char[len + 1];
1848
                fread(buf, 1, len, sndFile);
1849
                s.msg.assign(buf, len);
1850
                delete[] buf;
1851
                posSnd = len;
1852
            }
1853
            else if (isOpenSnd)
1854
            {
1855
                char *buf = new char[MAX_CHUNK + 1];
1856
                fread(buf, 1, MAX_CHUNK, sndFile);
1857
                s.msg.assign(buf, MAX_CHUNK);
1858
                delete[] buf;
1859
                posSnd = MAX_CHUNK;
1860
                len = MAX_CHUNK;
1861
            }
1862
        }
1863
        else if (sndFileName.find("/version.xma") > 0)
1864
        {
1865
            s.msg.assign("<version>9</version>\n");
1866
            len = s.msg.length();
1867
            posSnd = len;
1868
        }
1869
        else
1870
            len = 0;
1871
 
1872
        s.channel = 0;
1873
        s.level = 0;
1874
        s.port = 0;
1875
        s.value = 0;
1876
        s.MC = 0x0204;
1877
        s.dtype = 4;                // ftype --> function type
1878
        s.type = 0x0003;            // function: Sending file with length <len>
1879
        s.value1 = len;             // length of content to send
1880
        sendCommand(s);
1881
    }
1882
    else if (ft.ftype == 4 && ft.function == 0x0002)    // request next part of file
1883
    {
93 andreas 1884
        MSG_DEBUG("0x0004/0x0002: Request next part of file.");
11 andreas 1885
        s.channel = 0;
1886
        s.level = 0;
1887
        s.port = 0;
1888
        s.value = 0;
1889
        s.MC = 0x0204;
1890
        s.dtype = 4;                // ftype --> function type
1891
 
1892
        if (posSnd < lenSnd)
1893
        {
1894
            s.type = 0x0003;        // Next part of file
1895
 
1896
            if ((posSnd + MAX_CHUNK) > lenSnd)
1897
                len = lenSnd - posSnd;
1898
            else
1899
                len = MAX_CHUNK;
1900
 
1901
            s.value1 = len;
1902
 
1903
            if (isOpenSnd)
1904
            {
1905
                char *buf = new char[len + 1];
1906
                fread(buf, 1, len, sndFile);
1907
                s.msg.assign(buf, len);
1908
                delete[] buf;
1909
                posSnd += len;
1910
            }
1911
            else
1912
                s.value1 = 0;
1913
        }
1914
        else
1915
            s.type = 0x0004;        // function: End of file reached
1916
 
1917
        sendCommand(s);
1918
    }
1919
    else if (ft.ftype == 4 && ft.function == 0x0003)    // File content
1920
    {
93 andreas 1921
        MSG_DEBUG("0x0004/0x0003: Received (part of) file.");
11 andreas 1922
        len = ft.unk;
1923
 
1924
        if (isOpenRcv)
1925
        {
1926
            fwrite(ft.data, 1, len, rcvFile);
1927
            posRcv += ft.unk;
1928
        }
1929
        else
65 andreas 1930
            MSG_WARNING("No open file to write to! (" << rcvFileName << ")");
11 andreas 1931
 
1932
        s.channel = 0;
1933
        s.level = 0;
1934
        s.port = 0;
1935
        s.value = 0;
1936
        s.MC = 0x0204;
1937
        s.dtype = 4;                // ftype --> function type
1938
        s.type = 0x0002;            // function: Request next part of file
1939
        sendCommand(s);
1940
 
1941
        int prc = (int)(100.0 / (double)ftransfer.lengthFile * (double)posRcv);
1942
 
1943
        if (prc != ftr.data.filetransfer.info1)
1944
        {
1945
            ftr.data.filetransfer.info1 = (int)(100.0 / (double)ftransfer.lengthFile * (double)posRcv);
1946
            ftr.count = ftransfer.percent;
1947
 
1948
            if (callback)
1949
                callback(ftr);
13 andreas 1950
            else
1951
                MSG_WARNING("Missing callback function!");
11 andreas 1952
        }
1953
    }
1954
    else if (ft.ftype == 4 && ft.function == 0x0004)    // End of file
1955
    {
93 andreas 1956
        MSG_DEBUG("0x0004/0x0004: End of file.");
11 andreas 1957
 
1958
        if (isOpenRcv)
1959
        {
1960
            unsigned char buf[8];
1961
            fseek(rcvFile, 0, SEEK_SET);
1962
            fread(buf, 1, sizeof(buf), rcvFile);
1963
            fclose(rcvFile);
1964
            isOpenRcv = false;
1965
            rcvFile = nullptr;
1966
            posRcv = 0;
1967
 
1968
            if (buf[0] == 0x1f && buf[1] == 0x8b)   // GNUzip compressed?
1969
            {
1970
                TExpand exp(rcvFileName);
1971
                exp.unzip();
1972
            }
1973
        }
1974
 
1975
        ftr.count = ftransfer.percent;
1976
        ftr.data.filetransfer.info1 = 100;
1977
 
1978
        if (callback)
1979
            callback(ftr);
13 andreas 1980
        else
1981
            MSG_WARNING("Missing callback functiom!");
11 andreas 1982
 
1983
        s.channel = 0;
1984
        s.level = 0;
1985
        s.port = 0;
1986
        s.value = 0;
1987
        s.MC = 0x0204;
1988
        s.dtype = 4;                // ftype --> function type
1989
        s.type = 0x0005;            // function: ACK, file received. No answer expected.
1990
        sendCommand(s);
1991
    }
1992
    else if (ft.ftype == 4 && ft.function == 0x0005)    // ACK, controller received file, no answer
1993
    {
93 andreas 1994
        MSG_DEBUG("0x0004/0x0005: Controller received file.");
11 andreas 1995
        posSnd = 0;
1996
        lenSnd = 0;
1997
 
1998
        if (isOpenSnd && sndFile != nullptr)
1999
            fclose(sndFile);
2000
 
2001
        ftransfer.lengthFile = 0;
2002
        sndFile = nullptr;
2003
    }
2004
    else if (ft.ftype == 4 && ft.function == 0x0006)    // End of directory transfer ACK
2005
    {
93 andreas 2006
        MSG_DEBUG("0x0004/0x0006: End of directory transfer.");
11 andreas 2007
    }
2008
    else if (ft.ftype == 4 && ft.function == 0x0007)    // End of file transfer
2009
    {
93 andreas 2010
        MSG_DEBUG("0x0004/0x0007: End of file transfer.");
11 andreas 2011
 
2012
        if (callback)
2013
            callback(ftr);
13 andreas 2014
        else
2015
            MSG_WARNING("Missing callback function!");
11 andreas 2016
 
2017
        receiveSetup = false;
2018
    }
2019
}
2020
 
2021
int TAmxNet::msg97fill(ANET_COMMAND *com)
2022
{
2023
    DECL_TRACER("TAmxNet::msg97fill(ANET_COMMAND *com)");
2024
 
2025
    int pos = 0;
2026
    unsigned char buf[512];
2027
 
2028
    for (size_t i = 0; i < devInfo.size(); i++)
2029
    {
2030
        pos = 0;
2031
 
2032
        if (i == 0)
2033
            com->sep1 = 0x12;
2034
        else
2035
            com->sep1 = 0x02;
2036
 
2037
        memset(buf, 0, sizeof(buf));
2038
        com->data.srDeviceInfo.objectID = devInfo[i].objectID;
2039
        com->data.srDeviceInfo.parentID = devInfo[i].parentID;
2040
        com->data.srDeviceInfo.herstID = devInfo[i].manufacturerID;
2041
        com->data.srDeviceInfo.deviceID = devInfo[i].deviceID;
2042
        memcpy(com->data.srDeviceInfo.serial, devInfo[i].serialNum, 16);
2043
        com->data.srDeviceInfo.fwid = devInfo[i].firmwareID;
2044
        memcpy(buf, devInfo[i].versionInfo, strlen(devInfo[i].versionInfo));
2045
        pos = (int)strlen(devInfo[i].versionInfo) + 1;
2046
        memcpy(buf + pos, devInfo[i].deviceInfo, strlen(devInfo[i].deviceInfo));
2047
        pos += strlen(devInfo[i].deviceInfo) + 1;
2048
        memcpy(buf + pos, devInfo[i].manufacturerInfo, strlen(devInfo[i].manufacturerInfo));
2049
        pos += strlen(devInfo[i].manufacturerInfo) + 1;
2050
        *(buf + pos) = 0x02; // type IP address
2051
        pos++;
2052
        *(buf + pos) = 0x04; // field length: 4 bytes
2053
        // Now the IP Address
92 andreas 2054
        string addr = mSocket->getMyIP();
11 andreas 2055
        vector<string> parts = StrSplit(addr, ".");
2056
 
2057
        for (size_t i = 0; i < parts.size(); i++)
2058
        {
2059
            pos++;
2060
            *(buf + pos) = (unsigned char)atoi(parts[i].c_str());
2061
        }
2062
 
2063
        pos++;
2064
        com->data.srDeviceInfo.len = pos;
2065
        memcpy(com->data.srDeviceInfo.info, buf, pos);
2066
        com->hlen = 0x0016 - 3 + 31 + pos - 1;
2067
        comStack.push_back(*com);
2068
        sendCounter++;
2069
        com->count = sendCounter;
2070
    }
2071
 
2072
    return pos;
2073
}
2074
 
169 andreas 2075
void TAmxNet::runWrite()
2076
{
2077
    DECL_TRACER("TAmxNet::runWrite()");
2078
 
2079
    if (write_busy)
2080
        return;
2081
 
2082
    try
2083
    {
2084
        mWriteThread = std::thread([=] { this->start_write(); });
2085
        mWriteThread.detach();
2086
    }
2087
    catch (std::exception& e)
2088
    {
2089
        MSG_ERROR("Error starting write thread: " << e.what());
2090
        _netRunning = false;
2091
    }
2092
}
2093
 
11 andreas 2094
void TAmxNet::start_write()
2095
{
2096
    DECL_TRACER("TAmxNet::start_write()");
2097
 
92 andreas 2098
    if (!__CommValid || !mSocket || !isRunning() || !mSocket->isConnected())
11 andreas 2099
        return;
2100
 
2101
    if (write_busy)
2102
        return;
2103
 
2104
    write_busy = true;
2105
 
169 andreas 2106
    while (write_busy && !_restart_ && !killed && _netRunning)
11 andreas 2107
    {
169 andreas 2108
        while (comStack.size() > 0)
11 andreas 2109
        {
169 andreas 2110
            if (!isRunning())
2111
            {
2112
                comStack.clear();
2113
                write_busy = false;
2114
                return;
2115
            }
11 andreas 2116
 
169 andreas 2117
            mSend = comStack.at(0);
2118
            comStack.erase(comStack.begin());   // delete oldest element
2119
            unsigned char *buf = makeBuffer(mSend);
11 andreas 2120
 
169 andreas 2121
            if (buf == nullptr)
2122
            {
2123
                MSG_ERROR("Error creating a buffer! Token number: " << mSend.MC);
2124
                continue;
2125
            }
2126
 
2127
            MSG_DEBUG("Wrote buffer with " << (mSend.hlen + 4) << " bytes.");
2128
            mSocket->send((char *)buf, mSend.hlen + 4);
2129
            delete[] buf;
11 andreas 2130
        }
2131
 
169 andreas 2132
        mSendReady = false;
2133
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
11 andreas 2134
    }
2135
 
2136
    write_busy = false;
2137
}
2138
 
2139
uint16_t TAmxNet::swapWord(uint16_t w)
2140
{
2141
    uint16_t word = 0;
2142
    word = ((w << 8) & 0xff00) | ((w >> 8) & 0x00ff);
2143
    return word;
2144
}
2145
 
2146
uint32_t TAmxNet::swapDWord(uint32_t dw)
2147
{
2148
    uint32_t dword = 0;
2149
    dword = ((dw << 24) & 0xff000000) | ((dw << 8) & 0x00ff0000) | ((dw >> 8) & 0x0000ff00) | ((dw >> 24) & 0x000000ff);
2150
    return dword;
2151
}
2152
 
2153
unsigned char TAmxNet::calcChecksum(const unsigned char* buffer, size_t len)
2154
{
2155
    DECL_TRACER("TAmxNet::calcChecksum(const unsigned char* buffer, size_t len)");
2156
    unsigned long sum = 0;
2157
 
2158
    for (size_t i = 0; i < len; i++)
2159
        sum += (unsigned long)(*(buffer + i)) & 0x000000ff;
2160
 
2161
    sum &= 0x000000ff;
93 andreas 2162
    MSG_DEBUG("Checksum=" << std::setw(2) << std::setfill('0') << std::hex << sum << ", #bytes=" << len << " bytes.");
11 andreas 2163
    return (unsigned char)sum;
2164
}
2165
 
2166
uint16_t TAmxNet::makeWord(unsigned char b1, unsigned char b2)
2167
{
2168
    return ((b1 << 8) & 0xff00) | b2;
2169
}
2170
 
2171
uint32_t TAmxNet::makeDWord(unsigned char b1, unsigned char b2, unsigned char b3, unsigned char b4)
2172
{
2173
    return ((b1 << 24) & 0xff000000) | ((b2 << 16) & 0x00ff0000) | ((b3  << 8) & 0x0000ff00) | b4;
2174
}
2175
 
2176
bool TAmxNet::isCommand(const string& cmd)
2177
{
2178
    DECL_TRACER("TAmxNet::isCommand(string& cmd)");
2179
 
2180
    int i = 0;
2181
 
2182
    while (cmdList[i][0] != 0)
2183
    {
2184
        if (cmd.find(cmdList[i]) == 0)
2185
            return true;
2186
 
2187
        i++;
2188
    }
2189
 
127 andreas 2190
    if (cmd.length() > 0 && (cmd[0] == '^' || cmd[0] == '@' || cmd[0] == '?'))
2191
        return true;
2192
 
2193
    if (startsWith(cmd, "GET ") || startsWith(cmd, "SET "))
2194
        return true;
2195
 
11 andreas 2196
    return false;
2197
}
2198
 
2199
unsigned char *TAmxNet::makeBuffer(const ANET_COMMAND& s)
2200
{
2201
    DECL_TRACER("TAmxNet::makeBuffer (const ANET_COMMAND& s)");
2202
 
2203
    int pos = 0;
2204
    int len;
2205
    bool valid = false;
2206
    unsigned char *buf;
2207
 
2208
    try
2209
    {
2210
        buf = new unsigned char[s.hlen + 5];
2211
        memset(buf, 0, s.hlen + 5);
2212
    }
2213
    catch (std::exception& e)
2214
    {
2215
        MSG_ERROR("Error allocating memory: " << e.what());
2216
        return nullptr;
2217
    }
2218
 
2219
    *buf = s.ID;
2220
    *(buf + 1) = s.hlen >> 8;
2221
    *(buf + 2) = s.hlen;
2222
    *(buf + 3) = s.sep1;
2223
    *(buf + 4) = s.type;
2224
    *(buf + 5) = s.unk1 >> 8;
2225
    *(buf + 6) = s.unk1;
2226
    *(buf + 7) = s.device1 >> 8;
2227
    *(buf + 8) = s.device1;
2228
    *(buf + 9) = s.port1 >> 8;
2229
    *(buf + 10) = s.port1;
2230
    *(buf + 11) = s.system >> 8;
2231
    *(buf + 12) = s.system;
2232
    *(buf + 13) = s.device2 >> 8;
2233
    *(buf + 14) = s.device2;
2234
    *(buf + 15) = s.port2 >> 8;
2235
    *(buf + 16) = s.port2;
2236
    *(buf + 17) = s.unk6;
2237
    *(buf + 18) = s.count >> 8;
2238
    *(buf + 19) = s.count;
2239
    *(buf + 20) = s.MC >> 8;
2240
    *(buf + 21) = s.MC;
2241
 
2242
    // Here the fixed block is complete. The data are following.
2243
    switch (s.MC)
2244
    {
2245
        case 0x0006:
2246
        case 0x0007:
2247
        case 0x0018:
2248
        case 0x0019:
2249
        case 0x0084:
2250
        case 0x0085:
2251
        case 0x0086:
2252
        case 0x0087:
2253
        case 0x0088:
2254
        case 0x0089:
2255
            *(buf + 22) = s.data.chan_state.device >> 8;
2256
            *(buf + 23) = s.data.chan_state.device;
2257
            *(buf + 24) = s.data.chan_state.port >> 8;
2258
            *(buf + 25) = s.data.chan_state.port;
2259
            *(buf + 26) = s.data.chan_state.system >> 8;
2260
            *(buf + 27) = s.data.chan_state.system;
2261
            *(buf + 28) = s.data.chan_state.channel >> 8;
2262
            *(buf + 29) = s.data.chan_state.channel;
2263
            *(buf + 30) = calcChecksum(buf, 30);
2264
            valid = true;
2265
            break;
2266
 
2267
        case 0x000a:
2268
        case 0x008a:
2269
            *(buf + 22) = s.data.message_value.device >> 8;
2270
            *(buf + 23) = s.data.message_value.device;
2271
            *(buf + 24) = s.data.message_value.port >> 8;
2272
            *(buf + 25) = s.data.message_value.port;
2273
            *(buf + 26) = s.data.message_value.system >> 8;
2274
            *(buf + 27) = s.data.message_value.system;
2275
            *(buf + 28) = s.data.message_value.value >> 8;
2276
            *(buf + 29) = s.data.message_value.value;
2277
            *(buf + 30) = s.data.message_value.type;
2278
            pos = 31;
2279
 
2280
            switch (s.data.message_value.type)
2281
            {
2282
                case 0x10: *(buf + pos) = s.data.message_value.content.byte; break;
2283
 
2284
                case 0x11: *(buf + pos) = s.data.message_value.content.ch; break;
2285
 
2286
                case 0x20:
2287
                    *(buf + pos) = s.data.message_value.content.integer >> 8;
2288
                    pos++;
2289
                    *(buf + pos) = s.data.message_value.content.integer;
2290
                    break;
2291
 
2292
                case 0x21:
2293
                    *(buf + pos) = s.data.message_value.content.sinteger >> 8;
2294
                    pos++;
2295
                    *(buf + pos) = s.data.message_value.content.sinteger;
2296
                    break;
2297
 
2298
                case 0x40:
2299
                    *(buf + pos) = s.data.message_value.content.dword >> 24;
2300
                    pos++;
2301
                    *(buf + pos) = s.data.message_value.content.dword >> 16;
2302
                    pos++;
2303
                    *(buf + pos) = s.data.message_value.content.dword >> 8;
2304
                    pos++;
2305
                    *(buf + pos) = s.data.message_value.content.dword;
2306
                    break;
2307
 
2308
                case 0x41:
2309
                    *(buf + pos) = s.data.message_value.content.sdword >> 24;
2310
                    pos++;
2311
                    *(buf + pos) = s.data.message_value.content.sdword >> 16;
2312
                    pos++;
2313
                    *(buf + pos) = s.data.message_value.content.sdword >> 8;
2314
                    pos++;
2315
                    *(buf + pos) = s.data.message_value.content.sdword;
2316
                    break;
2317
 
2318
                case 0x4f:
2319
                    memcpy(buf + pos, &s.data.message_value.content.fvalue, 4);
2320
                    pos += 3;
2321
                    break;
2322
 
2323
                case 0x8f:
2324
                    memcpy(buf + pos, &s.data.message_value.content.fvalue, 8);
2325
                    pos += 3;
2326
                    break;
2327
            }
2328
 
2329
            pos++;
2330
            *(buf + pos) = calcChecksum(buf, pos);
2331
            valid = true;
13 andreas 2332
        break;
11 andreas 2333
 
2334
        case 0x000b:
2335
        case 0x000c:
2336
        case 0x008b:
2337
        case 0x008c:
2338
            *(buf + 22) = s.data.message_string.device >> 8;
2339
            *(buf + 23) = s.data.message_string.device;
2340
            *(buf + 24) = s.data.message_string.port >> 8;
2341
            *(buf + 25) = s.data.message_string.port;
2342
            *(buf + 26) = s.data.message_string.system >> 8;
2343
            *(buf + 27) = s.data.message_string.system;
2344
            *(buf + 28) = s.data.message_string.type;
2345
            *(buf + 29) = s.data.message_string.length >> 8;
2346
            *(buf + 30) = s.data.message_string.length;
2347
            pos = 31;
2348
            memcpy(buf + pos, s.data.message_string.content, s.data.message_string.length);
2349
            pos += s.data.message_string.length;
2350
            *(buf + pos) = calcChecksum(buf, pos);
2351
            valid = true;
13 andreas 2352
        break;
11 andreas 2353
 
2354
        case 0x008d:    // Custom event
2355
            *(buf + 22) = s.data.customEvent.device >> 8;
2356
            *(buf + 23) = s.data.customEvent.device;
2357
            *(buf + 24) = s.data.customEvent.port >> 8;
2358
            *(buf + 25) = s.data.customEvent.port;
2359
            *(buf + 26) = s.data.customEvent.system >> 8;
2360
            *(buf + 27) = s.data.customEvent.system;
2361
            *(buf + 28) = s.data.customEvent.ID >> 8;
2362
            *(buf + 29) = s.data.customEvent.ID;
2363
            *(buf + 30) = s.data.customEvent.type >> 8;
2364
            *(buf + 31) = s.data.customEvent.type;
2365
            *(buf + 32) = s.data.customEvent.flag >> 8;
2366
            *(buf + 33) = s.data.customEvent.flag;
2367
            *(buf + 34) = s.data.customEvent.value1 >> 24;
2368
            *(buf + 35) = s.data.customEvent.value1 >> 16;
2369
            *(buf + 36) = s.data.customEvent.value1 >> 8;
2370
            *(buf + 37) = s.data.customEvent.value1;
2371
            *(buf + 38) = s.data.customEvent.value2 >> 24;
2372
            *(buf + 39) = s.data.customEvent.value2 >> 16;
2373
            *(buf + 40) = s.data.customEvent.value2 >> 8;
2374
            *(buf + 41) = s.data.customEvent.value2;
2375
            *(buf + 42) = s.data.customEvent.value3 >> 24;
2376
            *(buf + 43) = s.data.customEvent.value3 >> 16;
2377
            *(buf + 44) = s.data.customEvent.value3 >> 8;
2378
            *(buf + 45) = s.data.customEvent.value3;
2379
            *(buf + 46) = s.data.customEvent.dtype;
2380
            *(buf + 47) = s.data.customEvent.length >> 8;
2381
            *(buf + 48) = s.data.customEvent.length;
2382
            pos = 49;
2383
 
2384
            if (s.data.customEvent.length > 0)
2385
            {
2386
                memcpy(buf + pos, s.data.customEvent.data, s.data.customEvent.length);
2387
                pos += s.data.customEvent.length;
2388
            }
2389
 
2390
            *(buf + pos) = 0;
2391
            *(buf + pos + 1) = 0;
2392
            pos += 2;
2393
            *(buf + pos) = calcChecksum(buf, pos);
2394
            valid = true;
13 andreas 2395
        break;
11 andreas 2396
 
2397
        case 0x0090:
2398
            *(buf + 22) = s.data.sendPortNumber.device >> 8;
2399
            *(buf + 23) = s.data.sendPortNumber.device;
2400
            *(buf + 24) = s.data.sendPortNumber.system >> 8;
2401
            *(buf + 25) = s.data.sendPortNumber.system;
2402
            *(buf + 26) = s.data.sendPortNumber.pcount >> 8;
2403
            *(buf + 27) = s.data.sendPortNumber.pcount;
2404
            *(buf + 28) = calcChecksum(buf, 28);
2405
            valid = true;
13 andreas 2406
        break;
11 andreas 2407
 
2408
        case 0x0091:
2409
        case 0x0092:
2410
            *(buf + 22) = s.data.sendOutpChannels.device >> 8;
2411
            *(buf + 23) = s.data.sendOutpChannels.device;
2412
            *(buf + 24) = s.data.sendOutpChannels.port >> 8;
2413
            *(buf + 25) = s.data.sendOutpChannels.port;
2414
            *(buf + 26) = s.data.sendOutpChannels.system >> 8;
2415
            *(buf + 27) = s.data.sendOutpChannels.system;
2416
            *(buf + 28) = s.data.sendOutpChannels.count >> 8;
2417
            *(buf + 29) = s.data.sendOutpChannels.count;
2418
            *(buf + 30) = calcChecksum(buf, 30);
2419
            valid = true;
13 andreas 2420
        break;
11 andreas 2421
 
2422
        case 0x0093:
2423
        case 0x0094:
2424
            *(buf + 22) = s.data.sendSize.device >> 8;
2425
            *(buf + 23) = s.data.sendSize.device;
2426
            *(buf + 24) = s.data.sendSize.port >> 8;
2427
            *(buf + 25) = s.data.sendSize.port;
2428
            *(buf + 26) = s.data.sendSize.system >> 8;
2429
            *(buf + 27) = s.data.sendSize.system;
2430
            *(buf + 28) = s.data.sendSize.type;
2431
            *(buf + 29) = s.data.sendSize.length >> 8;
2432
            *(buf + 30) = s.data.sendSize.length;
2433
            *(buf + 31) = calcChecksum(buf, 31);
2434
            valid = true;
13 andreas 2435
        break;
11 andreas 2436
 
2437
        case 0x0095:
2438
            *(buf + 22) = s.data.sendLevSupport.device >> 8;
2439
            *(buf + 23) = s.data.sendLevSupport.device;
2440
            *(buf + 24) = s.data.sendLevSupport.port >> 8;
2441
            *(buf + 25) = s.data.sendLevSupport.port;
2442
            *(buf + 26) = s.data.sendLevSupport.system >> 8;
2443
            *(buf + 27) = s.data.sendLevSupport.system;
2444
            *(buf + 28) = s.data.sendLevSupport.level >> 8;
2445
            *(buf + 29) = s.data.sendLevSupport.level;
2446
            *(buf + 30) = s.data.sendLevSupport.num;
2447
            *(buf + 31) = s.data.sendLevSupport.types[0];
2448
            *(buf + 32) = s.data.sendLevSupport.types[1];
2449
            *(buf + 33) = s.data.sendLevSupport.types[2];
2450
            *(buf + 34) = s.data.sendLevSupport.types[3];
2451
            *(buf + 35) = s.data.sendLevSupport.types[4];
2452
            *(buf + 36) = s.data.sendLevSupport.types[5];
2453
            *(buf + 37) = calcChecksum(buf, 37);
2454
            valid = true;
13 andreas 2455
        break;
11 andreas 2456
 
2457
        case 0x0096:
2458
            *(buf + 22) = s.data.sendStatusCode.device >> 8;
2459
            *(buf + 23) = s.data.sendStatusCode.device;
2460
            *(buf + 24) = s.data.sendStatusCode.port >> 8;
2461
            *(buf + 25) = s.data.sendStatusCode.port;
2462
            *(buf + 26) = s.data.sendStatusCode.system >> 8;
2463
            *(buf + 27) = s.data.sendStatusCode.system;
2464
            *(buf + 28) = s.data.sendStatusCode.status >> 8;
2465
            *(buf + 29) = s.data.sendStatusCode.status;
2466
            *(buf + 30) = s.data.sendStatusCode.type;
2467
            *(buf + 31) = s.data.sendStatusCode.length >> 8;
2468
            *(buf + 32) = s.data.sendStatusCode.length;
2469
            pos = 33;
2470
            memset((void*)&s.data.sendStatusCode.str[0], 0, sizeof(s.data.sendStatusCode.str));
2471
            memcpy(buf + pos, s.data.sendStatusCode.str, s.data.sendStatusCode.length);
2472
            pos += s.data.sendStatusCode.length;
2473
            *(buf + pos) = calcChecksum(buf, pos);
2474
            valid = true;
13 andreas 2475
        break;
11 andreas 2476
 
2477
        case 0x0097:
2478
            *(buf + 22) = s.data.srDeviceInfo.device >> 8;
2479
            *(buf + 23) = s.data.srDeviceInfo.device;
2480
            *(buf + 24) = s.data.srDeviceInfo.system >> 8;
2481
            *(buf + 25) = s.data.srDeviceInfo.system;
2482
            *(buf + 26) = s.data.srDeviceInfo.flag >> 8;
2483
            *(buf + 27) = s.data.srDeviceInfo.flag;
2484
            *(buf + 28) = s.data.srDeviceInfo.objectID;
2485
            *(buf + 29) = s.data.srDeviceInfo.parentID;
2486
            *(buf + 30) = s.data.srDeviceInfo.herstID >> 8;
2487
            *(buf + 31) = s.data.srDeviceInfo.herstID;
2488
            *(buf + 32) = s.data.srDeviceInfo.deviceID >> 8;
2489
            *(buf + 33) = s.data.srDeviceInfo.deviceID;
2490
            pos = 34;
2491
            memcpy(buf + pos, s.data.srDeviceInfo.serial, 16);
2492
            pos += 16;
2493
            *(buf + pos) = s.data.srDeviceInfo.fwid >> 8;
2494
            pos++;
2495
            *(buf + pos) = s.data.srDeviceInfo.fwid;
2496
            pos++;
2497
            memcpy(buf + pos, s.data.srDeviceInfo.info, s.data.srDeviceInfo.len);
2498
            pos += s.data.srDeviceInfo.len;
2499
            *(buf + pos) = calcChecksum(buf, pos);
2500
            valid = true;
13 andreas 2501
        break;
11 andreas 2502
 
2503
        case 0x0098:
2504
            *(buf + 22) = s.data.reqPortCount.device >> 8;
2505
            *(buf + 23) = s.data.reqPortCount.device;
2506
            *(buf + 24) = s.data.reqPortCount.system >> 8;
2507
            *(buf + 25) = s.data.reqPortCount.system;
2508
            *(buf + 26) = calcChecksum(buf, 26);
2509
            valid = true;
13 andreas 2510
        break;
11 andreas 2511
 
2512
        case 0x0204:    // file transfer
2513
            *(buf + 22) = s.data.filetransfer.ftype >> 8;
2514
            *(buf + 23) = s.data.filetransfer.ftype;
2515
            *(buf + 24) = s.data.filetransfer.function >> 8;
2516
            *(buf + 25) = s.data.filetransfer.function;
2517
            pos = 26;
2518
 
2519
            switch (s.data.filetransfer.function)
2520
            {
2521
                case 0x0001:
2522
                    *(buf + 26) = s.data.filetransfer.unk;
2523
                    *(buf + 27) = s.data.filetransfer.unk1;
2524
                    pos = 28;
13 andreas 2525
                break;
11 andreas 2526
 
2527
                case 0x0003:
2528
                    *(buf + 26) = s.data.filetransfer.unk >> 8;
2529
                    *(buf + 27) = s.data.filetransfer.unk;
2530
                    pos = 28;
2531
 
2532
                    for (uint32_t i = 0; i < s.data.filetransfer.unk && pos < (s.hlen + 3); i++)
2533
                    {
2534
                        *(buf + pos) = s.data.filetransfer.data[i];
2535
                        pos++;
2536
                    }
13 andreas 2537
                break;
11 andreas 2538
 
2539
                case 0x0101:
2540
                    if (s.data.filetransfer.ftype == 0)
2541
                    {
2542
                        *(buf + 26) = s.data.filetransfer.unk >> 24;
2543
                        *(buf + 27) = s.data.filetransfer.unk >> 16;
2544
                        *(buf + 28) = s.data.filetransfer.unk >> 8;
2545
                        *(buf + 29) = s.data.filetransfer.unk;
2546
                        *(buf + 30) = s.data.filetransfer.unk1 >> 24;
2547
                        *(buf + 31) = s.data.filetransfer.unk1 >> 16;
2548
                        *(buf + 32) = s.data.filetransfer.unk1 >> 8;
2549
                        *(buf + 33) = s.data.filetransfer.unk1;
2550
                        *(buf + 34) = s.data.filetransfer.unk2 >> 24;
2551
                        *(buf + 35) = s.data.filetransfer.unk2 >> 16;
2552
                        *(buf + 36) = s.data.filetransfer.unk2 >> 8;
2553
                        *(buf + 37) = s.data.filetransfer.unk2;
2554
                        *(buf + 38) = 0x00;
2555
                        *(buf + 39) = 0x00;
2556
                        *(buf + 40) = 0x3e;
2557
                        *(buf + 41) = 0x75;
2558
                        pos = 42;
2559
                        len = 0;
2560
 
2561
                        while (s.data.filetransfer.data[len] != 0)
2562
                        {
2563
                            *(buf + pos) = s.data.filetransfer.data[len];
2564
                            len++;
2565
                            pos++;
2566
                        }
2567
 
2568
                        *(buf + pos) = 0;
2569
                        pos++;
2570
                    }
2571
                    else
2572
                    {
2573
                        *(buf + 26) = s.data.filetransfer.unk >> 24;
2574
                        *(buf + 27) = s.data.filetransfer.unk >> 16;
2575
                        *(buf + 28) = s.data.filetransfer.unk >> 8;
2576
                        *(buf + 29) = s.data.filetransfer.unk;
2577
                        *(buf + 30) = 0x00;
2578
                        *(buf + 31) = 0x00;
2579
                        *(buf + 32) = 0x00;
2580
                        *(buf + 33) = 0x00;
2581
                        pos = 34;
2582
                    }
2583
 
13 andreas 2584
                break;
11 andreas 2585
 
2586
                case 0x0102:
2587
                    *(buf + 26) = 0x00;
2588
                    *(buf + 27) = 0x00;
2589
                    *(buf + 28) = 0x00;
2590
                    *(buf + 29) = s.data.filetransfer.info1;        // dir flag
2591
                    *(buf + 30) = s.data.filetransfer.info2 >> 8;   // # entries
2592
                    *(buf + 31) = s.data.filetransfer.info2;
2593
                    *(buf + 32) = s.data.filetransfer.unk >> 8;     // counter
2594
                    *(buf + 33) = s.data.filetransfer.unk;
2595
                    *(buf + 34) = s.data.filetransfer.unk1 >> 24;   // file size
2596
                    *(buf + 35) = s.data.filetransfer.unk1 >> 16;
2597
                    *(buf + 36) = s.data.filetransfer.unk1 >> 8;
2598
                    *(buf + 37) = s.data.filetransfer.unk1;
2599
                    *(buf + 38) = (s.data.filetransfer.info1 == 1) ? 0x0c : 0x0b;
2600
                    *(buf + 39) = (s.data.filetransfer.info1 == 1) ? 0x0e : 0x13;
2601
                    *(buf + 40) = 0x07;
2602
                    *(buf + 41) = s.data.filetransfer.unk2 >> 24;   // Date
2603
                    *(buf + 42) = s.data.filetransfer.unk2 >> 16;
2604
                    *(buf + 43) = s.data.filetransfer.unk2 >> 8;
2605
                    *(buf + 44) = s.data.filetransfer.unk2;
2606
                    pos = 45;
2607
                    len = 0;
2608
 
2609
                    while (s.data.filetransfer.data[len] != 0)
2610
                    {
2611
                        *(buf + pos) = s.data.filetransfer.data[len];
2612
                        pos++;
2613
                        len++;
2614
                    }
2615
 
2616
                    *(buf + pos) = 0;
2617
                    pos++;
13 andreas 2618
                break;
11 andreas 2619
 
2620
                case 0x0103:
2621
                    *(buf + 26) = s.data.filetransfer.unk >> 8;
2622
                    *(buf + 27) = s.data.filetransfer.unk;
2623
                    *(buf + 28) = s.data.filetransfer.unk1 >> 24;
2624
                    *(buf + 29) = s.data.filetransfer.unk1 >> 16;
2625
                    *(buf + 30) = s.data.filetransfer.unk1 >> 8;
2626
                    *(buf + 31) = s.data.filetransfer.unk1;
2627
                    pos = 32;
13 andreas 2628
                break;
11 andreas 2629
 
2630
                case 0x105:
2631
                    *(buf + 26) = s.data.filetransfer.unk >> 24;
2632
                    *(buf + 27) = s.data.filetransfer.unk >> 16;
2633
                    *(buf + 28) = s.data.filetransfer.unk >> 8;
2634
                    *(buf + 29) = s.data.filetransfer.unk;
2635
                    *(buf + 30) = s.data.filetransfer.unk1 >> 24;
2636
                    *(buf + 31) = s.data.filetransfer.unk1 >> 16;
2637
                    *(buf + 32) = s.data.filetransfer.unk1 >> 8;
2638
                    *(buf + 33) = s.data.filetransfer.unk1;
2639
                    pos = 34;
13 andreas 2640
                break;
11 andreas 2641
            }
2642
 
2643
            *(buf + pos) = calcChecksum(buf, pos);
2644
            valid = true;
13 andreas 2645
        break;
11 andreas 2646
 
2647
        case 0x0581:    // Pong
2648
            *(buf + 22) = s.data.srDeviceInfo.device >> 8;
2649
            *(buf + 23) = s.data.srDeviceInfo.device;
2650
            *(buf + 24) = s.data.srDeviceInfo.system >> 8;
2651
            *(buf + 25) = s.data.srDeviceInfo.system;
2652
            *(buf + 26) = s.data.srDeviceInfo.herstID >> 8;
2653
            *(buf + 27) = s.data.srDeviceInfo.herstID;
2654
            *(buf + 28) = s.data.srDeviceInfo.deviceID >> 8;
2655
            *(buf + 29) = s.data.srDeviceInfo.deviceID;
2656
            *(buf + 30) = s.data.srDeviceInfo.info[0];
2657
            *(buf + 31) = s.data.srDeviceInfo.info[1];
2658
            *(buf + 32) = s.data.srDeviceInfo.info[2];
2659
            *(buf + 33) = s.data.srDeviceInfo.info[3];
2660
            *(buf + 34) = s.data.srDeviceInfo.info[4];
2661
            *(buf + 35) = s.data.srDeviceInfo.info[5];
2662
            *(buf + 36) = calcChecksum(buf, 36);
2663
            valid = true;
13 andreas 2664
        break;
11 andreas 2665
    }
2666
 
2667
    if (!valid)
2668
    {
2669
        delete[] buf;
2670
        return 0;
2671
    }
2672
 
13 andreas 2673
//    MSG_TRACE("Buffer:");
2674
//    TError::logHex((char *)buf, s.hlen + 4);
11 andreas 2675
    return buf;
2676
}
2677
 
2678
void TAmxNet::setSerialNum(const string& sn)
2679
{
2680
    DECL_TRACER("TAmxNet::setSerialNum(const string& sn)");
2681
 
2682
    serNum = sn;
2683
    size_t len = (sn.length() > 15) ? 15 : sn.length();
2684
 
2685
    for (size_t i = 0; i < devInfo.size(); i++)
89 andreas 2686
        strncpy(devInfo[i].serialNum, sn.c_str(), len);
11 andreas 2687
}
2688
 
2689
int TAmxNet::countFiles()
2690
{
2691
    DECL_TRACER("TAmxNet::countFiles()");
2692
 
2693
    int count = 0;
2694
    ifstream in;
2695
 
2696
    try
2697
    {
2698
        in.open(TConfig::getProjectPath() + "/manifest.xma", fstream::in);
2699
 
2700
        if (!in)
2701
            return 0;
2702
 
2703
        for (string line; getline(in, line);)
2704
            count++;
2705
 
2706
        in.close();
2707
    }
2708
    catch (exception& e)
2709
    {
2710
        MSG_ERROR("Error: " << e.what());
2711
        return 0;
2712
    }
2713
 
2714
    return count;
2715
}
15 andreas 2716
 
2717
void TAmxNet::sendAllFuncNetwork(int state)
2718
{
2719
    DECL_TRACER("TAmxNet::sendAllFuncNetwork(int state)");
2720
 
17 andreas 2721
    if (mFuncsNetwork.empty())
2722
        return;
2723
 
83 andreas 2724
    MSG_DEBUG("Setting network state to " << state);
15 andreas 2725
    map<ulong, FUNC_NETWORK_t>::iterator iter;
2726
 
118 andreas 2727
    for (iter = mFuncsNetwork.begin(); iter != mFuncsNetwork.end(); ++iter)
15 andreas 2728
        iter->second.func(state);
2729
}
2730
 
2731
void TAmxNet::sendAllFuncTimer(const ANET_BLINK& blink)
2732
{
2733
    DECL_TRACER("TAmxNet::sendAllFuncTimer(const ANET_BLINK& blink)");
2734
 
17 andreas 2735
    if (mFuncsTimer.empty())
2736
        return;
2737
 
15 andreas 2738
    map<ulong, FUNC_TIMER_t>::iterator iter;
2739
 
118 andreas 2740
    for (iter = mFuncsTimer.begin(); iter != mFuncsTimer.end(); ++iter)
15 andreas 2741
        iter->second.func(blink);
2742
}
93 andreas 2743
 
2744
void TAmxNet::setWaitTime(int secs)
2745
{
2746
    DECL_TRACER("TAmxNet::setWaitTime(int secs)");
2747
 
2748
    if (secs <= 0 || secs > 300)    // Maximal 5 minutes
2749
        return;
2750
 
2751
    mOldWaitTime = mWaitTime;
2752
    mWaitTime = secs;
2753
}
2754
 
2755
int TAmxNet::swapWaitTime()
2756
{
2757
    DECL_TRACER("TAmxNet::restoreWaitTime()");
2758
 
2759
    int wt = mWaitTime;
2760
 
2761
    mWaitTime = mOldWaitTime;
2762
    mOldWaitTime = wt;
2763
    return mWaitTime;
2764
}