Subversion Repositories tpanel

Rev

Rev 150 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 150 Rev 169
Line 23... Line 23...
23
#include <fstream>
23
#include <fstream>
24
#include <cstring>
24
#include <cstring>
25
#include <string>
25
#include <string>
26
#include <chrono>
26
#include <chrono>
27
#include <thread>
27
#include <thread>
-
 
28
#include <mutex>
28
#include <map>
29
#include <map>
29
 
30
 
30
#include <sys/stat.h>
31
#include <sys/stat.h>
31
#include <sys/types.h>
32
#include <sys/types.h>
32
#include <unistd.h>
33
#include <unistd.h>
Line 41... Line 42...
41
#include "texcept.h"
42
#include "texcept.h"
42
#include "tpagemanager.h"
43
#include "tpagemanager.h"
43
 
44
 
44
using namespace amx;
45
using namespace amx;
45
using namespace std;
46
using namespace std;
-
 
47
using std::mutex;
46
 
48
 
47
using placeholders::_1;
49
using placeholders::_1;
48
using placeholders::_2;
50
using placeholders::_2;
49
 
51
 
50
string cmdList[] =
52
string cmdList[] =
Line 74... Line 76...
74
    "\0"
76
    "\0"
75
};
77
};
76
 
78
 
77
#define NUMBER_CMDS     144
79
#define NUMBER_CMDS     144
78
 
80
 
79
#ifndef __ANDROID__
-
 
80
std::atomic<bool> killed{false};
81
std::atomic<bool> killed{false};
81
std::atomic<bool> _netRunning{false};
82
std::atomic<bool> _netRunning{false};
82
#else
-
 
83
std::atomic<bool> killed{false};
-
 
84
std::atomic<bool> _netRunning{false};
-
 
85
#endif
-
 
86
amx::TAmxNet *gAmxNet = nullptr;
83
amx::TAmxNet *gAmxNet = nullptr;
87
static bool __CommValid = false;
84
static bool __CommValid = false;
88
std::map<ulong, FUNC_NETWORK_t> mFuncsNetwork;
85
std::map<ulong, FUNC_NETWORK_t> mFuncsNetwork;
89
std::map<ulong, FUNC_TIMER_t> mFuncsTimer;
86
std::map<ulong, FUNC_TIMER_t> mFuncsTimer;
90
 
87
 
-
 
88
//std::vector<ANET_COMMAND> TAmxNet::comStack; // commands to answer
91
extern TPageManager *gPageManager;
89
extern TPageManager *gPageManager;
92
 
90
 
-
 
91
//mutex read_mutex;
-
 
92
 
93
TAmxNet::TAmxNet()
93
TAmxNet::TAmxNet()
94
{
94
{
95
    DECL_TRACER("TAmxNet::TAmxNet()");
95
    DECL_TRACER("TAmxNet::TAmxNet()");
96
 
96
 
97
    init();
97
    init();
Line 125... Line 125...
125
TAmxNet::~TAmxNet()
125
TAmxNet::~TAmxNet()
126
{
126
{
127
    DECL_TRACER("TAmxNet::~TAmxNet()");
127
    DECL_TRACER("TAmxNet::~TAmxNet()");
128
 
128
 
129
    callback = 0;
129
    callback = 0;
-
 
130
    write_busy = false;
130
    stop();
131
    stop();
131
 
132
 
132
    if (mSocket)
133
    if (mSocket)
133
    {
134
    {
134
        delete mSocket;
135
        delete mSocket;
Line 169... Line 170...
169
    if (panName.find("MVP") != string::npos && panName.find("5200") != string::npos)
170
    if (panName.find("MVP") != string::npos && panName.find("5200") != string::npos)
170
    {
171
    {
171
        devID = 0x0149;
172
        devID = 0x0149;
172
        fwID = 0x0310;
173
        fwID = 0x0310;
173
    }
174
    }
-
 
175
    else if (panName.find("NX-CV7") != string::npos)
-
 
176
    {
-
 
177
        devID = 0x0123;
-
 
178
        fwID = 0x0135;
-
 
179
    }
174
 
180
 
175
    // Initialize the devive info structure
181
    // Initialize the devive info structure
176
    DEVICE_INFO di;
182
    DEVICE_INFO di;
177
    // Answer to MC = 0x0017 --> MC = 0x0097
183
    // Answer to MC = 0x0017 --> MC = 0x0097
178
    di.objectID = 0;
184
    di.objectID = 0;
179
    di.parentID = 0;
185
    di.parentID = 0;
180
    di.manufacturerID = 1;
186
    di.manufacturerID = 1;
181
    di.deviceID = devID;
187
    di.deviceID = devID;
182
    memset(di.serialNum, 0x20, sizeof(di.serialNum));
188
    memset(di.serialNum, 0, sizeof(di.serialNum));
183
 
189
 
184
    if (!serNum.empty())
190
    if (!serNum.empty())
185
        memcpy(di.serialNum, serNum.c_str(), serNum.length());
191
        memcpy(di.serialNum, serNum.c_str(), serNum.length());
186
 
192
 
187
    di.firmwareID = fwID;
193
    di.firmwareID = fwID;
Line 389... Line 395...
389
        {
395
        {
390
            // Start the input actor.
396
            // Start the input actor.
391
            start_read();
397
            start_read();
392
 
398
 
393
            // Start the output actor.
399
            // Start the output actor.
394
            if (isRunning())
400
            if (isRunning() && !write_busy)
395
                start_write();
401
                runWrite();
396
        }
402
        }
397
 
403
 
398
        if (!stopped_ && (killed || prg_stopped))
404
        if (!stopped_ && (killed || prg_stopped))
399
            stop();
405
            stop();
400
    }
406
    }
Line 1433... Line 1439...
1433
            comStack.push_back(com);
1439
            comStack.push_back(com);
1434
            mSendReady = true;
1440
            mSendReady = true;
1435
        break;
1441
        break;
1436
    }
1442
    }
1437
 
1443
 
1438
    if (mSendReady)
1444
    if (mSendReady && !write_busy)
1439
        start_write();
1445
        runWrite();
1440
 
1446
 
1441
    return mSendReady;
1447
    return mSendReady;
1442
}
1448
}
1443
 
1449
 
1444
void TAmxNet::handleFTransfer(ANET_SEND &s, ANET_FILETRANSFER &ft)
1450
void TAmxNet::handleFTransfer(ANET_SEND &s, ANET_FILETRANSFER &ft)
Line 2062... Line 2068...
2062
    }
2068
    }
2063
 
2069
 
2064
    return pos;
2070
    return pos;
2065
}
2071
}
2066
 
2072
 
-
 
2073
void TAmxNet::runWrite()
-
 
2074
{
-
 
2075
    DECL_TRACER("TAmxNet::runWrite()");
-
 
2076
 
-
 
2077
    if (write_busy)
-
 
2078
        return;
-
 
2079
 
-
 
2080
    try
-
 
2081
    {
-
 
2082
        mWriteThread = std::thread([=] { this->start_write(); });
-
 
2083
        mWriteThread.detach();
-
 
2084
    }
-
 
2085
    catch (std::exception& e)
-
 
2086
    {
-
 
2087
        MSG_ERROR("Error starting write thread: " << e.what());
-
 
2088
        _netRunning = false;
-
 
2089
    }
-
 
2090
}
-
 
2091
 
2067
void TAmxNet::start_write()
2092
void TAmxNet::start_write()
2068
{
2093
{
2069
    DECL_TRACER("TAmxNet::start_write()");
2094
    DECL_TRACER("TAmxNet::start_write()");
2070
 
2095
 
2071
    if (!__CommValid || !mSocket || !isRunning() || !mSocket->isConnected())
2096
    if (!__CommValid || !mSocket || !isRunning() || !mSocket->isConnected())
Line 2074... Line 2099...
2074
    if (write_busy)
2099
    if (write_busy)
2075
        return;
2100
        return;
2076
 
2101
 
2077
    write_busy = true;
2102
    write_busy = true;
2078
 
2103
 
2079
    while (comStack.size() > 0)
2104
    while (write_busy && !_restart_ && !killed && _netRunning)
2080
    {
2105
    {
2081
        if (!isRunning())
2106
        while (comStack.size() > 0)
2082
        {
2107
        {
-
 
2108
            if (!isRunning())
-
 
2109
            {
2083
            comStack.clear();
2110
                comStack.clear();
2084
            write_busy = false;
2111
                write_busy = false;
2085
            return;
2112
                return;
2086
        }
2113
            }
2087
 
2114
 
2088
        mSend = comStack.at(0);
2115
            mSend = comStack.at(0);
2089
        comStack.erase(comStack.begin());   // delete oldest element
2116
            comStack.erase(comStack.begin());   // delete oldest element
2090
        unsigned char *buf = makeBuffer(mSend);
2117
            unsigned char *buf = makeBuffer(mSend);
2091
 
2118
 
2092
        if (buf == nullptr)
2119
            if (buf == nullptr)
2093
        {
2120
            {
2094
            MSG_ERROR("Error creating a buffer! Token number: " << mSend.MC);
2121
                MSG_ERROR("Error creating a buffer! Token number: " << mSend.MC);
2095
            continue;
2122
                continue;
-
 
2123
            }
-
 
2124
 
-
 
2125
            MSG_DEBUG("Wrote buffer with " << (mSend.hlen + 4) << " bytes.");
-
 
2126
            mSocket->send((char *)buf, mSend.hlen + 4);
-
 
2127
            delete[] buf;
2096
        }
2128
        }
2097
 
2129
 
2098
        MSG_DEBUG("Wrote buffer with " << (mSend.hlen + 4) << " bytes.");
2130
        mSendReady = false;
2099
        mSocket->send((char *)buf, mSend.hlen + 4);
2131
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
2100
        delete[] buf;
-
 
2101
    }
2132
    }
2102
 
2133
 
2103
    mSendReady = false;
-
 
2104
    write_busy = false;
2134
    write_busy = false;
2105
}
2135
}
2106
 
2136
 
2107
uint16_t TAmxNet::swapWord(uint16_t w)
2137
uint16_t TAmxNet::swapWord(uint16_t w)
2108
{
2138
{