example: fix CI error of coap client demo
This commit is contained in:
parent
f5b221b9fb
commit
6ca2934843
3 changed files with 106 additions and 133 deletions
|
@ -1,24 +1,11 @@
|
||||||
menu "Example Configuration"
|
menu "Example Configuration"
|
||||||
|
|
||||||
config TARGET_DOMAIN
|
|
||||||
string "Target Domain"
|
|
||||||
default "californium.eclipse.org"
|
|
||||||
help
|
|
||||||
Target domain for the example to connect to.
|
|
||||||
|
|
||||||
config TARGET_DOMAIN_URI
|
config TARGET_DOMAIN_URI
|
||||||
string "Target Uri"
|
string "Target Uri"
|
||||||
default "coap://californium.eclipse.org"
|
default "coap://californium.eclipse.org"
|
||||||
help
|
help
|
||||||
Target uri for the example to use.
|
Target uri for the example to use.
|
||||||
|
|
||||||
config TARGET_PORT_NUMBER
|
|
||||||
int "Target port number"
|
|
||||||
range 0 65535
|
|
||||||
default 5683
|
|
||||||
help
|
|
||||||
Target port number for the example to connect to.
|
|
||||||
|
|
||||||
config WIFI_SSID
|
config WIFI_SSID
|
||||||
string "WiFi SSID"
|
string "WiFi SSID"
|
||||||
default "myssid"
|
default "myssid"
|
||||||
|
|
|
@ -6,9 +6,10 @@
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
*/
|
*/
|
||||||
#include "coap_client.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
@ -19,12 +20,28 @@
|
||||||
#include "esp_event_loop.h"
|
#include "esp_event_loop.h"
|
||||||
|
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "coap_config.h"
|
|
||||||
#include "resource.h"
|
|
||||||
#include "coap.h"
|
#include "coap.h"
|
||||||
|
|
||||||
|
/* The examples use simple WiFi configuration that you can set via
|
||||||
|
'make menuconfig'.
|
||||||
|
|
||||||
|
If you'd rather not, just change the below entries to strings with
|
||||||
|
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
||||||
|
*/
|
||||||
|
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||||
|
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||||
|
|
||||||
|
#define COAP_DEFAULT_TIME_SEC 5
|
||||||
|
#define COAP_DEFAULT_TIME_USEC 0
|
||||||
|
|
||||||
|
/* The examples use uri "coap://californium.eclipse.org" that
|
||||||
|
you can set via 'make menuconfig'.
|
||||||
|
|
||||||
|
If you'd rather not, just change the below entries to strings with
|
||||||
|
the config you want - ie #define COAP_DEFAULT_DEMO_URI "coap://californium.eclipse.org"
|
||||||
|
*/
|
||||||
|
#define COAP_DEFAULT_DEMO_URI CONFIG_TARGET_DOMAIN_URI
|
||||||
|
|
||||||
static EventGroupHandle_t wifi_event_group;
|
static EventGroupHandle_t wifi_event_group;
|
||||||
|
|
||||||
|
@ -33,7 +50,7 @@ static EventGroupHandle_t wifi_event_group;
|
||||||
to the AP with an IP? */
|
to the AP with an IP? */
|
||||||
const static int CONNECTED_BIT = BIT0;
|
const static int CONNECTED_BIT = BIT0;
|
||||||
|
|
||||||
const static char *TAG = "CoAP_demo";
|
const static char *TAG = "CoAP_client";
|
||||||
|
|
||||||
static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *local_interface, const coap_address_t *remote,
|
static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *local_interface, const coap_address_t *remote,
|
||||||
coap_pdu_t *sent, coap_pdu_t *received,
|
coap_pdu_t *sent, coap_pdu_t *received,
|
||||||
|
@ -50,6 +67,9 @@ static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *l
|
||||||
|
|
||||||
static void coap_demo_thread(void *p)
|
static void coap_demo_thread(void *p)
|
||||||
{
|
{
|
||||||
|
struct hostent *hp;
|
||||||
|
struct ip4_addr *ip4_addr;
|
||||||
|
|
||||||
coap_context_t* ctx = NULL;
|
coap_context_t* ctx = NULL;
|
||||||
coap_address_t dst_addr, src_addr;
|
coap_address_t dst_addr, src_addr;
|
||||||
static coap_uri_t uri;
|
static coap_uri_t uri;
|
||||||
|
@ -60,6 +80,33 @@ static void coap_demo_thread(void *p)
|
||||||
const char* server_uri = COAP_DEFAULT_DEMO_URI;
|
const char* server_uri = COAP_DEFAULT_DEMO_URI;
|
||||||
uint8_t get_method = 1;
|
uint8_t get_method = 1;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
/* Wait for the callback to set the CONNECTED_BIT in the
|
||||||
|
event group.
|
||||||
|
*/
|
||||||
|
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||||
|
false, true, portMAX_DELAY);
|
||||||
|
ESP_LOGI(TAG, "Connected to AP");
|
||||||
|
|
||||||
|
if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) {
|
||||||
|
ESP_LOGE(TAG, "CoAP server uri error");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
hp = gethostbyname((const char *)uri.host.s);
|
||||||
|
|
||||||
|
if (hp == NULL) {
|
||||||
|
ESP_LOGE(TAG, "DNS lookup failed");
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Code to print the resolved IP.
|
||||||
|
|
||||||
|
Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
|
||||||
|
ip4_addr = (struct ip4_addr *)hp->h_addr;
|
||||||
|
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*ip4_addr));
|
||||||
|
|
||||||
coap_address_init(&src_addr);
|
coap_address_init(&src_addr);
|
||||||
src_addr.addr.sin.sin_family = AF_INET;
|
src_addr.addr.sin.sin_family = AF_INET;
|
||||||
src_addr.addr.sin.sin_port = htons(0);
|
src_addr.addr.sin.sin_port = htons(0);
|
||||||
|
@ -70,9 +117,8 @@ static void coap_demo_thread(void *p)
|
||||||
coap_address_init(&dst_addr);
|
coap_address_init(&dst_addr);
|
||||||
dst_addr.addr.sin.sin_family = AF_INET;
|
dst_addr.addr.sin.sin_family = AF_INET;
|
||||||
dst_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
dst_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
||||||
dst_addr.addr.sin.sin_addr.s_addr = inet_addr(COAP_DEFAULT_DEMO_ADDR);
|
dst_addr.addr.sin.sin_addr.s_addr = ip4_addr->addr;
|
||||||
|
|
||||||
coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri);
|
|
||||||
request = coap_new_pdu();
|
request = coap_new_pdu();
|
||||||
if (request){
|
if (request){
|
||||||
request->hdr->type = COAP_MESSAGE_CON;
|
request->hdr->type = COAP_MESSAGE_CON;
|
||||||
|
@ -100,33 +146,17 @@ static void coap_demo_thread(void *p)
|
||||||
} else if (result < 0) {
|
} else if (result < 0) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
printf("select timeout\n");
|
ESP_LOGE(TAG, "select timeout");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
coap_free_context(ctx);
|
coap_free_context(ctx);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void coap_server_init(void)
|
|
||||||
{
|
|
||||||
int ret = pdPASS;
|
|
||||||
xTaskHandle coap_handle = NULL;
|
|
||||||
|
|
||||||
ret = xTaskCreate(coap_demo_thread,
|
|
||||||
COAP_DEMO_THREAD_NAME,
|
|
||||||
COAP_DEMO_THREAD_STACK_WORDS,
|
|
||||||
NULL,
|
|
||||||
COAP_DEMO_THREAD_PRORIOTY,
|
|
||||||
&coap_handle);
|
|
||||||
|
|
||||||
if (ret != pdPASS) {
|
|
||||||
ESP_LOGI(TAG, "create thread %s failed", COAP_DEMO_THREAD_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
||||||
{
|
{
|
||||||
switch(event->event_id) {
|
switch(event->event_id) {
|
||||||
|
@ -135,7 +165,6 @@ static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||||
coap_server_init();
|
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||||
|
@ -165,7 +194,6 @@ static void wifi_conn_init(void)
|
||||||
};
|
};
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||||
|
|
||||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,4 +201,5 @@ void app_main(void)
|
||||||
{
|
{
|
||||||
nvs_flash_init();
|
nvs_flash_init();
|
||||||
wifi_conn_init();
|
wifi_conn_init();
|
||||||
|
xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
/* CoAP client Example
|
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _COAP_CLIENT_H_
|
|
||||||
#define _COAP_CLIENT_H_
|
|
||||||
|
|
||||||
#include <sdkconfig.h>
|
|
||||||
|
|
||||||
/* The examples use simple WiFi configuration that you can set via
|
|
||||||
'make menuconfig'.
|
|
||||||
|
|
||||||
If you'd rather not, just change the below entries to strings with
|
|
||||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
||||||
*/
|
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
#define COAP_DEMO_THREAD_NAME "CoAP_demo"
|
|
||||||
#define COAP_DEMO_THREAD_STACK_WORDS 10240
|
|
||||||
#define COAP_DEMO_THREAD_PRORIOTY 8
|
|
||||||
|
|
||||||
#define COAP_DEFAULT_TIME_SEC 5
|
|
||||||
#define COAP_DEFAULT_TIME_USEC 0
|
|
||||||
|
|
||||||
/* The examples use domain of "californium.eclipse.org",uri "coap://californium.eclipse.org" and port number of 5683 that
|
|
||||||
you can set via 'make menuconfig'.
|
|
||||||
|
|
||||||
If you'd rather not, just change the below entries to strings with
|
|
||||||
the config you want - ie #define COAP_DEFAULT_DEMO_ADDR "californium.eclipse.org"
|
|
||||||
, ie #define COAP_DEFAULT_DEMO_URI "coap://californium.eclipse.org" and ie #define COAP_DEFAULT_PORT 5683
|
|
||||||
*/
|
|
||||||
#define COAP_DEFAULT_PORT CONFIG_TARGET_PORT_NUMBER
|
|
||||||
#define COAP_DEFAULT_DEMO_ADDR CONFIG_TARGET_DOMAIN
|
|
||||||
#define COAP_DEFAULT_DEMO_URI CONFIG_TARGET_DOMAIN_URI
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
Loading…
Reference in a new issue