diff --git a/examples/coap_server/Makefile b/examples/coap_server/Makefile new file mode 100644 index 00000000..0051878b --- /dev/null +++ b/examples/coap_server/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 -DMQTT_TASK + +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_server/gen_misc.sh b/examples/coap_server/gen_misc.sh new file mode 100755 index 00000000..ced20a09 --- /dev/null +++ b/examples/coap_server/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 */ + +#endif + diff --git a/examples/coap_server/user/Makefile b/examples/coap_server/user/Makefile new file mode 100644 index 00000000..244e135d --- /dev/null +++ b/examples/coap_server/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_server/user/coap_server_example_main.c b/examples/coap_server/user/coap_server_example_main.c old mode 100644 new mode 100755 index d6b87cbe..65e47fd4 --- a/examples/coap_server/user/coap_server_example_main.c +++ b/examples/coap_server/user/coap_server_example_main.c @@ -12,37 +12,29 @@ #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" -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. +// for libcirom.a +int _getpid_r(struct _reent *r) +{ + return -1; + } +int _kill_r(struct _reent *r, int pid, int sig) +{ + return -1; + } - 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_SERVER_THREAD_NAME "coap_server_thread" +#define COAP_SERVER_THREAD_STACK_WORDS 2048 +#define COAP_SERVER_THREAD_PRIO 8 + +LOCAL xTaskHandle coap_server_handle; #define COAP_DEFAULT_TIME_SEC 5 #define COAP_DEFAULT_TIME_USEC 0 -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_server"; - static coap_async_state_t *async = NULL; static void @@ -50,7 +42,7 @@ 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 World WTF!"; + 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->hdr->id = coap_new_message_id(ctx); @@ -90,13 +82,6 @@ static void coap_example_thread(void *p) int flags = 0; 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"); - /* Prepare the CoAP server socket */ coap_address_init(&serv_addr); serv_addr.addr.sin.sin_family = AF_INET; @@ -127,7 +112,7 @@ static void coap_example_thread(void *p) } else if (result < 0){ break; } else { - ESP_LOGE(TAG, "select timeout"); + printf("select timeout\n"); } if (async) { @@ -143,50 +128,17 @@ static void coap_example_thread(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_example_thread, + COAP_SERVER_THREAD_NAME, + COAP_SERVER_THREAD_STACK_WORDS, + NULL, + COAP_SERVER_THREAD_PRIO, + &coap_server_handle); + + if (ret != pdPASS) { + printf("create coap server thread %s failed\n", COAP_SERVER_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_thread, "coap", 2048, NULL, 5, NULL); } diff --git a/examples/coap_server/user/user_main.c b/examples/coap_server/user/user_main.c new file mode 100644 index 00000000..3481bbc9 --- /dev/null +++ b/examples/coap_server/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/lib/libcoap.a b/lib/libcoap.a index 027a49cc..a17da47d 100644 Binary files a/lib/libcoap.a and b/lib/libcoap.a differ diff --git a/third_party/coap/library/Makefile b/third_party/coap/library/Makefile index 10f4067c..3110901d 100644 --- a/third_party/coap/library/Makefile +++ b/third_party/coap/library/Makefile @@ -26,6 +26,7 @@ endif # for a subtree within the makefile rooted therein # #DEFINES += +DEFINES += -DWITH_POSIX ############################################################# # Recursion Magic - Don't touch this!! diff --git a/third_party/coap/library/address.c b/third_party/coap/library/address.c index bd72069b..15858537 100644 --- a/third_party/coap/library/address.c +++ b/third_party/coap/library/address.c @@ -12,6 +12,9 @@ #include "address.h" +#ifndef IN6_IS_ADDR_MULTICAST +#define IN6_IS_ADDR_MULTICAST(a) IN_MULTICAST(a) +#endif int coap_address_equals(const coap_address_t *a, const coap_address_t *b) { assert(a); assert(b); diff --git a/third_party/coap/library/coap_time.c b/third_party/coap/library/coap_time.c index 555a86e5..03bc1112 100644 --- a/third_party/coap/library/coap_time.c +++ b/third_party/coap/library/coap_time.c @@ -19,7 +19,7 @@ static coap_time_t coap_clock_offset = 0; /* _POSIX_TIMERS is > 0 when clock_gettime() is available */ /* Use real-time clock for correct timestamps in coap_log(). */ -#define COAP_CLOCK CLOCK_REALTIME +//#define COAP_CLOCK CLOCK_REALTIME #endif void diff --git a/third_party/coap/library/debug.c b/third_party/coap/library/debug.c index d60adb47..da7b8977 100644 --- a/third_party/coap/library/debug.c +++ b/third_party/coap/library/debug.c @@ -154,6 +154,10 @@ print_readable( const unsigned char *data, unsigned int len, #define min(a,b) ((a) < (b) ? (a) : (b)) #endif +#ifndef inet_ntop +#define inet_ntop(af,src,dst,size) (((af) == AF_INET) ? ipaddr_ntoa_r((const ip_addr_t *)(src),(dst),(size)) : NULL) +#endif + size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len) { #ifdef HAVE_ARPA_INET_H @@ -163,7 +167,7 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le switch (addr->addr.sa.sa_family) { case AF_INET: - addrptr = &addr->addr.sin.sin_addr; + addrptr = &addr->addr.sin.sin_addr.s_addr; port = ntohs(addr->addr.sin.sin_port); break; case AF_INET6: diff --git a/third_party/coap/platform/Makefile b/third_party/coap/platform/Makefile index 749b4787..e6dbca29 100644 --- a/third_party/coap/platform/Makefile +++ b/third_party/coap/platform/Makefile @@ -26,6 +26,7 @@ endif # for a subtree within the makefile rooted therein # #DEFINES += +DEFINES += -DWITH_POSIX ############################################################# # Recursion Magic - Don't touch this!!