Skip to content

Commit

Permalink
#63 - WiFi mocked. Sntp mocked.
Browse files Browse the repository at this point in the history
  • Loading branch information
PerMalmberg committed Aug 18, 2019
1 parent 1537e8b commit 329bdf5
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 46 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)

# Select the test project to build
set(selected_test_project hw_jsonfile_test)
set(selected_test_project sntp)

# For Linux builds, you may enable address sanitizer
set(SMOOTH_ENABLE_ASAN 0)
Expand All @@ -41,24 +41,24 @@ list(APPEND available_tests
http_server_test
destructing_event_queues
destructing_subscribing_event_queues
security
sntp
hw_interrupt_queue
hw_sdcard_test
hw_spiflash
hw_jsonfile_test)
hw_jsonfile_test
)

# These tests needs actual hardware to run
if(${ESP_PLATFORM})
list(APPEND available_tests

hw_sntp
hw_security
hw_wrover_kit_blinky
)
endif()

list(FIND available_tests ${selected_test_project} test_found)
if(${test_found} EQUAL -1)
message(FATAL_ERROR "'${selected_test_project}' isn't a valid test project for the current platform.")
message(FATAL_ERROR "'${selected_test_project}' not found or not a valid test project for the current platform.")
endif()

if(${ESP_PLATFORM})
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ target_include_directories(${PROJECT_NAME}
${SMOOTH_LIB_ROOT}/smooth/include
$ENV{IDF_PATH}/components/json/cJSON)

target_link_libraries(${PROJECT_NAME} mbedtls mbedx509 mbedcrypto)
target_link_libraries(${PROJECT_NAME} mbedtls mbedx509 mbedcrypto sodium)
set_compile_options(${PROJECT_NAME})
8 changes: 4 additions & 4 deletions lib/files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ set(SMOOTH_SOURCES
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/MMCSDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/SPISDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/SPIFlash.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/sntp/Sntp.h
${CMAKE_CURRENT_LIST_DIR}/smooth/application/security/PasswordHash.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/application/security/PasswordHash.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/network/Wifi.h
$ENV{IDF_PATH}/components/json/cJSON/cJSON.c
$ENV{IDF_PATH}/components/json/cJSON/cJSON.h
)
Expand All @@ -138,7 +142,6 @@ if (${ESP_PLATFORM})
list(APPEND SMOOTH_SOURCES
${CMAKE_CURRENT_LIST_DIR}/smooth/core/logging/idf/idf_log.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/core/network/Wifi.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/network/Wifi.h
${CMAKE_CURRENT_LIST_DIR}/smooth/core/io/Input.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/core/io/Output.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/core/io/InterruptInput.cpp
Expand All @@ -153,7 +156,6 @@ if (${ESP_PLATFORM})
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/SPISDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/MMCSDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/core/sntp/Sntp.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/sntp/Sntp.h
${CMAKE_CURRENT_LIST_DIR}/smooth/core/filesystem/MMCSDCard.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/Input.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/Output.h
Expand All @@ -166,8 +168,6 @@ if (${ESP_PLATFORM})
${CMAKE_CURRENT_LIST_DIR}/smooth/application/io/i2c/MCP23017.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/application/io/i2c/BME280.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/application/io/wiegand/Wiegand.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/application/security/PasswordHash.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/application/security/PasswordHash.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/Input.h
)
else()
Expand Down
3 changes: 2 additions & 1 deletion lib/smooth/application/security/PasswordHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace smooth::application::security
auto result = crypto_pwhash_str(out,
password.c_str(),
password.size(),
std::min(std::max(crypto_pwhash_OPSLIMIT_MIN, computation_count),
std::min(std::max(crypto_pwhash_OPSLIMIT_MIN,
static_cast<decltype(crypto_pwhash_OPSLIMIT_MIN)>(computation_count)),
crypto_pwhash_OPSLIMIT_MAX),
mem_limit) == 0;

Expand Down
6 changes: 3 additions & 3 deletions lib/smooth/core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ namespace smooth::core
// ready to receive network status events.
network::SocketDispatcher::instance();

if (wifi.is_configured())
if (get_wifi().is_configured())
{
wifi.connect_to_ap();
get_wifi().connect_to_ap();
}
}

void IDFApplication::event(const system_event_t& event)
{
wifi.event(event);
get_wifi().event(event);
}
#endif
}
5 changes: 3 additions & 2 deletions lib/smooth/core/sntp/Sntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
#include <lwip/apps/sntp.h>
#include <ctime>
#include <iostream>
#include <utility>

namespace smooth::core::sntp
{

Sntp::Sntp(const std::vector<std::string>& servers)
: servers(servers)
Sntp::Sntp(std::vector<std::string> servers)
: servers(std::move(servers))
{
}

Expand Down
34 changes: 17 additions & 17 deletions lib/smooth/include/smooth/core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
#include <smooth/core/ipc/Queue.h>
#include <smooth/core/Task.h>

#ifdef ESP_PLATFORM
#include <smooth/core/network/Wifi.h>
#endif // END ESP_PLATFORM

#include <smooth/core/ipc/TaskEventQueue.h>
#include <smooth/core/ipc/SubscribingTaskEventQueue.h>
#include <smooth/core/network/Wifi.h>

namespace smooth::core
{
Expand All @@ -50,14 +48,24 @@ namespace smooth::core

/// Initialize the application.
void init() override;

/// Returns the Wifi manager
/// \return The Wifi management instance
network::Wifi& get_wifi()
{
return wifi;
}
private:
network::Wifi wifi{};
};


#ifdef ESP_PLATFORM

/// The IDFApplication extends Application with things needed to run under the IDF framework
class IDFApplication
: public POSIXApplication,
public smooth::core::ipc::IEventListener<system_event_t>
public smooth::core::ipc::IEventListener<system_event_t>
{
public:
/// Constructor
Expand All @@ -66,40 +74,32 @@ namespace smooth::core
/// \param tick_interval The tick interval
IDFApplication(uint32_t priority, std::chrono::milliseconds tick_interval);

virtual ~IDFApplication()
{
}
~IDFApplication() override = default;

/// Event method for system events.
/// \param event The event.
void event(const system_event_t& event) override;

/// Returns the Wifi manager
/// \return The Wifi management instance
network::Wifi& get_wifi()
{
return wifi;
}

protected:
void init() override;

private:
static esp_err_t event_callback(void* ctx, system_event_t* event);

using SystemEventQueue = ipc::SubscribingTaskEventQueue<system_event_t>;
std::shared_ptr<SystemEventQueue> system_event;
network::Wifi wifi;

static const std::unordered_map<int, const char*> id_to_system_event;
};

#endif // END ESP_PLATFORM

class Application
:
#ifdef ESP_PLATFORM
public IDFApplication
#else
public POSIXApplication
public POSIXApplication
#endif
{
public:
Expand All @@ -112,7 +112,7 @@ namespace smooth::core
#ifdef ESP_PLATFORM
IDFApplication(priority, tick_interval)
#else
POSIXApplication(priority, tick_interval)
POSIXApplication(priority, tick_interval)
#endif
{
}
Expand Down
2 changes: 1 addition & 1 deletion lib/smooth/include/smooth/core/filesystem/MountPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "Path.h"

#ifdef ESP_PLATFORM
static smooth::core::filesystem::Path FlashMount{"/flash}";
static smooth::core::filesystem::Path FMount{"/flash"};
static smooth::core::filesystem::Path SDMount{"/sdcard"};
#else
// Place files in home folder on Linux.
Expand Down
7 changes: 6 additions & 1 deletion lib/smooth/include/smooth/core/network/Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#pragma once

#ifndef ESP_PLATFORM
#include "mock/Wifi.h"
#else

#include <string>
#include <array>
#include <esp_wifi.h>
Expand Down Expand Up @@ -76,4 +80,5 @@ namespace smooth::core::network
std::string ssid;
std::string password;
};
}
}
#endif
73 changes: 73 additions & 0 deletions lib/smooth/include/smooth/core/network/mock/Wifi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Smooth - C++ framework for writing applications based on Espressif's ESP-IDF.
// Copyright (C) 2017 Per Malmberg (https://github.com/PerMalmberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <string>
#include <array>
#include <smooth/core/ipc/IEventListener.h>

namespace smooth::core::network
{
/// Wifi management class
class Wifi
{
public:
Wifi() = default;

Wifi(const Wifi&) = delete;

virtual ~Wifi() = default;

/// Sets the hostname
/// \param name The name
void set_host_name(const std::string& /*name*/) {}

/// Sets the credentials for the Wifi network
/// \param ssid The SSID
/// \param password The password
void set_ap_credentials(const std::string& ssid, const std::string& password)
{
this->ssid = ssid;
this->password = password;
}

/// Enables, disables auto reconnect on loss of Wifi connection.
/// \param auto_connect
void set_auto_connect(bool /*auto_connect*/) {}

/// Initiates the connection to the AP.
void connect_to_ap() {}

/// Returns a value indicating of currently connected to the access point.
/// \return
bool is_connected_to_ap() const { return true; }

/// Returns a value indicating if the required settings are set.
/// \return true or false.
bool is_configured() const
{
return host_name.length() > 0 && ssid.length() > 0 && password.length() > 0;
}

std::string get_mac_address() { return "11:22:33:44:55:66"; }

private:
std::string host_name = "Smooth-Wifi";
std::string ssid;
std::string password;
};
}
9 changes: 7 additions & 2 deletions lib/smooth/include/smooth/core/sntp/Sntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#pragma once

#ifndef ESP_PLATFORM
#include "mock/Sntp.h"
#else

#include <vector>
#include <string>

Expand All @@ -24,7 +28,7 @@ namespace smooth::core::sntp
class Sntp
{
public:
explicit Sntp(const std::vector<std::string>& servers);
explicit Sntp(std::vector<std::string> servers);

void start();

Expand All @@ -34,4 +38,5 @@ namespace smooth::core::sntp
const std::vector<std::string> servers;
bool started = false;
};
}
}
#endif
43 changes: 43 additions & 0 deletions lib/smooth/include/smooth/core/sntp/mock/Sntp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Smooth - C++ framework for writing applications based on Espressif's ESP-IDF.
// Copyright (C) 2017 Per Malmberg (https://github.com/PerMalmberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <vector>
#include <string>

namespace smooth::core::sntp
{
class Sntp
{
public:
explicit Sntp(std::vector<std::string> /*servers*/)
{}

void start()
{
started = true;
}

[[nodiscard]] bool is_time_set() const
{
return started;
}

private:
bool started = false;
};
}
Loading

0 comments on commit 329bdf5

Please sign in to comment.