Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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