Skip to content

Commit

Permalink
Merge pull request #2260 from particle-iot/content_len/ch64987
Browse files Browse the repository at this point in the history
Increase the maximum size of event data
  • Loading branch information
avtolstoy authored Mar 10, 2021
2 parents 696c377 + fe185a8 commit 5428b7b
Show file tree
Hide file tree
Showing 28 changed files with 547 additions and 109 deletions.
2 changes: 1 addition & 1 deletion build/platform-id.mk
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ PLATFORM=newhal
STM32_DEVICE=newhalcpu
# used to define the sources in hal/src/new-hal
PLATFORM_NAME=newhal
PLATFORM_GEN=60000
PLATFORM_GEN=0
# define MCU-specific platform defines under platform/MCU/new-hal
PLATFORM_MCU=newhal-mcu
PLATFORM_NET=not-defined
Expand Down
2 changes: 1 addition & 1 deletion communication/inc/communication_dynalib.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DYNALIB_FN(BASE_IDX + 2, communication, extract_public_ec_key, int(uint8_t*, siz
#define BASE_IDX2 (BASE_IDX + 1)
#endif

DYNALIB_FN(BASE_IDX2 + 0, communication, spark_protocol_set_connection_property, int(ProtocolFacade*, unsigned, unsigned, const particle::protocol::connection_properties_t*, void*))
DYNALIB_FN(BASE_IDX2 + 0, communication, spark_protocol_set_connection_property, int(ProtocolFacade*, unsigned, int, const void*, void*))
DYNALIB_FN(BASE_IDX2 + 1, communication, spark_protocol_command, int(ProtocolFacade*, ProtocolCommands::Enum, uint32_t, const void*))
DYNALIB_FN(BASE_IDX2 + 2, communication, spark_protocol_time_request_pending, bool(ProtocolFacade*, void*))
DYNALIB_FN(BASE_IDX2 + 3, communication, spark_protocol_time_last_synced, system_tick_t(ProtocolFacade*, time32_t*, time_t*))
Expand Down
32 changes: 24 additions & 8 deletions communication/inc/dtls_session_persist.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "stddef.h"

// The size of the persisted data
#define SessionPersistBaseSize 216
#define SessionPersistBaseSize 226

// variable size due to int/size_t members
#define SessionPersistVariableSize (sizeof(int)+sizeof(int)+sizeof(size_t))
Expand Down Expand Up @@ -84,7 +84,7 @@ struct __attribute__((packed)) SessionPersistData
uint8_t opaque_ssl[64+SessionPersistVariableSize+32+48+2+8+2];
#endif

/**
/**
* Checksum of the state of the subscriptions that have been sent to the cloud.
*/
uint32_t subscriptions_crc;
Expand All @@ -93,17 +93,33 @@ struct __attribute__((packed)) SessionPersistData
*/
uint32_t describe_app_crc;
/**
* Checksum of the system describe message.
*/
* Checksum of the system describe message.
*/
uint32_t describe_system_crc;
/**
* Protocol flags.
*/
* Protocol flags.
*/
uint32_t protocol_flags;
/**
* Application state flags (see the `AppStateDescriptor::StateFlag` enum).
*/
* Application state flags (see the `AppStateDescriptor::StateFlag` enum).
*/
uint32_t app_state_flags;
/**
* Maximum size of a firmware binary.
*/
uint32_t max_binary_size;
/**
* Module version of the system firmware.
*/
uint16_t system_version;
/**
* Maximum size of a CoAP message.
*/
uint16_t max_message_size;
/**
* Size of an OTA update chunk.
*/
uint16_t ota_chunk_size;
};

class __attribute__((packed)) SessionPersistOpaque : public SessionPersistData
Expand Down
40 changes: 38 additions & 2 deletions communication/inc/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

namespace particle
{

namespace protocol
{

const size_t DEFAULT_OTA_CHUNK_SIZE = 512;

/**
* Tie ALL the bits together.
*/
Expand Down Expand Up @@ -167,6 +170,21 @@ class Protocol
*/
token_t next_token;

/**
* Maximum size of a firmware binary.
*/
size_t max_binary_size;

/**
* Size of an OTA update chunk.
*/
size_t ota_chunk_size;

/**
* Module version of the system firmware.
*/
uint16_t system_version;

void set_protocol_flags(uint32_t flags)
{
protocol_flags = flags;
Expand All @@ -193,7 +211,7 @@ class Protocol
*/
ProtocolError hello_response();

virtual size_t build_hello(Message& message, uint8_t flags)=0;
virtual size_t build_hello(Message& message, uint16_t flags) = 0;

/**
* Send a Ping message over the channel.
Expand Down Expand Up @@ -327,7 +345,10 @@ class Protocol
publisher(this),
last_ack_handlers_update(0),
protocol_flags(0),
initialized(false)
initialized(false),
max_binary_size(0), // Unlimited
ota_chunk_size(DEFAULT_OTA_CHUNK_SIZE),
system_version(0) // Unknown
{
}

Expand Down Expand Up @@ -363,6 +384,21 @@ class Protocol
protocol_flags |= ProtocolFlag::COMPRESSED_OTA;
}

void set_system_version(uint16_t version)
{
system_version = version;
}

void set_max_binary_size(size_t size)
{
max_binary_size = size;
}

void set_ota_chunk_size(size_t size)
{
ota_chunk_size = size;
}

void set_handlers(CommunicationsHandlers& handlers)
{
copy_and_init(&this->handlers, sizeof(this->handlers), &handlers, handlers.size);
Expand Down
123 changes: 108 additions & 15 deletions communication/inc/protocol_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,42 @@ const size_t MISSED_CHUNKS_TO_SEND = 20;
const size_t MISSED_CHUNKS_TO_SEND = 40;
#endif

const size_t MINIMUM_CHUNK_INCREASE = 2u;
const size_t MAX_EVENT_TTL_SECONDS = 16777215;
const size_t MAX_OPTION_DELTA_LENGTH = 12;
const size_t MAX_FUNCTION_ARG_LENGTH = 622;
const size_t MINIMUM_CHUNK_INCREASE = 2u;
const size_t MAX_EVENT_TTL_SECONDS = 16777215;
const size_t MAX_OPTION_DELTA_LENGTH = 12;
const size_t MAX_FUNCTION_KEY_LENGTH = 64;
const size_t MAX_VARIABLE_KEY_LENGTH = 64;
const size_t MAX_EVENT_NAME_LENGTH = 64;
const size_t MAX_EVENT_DATA_LENGTH = 622;
const size_t MAX_EVENT_NAME_LENGTH = 64;

#if HAL_PLATFORM_GEN >= 3
const size_t MAX_EVENT_DATA_LENGTH = 1024;
const size_t MAX_FUNCTION_ARG_LENGTH = 1024;
const size_t MAX_VARIABLE_VALUE_LENGTH = 1024;
#else
const size_t MAX_EVENT_DATA_LENGTH = 864;
const size_t MAX_FUNCTION_ARG_LENGTH = 864;
const size_t MAX_VARIABLE_VALUE_LENGTH = 864;
#endif

// Timeout in milliseconds given to receive an acknowledgement for a published event
const unsigned SEND_EVENT_ACK_TIMEOUT = 20000;

/**
* Maximum possible size of a CoAP message carrying a cloud event.
*/
const size_t MAX_EVENT_MESSAGE_SIZE = 4 /* Header */ + 1 /* Token */ + (MAX_EVENT_NAME_LENGTH + 5) /* Uri-Path options */
+ 5 /* Max-Age option */ + 1 /* Payload marker */ + MAX_EVENT_DATA_LENGTH /* Payload data */;
/**
* Maximum possible size of a CoAP message carrying a function call.
*/
const size_t MAX_FUNCTION_CALL_MESSAGE_SIZE = 4 /* Header */ + 1 /* Token */ + (MAX_FUNCTION_KEY_LENGTH + 5) /* Uri-Path options */
+ (MAX_FUNCTION_ARG_LENGTH + 3) /* Uri-Query option */;
/**
* Maximum possible size of a CoAP message carrying a variable value.
*/
const size_t MAX_VARIABLE_VALUE_MESSAGE_SIZE = 4 /* Header */ + 1 /* Token */ + 1 /* Payload marker */ +
MAX_VARIABLE_VALUE_LENGTH /* Payload data */;

#ifndef PROTOCOL_BUFFER_SIZE
#define PROTOCOL_BUFFER_SIZE MBEDTLS_SSL_MAX_CONTENT_LEN
#endif
Expand All @@ -105,11 +129,11 @@ namespace ChunkReceivedCode {
}

enum DescriptionType {
DESCRIBE_SYSTEM = 1<<0, // modules
DESCRIBE_APPLICATION = 1<<1, // functions and variables
DESCRIBE_METRICS = 1<<2, // metrics/diagnostics
DESCRIBE_SYSTEM = 1<<0, // modules
DESCRIBE_APPLICATION = 1<<1, // functions and variables
DESCRIBE_METRICS = 1<<2, // metrics/diagnostics
DESCRIBE_DEFAULT = DESCRIBE_SYSTEM | DESCRIBE_APPLICATION,
DESCRIBE_MAX = (1<<3)-1
DESCRIBE_MAX = (1<<3)-1
};

namespace Connection
Expand All @@ -123,7 +147,10 @@ enum Enum
PING = 0, ///< Set keepalive interval.
FAST_OTA = 1, ///< Enable/disable fast OTA.
DEVICE_INITIATED_DESCRIBE = 2, ///< Enable device-initiated describe messages.
COMPRESSED_OTA = 3 ///< Enable support for compressed/combined OTA updates.
COMPRESSED_OTA = 3, ///< Enable support for compressed/combined OTA updates.
SYSTEM_MODULE_VERSION = 4, ///< Module version of the system firmware.
MAX_BINARY_SIZE = 5, ///< Maximum size of a firmware binary.
OTA_CHUNK_SIZE = 6 ///< Size of an OTA update chunk.
};

}
Expand Down Expand Up @@ -172,7 +199,12 @@ class AppStateDescriptor {
APP_DESCRIBE_CRC = 0x02, ///< Checksum of the application description.
SUBSCRIPTIONS_CRC = 0x04, ///< Checksum of the event subscriptions.
PROTOCOL_FLAGS = 0x08, ///< Protocol flags.
ALL = SYSTEM_DESCRIBE_CRC | APP_DESCRIBE_CRC | SUBSCRIPTIONS_CRC | PROTOCOL_FLAGS ///< All the defined fields.
SYSTEM_MODULE_VERSION = 0x10, ///< Module version of the system firmware.
MAX_MESSAGE_SIZE = 0x20, ///< Maximum size of a CoAP message.
MAX_BINARY_SIZE = 0x40, ///< Maximum size of a firmware binary.
OTA_CHUNK_SIZE = 0x80, ///< Size of an OTA update chunk.
ALL = SYSTEM_DESCRIBE_CRC | APP_DESCRIBE_CRC | SUBSCRIPTIONS_CRC | PROTOCOL_FLAGS | SYSTEM_MODULE_VERSION |
MAX_MESSAGE_SIZE | MAX_BINARY_SIZE | OTA_CHUNK_SIZE ///< All the defined fields.
};

/**
Expand All @@ -184,13 +216,27 @@ class AppStateDescriptor {
* @param subscrCrc Checksum of the event subscriptions.
* @param protocolFlags Protocol flags.
*/
explicit AppStateDescriptor(uint32_t stateFlags = 0, uint32_t systemDescrCrc = 0, uint32_t appDescrCrc = 0,
uint32_t subscrCrc = 0, uint32_t protocolFlags = 0) :
explicit AppStateDescriptor(uint32_t stateFlags = 0, uint16_t systemVersion = 0, uint32_t systemDescrCrc = 0,
uint32_t appDescrCrc = 0, uint32_t subscrCrc = 0, uint32_t protocolFlags = 0, uint16_t maxMessageSize = 0,
uint32_t maxBinarySize = 0, uint16_t otaChunkSize = 0) :
stateFlags_(stateFlags),
systemDescrCrc_(systemDescrCrc),
appDescrCrc_(appDescrCrc),
subscrCrc_(subscrCrc),
protocolFlags_(protocolFlags) {
protocolFlags_(protocolFlags),
maxBinarySize_(maxBinarySize),
systemVersion_(systemVersion),
maxMessageSize_(maxMessageSize),
otaChunkSize_(otaChunkSize) {
}

/**
* Set the module version of the system firmware.
*/
AppStateDescriptor& systemVersion(uint16_t version) {
systemVersion_ = version;
stateFlags_ |= StateFlag::SYSTEM_MODULE_VERSION;
return *this;
}

/**
Expand Down Expand Up @@ -229,6 +275,33 @@ class AppStateDescriptor {
return *this;
}

/**
* Set the maximum size of a CoAP message.
*/
AppStateDescriptor& maxMessageSize(uint16_t size) {
maxMessageSize_ = size;
stateFlags_ |= StateFlag::MAX_MESSAGE_SIZE;
return *this;
}

/**
* Set the maximum size of a firmware binary.
*/
AppStateDescriptor& maxBinarySize(uint32_t size) {
maxBinarySize_ = size;
stateFlags_ |= StateFlag::MAX_BINARY_SIZE;
return *this;
}

/**
* Set the size of an OTA update chunk.
*/
AppStateDescriptor& otaChunkSize(uint16_t size) {
otaChunkSize_ = size;
stateFlags_ |= StateFlag::OTA_CHUNK_SIZE;
return *this;
}

/**
* Returns `true` if `other` is equal to this descriptor, otherwise returns `false`.
*
Expand All @@ -238,6 +311,10 @@ class AppStateDescriptor {
if ((stateFlags_ & flags) != (other.stateFlags_ & flags)) {
return false;
}
if ((flags & StateFlag::SYSTEM_MODULE_VERSION) && (stateFlags_ & StateFlag::SYSTEM_MODULE_VERSION) &&
(systemVersion_ != other.systemVersion_)) {
return false;
}
if ((flags & StateFlag::SUBSCRIPTIONS_CRC) && (stateFlags_ & StateFlag::SUBSCRIPTIONS_CRC) &&
(subscrCrc_ != other.subscrCrc_)) {
return false;
Expand All @@ -254,6 +331,18 @@ class AppStateDescriptor {
(protocolFlags_ != other.protocolFlags_)) {
return false;
}
if ((flags & StateFlag::MAX_MESSAGE_SIZE) && (stateFlags_ & StateFlag::MAX_MESSAGE_SIZE) &&
(maxMessageSize_ != other.maxMessageSize_)) {
return false;
}
if ((flags & StateFlag::MAX_BINARY_SIZE) && (stateFlags_ & StateFlag::MAX_BINARY_SIZE) &&
(maxBinarySize_ != other.maxBinarySize_)) {
return false;
}
if ((flags & StateFlag::OTA_CHUNK_SIZE) && (stateFlags_ & StateFlag::OTA_CHUNK_SIZE) &&
(otaChunkSize_ != other.otaChunkSize_)) {
return false;
}
return true;
}

Expand All @@ -279,6 +368,10 @@ class AppStateDescriptor {
uint32_t appDescrCrc_;
uint32_t subscrCrc_;
uint32_t protocolFlags_;
uint32_t maxBinarySize_;
uint16_t systemVersion_;
uint16_t maxMessageSize_;
uint16_t otaChunkSize_;
};

}} // namespace particle::protocol
6 changes: 5 additions & 1 deletion communication/inc/spark_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ namespace SparkAppStateSelector {
DESCRIBE_SYSTEM = 1,
SUBSCRIPTIONS = 2,
PROTOCOL_FLAGS = 3,
ALL = 4
ALL = 4,
SYSTEM_MODULE_VERSION = 5,
MAX_MESSAGE_SIZE = 6,
MAX_BINARY_SIZE = 7,
OTA_CHUNK_SIZE = 8
};
}

Expand Down
2 changes: 1 addition & 1 deletion communication/inc/spark_protocol_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void spark_protocol_set_product_id(ProtocolFacade* protocol, product_id_t produc
void spark_protocol_set_product_firmware_version(ProtocolFacade* protocol, product_firmware_version_t product_firmware_version, unsigned int param=0, void* reserved = NULL);
void spark_protocol_get_product_details(ProtocolFacade* protocol, product_details_t* product_details, void* reserved=NULL);

int spark_protocol_set_connection_property(ProtocolFacade* protocol, unsigned property_id, unsigned data, const particle::protocol::connection_properties_t* conn_prop, void* reserved);
int spark_protocol_set_connection_property(ProtocolFacade* protocol, unsigned property, int value, const void* data, void* reserved);
bool spark_protocol_time_request_pending(ProtocolFacade* protocol, void* reserved=NULL);
system_tick_t spark_protocol_time_last_synced(ProtocolFacade* protocol, time32_t* tm32, time_t* tm);

Expand Down
3 changes: 2 additions & 1 deletion communication/src/dtls_message_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ AppStateDescriptor SessionPersist::app_state_descriptor()
if (!is_valid()) {
return AppStateDescriptor();
}
return AppStateDescriptor(app_state_flags, describe_system_crc, describe_app_crc, subscriptions_crc, protocol_flags);
return AppStateDescriptor(app_state_flags, system_version, describe_system_crc, describe_app_crc, subscriptions_crc,
protocol_flags, max_message_size, max_binary_size, ota_chunk_size);
}

SessionPersist sessionPersist;
Expand Down
Loading

0 comments on commit 5428b7b

Please sign in to comment.