Code optimization

This commit is contained in:
Stephan Hadinger 2020-10-31 19:51:17 +01:00
parent cfe52e6bd4
commit edad344a13
7 changed files with 41 additions and 50 deletions

View File

@ -543,7 +543,15 @@ public:
inline bool getReachable(void) const { return reachable; }
inline bool getPower(uint8_t ep =0) const;
// Add an endpoint to a device
bool addEndpoint(uint8_t endpoint);
void clearEndpoints(void);
uint32_t countEndpoints(void) const; // return the number of known endpoints (0 if unknown)
void setManufId(const char * str);
void setModelId(const char * str);
void setFriendlyName(const char * str);
// dump device attributes to ZbData
void toAttributes(Z_attribute_list & attr_list) const;
@ -584,6 +592,9 @@ public:
}
return dirty;
}
protected:
static void setStringAttribute(char*& attr, const char * str);
};
/*********************************************************************************************\
@ -668,14 +679,6 @@ public:
// If it is already registered, update information, otherwise create the entry
Z_Device & updateDevice(uint16_t shortaddr, uint64_t longaddr = 0);
// Add an endpoint to a device
void addEndpoint(uint16_t shortaddr, uint8_t endpoint);
void clearEndpoints(uint16_t shortaddr);
uint32_t countEndpoints(uint16_t shortaddr) const; // return the number of known endpoints (0 if unknown)
void setManufId(uint16_t shortaddr, const char * str);
void setModelId(uint16_t shortaddr, const char * str);
void setFriendlyName(uint16_t shortaddr, const char * str);
inline const char * getFriendlyName(uint16_t shortaddr) const {
return findShortAddr(shortaddr).friendlyName;
}
@ -762,8 +765,6 @@ private:
// Create a new entry in the devices list - must be called if it is sure it does not already exist
Z_Device & createDeviceEntry(uint16_t shortaddr, uint64_t longaddr = 0);
void freeDeviceEntry(Z_Device *device);
void setStringAttribute(char*& attr, const char * str);
};
/*********************************************************************************************\

View File

@ -218,10 +218,9 @@ Z_Device & Z_Devices::updateDevice(uint16_t shortaddr, uint64_t longaddr) {
//
// Clear all endpoints
//
void Z_Devices::clearEndpoints(uint16_t shortaddr) {
Z_Device &device = getShortAddr(shortaddr);
void Z_Device::clearEndpoints(void) {
for (uint32_t i = 0; i < endpoints_max; i++) {
device.endpoints[i] = 0;
endpoints[i] = 0;
// no dirty here because it doesn't make sense to store it, does it?
}
}
@ -245,22 +244,13 @@ bool Z_Device::addEndpoint(uint8_t endpoint) {
return false;
}
void Z_Devices::addEndpoint(uint16_t shortaddr, uint8_t endpoint) {
if (getShortAddr(shortaddr).addEndpoint(endpoint)) {
dirty();
}
}
//
// Count the number of known endpoints
//
uint32_t Z_Devices::countEndpoints(uint16_t shortaddr) const {
uint32_t Z_Device::countEndpoints(void) const {
uint32_t count_ep = 0;
const Z_Device & device =findShortAddr(shortaddr);
if (!foundDevice(device)) return 0;
for (uint32_t i = 0; i < endpoints_max; i++) {
if (0 != device.endpoints[i]) {
if (0 != endpoints[i]) {
count_ep++;
}
}
@ -274,7 +264,7 @@ uint8_t Z_Devices::findFirstEndpoint(uint16_t shortaddr) const {
return findShortAddr(shortaddr).endpoints[0]; // returns 0x00 if no endpoint
}
void Z_Devices::setStringAttribute(char*& attr, const char * str) {
void Z_Device::setStringAttribute(char*& attr, const char * str) {
if (nullptr == str) { return; } // ignore a null parameter
size_t str_len = strlen(str);
@ -294,7 +284,7 @@ void Z_Devices::setStringAttribute(char*& attr, const char * str) {
attr = (char*) malloc(str_len + 1);
strlcpy(attr, str, str_len + 1);
}
dirty();
zigbee_devices.dirty();
}
//
@ -306,16 +296,16 @@ void Z_Devices::setStringAttribute(char*& attr, const char * str) {
// Impact:
// - Any actual change in ManufId (i.e. setting a different value) triggers a `dirty()` and saving to Flash
//
void Z_Devices::setManufId(uint16_t shortaddr, const char * str) {
setStringAttribute(getShortAddr(shortaddr).manufacturerId, str);
void Z_Device::setManufId(const char * str) {
setStringAttribute(manufacturerId, str);
}
void Z_Devices::setModelId(uint16_t shortaddr, const char * str) {
setStringAttribute(getShortAddr(shortaddr).modelId, str);
void Z_Device::setModelId(const char * str) {
setStringAttribute(modelId, str);
}
void Z_Devices::setFriendlyName(uint16_t shortaddr, const char * str) {
setStringAttribute(getShortAddr(shortaddr).friendlyName, str);
void Z_Device::setFriendlyName(const char * str) {
setStringAttribute(friendlyName, str);
}
@ -755,24 +745,24 @@ int32_t Z_Devices::deviceRestore(JsonParserObject json) {
// update internal device information
updateDevice(shortaddr, ieeeaddr);
if (modelid) { setModelId(shortaddr, modelid); }
if (manufid) { setManufId(shortaddr, manufid); }
if (friendlyname) { setFriendlyName(shortaddr, friendlyname); }
Z_Device & device = getShortAddr(shortaddr);
if (modelid) { device.setModelId(modelid); }
if (manufid) { device.setManufId(manufid); }
if (friendlyname) { device.setFriendlyName(friendlyname); }
// read "Endpoints"
JsonParserToken val_endpoints = json[PSTR("Endpoints")];
if (val_endpoints.isArray()) {
JsonParserArray arr_ep = JsonParserArray(val_endpoints);
clearEndpoints(shortaddr); // clear even if array is empty
device.clearEndpoints(); // clear even if array is empty
for (auto ep_elt : arr_ep) {
uint8_t ep = ep_elt.getUInt();
if (ep) { addEndpoint(shortaddr, ep); }
if (ep) { device.addEndpoint(ep); }
}
}
// read "Config"
JsonParserToken val_config = json[PSTR("Config")];
Z_Device & device = getShortAddr(shortaddr);
if (val_config.isArray()) {
JsonParserArray arr_config = JsonParserArray(val_config);
device.data.reset(); // remove existing configuration

View File

@ -227,7 +227,7 @@ void hydrateSingleDevice(const SBuffer & buf_d, uint32_t version) {
for (uint32_t j = 0; j < endpoints; j++) {
uint8_t ep = buf_d.get8(d++);
uint16_t ep_profile = buf_d.get16(d); d += 2;
zigbee_devices.addEndpoint(shortaddr, ep);
device.addEndpoint(ep);
// in clusters
while (d < buf_len) { // safe guard against overflow
@ -245,13 +245,13 @@ void hydrateSingleDevice(const SBuffer & buf_d, uint32_t version) {
}
// ModelId
zigbee_devices.setModelId(shortaddr, hydrateSingleString(buf_d, &d));
device.setModelId(hydrateSingleString(buf_d, &d));
// ManufID
zigbee_devices.setManufId(shortaddr, hydrateSingleString(buf_d, &d));
device.setManufId(hydrateSingleString(buf_d, &d));
// FriendlyName
zigbee_devices.setFriendlyName(shortaddr, hydrateSingleString(buf_d, &d));
device.setFriendlyName(hydrateSingleString(buf_d, &d));
if (d >= buf_len) { return; }

View File

@ -1775,8 +1775,8 @@ void Z_OccupancyCallback(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluste
// ======================================================================
void Z_postProcessAttributes(uint16_t shortaddr, uint16_t src_ep, class Z_attribute_list& attr_list) {
uint8_t count_ep = zigbee_devices.countEndpoints(shortaddr);
Z_Device & device = zigbee_devices.getShortAddr(shortaddr);
uint8_t count_ep = device.countEndpoints();
for (auto &attr : attr_list) {
// add endpoint suffix if needed
@ -1825,7 +1825,7 @@ void Z_postProcessAttributes(uint16_t shortaddr, uint16_t src_ep, class Z_attrib
// Then store the attribute at the attribute addres (via offset) and according to size 8/16/32 bits
// add the endpoint if it was not already known
zigbee_devices.addEndpoint(shortaddr, src_ep);
device.addEndpoint(src_ep);
// we don't apply the multiplier, but instead store in Z_Data_XXX object
Z_Data & data = device.data.getByType(map_type, src_ep);
uint8_t *attr_address = ((uint8_t*)&data) + sizeof(Z_Data) + map_offset;
@ -1851,8 +1851,8 @@ void Z_postProcessAttributes(uint16_t shortaddr, uint16_t src_ep, class Z_attrib
Z_Data_Set & data = device.data;
// update any internal structure
switch (ccccaaaa) {
case 0x00000004: zigbee_devices.setManufId(shortaddr, attr.getStr()); break;
case 0x00000005: zigbee_devices.setModelId(shortaddr, attr.getStr()); break;
case 0x00000004: device.setManufId(attr.getStr()); break;
case 0x00000005: device.setModelId(attr.getStr()); break;
case 0x00010021: zigbee_devices.setBatteryPercent(shortaddr, uval16 / 2); break;
case 0x00060000:
case 0x00068000: device.setPower(attr.getBool(), src_ep); break;

View File

@ -431,7 +431,7 @@ void convertClusterSpecific(class Z_attribute_list &attr_list, uint16_t cluster,
char command_suffix[4] = { 0x00 }; // empty string by default
// if SO101 and multiple endpoints, append endpoint number
if (Settings.flag4.zb_index_ep) {
if (zigbee_devices.countEndpoints(shortaddr) > 0) {
if (zigbee_devices.getShortAddr(shortaddr).countEndpoints() > 0) {
snprintf_P(command_suffix, sizeof(command_suffix), PSTR("%d"), srcendpoint);
}
}

View File

@ -539,7 +539,7 @@ int32_t Z_ReceiveActiveEp(int32_t res, const class SBuffer &buf) {
for (uint32_t i = 0; i < activeEpCount; i++) {
uint8_t ep = activeEpList[i];
zigbee_devices.addEndpoint(nwkAddr, ep);
zigbee_devices.getShortAddr(nwkAddr).addEndpoint(ep);
if ((i < 4) && (ep < 0x10)) {
zigbee_devices.queueTimer(nwkAddr, 0 /* groupaddr */, 1500, ep /* fake cluster as ep */, ep, Z_CAT_EP_DESC, 0 /* value */, &Z_SendSimpleDescReq);
}

View File

@ -1045,7 +1045,7 @@ void CmndZbName(void) {
Response_P(PSTR("{\"0x%04X\":{\"" D_JSON_ZIGBEE_NAME "\":\"%s\"}}"), shortaddr, friendlyName ? friendlyName : "");
} else {
if (strlen(p) > 32) { p[32] = 0x00; } // truncate to 32 chars max
zigbee_devices.setFriendlyName(shortaddr, p);
zigbee_devices.getShortAddr(shortaddr).setFriendlyName(p);
Response_P(PSTR("{\"0x%04X\":{\"" D_JSON_ZIGBEE_NAME "\":\"%s\"}}"), shortaddr, p);
}
}
@ -1076,7 +1076,7 @@ void CmndZbModelId(void) {
const char * modelId = zigbee_devices.getModelId(shortaddr);
Response_P(PSTR("{\"0x%04X\":{\"" D_JSON_ZIGBEE_MODELID "\":\"%s\"}}"), shortaddr, modelId ? modelId : "");
} else {
zigbee_devices.setModelId(shortaddr, p);
zigbee_devices.getShortAddr(shortaddr).setModelId(p);
Response_P(PSTR("{\"0x%04X\":{\"" D_JSON_ZIGBEE_MODELID "\":\"%s\"}}"), shortaddr, p);
}
}