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

CanMessageGenericConstructor.cpp « CAN « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72fd7e26e1e62ea425de646f7ef9670b27aa7580 (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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * CanMessageGenericConstructor.cpp
 *
 *  Created on: 23 Jul 2019
 *      Author: David
 */

#include "CanMessageGenericConstructor.h"
#include "Hardware/IoPorts.h"

#if SUPPORT_CAN_EXPANSION

#include "CanMessageBuffer.h"
#include "CanInterface.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"

#define STRINGIZE(_v) #_v

CanMessageGenericConstructor::CanMessageGenericConstructor(const ParamDescriptor *p_param) noexcept
	: paramTable(p_param), dataLen(0)
{
	msg.paramMap = 0;
}

// Append a value to the data, throwing if it wouldn't fit
void CanMessageGenericConstructor::StoreValue(const void *vp, size_t sz) THROWS(GCodeException)
{
	if (dataLen + sz > sizeof(msg.data))
	{
		throw ConstructParseException("CAN message too long");
	}
	memcpy(msg.data + dataLen, vp, sz);
	dataLen += sz;
}

// Insert a value in the data, throwing if it wouldn't fit
void CanMessageGenericConstructor::InsertValue(const void *vp, size_t sz, size_t pos) THROWS(GCodeException)
{
	if (dataLen + sz > sizeof(msg.data))
	{
		throw ConstructParseException("CAN message too long");
	}
	memmove(msg.data + pos + sz, msg.data + pos, dataLen - pos);
	memcpy(msg.data + pos, vp, sz);
	dataLen += sz;
}

// Populate the CAN message from a GCode message returning true if successful. Throws if an error occurs.
void CanMessageGenericConstructor::PopulateFromCommand(GCodeBuffer& gb) THROWS(GCodeException)
{
	uint32_t paramBit = 1;
	for (const ParamDescriptor *d = paramTable; d->letter != 0; ++d)
	{
		if (d->letter >= 'A' && d->letter <= 'Z' && gb.Seen(d->letter))
		{
			switch (d->type)
			{
			case ParamDescriptor::uint32:
				StoreValue(gb.GetUIValue());
				break;

			case ParamDescriptor::int32:
				StoreValue(gb.GetIValue());
				break;

			case ParamDescriptor::uint16:
				StoreValue((uint16_t)min<uint32_t>(gb.GetUIValue(), std::numeric_limits<uint16_t>::max()));
				break;

			case ParamDescriptor::int16:
				StoreValue((int16_t)constrain<int32_t>(gb.GetIValue(), std::numeric_limits<int16_t>::min(), std::numeric_limits<int16_t>::max()));
				break;

			case ParamDescriptor::uint8:
				StoreValue((uint8_t)min<uint32_t>(gb.GetUIValue(), std::numeric_limits<uint8_t>::max()));
				break;

			case ParamDescriptor::int8:
				StoreValue((int8_t)constrain<int32_t>(gb.GetIValue(), std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()));
				break;

			case ParamDescriptor::localDriver:
				{
					const DriverId id = gb.GetDriverId();
					StoreValue(id.localDriver);
				}
				break;

			case ParamDescriptor::float_p:
				StoreValue(gb.GetFValue());
				break;

			case ParamDescriptor::char_p:
				{
					String<StringLength20> str;
					gb.GetQuotedString(str.GetRef());
					if (str.strlen() != 1)
					{
						throw ConstructParseException("expected single-character quoted string after '%c'", (uint32_t)d->letter);
					}
					StoreValue(str[0]);
				}
				break;

			case ParamDescriptor::string:
				{
					String<StringLength20> str;
					gb.GetQuotedString(str.GetRef());
					StoreValue(str.c_str(), str.strlen() + 1);
				}
				break;

			case ParamDescriptor::reducedString:
				{
					String<StringLength20> str;
					gb.GetReducedString(str.GetRef());
					// We don't want port names sent to expansion boards to include the board number, so remove the board number.
					// We also use the reducedString type for sensor names, but they should't start with digits followed by '.'.
					(void)IoPort::RemoveBoardAddress(str.GetRef());
					StoreValue(str.c_str(), str.strlen() + 1);
				}
				break;

			case ParamDescriptor::uint32_array:
			case ParamDescriptor::uint16_array:
			case ParamDescriptor::uint8_array:
				{
					uint32_t arr[59];				// max size is 59 * uint8_t + 1 length byte + parameters present bitmap = 64
					size_t siz = min<size_t>(ARRAY_SIZE(arr), d->maxArrayLength);
					gb.GetUnsignedArray(arr, siz, false);
					StoreValue((uint8_t)siz);
					for (size_t i = 0; i < siz; ++i)
					{
						StoreValue(&arr[i], d->ItemSize());
					}
				}
				break;

			case ParamDescriptor::float_array:
				{
					float arr[14];
					size_t siz = min<size_t>(ARRAY_SIZE(arr), d->maxArrayLength);
					gb.GetFloatArray(arr, siz, false);
					StoreValue((uint8_t)siz);
					for (size_t i = 0; i < siz; ++i)
					{
						StoreValue(arr[i]);
					}
				}
				break;

			default:
				throw ConstructParseException("internal error at " __FILE__ "(" STRINGIZE(#__LINE__) ")");
			}
			msg.paramMap |= paramBit;
		}
		paramBit <<= 1;
	}
}

// Return the correct position in the data to insert a parameter. If successful, add the bit to the parameter map and pass back the expect5ed parameter type; else throw.
unsigned int CanMessageGenericConstructor::FindInsertPoint(char c, ParamDescriptor::ParamType& t, size_t &sz) THROWS(GCodeException)
{
	unsigned int pos = 0;
	uint32_t paramBit = 1;
	for (const ParamDescriptor *d = paramTable; d->letter != 0; ++d)
	{
		const bool present = (msg.paramMap & paramBit) != 0;
		if (d->letter == c)
		{
			if (present)
			{
				throw ConstructParseException("duplicate parameter");
			}
			msg.paramMap |= paramBit;
			t = d->type;
			sz = d->ItemSize();
			return pos;
		}

		if (present)
		{
			// This parameter is present, so skip it
			const size_t size = d->ItemSize();
			if (size != 0)
			{
				pos += size;
			}
			else
			{
				// The only item with size 0 is string, so skip up to and including the null terminator
				do
				{
				} while (msg.data[pos++] != 0);
			}
		}
		paramBit <<= 1;
	}
	throw ConstructParseException("wrong parameter letter");
}

//TODO factor out the common code in the following several routines
void CanMessageGenericConstructor::AddU64Param(char c, uint64_t v) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	if (t != ParamDescriptor::uint64)
	{
		throw ConstructParseException("u64val wrong parameter type");
	}
	InsertValue(&v, sz, pos);
}

void CanMessageGenericConstructor::AddUParam(char c, uint32_t v) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	switch (t)
	{
	case ParamDescriptor::uint32:
		break;

	case ParamDescriptor::uint16:
	case ParamDescriptor::pwmFreq:
		if (v >= (1u << 16))
		{
			throw ConstructParseException("uval too large");
		}
		break;

	case ParamDescriptor::uint8:
		if (v >= (1u << 8))
		{
			throw ConstructParseException("uval too large");
		}
		break;

	default:
		throw ConstructParseException("uval wrong parameter type");
	}

	InsertValue(&v, sz, pos);
}

void CanMessageGenericConstructor::AddIParam(char c, int32_t v) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	switch (t)
	{
	case ParamDescriptor::int32:
		break;

	case ParamDescriptor::int16:
		if (v >= (int32_t)(1u << 15) || v < -(int32_t)(1u << 15))
		{
			throw ConstructParseException("ival too large");
		}
		break;

	case ParamDescriptor::uint8:
		if (v >= (int32_t)(1u << 7) || v < -(int32_t)(1u << 7))
		{
			throw ConstructParseException("ival too large");
		}
		break;

	default:
		throw ConstructParseException("ival wrong parameter type");
	}

	InsertValue(&v, sz, pos);
}

void CanMessageGenericConstructor::AddFParam(char c, float v) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	if (t != ParamDescriptor::float_p)
	{
		throw ConstructParseException("fval wrong parameter type");
	}
	InsertValue(&v, sz, pos);
}

void CanMessageGenericConstructor::AddCharParam(char c, char v) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	if (t != ParamDescriptor::char_p)
	{
		throw ConstructParseException("cval wrong parameter type");
	}
	InsertValue(&v, sz, pos);
}

void CanMessageGenericConstructor::AddStringParam(char c, const char *v) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	switch (t)
	{
	case ParamDescriptor::string:
	case ParamDescriptor::reducedString:			//TODO currently we don't reduce the string, but it should already be reduced
		InsertValue(v, strlen(v) + 1, pos);
		break;

	default:
		throw ConstructParseException("sval wrong parameter type");
	}
}

void CanMessageGenericConstructor::AddDriverIdParam(char c, DriverId did) THROWS(GCodeException)
{
	ParamDescriptor::ParamType t;
	size_t sz;
	const unsigned int pos = FindInsertPoint(c, t, sz);
	if (t != ParamDescriptor::localDriver)
	{
		throw ConstructParseException("didval wrong parameter type");
	}

	InsertValue(&did.localDriver, sz, pos);
}

GCodeResult CanMessageGenericConstructor::SendAndGetResponse(CanMessageType msgType, CanAddress dest, const StringRef& reply) noexcept
{
	CanMessageBuffer * buf = CanMessageBuffer::Allocate();
	if (buf == nullptr)
	{
		reply.copy("no CAN buffer available");
		return GCodeResult::error;
	}

	const CanRequestId rid = CanInterface::AllocateRequestId(dest);
	const size_t actualMessageLength = CanMessageGeneric::GetActualDataLength(dataLen);
	CanMessageGeneric *m2 = buf->SetupGenericRequestMessage(rid, CanInterface::GetCanAddress(), dest, msgType, actualMessageLength);
	memcpy(m2, &msg, actualMessageLength);
	m2->requestId = rid;
	return CanInterface::SendRequestAndGetStandardReply(buf, rid, reply);
}

#endif

// End