SolarEnergy/esp32_device/main/dev_name.c

44 lines
879 B
C

#include "stddef.h"
#include "dev_name.h"
#include "storage.h"
#include "stdio.h"
#include "random.h"
#include "constants.h"
#include "string.h"
#include "esp_log.h"
#define DEV_PREFIX "ESP32 "
static const char *TAG = "dev_name";
bool dev_generate_name()
{
// Check if a device name has already been defined
if (storage_get_dev_name(NULL) > 0)
return false;
// Generate random device name
char name[DEV_NAME_LEN];
rand_str(DEV_NAME_LEN - 1, name);
storage_set_dev_name(name);
return true;
}
char *dev_name()
{
size_t len = storage_get_dev_name(NULL);
char *dev = malloc(len + strlen(DEV_PREFIX) + 1);
if (dev == NULL)
{
ESP_LOGE(TAG, "Failed to allocate memory to store dev name!");
return NULL;
}
strcpy(dev, DEV_PREFIX);
storage_get_dev_name(dev + strlen(DEV_PREFIX));
return dev;
}