From eb18c56078a047a2aef35a8f74b31ceebdb7928c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 22 Jul 2022 10:31:57 -0300 Subject: Add files --- main/CMakeLists.txt | 12 ++ main/component.mk | 8 ++ main/howsmyssl_com_root_cert.pem | 31 +++++ main/include/main.h | 44 ++++++ main/main.c | 285 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 380 insertions(+) create mode 100644 main/CMakeLists.txt create mode 100644 main/component.mk create mode 100644 main/howsmyssl_com_root_cert.pem create mode 100644 main/include/main.h create mode 100644 main/main.c (limited to 'main') diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..c99234f --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,12 @@ +# This example code is in the Public Domain (or CC0 licensed, at your option.) +# Unless required by applicable law or agreed to in writing, this +# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +# CONDITIONS OF ANY KIND, either express or implied. + +idf_component_register( + SRCS + "main.c" + INCLUDE_DIRS + "." "include" + EMBED_TXTFILES howsmyssl_com_root_cert.pem +) diff --git a/main/component.mk b/main/component.mk new file mode 100644 index 0000000..c1f04b7 --- /dev/null +++ b/main/component.mk @@ -0,0 +1,8 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) + +# embed files from the "certs" directory as binary data symbols +# in the app +COMPONENT_EMBED_TXTFILES := howsmyssl_com_root_cert.pem postman_root_cert.pem diff --git a/main/howsmyssl_com_root_cert.pem b/main/howsmyssl_com_root_cert.pem new file mode 100644 index 0000000..b85c803 --- /dev/null +++ b/main/howsmyssl_com_root_cert.pem @@ -0,0 +1,31 @@ +-----BEGIN CERTIFICATE----- +MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3Vw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LHYGmkgJykIrGF1XIpu+ILlaS/V9lZL +ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ +3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK +NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5 +ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur +TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC +jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc +oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq +4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA +mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d +emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc= +-----END CERTIFICATE----- diff --git a/main/include/main.h b/main/include/main.h new file mode 100644 index 0000000..14e997f --- /dev/null +++ b/main/include/main.h @@ -0,0 +1,44 @@ +/* + * This example code is in the Public Domain (or CC0 licensed, at your option.) + * Unless required by applicable law or agreed to in writing, this + * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ + +#pragma once + +// For pin mappings. +#include "hardware.h" +// For graphics. +#include "pax_gfx.h" +// For PNG images. +#include "pax_codecs.h" +// The screen driver. +#include "ili9341.h" +// For all system settings and alike. +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "esp_system.h" +#include "nvs.h" +#include "nvs_flash.h" +// For WiFi connectivity. +#include "wifi_connect.h" +#include "wifi_connection.h" +// For exiting to the launcher. +#include "soc/rtc.h" +#include "soc/rtc_cntl_reg.h" + +#include "esp_log.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_tls.h" +#include "esp_crt_bundle.h" + +#include "esp_http_client.h" + +// Updates the screen with the last drawing. +void disp_flush(); + +// Exits the app, returning to the launcher. +void exit_to_launcher(); diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..6d6a535 --- /dev/null +++ b/main/main.c @@ -0,0 +1,285 @@ +/* + * This example code is in the Public Domain (or CC0 licensed, at your option.) + * Unless required by applicable law or agreed to in writing, this + * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ + +// This file contains a simple Hello World app which you can base you own +// native Badge apps on. + +#include "main.h" + +static pax_buf_t buf; +xQueueHandle buttonQueue; + +#include +static const char *TAG = "TALER"; + +// Updates the screen with the latest buffer. +void disp_flush() +{ + ili9341_write(get_ili9341(), buf.buf); +} + +// Exits the app, returning to the launcher. +void exit_to_launcher() +{ + REG_WRITE(RTC_CNTL_STORE0_REG, 0); + esp_restart(); +} + +/* Root cert for howsmyssl.com, taken from howsmyssl_com_root_cert.pem + + The PEM file was extracted from the output of this command: + openssl s_client -showcerts -connect www.howsmyssl.com:443 event_id) + { + case HTTP_EVENT_ERROR: + ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); + break; + case HTTP_EVENT_ON_CONNECTED: + ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); + break; + case HTTP_EVENT_HEADER_SENT: + ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); + break; + case HTTP_EVENT_ON_HEADER: + ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); + break; + case HTTP_EVENT_ON_DATA: + ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); + /* + * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data. + * However, event handler can also be used in case chunked encoding is used. + */ + if (!esp_http_client_is_chunked_response(evt->client)) + { + // If user_data buffer is configured, copy the response into the buffer + if (evt->user_data) + { + memcpy(evt->user_data + output_len, evt->data, evt->data_len); + } + else + { + if (output_buffer == NULL) + { + output_buffer = (char *)malloc(esp_http_client_get_content_length(evt->client)); + output_len = 0; + if (output_buffer == NULL) + { + ESP_LOGE(TAG, "Failed to allocate memory for output buffer"); + return ESP_FAIL; + } + } + memcpy(output_buffer + output_len, evt->data, evt->data_len); + } + output_len += evt->data_len; + } + + break; + case HTTP_EVENT_ON_FINISH: + ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); + if (output_buffer != NULL) + { + + // order_id = (char *)malloc(output_len); + // memcpy(order_id, output_buffer, output_len); + // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response + // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len); + const pax_font_t *font = pax_font_saira_condensed; + + pax_vec1_t dims = pax_text_size(font, font->default_size, output_buffer); + + // Draw the centered text. + pax_draw_text( + &buf, // Buffer to draw to. + 0xff000000, // color + font, font->default_size, // Font and size to use. + // Position (top left corner) of the app. + (buf.width - dims.x) / 2.0, + (buf.height - dims.y) / 2.0, + // The text to be rendered. + output_buffer); + + // Draws the entire graphics buffer to the screen. + disp_flush(); + + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); + int mbedtls_err = 0; + esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL); + if (err != 0) + { + ESP_LOGI(TAG, "Last esp error code: 0x%x", err); + ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); + } + if (output_buffer != NULL) + { + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + // case HTTP_EVENT_REDIRECT: + // ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT"); + // esp_http_client_set_header(evt->client, "From", "user@example.com"); + // esp_http_client_set_header(evt->client, "Accept", "text/html"); + // break; + } + return ESP_OK; +} + +#define MAX_HTTP_OUTPUT_BUFFER 2048 +void app_main() +{ + + ESP_LOGI(TAG, "Starting app!"); + + // Initialize the screen, the I2C and the SPI busses. + bsp_init(); + + // Initialize the RP2040 (responsible for buttons, etc). + bsp_rp2040_init(); + + // This queue is used to receive button presses. + buttonQueue = get_rp2040()->queue; + + // Initialize graphics for the screen. + pax_buf_init(&buf, NULL, 320, 240, PAX_BUF_16_565RGB); + + // Initialize NVS. + nvs_flash_init(); + + // Initialize WiFi. This doesn't connect to Wifi yet. + wifi_init(); + + int hue = esp_random() & 255; + pax_col_t col = pax_col_hsv(hue, 255 /*saturation*/, 255 /*brighness*/); + pax_background(&buf, col); + + // This text is shown on screen. + char *text = "GNU Taler Merchant!"; + + char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; + // Pick the font (Saira is the only one that looks nice in this size). + const pax_font_t *font = pax_font_saira_condensed; + { + + // Determine how the text dimensions so we can display it centered on + // screen. + pax_vec1_t dims = pax_text_size(font, font->default_size, text); + + // Draw the centered text. + pax_draw_text( + &buf, // Buffer to draw to. + 0xff000000, // color + font, font->default_size, // Font and size to use. + // Position (top left corner) of the app. + (buf.width - dims.x) / 2.0, + (buf.height - dims.y) / 2.0, + // The text to be rendered. + text); + + // Draws the entire graphics buffer to the screen. + disp_flush(); + } + + // curl https://merchant-backend.taler.ar/instances/mch2022/private/orders + // -H "Authorization: Bearer secret-token:305a16ce7cc2e2793060e39b9210a700" + + while (1) + { + // POST + // const char *post_data = "{\"order\":{\"summary\":\"s\",\"amount\":\"ARS:1\"}}"; + // esp_http_client_set_url(client, "https://merchant-backend.taler.ar/instances/mch2022/private/orders"); + // esp_http_client_set_url(client, "http://arcade.taler.ar/test/instances/mch2022/private/orders"); + // esp_http_client_set_method(client, HTTP_METHOD_POST); + // esp_http_client_set_header(client, "Content-Type", "application/json"); + // esp_http_client_set_header(client, "Authorization", "Bearer secret-token:305a16ce7cc2e2793060e39b9210a700"); + // esp_http_client_set_post_field(client, post_data, strlen(post_data)); + esp_http_client_config_t config = { + .host = "arcade.taler.ar", + .port = 7777, + .user_data = local_response_buffer, // Pass address of local buffer to get response + .path = "/", + .keep_alive_enable = true, + .transport_type = HTTP_TRANSPORT_OVER_TCP, + // .event_handler = _http_event_handler, + // .cert_pem = howsmyssl_com_root_cert_pem_start, + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + + esp_err_t err = esp_http_client_perform(client); + // if (err == ESP_OK) + // { + // // ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %lld", + // // esp_http_client_get_status_code(client), + // // esp_http_client_get_content_length(client)); + + // // Determine how the text dimensions so we can display it centered on + // // screen. + // } + // else + // { + // ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); + // } + + // Wait for button presses and do another cycle. + int hue = esp_random() & 255; + pax_col_t col = pax_col_hsv(hue, 255 /*saturation*/, 255 /*brighness*/); + pax_background(&buf, col); + + // Determine how the text dimensions so we can display it centered on + // screen. + pax_vec1_t dims = pax_text_size(font, font->default_size, local_response_buffer); + + // Draw the centered text. + pax_draw_text( + &buf, // Buffer to draw to. + 0xff000000, // color + font, font->default_size, // Font and size to use. + // Position (top left corner) of the app. + (buf.width - dims.x) / 2.0, + (buf.height - dims.y) / 2.0, + // The text to be rendered. + local_response_buffer); + + // Draws the entire graphics buffer to the screen. + disp_flush(); + + // Structure used to receive data. + rp2040_input_message_t message; + + // Wait forever for a button press (because of portMAX_DELAY) + xQueueReceive(buttonQueue, &message, portMAX_DELAY); + + // Which button is currently pressed? + if (message.input == RP2040_INPUT_BUTTON_START && message.state) + { + // If home is pressed, exit to launcher. + exit_to_launcher(); + } + } +} -- cgit v1.2.3