Skip to content

Commit 6ae1b36

Browse files
committed
Get the Memfault integration to the point where I can build
1 parent edbfc2c commit 6ae1b36

File tree

7 files changed

+183
-99
lines changed

7 files changed

+183
-99
lines changed

NOTES.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,39 @@ Type some stuff... and it appears on my newest terminal! Cool! Trying to type on
242242
Documentation link: https://docs.memfault.com/docs/embedded/esp8266-rtos-sdk-guide/
243243

244244
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.
245+
246+
Ran into this while compiling:
247+
248+
```
249+
In file included from /home/fausto/Development/toastboard/src/components/memfault_port/memfault-firmware-sdk/components/include/memfault/components.h:20,
250+
from /home/fausto/Development/toastboard/src/main/main.c:17:
251+
/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
252+
#define MEMFAULT_PLATFORM_CONFIG_FILE "memfault_platform_config.h"
253+
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
254+
```
255+
256+
I later found this from the `CHANGES.md` file in the Memfault SDK:
257+
258+
```md
259+
- You must create the file `memfault_platform_config.h` and add it to your
260+
include path. This file can be used in place of compiler defines to tune the
261+
SDK configurations settings.
262+
```
263+
264+
After solving that by creating `src/main/include/memfault_platform_config.h`, there was another missing header file:
265+
266+
```
267+
/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
268+
#include "internal/esp_system_internal.h"
269+
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270+
```
271+
272+
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.
273+
274+
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`:
275+
276+
```
277+
CONFIG_USING_ESP_CONSOLE=y
278+
```
279+
280+
Et voila! It has built! Time to get it flashed onto the board.

src/CMakeLists.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
PROJECT_NAME := toastboard
22

3-
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
4-
5-
EXTRA_COMPONENT_DIRS += \
3+
EXTRA_COMPONENT_DIRS = \
64
components/memfault_port/memfault-firmware-sdk/ports/esp8266_sdk/memfault \
75
components/memfault_port/memfault-firmware-sdk/ports/esp8266_sdk/memfault_freertos
86

src/components/memfault_port/memfault_port.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44

55
#include <stdio.h>
66

7+
#include "esp_system.h"
78
#include "memfault/esp8266_port/core.h"
89

910
sMfltHttpClientConfig g_mflt_http_client_config = {
10-
.api_key = "YOUR_PROJECT_API_KEY",
11+
.api_key = CONFIG_MEMFAULT_API_KEY,
1112
};
1213

1314
static char s_fw_version[32];
1415

1516
void memfault_platform_get_device_info(sMemfaultDeviceInfo *info) {
17+
esp_chip_info_t chip_info;
18+
esp_chip_info(&chip_info);
19+
uint8_t device_mac;
20+
esp_base_mac_addr_get(&device_mac);
21+
1622
if (s_fw_version[0] == 0) {
1723
// initialize version
1824
char build_id[7];
@@ -23,10 +29,10 @@ void memfault_platform_get_device_info(sMemfaultDeviceInfo *info) {
2329
// platform specific version information. For more details
2430
// see https://mflt.io/version-nomenclature
2531
*info = (sMemfaultDeviceInfo) {
26-
.device_serial = "DEMOSERIAL",
27-
.software_type = "app-fw",
32+
.device_serial = device_mac,
33+
.software_type = "toastboard-firmware",
2834
.software_version = s_fw_version,
29-
.hardware_version = "dvt",
35+
.hardware_version = chip_info.revision,
3036
};
3137
}
3238

src/main/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
//! @file
4+
//!
5+
//! Copyright (c) Memfault, Inc.
6+
//! See License.txt for details
7+
//!
8+
//! Platform overrides for the default configuration settings in the memfault-firmware-sdk.
9+
//! Default configuration settings can be found in "memfault/config.h"
10+

src/main/main.c

Lines changed: 126 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,98 +6,146 @@
66
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
77
CONDITIONS OF ANY KIND, either express or implied.
88
*/
9-
#include <sys/param.h>
109

11-
#include "esp_event.h"
12-
#include "esp_log.h"
13-
#include "esp_netif.h"
14-
#include "esp_system.h"
15-
#include "freertos/FreeRTOS.h"
16-
#include "freertos/task.h"
17-
#include "nvs.h"
18-
#include "nvs_flash.h"
19-
#include "protocol_examples_common.h"
10+
#include <esp_wifi.h>
11+
#include <esp_event_loop.h>
12+
#include <esp_log.h>
13+
#include <esp_system.h>
14+
#include <nvs_flash.h>
15+
#include <sys/param.h>
2016

2117
#include <esp_http_server.h>
2218

23-
static const char *TAG = "APP";
24-
25-
esp_err_t hello_get_handler(httpd_req_t *req) {
26-
/* Send response with custom headers and body set as the
27-
* string passed in user context*/
28-
const char *resp_str = (const char *)req->user_ctx;
29-
httpd_resp_send(req, resp_str, strlen(resp_str));
30-
31-
/* After sending the HTTP response the old HTTP request
32-
* headers are lost. Check if HTTP request headers can be read now. */
33-
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
34-
ESP_LOGI(TAG, "Request headers lost");
35-
}
19+
#include "memfault/components.h"
3620

37-
return ESP_OK;
21+
/* A simple example that demonstrates how to create GET and POST
22+
* handlers for the web server.
23+
* The examples use simple WiFi configuration that you can set via
24+
* 'make menuconfig'.
25+
* If you'd rather not, just change the below entries to strings
26+
* with the config you want -
27+
* ie. #define EXAMPLE_WIFI_SSID "mywifissid"
28+
*/
29+
#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
30+
#define EXAMPLE_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD
31+
32+
static const char *TAG="TOASTBOARD";
33+
34+
/* An HTTP GET handler */
35+
esp_err_t hello_get_handler(httpd_req_t *req)
36+
{
37+
/* Send response with custom headers and body set as the
38+
* string passed in user context*/
39+
const char* resp_str = (const char*) req->user_ctx;
40+
httpd_resp_send(req, resp_str, strlen(resp_str));
41+
42+
/* After sending the HTTP response the old HTTP request
43+
* headers are lost. Check if HTTP request headers can be read now. */
44+
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
45+
ESP_LOGI(TAG, "Request headers lost");
46+
}
47+
return ESP_OK;
3848
}
3949

40-
httpd_uri_t hello = {.uri = "/hello",
41-
.method = HTTP_GET,
42-
.handler = hello_get_handler,
43-
/* Let's pass response string in user
44-
* context to demonstrate it's usage */
45-
.user_ctx = "Hello World!"};
46-
47-
httpd_handle_t start_webserver(void) {
48-
httpd_handle_t server = NULL;
49-
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
50-
51-
// Start the httpd server
52-
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
53-
if (httpd_start(&server, &config) == ESP_OK) {
54-
// Set URI handlers
55-
ESP_LOGI(TAG, "Registering URI handlers");
56-
httpd_register_uri_handler(server, &hello);
57-
return server;
58-
}
59-
60-
ESP_LOGI(TAG, "Error starting server!");
61-
return NULL;
50+
httpd_uri_t hello = {
51+
.uri = "/hello",
52+
.method = HTTP_GET,
53+
.handler = hello_get_handler,
54+
/* Let's pass response string in user
55+
* context to demonstrate it's usage */
56+
.user_ctx = "Hello World!"
57+
};
58+
59+
httpd_handle_t start_webserver(void)
60+
{
61+
httpd_handle_t server = NULL;
62+
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
63+
64+
// Start the httpd server
65+
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
66+
if (httpd_start(&server, &config) == ESP_OK) {
67+
// Set URI handlers
68+
ESP_LOGI(TAG, "Registering URI handlers");
69+
httpd_register_uri_handler(server, &hello);
70+
return server;
71+
}
72+
73+
ESP_LOGI(TAG, "Error starting server!");
74+
return NULL;
6275
}
6376

64-
void stop_webserver(httpd_handle_t server) {
65-
// Stop the httpd server
66-
httpd_stop(server);
77+
void stop_webserver(httpd_handle_t server)
78+
{
79+
// Stop the httpd server
80+
httpd_stop(server);
6781
}
6882

69-
static httpd_handle_t server = NULL;
70-
71-
static void disconnect_handler(void *arg, esp_event_base_t event_base,
72-
int32_t event_id, void *event_data) {
73-
httpd_handle_t *server = (httpd_handle_t *)arg;
74-
if (*server) {
75-
ESP_LOGI(TAG, "Stopping webserver");
76-
stop_webserver(*server);
77-
*server = NULL;
78-
}
83+
static esp_err_t event_handler(void *ctx, system_event_t *event)
84+
{
85+
httpd_handle_t *server = (httpd_handle_t *) ctx;
86+
/* For accessing reason codes in case of disconnection */
87+
system_event_info_t *info = &event->event_info;
88+
89+
switch(event->event_id) {
90+
case SYSTEM_EVENT_STA_START:
91+
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
92+
ESP_ERROR_CHECK(esp_wifi_connect());
93+
break;
94+
case SYSTEM_EVENT_STA_GOT_IP:
95+
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
96+
ESP_LOGI(TAG, "Got IP: '%s'",
97+
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
98+
99+
/* Start the web server */
100+
if (*server == NULL) {
101+
*server = start_webserver();
102+
}
103+
break;
104+
case SYSTEM_EVENT_STA_DISCONNECTED:
105+
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
106+
ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason);
107+
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
108+
/*Switch to 802.11 bgn mode */
109+
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
110+
}
111+
ESP_ERROR_CHECK(esp_wifi_connect());
112+
113+
/* Stop the web server */
114+
if (*server) {
115+
stop_webserver(*server);
116+
*server = NULL;
117+
}
118+
break;
119+
default:
120+
break;
121+
}
122+
return ESP_OK;
79123
}
80124

81-
static void connect_handler(void *arg, esp_event_base_t event_base,
82-
int32_t event_id, void *event_data) {
83-
httpd_handle_t *server = (httpd_handle_t *)arg;
84-
if (*server == NULL) {
85-
ESP_LOGI(TAG, "Starting webserver");
86-
*server = start_webserver();
87-
}
125+
static void initialise_wifi(void *arg)
126+
{
127+
tcpip_adapter_init();
128+
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg));
129+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
130+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
131+
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
132+
wifi_config_t wifi_config = {
133+
.sta = {
134+
.ssid = EXAMPLE_WIFI_SSID,
135+
.password = EXAMPLE_WIFI_PASS,
136+
},
137+
};
138+
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
139+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
140+
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
141+
ESP_ERROR_CHECK(esp_wifi_start());
88142
}
89143

90-
void app_main() {
91-
ESP_ERROR_CHECK(nvs_flash_init());
92-
ESP_ERROR_CHECK(esp_netif_init());
93-
ESP_ERROR_CHECK(esp_event_loop_create_default());
94-
95-
ESP_ERROR_CHECK(example_connect());
96-
97-
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
98-
&connect_handler, &server));
99-
ESP_ERROR_CHECK(esp_event_handler_register(
100-
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
144+
void app_main()
145+
{
146+
memfault_platform_boot();
101147

102-
server = start_webserver();
148+
static httpd_handle_t server = NULL;
149+
ESP_ERROR_CHECK(nvs_flash_init());
150+
initialise_wifi(&server);
103151
}

0 commit comments

Comments
 (0)