Generate device name
This commit is contained in:
		
							
								
								
									
										9
									
								
								esp32_device/.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								esp32_device/.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@@ -6,5 +6,12 @@
 | 
				
			|||||||
  "idf.openOcdConfigs": [
 | 
					  "idf.openOcdConfigs": [
 | 
				
			||||||
    "interface/ftdi/esp32_devkitj_v1.cfg",
 | 
					    "interface/ftdi/esp32_devkitj_v1.cfg",
 | 
				
			||||||
    "target/esp32.cfg"
 | 
					    "target/esp32.cfg"
 | 
				
			||||||
  ]
 | 
					  ],
 | 
				
			||||||
 | 
					  "files.associations": {
 | 
				
			||||||
 | 
					    "dev_name.h": "c",
 | 
				
			||||||
 | 
					    "stdio.h": "c",
 | 
				
			||||||
 | 
					    "storage.h": "c",
 | 
				
			||||||
 | 
					    "cstdlib": "c",
 | 
				
			||||||
 | 
					    "cstring": "c"
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,2 +1,3 @@
 | 
				
			|||||||
idf_component_register(SRCS "main.c"
 | 
					idf_component_register(SRCS "random.c" "storage.c" "main.c"
 | 
				
			||||||
 | 
					                        "dev_name.c"
 | 
				
			||||||
                    INCLUDE_DIRS ".")
 | 
					                    INCLUDE_DIRS ".")
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								esp32_device/main/constants.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								esp32_device/main/constants.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Device name len
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define DEV_NAME_LEN 10
 | 
				
			||||||
							
								
								
									
										40
									
								
								esp32_device/main/dev_name.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								esp32_device/main/dev_name.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					#include "stddef.h"
 | 
				
			||||||
 | 
					#include "dev_name.h"
 | 
				
			||||||
 | 
					#include "storage.h"
 | 
				
			||||||
 | 
					#include "stdio.h"
 | 
				
			||||||
 | 
					#include "random.h"
 | 
				
			||||||
 | 
					#include "constants.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define DEV_PREFIX "ESP32 "
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool dev_generate_name_if_required()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    // 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)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        printf("Failed to allocate memory to store dev name!\n");
 | 
				
			||||||
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    strcpy(dev, DEV_PREFIX);
 | 
				
			||||||
 | 
					    storage_get_dev_name(dev + strlen(DEV_PREFIX));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return dev;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										33
									
								
								esp32_device/main/dev_name.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								esp32_device/main/dev_name.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Device name management
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					extern "C"
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Generate random device name, if not existent
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    bool dev_generate_name_if_required();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Clear device name
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    void dev_remove_name();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Get current device name. This value MUST be freed after usage
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return NULL if no name was already defined.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    char *dev_name();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
@@ -1,9 +1,29 @@
 | 
				
			|||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include "esp_system.h"
 | 
					#include "esp_system.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "dev_name.h"
 | 
				
			||||||
 | 
					#include "storage.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void app_main(void)
 | 
					void app_main(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    printf("\n\n\nhello world and bye\n\n\n");
 | 
					    printf("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (storage_init() == false)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        printf("Failed to init storage!\n");
 | 
				
			||||||
 | 
					        fflush(stdout);
 | 
				
			||||||
 | 
					        esp_restart();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (dev_generate_name_if_required())
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        printf("Generated a new device name\n");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    char *name = dev_name();
 | 
				
			||||||
 | 
					    printf("Dev name: %s\n", name);
 | 
				
			||||||
 | 
					    free(name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fflush(stdout);
 | 
					    fflush(stdout);
 | 
				
			||||||
    esp_restart();
 | 
					    esp_restart();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										31
									
								
								esp32_device/main/random.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								esp32_device/main/random.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					#include "random.h"
 | 
				
			||||||
 | 
					#include "esp_random.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void rand_str(size_t len, char *dest)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < len; i++)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        uint32_t v = esp_random() % (26 * 2 + 10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Upper case letter
 | 
				
			||||||
 | 
					        if (v < 26)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            dest[i] = 65 + v;
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        v -= 26;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Lower case letter
 | 
				
			||||||
 | 
					        if (v < 26)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            dest[i] = 97 + v;
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        v -= 26;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Digit
 | 
				
			||||||
 | 
					        dest[i] = 48 + (v % 10);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dest[len] = '\0';
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								esp32_device/main/random.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								esp32_device/main/random.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Random utilities
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stddef.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					extern "C"
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Generate random string of given size
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    void rand_str(size_t len, char *dest);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
							
								
								
									
										55
									
								
								esp32_device/main/storage.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								esp32_device/main/storage.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					#include "constants.h"
 | 
				
			||||||
 | 
					#include "storage.h"
 | 
				
			||||||
 | 
					#include "nvs_flash.h"
 | 
				
			||||||
 | 
					#include "nvs.h"
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define STORAGE_NAMESPACE "storage"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define DEV_NAME_KEY "dev_name"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool storage_init()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    esp_err_t err = nvs_flash_init();
 | 
				
			||||||
 | 
					    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        printf("Need to reset storage\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // NVS partition was truncated and needs to be erased
 | 
				
			||||||
 | 
					        // Retry nvs_flash_init
 | 
				
			||||||
 | 
					        ESP_ERROR_CHECK(nvs_flash_erase());
 | 
				
			||||||
 | 
					        err = nvs_flash_init();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return err == ESP_OK;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void storage_set_dev_name(const char *name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    nvs_handle_t my_handle;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ESP_ERROR_CHECK(nvs_set_blob(my_handle, DEV_NAME_KEY, name, strlen(name)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    nvs_close(my_handle);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					size_t storage_get_dev_name(char *dest)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    nvs_handle_t my_handle;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    size_t len = (dest == NULL ? 0 : DEV_NAME_LEN);
 | 
				
			||||||
 | 
					    esp_err_t res = nvs_get_blob(my_handle, DEV_NAME_KEY, dest, &len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    nvs_close(my_handle);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (res == ESP_ERR_NVS_NOT_FOUND || len == 0)
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ESP_ERROR_CHECK(res);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return len;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										32
									
								
								esp32_device/main/storage.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								esp32_device/main/storage.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Storage management
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include <stddef.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					extern "C"
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Initialize storage
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    bool storage_init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Write device name
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    void storage_set_dev_name(const char *name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Get current device name
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    size_t storage_get_dev_name(char *dest);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		Reference in New Issue
	
	Block a user