1
0
mirror of https://github.com/FreeRTOS/coreMQTT synced 2025-06-09 12:49:24 +08:00
coreMQTT/v2.3.1/mqtt_serializepublishheader_function.html

186 lines
17 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>coreMQTT: MQTT_SerializePublishHeader</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">coreMQTT<span id="projectnumber">&#160;v2.3.1</span>
</div>
<div id="projectbrief">MQTT 3.1.1 Client Library</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('mqtt_serializepublishheader_function.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">MQTT_SerializePublishHeader </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="fragment"><div class="line"><a class="code hl_enumeration" href="group__mqtt__enum__types.html#gaba7ec045874a1c3432f99173367f735c">MQTTStatus_t</a> <a class="code hl_function" href="core__mqtt__serializer_8h.html#a659cf2bcaf2c5131daa0acc1917401a2">MQTT_SerializePublishHeader</a>( <span class="keyword">const</span> <a class="code hl_struct" href="struct_m_q_t_t_publish_info__t.html">MQTTPublishInfo_t</a> * pPublishInfo,</div>
<div class="line"> uint16_t packetId,</div>
<div class="line"> <span class="keywordtype">size_t</span> remainingLength,</div>
<div class="line"> <span class="keyword">const</span> <a class="code hl_struct" href="struct_m_q_t_t_fixed_buffer__t.html">MQTTFixedBuffer_t</a> * pFixedBuffer,</div>
<div class="line"> <span class="keywordtype">size_t</span> * pHeaderSize );</div>
<div class="ttc" id="acore__mqtt__serializer_8h_html_a659cf2bcaf2c5131daa0acc1917401a2"><div class="ttname"><a href="core__mqtt__serializer_8h.html#a659cf2bcaf2c5131daa0acc1917401a2">MQTT_SerializePublishHeader</a></div><div class="ttdeci">MQTTStatus_t MQTT_SerializePublishHeader(const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer, size_t *pHeaderSize)</div><div class="ttdoc">Serialize an MQTT PUBLISH packet header in the given buffer.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.c:2183</div></div>
<div class="ttc" id="agroup__mqtt__enum__types_html_gaba7ec045874a1c3432f99173367f735c"><div class="ttname"><a href="group__mqtt__enum__types.html#gaba7ec045874a1c3432f99173367f735c">MQTTStatus_t</a></div><div class="ttdeci">MQTTStatus_t</div><div class="ttdoc">Return codes from MQTT functions.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:87</div></div>
<div class="ttc" id="astruct_m_q_t_t_fixed_buffer__t_html"><div class="ttname"><a href="struct_m_q_t_t_fixed_buffer__t.html">MQTTFixedBuffer_t</a></div><div class="ttdoc">Buffer passed to MQTT library.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:123</div></div>
<div class="ttc" id="astruct_m_q_t_t_publish_info__t_html"><div class="ttname"><a href="struct_m_q_t_t_publish_info__t.html">MQTTPublishInfo_t</a></div><div class="ttdoc">MQTT PUBLISH packet parameters.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:202</div></div>
</div><!-- fragment --><p>Serialize an MQTT PUBLISH packet header in the given buffer. </p>
<p>This function serializes PUBLISH header in to the given buffer. The payload for PUBLISH will not be copied over to the buffer. This will help reduce the memory needed for the buffer and avoid an unwanted copy operation of the PUBLISH payload into the buffer. If the payload also would need to be part of the serialized buffer, consider using <a class="el" href="core__mqtt__serializer_8h.html#ac6c453f9c4b7f48aad511bc677ec7eb9" title="Serialize an MQTT PUBLISH packet in the given buffer.">MQTT_SerializePublish</a>.</p>
<p><a class="el" href="core__mqtt__serializer_8h.html#a9971fb98c6af22b1bfe697d44492a819" title="Get the packet size and remaining length of an MQTT PUBLISH packet.">MQTT_GetPublishPacketSize</a> should be called with <code>pPublishInfo</code> before invoking this function to get the size of the required <a class="el" href="struct_m_q_t_t_fixed_buffer__t.html" title="Buffer passed to MQTT library.">MQTTFixedBuffer_t</a> and <code>remainingLength</code>. The <code>remainingLength</code> must be the same as returned by <a class="el" href="core__mqtt__serializer_8h.html#a9971fb98c6af22b1bfe697d44492a819" title="Get the packet size and remaining length of an MQTT PUBLISH packet.">MQTT_GetPublishPacketSize</a>. The <a class="el" href="struct_m_q_t_t_fixed_buffer__t.html" title="Buffer passed to MQTT library.">MQTTFixedBuffer_t</a> must be at least as large as the size returned by <a class="el" href="core__mqtt__serializer_8h.html#a9971fb98c6af22b1bfe697d44492a819" title="Get the packet size and remaining length of an MQTT PUBLISH packet.">MQTT_GetPublishPacketSize</a>.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">pPublishInfo</td><td>MQTT PUBLISH packet parameters. </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">packetId</td><td>packet ID generated by <a class="el" href="core__mqtt_8h.html#a00e1a3eba2c21899a6b4312c7d65d090" title="Get a packet ID that is valid according to the MQTT 3.1.1 spec.">MQTT_GetPacketId</a>. </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">remainingLength</td><td>Remaining Length provided by <a class="el" href="core__mqtt__serializer_8h.html#a9971fb98c6af22b1bfe697d44492a819" title="Get the packet size and remaining length of an MQTT PUBLISH packet.">MQTT_GetPublishPacketSize</a>. </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">pFixedBuffer</td><td>Buffer for packet serialization. </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">pHeaderSize</td><td>Size of the serialized MQTT PUBLISH header.</td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><a class="el" href="group__mqtt__enum__types.html#ggaba7ec045874a1c3432f99173367f735cab1be4db832a0468f024243bca151a8df">MQTTNoMemory</a> if pFixedBuffer is too small to hold the MQTT packet; <a class="el" href="group__mqtt__enum__types.html#ggaba7ec045874a1c3432f99173367f735caa39030c93b0263b2699502a074f003b5">MQTTBadParameter</a> if invalid parameters are passed; <a class="el" href="group__mqtt__enum__types.html#ggaba7ec045874a1c3432f99173367f735ca484e062cb4f3fccc1858dd25cfeee056">MQTTSuccess</a> otherwise.</dd></dl>
<p><b>Example</b> </p><div class="fragment"><div class="line"><span class="comment">// Variables used in this example.</span></div>
<div class="line"><a class="code hl_enumeration" href="group__mqtt__enum__types.html#gaba7ec045874a1c3432f99173367f735c">MQTTStatus_t</a> status;</div>
<div class="line"><a class="code hl_struct" href="struct_m_q_t_t_publish_info__t.html">MQTTPublishInfo_t</a> publishInfo = { 0 };</div>
<div class="line"><a class="code hl_struct" href="struct_m_q_t_t_fixed_buffer__t.html">MQTTFixedBuffer_t</a> fixedBuffer;</div>
<div class="line">uint8_t buffer[ BUFFER_SIZE ];</div>
<div class="line"><span class="keywordtype">size_t</span> remainingLength = 0, packetSize = 0, headerSize = 0;</div>
<div class="line">uint16_t packetId;</div>
<div class="line">int32_t bytesSent;</div>
<div class="line"> </div>
<div class="line">fixedBuffer.<a class="code hl_variable" href="struct_m_q_t_t_fixed_buffer__t.html#acea147448e044870fb36b7fa2347dbd6">pBuffer</a> = buffer;</div>
<div class="line">fixedBuffer.<a class="code hl_variable" href="struct_m_q_t_t_fixed_buffer__t.html#a0b0b6a93cc62751ebeb03095d5431636">size</a> = BUFFER_SIZE;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// A packet identifier is unused for QoS 0 publishes. Otherwise, a valid, unused packet</span></div>
<div class="line"><span class="comment">// identifier must be used.</span></div>
<div class="line">packetId = 0;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Assume publishInfo has been initialized. Get the publish packet size.</span></div>
<div class="line">status = <a class="code hl_function" href="core__mqtt__serializer_8h.html#a9971fb98c6af22b1bfe697d44492a819">MQTT_GetPublishPacketSize</a>(</div>
<div class="line"> &amp;publishInfo, &amp;remainingLength, &amp;packetSize</div>
<div class="line">);</div>
<div class="line">assert( status == <a class="code hl_enumvalue" href="group__mqtt__enum__types.html#ggaba7ec045874a1c3432f99173367f735ca484e062cb4f3fccc1858dd25cfeee056">MQTTSuccess</a> );</div>
<div class="line"><span class="comment">// The payload will not be serialized, so the the fixed buffer does not need to hold it.</span></div>
<div class="line">assert( ( packetSize - publishInfo.<a class="code hl_variable" href="struct_m_q_t_t_publish_info__t.html#a7997964e11571f35f0c3b729db0f760f">payloadLength</a> ) &lt;= BUFFER_SIZE );</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Serialize the publish packet header into the fixed buffer.</span></div>
<div class="line">status = <a class="code hl_function" href="core__mqtt__serializer_8h.html#a659cf2bcaf2c5131daa0acc1917401a2">MQTT_SerializePublishHeader</a>(</div>
<div class="line"> &amp;publishInfo,</div>
<div class="line"> packetId,</div>
<div class="line"> remainingLength,</div>
<div class="line"> &amp;fixedBuffer,</div>
<div class="line"> &amp;headerSize</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line"><span class="keywordflow">if</span>( status == <a class="code hl_enumvalue" href="group__mqtt__enum__types.html#ggaba7ec045874a1c3432f99173367f735ca484e062cb4f3fccc1858dd25cfeee056">MQTTSuccess</a> )</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// The publish header and payload can now be sent to the broker.</span></div>
<div class="line"> <span class="comment">// mqttSocket here is a socket descriptor created and connected to the MQTT</span></div>
<div class="line"> <span class="comment">// broker outside of this function.</span></div>
<div class="line"> bytesSent = send( mqttSocket, ( <span class="keywordtype">void</span> * ) fixedBuffer.<a class="code hl_variable" href="struct_m_q_t_t_fixed_buffer__t.html#acea147448e044870fb36b7fa2347dbd6">pBuffer</a>, headerSize, 0 );</div>
<div class="line"> assert( bytesSent == headerSize );</div>
<div class="line"> bytesSent = send( mqttSocket, publishInfo.<a class="code hl_variable" href="struct_m_q_t_t_publish_info__t.html#afc28299f4f625f5e674bb61b42f03380">pPayload</a>, publishInfo.<a class="code hl_variable" href="struct_m_q_t_t_publish_info__t.html#a7997964e11571f35f0c3b729db0f760f">payloadLength</a>, 0 );</div>
<div class="line"> assert( bytesSent == publishInfo.<a class="code hl_variable" href="struct_m_q_t_t_publish_info__t.html#a7997964e11571f35f0c3b729db0f760f">payloadLength</a> );</div>
<div class="line">}</div>
<div class="ttc" id="acore__mqtt__serializer_8h_html_a9971fb98c6af22b1bfe697d44492a819"><div class="ttname"><a href="core__mqtt__serializer_8h.html#a9971fb98c6af22b1bfe697d44492a819">MQTT_GetPublishPacketSize</a></div><div class="ttdeci">MQTTStatus_t MQTT_GetPublishPacketSize(const MQTTPublishInfo_t *pPublishInfo, size_t *pRemainingLength, size_t *pPacketSize)</div><div class="ttdoc">Get the packet size and remaining length of an MQTT PUBLISH packet.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.c:2057</div></div>
<div class="ttc" id="agroup__mqtt__enum__types_html_ggaba7ec045874a1c3432f99173367f735ca484e062cb4f3fccc1858dd25cfeee056"><div class="ttname"><a href="group__mqtt__enum__types.html#ggaba7ec045874a1c3432f99173367f735ca484e062cb4f3fccc1858dd25cfeee056">MQTTSuccess</a></div><div class="ttdeci">@ MQTTSuccess</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:88</div></div>
<div class="ttc" id="astruct_m_q_t_t_fixed_buffer__t_html_a0b0b6a93cc62751ebeb03095d5431636"><div class="ttname"><a href="struct_m_q_t_t_fixed_buffer__t.html#a0b0b6a93cc62751ebeb03095d5431636">MQTTFixedBuffer_t::size</a></div><div class="ttdeci">size_t size</div><div class="ttdoc">Size of buffer.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:125</div></div>
<div class="ttc" id="astruct_m_q_t_t_fixed_buffer__t_html_acea147448e044870fb36b7fa2347dbd6"><div class="ttname"><a href="struct_m_q_t_t_fixed_buffer__t.html#acea147448e044870fb36b7fa2347dbd6">MQTTFixedBuffer_t::pBuffer</a></div><div class="ttdeci">uint8_t * pBuffer</div><div class="ttdoc">Pointer to buffer.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:124</div></div>
<div class="ttc" id="astruct_m_q_t_t_publish_info__t_html_a7997964e11571f35f0c3b729db0f760f"><div class="ttname"><a href="struct_m_q_t_t_publish_info__t.html#a7997964e11571f35f0c3b729db0f760f">MQTTPublishInfo_t::payloadLength</a></div><div class="ttdeci">size_t payloadLength</div><div class="ttdoc">Message payload length.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:236</div></div>
<div class="ttc" id="astruct_m_q_t_t_publish_info__t_html_afc28299f4f625f5e674bb61b42f03380"><div class="ttname"><a href="struct_m_q_t_t_publish_info__t.html#afc28299f4f625f5e674bb61b42f03380">MQTTPublishInfo_t::pPayload</a></div><div class="ttdeci">const void * pPayload</div><div class="ttdoc">Message payload.</div><div class="ttdef"><b>Definition:</b> core_mqtt_serializer.h:231</div></div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6 </li>
</ul>
</div>
</body>
</html>