Files
aqhomecontrol/aqhome/msg/msg_value3.c
2024-09-22 21:23:53 +02:00

227 lines
6.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/msg_value3.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/list.h>
#include <gwenhywfar/error.h>
#include <gwenhywfar/debug.h>
#define AQH_MSG_OFFS_VALUE3_UID 0 /* 4 bytes */
#define AQH_MSG_OFFS_VALUE3_MSGID 4 /* 2 bytes */
#define AQH_MSG_OFFS_VALUE3_VALUEID 6 /* 1 byte */
#define AQH_MSG_OFFS_VALUE3_VALUETYPE 7 /* 1 byte */
#define AQH_MSG_OFFS_VALUE3_VALUE 8 /* 2 bytes */
#define AQH_MSG_OFFS_VALUE3_DENOM 10 /* 2 bytes */
#define AQH_MSG_VALUE3_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_DENOM+2)
GWEN_MSG *AQH_Value3Msg_new(uint8_t srcAddr, uint8_t destAddr,
uint8_t code, uint16_t msgId,
uint8_t valueId,
uint16_t value, uint16_t denom)
{
GWEN_MSG *msg;
uint8_t *ptr;
int rv;
msg=AQH_NodeMsg_new(destAddr, srcAddr, code, AQH_MSG_VALUE3_MINSIZE, NULL);
ptr=GWEN_Msg_GetBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN;
*(ptr++)=0; /* uid (empty) */
*(ptr++)=0;
*(ptr++)=0;
*(ptr++)=0;
*(ptr++)=msgId & 0xff; /* msgid */
*(ptr++)=(msgId>>8) & 0xff;
*(ptr++)=valueId; /* valueid */
*(ptr++)=0; /* valuetype (empty) */
*(ptr++)=value & 0xff; /* value */
*(ptr++)=(value>>8) & 0xff;
*(ptr++)=denom & 0xff; /* denom */
*(ptr++)=(denom>>8) & 0xff;
rv=AQH_NodeMsg_AddChecksum(msg);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
GWEN_Msg_free(msg);
return NULL;
}
return msg;
}
uint32_t AQH_Value3Msg_GetUid(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_UID, 0);
}
uint16_t AQH_Value3Msg_GetMsgId(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_MSGID, 0);
}
uint8_t AQH_Value3Msg_GetValueId(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_VALUEID, 0);
}
uint8_t AQH_Value3Msg_GetValueType(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_VALUETYPE, 0);
}
uint16_t AQH_Value3Msg_GetValueNom(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_VALUE, 0);
}
uint16_t AQH_Value3Msg_GetValueDenom(const GWEN_MSG *msg)
{
return (uint16_t) GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_DENOM, 0);
}
const char *AQH_Value3Msg_GetValueTypeName(const GWEN_MSG *msg)
{
uint8_t t;
t=AQH_Value3Msg_GetValueType(msg);
switch(t) {
case AQH_MSG_VALUE3_TYPE_TEMP: return "temperature";
case AQH_MSG_VALUE3_TYPE_HUMIDITY: return "humidity";
case AQH_MSG_VALUE3_TYPE_DOOR: return "door_window";
default: break;
}
return "unknown";
}
const char *AQH_Value3Msg_GetValueAsWindowStateString(const GWEN_MSG *msg)
{
switch(AQH_Value3Msg_GetValueNom(msg)) {
case 0: return "closed";
case 128: return "tilted";
case 255: return "open";
default: break;
}
return "unknown";
}
const char *AQH_Value3Msg_GetValueTypeUnits(const GWEN_MSG *msg)
{
uint8_t t;
t=AQH_Value3Msg_GetValueType(msg);
switch(t) {
case AQH_MSG_VALUE3_TYPE_TEMP: return "Celsius";
case AQH_MSG_VALUE3_TYPE_HUMIDITY: return "%";
case AQH_MSG_VALUE3_TYPE_DOOR: return NULL;
default: break;
}
return NULL;
}
double AQH_Value3Msg_GetValue(const GWEN_MSG *msg)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE3_MINSIZE) {
const uint8_t *ptr;
double value;
double denom;
uint16_t intDenom;
ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN;
value=(double)((ptr[AQH_MSG_OFFS_VALUE3_VALUE])+(ptr[AQH_MSG_OFFS_VALUE3_VALUE+1]<<8));
intDenom=(ptr[AQH_MSG_OFFS_VALUE3_DENOM])+(ptr[AQH_MSG_OFFS_VALUE3_DENOM+1]<<8);
denom=(double)(intDenom);
if (intDenom==0)
denom=1.0;
return (double)(value/denom);
}
return 0.0;
}
void AQH_Value3Msg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE3_MINSIZE) {
const char *sCmd;
switch(AQH_NodeMsg_GetMsgType(msg)) {
case AQH_MSG_TYPE_VALUE_REPORT: sCmd="report"; break;
case AQH_MSG_TYPE_VALUE_SET: sCmd="set"; break;
case AQH_MSG_TYPE_VALUE_SET_ACK: sCmd="ack"; break;
case AQH_MSG_TYPE_VALUE_SET_NACK: sCmd="nack"; break;
default: sCmd="unknown"; break;
}
if (AQH_Value3Msg_GetValueType(msg)==AQH_MSG_VALUE3_TYPE_DOOR)
GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: VALUE3(%s) %s (uid=0x%08x, msgId=%u, value_id=0x%02x type=%s value=%s)\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sCmd,
sText,
(unsigned int) AQH_Value3Msg_GetUid(msg),
(unsigned int)AQH_Value3Msg_GetMsgId(msg),
AQH_Value3Msg_GetValueId(msg),
AQH_Value3Msg_GetValueTypeName(msg),
AQH_Value3Msg_GetValueAsWindowStateString(msg));
else
GWEN_Buffer_AppendArgs(dbuf,
"0x%02x->0x%02x: VALUE3(%s) %s (uid=0x%08x, msgId=%u, value_id=0x%02x type=%s value=%f [%04x/%04x])\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sCmd,
sText,
(unsigned int) AQH_Value3Msg_GetUid(msg),
(unsigned int)AQH_Value3Msg_GetMsgId(msg),
AQH_Value3Msg_GetValueId(msg),
AQH_Value3Msg_GetValueTypeName(msg),
AQH_Value3Msg_GetValue(msg),
AQH_Value3Msg_GetValueNom(msg),
AQH_Value3Msg_GetValueDenom(msg));
}
}