aqhome: Added test for serial interface, added api.h.

This commit is contained in:
Martin Preuss
2023-01-22 17:47:30 +01:00
parent a8a9571a27
commit c0fca3bf98
4 changed files with 142 additions and 8 deletions

View File

@@ -44,6 +44,7 @@
<headers dist="true" >
api.h
serial.h
serial_p.h
</headers>
@@ -72,5 +73,22 @@
</target>
<target type="Program" name="libtest" >
<includes type="c" >
$(gwenhywfar_cflags)
-I$(topsrcdir)
-I$(topbuilddir)
</includes>
<setVar name="local/cflags">$(visibility_cflags)</setVar>
<sources>libtest.c</sources>
<useTargets>aqhome</useTargets>
<libraries>$(gwenhywfar_libs)</libraries>
</target>
</gwbuild>

50
aqhome/api.h Normal file
View File

@@ -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

63
aqhome/libtest.c Normal file
View File

@@ -0,0 +1,63 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "aqhome/serial.h"
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/gwentime.h>
#include <gwenhywfar/buffer.h>
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();
}

View File

@@ -9,7 +9,10 @@
#ifndef AQH_SERIAL_H
#define AQH_SERIAL_H
#include <aqhome/api.h>
#include <gwenhywfar/buffer.h>
#include <inttypes.h>
@@ -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