From 0b0143fa92c1da2ae2fa49d8287c09a2dda50a92 Mon Sep 17 00:00:00 2001 From: ehouse Date: Sun, 15 Apr 2007 16:46:17 +0000 Subject: [PATCH] If BT is off, ask user to turn it on only once. If cancels, warn that it must be turned on, and don't attempt to turn it on again. This prevents BTLib from repeatedly asking for permission to turn BT on. --- xwords4/palm/l10n/StrRes_en_US.pre | 4 ++-- xwords4/palm/palmbt.c | 35 ++++++++++++++++++++++-------- xwords4/palm/palmbt.h | 6 +++-- xwords4/palm/palmmain.c | 33 ++++++++++++++++++++-------- xwords4/palm/palmmain.h | 3 +++ 5 files changed, 59 insertions(+), 22 deletions(-) diff --git a/xwords4/palm/l10n/StrRes_en_US.pre b/xwords4/palm/l10n/StrRes_en_US.pre index 800a3c307..badc8778f 100644 --- a/xwords4/palm/l10n/StrRes_en_US.pre +++ b/xwords4/palm/l10n/StrRes_en_US.pre @@ -129,8 +129,8 @@ #endif #ifdef XWFEATURE_BLUETOOTH -{ "STR_BT_NOINIT", "Unable to initialize Bluetooth. " - "Is it enabled?" }, +{ "STR_BT_NOINIT", "Bluetooth appears to be off. Please turn it " + "on if you want Crosswords to use it." }, #endif { "STR_ABOUT_CONTENT", diff --git a/xwords4/palm/palmbt.c b/xwords4/palm/palmbt.c index d434d37fe..864ee4489 100644 --- a/xwords4/palm/palmbt.c +++ b/xwords4/palm/palmbt.c @@ -53,7 +53,7 @@ typedef enum { , PBTST_L2C_CONNECTED /* slave */ } PBT_STATE; -#define PBT_MAX_ACTS 6 /* four wasn't enough */ +#define PBT_MAX_ACTS 8 /* six wasn't enough */ #define HASWORK(s) ((s)->queueCur != (s)->queueNext) #define MAX_PACKETS 4 @@ -125,7 +125,8 @@ static const BtLibSdpUuidType XWORDS_UUID = { { 0x83, 0xe0, 0x87, 0xae, 0x4e, 0x18, 0x46, 0xbe, 0x83, 0xe0, 0x7b, 0x3d, 0xe6, 0xa1, 0xc3, 0x3b } }; -static PalmBTStuff* pbt_checkInit( PalmAppGlobals* globals ); +static PalmBTStuff* pbt_checkInit( PalmAppGlobals* globals, + XP_Bool* userCancelled ); static Err bpd_discover( PalmBTStuff* btStuff, BtLibDeviceAddressType* addr ); static void pbt_setup_slave( PalmBTStuff* btStuff, const CommsAddrRec* addr ); static void pbt_takedown_slave( PalmBTStuff* btStuff ); @@ -168,7 +169,7 @@ static void libMgmtCallback( BtLibManagementEventType* mEvent, UInt32 refCon ); static void l2SocketCallback( BtLibSocketEventType* sEvent, UInt32 refCon ); XP_Bool -palm_bt_init( PalmAppGlobals* globals, DataCb dataCb ) +palm_bt_init( PalmAppGlobals* globals, DataCb dataCb, XP_Bool* userCancelled ) { XP_Bool inited; PalmBTStuff* btStuff; @@ -177,7 +178,7 @@ palm_bt_init( PalmAppGlobals* globals, DataCb dataCb ) btStuff = globals->btStuff; if ( !btStuff ) { - btStuff = pbt_checkInit( globals ); + btStuff = pbt_checkInit( globals, userCancelled ); /* Should I start master/slave setup here? If not, how? */ } else { pbt_reset( btStuff ); @@ -277,7 +278,7 @@ void palm_bt_addrString( PalmAppGlobals* globals, XP_BtAddr* btAddr, XP_BtAddrStr* str ) { - PalmBTStuff* btStuff = pbt_checkInit( globals ); + PalmBTStuff* btStuff = pbt_checkInit( globals, NULL ); str->chars[0] = '\0'; if ( !!btStuff ) { Err err; @@ -298,7 +299,7 @@ palm_bt_browse_device( PalmAppGlobals* globals, XP_BtAddr* btAddr, LOG_FUNC(); - btStuff = pbt_checkInit( globals ); + btStuff = pbt_checkInit( globals, NULL ); if ( NULL != btStuff ) { BtLibDeviceAddressType addr; Err err = bpd_discover( btStuff, &addr ); @@ -420,7 +421,8 @@ pbt_send_pending( PalmBTStuff* btStuff ) XP_S16 palm_bt_send( const XP_U8* buf, XP_U16 len, const CommsAddrRec* addr, - DataCb dataCb, OnConnCb connCb, PalmAppGlobals* globals ) + DataCb dataCb, OnConnCb connCb, PalmAppGlobals* globals, + XP_Bool* userCancelled ) { XP_S16 nSent = -1; PalmBTStuff* btStuff; @@ -428,7 +430,7 @@ palm_bt_send( const XP_U8* buf, XP_U16 len, const CommsAddrRec* addr, PBT_PicoRole picoRole; XP_LOGF( "%s(len=%d)", __FUNCTION__, len); - btStuff = pbt_checkInit( globals ); + btStuff = pbt_checkInit( globals, userCancelled ); if ( !!btStuff ) { if ( !btStuff->dataCb ) { btStuff->dataCb = dataCb; @@ -807,9 +809,10 @@ pbt_takedown_slave( PalmBTStuff* btStuff ) } static PalmBTStuff* -pbt_checkInit( PalmAppGlobals* globals ) +pbt_checkInit( PalmAppGlobals* globals, XP_Bool* userCancelledP ) { PalmBTStuff* btStuff = globals->btStuff; + XP_Bool userCancelled = XP_FALSE; if ( !btStuff ) { Err err; XP_U16 btLibRefNum; @@ -818,6 +821,8 @@ pbt_checkInit( PalmAppGlobals* globals ) if ( errNone == err ) { CALL_ERR( err, BtLibOpen, btLibRefNum, false ); + userCancelled = err == btLibErrBluetoothOff; + /* BT is probably off if this fails */ if ( errNone == err ) { btStuff = XP_MALLOC( globals->mpool, sizeof(*btStuff) ); @@ -836,6 +841,11 @@ pbt_checkInit( PalmAppGlobals* globals ) } } } + + if ( !!userCancelledP ) { + *userCancelledP = userCancelled; + } + return btStuff; } /* pbt_checkInit */ @@ -1084,8 +1094,15 @@ connEnumToStr( BtLibAccessibleModeEnum mode ) CASESTR(btLibNotAccessible); CASESTR(btLibConnectableOnly); CASESTR(btLibDiscoverableAndConnectable); + case 0x0006: + /* I've seen this on 68K even. Seems to happen when the other + device changes roles (temporarily). */ + return "undoc_06"; + case 0x00F8: /* seen on ARM only */ + return "undoc_F8"; default: XP_ASSERT(0); + XP_LOGF( "%s: got 0x%x", __func__, mode ); return ""; } } diff --git a/xwords4/palm/palmbt.h b/xwords4/palm/palmbt.h index 9f78fcc7a..438d29aee 100644 --- a/xwords4/palm/palmbt.h +++ b/xwords4/palm/palmbt.h @@ -50,14 +50,16 @@ typedef void (*DataCb)( PalmAppGlobals* globals, typedef void (*OnConnCb)( PalmAppGlobals* globals ); -XP_Bool palm_bt_init( PalmAppGlobals* globals, DataCb dataCb ); +XP_Bool palm_bt_init( PalmAppGlobals* globals, DataCb dataCb, + XP_Bool* userCancelled ); void palm_bt_close( PalmAppGlobals* globals ); void palm_bt_addrString( PalmAppGlobals* globals, XP_BtAddr* btAddr, XP_BtAddrStr* str ); XP_S16 palm_bt_send( const XP_U8* buf, XP_U16 len, const CommsAddrRec* addr, - DataCb cb, OnConnCb connCb, PalmAppGlobals* globals ); + DataCb cb, OnConnCb connCb, PalmAppGlobals* globals, + XP_Bool* userCancelled ); XP_Bool palm_bt_browse_device( PalmAppGlobals* globals, XP_BtAddr* btAddr, XP_UCHAR* out,XP_U16 len ); diff --git a/xwords4/palm/palmmain.c b/xwords4/palm/palmmain.c index 4e513a5a5..2bd14df24 100644 --- a/xwords4/palm/palmmain.c +++ b/xwords4/palm/palmmain.c @@ -1619,8 +1619,9 @@ handleNilEvent( PalmAppGlobals* globals ) && (when <= TimGetTicks()) ) { palmFireTimer( globals, why ); #ifdef XWFEATURE_BLUETOOTH - } else if ( palm_bt_doWork( globals, &globals->btUIState ) ) { - showConnState( globals ); + } else if ( (handled = palm_bt_doWork( globals, &globals->btUIState ) ), + showConnState( globals ), handled ) { + /* nothing to do */ #endif } else if ( globals->timeRequested ) { globals->timeRequested = false; @@ -3828,6 +3829,16 @@ palm_send_on_close( XWStreamCtxt* stream, void* closure ) comms_send( globals->game.comms, stream ); } /* palm_send_on_close */ +#ifdef XWFEATURE_BLUETOOTH +static void +handleUserBTCancel( PalmAppGlobals* globals ) +{ + XP_ASSERT( !globals->userCancelledBT ); + globals->userCancelledBT = XP_TRUE; + userErrorFromStrId( globals, STR_BT_NOINIT ); +} +#endif + static XP_S16 palm_send( const XP_U8* buf, XP_U16 len, const CommsAddrRec* addr, void* closure ) @@ -3850,11 +3861,12 @@ palm_send( const XP_U8* buf, XP_U16 len, #endif #ifdef XWFEATURE_BLUETOOTH case COMMS_CONN_BT: - if ( !!globals->mainForm ) { + if ( !!globals->mainForm && !globals->userCancelledBT ) { + XP_Bool userCancelled; result = palm_bt_send( buf, len, addr, btDataHandler, - btConnHandler, globals ); - if ( result < 0 ) { - userErrorFromStrId( globals, STR_BT_NOINIT ); + btConnHandler, globals, &userCancelled ); + if ( userCancelled ) { + handleUserBTCancel( globals ); } } break; @@ -3976,10 +3988,13 @@ palm_util_addrChange( XW_UtilCtxt* uc, const CommsAddrRec* oldAddr, ip_addr_change( globals, oldAddr, newAddr ); # endif # ifdef XWFEATURE_BLUETOOTH - } else if ( isBT ) { + } else if ( isBT && !globals->userCancelledBT ) { + XP_Bool userCancelled; XP_ASSERT( !!globals->mainForm ); - if ( !palm_bt_init( globals, btDataHandler ) ) { - userErrorFromStrId( globals, STR_BT_NOINIT ); + if ( !palm_bt_init( globals, btDataHandler, &userCancelled ) ) { + if ( userCancelled ) { + handleUserBTCancel( globals ); + } } # endif } diff --git a/xwords4/palm/palmmain.h b/xwords4/palm/palmmain.h index 99aa1d855..1cf58ee6f 100644 --- a/xwords4/palm/palmmain.h +++ b/xwords4/palm/palmmain.h @@ -274,6 +274,9 @@ struct PalmAppGlobals { Boolean menuIsDown; XP_Bool newGameIsNew; XP_Bool runningOnPOSE; /* Needed for NetLibSelect */ +#ifdef XWFEATURE_BLUETOOTH + XP_Bool userCancelledBT; +#endif GraphicsAbility able; XP_U16 prevScroll; /* for scrolling in 'ask' dialog */