Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/dfu_device_id #949

Merged
merged 4 commits into from
Jun 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- [Electron] Reduced data consumption connecting to the cloud with deep sleep. (NB: see the docs for how to gain the full data reduction.) [#953](https://github.com/spark/firmware/pull/953)
- Can set Claim Code via the Serial interface (for use by the CLI.) [#602](https://github.com/spark/firmware/issues/602)
- Device ID available via dfu-util. [#949](https://github.com/spark/firmware/pull/949)

### ENHANCEMENTS

Expand Down
5 changes: 5 additions & 0 deletions hal/inc/deviceid_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ unsigned HAL_Platform_ID();
*/
int HAL_Get_Device_Identifier(const char** name, char* buf, size_t buflen, unsigned index, void* reserved);

/**
* Save the device ID to persistent storage so it can be retrieved via DCT. This is
* done automatically by the system on startup.
*/
void HAL_save_device_id(uint32_t offset);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion hal/src/core/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

const unsigned device_id_len = 12;

/* Read the STM32 Device electronic signature */
unsigned HAL_device_ID(uint8_t* dest, unsigned destLen)
{
if (dest!=NULL && destLen!=0)
Expand All @@ -46,4 +47,4 @@ unsigned HAL_Platform_ID()
int HAL_Get_Device_Identifier(const char** name, char* buf, size_t buflen, unsigned index, void* reserved)
{
return -1;
}
}
3 changes: 3 additions & 0 deletions hal/src/stm32f2xx/core_hal_stm32f2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "malloc.h"
#include "usb_hal.h"
#include "usart_hal.h"
#include "deviceid_hal.h"

#define STOP_MODE_EXIT_CONDITION_PIN 0x01
#define STOP_MODE_EXIT_CONDITION_RTC 0x02
Expand Down Expand Up @@ -281,6 +282,8 @@ void HAL_Core_Setup(void) {
bootloader_update_if_needed();
HAL_Bootloader_Lock(true);

HAL_save_device_id(DCT_DEVICE_ID_OFFSET);

#if !defined(MODULAR_FIRMWARE)
module_user_init_hook();
#endif
Expand Down
12 changes: 12 additions & 0 deletions hal/src/stm32f2xx/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "deviceid_hal.h"
#include "platform_config.h"
#include "dct_hal.h"
#include <algorithm>
#include <cstring>

Expand All @@ -42,3 +43,14 @@ unsigned HAL_Platform_ID()
{
return PLATFORM_ID;
}

void HAL_save_device_id(uint32_t dct_offset)
{
const char* saved_device_id = (const char*)dct_read_app_data(dct_offset);
if (*saved_device_id == 0xFF)
{
uint8_t device_id[device_id_len];
HAL_device_ID(device_id, sizeof(device_id));
dct_write_app_data(device_id, dct_offset, device_id_len);
}
}
4 changes: 2 additions & 2 deletions hal/src/stm32f2xx/ota_flash_hal_stm32f2xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ int HAL_Set_System_Config(hal_system_config_t config_item, const void* data, uns
dct_write_app_data(&data_length, offset++, 1);
break;
case SYSTEM_CONFIG_SOFTAP_SUFFIX:
offset = DCT_DEVICE_ID_OFFSET;
length = DCT_DEVICE_ID_SIZE;
offset = DCT_DEVICE_CODE_OFFSET;
length = DCT_DEVICE_CODE_SIZE;
break;
case SYSTEM_CONFIG_SOFTAP_HOSTNAMES:
break;
Expand Down
27 changes: 15 additions & 12 deletions platform/MCU/STM32F2xx/SPARK_Firmware_Driver/inc/dct.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ typedef struct __attribute__((packed)) application_dct {
uint8_t claim_code[63]; // claim code. no terminating null.
uint8_t claimed[1]; // 0,0xFF, not claimed. 1 claimed.
uint8_t ssid_prefix[26]; // SSID prefix (25 chars max). First byte is length.
uint8_t device_id[6]; // 6 suffix characters (not null terminated))
uint8_t device_code[6]; // 6 suffix characters (not null terminated))
uint8_t version_string[32]; // version string including date
uint8_t dns_resolve[128]; // DNS names to resolve.
uint8_t reserved1[64];
uint8_t server_public_key[768]; // 4096 bits
uint8_t padding[2]; // align to 4 byte boundary
platform_flash_modules_t flash_modules[MAX_MODULES_SLOT];//100 bytes
platform_flash_modules_t flash_modules[MAX_MODULES_SLOT]; //100 bytes
uint16_t product_store[12];
uint8_t antenna_selection; // 0xFF is uninitialized
uint8_t cloud_transport; // 0xFF is uninitialized meaning platform default (TCP for Photon, UDP for Electron). 0 is TCP on Electron.
uint8_t alt_device_public_key[128]; // alternative device public key
uint8_t alt_device_private_key[192]; // alternative device private key
uint8_t cloud_transport; // 0xFF is uninitialized meaning platform default (TCP for Photon, UDP for Electron). 0 is TCP on Electron.
uint8_t alt_device_public_key[128]; // alternative device public key
uint8_t alt_device_private_key[192]; // alternative device private key
uint8_t alt_server_public_key[192];
uint8_t alt_server_address[DCT_SERVER_ADDRESS_SIZE]; // server address info

uint8_t reserved2[640];
uint8_t alt_server_address[DCT_SERVER_ADDRESS_SIZE]; // server address info
uint8_t device_id[12]; // the STM32 device ID
uint8_t reserved2[628];
// safe to add more data here or use up some of the reserved space to keep the end where it is
uint8_t end[0];
} application_dct_t;
Expand All @@ -93,7 +93,7 @@ typedef struct __attribute__((packed)) application_dct {
#define DCT_CLAIM_CODE_OFFSET (offsetof(application_dct_t, claim_code))
#define DCT_SSID_PREFIX_OFFSET (offsetof(application_dct_t, ssid_prefix))
#define DCT_DNS_RESOLVE_OFFSET (offsetof(application_dct_t, dns_resolve))
#define DCT_DEVICE_ID_OFFSET (offsetof(application_dct_t, device_id))
#define DCT_DEVICE_CODE_OFFSET (offsetof(application_dct_t, device_code))
#define DCT_DEVICE_CLAIMED_OFFSET (offsetof(application_dct_t, claimed))
#define DCT_FLASH_MODULES_OFFSET (offsetof(application_dct_t, flash_modules))
#define DCT_PRODUCT_STORE_OFFSET (offsetof(application_dct_t, product_store))
Expand All @@ -103,6 +103,7 @@ typedef struct __attribute__((packed)) application_dct {
#define DCT_ALT_DEVICE_PRIVATE_KEY_OFFSET (offsetof(application_dct_t, alt_device_private_key))
#define DCT_ALT_SERVER_PUBLIC_KEY_OFFSET (offsetof(application_dct_t, alt_server_public_key))
#define DCT_ALT_SERVER_ADDRESS_OFFSET (offsetof(application_dct_t, alt_server_address))
#define DCT_DEVICE_ID_OFFSET (offsetof(application_dct_t, device_id))

#define DCT_SYSTEM_FLAGS_SIZE (sizeof(application_dct_t::system_flags))
#define DCT_DEVICE_PRIVATE_KEY_SIZE (sizeof(application_dct_t::device_private_key))
Expand All @@ -113,7 +114,7 @@ typedef struct __attribute__((packed)) application_dct {
#define DCT_CLAIM_CODE_SIZE (sizeof(application_dct_t::claim_code))
#define DCT_SSID_PREFIX_SIZE (sizeof(application_dct_t::ssid_prefix))
#define DCT_DNS_RESOLVE_SIZE (sizeof(application_dct_t::dns_resolve))
#define DCT_DEVICE_ID_SIZE (sizeof(application_dct_t::device_id))
#define DCT_DEVICE_CODE_SIZE (sizeof(application_dct_t::device_code))
#define DCT_DEVICE_CLAIMED_SIZE (sizeof(application_dct_t::claimed))
#define DCT_FLASH_MODULES_SIZE (sizeof(application_dct_t::flash_modules))
#define DCT_PRODUCT_STORE_SIZE (sizeof(application_dct_t::product_store))
Expand All @@ -123,6 +124,7 @@ typedef struct __attribute__((packed)) application_dct {
#define DCT_ALT_DEVICE_PRIVATE_KEY_SIZE (sizeof(application_dct_t::alt_device_private_key))
#define DCT_ALT_SERVER_PUBLIC_KEY_SIZE (sizeof(application_dct_t::alt_server_public_key))
#define DCT_ALT_SERVER_ADDRESS_SIZE (sizeof(application_dct_t::alt_server_address))
#define DCT_DEVICE_ID_SIZE (sizeof(application_dct_t::device_id))

#define STATIC_ASSERT_DCT_OFFSET(field, expected) STATIC_ASSERT( dct_##field, offsetof(application_dct_t, field)==expected)
#define STATIC_ASSERT_FLAGS_OFFSET(field, expected) STATIC_ASSERT( dct_sysflag_##field, offsetof(platform_system_flags_t, field)==expected)
Expand All @@ -139,7 +141,7 @@ STATIC_ASSERT_DCT_OFFSET(country_code, 1758 /* 1634 + 124 */);
STATIC_ASSERT_DCT_OFFSET(claim_code, 1762 /* 1758 + 4 */);
STATIC_ASSERT_DCT_OFFSET(claimed, 1825 /* 1762 + 63 */ );
STATIC_ASSERT_DCT_OFFSET(ssid_prefix, 1826 /* 1825 + 1 */);
STATIC_ASSERT_DCT_OFFSET(device_id, 1852 /* 1826 + 26 */);
STATIC_ASSERT_DCT_OFFSET(device_code, 1852 /* 1826 + 26 */);
STATIC_ASSERT_DCT_OFFSET(version_string, 1858 /* 1852 + 6 */);
STATIC_ASSERT_DCT_OFFSET(dns_resolve, 1890 /* 1868 + 32 */);
STATIC_ASSERT_DCT_OFFSET(reserved1, 2018 /* 1890 + 128 */);
Expand All @@ -153,8 +155,9 @@ STATIC_ASSERT_DCT_OFFSET(alt_device_public_key, 2978 /* 2977 + 1 */);
STATIC_ASSERT_DCT_OFFSET(alt_device_private_key, 3106 /* 2978 + 128 */);
STATIC_ASSERT_DCT_OFFSET(alt_server_public_key, 3298 /* 3106 + 192 */);
STATIC_ASSERT_DCT_OFFSET(alt_server_address, 3490 /* 3298 + 192 */);
STATIC_ASSERT_DCT_OFFSET(device_id, 3618 /* 3490 + 128 */);

STATIC_ASSERT_DCT_OFFSET(reserved2, 3618 /* 3490 + 128 */);
STATIC_ASSERT_DCT_OFFSET(reserved2, 3630 /* 3618 + 12 */);
STATIC_ASSERT_DCT_OFFSET(end, 4258 /* 2952 + 1280 */);

STATIC_ASSERT_FLAGS_OFFSET(Bootloader_Version_SysFlag, 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#include "platforms.h"

// STM32 Device electronic signature
// Factory-programmed 12 byte unique device ID
#define ID1 (0x1FFF7A10)
#define ID2 (0x1FFF7A14)
#define ID3 (0x1FFF7A18)
Expand Down