Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MulticastResponder.cpp « MulticastDiscovery « Networking « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c2227c33616ffa7c376f259055885179e1ca3dc2 (plain)
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
/*
 * MulticastResponder.cpp
 *
 *  Created on: 12 Jul 2022
 *      Author: David
 */

#include "MulticastResponder.h"

#if SUPPORT_MULTICAST_DISCOVERY

#include <Platform/RepRap.h>
#include <Platform/Platform.h>
#include <Hardware/ExceptionHandlers.h>

#include "fgmc_header.h"
#include "fgmc_protocol.h"

extern "C" {
#include "LwipEthernet/Lwip/src/include/lwip/udp.h"
#include "LwipEthernet/Lwip/src/include/lwip/igmp.h"
extern struct netif gs_net_if;
}

static constexpr ip_addr_t ourGroup = IPADDR4_INIT_BYTES(239, 255, 2, 3);

static udp_pcb *ourPcb = nullptr;
static pbuf * volatile receivedPbuf = nullptr;
static volatile uint32_t receivedIpAddr;
static volatile uint16_t receivedPort;
static uint16_t lastMessageReceivedPort;
static unsigned int messagesProcessed = 0;
static FGMCProtocol *fgmcHandler = nullptr;
static uint32_t ticksToReboot = 0;
static uint32_t whenRebootScheduled;

static bool active = false;

// Receive callback function
extern "C" void rcvFunc(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) noexcept
{
	if (active && receivedPbuf == nullptr)
	{
		receivedIpAddr = addr->addr;
		receivedPort = port;
		receivedPbuf = p;
	}
	else
	{
		pbuf_free(p);
	}
}

void MulticastResponder::Init() noexcept
{
	// Nothing needed here yet
}

// Do some work, returning true if we did anything significant
void MulticastResponder::Spin() noexcept
{
	if (ticksToReboot != 0)
	{
		if (millis() - whenRebootScheduled > ticksToReboot)
		{
			reprap.EmergencyStop();									// this disables heaters and drives - Duet WiFi pre-production boards need drives disabled here
			SoftwareReset(SoftwareResetReason::user);				// doesn't return
		}

	}
	else
	{
		pbuf *rxPbuf = receivedPbuf;
		if (rxPbuf != nullptr)
		{
			lastMessageReceivedPort = receivedPort;
#if 0
			debugPrintf("Rx UDP: addr %u.%u.%u.%u port %u data",
				(unsigned int)(receivedIpAddr & 0xFF), (unsigned int)((receivedIpAddr >> 8) & 0xFF), (unsigned int)((receivedIpAddr >> 16) & 0xFF), (unsigned int)((receivedIpAddr >> 24) & 0xFF), lastMessageReceivedPort);
			for (size_t i = 0; i < rxPbuf->len; ++i)
			{
				debugPrintf(" %02x", ((const uint8_t*)(rxPbuf->payload))[i]);
			}
			debugPrintf("\n");
#endif
			receivedPbuf = nullptr;
			fgmcHandler->handleStream(0, (uint8_t *)rxPbuf->payload, rxPbuf->len);
			pbuf_free(rxPbuf);
			++messagesProcessed;
		}
	}
}

void MulticastResponder::Diagnostics(MessageType mtype) noexcept
{
	reprap.GetPlatform().MessageF(mtype, "=== Multicast handler ===\nResponder is %s, messages processed %u\n", (active) ? "active" : "inactive", messagesProcessed);
}

void MulticastResponder::Start(TcpPort port) noexcept
{
	if (fgmcHandler == nullptr)
	{
		fgmcHandler = new FGMCProtocol;
		fgmcHandler->init();
	}

	if (ourPcb == nullptr)
	{
		ourPcb = udp_new_ip_type(IPADDR_TYPE_ANY);
		if (ourPcb == nullptr)
		{
			reprap.GetPlatform().Message(ErrorMessage, "unable to allocate a pcb\n");
		}
		else
		{
			udp_set_multicast_ttl(ourPcb, 255);
			if (igmp_joingroup_netif(&gs_net_if, ip_2_ip4(&ourGroup)) != ERR_OK)			// without this call, multicast packets to this IP address get discarded
			{
				reprap.GetPlatform().Message(ErrorMessage, "igmp_joingroup failed\n");
			}
			else if (udp_bind(ourPcb, &ourGroup, port) != ERR_OK)
			{
				reprap.GetPlatform().Message(ErrorMessage, "udp_bind call failed\n");
			}
			else
			{
				udp_recv(ourPcb, rcvFunc, nullptr);
			}
		}
	}
	active = true;
	messagesProcessed = 0;
}

void MulticastResponder::Stop() noexcept
{
	if (ourPcb != nullptr)
	{
		udp_remove(ourPcb);
		ourPcb = nullptr;
	}
	active = false;
}

void MulticastResponder::SendResponse(uint8_t *data, size_t length) noexcept
{
#if 0
	debugPrintf("Tx UDP: port %u data", lastMessageReceivedPort);
	for (size_t i = 0; i < length; ++i)
	{
		debugPrintf(" %02x", data[i]);
	}
	debugPrintf("\n");
#endif

	pbuf * const pb = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_RAM);
#if 0
	if (pbuf_take(pb, data, length) != ERR_OK)
	{
		debugPrintf("pbuf_take returned error\n");
	}
	if (udp_sendto_if(ourPcb, pb, &ourGroup, lastMessageReceivedPort, &gs_net_if) != ERR_OK)
	{
		debugPrintf("UDP send failed\n");
	}
#else
	(void)pbuf_take(pb, data, length);
	(void)udp_sendto_if(ourPcb, pb, &ourGroup, lastMessageReceivedPort, &gs_net_if);
#endif
}

// Schedule a reboot. We delay a little while to allow the response to be transmitted first.
void MulticastResponder::ScheduleReboot() noexcept
{
	whenRebootScheduled = millis();
	ticksToReboot = 1000;
}

#endif

// End