Skip to content

Commit

Permalink
Commit 1
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Sep 13, 2023
1 parent d8480f1 commit 3653886
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/lighting-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
#include <esp_insights.h>
#include <matter/tracing/macros_impl.h>
#endif

using namespace ::chip;
Expand Down
2 changes: 2 additions & 0 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ source_set("esp32_trace") {
public = [ "include/matter/tracing/macros_impl.h" ]
sources = [ "include/matter/tracing/macros_impl.cpp" ]
public_configs = [ ":tracing" ]
public_deps = ["${chip_root}/src/crypto",
"${chip_root}/src/lib/core",]
}
48 changes: 46 additions & 2 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
#include "macros_impl.h"
#include <esp_heap_caps.h>
#include <esp_insights.h>

namespace Insights {

static bool isBlacklistInitialized = false;

uint64_t Insights::ESP32Backend::blacklistHashes[BLACKLIST_SIZE] = { 0 };
const char * ESP32Backend::blackListStrings[] = {
"PacketParser",
"MinMdnsResolver",
"SessionManager",
"DeviceCommissioner",
};
#define LOG_HEAP_INFO(label, group, entry_exit) \
do \
{ \
Expand All @@ -28,17 +38,51 @@ namespace Insights {
heap_caps_get_free_size(MALLOC_CAP_8BIT)); \
} while (0)

void ESP32Backend::InitializeBlacklistHashes()
{
for (int i = 0; i < BLACKLIST_SIZE; i++)
{
if (blackListStrings[i] == NULL)
break;
size_t data_length = strlen(blackListStrings[i]);
uint8_t sha1_hash[20];
CHIP_ERROR error = chip::Crypto::Hash_SHA1((const uint8_t *) blackListStrings[i], data_length, sha1_hash);
uint64_t truncated_hash = 0;
for (int j = 0; j < 8; j++)
{
truncated_hash <<= 8;
truncated_hash |= sha1_hash[j];
}
blacklistHashes[i] = truncated_hash;
if (error != CHIP_NO_ERROR)
{
printf("error");
}
}
}

ESP32Backend::ESP32Backend(const char * str, ...)
{
va_list args;
va_start(args, str);
mlabel = str;
mgroup = va_arg(args, const char *);
LOG_HEAP_INFO(mlabel, mgroup, "Entry");
if (!isBlacklistInitialized)
{
InitializeBlacklistHashes();
isBlacklistInitialized = true;
}
if (!isBlacklisted(mgroup))
{
LOG_HEAP_INFO(mlabel, mgroup, "Entry");
}
}

ESP32Backend::~ESP32Backend()
{
LOG_HEAP_INFO(mlabel, mgroup, "Exit");
if (!isBlacklisted(mgroup))
{
LOG_HEAP_INFO(mlabel, mgroup, "Exit");
}
}
} // namespace Insights
92 changes: 92 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,109 @@
#error "Tracing macros seem to be double defined"
#endif

/*static unsigned int hash(const char *str) {
unsigned int hash = 5381;
int c;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}
*/
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>

namespace Insights {
class ESP32Backend
{
public:
ESP32Backend(const char * str, ...);
~ESP32Backend();

static void SetString(const char * aLabel)
{
bool duplicate = false;
int i = 0;
for (i = 0; i < BLACKLIST_SIZE; i++)
{
if (blackListStrings[i] == NULL)
{
break;
}
if (strcmp(aLabel, blackListStrings[i]) == 0)
{
duplicate = true;
break;
}
}
if (i < BLACKLIST_SIZE && !duplicate)
{
blackListStrings[i] = aLabel;
}
}
static void RemoveString(const char * aLabel)
{
bool found = false;
int i = 0;
for (i = 0; i < BLACKLIST_SIZE; i++)
{
if (blackListStrings[i] == NULL)
{
break;
}
if (strcmp(aLabel, blackListStrings[i]) == 0)
{
found = true;
break;
}
}
if (found)
{
for (int j = i; j < BLACKLIST_SIZE - 1; j++)
{
blackListStrings[j] = blackListStrings[j + 1];
}
blackListStrings[BLACKLIST_SIZE - 1] = NULL;
}
}

static const char * GetString(int aIndex) { return blackListStrings[aIndex]; }

private:
const char * mlabel;
const char * mgroup;
static constexpr int BLACKLIST_SIZE = 10;
static uint64_t blacklistHashes[BLACKLIST_SIZE];
static void InitializeBlacklistHashes();
static const char * blackListStrings[BLACKLIST_SIZE];

bool isBlacklisted(const char * input)
{
uint8_t sha1_hash[20];
size_t data_length = strlen(input);
CHIP_ERROR error = chip::Crypto::Hash_SHA1((const uint8_t *) input, data_length, sha1_hash);
if (error != CHIP_NO_ERROR)
{
return false;
}
uint64_t truncated_hash = 0;
for (int j = 0; j < 8; j++)
{
truncated_hash <<= 8;
truncated_hash |= sha1_hash[j];
}

for (int i = 0; i < BLACKLIST_SIZE; i++)
{
if (truncated_hash == blacklistHashes[i])
{
return true;
}
}
return false;
}
};

} // namespace Insights

#define MATTER_TRACE_SCOPE(...) \
Expand Down

0 comments on commit 3653886

Please sign in to comment.