in source/ota_mqtt.c [840:934]
OtaErr_t requestJob_Mqtt( OtaAgentContext_t * pAgentCtx )
{
/* This buffer is used to store the generated MQTT topic. The static size
* is calculated from the template and the corresponding parameters. */
char pJobTopic[ TOPIC_GET_NEXT_BUFFER_SIZE ];
/* The following buffer is big enough to hold a dynamically constructed
* $next/get job message. It contains a client token that is used to track
* how many requests have been made. */
char pMsg[ MSG_GET_NEXT_BUFFER_SIZE ];
static uint32_t reqCounter = 0;
OtaErr_t otaError = OtaErrRequestJobFailed;
OtaMqttStatus_t mqttStatus = OtaMqttSuccess;
uint32_t msgSize = 0;
uint16_t topicLen = 0;
/* NULL-terminated list of topic string parts. */
const char * pTopicParts[] =
{
MQTT_API_THINGS,
NULL, /* Thing Name not available at compile time, initialized below. */
MQTT_API_JOBS_NEXT_GET,
NULL
};
char reqCounterString[ U32_MAX_LEN + 1 ];
/* NULL-terminated list of payload parts */
/* NOTE: this must agree with pOtaGetNextJobMsgTemplate, do not add spaces, etc. */
const char * pPayloadParts[] =
{
"{\"clientToken\":\"",
NULL, /* Request counter string not available at compile time, initialized below. */
":",
NULL, /* Thing Name not available at compile time, initialized below. */
"\"}",
NULL
};
assert( pAgentCtx != NULL );
/* Suppress warnings about unused variables. */
( void ) pOtaJobsGetNextTopicTemplate;
( void ) pOtaGetNextJobMsgTemplate;
pTopicParts[ 1 ] = ( const char * ) pAgentCtx->pThingName;
pPayloadParts[ 1 ] = reqCounterString;
pPayloadParts[ 3 ] = ( const char * ) pAgentCtx->pThingName;
( void ) stringBuilderUInt32Decimal( reqCounterString, sizeof( reqCounterString ), reqCounter );
/* Subscribe to the OTA job notification topic. */
mqttStatus = subscribeToJobNotificationTopics( pAgentCtx );
if( mqttStatus == OtaMqttSuccess )
{
LogDebug( ( "MQTT job request number: counter=%u", reqCounter ) );
msgSize = ( uint32_t ) stringBuilder(
pMsg,
sizeof( pMsg ),
pPayloadParts );
/* The buffer is static and the size is calculated to fit. */
assert( ( msgSize > 0U ) && ( msgSize < sizeof( pMsg ) ) );
reqCounter++;
topicLen = ( uint16_t ) stringBuilder(
pJobTopic,
sizeof( pJobTopic ),
pTopicParts );
/* The buffer is static and the size is calculated to fit. */
assert( ( topicLen > 0U ) && ( topicLen < sizeof( pJobTopic ) ) );
mqttStatus = pAgentCtx->pOtaInterface->mqtt.publish( pJobTopic, topicLen, pMsg, msgSize, 1 );
if( mqttStatus == OtaMqttSuccess )
{
LogDebug( ( "Published MQTT request to get the next job: "
"topic=%s",
pJobTopic ) );
otaError = OtaErrNone;
}
else
{
LogError( ( "Failed to publish MQTT message:"
"publish returned error: "
"OtaMqttStatus_t=%s",
OTA_MQTT_strerror( mqttStatus ) ) );
}
}
return otaError;
}