| 1 |
/* |
| 2 |
* SerialCommandTransceiver.hpp |
| 3 |
* |
| 4 |
* Copyright 2020 Kevin Krüger <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 |
* MA 02110-1301, USA. |
| 20 |
* |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef _SERIALCOMMANDTRANSCEIVER_H_INCLUDED_ |
| 25 |
#define _SERIALCOMMANDTRANSCEIVER_H_INCLUDED_ |
| 26 |
|
| 27 |
#include <cstdint> |
| 28 |
#include <iostream> |
| 29 |
#include <vector> |
| 30 |
#include <deque> |
| 31 |
#include <queue> |
| 32 |
#include <thread> |
| 33 |
|
| 34 |
#include <algorithm> |
| 35 |
#include "config.h" |
| 36 |
#include "INotifyPointingCoordinatesReceived.hpp" |
| 37 |
#include "ISerialInterface.hpp" |
| 38 |
#include "CriticalData.hpp" |
| 39 |
#include "SerialCommand.hpp" |
| 40 |
#include "CircularBuffer.hpp" |
| 41 |
|
| 42 |
namespace SerialDeviceControl |
| 43 |
{ |
| 44 |
//These types have to inherit/implement: |
| 45 |
//-The ISerialInterface.hpp as Interface type. |
| 46 |
//-The INotifyPointingCoordinatesReceived.hpp as callback type |
| 47 |
template<class InterfaceType, class CallbackType> |
| 48 |
class SerialCommandTransceiver |
| 49 |
{ |
| 50 |
public: |
| 51 |
//Create the serial transceiver according to provided template parameters. Requires references of the data received interface implementation and the serial interface implementation. |
| 52 |
SerialCommandTransceiver( |
| 53 |
InterfaceType &interfaceImplementation, |
| 54 |
CallbackType &dataReceivedCallback |
| 55 |
) : |
| 56 |
mInterfaceImplementation(interfaceImplementation), |
| 57 |
mDataReceivedCallback(dataReceivedCallback), |
| 58 |
mThreadRunning(false), |
| 59 |
mSerialReceiverBuffer(0x00), |
| 60 |
mSerialReaderThread() |
| 61 |
{ |
| 62 |
SerialCommand::PushHeader(mMessageHeader); |
| 63 |
} |
| 64 |
|
| 65 |
//Destroys this transceiver, and stops the thread pulling the serial data from the mount. |
| 66 |
virtual ~SerialCommandTransceiver() |
| 67 |
{ |
| 68 |
bool threadRunning = mThreadRunning.Get(); |
| 69 |
|
| 70 |
if(threadRunning) |
| 71 |
{ |
| 72 |
Stop(); |
| 73 |
} |
| 74 |
} |
| 75 |
//Start the serial command dispatching. |
| 76 |
virtual bool Start() |
| 77 |
{ |
| 78 |
mSerialReaderThread = std::thread(&SerialCommandTransceiver::SerialReaderThreadFunction, this); |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
//Stop the serial command dispatching. |
| 84 |
bool Stop() |
| 85 |
{ |
| 86 |
bool running = mThreadRunning.Get(); |
| 87 |
|
| 88 |
if(running) |
| 89 |
{ |
| 90 |
mThreadRunning.Set(false); |
| 91 |
|
| 92 |
mSerialReaderThread.join(); |
| 93 |
} |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
protected: |
| 99 |
//Send a message using the provided serial interface implementation. |
| 100 |
bool SendMessageBuffer( |
| 101 |
uint8_t* buffer, |
| 102 |
size_t offset, |
| 103 |
size_t length |
| 104 |
) |
| 105 |
{ |
| 106 |
return mInterfaceImplementation.Write(buffer, offset, length); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
//Reference to the serial implementation. |
| 111 |
InterfaceType &mInterfaceImplementation; |
| 112 |
|
| 113 |
//Reference to the data received interface. |
| 114 |
CallbackType &mDataReceivedCallback; |
| 115 |
|
| 116 |
//mutex locked running state variable, if set to false the serial receiver thread is terminated. |
| 117 |
CriticalData<bool> mThreadRunning; |
| 118 |
|
| 119 |
//A cicular buffer implementation to receive serial message from the mount. |
| 120 |
CircularBuffer<uint8_t, 256> mSerialReceiverBuffer; |
| 121 |
|
| 122 |
//Contains a message header for convinience. |
| 123 |
std::vector<uint8_t> mMessageHeader; |
| 124 |
|
| 125 |
//movable thread object to control. |
| 126 |
std::thread mSerialReaderThread; |
| 127 |
|
| 128 |
//buffer used when serial messages are parsed. |
| 129 |
std::vector<uint8_t> mParseBuffer; |
| 130 |
|
| 131 |
//When messages are received, try parsing them. |
| 132 |
//It may happen that messages are received in fragments, this function tries to piece together these fragments to valid messages. |
| 133 |
//skip any previous junk if message was found, drop anything until the end of the parsed message, to clean up the buffer. |
| 134 |
void TryParseMessagesFromBuffer() |
| 135 |
{ |
| 136 |
mParseBuffer.clear(); |
| 137 |
|
| 138 |
if(mSerialReceiverBuffer.Size() > 0) |
| 139 |
{ |
| 140 |
mSerialReceiverBuffer.CopyToVector(mParseBuffer); |
| 141 |
|
| 142 |
std::vector<uint8_t>::iterator startPosition = std::search(mParseBuffer.begin(), mParseBuffer.end(), mMessageHeader.begin(), |
| 143 |
mMessageHeader.end()); |
| 144 |
|
| 145 |
std::vector<uint8_t>::iterator endPosition = startPosition + MESSAGE_FRAME_SIZE; |
| 146 |
|
| 147 |
if(startPosition != mParseBuffer.end() && endPosition != mParseBuffer.end()) |
| 148 |
{ |
| 149 |
FloatByteConverter ra_bytes; |
| 150 |
FloatByteConverter dec_bytes; |
| 151 |
|
| 152 |
ra_bytes.bytes[0] = *(startPosition + 5); |
| 153 |
ra_bytes.bytes[1] = *(startPosition + 6); |
| 154 |
ra_bytes.bytes[2] = *(startPosition + 7); |
| 155 |
ra_bytes.bytes[3] = *(startPosition + 8); |
| 156 |
|
| 157 |
dec_bytes.bytes[0] = *(startPosition + 9); |
| 158 |
dec_bytes.bytes[1] = *(startPosition + 10); |
| 159 |
dec_bytes.bytes[2] = *(startPosition + 11); |
| 160 |
dec_bytes.bytes[3] = *(startPosition + 12); |
| 161 |
|
| 162 |
uint8_t cid = *(startPosition + 4); |
| 163 |
float ra = ra_bytes.decimal_number; |
| 164 |
float dec = dec_bytes.decimal_number; |
| 165 |
|
| 166 |
//std::cerr << "COMMAND RECEIVED:" << std::hex << (int)cid << std::endl; |
| 167 |
|
| 168 |
//handle specific response. |
| 169 |
switch(cid) |
| 170 |
{ |
| 171 |
case SerialCommandID::TELESCOPE_SITE_LOCATION_REPORT_COMMAND_ID: |
| 172 |
//std::cout << "new location received!" << std::endl; |
| 173 |
mDataReceivedCallback.OnSiteLocationCoordinatesReceived(ra, dec); |
| 174 |
break; |
| 175 |
|
| 176 |
/* The handbox unfortunately does not report "untracked" coordinates, -> reason for this big state machine. |
| 177 |
* case SerialCommandID::TELESCOPE_POSITION_REPORT_UNTRACKED_COMMAND_ID: |
| 178 |
std::cerr << "untracked pointing report:" << "RA:" << ra << " DEC:" << dec << std::endl; |
| 179 |
break;*/ |
| 180 |
|
| 181 |
case SerialCommandID::TELESCOPE_POSITION_REPORT_COMMAND_ID: |
| 182 |
mDataReceivedCallback.OnPointingCoordinatesReceived(ra, dec); |
| 183 |
break; |
| 184 |
|
| 185 |
default: |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
size_t dropCount = endPosition - mParseBuffer.begin(); |
| 190 |
|
| 191 |
mSerialReceiverBuffer.DiscardFront(dropCount); |
| 192 |
|
| 193 |
//std::cout << "Receive size after :" << mSerialReceiverBuffer.Size() << " dropped " << dropCount << std::endl; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
//Endless loop function of the thread used to receive the serial messages of the mount. |
| 198 |
void SerialReaderThreadFunction() |
| 199 |
{ |
| 200 |
std::cerr << "Serial Reader Thread started!" << std::endl; |
| 201 |
bool running = mThreadRunning.Get(); |
| 202 |
|
| 203 |
mInterfaceImplementation.Open(); |
| 204 |
|
| 205 |
if(running == false) |
| 206 |
{ |
| 207 |
mThreadRunning.Set(true); |
| 208 |
|
| 209 |
do |
| 210 |
{ |
| 211 |
//controller sends status messages about every second so wait a bit |
| 212 |
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::milliseconds(500)); |
| 213 |
|
| 214 |
size_t bufferContent = mInterfaceImplementation.BytesToRead(); |
| 215 |
int16_t data = -1; |
| 216 |
|
| 217 |
bool addSucceed = false; |
| 218 |
|
| 219 |
if(bufferContent > 0) |
| 220 |
{ |
| 221 |
while((data = mInterfaceImplementation.ReadByte()) > -1) |
| 222 |
{ |
| 223 |
addSucceed = mSerialReceiverBuffer.PushBack((uint8_t)data); |
| 224 |
} |
| 225 |
|
| 226 |
if(addSucceed) |
| 227 |
{ |
| 228 |
TryParseMessagesFromBuffer(); |
| 229 |
} |
| 230 |
} |
| 231 |
running = mThreadRunning.Get(); |
| 232 |
} |
| 233 |
while(running == true); |
| 234 |
} |
| 235 |
std::cerr << "Serial Reader Thread stopped!" << std::endl; |
| 236 |
mInterfaceImplementation.Flush(); |
| 237 |
mInterfaceImplementation.Close(); |
| 238 |
} |
| 239 |
}; |
| 240 |
} |
| 241 |
#endif |