Files
aqhomecontrol/aqhome/msg_value.c

122 lines
3.2 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 "aqhome/msg_value.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/list.h>
#include <gwenhywfar/error.h>
#include <gwenhywfar/debug.h>
uint32_t AQH_MsgValue_GetTimestamp(const AQH_MSG *msg)
{
if ((AQH_Msg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) &&
(AQH_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) {
const uint8_t *ptr;
ptr=AQH_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_TIMESTAMP;
return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24);
}
return 0;
}
uint8_t AQH_MsgValue_GetValueId(const AQH_MSG *msg)
{
if ((AQH_Msg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) &&
(AQH_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) {
const uint8_t *ptr;
ptr=AQH_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_VALUEID;
return ptr[0];
}
return 0;
}
uint8_t AQH_MsgValue_GetValueType(const AQH_MSG *msg)
{
if ((AQH_Msg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) &&
(AQH_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) {
const uint8_t *ptr;
ptr=AQH_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_VALUETYPE;
return ptr[0];
}
return 0;
}
const char *AQH_MsgValue_GetValueTypeName(const AQH_MSG *msg)
{
uint8_t t;
t=AQH_MsgValue_GetValueType(msg);
switch(t) {
case AQH_MSG_VALUE_TYPE_TEMP: return "temperature";
case AQH_MSG_VALUE_TYPE_HUMIDITY: return "humidity";
default: break;
}
return "unknown";
}
double AQH_MsgValue_GetValue(const AQH_MSG *msg)
{
if ((AQH_Msg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) &&
(AQH_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) {
const uint8_t *ptr;
double value;
double denom;
uint16_t intDenom;
ptr=AQH_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN;
value=(double)((ptr[AQH_MSG_OFFS_VALUE_VALUE])+(ptr[AQH_MSG_OFFS_VALUE_VALUE+1]<<8));
intDenom=(ptr[AQH_MSG_OFFS_VALUE_DENOM])+(ptr[AQH_MSG_OFFS_VALUE_DENOM+1]<<8);
denom=(double)(intDenom);
if (intDenom==0)
denom=1.0;
return (double)(value/denom);
}
return 0.0;
}
void AQH_MsgValue_DumpToBuffer(const AQH_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
if ((AQH_Msg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) &&
(AQH_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) {
GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: VALUE %s (timestamp=0x%08x, value_id=0x%02x type=%s value=%f)\n",
AQH_Msg_GetSourceAddress(msg),
AQH_Msg_GetDestAddress(msg),
sText,
(unsigned int) AQH_MsgValue_GetTimestamp(msg),
AQH_MsgValue_GetValueId(msg),
AQH_MsgValue_GetValueTypeName(msg),
AQH_MsgValue_GetValue(msg));
}
}