Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1357 from spark/feature/ch3737-6-digit-serial
Feature/ch3737 6 digit serial [0.7.0-rc.2]
- Loading branch information
Showing
14 changed files
with
376 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.