void doReset(ant_rx_thread_info_t *stRxThreadInfo) { int iMutexLockResult; ANT_FUNC_START(); /* Chip was reset or other error, only way to recover is to * close and open ANT chardev */ stRxThreadInfo->ucChipResetting = 1; if (g_fnStateCallback) { g_fnStateCallback(RADIO_STATUS_RESETTING); } stRxThreadInfo->ucRunThread = 0; ANT_DEBUG_V("getting stEnabledStatusLock in %s", __FUNCTION__); iMutexLockResult = pthread_mutex_lock(stRxThreadInfo->pstEnabledStatusLock); if (iMutexLockResult < 0) { ANT_ERROR("chip was reset, getting state mutex failed: %s", strerror(iMutexLockResult)); stRxThreadInfo->stRxThread = 0; stRxThreadInfo->ucChipResetting = 0; } else { ANT_DEBUG_V("got stEnabledStatusLock in %s", __FUNCTION__); stRxThreadInfo->stRxThread = 0; /* spoof our handle as closed so we don't * try to join ourselves in disable */ ant_disable(); int enableResult = ant_enable(); stRxThreadInfo->ucChipResetting = 0; if (enableResult) { /* failed */ if (g_fnStateCallback) { g_fnStateCallback(RADIO_STATUS_DISABLED); } } else { /* success */ if (g_fnStateCallback) { g_fnStateCallback(RADIO_STATUS_RESET); } } ANT_DEBUG_V("releasing stEnabledStatusLock in %s", __FUNCTION__); pthread_mutex_unlock(stRxThreadInfo->pstEnabledStatusLock); ANT_DEBUG_V("released stEnabledStatusLock in %s", __FUNCTION__); } ANT_FUNC_END(); }
ANTStatus ant_radio_hard_reset(void) { ANTStatus result_status = ANT_STATUS_NOT_SUPPORTED; ANT_FUNC_START(); #ifdef ANT_IOCTL_RESET ant_channel_type eChannel; int iLockResult; result_status = ANT_STATUS_FAILED; ANT_DEBUG_V("getting stEnabledStatusLock in %s", __FUNCTION__); iLockResult = pthread_mutex_lock(&stEnabledStatusLock); if(iLockResult) { ANT_ERROR("enable failed to get state lock: %s", strerror(iLockResult)); goto out; } ANT_DEBUG_V("got stEnabledStatusLock in %s", __FUNCTION__); stRxThreadInfo.ucChipResetting = 1; if (g_fnStateCallback) g_fnStateCallback(RADIO_STATUS_RESETTING); #ifdef ANT_IOCTL_RESET_PARAMETER ioctl(stRxThreadInfo.astChannels[0].iFd, ANT_IOCTL_RESET, ANT_IOCTL_RESET_PARAMETER); #else ioctl(stRxThreadInfo.astChannels[0].iFd, ANT_IOCTL_RESET); #endif // ANT_IOCTL_RESET_PARAMETER ant_disable(); if (ant_enable()) { /* failed */ if (g_fnStateCallback) g_fnStateCallback(RADIO_STATUS_DISABLED); } else { /* success */ if (g_fnStateCallback) g_fnStateCallback(RADIO_STATUS_RESET); result_status = ANT_STATUS_SUCCESS; } stRxThreadInfo.ucChipResetting = 0; ANT_DEBUG_V("releasing stEnabledStatusLock in %s", __FUNCTION__); pthread_mutex_unlock(&stEnabledStatusLock); ANT_DEBUG_V("released stEnabledStatusLock in %s", __FUNCTION__); out: #endif // ANT_IOCTL_RESET ANT_FUNC_END(); return result_status; }
ANTStatus ant_enable_radio(void) { int iLockResult; ANTStatus result_status = ANT_STATUS_FAILED; ANT_FUNC_START(); ANT_DEBUG_V("getting stEnabledStatusLock in %s", __FUNCTION__); iLockResult = pthread_mutex_lock(&stEnabledStatusLock); if(iLockResult) { ANT_ERROR("enable failed to get state lock: %s", strerror(iLockResult)); goto out; } ANT_DEBUG_V("got stEnabledStatusLock in %s", __FUNCTION__); if (g_fnStateCallback) { g_fnStateCallback(RADIO_STATUS_ENABLING); } if (ant_enable() < 0) { ANT_ERROR("ant enable failed: %s", strerror(errno)); ant_disable(); if (g_fnStateCallback) { g_fnStateCallback(ant_radio_enabled_status()); } } else { if (g_fnStateCallback) { g_fnStateCallback(RADIO_STATUS_ENABLED); } result_status = ANT_STATUS_SUCCESS; } ANT_DEBUG_V("releasing stEnabledStatusLock in %s", __FUNCTION__); pthread_mutex_unlock(&stEnabledStatusLock); ANT_DEBUG_V("released stEnabledStatusLock in %s", __FUNCTION__); out: ANT_FUNC_END(); return result_status; }
ANTStatus ant_enable_radio(void) { int result; int lockResult; ANTStatus result_status = ANT_STATUS_FAILED; ANT_FUNC_START(); ANT_DEBUG_V("getting enableLock in %s", __FUNCTION__); lockResult = pthread_mutex_lock(&enableLock); if(lockResult) { ANT_ERROR("Enable failed to get enableLock mutex: %s", strerror(lockResult)); return ANT_STATUS_FAILED; } ANT_DEBUG_V("got enableLock in %s", __FUNCTION__); if(RADIO_STATUS_DISABLED == radio_status) { radio_status = RADIO_STATUS_ENABLING; } ANT_DEBUG_V("getting txLock in %s", __FUNCTION__); lockResult = pthread_mutex_lock(&txLock); if (lockResult) { ANT_ERROR("Enable txLock failed: %s", strerror(lockResult)); pthread_mutex_unlock(&enableLock); return ANT_STATUS_FAILED; } ANT_DEBUG_V("got txLock in %s", __FUNCTION__); if (get_and_set_radio_status() != RADIO_STATUS_ENABLED) { if (RxParams.thread) { ANT_WARN("in enable call: rx thread still exists but radio crashed. trying to recover"); ANT_DEBUG_V("radio crashed, letting rx thread join"); pthread_join(RxParams.thread, NULL); RxParams.thread = 0; ANT_DEBUG_V("recovered. joined by rx thread"); } ANT_DEBUG_I("radio_status (%d -> %d)", radio_status, RADIO_STATUS_ENABLING); radio_status = RADIO_STATUS_ENABLING; #if USE_EXTERNAL_POWER_LIBRARY if (RxParams.pfStateCallback) RxParams.pfStateCallback(radio_status); #endif } #if USE_EXTERNAL_POWER_LIBRARY result = ant_enable(); ANT_DEBUG_D("ant_enable() result is %d", result); #else result = 0; #endif if (result == 0) { if (RxParams.thread) { result_status = ANT_STATUS_SUCCESS; radio_status = RADIO_STATUS_ENABLED; // sanity assign, cant be enabling ANT_DEBUG_D("ANT radio re-enabled"); } else { result = pthread_create(&RxParams.thread, NULL, ANTHCIRxThread, NULL); if (result) { ANT_ERROR("Thread initialization failed: %s", strerror(result)); result_status = ANT_STATUS_FAILED; } else { result_status = ANT_STATUS_SUCCESS; #if USE_EXTERNAL_POWER_LIBRARY if (radio_status == RADIO_STATUS_ENABLING) { ANT_DEBUG_I("radio_status (%d -> %d)", radio_status, RADIO_STATUS_ENABLED); radio_status = RADIO_STATUS_ENABLED; if (RxParams.pfStateCallback) RxParams.pfStateCallback(radio_status); } else { ANT_WARN("radio was already enabled but rx thread was not running"); } #else radio_status = RADIO_STATUS_ENABLED; #endif } } } else { result_status = ANT_STATUS_TRANSPORT_INIT_ERR; } if (result_status != ANT_STATUS_SUCCESS) // ant_enable() or rx thread creating failed { #if USE_EXTERNAL_POWER_LIBRARY ant_disable(); #endif switch (get_and_set_radio_status()) { case RADIO_STATUS_ENABLED: ANT_ERROR("SERIOUS: enable failed, but ANT is enabled without a rx thread"); break; default: ANT_DEBUG_D("Enable failed, after cleanup chip is not enabled"); break; } } ANT_DEBUG_V("releasing txLock in %s", __FUNCTION__); pthread_mutex_unlock(&txLock); ANT_DEBUG_V("released txLock in %s", __FUNCTION__); ANT_DEBUG_V("releasing enableLock in %s", __FUNCTION__); pthread_mutex_unlock(&enableLock); ANT_DEBUG_V("released enableLock in %s", __FUNCTION__); ANT_FUNC_END(); return result_status; }