diff --git a/xwords4/common/device.c b/xwords4/common/device.c index 2fc8275ca..42cb442cd 100644 --- a/xwords4/common/device.c +++ b/xwords4/common/device.c @@ -28,6 +28,16 @@ #include "nli.h" #include "dbgutil.h" +#ifdef DEBUG +# define MAGIC_INITED 0x8283413F +# define ASSERT_MAGIC() XP_ASSERT(dutil->magic == MAGIC_INITED) +#else +# define ASSERT_MAGIC() +#endif + +#define LAST_REG_KEY FULL_KEY("device_last_reg") +#define REG_INTERVAL_SECS 60 /* for now. :-) */ + static XWStreamCtxt* mkStream( XW_DUtilCtxt* dutil ) { @@ -584,3 +594,55 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic, } LOG_RETURN_VOID(); } + +static void +registerIf( XW_DUtilCtxt* dutil, XWEnv xwe ) +{ + XP_U32 prevNow; + XP_U32 len = sizeof(prevNow); + const XP_UCHAR* keys[] = { LAST_REG_KEY, NULL }; + dutil_loadPtr( dutil, xwe, keys, &prevNow, &len ); + + XP_U32 now = dutil_getCurSeconds( dutil, xwe ); + if ( prevNow + REG_INTERVAL_SECS < now ) { + XP_LOGFF( "been long enough; trying to register" ); + cJSON* params = cJSON_CreateObject(); + + MQTTDevID myID; + dvc_getMQTTDevID( dutil, xwe, &myID ); + XP_UCHAR tmp[32]; + formatMQTTDevID( &myID, tmp, VSIZE(tmp) ); + cJSON_AddStringToObject( params, "devid", tmp ); + + cJSON_AddStringToObject( params, "gitrev", GITREV_SHORT ); + cJSON_AddStringToObject( params, "os", "Linux" ); + /* // PENDING remove me in favor of SDK_INT */ + cJSON_AddStringToObject( params, "vers", "DEBUG" ); + /* params.put( "versI", Build.VERSION.SDK_INT ); */ + /* params.put( "vrntCode", BuildConfig.VARIANT_CODE ); */ + /* params.put( "vrntName", BuildConfig.VARIANT_NAME ); */ +#ifdef DEBUG + cJSON_AddBoolToObject( params, "dbg", XP_TRUE ); +#endif + char* loc = getenv("LANG"); + cJSON_AddStringToObject( params, "loc", loc ); + /* params.put( "tmpKey", getTmpKey(mContext) ); */ + /* params.put( "frstV", Utils.getFirstVersion( mContext ) ); */ + + cJSON_AddNumberToObject( params, "myNow", now ); + + dutil_sendViaWeb( dutil, xwe, "register", params ); + cJSON_Delete( params ); + + // dutil_storePtr( dutil, xwe, keys, &now, sizeof(now) ); + } +} /* registerIf */ + +void +dvc_init( XW_DUtilCtxt* dutil, XWEnv xwe ) +{ + LOG_FUNC(); + XP_ASSERT( 00 == dutil->magic ); + dutil->magic = MAGIC_INITED; + registerIf( dutil, xwe ); +} diff --git a/xwords4/common/device.h b/xwords4/common/device.h index 86035f9dd..9abd1a55c 100644 --- a/xwords4/common/device.h +++ b/xwords4/common/device.h @@ -59,4 +59,8 @@ void dvc_makeMQTTNoSuchGames( XW_DUtilCtxt* dutil, XWEnv xwe, XP_U32 gameID ); void dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic, const XP_U8* buf, XP_U16 len ); + +/* All platforms need to call this shortly after setting up their XW_DUtilCtxt */ +void dvc_init( XW_DUtilCtxt* dutil, XWEnv xwe ); + #endif diff --git a/xwords4/common/dutil.h b/xwords4/common/dutil.h index 4f4125f2f..739aa5acc 100644 --- a/xwords4/common/dutil.h +++ b/xwords4/common/dutil.h @@ -115,6 +115,9 @@ struct XW_DUtilCtxt { pthread_mutex_t kpMutex; #endif VTableMgr* vtMgr; +#ifdef DEBUG + XP_U32 magic; +#endif MPSLOT }; diff --git a/xwords4/linux/Makefile b/xwords4/linux/Makefile index ce6e74dbf..41a86f5b4 100644 --- a/xwords4/linux/Makefile +++ b/xwords4/linux/Makefile @@ -35,6 +35,8 @@ CFLAGS += -g $(GPROFFLAG) -Wall -Wunused-parameter -Wcast-align -Werror -O0 #CFLAGS += -Os -Werror -Wunused endif +CFLAGS += -DGITREV_SHORT="\"$(shell git rev-parse --short HEAD)\"" + # Getting ready for gtk 3.x CFLAGS += -DGTK_DISABLE_SINGLE_INCLUDES CFLAGS += -DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED diff --git a/xwords4/linux/lindutil.c b/xwords4/linux/lindutil.c index 2c9b39eea..c43a85f66 100644 --- a/xwords4/linux/lindutil.c +++ b/xwords4/linux/lindutil.c @@ -20,6 +20,7 @@ #include #include "dutil.h" +#include "device.h" #include "mempool.h" #include "knownplyr.h" #include "lindutil.h" @@ -96,8 +97,8 @@ linux_dutil_notifyPause( XW_DUtilCtxt* XP_UNUSED(duc), XWEnv XP_UNUSED(xwe), const XP_UCHAR* XP_UNUSED_DBG(name), const XP_UCHAR* XP_UNUSED_DBG(msg) ) { - XP_LOGF( "%s(id=%d, turn=%d, name=%s, typ=%d, %s)", __func__, gameID, pauser, - name, pauseTyp, msg ); + XP_LOGFF( "(id=%d, turn=%d, name=%s, typ=%d, %s)", gameID, pauser, + name, pauseTyp, msg ); } static XP_Bool @@ -131,7 +132,7 @@ linux_dutil_onDupTimerChanged( XW_DUtilCtxt* XP_UNUSED(duc), XWEnv XP_UNUSED(xwe XP_U32 XP_UNUSED_DBG(oldVal), XP_U32 XP_UNUSED_DBG(newVal) ) { - XP_LOGF( "%s(id=%d, oldVal=%d, newVal=%d)", __func__, gameID, oldVal, newVal ); + XP_LOGFF( "(id=%d, oldVal=%d, newVal=%d)", gameID, oldVal, newVal ); } static void @@ -238,6 +239,7 @@ sendViaThreadProc( void* arg ) snprintf( url, sizeof(url), "%s://%s/xw4/api/v1/%s", proto, params->connInfo.mqtt.hostName, svdp->api ); curl_easy_setopt( curl, CURLOPT_URL, url ); + XP_LOGFF( "url: %s", url ); curl_easy_setopt( curl, CURLOPT_POSTFIELDS, svdp->pstr ); curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, -1L ); @@ -449,7 +451,7 @@ linux_dutil_getUserString( XW_DUtilCtxt* XP_UNUSED(uc), return "%s: %d points\n"; default: - XP_LOGF( "%s(code=%d)", __func__, code ); + XP_LOGFF( "(code=%d)", code ); return (XP_UCHAR*)"unknown code"; } } /* linux_dutil_getUserString */ @@ -510,7 +512,7 @@ linux_dutil_loadPtr( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), *lenp = 0; /* doesn't exist */ } - XP_LOGF( "%s(key=%s) => len: %d", __func__, keys[0], *lenp ); + XP_LOGFF( "(key=%s) => len: %d", keys[0], *lenp ); } static void @@ -536,7 +538,7 @@ linux_dutil_phoneNumbersSame( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), LOG_FUNC(); XP_USE( duc ); XP_Bool result = 0 == strcmp( p1, p2 ); - XP_LOGF( "%s(%s, %s) => %d", __func__, p1, p2, result ); + XP_LOGFF( "(%s, %s) => %d", p1, p2, result ); return result; } #endif @@ -558,12 +560,12 @@ linux_dutil_deviceRegistered( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), DevIDType LaunchParams* params = (LaunchParams*)duc->closure; switch( typ ) { case ID_TYPE_NONE: /* error case */ - XP_LOGF( "%s: id rejected", __func__ ); + XP_LOGFF( "id rejected" ); params->lDevID = NULL; break; case ID_TYPE_RELAY: if ( !!params->pDb && 0 < strlen( idRelay ) ) { - XP_LOGF( "%s: new id: %s", __func__, idRelay ); + XP_LOGFF( "new id: %s", idRelay ); gdb_store( params->pDb, KEY_RDEVID, idRelay ); } break; diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index 6fc786927..0e7e69daa 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -3371,6 +3371,8 @@ main( int argc, char** argv ) XP_ASSERT( !!mainParams.dbName ); mainParams.pDb = gdb_open( mainParams.dbName ); + + dvc_init( mainParams.dutil, NULL_XWE ); if ( mainParams.useCurses ) { /* if ( mainParams.needsNewGame ) { */