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