|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "pico/cyw43_arch.h" |
| 8 | +#include "pico/stdlib.h" |
| 9 | +#include "lwip/altcp_tls.h" |
| 10 | + |
| 11 | +#include "lwip/netif.h" |
| 12 | + |
| 13 | +#include "FreeRTOS.h" |
| 14 | +#include "task.h" |
| 15 | +#include "example_http_client_util.h" |
| 16 | + |
| 17 | +#ifndef RUN_FREERTOS_ON_CORE |
| 18 | +#define RUN_FREERTOS_ON_CORE 0 |
| 19 | +#endif |
| 20 | + |
| 21 | +#define TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 2UL ) |
| 22 | +#define TEST_TASK_STACK_SIZE 1024 |
| 23 | + |
| 24 | +// Using this url as we know the root cert won't change for a long time |
| 25 | +#define HOST "fw-download-alias1.raspberrypi.com" |
| 26 | +#define URL_REQUEST "/net_install/boot.sig" |
| 27 | + |
| 28 | +// This is the PUBLIC root certificate exported from a browser |
| 29 | +// Note that the newlines are needed |
| 30 | +#define TLS_ROOT_CERT_OK "-----BEGIN CERTIFICATE-----\n\ |
| 31 | +MIIC+jCCAn+gAwIBAgICEAAwCgYIKoZIzj0EAwIwgbcxCzAJBgNVBAYTAkdCMRAw\n\ |
| 32 | +DgYDVQQIDAdFbmdsYW5kMRIwEAYDVQQHDAlDYW1icmlkZ2UxHTAbBgNVBAoMFFJh\n\ |
| 33 | +c3BiZXJyeSBQSSBMaW1pdGVkMRwwGgYDVQQLDBNSYXNwYmVycnkgUEkgRUNDIENB\n\ |
| 34 | +MR0wGwYDVQQDDBRSYXNwYmVycnkgUEkgUm9vdCBDQTEmMCQGCSqGSIb3DQEJARYX\n\ |
| 35 | +c3VwcG9ydEByYXNwYmVycnlwaS5jb20wIBcNMjExMjA5MTEzMjU1WhgPMjA3MTEx\n\ |
| 36 | +MjcxMTMyNTVaMIGrMQswCQYDVQQGEwJHQjEQMA4GA1UECAwHRW5nbGFuZDEdMBsG\n\ |
| 37 | +A1UECgwUUmFzcGJlcnJ5IFBJIExpbWl0ZWQxHDAaBgNVBAsME1Jhc3BiZXJyeSBQ\n\ |
| 38 | +SSBFQ0MgQ0ExJTAjBgNVBAMMHFJhc3BiZXJyeSBQSSBJbnRlcm1lZGlhdGUgQ0Ex\n\ |
| 39 | +JjAkBgkqhkiG9w0BCQEWF3N1cHBvcnRAcmFzcGJlcnJ5cGkuY29tMHYwEAYHKoZI\n\ |
| 40 | +zj0CAQYFK4EEACIDYgAEcN9K6Cpv+od3w6yKOnec4EbyHCBzF+X2ldjorc0b2Pq0\n\ |
| 41 | +N+ZvyFHkhFZSgk2qvemsVEWIoPz+K4JSCpgPstz1fEV6WzgjYKfYI71ghELl5TeC\n\ |
| 42 | +byoPY+ee3VZwF1PTy0cco2YwZDAdBgNVHQ4EFgQUJ6YzIqFh4rhQEbmCnEbWmHEo\n\ |
| 43 | +XAUwHwYDVR0jBBgwFoAUIIAVCSiDPXut23NK39LGIyAA7NAwEgYDVR0TAQH/BAgw\n\ |
| 44 | +BgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwCgYIKoZIzj0EAwIDaQAwZgIxAJYM+wIM\n\ |
| 45 | +PC3wSPqJ1byJKA6D+ZyjKR1aORbiDQVEpDNWRKiQ5QapLg8wbcED0MrRKQIxAKUT\n\ |
| 46 | +v8TJkb/8jC/oBVTmczKlPMkciN+uiaZSXahgYKyYhvKTatCTZb+geSIhc0w/2w==\n\ |
| 47 | +-----END CERTIFICATE-----\n" |
| 48 | + |
| 49 | +void main_task(__unused void *params) { |
| 50 | + if (cyw43_arch_init()) { |
| 51 | + printf("failed to initialise\n"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + cyw43_arch_enable_sta_mode(); |
| 56 | + printf("Connecting to Wi-Fi...\n"); |
| 57 | + if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { |
| 58 | + printf("failed to connect.\n"); |
| 59 | + exit(1); |
| 60 | + } else { |
| 61 | + printf("Connected.\n"); |
| 62 | + } |
| 63 | + |
| 64 | + static const uint8_t cert_ok[] = TLS_ROOT_CERT_OK; |
| 65 | + static EXAMPLE_HTTP_REQUEST_T req = {0}; |
| 66 | + req.hostname = HOST; |
| 67 | + req.url = URL_REQUEST; |
| 68 | + req.headers_fn = http_client_header_print_fn; |
| 69 | + req.recv_fn = http_client_receive_print_fn; |
| 70 | + req.tls_config = altcp_tls_create_config_client(cert_ok, sizeof(cert_ok)); |
| 71 | + |
| 72 | + int pass = http_client_request_sync(cyw43_arch_async_context(), &req); |
| 73 | + altcp_tls_free_config(req.tls_config); |
| 74 | + if (pass != 0) { |
| 75 | + panic("test failed"); |
| 76 | + } |
| 77 | + |
| 78 | + cyw43_arch_deinit(); |
| 79 | + panic("Test passed"); |
| 80 | +} |
| 81 | + |
| 82 | +void vLaunch( void) { |
| 83 | + TaskHandle_t task; |
| 84 | + xTaskCreate(main_task, "TestMainThread", TEST_TASK_STACK_SIZE, NULL, TEST_TASK_PRIORITY, &task); |
| 85 | + |
| 86 | +#if NO_SYS && configUSE_CORE_AFFINITY && configNUM_CORES > 1 |
| 87 | + // we must bind the main task to one core (well at least while the init is called) |
| 88 | + // (note we only do this in NO_SYS mode, because cyw43_arch_freertos |
| 89 | + // takes care of it otherwise) |
| 90 | + vTaskCoreAffinitySet(task, 1); |
| 91 | +#endif |
| 92 | + |
| 93 | + /* Start the tasks and timer running. */ |
| 94 | + vTaskStartScheduler(); |
| 95 | +} |
| 96 | + |
| 97 | +int main( void ) |
| 98 | +{ |
| 99 | + stdio_init_all(); |
| 100 | + |
| 101 | + /* Configure the hardware ready to run the demo. */ |
| 102 | + const char *rtos_name; |
| 103 | +#if ( portSUPPORT_SMP == 1 ) |
| 104 | + rtos_name = "FreeRTOS SMP"; |
| 105 | +#else |
| 106 | + rtos_name = "FreeRTOS"; |
| 107 | +#endif |
| 108 | + |
| 109 | +#if ( portSUPPORT_SMP == 1 ) && ( configNUM_CORES == 2 ) |
| 110 | + printf("Starting %s on both cores:\n", rtos_name); |
| 111 | + vLaunch(); |
| 112 | +#elif ( RUN_FREERTOS_ON_CORE == 1 ) |
| 113 | + printf("Starting %s on core 1:\n", rtos_name); |
| 114 | + multicore_launch_core1(vLaunch); |
| 115 | + while (true); |
| 116 | +#else |
| 117 | + printf("Starting %s on core 0:\n", rtos_name); |
| 118 | + vLaunch(); |
| 119 | +#endif |
| 120 | + return 0; |
| 121 | +} |
0 commit comments