From e639550d00d5c2f189bf33f79afb6f27c706deef Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sun, 22 Jan 2023 17:47:30 +0100 Subject: [PATCH] aqhome: Added test for serial interface, added api.h. --- aqhome/0BUILD | 18 ++++++++++++++ aqhome/api.h | 50 ++++++++++++++++++++++++++++++++++++++ aqhome/libtest.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ aqhome/serial.h | 19 +++++++++------ 4 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 aqhome/api.h create mode 100644 aqhome/libtest.c diff --git a/aqhome/0BUILD b/aqhome/0BUILD index a49987e..0cf98a6 100644 --- a/aqhome/0BUILD +++ b/aqhome/0BUILD @@ -44,6 +44,7 @@ + api.h serial.h serial_p.h @@ -72,5 +73,22 @@ + + + + + + $(gwenhywfar_cflags) + -I$(topsrcdir) + -I$(topbuilddir) + + + $(visibility_cflags) + + libtest.c + aqhome + $(gwenhywfar_libs) + + diff --git a/aqhome/api.h b/aqhome/api.h new file mode 100644 index 0000000..250b619 --- /dev/null +++ b/aqhome/api.h @@ -0,0 +1,50 @@ +/**************************************************************************** + * 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. + ****************************************************************************/ + +#ifndef AQH_API_H +#define AQH_API_H + + + +# ifdef BUILDING_AQHOME +# /* building AqHome */ +# if AQHOME_SYS_IS_WINDOWS +# /* for windows */ +# ifdef __declspec +# define AQHOME_API __declspec (dllexport) +# else /* if __declspec */ +# define AQHOME_API +# endif /* if NOT __declspec */ +# else +# /* for non-win32 */ +# ifdef GCC_WITH_VISIBILITY_ATTRIBUTE +# define AQHOME_API __attribute__((visibility("default"))) +# else +# define AQHOME_API +# endif +# endif +# else +# /* not building AqHome */ +# if AQHOME_SYS_IS_WINDOWS +# /* for windows */ +# ifdef __declspec +# define AQHOME_API __declspec (dllimport) +# else /* if __declspec */ +# define AQHOME_API +# endif /* if NOT __declspec */ +# else +# /* for non-win32 */ +# define AQHOME_API +# endif +# endif + + + + +#endif + diff --git a/aqhome/libtest.c b/aqhome/libtest.c new file mode 100644 index 0000000..0fcefc7 --- /dev/null +++ b/aqhome/libtest.c @@ -0,0 +1,63 @@ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "aqhome/serial.h" + +#include +#include +#include +#include + + + +int testRecv() +{ + AQH_SERIAL *sr; + int rv; + int i; + GWEN_BUFFER *dbuf; + + fprintf(stdout, "Opening device...\n"); + sr=AQH_Serial_new("/dev/ttyUSB0", 240); + rv=AQH_Serial_Open(sr); + if (rv<0) { + DBG_ERROR(NULL, "ERROR opening device (%d)", rv); + AQH_Serial_free(sr); + return 2; + } + fprintf(stdout, "Device open, waiting for packets\n"); + + dbuf=GWEN_Buffer_new(0, 256, 0, 1); + for (i=0; ; i++) { + uint8_t buffer[1024]; + GWEN_TIME *ti; + + fprintf(stdout, "Waiting for packet...\n"); + rv=AQH_Serial_Recv(sr, buffer, sizeof(buffer)-1); + if (rv<0) + break; + ti=GWEN_CurrentTime(); + GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss", dbuf); + fprintf(stdout, "%s: Received:\n", GWEN_Buffer_GetStart(dbuf)); + GWEN_Text_DumpString(buffer, rv, 2); + GWEN_Time_free(ti); + GWEN_Buffer_Reset(dbuf); + fprintf(stdout, "\n"); + } + GWEN_Buffer_free(dbuf); + + AQH_Serial_Close(sr); + AQH_Serial_free(sr); + return 0; +} + + + +int main(void) +{ + return testRecv(); +} + + diff --git a/aqhome/serial.h b/aqhome/serial.h index c28878c..0458653 100644 --- a/aqhome/serial.h +++ b/aqhome/serial.h @@ -9,7 +9,10 @@ #ifndef AQH_SERIAL_H #define AQH_SERIAL_H +#include + #include + #include @@ -21,19 +24,19 @@ extern "C" { typedef struct AQH_SERIAL AQH_SERIAL; -AQH_SERIAL *AQH_Serial_new(const char *deviceName, uint8_t addr); -void AQH_Serial_free(AQH_SERIAL *sr); +AQHOME_API AQH_SERIAL *AQH_Serial_new(const char *deviceName, uint8_t addr); +AQHOME_API void AQH_Serial_free(AQH_SERIAL *sr); -uint8_t AQH_Serial_GetAddress(const AQH_SERIAL *sr); +AQHOME_API uint8_t AQH_Serial_GetAddress(const AQH_SERIAL *sr); -int AQH_Serial_Open(AQH_SERIAL *sr); -void AQH_Serial_Close(AQH_SERIAL *sr); +AQHOME_API int AQH_Serial_Open(AQH_SERIAL *sr); +AQHOME_API void AQH_Serial_Close(AQH_SERIAL *sr); -int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len); -int AQH_Serial_Send(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len); +AQHOME_API int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len); +AQHOME_API int AQH_Serial_Send(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len); -int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr, uint8_t len); +AQHOME_API int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr, uint8_t len); #ifdef __cplusplus