rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
ethernet_console.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#if EFI_ETHERNET
4
5#include "lwipthread.h"
6
7#include "lwip/sockets.h"
8
9#include "thread_controller.h"
10#include "tunerstudio.h"
11
12static int listenerSocket = -1;
13static int connectionSocket = -1;
14
15static void do_connection() {
16 if (connectionSocket != -1) {
17 auto localCopy = connectionSocket;
19
20 lwip_close(localCopy);
21 }
22
23 sockaddr_in remote;
24 socklen_t size = sizeof(remote);
25 connectionSocket = lwip_accept(listenerSocket, (sockaddr*)&remote, &size);
26}
27
28class EthernetChannel final : public TsChannelBase {
29public:
30 EthernetChannel()
31 : TsChannelBase("Ethernet")
32 {
33 }
34
35 bool isReady() const override {
36 return connectionSocket != -1;
37 }
38
39 void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override {
40 // If not the end of a packet, set the MSG_MORE flag to indicate to the transport
41 // that we have more to add to the buffer before queuing a flush.
42 auto flags = isEndOfPacket ? 0 : MSG_MORE;
43 lwip_send(connectionSocket, buffer, size, flags);
44 }
45
46 size_t readTimeout(uint8_t* buffer, size_t size, int /*timeout*/) override {
47 auto result = lwip_recv(connectionSocket, buffer, size, /*flags =*/ 0);
48
49 if (result == -1) {
51 return 0;
52 }
53
54 return result;
55 }
56};
57
58static EthernetChannel ethChannel;
59
60struct EthernetThread : public TunerstudioThread {
61 EthernetThread() : TunerstudioThread("Ethernet Console") { }
62
63 TsChannelBase* setupChannel() override {
64 lwipInit(nullptr);
65
66 sockaddr_in address;
67 address.sin_family = AF_INET;
68 address.sin_port = htons(29000);
69 address.sin_addr.s_addr = INADDR_ANY;
70
71 listenerSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);
72 lwip_bind(listenerSocket, (sockaddr*)&address, sizeof(address));
73 lwip_listen(listenerSocket, 1);
74
76
77 return &ethChannel;
78 }
79};
80
81static EthernetThread ethernetConsole;
82
86
87#endif // EFI_ETHERNET
virtual bool isReady() const
virtual void write(const uint8_t *buffer, size_t size, bool isEndOfPacket=false)=0
virtual size_t readTimeout(uint8_t *buffer, size_t size, int timeout)=0
virtual TsChannelBase * setupChannel()=0
static int connectionSocket
static int listenerSocket
static void do_connection()
static EthernetThread ethernetConsole
static EthernetChannel ethChannel
composite packet size
static BigBufferHandle buffer