Tim Zeuner 1 year ago
parent
commit
6577c39a1b
4 changed files with 61 additions and 19 deletions
  1. 1
    1
      Communication/uart_communication.cpp
  2. 52
    11
      Socket/lfr_socket.cpp
  3. 3
    1
      Socket/lfr_socket.h
  4. 5
    6
      lfr_state_machine.cpp

+ 1
- 1
Communication/uart_communication.cpp View File

} }


int LFR_UART::writeDataToFile(uint8_t *buff, uint32_t bufferLength) { int LFR_UART::writeDataToFile(uint8_t *buff, uint32_t bufferLength) {
std::cout << "Sending uart telegram" << std::endl;
//std::cout << "Sending uart telegram" << std::endl;
return write(this->fileDescriptor, buff, bufferLength); return write(this->fileDescriptor, buff, bufferLength);
} }



+ 52
- 11
Socket/lfr_socket.cpp View File



LFR_Socket::LFR_Socket(ExceptionCallback cb): cb(cb) LFR_Socket::LFR_Socket(ExceptionCallback cb): cb(cb)
{ {
bufferIterator = std::begin(buffer);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ {
throw std::runtime_error("Failed opening the socket"); throw std::runtime_error("Failed opening the socket");
bool connected = false; bool connected = false;
while(true) while(true)
{ {
LFR_Socket::LFR_Telegram telegram;
LFR_Socket::LFR_Telegram telegram = {};
bool received = false; bool received = false;
if(!stop && !listeners.empty()) if(!stop && !listeners.empty())
{ {
} }
else else
{ {
//std::cout << "accepted connection" << std::endl;
std::cout << "accepted connection" << std::endl;
pollfds[1].fd = new_socket; pollfds[1].fd = new_socket;
connected = true; connected = true;
} }
} }
else if (connected && pollfds[0].revents & POLLIN) else if (connected && pollfds[0].revents & POLLIN)
{ {
//std::cout << "second connection incoming" << std::endl;
std::cout << "second connection incoming" << std::endl;
int tmp_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen); int tmp_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
close(tmp_socket); close(tmp_socket);
//std::cout << "second connection closed" << std::endl;
std::cout << "second connection closed" << std::endl;
} }
else if (pollfds[1].revents & POLLIN) else if (pollfds[1].revents & POLLIN)
{ {
bytes_received = recv(new_socket, buffer, sizeof(buffer), 0);
if (bytes_received > 0)
LFR_Telegram tmpBuffer = {0};
bytesReceived = recv(new_socket, tmpBuffer, sizeof(tmpBuffer), 0);
if (bytesReceived > 0)
{ {
received = true;
std::copy(std::begin(buffer), std::end(buffer), std::begin(telegram));
//std::cout << "message received" << std::endl;
if(bufferIterator - std::begin(buffer) + bytesReceived > 1024)
{
//Too long for buffer. Clear it.
std::fill(std::begin(buffer), std::end(buffer), 0);
bufferIterator = std::begin(buffer);
}
bufferIterator = std::copy_if(
std::begin(tmpBuffer),
std::begin(tmpBuffer)+bytesReceived,
bufferIterator,
[](char c){
return(c != '\n' && c != '\r');
});

//std::cout << "bytes received: " << bytesReceived << std::endl;
//received = true;
} }
else if(bytes_received == 0)
else if(bytesReceived == 0)
{ {
connected = false; connected = false;
close(pollfds[1].fd); close(pollfds[1].fd);
//std::cout << "connection closed by peer" << std::endl;
std::cout << "connection closed by peer" << std::endl;
} }
} }
} }
} }
} }


// Search the buffer for the start of a telegram
char *telegramStart = NULL;
telegramStart = strstr(buffer, "aa");
if(telegramStart && telegramStart != std::begin(buffer)) {
//Move the content of the buffer
auto delta = bufferIterator - telegramStart;
std::copy(telegramStart, std::end(buffer), std::begin(buffer));
bufferIterator = std::begin(buffer) + delta;
}

// Search the buffer for the end of a telegram
char *telegramEnd = NULL;
telegramEnd = strstr(buffer, "zz");
if(telegramEnd && telegramStart && telegramEnd-telegramStart>2) {
std::copy(telegramStart+2, telegramEnd, telegram);
received = true;
std::copy(telegramEnd+2, std::end(buffer), std::begin(buffer));
bufferIterator = std::begin(buffer);
}
else if(telegramEnd && telegramStart && telegramEnd-telegramStart<=2)
{
//Got an empty telegram
std::copy(telegramEnd, std::end(buffer), std::begin(buffer));
bufferIterator = std::begin(buffer);
}
//Invoke the callback method (ListenerPair second -> ListenerCallback) //Invoke the callback method (ListenerPair second -> ListenerCallback)
if (received) if (received)
{ {

+ 3
- 1
Socket/lfr_socket.h View File

#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <exception> #include <exception>
#include <cstring>


class LFR_Socket class LFR_Socket
{ {
std::unique_ptr<std::thread> thread; std::unique_ptr<std::thread> thread;
mutable std::mutex mutex; mutable std::mutex mutex;


int server_fd, new_socket, bytes_received;
int server_fd, new_socket, bytesReceived;
struct sockaddr_in address; struct sockaddr_in address;
int opt = 1; int opt = 1;
int addrlen = sizeof(address); int addrlen = sizeof(address);
char buffer[1024] = {0}; char buffer[1024] = {0};
char* bufferIterator;


//void provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi); //void provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi);

+ 5
- 6
lfr_state_machine.cpp View File

std::vector<std::string> splitStr = split(s, ";"); std::vector<std::string> splitStr = split(s, ";");
if(!checkStringValidity(splitStr)) if(!checkStringValidity(splitStr))
{ {
std::cout<<"Invalid String" << std::endl;
std::cout<<"Error: Invalid String: " << s << std::endl;
return; return;
} }
else
{
std::cout<< s << std::endl;
}
double wheels[4] = {0.0, 0.0, 0.0, 0.0}; double wheels[4] = {0.0, 0.0, 0.0, 0.0};
int mode = std::stoi(splitStr[0]); int mode = std::stoi(splitStr[0]);
if(mode == 0) { if(mode == 0) {
currentState = &newState; currentState = &newState;
currentState->enter(this); currentState->enter(this);
} }
else
{
std::cout << "no switch" << std::endl;
}

} }


void LFR_StateMachine::enterAutonomous() void LFR_StateMachine::enterAutonomous()

Loading…
Cancel
Save