Skip to content

Updates for clean build with latest ESP-IDF v5.4 #128

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
144 changes: 2 additions & 142 deletions main/demo_tasks/ota_over_mqtt_demo/ota_over_mqtt_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,6 @@ static OtaMqttStatus_t prvMQTTSubscribe( const char * pTopicFilter,
uint16_t topicFilterLength,
uint8_t ucQoS );

/**
* @brief Function is used by OTA agent to unsubscribe a topicfilter from MQTT broker.
*
* The implementation queues an UNSUBSCRIBE request for the topic filter with the MQTT agent. It then waits
* for a successful completion of the request from the agent. Notification along with results of
* operation is sent using xTaskNotify API to the caller task. MQTT agent also removes the topic filter
* subscription from its memory so any future
* packets on this topic will not be routed to the OTA agent.
*
* @param[in] pTopicFilter Topic filter to be unsubscribed.
* @param[in] topicFilterLength Length of the topic filter.
* @param[in] ucQos Qos value for the topic.
* @return OtaMqttSuccess if successful. Appropriate error code otherwise.
*
*/
static OtaMqttStatus_t prvMQTTUnsubscribe( const char * pTopicFilter,
uint16_t topicFilterLength,
uint8_t ucQoS );

/**
* @brief The function which runs the OTA demo task.
*
Expand All @@ -319,21 +300,6 @@ static OtaMqttStatus_t prvMQTTUnsubscribe( const char * pTopicFilter,
*/
static void prvOTADemoTask( void * pvParam );

/**
* @brief Matches a client identifier within an OTA topic.
* This function is used to validate that topic is valid and intended for this device thing name.
*
* @param[in] pTopic Pointer to the topic
* @param[in] topicNameLength length of the topic
* @param[in] pClientIdentifier Client identifier, should be null terminated.
* @param[in] clientIdentifierLength Length of the client identifier.
* @return true if client identifier is found within the topic at the right index.
*/
static bool prvMatchClientIdentifierInTopic( const char * pTopic,
size_t topicNameLength,
const char * pClientIdentifier,
size_t clientIdentifierLength );

/**
* @brief Suspends the OTA agent.
*/
Expand All @@ -356,39 +322,6 @@ static void prvCoreMqttAgentEventHandler( void * pvHandlerArg,

/* Static function definitions ************************************************/

static bool prvMatchClientIdentifierInTopic( const char * pTopic,
size_t topicNameLength,
const char * pClientIdentifier,
size_t clientIdentifierLength )
{
bool isMatch = false;
size_t idx, matchIdx = 0;

for( idx = OTA_TOPIC_CLIENT_IDENTIFIER_START_IDX; idx < topicNameLength; idx++ )
{
if( matchIdx == clientIdentifierLength )
{
if( pTopic[ idx ] == '/' )
{
isMatch = true;
}

break;
}
else
{
if( pClientIdentifier[ matchIdx ] != pTopic[ idx ] )
{
break;
}
}

matchIdx++;
}

return isMatch;
}

static void prvCommandCallback( MQTTAgentCommandContext_t * pCommandContext,
MQTTAgentReturnInfo_t * pxReturnInfo )
{
Expand Down Expand Up @@ -534,80 +467,6 @@ static OtaMqttStatus_t prvMQTTPublish( const char * const pacTopic,
return otaRet;
}

static OtaMqttStatus_t prvMQTTUnsubscribe( const char * pTopicFilter,
uint16_t topicFilterLength,
uint8_t ucQoS )
{
MQTTStatus_t mqttStatus;
uint32_t ulNotifiedValue;
MQTTAgentSubscribeArgs_t xSubscribeArgs = { 0 };
MQTTSubscribeInfo_t xSubscribeInfo = { 0 };
BaseType_t result;
MQTTAgentCommandInfo_t xCommandParams = { 0 };
MQTTAgentCommandContext_t xApplicationDefinedContext = { 0 };
OtaMqttStatus_t otaRet = OtaMqttSuccess;

configASSERT( pTopicFilter != NULL );
configASSERT( topicFilterLength > 0 );

xSubscribeInfo.pTopicFilter = pTopicFilter;
xSubscribeInfo.topicFilterLength = topicFilterLength;
xSubscribeInfo.qos = ucQoS;
xSubscribeArgs.pSubscribeInfo = &xSubscribeInfo;
xSubscribeArgs.numSubscriptions = 1;


xApplicationDefinedContext.xTaskToNotify = xTaskGetCurrentTaskHandle();

xCommandParams.blockTimeMs = otademoconfigMQTT_TIMEOUT_MS;
xCommandParams.cmdCompleteCallback = prvCommandCallback;
xCommandParams.pCmdCompleteCallbackContext = ( void * ) &xApplicationDefinedContext;

ESP_LOGI( TAG, "Unsubscribing to topic filter: %s", pTopicFilter );
xTaskNotifyStateClear( NULL );


mqttStatus = MQTTAgent_Unsubscribe( &xGlobalMqttAgentContext,
&xSubscribeArgs,
&xCommandParams );

/* Wait for command to complete so MQTTSubscribeInfo_t remains in scope for the
* duration of the command. */
if( mqttStatus == MQTTSuccess )
{
result = xTaskNotifyWait( 0, MAX_UINT32, &ulNotifiedValue, portMAX_DELAY );

if( result == pdTRUE )
{
mqttStatus = xApplicationDefinedContext.xReturnStatus;
}
else
{
mqttStatus = MQTTRecvFailed;
}
}

if( mqttStatus != MQTTSuccess )
{
ESP_LOGE( TAG, "Failed to UNSUBSCRIBE from topic %.*s with error = %u.",
topicFilterLength,
pTopicFilter,
mqttStatus );

otaRet = OtaMqttUnsubscribeFailed;
}
else
{
ESP_LOGI( TAG, "UNSUBSCRIBED from topic %.*s.\n\n",
topicFilterLength,
pTopicFilter );

otaRet = OtaMqttSuccess;
}

return otaRet;
}

/*-----------------------------------------------------------*/

static void requestJobDocumentHandler( void )
Expand Down Expand Up @@ -825,7 +684,7 @@ static OtaPalJobDocProcessingResult_t receivedJobDocumentHandler( OtaJobEventDat
{
bool parseJobDocument = false;
bool handled = false;
char * jobId;
const char * jobId;
const char ** jobIdptr = &jobId;
size_t jobIdLength = 0U;
OtaPalStatus_t palStatus;
Expand Down Expand Up @@ -1304,6 +1163,7 @@ static void processOTAEvents( void )
case OtaAgentEventRequestFileBlock:
case OtaAgentEventReceivedFileBlock:
nextEvent.eventId = OtaAgentEventRequestFileBlock;
break;

case OtaAgentEventCloseFile:
nextEvent.eventId = OtaAgentEventActivateImage;
Expand Down
10 changes: 8 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ static BaseType_t prvInitializeNetworkContext( void )
/* This is used to store the error return of ESP-IDF functions. */
esp_err_t xEspErrRet;

/* This is used as a temporary buffer for anything */
char *variableBuffer = NULL;

/* Verify that the MQTT endpoint and thing name have been configured by the
* user. */
if( strlen( CONFIG_GRI_MQTT_ENDPOINT ) == 0 )
Expand All @@ -155,8 +158,9 @@ static BaseType_t prvInitializeNetworkContext( void )

/* Get the device certificate from esp_secure_crt_mgr and put into network
* context. */
xEspErrRet = esp_secure_cert_get_device_cert( &xNetworkContext.pcClientCert,
xEspErrRet = esp_secure_cert_get_device_cert( &variableBuffer,
&xNetworkContext.pcClientCertSize );
xNetworkContext.pcClientCert = variableBuffer;

if( xEspErrRet == ESP_OK )
{
Expand Down Expand Up @@ -208,8 +212,10 @@ static BaseType_t prvInitializeNetworkContext( void )
xRet = pdFAIL;
}
#else /* if CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL */
xEspErrRet = esp_secure_cert_get_priv_key( &xNetworkContext.pcClientKey,
xEspErrRet = esp_secure_cert_get_priv_key( &variableBuffer,
&xNetworkContext.pcClientKeySize );

xNetworkContext.pcClientKey = variableBuffer;

if( xEspErrRet == ESP_OK )
{
Expand Down
Loading