Skip to content

Commit

Permalink
implements System feature FEATURE_NCP_FW_UPDATES and performs NCP FW …
Browse files Browse the repository at this point in the history
…update in Safe Mode
  • Loading branch information
technobly committed Feb 4, 2022
1 parent 52ddab5 commit 82b8b9f
Show file tree
Hide file tree
Showing 7 changed files with 956 additions and 3 deletions.
3 changes: 2 additions & 1 deletion hal/inc/core_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ typedef enum HAL_Feature {
FEATURE_WIFI_POWERSAVE_CLOCK, // [write only] enables/disables the WiFi powersave clock on the TESTMODE pin. This setting is persisted to the DCT.
FEATURE_ETHERNET_DETECTION, // [read/write] enables Ethernet FeatherWing detection on boot
FEATURE_LED_OVERRIDDEN, // [read/write] override system RGB signaling on boot.
FEATURE_DISABLE_EXTERNAL_LOW_SPEED_CLOCK // [read/write] force usage of internal low speed clock
FEATURE_DISABLE_EXTERNAL_LOW_SPEED_CLOCK, // [read/write] force usage of internal low speed clock
FEATURE_NCP_FW_UPDATES, // [read/write] enables/disables NCP FW updates
} HAL_Feature;

int HAL_Feature_Set(HAL_Feature feature, bool enabled);
Expand Down
7 changes: 7 additions & 0 deletions hal/src/nRF52840/core_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ int HAL_Feature_Set(HAL_Feature feature, bool enabled) {
case FEATURE_LED_OVERRIDDEN: {
return Write_Feature_Flag(FEATURE_FLAG_LED_OVERRIDDEN, enabled, NULL);
}
case FEATURE_NCP_FW_UPDATES: {
return Write_Feature_Flag(FEATURE_FLAG_NCP_FW_UPDATES, enabled, NULL);
}
}

return -1;
Expand All @@ -823,6 +826,10 @@ bool HAL_Feature_Get(HAL_Feature feature) {
bool value = false;
return (Read_Feature_Flag(FEATURE_FLAG_LED_OVERRIDDEN, &value) == 0) ? value : false;
}
case FEATURE_NCP_FW_UPDATES: {
bool value = false;
return (Read_Feature_Flag(FEATURE_FLAG_NCP_FW_UPDATES, &value) == 0) ? value : false;
}
}
return false;
}
Expand Down
9 changes: 8 additions & 1 deletion hal/src/stm32f2xx/core_hal_stm32f2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1221,11 +1221,14 @@ int HAL_Feature_Set(HAL_Feature feature, bool enabled)
return Write_Feature_Flag(FEATURE_FLAG_LED_OVERRIDDEN, !enabled, NULL);
}
#if HAL_PLATFORM_INTERNAL_LOW_SPEED_CLOCK
case FEATURE_DISABLE_EXTERNAL_LOW_SPEED_CLOCK: {
case FEATURE_DISABLE_EXTERNAL_LOW_SPEED_CLOCK: {
uint32_t value = enabled ? DCT_EXT_LOW_SPEED_CLOCK_DISABLE_SET : DCT_EXT_LOW_SPEED_CLOCK_DISABLE_CLEAR;
return dct_write_app_data(&value, DCT_EXT_LOW_SPEED_CLOCK_DISABLE_OFFSET, sizeof(value));
}
#endif // HAL_PLATFORM_INTERNAL_LOW_SPEED_CLOCK
case FEATURE_NCP_FW_UPDATES: {
return Write_Feature_Flag(FEATURE_FLAG_NCP_FW_UPDATES, enabled, NULL);
}
}
return -1;
}
Expand Down Expand Up @@ -1274,6 +1277,10 @@ bool HAL_Feature_Get(HAL_Feature feature)
return value == DCT_EXT_LOW_SPEED_CLOCK_DISABLE_SET;
}
#endif // HAL_PLATFORM_INTERNAL_LOW_SPEED_CLOCK
case FEATURE_NCP_FW_UPDATES: {
bool value = false;
return (Read_Feature_Flag(FEATURE_FLAG_NCP_FW_UPDATES, &value) == 0) ? value : false;
}
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions platform/MCU/STM32F2xx/SPARK_Firmware_Driver/inc/dct.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ STATIC_ASSERT(static_ip_config_size, sizeof(static_ip_config_t)==24);
typedef enum Feature_Flag {
FEATURE_FLAG_RESET_INFO = 0x01, // HAL_Feature::FEATURE_RESET_INFO
FEATURE_FLAG_LED_OVERRIDDEN = 0x02,
FEATURE_FLAG_NCP_FW_UPDATES = 0x04,
} Feature_Flag;

/**
Expand Down
3 changes: 2 additions & 1 deletion platform/MCU/nRF52840/inc/dct.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ PARTICLE_STATIC_ASSERT(static_ip_config_size, sizeof(static_ip_config_t)==24);
typedef enum Feature_Flag {
FEATURE_FLAG_RESET_INFO = 0x01,
FEATURE_FLAG_ETHERNET_DETECTION = 0x02,
FEATURE_FLAG_LED_OVERRIDDEN = 0x04
FEATURE_FLAG_LED_OVERRIDDEN = 0x04,
FEATURE_FLAG_NCP_FW_UPDATES = 0x08,
} Feature_Flag;

/**
Expand Down
121 changes: 121 additions & 0 deletions system/inc/ncp_fw_update.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2021 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "hal_platform.h"
#include "system_tick_hal.h"
#include "at_parser.h"
// #include "at_command.h"
#include "at_response.h"

#if HAL_PLATFORM_CELLULAR

namespace particle {

namespace system {

enum NcpFWUpdateState {
FW_UPDATE_IDLE_STATE = 0,
FW_UPDATE_IDLE_STATE_1 = 101, // DEBUG
FW_UPDATE_IDLE_STATE_2 = 102, // DEBUG
FW_UPDATE_IDLE_STATE_3 = 103, // DEBUG
FW_UPDATE_QUALIFY_FLAGS_STATE = 1,
FW_UPDATE_QUALIFY_MODEM_ON_STATE = 2,
FW_UPDATE_SETUP_CLOUD_CONNECT_STATE = 3,
FW_UPDATE_SETUP_CLOUD_CONNECTING_STATE = 4,
FW_UPDATE_SETUP_CLOUD_CONNECTED_STATE = 5,
FW_UPDATE_DOWNLOAD_CLOUD_DISCONNECT_STATE = 6,
FW_UPDATE_DOWNLOAD_CLOUD_DISCONNECTED_STATE = 7,
FW_UPDATE_DOWNLOAD_READY_STATE = 8,
FW_UPDATE_INSTALL_CELL_DISCONNECTING_STATE = 9,
FW_UPDATE_INSTALL_STATE_STARTING = 10,
FW_UPDATE_INSTALL_STATE_WAITING = 12,
FW_UPDATE_FINISHED_FACTORY_RESET_STATE = 13,
FW_UPDATE_FINISHED_POWERING_OFF_STATE = 14,
FW_UPDATE_FINISHED_CLOUD_CONNECTING_STATE = 15,
FW_UPDATE_FINISHED_CLOUD_CONNECTED_STATE = 16,
FW_UPDATE_FINISHED_STATE = 17,
};
enum NcpFWUpdateStatus {
FW_UPDATE_NONE_STATUS = 0,
FW_UPDATE_DOWNLOADING_STATUS = 1,
FW_UPDATE_UPDATING_STATUS = 2,
FW_UPDATE_SUCCESS_STATUS = 3,
FW_UPDATE_FAILED_STATUS = 4,
};

struct HTTPSresponse {
int command;
int result;
int status_code;
char md5_sum[40];
int err_class;
int err_code;
};

class NcpFwUpdate {
public:
/**
* Get the singleton instance of this class.
*/
static NcpFwUpdate* instance();

NcpFwUpdate();
~NcpFwUpdate();

void checkUpdate();

NcpFWUpdateStatus process();

private:

NcpFWUpdateState ncpFwUpdateState_ = FW_UPDATE_IDLE_STATE;
NcpFWUpdateStatus ncpFwUpdateStatus_ = FW_UPDATE_NONE_STATUS;
int foatReady_;
system_tick_t startInstallTimer_;
system_tick_t atOkCheckTimer_;
int lastRespCode_;
int atResp_;
int atResponsive_;
int startingFirmwareVersion_;
int firmwareVersion_;
int updateVersion_;
system_tick_t cooldownTimer_;
system_tick_t cooldownTimeout_;

// void dumpAtCmd_(const char* buf, int len);
static void ATResponseOut_(void* data, const char* msg);
static int cbUUHTTPCR_(int type, const char* buf, int len, HTTPSresponse* data);
static int cbUHTTPER_(int type, const char* buf, int len, HTTPSresponse* data);
static int cbUUFWINSTALL_(int type, const char* buf, int len, int* data);
static int cbULSTFILE_(int type, const char* buf, int len, int* data);
static int cbATI9_(int type, const char* buf, int len, int* val);
static int cbUPSND_(int type, const char* buf, int len, int* data);
static int httpRespCallback_(AtResponseReader* reader, const char* prefix, void* data);
int getAppFirmwareVersion_();
int setupHTTPSProperties_();
void cooldown_(system_tick_t timer);
void updateCooldown_();
bool inCooldown_();
};

} // namespace system

} // namespace particle

#endif // #if HAL_PLATFORM_CELLULAR
Loading

0 comments on commit 82b8b9f

Please sign in to comment.