Skip to content

Commit eeb4902

Browse files
committed
Add HTTP client examples
Examples to demonstrate HTTP, HTTPS and TLS validation Fixes #318
1 parent bf0017c commit eeb4902

File tree

12 files changed

+702
-0
lines changed

12 files changed

+702
-0
lines changed

pico_w/wifi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ else()
1616
add_subdirectory(tcp_server)
1717
add_subdirectory(freertos)
1818
add_subdirectory(udp_beacon)
19+
add_subdirectory(http_client)
1920

2021
if (NOT PICO_MBEDTLS_PATH)
2122
message("Skipping tls examples as PICO_MBEDTLS_PATH is not defined")

pico_w/wifi/freertos/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ else()
55

66
add_subdirectory(iperf)
77
add_subdirectory(ping)
8+
add_subdirectory(http_client)
89
endif()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
add_executable(picow_freertos_http_client_nosys
2+
picow_freertos_http_client.c
3+
)
4+
target_compile_definitions(picow_freertos_http_client_nosys PRIVATE
5+
WIFI_SSID=\"${WIFI_SSID}\"
6+
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
7+
ALTCP_MBEDTLS_AUTHMODE=MBEDTLS_SSL_VERIFY_REQUIRED
8+
)
9+
target_include_directories(picow_freertos_http_client_nosys PRIVATE
10+
${CMAKE_CURRENT_LIST_DIR}
11+
${CMAKE_CURRENT_LIST_DIR}/../.. # for our common lwipopts
12+
)
13+
target_link_libraries(picow_freertos_http_client_nosys
14+
pico_cyw43_arch_lwip_threadsafe_background
15+
pico_stdlib
16+
pico_http_client
17+
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
18+
)
19+
pico_add_extra_outputs(picow_freertos_http_client_nosys)
20+
21+
add_executable(picow_freertos_http_client_sys
22+
picow_freertos_http_client.c
23+
)
24+
target_compile_definitions(picow_freertos_http_client_sys PRIVATE
25+
WIFI_SSID=\"${WIFI_SSID}\"
26+
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
27+
NO_SYS=0 # don't want NO_SYS (generally this would be in your lwipopts.h)
28+
ALTCP_MBEDTLS_AUTHMODE=MBEDTLS_SSL_VERIFY_REQUIRED
29+
)
30+
target_include_directories(picow_freertos_http_client_sys PRIVATE
31+
${CMAKE_CURRENT_LIST_DIR}
32+
${CMAKE_CURRENT_LIST_DIR}/../.. # for our common lwipopts
33+
)
34+
target_link_libraries(picow_freertos_http_client_sys
35+
pico_cyw43_arch_lwip_sys_freertos
36+
pico_stdlib
37+
pico_http_client
38+
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
39+
)
40+
pico_add_extra_outputs(picow_freertos_http_client_sys)
41+
42+
# Ignore warnings from lwip code
43+
set_source_files_properties(
44+
${PICO_LWIP_PATH}/src/apps/altcp_tls/altcp_tls_mbedtls.c
45+
PROPERTIES
46+
COMPILE_OPTIONS "-Wno-unused-result"
47+
)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* FreeRTOS V202111.00
3+
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*
22+
* http://www.FreeRTOS.org
23+
* http://aws.amazon.com/freertos
24+
*
25+
* 1 tab == 4 spaces!
26+
*/
27+
28+
#ifndef FREERTOS_CONFIG_H
29+
#define FREERTOS_CONFIG_H
30+
31+
/*-----------------------------------------------------------
32+
* Application specific definitions.
33+
*
34+
* These definitions should be adjusted for your particular hardware and
35+
* application requirements.
36+
*
37+
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
38+
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
39+
*
40+
* See http://www.freertos.org/a00110.html
41+
*----------------------------------------------------------*/
42+
43+
/* Scheduler Related */
44+
#define configUSE_PREEMPTION 1
45+
#define configUSE_TICKLESS_IDLE 0
46+
#define configUSE_IDLE_HOOK 0
47+
#define configUSE_TICK_HOOK 0
48+
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
49+
#define configMAX_PRIORITIES 32
50+
#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256
51+
#define configUSE_16_BIT_TICKS 0
52+
53+
#define configIDLE_SHOULD_YIELD 1
54+
55+
/* Synchronization Related */
56+
#define configUSE_MUTEXES 1
57+
#define configUSE_RECURSIVE_MUTEXES 1
58+
#define configUSE_APPLICATION_TASK_TAG 0
59+
#define configUSE_COUNTING_SEMAPHORES 1
60+
#define configQUEUE_REGISTRY_SIZE 8
61+
#define configUSE_QUEUE_SETS 1
62+
#define configUSE_TIME_SLICING 1
63+
#define configUSE_NEWLIB_REENTRANT 0
64+
// todo need this for lwip FreeRTOS sys_arch to compile
65+
#define configENABLE_BACKWARD_COMPATIBILITY 1
66+
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
67+
68+
/* System */
69+
#define configSTACK_DEPTH_TYPE uint32_t
70+
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
71+
72+
/* Memory allocation related definitions. */
73+
#define configSUPPORT_STATIC_ALLOCATION 0
74+
#define configSUPPORT_DYNAMIC_ALLOCATION 1
75+
#define configTOTAL_HEAP_SIZE (128*1024)
76+
#define configAPPLICATION_ALLOCATED_HEAP 0
77+
78+
/* Hook function related definitions. */
79+
#define configCHECK_FOR_STACK_OVERFLOW 0
80+
#define configUSE_MALLOC_FAILED_HOOK 0
81+
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
82+
83+
/* Run time and task stats gathering related definitions. */
84+
#define configGENERATE_RUN_TIME_STATS 0
85+
#define configUSE_TRACE_FACILITY 1
86+
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
87+
88+
/* Co-routine related definitions. */
89+
#define configUSE_CO_ROUTINES 0
90+
#define configMAX_CO_ROUTINE_PRIORITIES 1
91+
92+
/* Software timer related definitions. */
93+
#define configUSE_TIMERS 1
94+
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
95+
#define configTIMER_QUEUE_LENGTH 10
96+
#define configTIMER_TASK_STACK_DEPTH 1024
97+
98+
/* Interrupt nesting behaviour configuration. */
99+
/*
100+
#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor]
101+
#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application]
102+
#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application]
103+
*/
104+
105+
#if FREE_RTOS_KERNEL_SMP // set by the RP2040 SMP port of FreeRTOS
106+
/* SMP port only */
107+
#define configNUM_CORES 2
108+
#define configTICK_CORE 0
109+
#define configRUN_MULTIPLE_PRIORITIES 1
110+
#define configUSE_CORE_AFFINITY 1
111+
#endif
112+
113+
/* RP2040 specific */
114+
#define configSUPPORT_PICO_SYNC_INTEROP 1
115+
#define configSUPPORT_PICO_TIME_INTEROP 1
116+
117+
#include <assert.h>
118+
/* Define to trap errors during development. */
119+
#define configASSERT(x) assert(x)
120+
121+
/* Set the following definitions to 1 to include the API function, or zero
122+
to exclude the API function. */
123+
#define INCLUDE_vTaskPrioritySet 1
124+
#define INCLUDE_uxTaskPriorityGet 1
125+
#define INCLUDE_vTaskDelete 1
126+
#define INCLUDE_vTaskSuspend 1
127+
#define INCLUDE_vTaskDelayUntil 1
128+
#define INCLUDE_vTaskDelay 1
129+
#define INCLUDE_xTaskGetSchedulerState 1
130+
#define INCLUDE_xTaskGetCurrentTaskHandle 1
131+
#define INCLUDE_uxTaskGetStackHighWaterMark 1
132+
#define INCLUDE_xTaskGetIdleTaskHandle 1
133+
#define INCLUDE_eTaskGetState 1
134+
#define INCLUDE_xTimerPendFunctionCall 1
135+
#define INCLUDE_xTaskAbortDelay 1
136+
#define INCLUDE_xTaskGetHandle 1
137+
#define INCLUDE_xTaskResumeFromISR 1
138+
#define INCLUDE_xQueueGetMutexHolder 1
139+
140+
/* A header file that defines trace macro can be included here. */
141+
142+
#endif /* FREERTOS_CONFIG_H */
143+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef _LWIPOPTS_H
2+
#define _LWIPOPTS_H
3+
4+
// Generally you would define your own explicit list of lwIP options
5+
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html)
6+
//
7+
// This example uses a common include to avoid repetition
8+
#include "lwipopts_examples_common.h"
9+
10+
#if !NO_SYS
11+
#define TCPIP_THREAD_STACKSIZE 1024
12+
#define DEFAULT_THREAD_STACKSIZE 1024
13+
#define DEFAULT_RAW_RECVMBOX_SIZE 8
14+
#define TCPIP_MBOX_SIZE 8
15+
#define LWIP_TIMEVAL_PRIVATE 0
16+
17+
// not necessary, can be done either way
18+
#define LWIP_TCPIP_CORE_LOCKING_INPUT 1
19+
20+
// ping_thread sets socket receive timeout, so enable this feature
21+
#define LWIP_SO_RCVTIMEO 1
22+
#endif
23+
24+
#define LWIP_ALTCP 1
25+
26+
// If you don't want to use TLS (just a http request) you can avoid linking to mbedtls and remove the following
27+
#define LWIP_ALTCP_TLS 1
28+
#define LWIP_ALTCP_TLS_MBEDTLS 1
29+
30+
// Note bug in lwip with LWIP_ALTCP and LWIP_DEBUG
31+
// https://savannah.nongnu.org/bugs/index.php?62159
32+
//#define LWIP_DEBUG 1
33+
#undef LWIP_DEBUG
34+
#define ALTCP_MBEDTLS_DEBUG LWIP_DBG_ON
35+
36+
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* Workaround for some mbedtls source files using INT_MAX without including limits.h */
2+
#include <limits.h>
3+
4+
#define MBEDTLS_NO_PLATFORM_ENTROPY
5+
#define MBEDTLS_ENTROPY_HARDWARE_ALT
6+
7+
#define MBEDTLS_SSL_OUT_CONTENT_LEN 2048
8+
9+
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
10+
#define MBEDTLS_HAVE_TIME
11+
12+
#define MBEDTLS_CIPHER_MODE_CBC
13+
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
14+
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
15+
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
16+
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
17+
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
18+
#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
19+
#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
20+
#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
21+
#define MBEDTLS_ECP_DP_BP256R1_ENABLED
22+
#define MBEDTLS_ECP_DP_BP384R1_ENABLED
23+
#define MBEDTLS_ECP_DP_BP512R1_ENABLED
24+
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
25+
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
26+
#define MBEDTLS_PKCS1_V15
27+
#define MBEDTLS_SHA256_SMALLER
28+
#define MBEDTLS_SSL_SERVER_NAME_INDICATION
29+
#define MBEDTLS_AES_C
30+
#define MBEDTLS_ASN1_PARSE_C
31+
#define MBEDTLS_BIGNUM_C
32+
#define MBEDTLS_CIPHER_C
33+
#define MBEDTLS_CTR_DRBG_C
34+
#define MBEDTLS_ENTROPY_C
35+
#define MBEDTLS_ERROR_C
36+
#define MBEDTLS_MD_C
37+
#define MBEDTLS_MD5_C
38+
#define MBEDTLS_OID_C
39+
#define MBEDTLS_PKCS5_C
40+
#define MBEDTLS_PK_C
41+
#define MBEDTLS_PK_PARSE_C
42+
#define MBEDTLS_PLATFORM_C
43+
#define MBEDTLS_RSA_C
44+
#define MBEDTLS_SHA1_C
45+
#define MBEDTLS_SHA224_C
46+
#define MBEDTLS_SHA256_C
47+
#define MBEDTLS_SHA512_C
48+
#define MBEDTLS_SSL_CLI_C
49+
#define MBEDTLS_SSL_SRV_C
50+
#define MBEDTLS_SSL_TLS_C
51+
#define MBEDTLS_X509_CRT_PARSE_C
52+
#define MBEDTLS_X509_USE_C
53+
#define MBEDTLS_AES_FEWER_TABLES
54+
55+
/* TLS 1.2 */
56+
#define MBEDTLS_SSL_PROTO_TLS1_2
57+
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
58+
#define MBEDTLS_GCM_C
59+
#define MBEDTLS_ECDH_C
60+
#define MBEDTLS_ECP_C
61+
#define MBEDTLS_ECDSA_C
62+
#define MBEDTLS_ASN1_WRITE_C
63+
64+
// The following is needed to parse a certificate
65+
#define MBEDTLS_PEM_PARSE_C
66+
#define MBEDTLS_BASE64_C

0 commit comments

Comments
 (0)