Commit e4db3ce6 authored by Michael Ott's avatar Michael Ott
Browse files

Switch to 112bit MQTT topics

parent 55d51d27
......@@ -167,7 +167,6 @@ int mqttCallback(SimpleMQTTMessage *msg)
<< " Raw: " << hex << setw(16) << setfill('0') << sid.getRaw()[0] << hex << setw(16) << setfill('0') << sid.getRaw()[1] << endl
<< " DeviceLocation: " << hex << setw(16) << setfill('0') << sid.getDeviceLocation() << endl
<< " device_id: " << hex << setw(8) << setfill('0') << sid.getDeviceSensorId().device_id << endl
<< " reserved: " << hex << setw(4) << setfill('0') << sid.getDeviceSensorId().rsvd << endl
<< " sensor_number: " << hex << setw(4) << setfill('0') << sid.getDeviceSensorId().sensor_number << endl << dec;
cout << "Payload (" << len/sizeof(mqttPayload) << " messages):"<< endl;
......
......@@ -125,7 +125,7 @@ uint64_t SensorCache::getSensor(std::string topic, uint64_t avg) {
return getSensor(DCDB::SensorId(topic), avg);
}
int wl = 33 - topic.length();
int wl = 29 - topic.length();
/* Create SensorIds with the lowest and highest values matching the wildcard */
DCDB::SensorId sidLow(std::string(topic).replace(wp, 1, std::string(wl, '0')));
......
......@@ -49,8 +49,8 @@ typedef uint64_t DeviceLocation;
* approach is, for example, to use MAC addresses as the device_id.
*/
typedef struct {
uint64_t sensor_number : 16; /**< The sensor_number of the sensor */
uint64_t rsvd : 16; /**< Reserved */
uint64_t sensor_number : 16; /**< The sensor_number of the sensor */
uint64_t device_id : 32; /**< The location-independent device_id */
} DeviceSensorId;
......
......@@ -184,8 +184,8 @@ bool SensorConfigImpl::validateSensorPattern(const char* sensorPattern)
{
unsigned int wildcards, bits;
/* 128 bits in HEX is at most 32 characters. Considering forward-slash separators, 64 is a reasonable maxmimum string length */
if (strlen(sensorPattern) > 64) {
/* 112 bits in HEX is at most 28 characters. Considering forward-slash separators, 56 is a reasonable maxmimum string length */
if (strlen(sensorPattern) > 56) {
return false;
}
......@@ -241,7 +241,7 @@ bool SensorConfigImpl::validateSensorPattern(const char* sensorPattern)
return false;
/* In case of a wildcard, there should be some bits left for the wildcard to fill in */
if (wildcards == 1 && bits >= 128)
if (wildcards == 1 && bits >= 112)
return false;
/* Looks good */
......@@ -903,10 +903,14 @@ SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds
std::string low = pattern;
std::string high = pattern;
if (pattern.find("*") != std::string::npos) {
low.replace(pattern.find("*"), 1, 33-pattern.length(), '0');
high.replace(pattern.find("*"), 1, 33-pattern.length(), 'F');
low.replace(pattern.find("*"), 1, 29-pattern.length(), '0');
high.replace(pattern.find("*"), 1, 29-pattern.length(), 'F');
}
/* The week stamp is imprinted on the lower 4 bits */
low.append("0000");
high.append("FFFF");
SensorId lowId, highId;
if (!lowId.mqttTopicConvert(low)) {
return SC_INVALIDPATTERN;
......
......@@ -119,7 +119,7 @@ bool SensorId::mqttTopicConvert(std::string mqttTopic)
const char* buf = mqttTopic.c_str();
data.raw[0] = 0;
data.raw[1] = 0;
while (*buf && pos < 128) {
while (*buf && pos < 112) {
if (*buf >= '0' && *buf <= '9') {
data.raw[pos / 64] |= (((uint64_t)(*buf - '0')) << (60-(pos%64)));
pos += 4;
......@@ -134,7 +134,7 @@ bool SensorId::mqttTopicConvert(std::string mqttTopic)
}
buf++;
}
return pos == 128;
return pos == 112;
}
/**
......@@ -154,13 +154,13 @@ std::string SensorId::serialize() const
/**
* @details
* This function returns a hex representation of a SensorId as a
* 32-character std::string.
* 28-character std::string.
*/
std::string SensorId::toString() const
{
char buf[33];
snprintf(buf, sizeof(buf), "%016" PRIx64 "%016" PRIx64, data.raw[0], data.raw[1]);
return std::string(buf);
return std::string(buf).substr(0, 28);
}
/**
......@@ -178,12 +178,12 @@ bool SensorId::patternMatch(std::string pattern)
boost::algorithm::to_lower(pattern);
/* Calculate the wildcard length */
int wl = (pattern.find("*") != std::string::npos) ? 33 - pattern.length() : 0;
int wl = (pattern.find("*") != std::string::npos) ? 29 - pattern.length() : 0;
/* Do a character by character comparison */
int posP = 0;
int posS = 0;
while (posS < 32) {
while (posS < 28) {
char p, cs[2];
uint64_t s;
p = pattern.c_str()[posP];
......@@ -198,7 +198,7 @@ bool SensorId::patternMatch(std::string pattern)
posP++;
continue;
}
else if ((posS >= 24 && posS <= 27) || (p==cs[0])) {
else if (p==cs[0]) {
posS++;
posP++;
continue;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment