108 lines
2.8 KiB
C
108 lines
2.8 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
|
*
|
|
* The license for this file can be found in the file COPYING which you
|
|
* should have received along with this file.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
#include "./endpoint2_mqtt.h"
|
|
|
|
#include <gwenhywfar/endpoint2_msgio.h>
|
|
#include <gwenhywfar/debug.h>
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static int _getBytesNeededForMessage(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg);
|
|
static int _calcAndSetPayloadSizeAndOffset(GWEN_MSG *msg);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* implementations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
void AQH_MqttEndpoint2_Extend(GWEN_MSG_ENDPOINT2 *ep)
|
|
{
|
|
if (ep) {
|
|
GWEN_MsgIoEndpoint2_SetGetNeededBytesFn(ep, _getBytesNeededForMessage);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg)
|
|
{
|
|
uint32_t bytesInMsg;
|
|
|
|
bytesInMsg=GWEN_Msg_GetBytesInBuffer(msg);
|
|
if (bytesInMsg<2) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "Header not yet complete");
|
|
return (int) (2-bytesInMsg);
|
|
}
|
|
if (!(GWEN_Msg_GetFlags(msg) & GWEN_MSG_FLAGS_PAYLOADINFO_SET))
|
|
_calcAndSetPayloadSizeAndOffset(msg);
|
|
|
|
if (GWEN_Msg_GetFlags(msg) & GWEN_MSG_FLAGS_PAYLOADINFO_SET) {
|
|
uint32_t msgLength;
|
|
|
|
msgLength=GWEN_Msg_GetParsedPayloadOffset(msg)+GWEN_Msg_GetParsedPayloadSize(msg);
|
|
return (int)(msgLength-bytesInMsg);
|
|
}
|
|
else {
|
|
DBG_INFO(AQH_LOGDOMAIN, "Size field not complete, requesting another byte");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int _calcAndSetPayloadSizeAndOffset(GWEN_MSG *msg)
|
|
{
|
|
int remainingBytesInBuffer;
|
|
uint32_t mqttRemainingLength=0;
|
|
|
|
remainingBytesInBuffer=GWEN_Msg_GetBytesInBuffer(msg);
|
|
if (remainingBytesInBuffer>1) {
|
|
const uint8_t *ptr;
|
|
int idx;
|
|
int shift=0;
|
|
|
|
ptr=GWEN_Msg_GetConstBuffer(msg);
|
|
idx=1;
|
|
remainingBytesInBuffer--;
|
|
while(remainingBytesInBuffer) {
|
|
uint8_t len;
|
|
|
|
len=ptr[idx];
|
|
mqttRemainingLength+=(len & 0x7f)<<shift;
|
|
if (!(len & 0x80)) {
|
|
/* last byte of size */
|
|
GWEN_Msg_SetParsedPayloadSize(msg, mqttRemainingLength);
|
|
GWEN_Msg_SetParsedPayloadOffset(msg, idx+1); /* payload follows at next byte */
|
|
GWEN_Msg_AddFlags(msg, GWEN_MSG_FLAGS_PAYLOADINFO_SET);
|
|
return 1; /* size successfully determined */
|
|
}
|
|
shift+=7;
|
|
idx++;
|
|
remainingBytesInBuffer--;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|