diff --git a/examples/coap_client/Makefile b/examples/coap_client/Makefile new file mode 100644 index 00000000..56be5d96 --- /dev/null +++ b/examples/coap_client/Makefile @@ -0,0 +1,126 @@ +############################################################# +# Required variables for each makefile +# Discard this section from all parent makefiles +# Expected variables (with automatic defaults): +# CSRCS (all "C" files in the dir) +# SUBDIRS (all subdirs with a Makefile) +# GEN_LIBS - list of libs to be generated () +# GEN_IMAGES - list of object file images to be generated () +# GEN_BINS - list of binaries to be generated () +# COMPONENTS_xxx - a list of libs/objs in the form +# subdir/lib to be extracted and rolled up into +# a generated lib/image xxx.a () +# +TARGET = eagle +#FLAVOR = release +FLAVOR = debug + +#EXTRA_CCFLAGS += -u + +ifndef PDIR # { +GEN_IMAGES= eagle.app.v6.out +GEN_BINS= eagle.app.v6.bin +SPECIAL_MKTARGETS=$(APP_MKTARGETS) +SUBDIRS= \ + user + +endif # } PDIR + +LDDIR = $(SDK_PATH)/ld + +CCFLAGS += -Os + +TARGET_LDFLAGS = \ + -nostdlib \ + -Wl,-EL \ + --longcalls \ + --text-section-literals + +ifeq ($(FLAVOR),debug) + TARGET_LDFLAGS += -g -O2 +endif + +ifeq ($(FLAVOR),release) + TARGET_LDFLAGS += -g -O0 +endif + +COMPONENTS_eagle.app.v6 = \ + user/libuser.a + +LINKFLAGS_eagle.app.v6 = \ + -L$(SDK_PATH)/lib \ + -Wl,--gc-sections \ + -nostdlib \ + -T$(LD_FILE) \ + -Wl,--no-check-sections \ + -u call_user_start \ + -Wl,-static \ + -Wl,--start-group \ + -lcirom \ + -lgcc \ + -lhal \ + -lcrypto \ + -lfreertos \ + -llwip \ + -lmain \ + -lnet80211 \ + -lphy \ + -lpp \ + -lmbedtls \ + -lopenssl \ + -lcoap \ + -lwpa \ + $(DEP_LIBS_eagle.app.v6)\ + -Wl,--end-group + +DEPENDS_eagle.app.v6 = \ + $(LD_FILE) \ + $(LDDIR)/eagle.rom.addr.v6.ld + +############################################################# +# Configuration i.e. compile options etc. +# Target specific stuff (defines etc.) goes in here! +# Generally values applying to a tree are captured in the +# makefile at its root level - these are then overridden +# for a subtree within the makefile rooted therein +# + +#UNIVERSAL_TARGET_DEFINES = \ + +# Other potential configuration flags include: +# -DTXRX_TXBUF_DEBUG +# -DTXRX_RXBUF_DEBUG +# -DWLAN_CONFIG_CCX +# -DMQTT_TASK: Define MQTT_TASK to enable MQTT start background +# thread for a client. +CONFIGURATION_DEFINES = -DICACHE_FLASH + +DEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ + $(CONFIGURATION_DEFINES) + +DDEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ + $(CONFIGURATION_DEFINES) + + +############################################################# +# Recursion Magic - Don't touch this!! +# +# Each subtree potentially has an include directory +# corresponding to the common APIs applicable to modules +# rooted at that subtree. Accordingly, the INCLUDE PATH +# of a module can only contain the include directories up +# its parent path, and not its siblings +# +# Required for each makefile to inherit from the parent +# +INCLUDES := $(INCLUDES) -I $(PDIR)include +INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include +INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include/coap +INCLUDES += -I $(SDK_PATH)/third_party/coap/library/include/coap +sinclude $(SDK_PATH)/Makefile + +.PHONY: FORCE +FORCE: + diff --git a/examples/coap_client/gen_misc.sh b/examples/coap_client/gen_misc.sh new file mode 100755 index 00000000..ced20a09 --- /dev/null +++ b/examples/coap_client/gen_misc.sh @@ -0,0 +1,192 @@ +#!/bin/bash + +:< + * + * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef __USER_CONFIG_H__ +#define __USER_CONFIG_H__ + +#define SSID "BL_841R" /* Wi-Fi SSID */ +#define PASSWORD "1234567890" /* Wi-Fi Password */ + +#define COAP_SERVER "192.168.111.106" // domain name or IP address +#define COAP_DEFAULT_DEMO_URI "coap://192.168.111.106/Espressif" // point at a server resource + +#endif diff --git a/examples/coap_client/user/Makefile b/examples/coap_client/user/Makefile new file mode 100644 index 00000000..244e135d --- /dev/null +++ b/examples/coap_client/user/Makefile @@ -0,0 +1,47 @@ + +############################################################# +# Required variables for each makefile +# Discard this section from all parent makefiles +# Expected variables (with automatic defaults): +# CSRCS (all "C" files in the dir) +# SUBDIRS (all subdirs with a Makefile) +# GEN_LIBS - list of libs to be generated () +# GEN_IMAGES - list of images to be generated () +# COMPONENTS_xxx - a list of libs/objs in the form +# subdir/lib to be extracted and rolled up into +# a generated lib/image xxx.a () +# +ifndef PDIR +GEN_LIBS = libuser.a +endif + + +############################################################# +# Configuration i.e. compile options etc. +# Target specific stuff (defines etc.) goes in here! +# Generally values applying to a tree are captured in the +# makefile at its root level - these are then overridden +# for a subtree within the makefile rooted therein +# +#DEFINES += +DEFINES += -DWITH_POSIX +############################################################# +# Recursion Magic - Don't touch this!! +# +# Each subtree potentially has an include directory +# corresponding to the common APIs applicable to modules +# rooted at that subtree. Accordingly, the INCLUDE PATH +# of a module can only contain the include directories up +# its parent path, and not its siblings +# +# Required for each makefile to inherit from the parent +# + +INCLUDES := $(INCLUDES) -I $(PDIR)include +INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include +INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include/coap +INCLUDES += -I $(SDK_PATH)/third_party/coap/library/include/coap +INCLUDES += -I ./ +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile + diff --git a/examples/coap_client/user/coap_client_example_main.c b/examples/coap_client/user/coap_client_example_main.c old mode 100644 new mode 100755 index b72836e7..53eb658f --- a/examples/coap_client/user/coap_client_example_main.c +++ b/examples/coap_client/user/coap_client_example_main.c @@ -13,44 +13,30 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_log.h" #include "esp_wifi.h" -#include "esp_event_loop.h" - -#include "nvs_flash.h" - #include "coap.h" +#include "user_config.h" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. +#define COAP_CLIENT_THREAD_NAME "coap_client_thread" +#define COAP_CLIENT_THREAD_STACK_WORDS 2048 +#define COAP_CLIENT_THREAD_PRIO 8 - 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 +static xTaskHandle coap_client_handle; #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'. +// for libcirom.a +int _getpid_r(struct _reent *r) +{ + return -1; + } - 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; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const static int CONNECTED_BIT = BIT0; - -const static char *TAG = "CoAP_client"; +int _kill_r(struct _reent *r, int pid, int sig) +{ + return -1; + } 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, @@ -65,11 +51,10 @@ static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *l } } -static void coap_example_task(void *p) +static void coap_client_example_thread(void *p) { - struct hostent *hp; - struct ip4_addr *ip4_addr; - + ip_addr_t target_ip; + int ret = 0; coap_context_t* ctx = NULL; coap_address_t dst_addr, src_addr; static coap_uri_t uri; @@ -81,33 +66,20 @@ static void coap_example_task(void *p) 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"); + printf("CoAP server uri error\n"); break; } - printf("host:%s path:%s\n", (const char *)uri.host.s, (const char *)uri.path.s); - hp = gethostbyname((const char *)uri.host.s); + ret = netconn_gethostbyname(COAP_SERVER, &target_ip); - if (hp == NULL) { - ESP_LOGE(TAG, "DNS lookup failed"); - vTaskDelay(1000 / portTICK_PERIOD_MS); + if (ret != 0) { + printf("DNS lookup failed\n"); + vTaskDelay(1000 / portTICK_RATE_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); src_addr.addr.sin.sin_family = AF_INET; src_addr.addr.sin.sin_port = htons(0); @@ -118,7 +90,7 @@ static void coap_example_task(void *p) coap_address_init(&dst_addr); dst_addr.addr.sin.sin_family = AF_INET; dst_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT); - dst_addr.addr.sin.sin_addr.s_addr = ip4_addr->addr; + dst_addr.addr.sin.sin_addr.s_addr = target_ip.addr; request = coap_new_pdu(); if (request){ @@ -147,7 +119,7 @@ static void coap_example_task(void *p) } else if (result < 0) { break; } else { - ESP_LOGE(TAG, "select timeout"); + printf("select timeout\n"); } } } @@ -158,49 +130,17 @@ static void coap_example_task(void *p) vTaskDelete(NULL); } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +void user_conn_init(void) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; + int ret; + ret = xTaskCreate(coap_client_example_thread, + COAP_CLIENT_THREAD_NAME, + COAP_CLIENT_THREAD_STACK_WORDS, + NULL, + COAP_CLIENT_THREAD_PRIO, + &coap_client_handle); + + if (ret != pdPASS) { + printf("create coap client thread %s failed\n", COAP_CLIENT_THREAD_NAME); } - return ESP_OK; -} - -static void wifi_conn_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) ); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_WIFI_PASS, - }, - }; - 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_start() ); -} - -void app_main(void) -{ - ESP_ERROR_CHECK( nvs_flash_init() ); - wifi_conn_init(); - xTaskCreate(coap_example_task, "coap", 2048, NULL, 5, NULL); } diff --git a/examples/coap_client/user/user_main.c b/examples/coap_client/user/user_main.c new file mode 100644 index 00000000..3481bbc9 --- /dev/null +++ b/examples/coap_client/user/user_main.c @@ -0,0 +1,122 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2015 + * + * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include "esp_common.h" +#include "user_config.h" + +/****************************************************************************** + * FunctionName : user_rf_cal_sector_set + * Description : SDK just reversed 4 sectors, used for rf init data and paramters. + * We add this function to force users to set rf cal sector, since + * we don't know which sector is free in user's application. + * sector map for last several sectors : ABCCC + * A : rf cal + * B : rf init data + * C : sdk parameters + * Parameters : none + * Returns : rf cal sector +*******************************************************************************/ +uint32 user_rf_cal_sector_set(void) +{ + flash_size_map size_map = system_get_flash_size_map(); + uint32 rf_cal_sec = 0; + + switch (size_map) { + case FLASH_SIZE_4M_MAP_256_256: + rf_cal_sec = 128 - 5; + break; + + case FLASH_SIZE_8M_MAP_512_512: + rf_cal_sec = 256 - 5; + break; + + case FLASH_SIZE_16M_MAP_512_512: + case FLASH_SIZE_16M_MAP_1024_1024: + rf_cal_sec = 512 - 5; + break; + + case FLASH_SIZE_32M_MAP_512_512: + case FLASH_SIZE_32M_MAP_1024_1024: + rf_cal_sec = 1024 - 5; + break; + case FLASH_SIZE_64M_MAP_1024_1024: + rf_cal_sec = 2048 - 5; + break; + case FLASH_SIZE_128M_MAP_1024_1024: + rf_cal_sec = 4096 - 5; + break; + default: + rf_cal_sec = 0; + break; + } + + return rf_cal_sec; +} + +void wifi_event_handler_cb(System_Event_t *event) +{ + if (event == NULL) { + return; + } + + switch (event->event_id) { + case EVENT_STAMODE_GOT_IP: + os_printf("sta got ip ,create task and free heap size is %d\n", system_get_free_heap_size()); + user_conn_init(); + break; + + case EVENT_STAMODE_CONNECTED: + os_printf("sta connected\n"); + break; + + case EVENT_STAMODE_DISCONNECTED: + wifi_station_connect(); + break; + + default: + break; + } +} + +/****************************************************************************** + * FunctionName : user_init + * Description : entry of user application, init user function here + * Parameters : none + * Returns : none +*******************************************************************************/ +void user_init(void) +{ + os_printf("SDK version:%s %d\n", system_get_sdk_version(), system_get_free_heap_size()); + wifi_set_opmode(STATION_MODE); + + struct station_config config; + bzero(&config, sizeof(struct station_config)); + sprintf(config.ssid, SSID); + sprintf(config.password, PASSWORD); + wifi_station_set_config(&config); + + wifi_set_event_handler_cb(wifi_event_handler_cb); + + wifi_station_connect(); +} diff --git a/examples/coap_server/Makefile b/examples/coap_server/Makefile index 0051878b..56be5d96 100644 --- a/examples/coap_server/Makefile +++ b/examples/coap_server/Makefile @@ -93,7 +93,7 @@ DEPENDS_eagle.app.v6 = \ # -DWLAN_CONFIG_CCX # -DMQTT_TASK: Define MQTT_TASK to enable MQTT start background # thread for a client. -CONFIGURATION_DEFINES = -DICACHE_FLASH -DMQTT_TASK +CONFIGURATION_DEFINES = -DICACHE_FLASH DEFINES += \ $(UNIVERSAL_TARGET_DEFINES) \ diff --git a/examples/coap_server/user/coap_server_example_main.c b/examples/coap_server/user/coap_server_example_main.c index 65e47fd4..29299d8c 100755 --- a/examples/coap_server/user/coap_server_example_main.c +++ b/examples/coap_server/user/coap_server_example_main.c @@ -16,35 +16,35 @@ #include "esp_wifi.h" #include "coap.h" -// for libcirom.a -int _getpid_r(struct _reent *r) -{ - return -1; - } -int _kill_r(struct _reent *r, int pid, int sig) -{ - return -1; - } - #define COAP_SERVER_THREAD_NAME "coap_server_thread" #define COAP_SERVER_THREAD_STACK_WORDS 2048 #define COAP_SERVER_THREAD_PRIO 8 -LOCAL xTaskHandle coap_server_handle; +static xTaskHandle coap_server_handle; #define COAP_DEFAULT_TIME_SEC 5 #define COAP_DEFAULT_TIME_USEC 0 static coap_async_state_t *async = NULL; +// for libcirom.a +int _getpid_r(struct _reent *r) +{ + return -1; + } + +int _kill_r(struct _reent *r, int pid, int sig) +{ + return -1; + } + static void send_async_response(coap_context_t *ctx, const coap_endpoint_t *local_if) { coap_pdu_t *response; unsigned char buf[3]; const char* response_data = "Hello esp8266!"; - size_t size = sizeof(coap_hdr_t) + 20; - response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size); + response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, COAP_MAX_PDU_SIZE); response->hdr->id = coap_new_message_id(ctx); if (async->tokenlen) coap_add_token(response, async->tokenlen, async->token); @@ -72,7 +72,7 @@ async_handler(coap_context_t *ctx, struct coap_resource_t *resource, async = coap_register_async(ctx, peer, request, COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM, (void*)"no data"); } -static void coap_example_thread(void *p) +static void coap_server_example_thread(void *p) { coap_context_t* ctx = NULL; coap_address_t serv_addr; @@ -131,7 +131,7 @@ static void coap_example_thread(void *p) void user_conn_init(void) { int ret; - ret = xTaskCreate(coap_example_thread, + ret = xTaskCreate(coap_server_example_thread, COAP_SERVER_THREAD_NAME, COAP_SERVER_THREAD_STACK_WORDS, NULL,