examples/protocols/asio: use common network component

* Original commit: espressif/esp-idf@aa4a7804ec
This commit is contained in:
Ivan Grokhotkov
2018-11-21 00:44:31 +08:00
committed by gabsuren
parent de830e51d4
commit 3e7591e92a
36 changed files with 161 additions and 850 deletions

View File

@@ -1,28 +1,16 @@
menu "Example Configuration"
config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
config EXAMPLE_PORT
string "asio example port number"
string "Asio example server port number"
default "2222"
help
Port number used by ASIO example
Port number used by Asio example.
config EXAMPLE_SERVER_IP
string "asio example server ip"
string "Asio example server ip"
default "FROM_STDIN"
help
asio example server ip for this client to connect to (leave default=FROM_STDIN to enter the server address
via serial terminal).
Asio example server ip for this client to connect to.
Leave default "FROM_STDIN" to enter the server address via serial terminal.
endmenu

View File

@@ -14,6 +14,10 @@
#include <thread>
#include "asio.hpp"
#include "chat_message.hpp"
#include "protocol_examples_common.h"
#include "esp_event.h"
#include "tcpip_adapter.h"
#include "nvs_flash.h"
using asio::ip::tcp;
@@ -130,8 +134,21 @@ private:
void read_line(char * line, int max_chars);
void asio_main()
extern "C" void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
/* This helper function configures blocking UART I/O */
ESP_ERROR_CHECK(example_configure_stdin_stdout());
std::string name(CONFIG_EXAMPLE_SERVER_IP);
std::string port(CONFIG_EXAMPLE_PORT);
char line[chat_message::max_body_length + 1] = { 0 };
@@ -152,7 +169,7 @@ void asio_main()
std::thread t([&io_context](){ io_context.run(); });
while (std::cin.getline(line, chat_message::max_body_length + 1) && std::string(line) != "exit\n") {
while (std::cin.getline(line, chat_message::max_body_length + 1) && std::string(line) != "exit") {
chat_message msg;
msg.body_length(std::strlen(line));
std::memcpy(msg.body(), line, msg.body_length());
@@ -162,4 +179,6 @@ void asio_main()
c.close();
t.join();
ESP_ERROR_CHECK(example_disconnect());
}