From ab8d3d168ce5dbbd27e454c73d6178f36d9ec060 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 12 Feb 2024 14:49:15 -0800 Subject: [PATCH] new API for general-purpose web api sends Will later expand to include a callback for when the (async) web API returns. --- xwords4/common/device.c | 23 ++++++++++++++++++++++- xwords4/common/dutil.h | 7 +++++-- xwords4/linux/lindutil.c | 14 +++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/xwords4/common/device.c b/xwords4/common/device.c index f744a22f9..4bf11fdc8 100644 --- a/xwords4/common/device.c +++ b/xwords4/common/device.c @@ -495,6 +495,26 @@ dispatchMsgs( XW_DUtilCtxt* dutil, XWEnv xwe, XP_U8 proto, XWStreamCtxt* stream, } } +static void +ackMQTTMsg( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic, + XP_U32 gameID, const XP_U8* buf, XP_U16 len ) +{ + cJSON* params = cJSON_CreateObject(); + + cJSON_AddStringToObject( params, "topic", topic ); + + Md5SumBuf sb; + dutil_md5sum( dutil, xwe, buf, len, &sb ); + cJSON_AddStringToObject( params, "sum", sb.buf ); + + XP_UCHAR gid16[16]; + XP_SNPRINTF( gid16, VSIZE(gid16), "%08X", gameID ); + cJSON_AddStringToObject( params, "gid16", gid16 ); + + dutil_sendViaWeb( dutil, xwe, "ack", params ); + cJSON_Delete( params ); +} + void dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic, const XP_U8* buf, XP_U16 len ) @@ -530,7 +550,8 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic, MQTTCmd cmd = stream_getU8( stream ); /* Need to ack even if discarded/malformed */ - dutil_ackMQTTMsg( dutil, xwe, topic, gameID, &senderID, buf, len ); + // dutil_ackMQTTMsg( dutil, xwe, topic, gameID, &senderID, buf, len ); + ackMQTTMsg( dutil, xwe, topic, gameID, buf, len ); switch ( cmd ) { case CMD_INVITE: { diff --git a/xwords4/common/dutil.h b/xwords4/common/dutil.h index d93a11a82..fced5ac21 100644 --- a/xwords4/common/dutil.h +++ b/xwords4/common/dutil.h @@ -28,6 +28,7 @@ #include "vtabmgr.h" #include "commstyp.h" #include "nlityp.h" +#include "cJSON.h" typedef enum { UNPAUSED, PAUSED, @@ -105,6 +106,8 @@ typedef struct _DUtilVtable { void (*m_dutil_ackMQTTMsg)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* topic, XP_U32 gameID, const MQTTDevID* senderID, const XP_U8* msg, XP_U16 len ); + void (*m_dutil_sendViaWeb)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api, + const cJSON* params ); } DUtilVtable; struct XW_DUtilCtxt { @@ -182,9 +185,9 @@ void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil ); (duc)->vtable.m_dutil_onCtrlReceived((duc),(xwe),(buf),(len)) #define dutil_onGameGoneReceived(duc, xwe, gameID, from) \ (duc)->vtable.m_dutil_onGameGoneReceived((duc),(xwe),(gameID),(from)) - #define dutil_ackMQTTMsg( duc, xwe, topic, gameID, senderID, msg, len ) \ (duc)->vtable.m_dutil_ackMQTTMsg( (duc), (xwe), (topic), (gameID), \ (senderID), (msg), (len) ) - +#define dutil_sendViaWeb( duc, xwe, api, params ) \ + (duc)->vtable.m_dutil_sendViaWeb((duc), (xwe), (api), (params)) #endif diff --git a/xwords4/linux/lindutil.c b/xwords4/linux/lindutil.c index c92d74b98..5f2e9cab3 100644 --- a/xwords4/linux/lindutil.c +++ b/xwords4/linux/lindutil.c @@ -309,7 +309,18 @@ linux_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), sendViaCurl( lduc, adp ); #endif /* LOG_RETURN_VOID(); */ - } +} + +static void +linux_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api, + const cJSON* params ) +{ + XP_USE(duc); + XP_USE(xwe); + char* pstr = cJSON_PrintUnformatted( params ); + XP_LOGFF( "(api: %s, params: %s)", api, pstr ); + free( pstr ); +} XW_DUtilCtxt* linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) @@ -361,6 +372,7 @@ linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) SET_PROC(onCtrlReceived); SET_PROC(onGameGoneReceived); SET_PROC(ackMQTTMsg); + SET_PROC(sendViaWeb); # undef SET_PROC