|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 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 | + |
| 10 | +#include "lwip/ip4_addr.h" |
| 11 | +#include "lwip/dns.h" |
| 12 | +#include "lwip/pbuf.h" |
| 13 | +#include "lwip/udp.h" |
| 14 | +#include "lwip/sockets.h" |
| 15 | +#include "lwip/netdb.h" |
| 16 | + |
| 17 | +#include "FreeRTOS.h" |
| 18 | +#include "task.h" |
| 19 | + |
| 20 | +#ifndef RUN_FREERTOS_ON_CORE |
| 21 | +#define RUN_FREERTOS_ON_CORE 0 |
| 22 | +#endif |
| 23 | + |
| 24 | +#define TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 1UL ) |
| 25 | + |
| 26 | +#define NTP_SERVER ("pool.ntp.org") |
| 27 | +#define NTP_MSG_LEN (48) |
| 28 | +#define NTP_PORT (123) |
| 29 | +// seconds between 1 Jan 1900 and 1 Jan 1970 |
| 30 | +#define NTP_DELTA (2208988800) |
| 31 | +#define ntpEST_TIME (30 * 1000) |
| 32 | +#define NTP_FAIL_TIME (10) |
| 33 | + |
| 34 | +void main_task(__unused void *params) { |
| 35 | + if (cyw43_arch_init()) { |
| 36 | + printf("failed to initialise\n"); |
| 37 | + vTaskDelete(NULL); |
| 38 | + } |
| 39 | + cyw43_arch_enable_sta_mode(); |
| 40 | + printf("Connecting to Wi-Fi...\n"); |
| 41 | + if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { |
| 42 | + printf("failed to connect.\n"); |
| 43 | + vTaskDelete(NULL); |
| 44 | + } else { |
| 45 | + printf("Connected.\n"); |
| 46 | + } |
| 47 | + |
| 48 | + while(true) { |
| 49 | + const struct addrinfo ntp_info = { |
| 50 | + .ai_flags = 0, |
| 51 | + .ai_family = AF_UNSPEC, |
| 52 | + .ai_socktype = SOCK_DGRAM, |
| 53 | + .ai_protocol = 0, |
| 54 | + }; |
| 55 | + |
| 56 | + struct addrinfo *dns_result = NULL; |
| 57 | + printf("Trying DNS...\n"); |
| 58 | + int err = getaddrinfo(NTP_SERVER, "123", &ntp_info, &dns_result); |
| 59 | + if (err != 0) |
| 60 | + { |
| 61 | + printf("DNS lookup failed with error: %d\n", err); |
| 62 | + continue; |
| 63 | + } |
| 64 | +#ifndef INET6_ADDRSTRLEN |
| 65 | + char address[INET_ADDRSTRLEN] = {0}; |
| 66 | +#else |
| 67 | + char address[INET6_ADDRSTRLEN] = {0}; |
| 68 | +#endif |
| 69 | + ip_addr_t ip_addr; |
| 70 | +#if LWIP_IPV4 |
| 71 | + if (dns_result->ai_addr->sa_family == AF_INET) { |
| 72 | + const struct sockaddr_in *sock_addr = (struct sockaddr_in*)dns_result->ai_addr; |
| 73 | + inet_addr_to_ip4addr(ip_2_ip4(&ip_addr), &sock_addr->sin_addr); |
| 74 | + IP_SET_TYPE(&ip_addr, IPADDR_TYPE_V4); |
| 75 | + } |
| 76 | +#endif |
| 77 | +#if LWIP_IPV6 |
| 78 | + if (dns_result->ai_addr->sa_family == AF_INET6) { |
| 79 | + const struct sockaddr_in6 *sock_addr = (struct sockaddr_in6*)dns_result->ai_addr; |
| 80 | + inet6_addr_to_ip6addr(ip_2_ip6(&ip_addr), &sock_addr->sin6_addr); |
| 81 | + IP_SET_TYPE(&ip_addr, IPADDR_TYPE_V6); |
| 82 | + } |
| 83 | +#endif |
| 84 | + inet_ntop(dns_result->ai_family, &ip_addr, address, sizeof(address)); |
| 85 | + printf("Got DNS response: %s\n", address); |
| 86 | + |
| 87 | + int sock = socket(dns_result->ai_family, dns_result->ai_socktype, dns_result->ai_protocol); |
| 88 | + if (sock == -1) |
| 89 | + { |
| 90 | + printf("Failed to allocate socket"); |
| 91 | + freeaddrinfo(dns_result); |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + struct timeval timeout = { |
| 96 | + .tv_sec = NTP_FAIL_TIME, |
| 97 | + }; |
| 98 | + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); |
| 99 | + err = connect(sock, dns_result->ai_addr, dns_result->ai_addrlen); |
| 100 | + freeaddrinfo(dns_result); |
| 101 | + if (err == -1) |
| 102 | + { |
| 103 | + printf("Failed to connect to NTP server"); |
| 104 | + close(sock); |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + unsigned char request[NTP_MSG_LEN] = {0x1b, 0}; |
| 109 | + int amount = send(sock, request, NTP_MSG_LEN, 0); |
| 110 | + if (amount != NTP_MSG_LEN) |
| 111 | + { |
| 112 | + printf("We were unable to send the complete NTP request"); |
| 113 | + close(sock); |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + amount = recv(sock, request, NTP_MSG_LEN, 0); |
| 118 | + close(sock); |
| 119 | + if (amount != NTP_MSG_LEN) |
| 120 | + { |
| 121 | + printf("We did not receive a complete NTP response"); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + uint8_t mode = request[0] & 0x7; |
| 126 | + uint8_t stratum = request[1]; |
| 127 | + |
| 128 | + // Check the result |
| 129 | + if (amount == NTP_MSG_LEN && |
| 130 | + mode == 0x4 && |
| 131 | + stratum != 0) |
| 132 | + { |
| 133 | + uint8_t seconds_buf[4] = {}; |
| 134 | + memcpy(seconds_buf, request + 40, 4); |
| 135 | + uint32_t seconds_since_1900 = seconds_buf[0] << 24 | seconds_buf[1] << 16 | seconds_buf[2] << 8 | seconds_buf[3]; |
| 136 | + uint32_t seconds_since_1970 = seconds_since_1900 - NTP_DELTA; |
| 137 | + time_t epoch = seconds_since_1970; |
| 138 | + struct tm *utc = gmtime(&epoch); |
| 139 | + printf("got ntp response: %02d/%02d/%04d %02d:%02d:%02d\n", utc->tm_mday, utc->tm_mon + 1, utc->tm_year + 1900, |
| 140 | + utc->tm_hour, utc->tm_min, utc->tm_sec); |
| 141 | + } |
| 142 | + |
| 143 | + // This assumes one tick is one ms, which is true for the default configuration |
| 144 | + vTaskDelay(ntpEST_TIME); |
| 145 | + } |
| 146 | + |
| 147 | + cyw43_arch_deinit(); |
| 148 | +} |
| 149 | + |
| 150 | +void vLaunch( void) { |
| 151 | + TaskHandle_t task; |
| 152 | + xTaskCreate(main_task, "TestMainThread", 4096, NULL, TEST_TASK_PRIORITY, &task); |
| 153 | + |
| 154 | +#if NO_SYS && configUSE_CORE_AFFINITY && configNUM_CORES > 1 |
| 155 | + // we must bind the main task to one core (well at least while the init is called) |
| 156 | + // (note we only do this in NO_SYS mode, because cyw43_arch_freertos |
| 157 | + // takes care of it otherwise) |
| 158 | + vTaskCoreAffinitySet(task, 1); |
| 159 | +#endif |
| 160 | + |
| 161 | + /* Start the tasks and timer running. */ |
| 162 | + vTaskStartScheduler(); |
| 163 | +} |
| 164 | + |
| 165 | +int main( void ) |
| 166 | +{ |
| 167 | + stdio_init_all(); |
| 168 | + |
| 169 | + /* Configure the hardware ready to run the demo. */ |
| 170 | + const char *rtos_name; |
| 171 | +#if ( portSUPPORT_SMP == 1 ) |
| 172 | + rtos_name = "FreeRTOS SMP"; |
| 173 | +#else |
| 174 | + rtos_name = "FreeRTOS"; |
| 175 | +#endif |
| 176 | + |
| 177 | +#if ( portSUPPORT_SMP == 1 ) && ( configNUM_CORES == 2 ) |
| 178 | + printf("Starting %s on both cores:\n", rtos_name); |
| 179 | + vLaunch(); |
| 180 | +#elif ( RUN_FREERTOS_ON_CORE == 1 ) |
| 181 | + printf("Starting %s on core 1:\n", rtos_name); |
| 182 | + multicore_launch_core1(vLaunch); |
| 183 | + while (true); |
| 184 | +#else |
| 185 | + printf("Starting %s on core 0:\n", rtos_name); |
| 186 | + vLaunch(); |
| 187 | +#endif |
| 188 | + return 0; |
| 189 | +} |
0 commit comments