Skip to content

Commit

Permalink
Merge pull request #1357 from spark/feature/ch3737-6-digit-serial
Browse files Browse the repository at this point in the history
Feature/ch3737 6 digit serial [0.7.0-rc.2]
  • Loading branch information
technobly committed Jul 12, 2017
2 parents c5e9492 + db69867 commit deb48b2
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 154 deletions.
8 changes: 8 additions & 0 deletions hal/src/electron/device_code.cpp
@@ -0,0 +1,8 @@
#include "device_code.h"
#include <string.h>

bool fetch_or_generate_ssid_prefix(device_code_t* result) {
strcpy(reinterpret_cast<char*>(result->value), "Electron");
result->length = 8;
return true;
}
76 changes: 7 additions & 69 deletions hal/src/photon/softap.cpp
Expand Up @@ -19,6 +19,7 @@
#include "bytes2hexbuf.h"
#include "spark_wiring_wifi_credentials.h"
#include "mbedtls/aes.h"
#include "device_code.h"

#if SOFTAP_HTTP
#include "http_server.h"
Expand Down Expand Up @@ -588,7 +589,7 @@ class ConfigureAPCommand : public JSONRequestCommand {
decrypt_result = 1;

return (len / 2);
}
}

virtual bool parsed_value(unsigned key, jsmntok_t* t, char* str) {
std::unique_ptr<char[]> tmp;
Expand Down Expand Up @@ -619,7 +620,7 @@ class ConfigureAPCommand : public JSONRequestCommand {
}
#endif
credentials.setPassword(str);
}
}
break;
case 3:
// ch
Expand All @@ -638,7 +639,7 @@ class ConfigureAPCommand : public JSONRequestCommand {
// outer identity
if (t->type == JSMN_STRING) {
credentials.setOuterIdentity(str);
}
}
break;
case 7:
// inner identity
Expand Down Expand Up @@ -866,64 +867,9 @@ struct AllSoftAPCommands {
connectAP(complete, softap_complete) {}
};

/**
* Converts a given 32-bit value to a alphanumeric code
* @param value The value to convert
* @param dest The number of charactres
* @param len
*/
void bytesToCode(uint32_t value, char* dest, unsigned len) {
static const char* symbols = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
while (len --> 0) {
*dest++ = symbols[value % 32];
value /= 32;
}
}

/**
* Generates a random code.
* @param dest
* @param len The length of the code, should be event.
*/
void random_code(uint8_t* dest, unsigned len) {
unsigned value = HAL_RNG_GetRandomNumber();
bytesToCode(value, (char*)dest, len);
}

const int DEVICE_CODE_LEN = 4;

STATIC_ASSERT(device_code_len_is_same_as_dct_storage, DEVICE_CODE_LEN<=DCT_DEVICE_CODE_SIZE);


extern "C" bool fetch_or_generate_setup_ssid(wiced_ssid_t* SSID);

/**
* Copies the device code to the destination, generating it if necessary.
* @param dest A buffer with room for at least 6 characters. The
* device code is copied here, without a null terminator.
* @return true if the device code was generated.
*/
bool fetch_or_generate_device_code(wiced_ssid_t* SSID) {
const uint8_t* suffix = (const uint8_t*)dct_read_app_data_lock(DCT_DEVICE_CODE_OFFSET);
int8_t c = (int8_t)*suffix; // check out first byte
bool generate = (!c || c<0);
uint8_t* dest = SSID->value+SSID->length;
SSID->length += DEVICE_CODE_LEN;
if (generate) {
dct_read_app_data_unlock(DCT_DEVICE_CODE_OFFSET);
random_code(dest, DEVICE_CODE_LEN);
dct_write_app_data(dest, DCT_DEVICE_CODE_OFFSET, DEVICE_CODE_LEN);
}
else {
memcpy(dest, suffix, DEVICE_CODE_LEN);
dct_read_app_data_unlock(DCT_DEVICE_CODE_OFFSET);
}
return generate;
}

const int MAX_SSID_PREFIX_LEN = 25;

bool fetch_or_generate_ssid_prefix(wiced_ssid_t* SSID) {
bool fetch_or_generate_ssid_prefix(device_code_t* SSID) {
const uint8_t* prefix = (const uint8_t*)dct_read_app_data_lock(DCT_SSID_PREFIX_OFFSET);
uint8_t len = *prefix;
bool generate = (!len || len>MAX_SSID_PREFIX_LEN);
Expand All @@ -942,13 +888,6 @@ bool fetch_or_generate_ssid_prefix(wiced_ssid_t* SSID) {
return generate;
}

bool fetch_or_generate_setup_ssid(wiced_ssid_t* SSID) {
bool result = fetch_or_generate_ssid_prefix(SSID);
SSID->value[SSID->length++] = '-';
result |= fetch_or_generate_device_code(SSID);
return result;
}

extern "C" wiced_ip_setting_t device_init_ip_settings;

/**
Expand All @@ -960,10 +899,9 @@ class SoftAPController {

wiced_result_t setup_soft_ap_credentials() {


wiced_config_soft_ap_t expected;
memset(&expected, 0, sizeof(expected));
fetch_or_generate_setup_ssid(&expected.SSID);
fetch_or_generate_setup_ssid((device_code_t*)&expected.SSID);

expected.channel = 11;
expected.details_valid = WICED_TRUE;
Expand Down Expand Up @@ -1322,7 +1260,7 @@ class SimpleProtocolDispatcher
int result = reader.read(&tmp, 1);
if (result >= 0) {
*c = tmp;
}
}
return result;
}

Expand Down
71 changes: 71 additions & 0 deletions hal/src/stm32f2xx/device_code.cpp
@@ -0,0 +1,71 @@

#include "dct.h"
#include "device_code.h"
#include "rng_hal.h"
#include <string.h>

const int DEVICE_CODE_LEN = 6;

STATIC_ASSERT(device_code_len_is_same_as_dct_storage, DEVICE_CODE_LEN<=DCT_DEVICE_CODE_SIZE);


/**
* Converts a given 32-bit value to a alphanumeric code
* @param value The value to convert
* @param dest The number of charactres
* @param len
*/
void bytesToCode(uint32_t value, char* dest, unsigned len) {
static const char* symbols = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
while (len --> 0) {
*dest++ = symbols[value % 32];
value /= 32;
}
}

/**
* Generates a random code.
* @param dest
* @param len The length of the code, should be event.
*/
void random_code(uint8_t* dest, unsigned len) {
unsigned value = HAL_RNG_GetRandomNumber();
bytesToCode(value, (char*)dest, len);
}



bool fetch_or_generate_setup_ssid(device_code_t* SSID) {
bool result = fetch_or_generate_ssid_prefix(SSID);
SSID->value[SSID->length++] = '-';
result |= fetch_or_generate_device_code(SSID);
return result;
}


/**
* Copies the device code to the destination, generating it if necessary.
* @param dest A buffer with room for at least 6 characters. The
* device code is copied here, without a null terminator.
* @return true if the device code was generated.
*/
bool fetch_or_generate_device_code(device_code_t* SSID) {
const uint8_t* suffix = (const uint8_t*)dct_read_app_data_lock(DCT_DEVICE_CODE_OFFSET);
int8_t c = (int8_t)*suffix; // check out first byte
bool generate = (!c || c<0);
uint8_t* dest = SSID->value+SSID->length;
uint8_t code_length = DEVICE_CODE_LEN;
if (generate) {
dct_read_app_data_unlock(DCT_DEVICE_CODE_OFFSET);
random_code(dest, DEVICE_CODE_LEN);
dct_write_app_data(dest, DCT_DEVICE_CODE_OFFSET, DEVICE_CODE_LEN);
}
else {
memcpy(dest, suffix, DEVICE_CODE_LEN);
code_length = strnlen((char*)dest, DEVICE_CODE_LEN);
dct_read_app_data_unlock(DCT_DEVICE_CODE_OFFSET);
}
SSID->length += code_length;

return generate;
}
50 changes: 50 additions & 0 deletions hal/src/stm32f2xx/device_code.h
@@ -0,0 +1,50 @@
/**
Copyright (c) 2017 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 <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* Variable String.
*/
typedef struct
{
uint8_t length;
uint8_t value[32];
} device_code_t;

/**
* Appends the device code to the end of the existing value. The length field must be set to the length of
* the current value.
*/
bool fetch_or_generate_device_code(device_code_t* value);
bool fetch_or_generate_setup_ssid(device_code_t* value);

/**
* This function is provided externally by the device HAL since the prefix is specific to the platform.
*/
extern bool fetch_or_generate_ssid_prefix(device_code_t* value);


#ifdef __cplusplus
}
#endif
4 changes: 3 additions & 1 deletion hal/src/stm32f2xx/ota_flash_hal_stm32f2xx.cpp
Expand Up @@ -586,7 +586,9 @@ int HAL_Set_System_Config(hal_system_config_t config_item, const void* data, uns
break;
case SYSTEM_CONFIG_SOFTAP_SUFFIX:
offset = DCT_DEVICE_CODE_OFFSET;
length = DCT_DEVICE_CODE_SIZE;
// copy the string with a zero terminator if less than the maximum width
data_length = std::min(DCT_DEVICE_CODE_SIZE, data_length+1);
length = data_length;
break;
case SYSTEM_CONFIG_SOFTAP_HOSTNAMES:
break;
Expand Down
16 changes: 7 additions & 9 deletions system/inc/system_setup.h
Expand Up @@ -71,7 +71,7 @@ template<typename Config> class SystemSetupConsole
{
public:
SystemSetupConsole(Config& config);
~SystemSetupConsole() = default;
~SystemSetupConsole();
virtual void loop(void);
protected:
virtual void exit()=0;
Expand All @@ -96,7 +96,11 @@ template<typename Config> class SystemSetupConsole

private:
USBSerial serial;

#if SETUP_OVER_SERIAL1
bool serial1Enabled;
uint8_t magicPos; // how far long the magic key we are
WiFiTester* tester;
#endif
};

#if Wiring_WiFi
Expand All @@ -107,7 +111,6 @@ class WiFiSetupConsole : public SystemSetupConsole<WiFiSetupConsoleConfig>
public:
WiFiSetupConsole(WiFiSetupConsoleConfig& config);
~WiFiSetupConsole();
virtual void loop() override;

protected:
virtual void handle(char c) override;
Expand All @@ -116,11 +119,6 @@ class WiFiSetupConsole : public SystemSetupConsole<WiFiSetupConsoleConfig>
virtual void cleanup() override;
#endif
private:
#if SETUP_OVER_SERIAL1
bool serial1Enabled;
uint8_t magicPos; // how far long the magic key we are
WiFiTester* tester;
#endif
char ssid[33];
char password[65];
char security_type_string[2];
Expand All @@ -145,7 +143,7 @@ class CellularSetupConsole : public SystemSetupConsole<CellularSetupConsoleConfi

public:
CellularSetupConsole(CellularSetupConsoleConfig& config);
~CellularSetupConsole() = default;
~CellularSetupConsole();

virtual void exit() override;
virtual void handle(char c) override;
Expand Down

0 comments on commit deb48b2

Please sign in to comment.