void Edge305Device::readGpxDataFromDevice() { if (Log::enabledDbg()) { Log::dbg("Thread readGpxData started"); } /* Thread-Status 0 = idle 1 = working 2 = waiting 3 = finished */ lockVariables(); this->threadState = 1; this->transferSuccessful = false; unlockVariables(); string gpxDataXml = readGpxData(); lockVariables(); this->threadState = 3; this->gpxDataGpsXml = gpxDataXml; unlockVariables(); if (Log::enabledDbg()) { Log::dbg("Thread readFitnessData finished"); } }
void Edge305Device::readFitnessDataFromDevice(bool readTrackData, string fitnessDetailId) { Log::dbg("Thread readFitnessData started"); /* Thread-Status 0 = idle 1 = working 2 = waiting 3 = finished */ lockVariables(); this->threadState = 1; this->transferSuccessful = false; unlockVariables(); string fitnessDataXml = readFitnessData(readTrackData, fitnessDetailId); lockVariables(); this->threadState = 3; fitnessDataTcdXml = fitnessDataXml; unlockVariables(); if (Log::enabledDbg()) { Log::dbg("Thread readFitnessData finished"); } }
/** * This is used to indicate the status of the write fitness data process. * @return 0 = idle 1 = working 2 = waiting 3 = finished */ int GarminFilebasedDevice::finishWriteFitnessData() { lockVariables(); int status = this->threadState; unlockVariables(); return status; }
int GarminFilebasedDevice::startWriteToGps(const string filename, const string xml) { string::size_type loc = filename.find( "..", 0 ); if( loc != string::npos ) { Log::err("SECURITY WARNING: Filenames with .. are not allowed!"); return 0; } loc = filename.find( "/", 0 ); if( loc != string::npos ) { Log::err("SECURITY WARNING: Filenames with / are not allowed!"); return 0; } // There shouldn't be a thread running... but who knows... lockVariables(); this->xmlToWrite = xml; this->filenameToWrite = gpxDirectory + "/" + filename; this->overwriteFile = 2; // not yet asked this->workType = WRITEGPX; unlockVariables(); if (Log::enabledDbg()) Log::dbg("Saving to file: "+this->filenameToWrite); if (startThread()) { return 1; } return 0; }
int Edge305Device::finishReadFitnessDetail() { lockVariables(); int status = this->threadState; unlockVariables(); return status; }
void GarminFilebasedDevice::userAnswered(const int answer) { if (answer == 1) { if (Log::enabledDbg()) Log::dbg("User wants file overwritten"); lockVariables(); this->overwriteFile = 1; unlockVariables(); } else { if (Log::enabledDbg()) Log::dbg("User wants file to be untouched"); lockVariables(); this->overwriteFile = 0; unlockVariables(); } lockVariables(); this->threadState = 1; /* set back to working */ unlockVariables(); signalThread(); }
int Edge305Device::getThreadState() { /* 0 = idle 1 = working 2 = waiting 3 = finished */ lockVariables(); int status = this->threadState; unlockVariables(); return status; }
int GarminFilebasedDevice::finishWriteToGps() { /* 0 = idle 1 = working 2 = waiting 3 = finished */ lockVariables(); int status = this->threadState; unlockVariables(); return status; }
/** * Starts a thread that writes the passed xml string to the given filename * @param filename - filename on disk * @param data - the filename to write to on the device. * @param dataTypeName - a Fitness DataType from the GarminDevice.xml retrieved with DeviceDescription * @return int returns 1 if successful otherwise 0 */ int GarminFilebasedDevice::startWriteFitnessData(string filename, string data, string dataTypeName) { string::size_type loc = filename.find( "../", 0 ); if( loc != string::npos ) { Log::err("SECURITY WARNING: Filenames with ../ are not allowed! "+filename); return 0; } string pathToWrite = ""; for (list<MassStorageDirectoryType>::iterator it=deviceDirectories.begin(); it!=deviceDirectories.end(); it++) { MassStorageDirectoryType dt = (*it); if ((dataTypeName.compare(dt.name) == 0) && (dt.writeable)) { pathToWrite = dt.path; } } if (pathToWrite.length() == 0) { Log::err("Path for " + dataTypeName + " not found. Not writing to device!"); return 0; } // There shouldn't be a thread running... but who knows... lockVariables(); this->xmlToWrite = data; this->filenameToWrite = this->baseDirectory + "/" + pathToWrite + "/" + filename; this->overwriteFile = 2; // not yet asked this->workType = WRITEFITNESSDATA; unlockVariables(); if (Log::enabledDbg()) Log::dbg("Saving to file: "+this->filenameToWrite); if (startThread()) { return 1; } return 0; return 0; }
void GarminFilebasedDevice::writeGpxFile() { lockVariables(); string xml = this->xmlToWrite; string filename = this->filenameToWrite; string systemCmd = this->storageCmd; this->threadState = 1; // Working unlockVariables(); struct stat stFileInfo; int intStat; // Attempt to get the file attributes intStat = stat(filename.c_str(),&stFileInfo); if(intStat == 0) { // File exists - we need to ask the user to overwrite lockVariables(); this->waitingMessage = new MessageBox(Question, "File "+filename+" exists. Overwrite?", BUTTON_YES | BUTTON_NO , BUTTON_NO, this); this->threadState = 2; unlockVariables(); waitThread(); // Sleep until thread gets signal (user answered) bool doOverwrite = true; lockVariables(); if (this->overwriteFile != 1) { this->threadState = 3; this->transferSuccessful = false; doOverwrite = false; } unlockVariables(); if (!doOverwrite) { Log::dbg("Thread aborted"); return; } } ofstream file; file.open (filename.c_str()); file << xml; file.close(); // Execute extern command if wanted if (systemCmd.length() > 0) { string placeholder = "%1"; int pos=systemCmd.find( placeholder ); if (pos >=0) { systemCmd.replace(systemCmd.find(placeholder),placeholder.length(),filename); } pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL); Log::dbg("Thread before executing user command: "+systemCmd); int ret = system(systemCmd.c_str()); pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL); if (ret != 0) { lockVariables(); this->waitingMessage = new MessageBox(Question, "Error executing command: "+systemCmd, BUTTON_OK , BUTTON_OK, NULL); this->threadState = 2; unlockVariables(); sleep(1); // give application time to fetch messagebox lockVariables(); this->threadState = 3; unlockVariables(); Log::err("Executing user command failed: "+systemCmd); return; } } lockVariables(); this->threadState = 3; // Finished this->transferSuccessful = true; // Successfull; unlockVariables(); }