2024-07-27 13:31:17 +00:00
|
|
|
#include "stddef.h"
|
|
|
|
#include "dev_name.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "random.h"
|
|
|
|
#include "constants.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
2024-08-17 15:19:47 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2024-07-27 13:31:17 +00:00
|
|
|
#define DEV_PREFIX "ESP32 "
|
|
|
|
|
2024-08-17 15:19:47 +00:00
|
|
|
static const char *TAG = "dev_name";
|
|
|
|
|
2024-07-27 14:15:35 +00:00
|
|
|
bool dev_generate_name()
|
2024-07-27 13:31:17 +00:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2024-08-17 15:19:47 +00:00
|
|
|
ESP_LOGE(TAG, "Failed to allocate memory to store dev name!\n");
|
2024-07-27 13:31:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(dev, DEV_PREFIX);
|
|
|
|
storage_get_dev_name(dev + strlen(DEV_PREFIX));
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|