antfs_host.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. #include "types.h"
  9. #include "dsi_thread.h"
  10. #include "config.h"
  11. #include "dsi_convert.h"
  12. #include "antfs_host.hpp"
  13. #include "dsi_debug.hpp"
  14. #if defined(DEBUG_FILE)
  15. #include "macros.h"
  16. #endif
  17. //////////////////////////////////////////////////////////////////////////////////
  18. // Private Definitions
  19. //////////////////////////////////////////////////////////////////////////////////
  20. #define DEFAULT_CHANNEL_NUMBER ((UCHAR) 0)
  21. //////////////////////////////////////////////////////////////////////////////////
  22. // Public Functions
  23. //////////////////////////////////////////////////////////////////////////////////
  24. ANTFSHost::ANTFSHost()
  25. {
  26. bInitFailed = FALSE;
  27. bOpen = FALSE;
  28. eWrappedState = ANTFS_STATE_IDLE;
  29. ulHostSerialNumber = 0;
  30. hANTFSThread = (DSI_THREAD_ID)NULL; // Handle for the ANTFS thread
  31. bKillThread = FALSE;
  32. bANTFSThreadRunning = FALSE;
  33. pclMsgHandler = new DSIANTDevicePolling();
  34. pclHost = new ANTFSHostChannel();
  35. if (DSIThread_MutexInit(&stMutexCriticalSection) != DSI_THREAD_ENONE)
  36. {
  37. bInitFailed = TRUE;
  38. }
  39. if (DSIThread_MutexInit(&stMutexResponseQueue) != DSI_THREAD_ENONE)
  40. {
  41. bInitFailed = TRUE;
  42. }
  43. if (DSIThread_CondInit(&stCondANTFSThreadExit) != DSI_THREAD_ENONE)
  44. {
  45. bInitFailed = TRUE;
  46. }
  47. if (DSIThread_CondInit(&stCondWaitForResponse) != DSI_THREAD_ENONE)
  48. {
  49. bInitFailed = TRUE;
  50. }
  51. }
  52. ///////////////////////////////////////////////////////////////////////
  53. ANTFSHost::~ANTFSHost()
  54. {
  55. this->Close();
  56. delete pclHost;
  57. delete pclMsgHandler;
  58. if (bInitFailed == FALSE)
  59. {
  60. DSIThread_MutexDestroy(&stMutexCriticalSection);
  61. DSIThread_MutexDestroy(&stMutexResponseQueue);
  62. DSIThread_CondDestroy(&stCondANTFSThreadExit);
  63. DSIThread_CondDestroy(&stCondWaitForResponse);
  64. }
  65. }
  66. ///////////////////////////////////////////////////////////////////////
  67. UCHAR ANTFSHost::GetVersion(UCHAR *pucVersionString_, UCHAR ucBufferSize_)
  68. {
  69. return pclHost->GetVersion(pucVersionString_, ucBufferSize_);
  70. }
  71. ///////////////////////////////////////////////////////////////////////
  72. BOOL ANTFSHost::Init(UCHAR ucUSBDeviceNum_, USHORT usBaudRate_)
  73. {
  74. return InitHost(ucUSBDeviceNum_, usBaudRate_, FALSE);
  75. }
  76. ///////////////////////////////////////////////////////////////////////
  77. BOOL ANTFSHost::Init(void)
  78. {
  79. return InitHost(0, 0 , TRUE);
  80. }
  81. ///////////////////////////////////////////////////////////////////////
  82. void ANTFSHost::GetCurrentConfig( ANTFS_CONFIG_PARAMETERS* pCfg_ )
  83. {
  84. pclHost->GetCurrentConfig( pCfg_ );
  85. }
  86. ///////////////////////////////////////////////////////////////////////
  87. BOOL ANTFSHost::SetCurrentConfig( const ANTFS_CONFIG_PARAMETERS* pCfg_ )
  88. {
  89. return pclHost->SetCurrentConfig( pCfg_ );
  90. }
  91. ///////////////////////////////////////////////////////////////////////
  92. void ANTFSHost::SetChannelID(UCHAR ucDeviceType_, UCHAR ucTransmissionType_)
  93. {
  94. pclHost->SetChannelID(ucDeviceType_, ucTransmissionType_);
  95. }
  96. ///////////////////////////////////////////////////////////////////////
  97. void ANTFSHost::SetMessagePeriod(USHORT usMessagePeriod_)
  98. {
  99. pclHost->SetChannelPeriod(usMessagePeriod_);
  100. }
  101. ///////////////////////////////////////////////////////////////////////
  102. void ANTFSHost::SetNetworkkey(UCHAR ucNetworkkey[])
  103. {
  104. pclHost->SetNetworkKey(0, ucNetworkkey);
  105. }
  106. ///////////////////////////////////////////////////////////////////////
  107. void ANTFSHost::SetProximitySearch(UCHAR ucSearchThreshold_)
  108. {
  109. pclHost->SetProximitySearch(ucSearchThreshold_);
  110. }
  111. ///////////////////////////////////////////////////////////////////////
  112. ANTFS_STATE ANTFSHost::GetStatus(void)
  113. {
  114. ANT_USB_STATE eUSBState;
  115. ANTFS_HOST_STATE eANTFSState;
  116. DSIThread_MutexLock(&stMutexCriticalSection);
  117. eUSBState = pclMsgHandler->GetStatus();
  118. switch(eUSBState)
  119. {
  120. case ANT_USB_STATE_OFF:
  121. case ANT_USB_STATE_IDLE:
  122. eWrappedState = ANTFS_STATE_IDLE;
  123. break;
  124. case ANT_USB_STATE_IDLE_POLLING_USB:
  125. eWrappedState = ANTFS_STATE_IDLE_POLLING_USB;
  126. break;
  127. case ANT_USB_STATE_OPEN:
  128. eWrappedState = ANTFS_STATE_OPEN;
  129. break;
  130. }
  131. if(eWrappedState < ANTFS_STATE_OPEN)
  132. {
  133. DSIThread_MutexUnlock(&stMutexCriticalSection);
  134. return eWrappedState;
  135. }
  136. eANTFSState = pclHost->GetStatus();
  137. switch(eANTFSState)
  138. {
  139. case ANTFS_HOST_STATE_OFF:
  140. case ANTFS_HOST_STATE_IDLE:
  141. // Keep state...
  142. break;
  143. case ANTFS_HOST_STATE_REQUESTING_SESSION: // We should not get this one
  144. // Keep as Open
  145. break;
  146. case ANTFS_HOST_STATE_DISCONNECTING:
  147. eWrappedState = ANTFS_STATE_DISCONNECTING;
  148. break;
  149. case ANTFS_HOST_STATE_SEARCHING:
  150. eWrappedState = ANTFS_STATE_SEARCHING;
  151. break;
  152. case ANTFS_HOST_STATE_CONNECTED:
  153. eWrappedState = ANTFS_STATE_CONNECTED;
  154. break;
  155. case ANTFS_HOST_STATE_AUTHENTICATING:
  156. eWrappedState = ANTFS_STATE_AUTHENTICATING;
  157. break;
  158. case ANTFS_HOST_STATE_TRANSPORT:
  159. eWrappedState = ANTFS_STATE_TRANSPORT;
  160. break;
  161. case ANTFS_HOST_STATE_DOWNLOADING:
  162. eWrappedState = ANTFS_STATE_DOWNLOADING;
  163. break;
  164. case ANTFS_HOST_STATE_UPLOADING:
  165. eWrappedState = ANTFS_STATE_UPLOADING;
  166. break;
  167. case ANTFS_HOST_STATE_ERASING:
  168. eWrappedState = ANTFS_STATE_ERASING;
  169. break;
  170. case ANTFS_HOST_STATE_SENDING:
  171. eWrappedState = ANTFS_STATE_SENDING;
  172. break;
  173. case ANTFS_HOST_STATE_RECEIVING:
  174. eWrappedState = ANTFS_STATE_RECEIVING;
  175. break;
  176. }
  177. DSIThread_MutexUnlock(&stMutexCriticalSection);
  178. return eWrappedState;
  179. }
  180. ///////////////////////////////////////////////////////////////////////
  181. UCHAR ANTFSHost::GetConnectedDeviceBeaconAntfsState(void)
  182. {
  183. return pclHost->GetConnectedDeviceBeaconAntfsState();
  184. }
  185. ///////////////////////////////////////////////////////////////////////
  186. void ANTFSHost::Close(void)
  187. {
  188. #if defined(DEBUG_FILE)
  189. DSIDebug::ThreadWrite("ANTFSHost::Close(): Closing ANTFS...");
  190. #endif
  191. DSIThread_MutexLock(&stMutexCriticalSection);
  192. if(bOpen == FALSE)
  193. {
  194. DSIThread_MutexUnlock(&stMutexCriticalSection);
  195. return;
  196. }
  197. // Stop the response thread
  198. bKillThread = TRUE;
  199. #if defined(DEBUG_FILE)
  200. DSIDebug::ThreadWrite("ANTFSHost::Close(): SetEvent(stCondWaitForResponse).");
  201. #endif
  202. DSIThread_MutexLock(&stMutexResponseQueue);
  203. DSIThread_CondSignal(&stCondWaitForResponse);
  204. clResponseQueue.Clear();
  205. DSIThread_MutexUnlock(&stMutexResponseQueue);
  206. if (hANTFSThread)
  207. {
  208. if (bANTFSThreadRunning == TRUE)
  209. {
  210. #if defined(DEBUG_FILE)
  211. DSIDebug::ThreadWrite("ANTFSHost::Close(): Killing thread.");
  212. #endif
  213. if (DSIThread_CondTimedWait(&stCondANTFSThreadExit, &stMutexCriticalSection, 9000) != DSI_THREAD_ENONE)
  214. {
  215. #if defined(DEBUG_FILE)
  216. DSIDebug::ThreadWrite("ANTFSHost::Close(): Thread not dead.");
  217. DSIDebug::ThreadWrite("ANTFSHost::Close(): Forcing thread termination...");
  218. #endif
  219. DSIThread_DestroyThread(hANTFSThread);
  220. }
  221. else
  222. {
  223. #if defined(DEBUG_FILE)
  224. DSIDebug::ThreadWrite("ANTFSHost::Close(): Thread terminated successfully.");
  225. #endif
  226. }
  227. }
  228. DSIThread_ReleaseThreadID(hANTFSThread);
  229. hANTFSThread = (DSI_THREAD_ID)NULL;
  230. }
  231. pclMsgHandler->RemoveMessageProcessor((DSIANTMessageProcessor*) pclHost);
  232. pclMsgHandler->Close();
  233. bOpen = FALSE;
  234. eWrappedState = ANTFS_STATE_IDLE;
  235. DSIThread_MutexUnlock(&stMutexCriticalSection);
  236. #if defined(DEBUG_FILE)
  237. DSIDebug::ThreadWrite("ANTFSHost::Close(): Closed.");
  238. #endif
  239. }
  240. ///////////////////////////////////////////////////////////////////////
  241. void ANTFSHost::Cancel(void)
  242. {
  243. pclHost->Cancel();
  244. }
  245. //////////////////////////////////////////////////////////////////////////////////
  246. // ANTFS Link Layer
  247. //////////////////////////////////////////////////////////////////////////////////
  248. ///////////////////////////////////////////////////////////////////////
  249. USHORT ANTFSHost::AddSearchDevice(ANTFS_DEVICE_PARAMETERS *pstDeviceSearchMask_, ANTFS_DEVICE_PARAMETERS *pstDeviceParameters_)
  250. {
  251. return pclHost->AddSearchDevice(pstDeviceSearchMask_, pstDeviceParameters_);
  252. }
  253. ///////////////////////////////////////////////////////////////////////
  254. void ANTFSHost::RemoveSearchDevice(USHORT usHandle_)
  255. {
  256. pclHost->RemoveSearchDevice(usHandle_);
  257. }
  258. ///////////////////////////////////////////////////////////////////////
  259. void ANTFSHost::ClearSearchDeviceList(void)
  260. {
  261. pclHost->ClearSearchDeviceList();
  262. }
  263. ///////////////////////////////////////////////////////////////////////
  264. ANTFS_RETURN ANTFSHost::SearchForDevice(UCHAR ucSearchRadioFrequency_, UCHAR ucConnectedRadioFrequency_, USHORT usRadioChannelID_, BOOL bUseRequestPage_)
  265. {
  266. DSIThread_MutexLock(&stMutexCriticalSection);
  267. if ((pclMsgHandler->GetStatus() < ANT_USB_STATE_OPEN))
  268. {
  269. #if defined(DEBUG_FILE)
  270. DSIDebug::ThreadWrite("ANTFSHost::SearchForDevice(): Not in correct state.");
  271. #endif
  272. DSIThread_MutexUnlock(&stMutexCriticalSection);
  273. return ANTFS_RETURN_FAIL;
  274. }
  275. DSIThread_MutexUnlock(&stMutexCriticalSection);
  276. return pclHost->SearchForDevice(ucSearchRadioFrequency_, ucConnectedRadioFrequency_, usRadioChannelID_, bUseRequestPage_);
  277. }
  278. ///////////////////////////////////////////////////////////////////////
  279. BOOL ANTFSHost::GetFoundDeviceParameters(ANTFS_DEVICE_PARAMETERS *pstDeviceParameters_, UCHAR *aucFriendlyName_, UCHAR *pucBufferSize_)
  280. {
  281. return pclHost->GetFoundDeviceParameters(pstDeviceParameters_, aucFriendlyName_, pucBufferSize_);
  282. }
  283. ///////////////////////////////////////////////////////////////////////
  284. BOOL ANTFSHost::GetFoundDeviceChannelID(USHORT *pusDeviceNumber_, UCHAR *pucDeviceType_, UCHAR *pucTransmitType_)
  285. {
  286. return pclHost->GetFoundDeviceChannelID(pusDeviceNumber_, pucDeviceType_, pucTransmitType_);
  287. }
  288. //////////////////////////////////////////////////////////////////////////////////
  289. ANTFS_RETURN ANTFSHost::Authenticate(UCHAR ucAuthenticationType_, UCHAR *pucAuthenticationString_, UCHAR ucLength_, UCHAR *pucResponseBuffer_, UCHAR *pucResponseBufferSize_, ULONG ulResponseTimeout_)
  290. {
  291. return pclHost->Authenticate(ucAuthenticationType_, pucAuthenticationString_, ucLength_, pucResponseBuffer_, pucResponseBufferSize_, ulResponseTimeout_);
  292. }
  293. //////////////////////////////////////////////////////////////////////////////////
  294. // Always On Transport Layer
  295. //////////////////////////////////////////////////////////////////////////////////
  296. ///////////////////////////////////////////////////////////////////////
  297. ANTFS_RETURN ANTFSHost::Disconnect(USHORT usBlackoutTime_, UCHAR ucDisconnectType_, UCHAR ucTimeDuration_, UCHAR ucAppSpecificDuration_)
  298. {
  299. return pclHost->Disconnect(usBlackoutTime_, ucDisconnectType_, ucTimeDuration_, ucAppSpecificDuration_);
  300. }
  301. ///////////////////////////////////////////////////////////////////////
  302. BOOL ANTFSHost::Blackout(ULONG ulDeviceID_, USHORT usManufacturerID_, USHORT usDeviceType_, USHORT usBlackoutTime_)
  303. {
  304. return pclHost->Blackout(ulDeviceID_, usManufacturerID_, usDeviceType_, usBlackoutTime_);
  305. }
  306. ///////////////////////////////////////////////////////////////////////
  307. BOOL ANTFSHost::RemoveBlackout(ULONG ulDeviceID_, USHORT usManufacturerID_, USHORT usDeviceType_)
  308. {
  309. return pclHost->RemoveBlackout(ulDeviceID_, usManufacturerID_, usDeviceType_);
  310. }
  311. ///////////////////////////////////////////////////////////////////////
  312. void ANTFSHost::ClearBlackoutList(void)
  313. {
  314. pclHost->ClearBlackoutList();
  315. }
  316. ///////////////////////////////////////////////////////////////////////
  317. ANTFS_RETURN ANTFSHost::Download(USHORT usFileIndex_, ULONG ulDataOffset_, ULONG ulMaxDataLength_, ULONG ulMaxBlockSize_)
  318. {
  319. return pclHost->Download(usFileIndex_, ulDataOffset_, ulMaxDataLength_, ulMaxBlockSize_);
  320. }
  321. ///////////////////////////////////////////////////////////////////////
  322. ANTFS_RETURN ANTFSHost::Upload(USHORT usFileIndex_, ULONG ulDataOffset_, ULONG ulDataLength_, void *pvData_, BOOL bForceOffset_, ULONG ulMaxBlockSize_)
  323. {
  324. return pclHost->Upload(usFileIndex_, ulDataOffset_, ulDataLength_, pvData_, bForceOffset_, ulMaxBlockSize_);
  325. }
  326. ///////////////////////////////////////////////////////////////////////
  327. ANTFS_RETURN ANTFSHost::ManualTransfer(USHORT usFileIndex_, ULONG ulDataOffset_, ULONG ulDataLength_, void *pvData_)
  328. {
  329. return pclHost->ManualTransfer(usFileIndex_, ulDataOffset_, ulDataLength_, pvData_);
  330. }
  331. ///////////////////////////////////////////////////////////////////////
  332. BOOL ANTFSHost::GetDownloadStatus(ULONG *pulByteProgress_, ULONG *pulTotalLength_)
  333. {
  334. return pclHost->GetDownloadStatus(pulByteProgress_, pulTotalLength_);
  335. }
  336. ///////////////////////////////////////////////////////////////////////
  337. BOOL ANTFSHost::GetTransferData(ULONG *pulDataSize_ , void *pvData_)
  338. {
  339. return pclHost->GetTransferData(pulDataSize_, pvData_);
  340. }
  341. ///////////////////////////////////////////////////////////////////////
  342. BOOL ANTFSHost::RecoverTransferData(ULONG *pulDataSize_ , void *pvData_)
  343. {
  344. return pclHost->RecoverTransferData(pulDataSize_, pvData_);
  345. }
  346. ///////////////////////////////////////////////////////////////////////
  347. BOOL ANTFSHost::GetUploadStatus(ULONG *pulByteProgress_, ULONG *pulTotalLength_)
  348. {
  349. return pclHost->GetUploadStatus(pulByteProgress_, pulTotalLength_);
  350. }
  351. ///////////////////////////////////////////////////////////////////////
  352. ANTFS_RETURN ANTFSHost::EraseData(USHORT usDataFileIndex_)
  353. {
  354. return pclHost->EraseData(usDataFileIndex_);
  355. }
  356. //////////////////////////////////////////////////////////////////////
  357. BOOL ANTFSHost::EnablePing(BOOL bEnable_)
  358. {
  359. return pclHost->EnablePing(bEnable_);
  360. }
  361. #if defined(DEBUG_FILE)
  362. ///////////////////////////////////////////////////////////////////////
  363. void ANTFSHost::SetDebug(BOOL bDebugOn_, const char *pcDirectory_)
  364. {
  365. pclMsgHandler->SetDebug(bDebugOn_, pcDirectory_);
  366. }
  367. #endif
  368. //////////////////////////////////////////////////////////////////////////////////
  369. // Private Functions
  370. //////////////////////////////////////////////////////////////////////////////////
  371. DSI_THREAD_RETURN ANTFSHost::ANTFSThreadStart(void *pvParameter_)
  372. {
  373. #if defined(DEBUG_FILE)
  374. DSIDebug::ThreadInit("ANTFSHostWrapper");
  375. #endif
  376. ((ANTFSHost *)pvParameter_)->ANTFSThread();
  377. return 0;
  378. }
  379. ///////////////////////////////////////////////////////////////////////
  380. // ANTFS Task Thread
  381. ///////////////////////////////////////////////////////////////////////
  382. void ANTFSHost::ANTFSThread(void)
  383. {
  384. ANTFS_RESPONSE eWrappedResponse;
  385. ANT_USB_RESPONSE eUSBResponse;
  386. ANTFS_HOST_RESPONSE eANTFSResponse;
  387. bANTFSThreadRunning = TRUE;
  388. #if defined(DEBUG_FILE)
  389. DSIDebug::ThreadWrite("ANTFSHost::ANTFSThread(): Awaiting Responses..");
  390. #endif
  391. while (bKillThread == FALSE)
  392. {
  393. eWrappedResponse = ANTFS_RESPONSE_NONE;
  394. eUSBResponse = ANT_USB_RESPONSE_NONE;
  395. eANTFSResponse = ANTFS_HOST_RESPONSE_NONE;
  396. eANTFSResponse = pclHost->WaitForResponse(100);
  397. switch(eANTFSResponse)
  398. {
  399. case ANTFS_HOST_RESPONSE_NONE:
  400. eWrappedResponse = ANTFS_RESPONSE_NONE;
  401. break;
  402. case ANTFS_HOST_RESPONSE_INIT_PASS:
  403. eWrappedResponse = ANTFS_RESPONSE_NONE;
  404. break;
  405. case ANTFS_HOST_RESPONSE_SERIAL_FAIL:
  406. #if defined(DEBUG_FILE)
  407. DSIDebug::ThreadWrite("ANTFSHost::ANTFSThread(): Serial fail from ANTFSHostChannel ..");
  408. #endif
  409. eWrappedResponse = ANTFS_RESPONSE_SERIAL_FAIL;
  410. break;
  411. case ANTFS_HOST_RESPONSE_REQUEST_SESSION_FAIL:
  412. // Should not get this one, as Request Session is not included from this wrapper
  413. break;
  414. case ANTFS_HOST_RESPONSE_CONNECT_PASS:
  415. eWrappedResponse = ANTFS_RESPONSE_CONNECT_PASS;
  416. break;
  417. case ANTFS_HOST_RESPONSE_DISCONNECT_PASS:
  418. eWrappedResponse = ANTFS_RESPONSE_DISCONNECT_PASS;
  419. break;
  420. case ANTFS_HOST_RESPONSE_CONNECTION_LOST:
  421. eWrappedResponse = ANTFS_RESPONSE_CONNECTION_LOST;
  422. break;
  423. case ANTFS_HOST_RESPONSE_AUTHENTICATE_NA:
  424. eWrappedResponse = ANTFS_RESPONSE_AUTHENTICATE_NA;
  425. break;
  426. case ANTFS_HOST_RESPONSE_AUTHENTICATE_PASS:
  427. eWrappedResponse = ANTFS_RESPONSE_AUTHENTICATE_PASS;
  428. break;
  429. case ANTFS_HOST_RESPONSE_AUTHENTICATE_REJECT:
  430. eWrappedResponse = ANTFS_RESPONSE_AUTHENTICATE_REJECT;
  431. break;
  432. case ANTFS_HOST_RESPONSE_AUTHENTICATE_FAIL:
  433. eWrappedResponse = ANTFS_RESPONSE_AUTHENTICATE_FAIL;
  434. break;
  435. case ANTFS_HOST_RESPONSE_DOWNLOAD_PASS:
  436. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_PASS;
  437. break;
  438. case ANTFS_HOST_RESPONSE_DOWNLOAD_REJECT:
  439. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_REJECT;
  440. break;
  441. case ANTFS_HOST_RESPONSE_DOWNLOAD_INVALID_INDEX:
  442. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_INVALID_INDEX;
  443. break;
  444. case ANTFS_HOST_RESPONSE_DOWNLOAD_FILE_NOT_READABLE:
  445. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_FILE_NOT_READABLE;
  446. break;
  447. case ANTFS_HOST_RESPONSE_DOWNLOAD_NOT_READY:
  448. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_NOT_READY;
  449. break;
  450. case ANTFS_HOST_RESPONSE_DOWNLOAD_FAIL:
  451. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_FAIL;
  452. break;
  453. case ANTFS_HOST_RESPONSE_DOWNLOAD_CRC_REJECTED:
  454. eWrappedResponse = ANTFS_RESPONSE_DOWNLOAD_CRC_REJECTED;
  455. break;
  456. case ANTFS_HOST_RESPONSE_UPLOAD_PASS:
  457. eWrappedResponse = ANTFS_RESPONSE_UPLOAD_PASS;
  458. break;
  459. case ANTFS_HOST_RESPONSE_UPLOAD_REJECT:
  460. eWrappedResponse = ANTFS_RESPONSE_UPLOAD_REJECT;
  461. break;
  462. case ANTFS_HOST_RESPONSE_UPLOAD_INVALID_INDEX:
  463. eWrappedResponse = ANTFS_RESPONSE_UPLOAD_INVALID_INDEX;
  464. break;
  465. case ANTFS_HOST_RESPONSE_UPLOAD_FILE_NOT_WRITEABLE:
  466. eWrappedResponse = ANTFS_RESPONSE_UPLOAD_FILE_NOT_WRITEABLE;
  467. break;
  468. case ANTFS_HOST_RESPONSE_UPLOAD_INSUFFICIENT_SPACE:
  469. eWrappedResponse = ANTFS_RESPONSE_UPLOAD_INSUFFICIENT_SPACE;
  470. break;
  471. case ANTFS_HOST_RESPONSE_UPLOAD_FAIL:
  472. eWrappedResponse = ANTFS_RESPONSE_UPLOAD_FAIL;
  473. break;
  474. case ANTFS_HOST_RESPONSE_ERASE_PASS:
  475. eWrappedResponse = ANTFS_RESPONSE_ERASE_PASS;
  476. break;
  477. case ANTFS_HOST_RESPONSE_ERASE_FAIL:
  478. eWrappedResponse = ANTFS_RESPONSE_ERASE_FAIL;
  479. break;
  480. case ANTFS_HOST_RESPONSE_MANUAL_TRANSFER_PASS:
  481. eWrappedResponse = ANTFS_RESPONSE_MANUAL_TRANSFER_PASS;
  482. break;
  483. case ANTFS_HOST_RESPONSE_MANUAL_TRANSFER_TRANSMIT_FAIL:
  484. eWrappedResponse = ANTFS_RESPONSE_MANUAL_TRANSFER_TRANSMIT_FAIL;
  485. break;
  486. case ANTFS_HOST_RESPONSE_MANUAL_TRANSFER_RESPONSE_FAIL:
  487. eWrappedResponse = ANTFS_RESPONSE_MANUAL_TRANSFER_RESPONSE_FAIL;
  488. break;
  489. case ANTFS_HOST_RESPONSE_CANCEL_DONE:
  490. eWrappedResponse = ANTFS_RESPONSE_CANCEL_DONE;
  491. break;
  492. }
  493. if(eWrappedResponse != ANTFS_RESPONSE_NONE)
  494. {
  495. AddResponse(eWrappedResponse);
  496. }
  497. if (bKillThread)
  498. break;
  499. eUSBResponse = pclMsgHandler->WaitForResponse(100);
  500. switch(eUSBResponse)
  501. {
  502. case ANT_USB_RESPONSE_OPEN_FAIL: // Using PollUSB, so should not get this event
  503. #if defined(DEBUG_FILE)
  504. DSIDebug::ThreadWrite("ANTFSHost::ANTFSThread(): Invalid response ANT_USB_RESPONSE_OPEN_FAIL...");
  505. #endif
  506. case ANT_USB_RESPONSE_NONE:
  507. eWrappedResponse = ANTFS_RESPONSE_NONE;
  508. break;
  509. case ANT_USB_RESPONSE_OPEN_PASS:
  510. eWrappedResponse = ANTFS_RESPONSE_OPEN_PASS;
  511. if(ulHostSerialNumber == 0)
  512. {
  513. ulHostSerialNumber = pclMsgHandler->GetSerialNumber();
  514. pclHost->SetSerialNumber(ulHostSerialNumber);
  515. }
  516. break;
  517. case ANT_USB_RESPONSE_SERIAL_FAIL:
  518. #if defined(DEBUG_FILE)
  519. DSIDebug::ThreadWrite("ANTFSHost::ANTFSThread(): Serial fail from DSIANTDevice ..");
  520. #endif
  521. if(eWrappedResponse != ANTFS_RESPONSE_SERIAL_FAIL) // We do this to avoid reporting a serial failure twice
  522. eWrappedResponse = ANTFS_RESPONSE_SERIAL_FAIL;
  523. else
  524. eWrappedResponse = ANTFS_RESPONSE_NONE;
  525. break;
  526. }
  527. if(eWrappedResponse != ANTFS_RESPONSE_NONE)
  528. {
  529. AddResponse(eWrappedResponse);
  530. }
  531. } // while()
  532. #if defined(DEBUG_FILE)
  533. DSIDebug::ThreadWrite("ANTFSHost::ANTFSThread(): Exiting thread.");
  534. #endif
  535. DSIThread_MutexLock(&stMutexCriticalSection);
  536. bANTFSThreadRunning = FALSE;
  537. DSIThread_CondSignal(&stCondANTFSThreadExit);
  538. DSIThread_MutexUnlock(&stMutexCriticalSection);
  539. }
  540. ///////////////////////////////////////////////////////////////////////
  541. BOOL ANTFSHost::InitHost(UCHAR ucUSBDeviceNum_, USHORT usBaudRate_, BOOL bAutoInit_)
  542. {
  543. if (bInitFailed == TRUE)
  544. {
  545. #if defined(DEBUG_FILE)
  546. DSIDebug::ThreadWrite("ANTFSHost::InitHost(): bInitFailed == TRUE");
  547. #endif
  548. return FALSE;
  549. }
  550. //Ensure we are closed before continuing
  551. this->Close();
  552. bKillThread = FALSE;
  553. if(bAutoInit_)
  554. {
  555. if(pclMsgHandler->Open() == FALSE) // Poll USB
  556. {
  557. #if defined(DEBUG_FILE)
  558. DSIDebug::ThreadWrite("ANTFSHost::InitHost(): Failed to init pclMsgHandler.");
  559. #endif
  560. return FALSE;
  561. }
  562. }
  563. else
  564. {
  565. if(pclMsgHandler->Open(ucUSBDeviceNum_, usBaudRate_) == FALSE) // Poll USB
  566. {
  567. #if defined(DEBUG_FILE)
  568. DSIDebug::ThreadWrite("ANTFSHost::InitHost(): Failed to init pclMsgHandler.");
  569. #endif
  570. return FALSE;
  571. }
  572. }
  573. pclMsgHandler->AddMessageProcessor(DEFAULT_CHANNEL_NUMBER, (DSIANTMessageProcessor*) pclHost); // Initializes channel object
  574. if (hANTFSThread == NULL)
  575. {
  576. hANTFSThread = DSIThread_CreateThread(&ANTFSHost::ANTFSThreadStart, this);
  577. if (hANTFSThread == NULL)
  578. return FALSE;
  579. }
  580. DSIThread_MutexLock(&stMutexResponseQueue);
  581. clResponseQueue.Clear();
  582. DSIThread_MutexUnlock(&stMutexResponseQueue);
  583. bOpen = TRUE;
  584. return TRUE;
  585. }
  586. ///////////////////////////////////////////////////////////////////////
  587. void ANTFSHost::AddResponse(ANTFS_RESPONSE eResponse_)
  588. {
  589. DSIThread_MutexLock(&stMutexResponseQueue);
  590. clResponseQueue.AddResponse(eResponse_);
  591. DSIThread_CondSignal(&stCondWaitForResponse);
  592. DSIThread_MutexUnlock(&stMutexResponseQueue);
  593. }
  594. ///////////////////////////////////////////////////////////////////////
  595. //Returns a response if there is one ready, otherwise waits the specified time for one to occur
  596. ANTFS_RESPONSE ANTFSHost::WaitForResponse(ULONG ulMilliseconds_)
  597. {
  598. ANTFS_RESPONSE stResponse = ANTFS_RESPONSE_NONE;
  599. if (bKillThread == TRUE)
  600. return ANTFS_RESPONSE_NONE;
  601. //Wait for response
  602. DSIThread_MutexLock(&stMutexResponseQueue);
  603. if(clResponseQueue.isEmpty())
  604. {
  605. UCHAR ucResult = DSIThread_CondTimedWait(&stCondWaitForResponse, &stMutexResponseQueue, ulMilliseconds_);
  606. switch(ucResult)
  607. {
  608. case DSI_THREAD_ENONE:
  609. stResponse = clResponseQueue.GetResponse();
  610. break;
  611. case DSI_THREAD_ETIMEDOUT:
  612. stResponse = ANTFS_RESPONSE_NONE;
  613. break;
  614. case DSI_THREAD_EOTHER:
  615. #if defined(DEBUG_FILE)
  616. DSIDebug::ThreadWrite("ANTFSHost::WaitForResponse(): CondTimedWait() Failed!");
  617. #endif
  618. stResponse = ANTFS_RESPONSE_NONE;
  619. break;
  620. default:
  621. #if defined(DEBUG_FILE)
  622. DSIDebug::ThreadWrite("ANTFSHost::WaitForResponse(): Error Unknown");
  623. #endif
  624. stResponse = ANTFS_RESPONSE_NONE;
  625. break;
  626. }
  627. }
  628. else
  629. {
  630. stResponse = clResponseQueue.GetResponse();
  631. }
  632. DSIThread_MutexUnlock(&stMutexResponseQueue);
  633. return stResponse;
  634. }