Skip to content

Commit

Permalink
[gen2][supply secure] system
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis authored and technobly committed May 18, 2022
1 parent f12f66a commit 941ebef
Show file tree
Hide file tree
Showing 18 changed files with 21 additions and 284 deletions.
Empty file.
16 changes: 8 additions & 8 deletions system/inc/system_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,29 @@ inline void cloud_disconnect(unsigned flags, System_Reset_Reason resetReason) {
cloud_disconnect(flags, CLOUD_DISCONNECT_REASON_SYSTEM_RESET, NETWORK_DISCONNECT_REASON_NONE, resetReason, 0);
}

#if PLATFORM_ID==3
#if PLATFORM_ID == PLATFORM_GCC
// avoid a c-linkage incompatible with C error on newer versions of gcc
String spark_deviceID(void);
#endif
#endif // PLATFORM_ID == PLATFORM_GCC

#ifdef __cplusplus
extern "C" {
#endif

#if PLATFORM_ID!=3
#if PLATFORM_ID != PLATFORM_GCC
String spark_deviceID(void);
#endif
#endif // PLATFORM_ID != PLATFORM_GCC

class String;


#if defined(PLATFORM_ID)

#if !defined(UNIT_TEST) && PLATFORM_ID !=3 && PLATFORM_ID != 20
#if !defined(UNIT_TEST) && PLATFORM_ID != PLATFORM_GCC
PARTICLE_STATIC_ASSERT(spark_data_typedef_is_1_byte, sizeof(Spark_Data_TypeDef)==1);
#endif
#endif // !defined(UNIT_TEST) && PLATFORM_ID != PLATFORM_GCC

#endif
#endif // defined(PLATFORM_ID)

const uint32_t PUBLISH_EVENT_FLAG_PUBLIC = 0x0;
const uint32_t PUBLISH_EVENT_FLAG_PRIVATE = 0x1;
Expand Down Expand Up @@ -374,7 +374,7 @@ extern const unsigned char backup_tcp_public_server_address[18];

#if HAL_PLATFORM_CELLULAR || HAL_PLATFORM_NCP
#define TIMING_FLASH_UPDATE_TIMEOUT (300000) // 300sec
#else // HAL_PLATFORM_CELLULAR || HAL_PLATFORM_NCP
#else // likely newhal and gcc
#define TIMING_FLASH_UPDATE_TIMEOUT (30000) // 30sec
#endif // HAL_PLATFORM_CELLULAR || HAL_PLATFORM_NCP

Expand Down
2 changes: 1 addition & 1 deletion system/inc/system_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef enum network_ready_type {
} network_ready_type;
/**
* network_handle_t used to differentiate between two networks
* on the same device, e.g. WLAN and AP modes on Photon.
* on the same device
*/
typedef network_interface_t network_handle_t;
const network_interface_t NIF_DEFAULT = 0;
Expand Down
2 changes: 1 addition & 1 deletion system/inc/system_sleep_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class SystemSleepConfigurationHelper {
}
wakeup = wakeupSourceFeatured(HAL_WAKEUP_SOURCE_TYPE_GPIO, wakeup->next);
}
#endif
#endif // HAL_PLATFORM_FUELGAUGE_MAX17043
return false;
}

Expand Down
8 changes: 0 additions & 8 deletions system/src/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ CPPSRC += $(call target_files,src/,*.cpp)
# ASM source files included in this build.
ASRC +=

ifeq ($(PLATFORM_ID),6)
CFLAGS += -DLOG_COMPILE_TIME_LEVEL=LOG_LEVEL_NONE
endif

ifeq ($(PLATFORM_ID),8)
CFLAGS += -DLOG_COMPILE_TIME_LEVEL=LOG_LEVEL_NONE
endif

INCLUDE_DIRS += $(TARGET_SRC_PATH)
INCLUDE_DIRS += $(TARGET_SRC_PATH)/control/proto

Expand Down
129 changes: 2 additions & 127 deletions system/src/control/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

#if HAL_PLATFORM_NRF52840
#include "ota_flash_hal_impl.h"
#else
#include "ota_flash_hal_stm32f2xx.h"
#endif

#if HAL_PLATFORM_NCP
Expand Down Expand Up @@ -240,131 +238,8 @@ int echo(ctrl_request* req) {
return 0;
}

#if !HAL_PLATFORM_NRF52840

int handleSetSecurityKeyRequest(ctrl_request* req) {
particle_ctrl_SetSecurityKeyRequest pbReq = {};
DecodedString pbKey(&pbReq.data);
int ret = decodeRequestMessage(req, particle_ctrl_SetSecurityKeyRequest_fields, &pbReq);
if (ret == 0) {
ret = store_security_key_data((security_key_type)pbReq.type, pbKey.data, pbKey.size);
}
return ret;
}

int handleGetSecurityKeyRequest(ctrl_request* req) {
particle_ctrl_GetSecurityKeyRequest pbReq = {};
int ret = decodeRequestMessage(req, particle_ctrl_GetSecurityKeyRequest_fields, &pbReq);
if (ret == 0) {
particle_ctrl_GetSecurityKeyReply pbRep = {};
EncodedString pbKey(&pbRep.data);
ret = lock_security_key_data((security_key_type)pbReq.type, &pbKey.data, &pbKey.size);
if (ret == 0) {
ret = encodeReplyMessage(req, particle_ctrl_GetSecurityKeyReply_fields, &pbRep);
unlock_security_key_data((security_key_type)pbReq.type);
}
}

return ret;
}

int handleSetServerAddressRequest(ctrl_request* req) {
particle_ctrl_SetServerAddressRequest pbReq = {};
int ret = decodeRequestMessage(req, particle_ctrl_SetServerAddressRequest_fields, &pbReq);
if (ret == 0) {
ServerAddress addr = {};
// Check if the address string contains an IP address
// TODO: Move IP address parsing/encoding to separate functions
unsigned n1 = 0, n2 = 0, n3 = 0, n4 = 0;
if (sscanf(pbReq.address, "%u.%u.%u.%u", &n1, &n2, &n3, &n4) == 4) {
addr.addr_type = IP_ADDRESS;
addr.ip = ((n1 & 0xff) << 24) | ((n2 & 0xff) << 16) | ((n3 & 0xff) << 8) | (n4 & 0xff);
} else {
const size_t n = strlen(pbReq.address);
if (n < sizeof(ServerAddress::domain)) {
addr.addr_type = DOMAIN_NAME;
addr.length = n;
memcpy(addr.domain, pbReq.address, n);
} else {
ret = SYSTEM_ERROR_TOO_LARGE;
}
}
if (ret == 0) {
addr.port = pbReq.port;
ret = store_server_address((server_protocol_type)pbReq.protocol, &addr);
}
}
return ret;
}

int handleGetServerAddressRequest(ctrl_request* req) {
particle_ctrl_GetServerAddressRequest pbReq = {};
int ret = decodeRequestMessage(req, particle_ctrl_GetServerAddressRequest_fields, &pbReq);
if (ret == 0) {
ServerAddress addr = {};
ret = load_server_address((server_protocol_type)pbReq.protocol, &addr);
if (ret == 0) {
if (addr.addr_type == IP_ADDRESS) {
const unsigned n1 = (addr.ip >> 24) & 0xff,
n2 = (addr.ip >> 16) & 0xff,
n3 = (addr.ip >> 8) & 0xff,
n4 = addr.ip & 0xff;
const int n = snprintf(addr.domain, sizeof(addr.domain), "%u.%u.%u.%u", n1, n2, n3, n4);
if (n > 0 && (size_t)n < sizeof(addr.domain)) {
addr.length = n;
} else {
ret = SYSTEM_ERROR_TOO_LARGE;
}
}
if (ret == 0) {
particle_ctrl_GetServerAddressReply pbRep = {};
EncodedString pbAddr(&pbRep.address, addr.domain, addr.length);
pbRep.port = addr.port;
ret = encodeReplyMessage(req, particle_ctrl_GetServerAddressReply_fields, &pbRep);
}
}
}
return ret;
}

int handleSetServerProtocolRequest(ctrl_request* req) {
particle_ctrl_SetServerProtocolRequest pbReq = {};
int ret = decodeRequestMessage(req, particle_ctrl_SetServerProtocolRequest_fields, &pbReq);
if (ret == 0) {
bool udpEnabled = false;
if (pbReq.protocol == particle_ctrl_ServerProtocolType_TCP_PROTOCOL ||
(udpEnabled = (pbReq.protocol == particle_ctrl_ServerProtocolType_UDP_PROTOCOL))) {
ret = HAL_Feature_Set(FEATURE_CLOUD_UDP, udpEnabled);
} else {
ret = SYSTEM_ERROR_NOT_SUPPORTED;
}
}
return ret;
}

int handleGetServerProtocolRequest(ctrl_request* req) {
particle_ctrl_GetServerProtocolReply pbRep = {};
if (HAL_Feature_Get(FEATURE_CLOUD_UDP)) {
pbRep.protocol = particle_ctrl_ServerProtocolType_UDP_PROTOCOL;
} else {
pbRep.protocol = particle_ctrl_ServerProtocolType_TCP_PROTOCOL;
}
const int ret = encodeReplyMessage(req, particle_ctrl_GetServerProtocolReply_fields, &pbRep);
return ret;
}

int handleSetSoftapSsidRequest(ctrl_request* req) {
particle_ctrl_SetSoftApSsidRequest pbReq = {};
int ret = decodeRequestMessage(req, particle_ctrl_SetSoftApSsidRequest_fields, &pbReq);
if (ret == 0 && (!HAL_Set_System_Config(SYSTEM_CONFIG_SOFTAP_PREFIX, pbReq.prefix, strlen(pbReq.prefix)) ||
!HAL_Set_System_Config(SYSTEM_CONFIG_SOFTAP_SUFFIX, pbReq.suffix, strlen(pbReq.suffix)))) {
ret = SYSTEM_ERROR_UNKNOWN;
}
return ret;
}

#else // HAL_PLATFORM_NRF52840

#if HAL_PLATFORM_NRF52840
// TODO
int handleSetSecurityKeyRequest(ctrl_request*) {
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand Down Expand Up @@ -394,7 +269,7 @@ int handleSetSoftapSsidRequest(ctrl_request*) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

#endif
#endif // HAL_PLATFORM_NRF52840

} // particle::control::config

Expand Down
4 changes: 0 additions & 4 deletions system/src/control/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
#include "system_network.h"
#include "common.h"

#if HAL_PLATFORM_NRF52840
#include "ota_flash_hal_impl.h"
#else
#include "ota_flash_hal_stm32f2xx.h"
#endif

#include "delay_hal.h"

Expand Down
8 changes: 4 additions & 4 deletions system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@
#include "radio_common.h"
#endif

#if PLATFORM_ID == 3
#if PLATFORM_ID == PLATFORM_GCC
// Application loop uses std::this_thread::sleep_for() to workaround 100% CPU usage on the GCC platform
#include <thread>
#endif
#endif // PLATFORM_ID == PLATFORM_GCC

using namespace spark;
using namespace particle;
Expand Down Expand Up @@ -516,15 +516,15 @@ void app_loop(bool threaded)
}
}
} while(false);
#if PLATFORM_ID == 3 && SUSPEND_APPLICATION_THREAD_LOOP_COUNT
#if PLATFORM_ID == PLATFORM_GCC && SUSPEND_APPLICATION_THREAD_LOOP_COUNT
// Suspend thread execution for some minimum time on every Nth loop iteration in order to workaround
// 100% CPU usage on the virtual device platform
static uint32_t loops = 0;
if (++loops >= SUSPEND_APPLICATION_THREAD_LOOP_COUNT) {
loops = 0;
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
#endif // PLATFORM_ID == 3
#endif // PLATFORM_ID == PLATFORM_GCC && SUSPEND_APPLICATION_THREAD_LOOP_COUNT
}


Expand Down
1 change: 0 additions & 1 deletion system/src/system_network_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include "hal_platform.h"

/* FIXME: there should be a define that tells whether there is NetworkManager available
* or not */
#if !HAL_PLATFORM_IFAPI
Expand Down
6 changes: 3 additions & 3 deletions system/src/system_network_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ class ManagedNetworkInterface : public NetworkInterface
volatile uint8_t WLAN_DHCP_PENDING = 0;
volatile uint8_t WLAN_LISTEN_ON_FAILED_CONNECT = 0;
volatile uint8_t WLAN_INITIALIZED = 0;
#if PLATFORM_ID == PLATFORM_ELECTRON // Electron
volatile uint32_t START_LISTENING_TIMER_MS = 300000UL; // 5 minute default on Electron
#if HAL_PLATFORM_CELLULAR
volatile uint32_t START_LISTENING_TIMER_MS = 300000UL; // 5 minute default for Cellular devices
#else
volatile uint32_t START_LISTENING_TIMER_MS = 0UL; // Disabled by default on Photon/P1/Core
volatile uint32_t START_LISTENING_TIMER_MS = 0UL; // Disabled for Wi-Fi devices
#endif
volatile uint32_t start_listening_timer_base = 0;
volatile uint32_t start_listening_timer_duration = 0;
Expand Down
27 changes: 1 addition & 26 deletions system/src/system_sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ network_status_t system_sleep_network_suspend(network_interface_index index) {

// Turn off the modem
if (!network_is_off(index, nullptr)) {
#if PLATFORM_GEN == 2
if (!SPARK_WLAN_SLEEP) {
status.on = true;
}
#endif
#if PLATFORM_GEN == 3
#if HAL_PLATFORM_IFAPI
if_t iface;
if (!if_get_by_index(index, &iface)) {
if (NetworkManager::instance()->isInterfacePowerState(iface, IF_POWER_STATE_UP) ||
Expand All @@ -97,25 +92,12 @@ network_status_t system_sleep_network_suspend(network_interface_index index) {
}

int system_sleep_network_resume(network_interface_index index, network_status_t status) {
#if PLATFORM_GEN == 2
/* On Gen2, calling network_on() and network_connect() will block until the connection is established
* if single threaded, or this function is invoked synchronously by the system thread if system threading
* is enabled. In both case, that would block the user application. Setting a flag here to unblock the user
* application and restore the connection later. */
if (status.on) {
SPARK_WLAN_SLEEP = 0;
}
if (status.connected) {
SPARK_WLAN_CONNECT_RESTORE = 1;
}
#else
if (status.on) {
network_on(index, 0, 0, nullptr);
}
if (status.connected) {
network_connect(index, 0, 0, nullptr);
}
#endif
return SYSTEM_ERROR_NONE;
}
#endif // PLATFORM_ID != PLATFORM_GCC
Expand Down Expand Up @@ -259,12 +241,5 @@ int system_sleep_ext_impl(const hal_sleep_config_t* config, hal_wakeup_source_ba

int system_sleep_ext(const hal_sleep_config_t* config, hal_wakeup_source_base_t** reason, void* reserved) {
LOG(TRACE, "Entering system_sleep_ext()");
#if HAL_PLATFORM_GEN == 2
// Cancel current connection attempt to unblock the system thread
// on Gen 2 platforms
if (network_connecting(NETWORK_INTERFACE_ALL, 0, nullptr)) {
network_connect_cancel(NETWORK_INTERFACE_ALL, 1, 0, 0);
}
#endif // HAL_PLATFORM_GEN == 2
return system_sleep_ext_impl(config, reason, reserved);
}
4 changes: 1 addition & 3 deletions system/src/system_sleep_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#include "cellular_hal.h"
#endif // HAL_PLATFORM_CELLULAR
#include "spark_wiring_system.h"
#if PLATFORM_ID == PLATFORM_ELECTRON_PRODUCTION
# include "parser.h"
#endif
#include "system_sleep_configuration.h"
#include "check.h"

Expand Down Expand Up @@ -64,6 +61,7 @@ static void network_resume() {
if (wakeupState.wifi) {
SPARK_WLAN_SLEEP = 0;
}
// Outdated - Fix with GCC refactoring
// Gen2-only: Set the system flags that triggers the wifi/cloud reconnection in the background loop
// FIXME: Gen3 won't automatically restore the modem state and network connection if cloud auto-connect flag is not set.
// See manage_network_connection() in system_network_manager_api.cpp.
Expand Down
9 changes: 0 additions & 9 deletions system/src/system_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,6 @@ void establish_cloud_connection()
return;
}

#if PLATFORM_ID==PLATFORM_ELECTRON_PRODUCTION
const CellularNetProvData provider_data = cellular_network_provider_data_get(NULL);
protocol::connection_properties_t conn_prop = {};
conn_prop.size = sizeof(conn_prop);
conn_prop.keepalive_source = protocol::KeepAliveSource::SYSTEM;
spark_set_connection_property(protocol::Connection::PING, (provider_data.keepalive * 1000), &conn_prop, nullptr);
spark_cloud_udp_port_set(provider_data.port);
#endif // PLATFORM_ID==PLATFORM_ELECTRON_PRODUCTION

INFO("Cloud: connecting");
const auto diag = CloudDiagnostics::instance();
diag->status(CloudDiagnostics::CONNECTING);
Expand Down
Loading

0 comments on commit 941ebef

Please sign in to comment.