Skip to content

Commit b2341f0

Browse files
committed
Refactoring DAC parameters.
1 parent c82c045 commit b2341f0

5 files changed

Lines changed: 90 additions & 58 deletions

File tree

thing/src/thing.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
static void (*reset)() = NULL;
1010
static long (*getTime)() = NULL;
1111
static bool (*initializeRadio)(RadioAddress) = NULL;
12-
static char *(*generateThingId)() = NULL;
12+
static char *(*loadThingId)() = NULL;
1313
static char *(*loadRegistrationCode)() = NULL;
1414
static bool (*configureRadio)() = NULL;
1515
static bool (*changeRadioAddress)(RadioAddress, bool) = NULL;
@@ -52,11 +52,11 @@ void registerRadioInitializer(bool (*_initializeRadio)(RadioAddress address)) {
5252
initializeRadio = _initializeRadio;
5353
}
5454

55-
void registerThingIdGenerator(char *(*_generateThingId)()) {
56-
generateThingId = _generateThingId;
55+
void registerThingIdLoader(char *(*_loadThingId)()) {
56+
loadThingId = _loadThingId;
5757
}
5858

59-
void registerRegistrationLoader(char *(*_loadRegistrationCode)()) {
59+
void registerRegistrationCodeLoader(char *(*_loadRegistrationCode)()) {
6060
loadRegistrationCode = _loadRegistrationCode;
6161
}
6262

@@ -223,7 +223,7 @@ void unregisterThingHooks() {
223223
initializeRadio = NULL;
224224
configureRadio = NULL;
225225
changeRadioAddress = NULL;
226-
generateThingId = NULL;
226+
loadThingId = NULL;
227227
loadRegistrationCode = NULL;
228228
loadThingInfo = NULL;
229229
saveThingInfo = NULL;
@@ -233,7 +233,25 @@ void unregisterThingHooks() {
233233
}
234234

235235
void chooseUplinkAddress(RadioAddress chosen) {
236+
if (thingInfo.uplinkChannelBegin == thingInfo.uplinkChannelEnd) {
237+
chosen[0] = thingInfo.uplinkAddressHighByte;
238+
chosen[1] = thingInfo.uplinkAddressLowByte;
239+
chosen[2] = thingInfo.uplinkChannelBegin;
236240

241+
return;
242+
}
243+
244+
int uplinkChannels = (thingInfo.uplinkChannelEnd - thingInfo.uplinkChannelEnd) + 1;
245+
int chosenChannelIndex;
246+
#ifdef ARDUINO
247+
chosenChannelIndex = random(uplinkChannels);
248+
#else
249+
srand((unsigned)getTime());
250+
chosenChannelIndex = rand() % uplinkChannels;
251+
#endif
252+
chosen[0] = thingInfo.uplinkAddressHighByte;
253+
chosen[1] = thingInfo.uplinkAddressHighByte;
254+
chosen[2] = thingInfo.uplinkChannelEnd + chosenChannelIndex;
237255
}
238256

239257
void sendAndRelease(RadioAddress to, ProtocolData *pData) {
@@ -246,33 +264,31 @@ int introduce(char *thingId, char *registrationCode) {
246264
Serial.println(F("enter introduce."));
247265
#else
248266
DEBUG_OUT("enter introduce.");
249-
#endif
250-
267+
#endif
251268
Protocol introduction = createProtocol(NAME_TUXP_PROTOCOL_INTRODUCTION);
252-
269+
253270
if(addStringAttribute(&introduction, NAME_ATTRIBUTE_THING_ID_TUXP_PROTOCOL_INTRODUCTION,
254271
thingId) != 0)
255272
return THING_ERROR_SET_PROTOCOL_ATTRIBUTE;
256-
273+
257274
if (addBytesAttribute(&introduction, NAME_ATTRIBUTE_ADDRESS_TUXP_PROTOCOL_INTRODUCTION,
258-
dacClientAddress,3) != 0)
275+
dacClientAddress, 3) != 0)
259276
return THING_ERROR_SET_PROTOCOL_ATTRIBUTE;
260277

261278
if (setText(&introduction, registrationCode) != 0)
262279
return THING_ERROR_SET_PROTOCOL_TEXT;
263-
280+
264281
ProtocolData pData = {NULL, 0};
265282
if (translateAndRelease(&introduction, &pData) != 0)
266283
return THING_ERROR_PROTOCOL_TRANSLATION;
267-
284+
268285
thingInfo.dacState = INTRODUCTING;
269286

270287
#if defined(ARDUINO) && defined(ENABLE_DEBUG)
271288
Serial.println(F("Sending introduction protocol to DAC service...."));
272289
#else
273290
DEBUG_OUT("Sending introduction protocol to DAC service....");
274291
#endif
275-
276292
sendAndRelease(dacServiceAddress, &pData);
277293

278294
return 0;
@@ -305,7 +321,7 @@ int isConfigured(char *thingId) {
305321
bool checkHooks() {
306322
if (loadThingInfo == NULL ||
307323
saveThingInfo == NULL ||
308-
generateThingId == NULL ||
324+
loadThingId == NULL ||
309325
loadRegistrationCode == NULL ||
310326
initializeRadio == NULL ||
311327
configureRadio == NULL ||
@@ -406,7 +422,7 @@ int toBeAThing() {
406422
return 0;
407423
} else {
408424
if (thingInfo.thingId == NULL) {
409-
thingInfo.thingId = generateThingId();
425+
thingInfo.thingId = loadThingId();
410426

411427
if(!configureRadio())
412428
return debugErrorAndReturn("toBeAThing", THING_ERROR_CONFIGURE_RADIO);
@@ -622,12 +638,8 @@ int processAllocation(Protocol *allocation) {
622638
if(!getIntAttributeValue(allocation, NAME_ATTRIBUTE_UPLINK_CHANNEL_END_TUXP_PROTOCOL_ALLOCATION, &uplinkChannelEnd))
623639
return TUXP_ERROR_LACK_OF_ALLOCATION_PARAMETERS;
624640

625-
uint8_t uplinkAddressHighByte;
626-
if (!getByteAttributeValue(allocation, NAME_ATTRIBUTE_UPLINK_ADDRESS_HIGH_BYTE_TUXP_PROTOCOL_ALLOCATION, uplinkAddressHighByte))
627-
return TUXP_ERROR_LACK_OF_ALLOCATION_PARAMETERS;
628-
629-
uint8_t uplinkAddressLowByte;
630-
if(!getByteAttributeValue(allocation, NAME_ATTRIBUTE_UPLINK_ADDRESS_LOW_BYTE_TUXP_PROTOCOL_ALLOCATION, uplinkAddressLowByte))
641+
uint8_t *uplinkAddress = getBytesAttributeValue(allocation, NAME_ATTRIBUTE_UPLINK_ADDRESS_TUXP_PROTOCOL_ALLOCATION);
642+
if (!uplinkAddress || uplinkAddress[0] != 0x02)
631643
return TUXP_ERROR_LACK_OF_ALLOCATION_PARAMETERS;
632644

633645
uint8_t *allocatedAddress = getBytesAttributeValue(allocation, NAME_ATTRIBUTE_ALLOCATED_ADDRESS_TUXP_PROTOCOL_ALLOCATION);
@@ -638,7 +650,7 @@ int processAllocation(Protocol *allocation) {
638650
return TUXP_ERROR_ILLEGAL_ALLOCATED_ADDRESS;
639651

640652
return allocated(uplinkChannelBegin, uplinkChannelEnd,
641-
uplinkAddressHighByte, uplinkAddressLowByte, allocatedAddress);
653+
uplinkAddress[1], uplinkAddress[2], allocatedAddress);
642654
}
643655

644656
void processNotConfigured() {

thing/src/thing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ typedef struct LanNotificationAndRexInfo {
8282
void registerResetter(void (*reset)());
8383
void registerTimer(long (*getTime)());
8484
void registerRadioInitializer(bool (*initializeRadio)(RadioAddress address));
85-
void registerThingIdGenerator(char *(*generateThingId)());
85+
void registerThingIdLoader(char *(*loadThingId)());
8686
void registerRegistrationCodeLoader(char *(*loadRegistrationCode()));
8787
void registerRadioConfigurer(bool (*configureRadio)());
8888
void registerRadioAddressChanger(bool (*changeRadioAddress)(RadioAddress address, bool savePersistently));

thing/tests/thing_test.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string.h>
2+
#include <time.h>
23

34
#include "unity.h"
45

@@ -16,7 +17,7 @@ static DacState dacState = INITIAL;
1617
static const uint8_t dacServiceAddress[] = {0xef, 0xef, 0x1f};
1718
static const uint8_t dacClientAddress[] = {0xef, 0xee, 0x1f};
1819
static const uint8_t configuredNodeAddress[] = {0x00, 0x01, 0x17};
19-
static const uint8_t gatewayUplinkAddress[] = {0x00, 0xef, 0x17};
20+
static const uint8_t gatewayUplinkAddress[] = {0x00, 0x00, 0x17};
2021
static uint8_t nodeAddress[] = {0x00, 0x00, 0x00};
2122

2223
static bool flashProcessed = false;
@@ -33,7 +34,7 @@ bool initializeRadioImpl(RadioAddress address) {
3334
return true;
3435
}
3536

36-
char *generateThingIdImpl() {
37+
char *loadThingIdImpl() {
3738
return "SL-LE01-C980AFE9";
3839
}
3940

@@ -92,12 +93,13 @@ void sendToGatewayMock1(uint8_t address[], uint8_t data[], int dataSize) {
9293
if (dacState == INITIAL) {
9394
TEST_ASSERT_EQUAL_UINT8_ARRAY(dacServiceAddress, address, 3);
9495

95-
TEST_ASSERT_EQUAL_INT(29, dataSize);
96-
uint8_t expectedIntroductionData[] = {
96+
TEST_ASSERT_EQUAL_INT(43, dataSize);
97+
uint8_t expectedIntroductionData[] ={
9798
0xff,
98-
0xf8, 0x03, 0x00, 0x01, 0x80,
99-
0x01, 0xfb, 0xef, 0xee, 0x1f, 0xfe,
100-
0x53, 0x4c, 0x2d, 0x4c, 0x45, 0x30, 0x31, 0x2d, 0x43, 0x39, 0x38, 0x30, 0x41, 0x46, 0x45, 0x39,
99+
0xf8, 0x03, 0x00, 0x02, 0x80,
100+
0x01, 0x53, 0x4c, 0x2d, 0x4c, 0x45, 0x30, 0x31, 0x2d, 0x43, 0x39, 0x38, 0x30, 0x41, 0x46, 0x45, 0x39, 0xfe,
101+
0x02, 0xfb, 0xef, 0xee, 0x1f, 0xfe,
102+
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
101103
0xff
102104
};
103105
TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedIntroductionData, data, dataSize);
@@ -113,18 +115,25 @@ void sendToGatewayMock1(uint8_t address[], uint8_t data[], int dataSize) {
113115
TEST_ASSERT_EQUAL_UINT8(0x03, actualAddress[0]);
114116
TEST_ASSERT_EQUAL_UINT8_ARRAY(dacClientAddress, actualAddress + 1, 3);
115117

116-
char *text = getText(&introduction);
117-
TEST_ASSERT_EQUAL_CHAR_ARRAY(thingId, text, strlen(thingId));
118+
char *thingIdFromIntroduction = getStringAttributeValue(&introduction, 0x01);
119+
if (!thingIdFromIntroduction)
120+
TEST_FAIL_MESSAGE("Failed to get thing ID from introduction.");
121+
TEST_ASSERT_EQUAL_CHAR_ARRAY(thingId, thingIdFromIntroduction, strlen(thingId));
122+
123+
char *registrationCode = getText(&introduction);
124+
if (!registrationCode)
125+
TEST_FAIL_MESSAGE("Failed to get registration code from introduction.");
126+
TEST_ASSERT_EQUAL_CHAR_ARRAY("abcdefghijkl", registrationCode, 12);
118127

119128
Protocol allocation = createProtocol(NAME_TUXP_PROTOCOL_ALLOCATION);
120129

121-
uint8_t gatewayUplinkAddress[] = {0x00, 0xef, 0x17};
122130
addIntAttribute(&allocation,
123-
NAME_ATTRIBUTE_UPLINK_CHANNEL_BEGIN_TUXP_PROTOCOL_ALLOCATION, 0);
131+
NAME_ATTRIBUTE_UPLINK_CHANNEL_BEGIN_TUXP_PROTOCOL_ALLOCATION, 0x17);
124132
addIntAttribute(&allocation,
125-
NAME_ATTRIBUTE_UPLINK_CHANNEL_END_TUXP_PROTOCOL_ALLOCATION, 0);
126-
addByteAttribute(&allocation, NAME_ATTRIBUTE_UPLINK_ADDRESS_HIGH_BYTE_TUXP_PROTOCOL_ALLOCATION, 0);
127-
addByteAttribute(&allocation,NAME_ATTRIBUTE_UPLINK_ADDRESS_LOW_BYTE_TUXP_PROTOCOL_ALLOCATION, 0);
133+
NAME_ATTRIBUTE_UPLINK_CHANNEL_END_TUXP_PROTOCOL_ALLOCATION, 0x17);
134+
uint8_t uplinkAddress[] = {0x00, 0x00};
135+
addBytesAttribute(&allocation, NAME_ATTRIBUTE_UPLINK_ADDRESS_TUXP_PROTOCOL_ALLOCATION,
136+
uplinkAddress, 2);
128137

129138
uint8_t allocatedAddress[] = {0x00, 0x01, 0x17};
130139
addBytesAttribute(&allocation,
@@ -220,15 +229,23 @@ int receiveRadioDataImpl(uint8_t buffer[], int bufferSize) {
220229
return 0;
221230
}
222231

232+
long getTimeImpl() {
233+
time_t current;
234+
time(&current);
235+
236+
return current;
237+
}
238+
223239
void registerThingHooks() {
224240
registerRadioInitializer(initializeRadioImpl);
225-
registerThingIdGenerator(generateThingIdImpl);
226-
registerThingIdGenerator(loadRegistrationCodeImpl);
241+
registerThingIdLoader(loadThingIdImpl);
242+
registerRegistrationCodeLoader(loadRegistrationCodeImpl);
227243
registerRadioConfigurer(configureRadioImpl);
228244
registerRadioAddressChanger(changeRadioAddressImpl);
229245
registerThingInfoLoader(loadThingInfoImpl);
230246
registerThingInfoSaver(saveThingInfoImpl);
231247
registerResetter(resetImpl);
248+
registerTimer(getTimeImpl);
232249
registerRadioDataReceiver(receiveRadioDataImpl);
233250
registerThingProtocolsConfigurer(configureThingProtocolsImpl);
234251
}
@@ -291,8 +308,8 @@ void testLoraDacAllocated() {
291308
loadThingInfoImpl(&thingInfo);
292309
TEST_ASSERT_EQUAL_INT(ALLOCATED, thingInfo.dacState);
293310

294-
TEST_ASSERT_EQUAL_INT(0, thingInfo.uplinkChannelBegin);
295-
TEST_ASSERT_EQUAL_INT(0, thingInfo.uplinkChannelEnd);
311+
TEST_ASSERT_EQUAL_INT(23, thingInfo.uplinkChannelBegin);
312+
TEST_ASSERT_EQUAL_INT(23, thingInfo.uplinkChannelEnd);
296313
TEST_ASSERT_EQUAL_UINT8(0x00, thingInfo.uplinkAddressHighByte);
297314
TEST_ASSERT_EQUAL_UINT8(0x00, thingInfo.uplinkAddressLowByte);
298315

tuxp/src/tuxp.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,24 @@ int addBytesAttribute(Protocol *protocol, uint8_t name, uint8_t bytes[], int siz
8888
int addStringAttribute(Protocol *protocol, uint8_t name, char string[]) {
8989
if(protocol->text)
9090
return debugErrorAndReturn("addStringAttribute", TUXP_ERROR_PROTOCOL_CHANGE_CLOSED);
91-
92-
int length = strlen(string);
93-
if (length > MAX_SIZE_ATTRIBUTE_DATA)
91+
92+
long strLength = strlen(string);
93+
if (strLength > MAX_SIZE_ATTRIBUTE_DATA)
9494
return debugErrorAndReturn("addStringAttribute", TUXP_ERROR_ATTRIBUTE_DATA_TOO_LARGE);
95-
95+
9696
ProtocolAttribute *attribute = malloc(sizeof(ProtocolAttribute));
97+
if (!attribute)
98+
return debugErrorAndReturn("addStringAttribute", TUXP_ERROR_OUT_OF_MEMEORY);
99+
97100
attribute->name = name;
98101
attribute->dataType = TYPE_CHARS;
99-
100-
attribute->value.csValue = malloc((strlen(string) + 1) * sizeof(char));
102+
103+
attribute->value.csValue = malloc((strLength + 1) * sizeof(char));
101104
if (!attribute->value.csValue)
102-
return debugErrorAndReturn("addStringAttribute", TUXP_ERROR_OUT_OF_MEMEORY);
105+
return debugErrorAndReturn("addStringAttribute", TUXP_ERROR_OUT_OF_MEMEORY);
106+
103107
strcpy(attribute->value.csValue, string);
104-
108+
105109
addAttributeToProtocol(protocol, attribute);
106110
return 0;
107111
}
@@ -682,7 +686,7 @@ int translateProtocol(Protocol *protocol, ProtocolData *pData) {
682686
releaseProtocolData(&attributeData);
683687
return debugErrorAndReturn("translateProtocol", TUXP_ERROR_PROTOCOL_DATA_TOO_LARGE);
684688
}
685-
689+
686690
memcpy(buff + position, attributeData.data, attributeData.dataSize);
687691
position += attributeData.dataSize;
688692

tuxp/src/tuxp.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ static const ProtocolName NAME_TUXP_PROTOCOL_INTRODUCTION = {{0xf8, 0x03}, 0x00}
5454
static const ProtocolName NAME_TUXP_PROTOCOL_ALLOCATION = {{0xf8, 0x03}, 0x03};
5555
#define NAME_ATTRIBUTE_UPLINK_CHANNEL_BEGIN_TUXP_PROTOCOL_ALLOCATION 0x04
5656
#define NAME_ATTRIBUTE_UPLINK_CHANNEL_END_TUXP_PROTOCOL_ALLOCATION 0x05
57-
#define NAME_ATTRIBUTE_UPLINK_ADDRESS_HIGH_BYTE_TUXP_PROTOCOL_ALLOCATION 0x06
58-
#define NAME_ATTRIBUTE_UPLINK_ADDRESS_LOW_BYTE_TUXP_PROTOCOL_ALLOCATION 0x07
59-
#define NAME_ATTRIBUTE_ALLOCATED_ADDRESS_TUXP_PROTOCOL_ALLOCATION 0x08
57+
#define NAME_ATTRIBUTE_UPLINK_ADDRESS_TUXP_PROTOCOL_ALLOCATION 0x06
58+
#define NAME_ATTRIBUTE_ALLOCATED_ADDRESS_TUXP_PROTOCOL_ALLOCATION 0x07
6059

61-
static const ProtocolName NAME_TUXP_PROTOCOL_ALLOCATED = {{0xf8, 0x03}, 0x06};
62-
static const ProtocolName NAME_TUXP_PROTOCOL_CONFIGURED = {{0xf8, 0x03}, 0x07};
60+
static const ProtocolName NAME_TUXP_PROTOCOL_ALLOCATED = {{0xf8, 0x03}, 0x09};
61+
static const ProtocolName NAME_TUXP_PROTOCOL_CONFIGURED = {{0xf8, 0x03}, 0x0a};
6362

64-
static const ProtocolName NAME_TUXP_PROTOCOL_IS_CONFIGURED = {{0xf8, 0x03}, 0x08};
65-
#define NAME_ATTRIBUTE_ADDRESS_TUXP_PROTOCOL_IS_CONFIGURED 0x01
63+
static const ProtocolName NAME_TUXP_PROTOCOL_IS_CONFIGURED = {{0xf8, 0x03}, 0x0b};
64+
#define NAME_ATTRIBUTE_ADDRESS_TUXP_PROTOCOL_IS_CONFIGURED 0x02
6665

67-
static const ProtocolName NAME_TUXP_PROTOCOL_NOT_CONFIGURED = {{0xf8, 0x03}, 0x09};
66+
static const ProtocolName NAME_TUXP_PROTOCOL_NOT_CONFIGURED = {{0xf8, 0x03}, 0x0c};
6867

6968
Protocol createEmptyProtocol();
7069
Protocol createProtocol(ProtocolName name);

0 commit comments

Comments
 (0)