Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Get the Memfault integration to the point where I can build
  • Loading branch information
fnune committed Feb 16, 2021
1 parent edbfc2c commit 6ae1b36
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 99 deletions.
36 changes: 36 additions & 0 deletions NOTES.md
Expand Up @@ -242,3 +242,39 @@ Type some stuff... and it appears on my newest terminal! Cool! Trying to type on
Documentation link: https://docs.memfault.com/docs/embedded/esp8266-rtos-sdk-guide/

I'm starting by getting some application code in place. It's just going to be an HTTP server with simple routes. Then, I'll try to integrate the Memfault SDK on top.

Ran into this while compiling:

```
In file included from /home/fausto/Development/toastboard/src/components/memfault_port/memfault-firmware-sdk/components/include/memfault/components.h:20,
from /home/fausto/Development/toastboard/src/main/main.c:17:
/home/fausto/Development/toastboard/src/components/memfault_port/memfault-firmware-sdk/components/include/memfault/config.h:30:39: fatal error: memfault_platform_config.h: No such file or directory
#define MEMFAULT_PLATFORM_CONFIG_FILE "memfault_platform_config.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

I later found this from the `CHANGES.md` file in the Memfault SDK:

```md
- You must create the file `memfault_platform_config.h` and add it to your
include path. This file can be used in place of compiler defines to tune the
SDK configurations settings.
```

After solving that by creating `src/main/include/memfault_platform_config.h`, there was another missing header file:

```
/home/fausto/Development/toastboard/src/components/memfault_port/memfault-firmware-sdk/ports/esp8266_sdk/memfault/esp_reboot_tracking.c:14:10: fatal error: internal/esp_system_internal.h: No such file or directory
#include "internal/esp_system_internal.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

I got a bunch of compiler errors because I was on the `master` branch of the SDK and the Memfault integration supports only v3.3. The changes from `master` to v3.3 are significant in the HTTP server example, so I'm updating my application code.

The next compiler error was a missing header file `esp_console.h` expected in `memfault_cli.c`. I could solve it by adding this to `src/sdkconfig`:

```
CONFIG_USING_ESP_CONSOLE=y
```

Et voila! It has built! Time to get it flashed onto the board.
10 changes: 0 additions & 10 deletions src/CMakeLists.txt

This file was deleted.

4 changes: 1 addition & 3 deletions src/Makefile
@@ -1,8 +1,6 @@
PROJECT_NAME := toastboard

EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common

EXTRA_COMPONENT_DIRS += \
EXTRA_COMPONENT_DIRS = \
components/memfault_port/memfault-firmware-sdk/ports/esp8266_sdk/memfault \
components/memfault_port/memfault-firmware-sdk/ports/esp8266_sdk/memfault_freertos

Expand Down
14 changes: 10 additions & 4 deletions src/components/memfault_port/memfault_port.c
Expand Up @@ -4,15 +4,21 @@

#include <stdio.h>

#include "esp_system.h"
#include "memfault/esp8266_port/core.h"

sMfltHttpClientConfig g_mflt_http_client_config = {
.api_key = "YOUR_PROJECT_API_KEY",
.api_key = CONFIG_MEMFAULT_API_KEY,
};

static char s_fw_version[32];

void memfault_platform_get_device_info(sMemfaultDeviceInfo *info) {
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
uint8_t device_mac;
esp_base_mac_addr_get(&device_mac);

if (s_fw_version[0] == 0) {
// initialize version
char build_id[7];
Expand All @@ -23,10 +29,10 @@ void memfault_platform_get_device_info(sMemfaultDeviceInfo *info) {
// platform specific version information. For more details
// see https://mflt.io/version-nomenclature
*info = (sMemfaultDeviceInfo) {
.device_serial = "DEMOSERIAL",
.software_type = "app-fw",
.device_serial = device_mac,
.software_type = "toastboard-firmware",
.software_version = s_fw_version,
.hardware_version = "dvt",
.hardware_version = chip_info.revision,
};
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/CMakeLists.txt

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/include/memfault_platform_config.h
@@ -0,0 +1,10 @@
#pragma once

//! @file
//!
//! Copyright (c) Memfault, Inc.
//! See License.txt for details
//!
//! Platform overrides for the default configuration settings in the memfault-firmware-sdk.
//! Default configuration settings can be found in "memfault/config.h"

204 changes: 126 additions & 78 deletions src/main/main.c
Expand Up @@ -6,98 +6,146 @@
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <sys/param.h>

#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"
#include <esp_wifi.h>
#include <esp_event_loop.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>

#include <esp_http_server.h>

static const char *TAG = "APP";

esp_err_t hello_get_handler(httpd_req_t *req) {
/* Send response with custom headers and body set as the
* string passed in user context*/
const char *resp_str = (const char *)req->user_ctx;
httpd_resp_send(req, resp_str, strlen(resp_str));

/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
ESP_LOGI(TAG, "Request headers lost");
}
#include "memfault/components.h"

return ESP_OK;
/* A simple example that demonstrates how to create GET and POST
* handlers for the web server.
* The examples use simple WiFi configuration that you can set via
* 'make menuconfig'.
* If you'd rather not, just change the below entries to strings
* with the config you want -
* ie. #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
#define EXAMPLE_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD

static const char *TAG="TOASTBOARD";

/* An HTTP GET handler */
esp_err_t hello_get_handler(httpd_req_t *req)
{
/* Send response with custom headers and body set as the
* string passed in user context*/
const char* resp_str = (const char*) req->user_ctx;
httpd_resp_send(req, resp_str, strlen(resp_str));

/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
ESP_LOGI(TAG, "Request headers lost");
}
return ESP_OK;
}

httpd_uri_t hello = {.uri = "/hello",
.method = HTTP_GET,
.handler = hello_get_handler,
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = "Hello World!"};

httpd_handle_t start_webserver(void) {
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();

// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &hello);
return server;
}

ESP_LOGI(TAG, "Error starting server!");
return NULL;
httpd_uri_t hello = {
.uri = "/hello",
.method = HTTP_GET,
.handler = hello_get_handler,
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = "Hello World!"
};

httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();

// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &hello);
return server;
}

ESP_LOGI(TAG, "Error starting server!");
return NULL;
}

void stop_webserver(httpd_handle_t server) {
// Stop the httpd server
httpd_stop(server);
void stop_webserver(httpd_handle_t server)
{
// Stop the httpd server
httpd_stop(server);
}

static httpd_handle_t server = NULL;

static void disconnect_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
httpd_handle_t *server = (httpd_handle_t *)arg;
if (*server) {
ESP_LOGI(TAG, "Stopping webserver");
stop_webserver(*server);
*server = NULL;
}
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
httpd_handle_t *server = (httpd_handle_t *) ctx;
/* For accessing reason codes in case of disconnection */
system_event_info_t *info = &event->event_info;

switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
ESP_ERROR_CHECK(esp_wifi_connect());
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
ESP_LOGI(TAG, "Got IP: '%s'",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));

/* Start the web server */
if (*server == NULL) {
*server = start_webserver();
}
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason);
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
/*Switch to 802.11 bgn mode */
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
}
ESP_ERROR_CHECK(esp_wifi_connect());

/* Stop the web server */
if (*server) {
stop_webserver(*server);
*server = NULL;
}
break;
default:
break;
}
return ESP_OK;
}

static void connect_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
httpd_handle_t *server = (httpd_handle_t *)arg;
if (*server == NULL) {
ESP_LOGI(TAG, "Starting webserver");
*server = start_webserver();
}
static void initialise_wifi(void *arg)
{
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}

void app_main() {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

ESP_ERROR_CHECK(example_connect());

ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
&connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
void app_main()
{
memfault_platform_boot();

server = start_webserver();
static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(nvs_flash_init());
initialise_wifi(&server);
}

0 comments on commit 6ae1b36

Please sign in to comment.