File: canserver.cpp

package info (click to toggle)
savvycan 220-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,456 kB
  • sloc: cpp: 61,803; sh: 293; javascript: 91; python: 44; makefile: 8
file content (238 lines) | stat: -rw-r--r-- 6,572 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
//  canserver.cpp
//  SavvyCAN
//
//  Created by Chris Whiteford on 2022-01-21.
//

#include <QObject>
#include <QDebug>
#include <QCanBusFrame>
#include <QSettings>
#include <QStringBuilder>
#include <QtNetwork>

#include "utility.h"
#include "canserver.h"

CANserver::CANserver(QString serverAddressString) :
    CANConnection(serverAddressString, "CANserver", CANCon::CANSERVER, 0, 0, false, 0, 3, 4000, true),
    _udpClient(new QUdpSocket(this)),
    _heartbeatTimer(new QTimer(this))
{
    
    qDebug() << "CANserver: " << "Constructing new Connection...";
    
    CANBus bus_info;
    bus_info.setActive(true);
    bus_info.setListenOnly(true);
    bus_info.setSpeed(500000);

    setBusConfig(0, bus_info);
    setBusConfig(1, bus_info);
    setBusConfig(2, bus_info);
    
    // setup udp client
    this->_canserverAddress = QHostAddress(serverAddressString);
    connect(_udpClient, SIGNAL(readyRead()), this, SLOT(readNetworkData()));

    // setup signal and slot
    connect(_heartbeatTimer, SIGNAL(timeout()), this, SLOT(heartbeatTimerSlot()));
    _heartbeatTimer->stop();
}


CANserver::~CANserver()
{
    qDebug() << "CANserver: " << "Deconstructing Connection...";
    
    stop();
    
    delete _heartbeatTimer; _heartbeatTimer = NULL;
    delete _udpClient; _udpClient = NULL;
}

void CANserver::piStarted()
{
    this->connectToDevice();
}

void CANserver::piSuspend(bool pSuspend)
{
    /* update capSuspended */
    setCapSuspended(pSuspend);

    /* flush queue if we are suspended */
    if(isCapSuspended())
        getQueue().flush();
}


void CANserver::piStop()
{
    this->disconnectFromDevice();
}


bool CANserver::piGetBusSettings(int pBusIdx, CANBus& pBus)
{
    return getBusConfig(pBusIdx, pBus);
}


void CANserver::piSetBusSettings(int pBusIdx, CANBus bus)
{
    /* sanity checks */
    if( (pBusIdx < 0) || pBusIdx >= getNumBuses())
        return;

    /* copy bus config */
    setBusConfig(pBusIdx, bus);
}


bool CANserver::piSendFrame(const CANFrame& )
{
    //We don't support sending frames right now
    return true;
}

void CANserver::connectToDevice()
{
    qDebug() << "CANserver: " << "Establishing UDP connection to a CANserver device...";
    
    bool bindResult = _udpClient->bind(1338);
    qDebug() << "CANserver: " << "UDP Bind result: " << bindResult;
    
    heartbeat();
    
    qDebug() << "CANserver: " << "Scheduling additional heartbeats...";
    _heartbeatTimer->start(3500);

    setStatus(CANCon::CONNECTED);

    CANConStatus stats;
    stats.conStatus = getStatus();
    stats.numHardwareBuses = 3;
    emit status(stats);
}

void CANserver::disconnectFromDevice()
{
    _heartbeatTimer->stop();
    
    QByteArray Data;
    Data.append("bye");
    QNetworkDatagram datagram(Data, _canserverAddress, 1338);
    
    qDebug() << "CANserver: " << "Sending bye...";
    _udpClient->writeDatagram(datagram);

    
    _udpClient->close();
}

void CANserver::heartbeat()
{
    //Send the PANDA hello packet (in this case we are using the v1 version of the protocol because we just want to get ALL the data)
    QByteArray Data;
    Data.append("hello");
    QNetworkDatagram datagram(Data, _canserverAddress, 1338);

    qDebug() << "CANserver: " << "Sending a heartbeat...";
    _udpClient->writeDatagram(datagram);
}

void CANserver::readNetworkData()
{
    QByteArray datagram;
    do {
        datagram.resize(_udpClient->pendingDatagramSize());
        _udpClient->readDatagram(datagram.data(), datagram.size());
    } while (_udpClient->hasPendingDatagrams());

    
    // If capture is suspended bail out after reading the bytes from the network.  No need to parse them
    if(isCapSuspended())
        return;

    
    uint16_t packetCount = datagram.length() / 16;
    //qDebug() << "Processing " << packetCount << " packets";

    //Process this chunk of data
    for (int i = 0 ; i < packetCount; i++)
    {
        uint16_t headerByteLocation = (i*16);
        uint16_t dataByteLocation = (i*16) + 8;

        
        uint32_t headerData1Byte1 = datagram.at(headerByteLocation + 0) & 0xFF;
        uint32_t headerData1Byte2 = datagram.at(headerByteLocation + 1) & 0xFF;
        uint32_t headerData1Byte3 = datagram.at(headerByteLocation + 2) & 0xFF;
        uint32_t headerData1Byte4 = datagram.at(headerByteLocation + 3) & 0xFF;
        
        uint32_t headerData2Byte1 = datagram.at(headerByteLocation + 4) & 0xFF;
        uint32_t headerData2Byte2 = datagram.at(headerByteLocation + 5) & 0xFF;
        uint32_t headerData2Byte3 = datagram.at(headerByteLocation + 6) & 0xFF;
        uint32_t headerData2Byte4 = datagram.at(headerByteLocation + 7) & 0xFF;
        
        
        //printf("HB1: %02X, %02X, %02X, %02X\n", headerData1Byte1, headerData1Byte2, headerData1Byte3, headerData1Byte4);
        //printf("HB2: %02X, %02X, %02X, %02X\n", headerData2Byte1, headerData2Byte2, headerData2Byte3, headerData2Byte4);

        uint32_t headerData1 = headerData1Byte1;
        headerData1 += headerData1Byte2 << 8;
        headerData1 += headerData1Byte3 << 16;
        headerData1 += headerData1Byte4 << 24;

        uint32_t headerData2 = headerData2Byte1;
        headerData2 += headerData2Byte2 << 8;
        headerData2 += headerData2Byte3 << 16;
        headerData2 += headerData2Byte4 << 24;

        uint32_t frameId = headerData1 >> 21;
        uint32_t length = (headerData2 & 0x0F);
        uint32_t busId = (headerData2 >> 4);

        //printf("frameId: %02X, busId: %d, length: %d\n", frameId, busId, length);
        
        CANFrame* frame_p = getQueue().get();
        if(frame_p)
        {
            frame_p->setFrameId(frameId);
            frame_p->setExtendedFrameFormat(0);

            // We need to change the bus id if it is the special CANserver bus id.
            // This keeps us from needing to define 15 busses just to get access to our special one
            if (busId == 15)
            {
                busId = 2;
            }
            frame_p->bus = busId;
            
            frame_p->setFrameType(QCanBusFrame::DataFrame);
            frame_p->isReceived = true;
        
            frame_p->setTimeStamp(QCanBusFrame::TimeStamp::fromMicroSeconds(QDateTime::currentMSecsSinceEpoch() * 1000ul));

            frame_p->setPayload(datagram.mid(dataByteLocation, length));
        
            checkTargettedFrame(*frame_p);

            /* enqueue frame */
            getQueue().queue();
        }
    }

}

void CANserver::heartbeatTimerSlot()
{
    heartbeat();
}

void CANserver::readSettings()
{
    QSettings settings;
}