tile_t* tilecache_get(tilecache_t *cache, int zoom, int x, int y) { LOCK_MUTEX(&cache->lock); tilecache_slot_t *slot = cache->head; tile_t *tile = NULL; gboolean found = FALSE; for (; slot; slot=slot->next) { tile = slot->tile; if (tile->zoom == zoom && tile->x == x && tile->y == y) { found = TRUE; //log_debug("tilecache_get: x=%d, y=%d, zoom=%d, found", x, y, zoom); break; } } UNLOCK_MUTEX(&cache->lock); if (found) return tile; else return NULL; }
static int _is_job_running(struct workqueue_ctx* ctx, int job_id) { int i; int ret = 0; int rc; for (i = 0; i < ctx->num_worker_threads && !ret; i++) { if (ctx->thread[i]) { TRY_LOCK_MUTEX(&ctx->thread[i]->mutex, rc); #ifdef WINDOWS if (rc) return -EBUSY; #else if (rc == EBUSY) return -EBUSY; #endif if (ctx->thread[i]->job && ctx->thread[i]->job->job_id == job_id) { ret = 1; } UNLOCK_MUTEX(&ctx->thread[i]->mutex); } } return ret; }
int workqueue_show_status(struct workqueue_ctx* ctx, FILE *fp) { int i; long long time_ms; struct TIME_STRUCT_TYPE now_time; GET_TIME(now_time); LOCK_MUTEX(&ctx->mutex); fprintf(fp, "Number of worker threads=%d \n", ctx->num_worker_threads); fprintf(fp, "Total jobs added=%d queue_size=%d waiting_jobs=%d \n", ctx->job_count, ctx->queue_size, ctx->waiting_jobs); fprintf(fp, "\n"); fprintf(fp, "%3s | %8s | %4s | %6s ms\n", "Qi", "JobID", "Pri", "Time" ); fprintf(fp, "---------------------------------\n"); for (i = 0; i < ctx->queue_size; i++) { if (!ctx->queue[i]) continue; /* unused location */ if (TIME_SEC(ctx->queue[i]->start_time) || TIME_MSEC(ctx->queue[i]->start_time)) { // has been scheduled for a time in the future. time_ms = time_diff_ms(&ctx->queue[i]->start_time, &now_time); } else { // will run ASAP time_ms = 0; } fprintf(fp,"%3d | %8d | %4d | %6lld ms\n", i, ctx->queue[i]->job_id, ctx->queue[i]->priority, time_ms); } UNLOCK_MUTEX(&ctx->mutex); fflush(fp); return 0; }
/** * Tells the specified scheduler it should stop processing its tasks. **/ void io_sched_stop_scheduler ( p_io_scheduler_t scheduler ) { p_io_scheduler_task_t task; LOGSVC_DEBUG( "io_sched_stop_scheduler()" ); if ( scheduler ) { // Clear out the scheduled tasks. LOCK_MUTEX( scheduler->task_list_mutex ); task = scheduler->scheduled_tasks; while ( task ) { task->opts |= IO_SCHEDULER_REMOVE; task = task->next; } scheduler->stop_scheduler = CMNUTIL_TRUE; UNLOCK_MUTEX( scheduler->task_list_mutex ); if ( scheduler->scheduler_thread ) { //LOGSVC_DEBUG( "Killing scheduler thread" ); //pthread_kill ( scheduler->scheduler_thread, SIGKILL ); //LOGSVC_DEBUG( "Detaching scheduler thread" ); //pthread_detach ( scheduler->scheduler_thread ); LOGSVC_DEBUG( "Cancelling scheduler thread" ); if ( pthread_cancel ( scheduler->scheduler_thread ) == 0 ) { LOGSVC_DEBUG( "Scheduler thread cancelled" ); pthread_join ( scheduler->scheduler_thread, NULL ); LOGSVC_DEBUG( "Scheduler thread joined" ); } } //else { // LOGSVC_DEBUG( "Setting scheduler stop flag" ); // scheduler->stop_scheduler = CMNUTIL_TRUE; //} } }
void tilecache_cleanup(tilecache_t *cache, gboolean free_cache) { if (! cache) return; LOCK_MUTEX(&cache->lock); tilecache_slot_t *slot = cache->head, *next; while(slot) { next = slot->next; free_slot_tile(slot->tile); free(slot); slot = next; } cache->head = cache->tail = NULL; cache->count = 0; UNLOCK_MUTEX(&cache->lock); if (free_cache) { pthread_mutex_destroy(&(cache->lock)); free(cache); } }
/*! \fn void Posix_QextSerialPort::setParity(ParityType parity) Sets the parity associated with the serial port. The possible values of parity are: \verbatim PAR_SPACE Space Parity PAR_MARK Mark Parity PAR_NONE No Parity PAR_EVEN Even Parity PAR_ODD Odd Parity \endverbatim \note This function is subject to the following limitations: \par POSIX systems do not support mark parity. \par POSIX systems support space parity only if tricked into doing so, and only with fewer than 8 data bits. Use space parity very carefully with POSIX systems. */ void Posix_QextSerialPort::setParity(ParityType parity) { LOCK_MUTEX(); if (Settings.Parity!=parity) { if (parity==PAR_MARK || (parity==PAR_SPACE && Settings.DataBits==DATA_8)) { } else { Settings.Parity=parity; } } if (isOpen()) { switch (parity) { /*space parity*/ case PAR_SPACE: if (Settings.DataBits==DATA_8) { TTY_PORTABILITY_WARNING("Posix_QextSerialPort: Space parity is only supported in POSIX with 7 or fewer data bits"); } else { /*space parity not directly supported - add an extra data bit to simulate it*/ Posix_CommConfig.c_cflag&=~(PARENB|CSIZE); switch(Settings.DataBits) { case DATA_5: Settings.DataBits=DATA_6; Posix_CommConfig.c_cflag|=CS6; break; case DATA_6: Settings.DataBits=DATA_7; Posix_CommConfig.c_cflag|=CS7; break; case DATA_7: Settings.DataBits=DATA_8; Posix_CommConfig.c_cflag|=CS8; break; case DATA_8: break; } tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } break; /*mark parity - WINDOWS ONLY*/ case PAR_MARK: TTY_WARNING("Posix_QextSerialPort: Mark parity is not supported by POSIX."); break; /*no parity*/ case PAR_NONE: Posix_CommConfig.c_cflag&=(~PARENB); tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); break; /*even parity*/ case PAR_EVEN: Posix_CommConfig.c_cflag&=(~PARODD); Posix_CommConfig.c_cflag|=PARENB; tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); break; /*odd parity*/ case PAR_ODD: Posix_CommConfig.c_cflag|=(PARENB|PARODD); tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); break; } } UNLOCK_MUTEX(); }
/*! \fn void Posix_QextSerialPort::setBaudRate(BaudRateType baudRate) Sets the baud rate of the serial port. Note that not all rates are applicable on all platforms. The following table shows translations of the various baud rate constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * are speeds that are usable on both Windows and POSIX. \note BAUD76800 may not be supported on all POSIX systems. SGI/IRIX systems do not support BAUD1800. \verbatim RATE Windows Speed POSIX Speed ----------- ------------- ----------- BAUD50 110 50 BAUD75 110 75 *BAUD110 110 110 BAUD134 110 134.5 BAUD150 110 150 BAUD200 110 200 *BAUD300 300 300 *BAUD600 600 600 *BAUD1200 1200 1200 BAUD1800 1200 1800 *BAUD2400 2400 2400 *BAUD4800 4800 4800 *BAUD9600 9600 9600 BAUD14400 14400 9600 *BAUD19200 19200 19200 *BAUD38400 38400 38400 BAUD56000 56000 38400 *BAUD57600 57600 57600 BAUD76800 57600 76800 *BAUD115200 115200 115200 BAUD128000 128000 115200 BAUD256000 256000 115200 \endverbatim */ void Posix_QextSerialPort::setBaudRate(BaudRateType baudRate) { LOCK_MUTEX(); if (Settings.BaudRate!=baudRate) { switch (baudRate) { case BAUD14400: Settings.BaudRate=BAUD9600; break; case BAUD56000: Settings.BaudRate=BAUD38400; break; case BAUD76800: #ifndef B76800 Settings.BaudRate=BAUD57600; #else Settings.BaudRate=baudRate; #endif break; case BAUD128000: case BAUD256000: Settings.BaudRate=BAUD115200; break; default: Settings.BaudRate=baudRate; break; } } if (isOpen()) { switch (baudRate) { /*50 baud*/ case BAUD50: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 50 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B50; #else cfsetispeed(&Posix_CommConfig, B50); cfsetospeed(&Posix_CommConfig, B50); #endif break; /*75 baud*/ case BAUD75: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 75 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B75; #else cfsetispeed(&Posix_CommConfig, B75); cfsetospeed(&Posix_CommConfig, B75); #endif break; /*110 baud*/ case BAUD110: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B110; #else cfsetispeed(&Posix_CommConfig, B110); cfsetospeed(&Posix_CommConfig, B110); #endif break; /*134.5 baud*/ case BAUD134: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 134.5 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B134; #else cfsetispeed(&Posix_CommConfig, B134); cfsetospeed(&Posix_CommConfig, B134); #endif break; /*150 baud*/ case BAUD150: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 150 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B150; #else cfsetispeed(&Posix_CommConfig, B150); cfsetospeed(&Posix_CommConfig, B150); #endif break; /*200 baud*/ case BAUD200: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 200 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B200; #else cfsetispeed(&Posix_CommConfig, B200); cfsetospeed(&Posix_CommConfig, B200); #endif break; /*300 baud*/ case BAUD300: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B300; #else cfsetispeed(&Posix_CommConfig, B300); cfsetospeed(&Posix_CommConfig, B300); #endif break; /*600 baud*/ case BAUD600: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B600; #else cfsetispeed(&Posix_CommConfig, B600); cfsetospeed(&Posix_CommConfig, B600); #endif break; /*1200 baud*/ case BAUD1200: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B1200; #else cfsetispeed(&Posix_CommConfig, B1200); cfsetospeed(&Posix_CommConfig, B1200); #endif break; /*1800 baud*/ case BAUD1800: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows and IRIX do not support 1800 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B1800; #else cfsetispeed(&Posix_CommConfig, B1800); cfsetospeed(&Posix_CommConfig, B1800); #endif break; /*2400 baud*/ case BAUD2400: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B2400; #else cfsetispeed(&Posix_CommConfig, B2400); cfsetospeed(&Posix_CommConfig, B2400); #endif break; /*4800 baud*/ case BAUD4800: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B4800; #else cfsetispeed(&Posix_CommConfig, B4800); cfsetospeed(&Posix_CommConfig, B4800); #endif break; /*9600 baud*/ case BAUD9600: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B9600; #else cfsetispeed(&Posix_CommConfig, B9600); cfsetospeed(&Posix_CommConfig, B9600); #endif break; /*14400 baud*/ case BAUD14400: TTY_WARNING("Posix_QextSerialPort: POSIX does not support 14400 baud operation. Switching to 9600 baud."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B9600; #else cfsetispeed(&Posix_CommConfig, B9600); cfsetospeed(&Posix_CommConfig, B9600); #endif break; /*19200 baud*/ case BAUD19200: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B19200; #else cfsetispeed(&Posix_CommConfig, B19200); cfsetospeed(&Posix_CommConfig, B19200); #endif break; /*38400 baud*/ case BAUD38400: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B38400; #else cfsetispeed(&Posix_CommConfig, B38400); cfsetospeed(&Posix_CommConfig, B38400); #endif break; /*56000 baud*/ case BAUD56000: TTY_WARNING("Posix_QextSerialPort: POSIX does not support 56000 baud operation. Switching to 38400 baud."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B38400; #else cfsetispeed(&Posix_CommConfig, B38400); cfsetospeed(&Posix_CommConfig, B38400); #endif break; /*57600 baud*/ case BAUD57600: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B57600; #else cfsetispeed(&Posix_CommConfig, B57600); cfsetospeed(&Posix_CommConfig, B57600); #endif break; /*76800 baud*/ case BAUD76800: TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows and some POSIX systems do not support 76800 baud operation."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); #ifdef B76800 Posix_CommConfig.c_cflag|=B76800; #else TTY_WARNING("Posix_QextSerialPort: Posix_QextSerialPort was compiled without 76800 baud support. Switching to 57600 baud."); Posix_CommConfig.c_cflag|=B57600; #endif //B76800 #else //CBAUD #ifdef B76800 cfsetispeed(&Posix_CommConfig, B76800); cfsetospeed(&Posix_CommConfig, B76800); #else TTY_WARNING("Posix_QextSerialPort: Posix_QextSerialPort was compiled without 76800 baud support. Switching to 57600 baud."); cfsetispeed(&Posix_CommConfig, B57600); cfsetospeed(&Posix_CommConfig, B57600); #endif //B76800 #endif //CBAUD break; /*115200 baud*/ case BAUD115200: #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B115200; #else cfsetispeed(&Posix_CommConfig, B115200); cfsetospeed(&Posix_CommConfig, B115200); #endif break; /*128000 baud*/ case BAUD128000: TTY_WARNING("Posix_QextSerialPort: POSIX does not support 128000 baud operation. Switching to 115200 baud."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B115200; #else cfsetispeed(&Posix_CommConfig, B115200); cfsetospeed(&Posix_CommConfig, B115200); #endif break; /*256000 baud*/ case BAUD256000: TTY_WARNING("Posix_QextSerialPort: POSIX does not support 256000 baud operation. Switching to 115200 baud."); #ifdef CBAUD Posix_CommConfig.c_cflag&=(~CBAUD); Posix_CommConfig.c_cflag|=B115200; #else cfsetispeed(&Posix_CommConfig, B115200); cfsetospeed(&Posix_CommConfig, B115200); #endif break; } tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } UNLOCK_MUTEX(); }
int submit_data_chunk (buf_t *buf, char *data, size_t size) { long buf_write_pos; /* offset of first available write location */ size_t write_size; DEBUG1("Enter submit_data_chunk, size %d", size); pthread_cleanup_push(buffer_mutex_unlock, buf); /* Put the data into the buffer as space is made available */ while (size > 0 && !buf->abort_write) { /* Section 1: Write a chunk of data */ DEBUG("Obtaining lock on buffer"); LOCK_MUTEX(buf->mutex); if (buf->size - buf->curfill > 0) { /* Figure how much we can write into the buffer. Requirements: 1. Don't write more data than we have. 2. Don't write more data than we have room for. 3. Don't write past the end of the buffer. */ buf_write_pos = (buf->start + buf->curfill) % buf->size; write_size = MIN3(size, buf->size - buf->curfill, buf->size - buf_write_pos); memcpy(buf->buffer + buf_write_pos, data, write_size); buf->curfill += write_size; data += write_size; size -= write_size; buf->position_end += write_size; DEBUG1("writing chunk into buffer, curfill = %ld", buf->curfill); } else { if (buf->cancel_flag || sig_request.cancel) { UNLOCK_MUTEX(buf->mutex); break; } /* No room for more data, wait until there is */ DEBUG("No room for data in buffer. Waiting."); COND_WAIT(buf->write_cond, buf->mutex); } /* Section 2: signal if we are not prebuffering, done prebuffering, or paused */ if (buf->prebuffering && (buf->prebuffer_size <= buf->curfill)) { DEBUG("prebuffering done") buf->prebuffering = 0; /* done prebuffering */ } if (!buf->prebuffering && !buf->paused) { DEBUG("Signalling playback thread that more data is available."); COND_SIGNAL(buf->playback_cond); } else DEBUG("Not signalling playback thread since prebuffering or paused."); UNLOCK_MUTEX(buf->mutex); } pthread_cleanup_pop(0); DEBUG("Exit submit_data_chunk"); return !buf->abort_write; }
void buffer_mutex_unlock (void *arg) { buf_t *buf = (buf_t *)arg; UNLOCK_MUTEX(buf->mutex); }
/*! \fn void Win_QextSerialPort::close() Closes a serial port. This function has no effect if the serial port associated with the class is not currently open. */ void Win_QextSerialPort::close() { LOCK_MUTEX(); CloseHandle(Win_Handle); QIODevice::close(); UNLOCK_MUTEX(); }
int playMusic( sp_session *session , char *uri , char *name , playqueue_fifo_t *playqueue ) { TRACE_2( PLAYERMANAGER , "playMusic()."); static int firstTime = 0; int status = PC_SUCCESS; char response[255] = { 0 }; sp_error error; LOCK_MUTEX( PLAYERMANAGER , &mutexSession ); TRACE_3( PLAYERMANAGER , "Test if a music is playing or not"); if( currentTrack == NULL ) { TRACE_WARNING( PLAYERMANAGER , "Cannot play track because no track has been loaded."); status = PC_ERROR; snprintf( response , 255 , "NOK: Cannot play track because no track has been loaded."); sendVoid( ( void * )response , strlen( response ) ); } else { TRACE_1( PLAYERMANAGER , "Getting the track."); currentTrack = getNextTrackToPlayqueue( playqueue ); loadTrack( session , currentTrack ); error = sp_session_player_play( session , 1 ); if( error != SP_ERROR_OK ) { TRACE_ERROR( PLAYERMANAGER , "Cannot play track, reason: %s" , sp_error_message( error ) ); status = PC_ERROR; } else { TRACE_1( PLAYERMANAGER , "Success to play track."); if( firstTime++ != 0 ) playStream( name ); playing = TRUE; snprintf( response , 255 , "OK"); sendVoid( ( void * )response , strlen( response ) ); } if( currentStreamName[0] == 0 ) { strncpy( currentStreamName , name , strlen( name ) ); } else if( strcmp( currentStreamName , name ) != 0 ) { memset( currentStreamName , 0 , 255 ); strncpy( currentStreamName , name , strlen( name ) ); } else if( !strcmp( currentStreamName , name ) ) { //Do nothing } } UNLOCK_MUTEX( PLAYERMANAGER , &mutexSession ); return status; }
void *workThreadReceive( void *pObject ) #endif { #ifdef WIN32 DWORD errorCode = 0; #else int rv = 0; #endif CPeakObj * pobj = ( CPeakObj *)pObject; if ( NULL == pobj ) { #ifdef WIN32 ExitThread( errorCode ); // Fail #else pthread_exit( &rv ); #endif } PeakCanMsg peakMsg; while ( pobj->m_bRun ) { // Noting to do if we should end... if ( !pobj->m_bRun ) continue; LOCK_MUTEX( pobj->m_peakMutex ); while ( 0 == ( pobj->m_procRead( &peakMsg ) & PEAK_CAN_ERR_QRCVEMPTY ) ) { // Check if this is a status message if ( PCAN_MSGTYPE_STATUS & peakMsg.msgType ) { continue; // TODO } // Write to the receive buffer if ( pobj->m_receiveList.nCount < PEAKDRV_MAX_RCVMSG ) { PCANALMSG pMsg = new canalMsg; pMsg->flags = 0; if ( NULL != pMsg ) { dllnode *pNode = new dllnode; if ( NULL != pNode ) { pMsg->timestamp = GetTickCount() * 1000; pMsg->id = peakMsg.id; pMsg->sizeData = peakMsg.len; memcpy( pMsg->data, peakMsg.data, pMsg->sizeData ); // If extended set extended flag if ( PCAN_MSGTYPE_EXTENDED & peakMsg.msgType ) { pMsg->flags |= CANAL_IDFLAG_EXTENDED; } // Check for RTS package if ( PCAN_MSGTYPE_RTR & peakMsg.msgType ) { pMsg->flags |= CANAL_IDFLAG_RTR; } pNode->pObject = pMsg; LOCK_MUTEX( pobj->m_receiveMutex ); dll_addNode( &pobj->m_receiveList, pNode ); UNLOCK_MUTEX( pobj->m_receiveMutex ); // Update statistics pobj->m_stat.cntReceiveData += pMsg->sizeData; pobj->m_stat.cntReceiveFrames += 1; } else { delete pMsg; } } } else { // Full buffer pobj->m_stat.cntOverruns++; } } // while rcv msg UNLOCK_MUTEX( pobj->m_peakMutex ); SLEEP( 1 ); } // while #ifdef WIN32 ExitThread( errorCode ); #else pthread_exit( &rv ); #endif }
void *workThreadTransmit( void *pObject ) #endif { #ifdef WIN32 DWORD errorCode = 0; #else int rv = 0; #endif CPeakObj * pobj = ( CPeakObj *)pObject; if ( NULL == pobj ) { #ifdef WIN32 ExitThread( errorCode ); // Fail #else pthread_exit( &rv ); #endif } while ( pobj->m_bRun ) { LOCK_MUTEX( pobj->m_peakMutex ); // Noting to do if we should end... if ( !pobj->m_bRun ) continue; // Is there something to transmit... int ret; while ( ( NULL != pobj->m_transmitList.pHead ) && ( NULL != pobj->m_transmitList.pHead->pObject ) ) { canalMsg msg; memcpy( &msg, pobj->m_transmitList.pHead->pObject, sizeof( canalMsg ) ); LOCK_MUTEX( pobj->m_transmitMutex ); dll_removeNode( &pobj->m_transmitList, pobj->m_transmitList.pHead ); UNLOCK_MUTEX( pobj->m_transmitMutex ); PeakCanMsg peakMsg; peakMsg.id = msg.id; peakMsg.msgType = 0; peakMsg.len = msg.sizeData; memcpy( peakMsg.data, msg.data, peakMsg.len ); // Check if RTR if ( ( msg.flags & CANAL_IDFLAG_RTR ) ) { peakMsg.msgType |= PCAN_MSGTYPE_RTR; } // Check if extended if ( ( msg.flags & CANAL_IDFLAG_EXTENDED ) ) { peakMsg.msgType |= PCAN_MSGTYPE_EXTENDED; } if ( PEAK_CAN_ERR_OK == ( ret = pobj->m_procWrite( &peakMsg ) ) ) { // Message sent successfully // Update statistics pobj->m_stat.cntTransmitData += msg.sizeData; pobj->m_stat.cntTransmitFrames += 1; } else { // Failed - put message back in queue front PCANALMSG pMsg = new canalMsg; if ( NULL != pMsg ) { // Copy in data memcpy ( pMsg, &msg, sizeof( canalMsg ) ); dllnode *pNode = new dllnode; if ( NULL != pNode ) { pNode->pObject = pMsg; LOCK_MUTEX( pobj->m_transmitMutex ); dll_addNodeHead( &pobj->m_transmitList, pNode ); UNLOCK_MUTEX( pobj->m_transmitMutex ); } else { delete pMsg; } } // unable to allocate storage } // faild to send message } // while data // No data to write UNLOCK_MUTEX( pobj->m_peakMutex ); SLEEP( 1 ); //} } // while #ifdef WIN32 ExitThread( errorCode ); #else pthread_exit( &rv ); #endif }
// This function reads the current Win_CommConfig settings and updates this // structure with saved settings bool Win_QextSerialPort::UpdateComConfig(void) { // Question: Is it possible to change the win_commConfig settings while the port is open? - yes, but not all settings!!! Baud rate can only be changed on closed ports. // BUG replace global win_commConfig COMMCONFIG Win_CommConfig; COMMCONFIG readCommConfig; Q_ASSERT(Win_Handle!=INVALID_HANDLE_VALUE); LOCK_MUTEX(); unsigned long confSize = sizeof(COMMCONFIG); Win_CommConfig.dwSize = confSize; // TODO: what is this? // read current settings GetCommConfig(Win_Handle, &Win_CommConfig, &confSize); GetCommState(Win_Handle, &(Win_CommConfig.dcb)); /*set up default parameters*/ Win_CommConfig.dcb.fBinary = TRUE; Win_CommConfig.dcb.fAbortOnError = FALSE; Win_CommConfig.dcb.fNull = FALSE; Win_CommConfig.dcb.Parity = NOPARITY; Win_CommConfig.dcb.StopBits = ONESTOPBIT; Win_CommConfig.dcb.fParity = TRUE; // data bit settings switch (Settings.DataBits) { case DATA_5:/*5 data bits*/ if (Settings.StopBits==STOP_2) { //BUG think about warnings TTY_WARNING("Win_QextSerialPort: 5 Data bits cannot be used with 2 stop bits."); } else { Win_CommConfig.dcb.ByteSize=5; } break; case DATA_6:/*6 data bits*/ if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Win_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits."); } else { Win_CommConfig.dcb.ByteSize=6; } break; case DATA_7:/*7 data bits*/ if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Win_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits."); } else { Win_CommConfig.dcb.ByteSize=7; } break; case DATA_8:/*8 data bits*/ if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Win_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits."); } else { Win_CommConfig.dcb.ByteSize=8; } break; default: Q_ASSERT(0); // This should never happen BUG replace by a error message } // parity settings switch (Settings.Parity) { case PAR_SPACE: /*space parity*/ if (Settings.DataBits==DATA_8) { // BUG this assumes that data was set first TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems."); } Win_CommConfig.dcb.fParity=TRUE; // enable parity checking Win_CommConfig.dcb.Parity=SPACEPARITY; break; case PAR_MARK: /* mark parity - WINDOWS ONLY */ TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems"); Win_CommConfig.dcb.fParity=TRUE; // enable parity checking Win_CommConfig.dcb.Parity=MARKPARITY; break; case PAR_NONE: /* no parity */ Win_CommConfig.dcb.fParity=FALSE; // disable parity checking Win_CommConfig.dcb.Parity=NOPARITY; break; case PAR_EVEN:/* even parity */ Win_CommConfig.dcb.fParity=TRUE; // enable parity checking Win_CommConfig.dcb.Parity=EVENPARITY; break; case PAR_ODD:/* odd parity */ Win_CommConfig.dcb.fParity=TRUE; // enable parity checking Win_CommConfig.dcb.Parity=ODDPARITY; break; default: Q_ASSERT(0); // This should never happen BUG replace by a error message } // baud settings switch (Settings.BaudRate) { case BAUD50:/*50 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 50 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; case BAUD75:/*75 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 75 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; case BAUD110:/*110 baud*/ Win_CommConfig.dcb.BaudRate=CBR_110; break; case BAUD134: /*134.5 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 134.5 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; case BAUD150:/*150 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 150 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; case BAUD200:/*200 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 200 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; case BAUD300:/*300 baud*/ Win_CommConfig.dcb.BaudRate=CBR_300; break; case BAUD600:/*600 baud*/ Win_CommConfig.dcb.BaudRate=CBR_600; break; case BAUD1200:/*1200 baud*/ Win_CommConfig.dcb.BaudRate=CBR_1200; break; case BAUD1800:/*1800 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 1800 baud operation. Switching to 1200 baud."); Win_CommConfig.dcb.BaudRate=CBR_1200; break; case BAUD2400:/*2400 baud*/ Win_CommConfig.dcb.BaudRate=CBR_2400; break; case BAUD4800:/*4800 baud*/ Win_CommConfig.dcb.BaudRate=CBR_4800; break; case BAUD9600:/*9600 baud*/ Win_CommConfig.dcb.BaudRate=CBR_9600; break; case BAUD14400:/*14400 baud*/ TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 14400 baud operation."); Win_CommConfig.dcb.BaudRate=CBR_14400; break; case BAUD19200:/*19200 baud*/ Win_CommConfig.dcb.BaudRate=CBR_19200; break; case BAUD38400:/*38400 baud*/ Win_CommConfig.dcb.BaudRate=CBR_38400; break; case BAUD56000:/*56000 baud*/ TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 56000 baud operation."); Win_CommConfig.dcb.BaudRate=CBR_56000; break; case BAUD57600:/*57600 baud*/ Win_CommConfig.dcb.BaudRate=CBR_57600; break; case BAUD76800:/*76800 baud*/ TTY_WARNING("Win_QextSerialPort: Windows does not support 76800 baud operation. Switching to 57600 baud."); Win_CommConfig.dcb.BaudRate=CBR_57600; break; case BAUD115200:/*115200 baud*/ Win_CommConfig.dcb.BaudRate=CBR_115200; break; case BAUD128000: Win_CommConfig.dcb.BaudRate=CBR_128000; break; case BAUD230400: Win_CommConfig.dcb.BaudRate=230400; break; case BAUD250000: Win_CommConfig.dcb.BaudRate=250000; break; case BAUD460800: Win_CommConfig.dcb.BaudRate = 460800; break; case BAUD500000: Win_CommConfig.dcb.BaudRate = 500000; break; case BAUD614400: Win_CommConfig.dcb.BaudRate = 614400; break; case BAUD750000: Win_CommConfig.dcb.BaudRate = 750000; break; case BAUD921600: Win_CommConfig.dcb.BaudRate = 921600; break; case BAUD1000000: Win_CommConfig.dcb.BaudRate = 1000000; break; case BAUD1228800: Win_CommConfig.dcb.BaudRate = 1228800; break; case BAUD2457600: Win_CommConfig.dcb.BaudRate = 2457600; break; case BAUD3000000: Win_CommConfig.dcb.BaudRate = 3000000; break; case BAUD6000000: Win_CommConfig.dcb.BaudRate = 6000000; break; default: Win_CommConfig.dcb.BaudRate = (unsigned int)Settings.BaudRate; break; } // STOP bits switch (Settings.StopBits) { case STOP_1:/*one stop bit*/ Win_CommConfig.dcb.StopBits=ONESTOPBIT; break; case STOP_1_5:/*1.5 stop bits*/ TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX."); if (Settings.DataBits!=DATA_5) { TTY_WARNING("Win_QextSerialPort: 1.5 stop bits can only be used with 5 data bits"); } else { Win_CommConfig.dcb.StopBits=ONE5STOPBITS; } break; case STOP_2:/*two stop bits*/ if (Settings.DataBits==DATA_5) {// BUG this assumes, that DATA was set first TTY_WARNING("Win_QextSerialPort: 2 stop bits cannot be used with 5 data bits"); } else { Win_CommConfig.dcb.StopBits=TWOSTOPBITS; } break; default: Q_ASSERT(0); // This should never happen BUG replace by a error message } switch (Settings.FlowControl) { case FLOW_OFF:/*no flow control*/ Win_CommConfig.dcb.fOutxCtsFlow = FALSE; Win_CommConfig.dcb.fOutxDsrFlow = FALSE; Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE; Win_CommConfig.dcb.fInX=FALSE; Win_CommConfig.dcb.fOutX=FALSE; break; case FLOW_XONXOFF:/*software (XON/XOFF) flow control*/ Win_CommConfig.dcb.fOutxCtsFlow = FALSE; Win_CommConfig.dcb.fOutxDsrFlow = FALSE; Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE; Win_CommConfig.dcb.fInX=TRUE; Win_CommConfig.dcb.fOutX=TRUE; break; case FLOW_HARDWARE: Win_CommConfig.dcb.fOutxCtsFlow=TRUE; Win_CommConfig.dcb.fOutxDsrFlow = FALSE; // guess? Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_HANDSHAKE; Win_CommConfig.dcb.fInX=FALSE; Win_CommConfig.dcb.fOutX=FALSE; break; default: Q_ASSERT(0); // This should never happen BUG replace by a error message } // write configuration back SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); // read current settings GetCommConfig(Win_Handle, &readCommConfig, &confSize); UNLOCK_MUTEX(); if(Win_CommConfig.dcb.BaudRate != readCommConfig.dcb.BaudRate) { Settings.BaudRate = readCommConfig.dcb.BaudRate; } return true; }
/*! \fn void Win_QextSerialPort::setTimeout(ulong millisec); Sets the read and write timeouts for the port to millisec milliseconds. Setting 0 indicates that timeouts are not used for read nor write operations; however read() and write() functions will still block. Set -1 to provide non-blocking behaviour (read() and write() will return immediately). */ void Win_QextSerialPort::setTimeout(long millisec) { LOCK_MUTEX(); Settings.Timeout_Millisec = millisec; // BUG it might be necessary to change the timeout while the port is open UNLOCK_MUTEX(); }
/*! \fn void Win_QextSerialPort::setParity(ParityType parity) Sets the parity associated with the serial port. The possible values of parity are: \verbatim PAR_SPACE Space Parity PAR_MARK Mark Parity PAR_NONE No Parity PAR_EVEN Even Parity PAR_ODD Odd Parity \endverbatim */ void Win_QextSerialPort::setParity(ParityType parity) { LOCK_MUTEX(); Settings.Parity=parity; UNLOCK_MUTEX(); }
static inline void inl_io_sched_pump ( p_io_scheduler_t scheduler ) { fd_set rd, wr, er; fd_t maxfd = INVALID_GENERAL_FD; /* -1 */ p_io_scheduler_task_t ptask, pnext_task; struct timeval tv_select_timeout; if ( !( scheduler->scheduled_tasks ) && ( scheduler->scheduler_thread ) ) { /* Nothing to do, and we're running in a secondary thread; give the system a chance to do something else. */ memset ( &tv_select_timeout, 0, sizeof( struct timeval ) ); tv_select_timeout.tv_usec = 1000; /* 1 ms */ select ( 0, NULL, NULL, NULL, &tv_select_timeout ); return; } FD_ZERO ( &rd ); FD_ZERO ( &wr ); FD_ZERO ( &er ); /* * * We lock a mutex here, specifically so that we do not break any links while someone else * is possibly trying to add to our task list. We don't have to worry about that later; this * is the only place where tasks are removed from the list - everyone else merely requests * their removal. When we loop later to check for the file descriptors being in the FD sets * it will not matter if a new file descriptor was added into our task list, simply because * there is no way for it to be set in the FD sets since we were never checking for it. * * When removing, we do not allow the tasks with an FD of INVALID_FD to be removed. These are * special, recurring system tasks that need to remain scheduled at all times. * */ if ( scheduler->scheduled_tasks && !( scheduler->stop_scheduler ) ) { LOCK_MUTEX( scheduler->task_list_mutex ); /* Clear any removes from the front of the scheduled task list. */ while ( scheduler->scheduled_tasks && S_IOSCHED_OPTS_REMOVE( scheduler->scheduled_tasks ) && ( scheduler->scheduled_tasks->fd != INVALID_GENERAL_FD ) ) { ptask = scheduler->scheduled_tasks->next; io_sched_destroy_task ( scheduler->scheduled_tasks ); scheduler->scheduled_tasks = ptask; } /* At this point we know that either (a) the list is empty, or (b) the first item is ready to be processed. */ ptask = scheduler->scheduled_tasks; while ( ptask ) { /* If it is a timer task, we don't need to worry about the FD sets. */ if ( !(S_IOSCHED_OPTS_TIMER_ONLY( ptask )) ) { if ( S_IOSCHED_OPTS_READ( ptask ) ) { maxfd = ( ptask->fd > maxfd ) ? ptask->fd : maxfd; FD_SET ( ptask->fd, &rd ); } if ( S_IOSCHED_OPTS_WRITE( ptask ) ) { maxfd = ( ptask->fd > maxfd ) ? ptask->fd : maxfd; FD_SET ( ptask->fd, &wr ); } if ( S_IOSCHED_OPTS_ERROR( ptask ) ) { maxfd = ( ptask->fd > maxfd ) ? ptask->fd : maxfd; FD_SET ( ptask->fd, &er ); } } /* Scan ahead for removes ... */ while ( ptask->next && S_IOSCHED_OPTS_REMOVE( ptask->next ) && ( ptask->next->fd != INVALID_GENERAL_FD ) ) { pnext_task = ptask->next->next; io_sched_destroy_task ( ptask->next ); ptask->next = pnext_task; } ptask = ptask->next; } UNLOCK_MUTEX( scheduler->task_list_mutex ); } if ( !( scheduler->stop_scheduler ) ) { memset ( &tv_select_timeout, 0, sizeof( struct timeval ) ); tv_select_timeout.tv_usec = 10000; /* 10 ms */ if ( select ( maxfd + 1, &rd, &wr, &er, &tv_select_timeout ) < 0 ) return; ptask = scheduler->scheduled_tasks; while ( ptask && !( scheduler->stop_scheduler ) ) { if ( !S_IOSCHED_OPTS_REMOVE( ptask ) ) { if ( io_sched_process_task ( ptask, &rd, &wr, &er ) ) io_sched_unschedule_task ( ptask ); } ptask = ptask->next; } } }
bool CPeakObj::open( const char *szFileName, unsigned long flags ) { const char *p; uint32_t busspeed = 125; uint16_t btr0btr1 = 0x031C; char szDrvParams[ MAX_PATH ]; int port; int irq; int hwtype = 0; char *next_token = NULL; // Save flags m_initFlag = flags; // save parameter string and convert to upper case #ifdef WIN32 strncpy_s( szDrvParams, sizeof( szDrvParams ), szFileName, MAX_PATH ); _strupr_s( szDrvParams ); #else strncpy( szDrvParams, szFileName, MAX_PATH ); _strupr( szDrvParams ); #endif // Initiate statistics m_stat.cntReceiveData = 0; m_stat.cntReceiveFrames = 0; m_stat.cntTransmitData = 0; m_stat.cntTransmitFrames = 0; m_stat.cntBusOff = 0; m_stat.cntBusWarnings = 0; m_stat.cntOverruns = 0; // if open we have noting to do if ( m_bRun ) return true; // Boardtype #ifdef WIN32 p = strtok_s( (char * )szDrvParams, ";", &next_token ); #else p = strtok( (char * )szDrvParams, ";" ); #endif if ( NULL != p ) { if ( isalpha( *p ) ) { // Symbolic form int symidx = 0; while( -1 != peaksymtbl[ symidx ]. id ) { if ( NULL != strstr( peaksymtbl[ symidx ]. symname, p ) ) { m_dwBrdType = peaksymtbl[ symidx ]. id; m_bPnp = peaksymtbl[ symidx ].bPnp; #ifdef WIN32 strcpy_s( m_dllName, sizeof( m_dllName ), peaksymtbl[ symidx ]. dllname ); #else strcpy( m_dllName, peaksymtbl[ symidx ]. dllname ); #endif break; } symidx++; } } else { // Numeric form m_dwBrdType = atoi( p ); m_bPnp = peaksymtbl[ m_dwBrdType ].bPnp; #ifdef WIN32 strcpy_s( m_dllName, sizeof( m_dllName ), peaksymtbl[ m_dwBrdType ]. dllname ); #else strcpy_s( m_dllName, peaksymtbl[ m_dwBrdType ]. dllname ); #endif } } // Set default values from board-type m_channel = 0; port = -1; irq = -1; // Bus-Speed #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf_s( p + 2, "%x", &busspeed ); #else sscanf( p + 2, "%x", &busspeed ); #endif } else { busspeed = atol( p ); } } // Handle busspeed switch ( busspeed ) { case 5: btr0btr1 = 0x7F7F; break; case 10: btr0btr1 = 0x672F; break; case 20: btr0btr1 = 0x532F; break; case 50: btr0btr1 = 0x472F; break; case 100: btr0btr1 = 0x432F; break; case 125: btr0btr1 = 0x031C; break; case 250: btr0btr1 = 0x011C; break; case 500: btr0btr1 = 0x001C; break; case 800: btr0btr1 = 0x0016; break; case 1000: btr0btr1 = 0x0014; break; } if ( !m_bPnp ) { // hwtype #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf( p + 2, "%x", &hwtype ); #else sscanf( p + 2, "%x", &hwtype ); #endif } else { hwtype = atoi( p ); } } // port #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf( p + 2, "%x", &port ); #else sscanf( p + 2, "%x", &port ); #endif } else { port = atoi( p ); } } // irq #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf( p + 2, "%x", &irq ); #else sscanf( p + 2, "%x", &irq ); #endif } else { irq = atoi( p ); } } } // channel #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf( p + 2, "%x", &m_channel ); #else sscanf( p + 2, "%x", &m_channel ); #endif } else { m_channel = atoi( p ); } } // Filter #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf( p + 2, "%x", &m_Peak_filter ); #else sscanf( p + 2, "%x", &m_Peak_filter ); #endif } else { m_Peak_filter = atol( p ); } } // Mask #ifdef WIN32 p = strtok_s( NULL, ";", &next_token ); #else p = strtok( NULL, ";" ); #endif if ( NULL != p ) { if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) ) ) { #ifdef WIN32 sscanf( p + 2, "%x", &m_Peak_mask ); #else sscanf( p + 2, "%x", &m_Peak_mask ); #endif } else { m_Peak_mask = atol( p ); } } switch ( m_dwBrdType ) { case 0: // CAN DONGLE if ( !nDongleDriverUseCnt ) { // Initialize Driver DLL if ( !initialize( m_dllName, m_bPnp ) ) { // Failed to initialize return false; } } else if ( 1 == nDongleDriverUseCnt ) { // Only one instance allowed setLastError( CANAL_ERROR_ONLY_ONE_INSTANCE, GetLastError(), "PEAK CAN DONGLE accept only one instance"); return false; } nDongleDriverUseCnt++; port = 0x378; irq = 7; break; case 1: // CAN DONGLE PRO if ( !nDongleProDriverUseCnt ) { // Initialize Driver DLL if ( !initialize( m_dllName, m_bPnp ) ) { // Failed to initialize return false; } } else if ( 1 == nDongleProDriverUseCnt ) { // Only one instance allowed setLastError( CANAL_ERROR_ONLY_ONE_INSTANCE, GetLastError(), "PEAK CAN DONGLE PRO accept only one instance"); return false; } nDongleProDriverUseCnt++; port = 0x378; irq = 7; break; case 2: // ISA if ( !nIsaDriverUseCnt ) { // Initialize Driver DLL if ( !initialize( m_dllName, m_bPnp ) ) { // Failed to initialize return false; } } else if ( 1 == nIsaDriverUseCnt ) { // Only one instance allowed setLastError( CANAL_ERROR_ONLY_ONE_INSTANCE, GetLastError(), "PEAK CAN ISA accept only one instance"); return false; } nIsaDriverUseCnt++; port = 0x300; irq = 10; break; case 3: // PCI if ( !nPciDriverUseCnt ) { // Initialize Driver DLL if ( !initialize( m_dllName, m_bPnp ) ) { // Failed to initialize return false; } } else if ( 1 == nPciDriverUseCnt ) { // Only one instance allowed setLastError( CANAL_ERROR_ONLY_ONE_INSTANCE, GetLastError(), "PEAK CAN PCI accept only one instance"); return false; } nPciDriverUseCnt++; break; case 4: // PCI2 if ( !nPci2DriverUseCnt ) { // Initialize Driver DLL if ( !initialize( m_dllName, m_bPnp ) ) { // Failed to initialize return false; } } else if ( 1 == nPci2DriverUseCnt ) { // Only one instance allowed setLastError( CANAL_ERROR_ONLY_ONE_INSTANCE, GetLastError(), "PEAK CAN PCI2 accept only one instance"); return false; } nPci2DriverUseCnt++; break; case 5: // USB if ( !nUSBDriverUseCnt ) { // Initialize Driver DLL if ( !initialize( m_dllName, m_bPnp ) ) { // Failed to initialize return false; } } else if ( 1 == nUSBDriverUseCnt ) { // Only one instance allowed setLastError( CANAL_ERROR_ONLY_ONE_INSTANCE, GetLastError(), "PEAK CAN USB accept only one instance"); return false; } nUSBDriverUseCnt++; break; } // Init the device unsigned long rv; if ( m_bPnp ) { rv = m_procInitPnp( btr0btr1, ( m_initFlag & 1 ) ); if ( PEAK_CAN_ERR_OK != rv ) { setLastError( CANAL_ERROR_INIT_FAIL, rv, "Init error (PCI): suberror is driver failure code"); return false; } } else { rv = m_procInit( btr0btr1, ( m_initFlag & 1 ), hwtype, port, irq ); if ( PEAK_CAN_ERR_OK != rv ) { setLastError( CANAL_ERROR_INIT_FAIL, rv, "Init error: suberror is driver failure code"); return false; } } // Run run run ..... // (otional (for hard - fellow - rockers) "to the hills..." m_bRun = true; #ifdef WIN32 // Start write thread DWORD threadId; if ( NULL == ( m_hTreadTransmit = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) workThreadTransmit, this, 0, &threadId ) ) ) { // Failure setLastError( CANAL_ERROR_INIT_FAIL, GetLastError(), "Init error: Unable to create transmit thread"); close(); return false; } // Start read thread if ( NULL == ( m_hTreadReceive = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) workThreadReceive, this, 0, &threadId ) ) ) { // Failure setLastError( CANAL_ERROR_INIT_FAIL, GetLastError(), "Init error: Unable to create receive thread"); close(); return false; } // Release the mutex UNLOCK_MUTEX( m_peakMutex ); UNLOCK_MUTEX( m_receiveMutex ); UNLOCK_MUTEX( m_transmitMutex ); #else // LINUX pthread_attr_t thread_attr; pthread_attr_init( &thread_attr ); // Create the log write thread. if ( pthread_create( &m_threadId, &thread_attr, workThreadTransmit, this ) ) { syslog( LOG_CRIT, "canallogger: Unable to create peakdrv write thread."); setLastError( CANAL_ERROR_INIT_FAIL, GetLastError(), "Init error: Unable to create transmit thread"); rv = false; fclose( m_flog ); } // Create the log write thread. if ( pthread_create( &m_threadId, &thread_attr, workThreadReceive, this ) ) { syslog( LOG_CRIT, "canallogger: Unable to create peakdrv receive thread."); setLastError( CANAL_ERROR_INIT_FAIL, GetLastError(), "Init error: Unable to create receive thread"); rv = false; fclose( m_flog ); } // Release the mutex pthread_mutex_unlock( &m_ixxMutex ); #endif // We are open m_bOpen = true; return true; }
int pauseMusic(sp_session *session , char *uri , char *name ) { TRACE_2( PLAYERMANAGER , "pauseMusic()."); int status = PC_SUCCESS; char response[255] = { 0 }; LOCK_MUTEX( PLAYERMANAGER , &mutexSession ); sp_error error; if( pausing == FALSE ) { pauseStream( name ); error = sp_session_player_play( session , 0 ); if( error != SP_ERROR_OK ) { TRACE_ERROR( PLAYERMANAGER , "Cannot pause track, reason: %s" , sp_error_message( error ) ); status = PC_ERROR; snprintf( response , 255 , "NOK: Cannot pause track, reason: %s" , sp_error_message( error ) ); sendVoid( ( void * )response , strlen( response ) ); } else { TRACE_1( PLAYERMANAGER , "Success to pause track."); pausing = TRUE; snprintf( response , 255 , "OK"); sendVoid( ( void * )response , strlen( response ) ); } } else if( pausing == TRUE ) { playStream( name ); error = sp_session_player_play( session , 1 ); if( error != SP_ERROR_OK ) { TRACE_ERROR( PLAYERMANAGER , "Cannot play track, reason: %s" , sp_error_message( error ) ); status = PC_ERROR; } else { TRACE_1( PLAYERMANAGER , "Success to play track."); pausing = FALSE; } } UNLOCK_MUTEX( PLAYERMANAGER , &mutexSession ); return status; }
bool CPeakObj::close( void ) { unsigned long rv; // Do nothing if already terminated if ( !m_bRun ) return false; m_bRun = false; UNLOCK_MUTEX( m_peakMutex ); Sleep( 1000 ); LOCK_MUTEX( m_peakMutex ); // Close the driver rv = m_procClose(); if ( PEAK_CAN_ERR_OK != rv ) { setLastError( CANAL_ERROR_INIT_FAIL, rv, "Error on close."); return false; } // terminate the worker thread #ifdef WIN32 // Wait for transmit thread to terminate while ( true ) { GetExitCodeThread( m_hTreadTransmit, &rv ); if ( STILL_ACTIVE != rv ) break; } // Wait for receive thread to terminate while ( true ) { GetExitCodeThread( m_hTreadReceive, &rv ); if ( STILL_ACTIVE != rv ) break; } #else int *trv; pthread_join( m_threadIdReceive, (void **)&trv ); pthread_join( m_threadIdTransmit, (void **)&trv ); pthread_mutex_destroy( &m_ixxMutex ); #endif switch ( m_dwBrdType ) { case 0: // CAN DONGLE nDongleDriverUseCnt--; if ( !nDongleDriverUseCnt ) { // Free library FreeLibrary( m_hinst ); m_hinst = NULL; } break; case 1: // CAN DONGLE PRO nDongleProDriverUseCnt--; if ( !nDongleProDriverUseCnt ) { // Free library FreeLibrary( m_hinst ); m_hinst = NULL; } break; case 2: // ISA nIsaDriverUseCnt--; if ( !nIsaDriverUseCnt ) { // Free library FreeLibrary( m_hinst ); m_hinst = NULL; } break; case 3: // PCI nPciDriverUseCnt--; if ( !nPciDriverUseCnt ) { // Free library FreeLibrary( m_hinst ); m_hinst = NULL; } break; case 4: // PCI2 nPci2DriverUseCnt--; if ( !nPci2DriverUseCnt ) { // Free library FreeLibrary( m_hinst ); m_hinst = NULL; } break; case 5: // USB nUSBDriverUseCnt--; if ( !nUSBDriverUseCnt ) { // Free library FreeLibrary( m_hinst ); m_hinst = NULL; } break; } // We are closed m_bOpen = false; return true; }
/*! \fn void Win_QextSerialPort::setBaudRate(BaudRateType baudRate) Sets the baud rate of the serial port. Note that not all rates are applicable on all platforms. The following table shows translations of the various baud rate constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * are speeds that are usable on both Windows and POSIX. \verbatim RATE Windows Speed POSIX Speed ----------- ------------- ----------- BAUD50 110 50 BAUD75 110 75 *BAUD110 110 110 BAUD134 110 134.5 BAUD150 110 150 BAUD200 110 200 *BAUD300 300 300 *BAUD600 600 600 *BAUD1200 1200 1200 BAUD1800 1200 1800 *BAUD2400 2400 2400 *BAUD4800 4800 4800 *BAUD9600 9600 9600 BAUD14400 14400 9600 *BAUD19200 19200 19200 *BAUD38400 38400 38400 BAUD56000 56000 38400 *BAUD57600 57600 57600 BAUD76800 57600 76800 *BAUD115200 115200 115200 BAUD128000 128000 115200 BAUD256000 256000 115200 \endverbatim */ void Win_QextSerialPort::setBaudRate(BaudRateType baudRate) { LOCK_MUTEX(); if (Settings.BaudRate!=baudRate) { switch (baudRate) { case BAUD50: case BAUD75: case BAUD134: case BAUD150: case BAUD200: Settings.BaudRate=BAUD110; break; case BAUD1800: Settings.BaudRate=BAUD1200; break; case BAUD76800: Settings.BaudRate=BAUD57600; break; default: Settings.BaudRate=baudRate; break; } } if (isOpen()) { switch (baudRate) { /*50 baud*/ case BAUD50: TTY_WARNING("Win_QextSerialPort: Windows does not support 50 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; /*75 baud*/ case BAUD75: TTY_WARNING("Win_QextSerialPort: Windows does not support 75 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; /*110 baud*/ case BAUD110: Win_CommConfig.dcb.BaudRate=CBR_110; break; /*134.5 baud*/ case BAUD134: TTY_WARNING("Win_QextSerialPort: Windows does not support 134.5 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; /*150 baud*/ case BAUD150: TTY_WARNING("Win_QextSerialPort: Windows does not support 150 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; /*200 baud*/ case BAUD200: TTY_WARNING("Win_QextSerialPort: Windows does not support 200 baud operation. Switching to 110 baud."); Win_CommConfig.dcb.BaudRate=CBR_110; break; /*300 baud*/ case BAUD300: Win_CommConfig.dcb.BaudRate=CBR_300; break; /*600 baud*/ case BAUD600: Win_CommConfig.dcb.BaudRate=CBR_600; break; /*1200 baud*/ case BAUD1200: Win_CommConfig.dcb.BaudRate=CBR_1200; break; /*1800 baud*/ case BAUD1800: TTY_WARNING("Win_QextSerialPort: Windows does not support 1800 baud operation. Switching to 1200 baud."); Win_CommConfig.dcb.BaudRate=CBR_1200; break; /*2400 baud*/ case BAUD2400: Win_CommConfig.dcb.BaudRate=CBR_2400; break; /*4800 baud*/ case BAUD4800: Win_CommConfig.dcb.BaudRate=CBR_4800; break; /*9600 baud*/ case BAUD9600: Win_CommConfig.dcb.BaudRate=CBR_9600; break; /*14400 baud*/ case BAUD14400: TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 14400 baud operation."); Win_CommConfig.dcb.BaudRate=CBR_14400; break; /*19200 baud*/ case BAUD19200: Win_CommConfig.dcb.BaudRate=CBR_19200; break; /*38400 baud*/ case BAUD38400: Win_CommConfig.dcb.BaudRate=CBR_38400; break; /*56000 baud*/ case BAUD56000: TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 56000 baud operation."); Win_CommConfig.dcb.BaudRate=CBR_56000; break; /*57600 baud*/ case BAUD57600: Win_CommConfig.dcb.BaudRate=CBR_57600; break; /*76800 baud*/ case BAUD76800: TTY_WARNING("Win_QextSerialPort: Windows does not support 76800 baud operation. Switching to 57600 baud."); Win_CommConfig.dcb.BaudRate=CBR_57600; break; /*115200 baud*/ case BAUD115200: Win_CommConfig.dcb.BaudRate=CBR_115200; break; /*128000 baud*/ case BAUD128000: TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 128000 baud operation."); Win_CommConfig.dcb.BaudRate=CBR_128000; break; /*256000 baud*/ case BAUD256000: TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 256000 baud operation."); Win_CommConfig.dcb.BaudRate=CBR_256000; break; } SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); } UNLOCK_MUTEX(); }
/*! \fn bool Win_QextSerialPort::open(OpenMode mode) Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool Win_QextSerialPort::open(OpenMode mode) { unsigned long confSize = sizeof(COMMCONFIG); m_WinCommConfig.dwSize = confSize; DWORD dwFlagsAndAttributes = 0; if (queryMode() == QextSerialBase::EventDriven) { dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED; } LOCK_MUTEX(); if (mode == QIODevice::NotOpen) { return isOpen(); } if (!isOpen()) { /*open the port*/ m_WinHandle=CreateFileA(port.toAscii(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); if (m_WinHandle!=INVALID_HANDLE_VALUE) { /*configure port settings*/ GetCommConfig(m_WinHandle, &m_WinCommConfig, &confSize); GetCommState(m_WinHandle, &(m_WinCommConfig.dcb)); /*set up parameters*/ m_WinCommConfig.dcb.fBinary=TRUE; m_WinCommConfig.dcb.fInX=FALSE; m_WinCommConfig.dcb.fOutX=FALSE; m_WinCommConfig.dcb.fAbortOnError=FALSE; m_WinCommConfig.dcb.fNull=FALSE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setStopBits(Settings.StopBits); setParity(Settings.Parity); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); //init event driven approach if (queryMode() == QextSerialBase::EventDriven) { m_WinCommTimeouts.ReadIntervalTimeout = MAXDWORD; m_WinCommTimeouts.ReadTotalTimeoutMultiplier = 0; m_WinCommTimeouts.ReadTotalTimeoutConstant = 0; m_WinCommTimeouts.WriteTotalTimeoutMultiplier = 0; m_WinCommTimeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_WinHandle, &m_WinCommTimeouts); if (!SetCommMask( m_WinHandle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) { qWarning("Failed to set Comm Mask. Error code: %ld", GetLastError()); UNLOCK_MUTEX(); return false; } m_pOverlapThread->start(); } QIODevice::open(mode); } } else { UNLOCK_MUTEX(); return false; } UNLOCK_MUTEX(); return isOpen(); }
void *buffer_thread_func (void *arg) { buf_t *buf = (buf_t*) arg; size_t write_amount; DEBUG("Enter buffer_thread_func"); buffer_thread_init(buf); pthread_cleanup_push(buffer_thread_cleanup, buf); DEBUG("Start main play loop"); /* This test is safe since curfill will never decrease and eos will never be unset. */ while ( !(buf->eos && buf->curfill == 0) && !buf->abort_write) { if (buf->cancel_flag || sig_request.cancel) break; DEBUG("Check for something to play"); /* Block until we can play something */ LOCK_MUTEX (buf->mutex); if (buf->prebuffering || buf->paused || (buf->curfill < buf->audio_chunk_size && !buf->eos)) { DEBUG("Waiting for more data to play."); COND_WAIT(buf->playback_cond, buf->mutex); } DEBUG("Ready to play"); UNLOCK_MUTEX(buf->mutex); if (buf->cancel_flag || sig_request.cancel) break; /* Don't need to lock buffer while running actions since position won't change. We clear out any actions before we compute the dequeue size so we don't consider actions that need to run right now. */ execute_actions(buf, &buf->actions, buf->position); LOCK_MUTEX(buf->mutex); /* Need to be locked while we check things. */ write_amount = compute_dequeue_size(buf, buf->audio_chunk_size); UNLOCK_MUTEX(buf->mutex); if(write_amount){ /* we might have been woken spuriously */ /* No need to lock mutex here because the other thread will NEVER reduce the number of bytes stored in the buffer */ DEBUG1("Sending %d bytes to the audio device", write_amount); write_amount = buf->write_func(buf->buffer + buf->start, write_amount, /* Only set EOS if this is the last chunk */ write_amount == buf->curfill ? buf->eos : 0, buf->write_arg); if (!write_amount) { DEBUG("Error writing to the audio device. Aborting."); buffer_abort_write(buf); } LOCK_MUTEX(buf->mutex); buf->curfill -= write_amount; buf->position += write_amount; buf->start = (buf->start + write_amount) % buf->size; DEBUG1("Updated buffer fill, curfill = %ld", buf->curfill); /* If we've essentially emptied the buffer and prebuffering is enabled, we need to do another prebuffering session */ if (!buf->eos && (buf->curfill < buf->audio_chunk_size)) buf->prebuffering = buf->prebuffer_size > 0; }else{ DEBUG("Woken spuriously"); } /* Signal a waiting decoder thread that they can put more audio into the buffer */ DEBUG("Signal decoder thread that buffer space is available"); COND_SIGNAL(buf->write_cond); UNLOCK_MUTEX(buf->mutex); } pthread_cleanup_pop(1); DEBUG("exiting buffer_thread_func"); return 0; }
/*! \fn void Win_QextSerialPort::setParity(ParityType parity) Sets the parity associated with the serial port. The possible values of parity are: \verbatim PAR_SPACE Space Parity PAR_MARK Mark Parity PAR_NONE No Parity PAR_EVEN Even Parity PAR_ODD Odd Parity \endverbatim */ void Win_QextSerialPort::setParity(ParityType parity) { LOCK_MUTEX(); if (Settings.Parity!=parity) { Settings.Parity=parity; } if (isOpen()) { m_WinCommConfig.dcb.Parity=(unsigned char)parity; switch (parity) { /*space parity*/ case PAR_SPACE: { if (Settings.DataBits==DATA_8) { TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems."); } m_WinCommConfig.dcb.fParity=TRUE; } break; /*mark parity - WINDOWS ONLY*/ case PAR_MARK: { TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems"); m_WinCommConfig.dcb.fParity=TRUE; } break; /*no parity*/ case PAR_NONE: { m_WinCommConfig.dcb.fParity=FALSE; } break; /*even parity*/ case PAR_EVEN: { m_WinCommConfig.dcb.fParity=TRUE; } break; /*odd parity*/ case PAR_ODD: { m_WinCommConfig.dcb.fParity=TRUE; } break; default: { Assert(false); } break; } SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } UNLOCK_MUTEX(); }
size_t buffer_get_data (buf_t *buf, char *data, long nbytes) { int write_amount; int orig_size; orig_size = nbytes; DEBUG("Enter buffer_get_data"); pthread_cleanup_push(buffer_mutex_unlock, buf); LOCK_MUTEX(buf->mutex); /* Put the data into the buffer as space is made available */ while (nbytes > 0) { if (buf->abort_write) break; DEBUG("Obtaining lock on buffer"); /* Block until we can read something */ if (buf->curfill == 0 && buf->eos) break; /* No more data to read */ if (buf->curfill == 0 || (buf->prebuffering && !buf->eos)) { DEBUG("Waiting for more data to copy."); COND_WAIT(buf->playback_cond, buf->mutex); } if (buf->abort_write) break; /* Note: Even if curfill is still 0, nothing bad will happen here */ /* For simplicity, the number of bytes played must satisfy the following three requirements: 1. Do not copy more bytes than are stored in the buffer. 2. Do not copy more bytes than the reqested data size. 3. Do not run off the end of the buffer. */ write_amount = compute_dequeue_size(buf, nbytes); UNLOCK_MUTEX(buf->mutex); execute_actions(buf, &buf->actions, buf->position); /* No need to lock mutex here because the other thread will NEVER reduce the number of bytes stored in the buffer */ DEBUG1("Copying %d bytes from the buffer", write_amount); memcpy(data, buf->buffer + buf->start, write_amount); LOCK_MUTEX(buf->mutex); buf->curfill -= write_amount; data += write_amount; nbytes -= write_amount; buf->start = (buf->start + write_amount) % buf->size; DEBUG1("Updated buffer fill, curfill = %ld", buf->curfill); /* Signal a waiting decoder thread that they can put more audio into the buffer */ DEBUG("Signal decoder thread that buffer space is available"); COND_SIGNAL(buf->write_cond); } UNLOCK_MUTEX(buf->mutex); pthread_cleanup_pop(0); pthread_testcancel(); DEBUG("Exit buffer_get_data"); return orig_size - nbytes; }
/*! \fn void Win_QextSerialPort::setDataBits(DataBitsType dataBits) Sets the number of data bits used by the serial port. Possible values of dataBits are: \verbatim DATA_5 5 data bits DATA_6 6 data bits DATA_7 7 data bits DATA_8 8 data bits \endverbatim \note This function is subject to the following restrictions: \par 5 data bits cannot be used with 2 stop bits. \par 1.5 stop bits can only be used with 5 data bits. \par 8 data bits cannot be used with space parity on POSIX systems. */ void Win_QextSerialPort::setDataBits(DataBitsType dataBits) { LOCK_MUTEX(); if (Settings.DataBits!=dataBits) { if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) || (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5)) { } else { Settings.DataBits=dataBits; } } if (isOpen()) { switch(dataBits) { /*5 data bits*/ case DATA_5: { if (Settings.StopBits==STOP_2) { TTY_WARNING("Win_QextSerialPort: 5 Data bits cannot be used with 2 stop bits."); } else { m_WinCommConfig.dcb.ByteSize=5; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } } break; /*6 data bits*/ case DATA_6: { if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Win_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits."); } else { m_WinCommConfig.dcb.ByteSize=6; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } } break; /*7 data bits*/ case DATA_7: { if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Win_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits."); } else { m_WinCommConfig.dcb.ByteSize=7; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } } break; /*8 data bits*/ case DATA_8: { if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Win_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits."); } else { m_WinCommConfig.dcb.ByteSize=8; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } } break; default: { Assert(false); } break; } } UNLOCK_MUTEX(); }
/*! \fn void Posix_QextSerialPort::setDataBits(DataBitsType dataBits) Sets the number of data bits used by the serial port. Possible values of dataBits are: \verbatim DATA_5 5 data bits DATA_6 6 data bits DATA_7 7 data bits DATA_8 8 data bits \endverbatim \note This function is subject to the following restrictions: \par 5 data bits cannot be used with 2 stop bits. \par 8 data bits cannot be used with space parity on POSIX systems. */ void Posix_QextSerialPort::setDataBits(DataBitsType dataBits) { LOCK_MUTEX(); if (Settings.DataBits!=dataBits) { if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) || (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5) || (Settings.Parity==PAR_SPACE && dataBits==DATA_8)) { } else { Settings.DataBits=dataBits; } } if (isOpen()) { switch(dataBits) { /*5 data bits*/ case DATA_5: if (Settings.StopBits==STOP_2) { TTY_WARNING("Posix_QextSerialPort: 5 Data bits cannot be used with 2 stop bits."); } else { Settings.DataBits=dataBits; Posix_CommConfig.c_cflag&=(~CSIZE); Posix_CommConfig.c_cflag|=CS5; tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } break; /*6 data bits*/ case DATA_6: if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Posix_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits."); } else { Settings.DataBits=dataBits; Posix_CommConfig.c_cflag&=(~CSIZE); Posix_CommConfig.c_cflag|=CS6; tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } break; /*7 data bits*/ case DATA_7: if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Posix_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits."); } else { Settings.DataBits=dataBits; Posix_CommConfig.c_cflag&=(~CSIZE); Posix_CommConfig.c_cflag|=CS7; tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } break; /*8 data bits*/ case DATA_8: if (Settings.StopBits==STOP_1_5) { TTY_WARNING("Posix_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits."); } else { Settings.DataBits=dataBits; Posix_CommConfig.c_cflag&=(~CSIZE); Posix_CommConfig.c_cflag|=CS8; tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } break; } } UNLOCK_MUTEX(); }
/*! \fn void Win_QextSerialPort::setStopBits(StopBitsType stopBits) Sets the number of stop bits used by the serial port. Possible values of stopBits are: \verbatim STOP_1 1 stop bit STOP_1_5 1.5 stop bits STOP_2 2 stop bits \endverbatim \note This function is subject to the following restrictions: \par 2 stop bits cannot be used with 5 data bits. \par 1.5 stop bits cannot be used with 6 or more data bits. \par POSIX does not support 1.5 stop bits. */ void Win_QextSerialPort::setStopBits(StopBitsType stopBits) { LOCK_MUTEX(); if (Settings.StopBits!=stopBits) { if ((Settings.DataBits==DATA_5 && stopBits==STOP_2) || (stopBits==STOP_1_5 && Settings.DataBits!=DATA_5)) { } else { Settings.StopBits=stopBits; } } if (isOpen()) { switch (stopBits) { /*one stop bit*/ case STOP_1: { m_WinCommConfig.dcb.StopBits=ONESTOPBIT; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } break; /*1.5 stop bits*/ case STOP_1_5: { TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX."); if (Settings.DataBits!=DATA_5) { TTY_WARNING("Win_QextSerialPort: 1.5 stop bits can only be used with 5 data bits"); } else { m_WinCommConfig.dcb.StopBits=ONE5STOPBITS; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } } break; /*two stop bits*/ case STOP_2: { if (Settings.DataBits==DATA_5) { TTY_WARNING("Win_QextSerialPort: 2 stop bits cannot be used with 5 data bits"); } else { m_WinCommConfig.dcb.StopBits=TWOSTOPBITS; SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); } } break; default: { Assert(false); } break; } } UNLOCK_MUTEX(); }
void *workThread(void *pObject) { int rv = 0; int cnt; bool bActivity = true; short nPollCnt = 0; char szResponse[ 32 ]; printf("can232obj in workThread\n"); CCAN232Obj * pcan232obj = (CCAN232Obj *) pObject; if (NULL == pcan232obj) { pthread_exit(&rv); } while (pcan232obj->m_can232obj.m_bRun) { /////////////////////////////////////////////////////////////////////// // Receive /////////////////////////////////////////////////////////////////////// LOCK_MUTEX(pcan232obj->m_can232ObjMutex); // Noting to do if we should end... if (!pcan232obj->m_can232obj.m_bRun) continue; int cnt; unsigned char c; //printf("workThread - Receive\n"); c = pcan232obj->m_can232obj.m_comm.readChar(&cnt); while (-1 != cnt) { bActivity = true; if (CAN232_STATE_NONE == pcan232obj->m_can232obj.m_state) { if (('t' == c) || ('T' == c) || ('r' == c) || ('R' == c)) { pcan232obj->m_can232obj.m_state = CAN232_STATE_MSG; pcan232obj->m_can232obj.m_receiveBuf[ 0 ] = c; pcan232obj->m_can232obj.m_cntRcv = 1; } } else if (CAN232_STATE_MSG == pcan232obj->m_can232obj.m_state) { pcan232obj->m_can232obj.m_receiveBuf[ pcan232obj->m_can232obj.m_cntRcv++ ] = c; if (0x0d == c) { // One full message received If there is place in the queue // add message to it if (pcan232obj->m_can232obj.m_rcvList.nCount < CAN232_MAX_RCVMSG) { PCANALMSG pMsg = new canalMsg; pMsg->flags = 0; if (NULL != pMsg) { dllnode *pNode = new dllnode; if (NULL != pNode) { printf("workThread R - m_receiveBuf = [%s]\n", pcan232obj->m_can232obj.m_receiveBuf); int cnt = pcan232obj->m_can232obj.m_cntRcv; printf("workThread R - m_receiveBuf ["); for (int i = 0; i < cnt; i++) { printf("%02X ", pcan232obj->m_can232obj.m_receiveBuf[i]); } printf("]\n"); if (!can232ToCanal(pcan232obj->m_can232obj.m_receiveBuf, pMsg)) { pNode->pObject = pMsg; dll_addNode(&pcan232obj->m_can232obj.m_rcvList, pNode); // Update statistics pcan232obj->m_can232obj.m_stat.cntReceiveData += pMsg->sizeData; pcan232obj->m_can232obj.m_stat.cntReceiveFrames += 1; printf("workThread R - RcvFrames = [%ld]\n\n", pcan232obj->m_can232obj.m_stat.cntReceiveFrames); } else { // Failed to translate message printf("workThread R - Receive Failed to translate message\n"); delete pMsg; delete pNode; } } else { delete pMsg; } } } pcan232obj->m_can232obj.m_state = CAN232_STATE_NONE; } if (pcan232obj->m_can232obj.m_cntRcv > sizeof( pcan232obj->m_can232obj.m_receiveBuf)) { // Problems start all over again pcan232obj->m_can232obj.m_state = CAN232_STATE_NONE; } } c = pcan232obj->m_can232obj.m_comm.readChar(&cnt); } // while ( 0 != cnt ) UNLOCK_MUTEX(pcan232obj->m_can232ObjMutex); /////////////////////////////////////////////////////////////////////// // Transmit /////////////////////////////////////////////////////////////////////// //printf("workThread - Transmit\n"); LOCK_MUTEX(pcan232obj->m_can232ObjMutex); // Is there something to transmit // while ( ( NULL != pcan232obj->m_can232obj.m_sndList.pHead ) && // ( NULL != pcan232obj->m_can232obj.m_sndList.pHead->pObject ) ) { if ((NULL != pcan232obj->m_can232obj.m_sndList.pHead) && (NULL != pcan232obj->m_can232obj.m_sndList.pHead->pObject)) { char buf[ 80 ]; canalMsg msg; bActivity = true; memcpy(&msg, pcan232obj->m_can232obj.m_sndList.pHead->pObject, sizeof( canalMsg)); dll_removeNode(&pcan232obj->m_can232obj.m_sndList, pcan232obj->m_can232obj.m_sndList.pHead); // Must be a valid standard id if (!(msg.flags & CANAL_IDFLAG_EXTENDED) && (msg.id > 0x7ff)) { msg.id &= 0x7ff; }; // Must be a valid extended id if ((msg.flags & CANAL_IDFLAG_EXTENDED) && (msg.id > 0x1fffffff)) { msg.id &= 0x1fffffff; } // Currently there is a bug in ice old asm can232 tranceiver, and allow only small hex digits if (msg.flags & CANAL_IDFLAG_EXTENDED) { sprintf(buf, "T%8.8lx%i", msg.id, msg.sizeData); } else { sprintf(buf, "t%3.3lx%i", msg.id, msg.sizeData); } if (msg.sizeData) { char hex[5]; for (int i = 0; i < msg.sizeData; i++) { sprintf(hex, "%2.2X", msg.data[i]); //sprintf( hex, "%02.2x", msg.data[i] ); strcat(buf, hex); } strcat(buf, "\r"); } // Send the data pcan232obj->m_can232obj.m_comm.comm_puts(buf, strlen(buf), true); printf("workThread T - Write ["); for (int i = 0; i < strlen(buf); i++) { if (buf[i] == 0x0D) { printf("[CR]"); } else { printf("%c", buf[i]); } } printf("]\n"); pcan232obj->m_can232obj.m_comm.comm_gets(szResponse, sizeof( szResponse), 10000); printf("workThread T - Read ["); for (int i = 0; i < strlen(szResponse); i++) { if (szResponse[i] == 0x0D) { printf("[CR]"); } else { printf("%c", szResponse[i]); } } printf("]\n\n"); // needed !! At least at 19200 baud SLEEP(100); // Update statistics pcan232obj->m_can232obj.m_stat.cntTransmitData += msg.sizeData; pcan232obj->m_can232obj.m_stat.cntTransmitFrames += 1; //} // while there is something to transmit } // if there is something to transmit // If not in autopoll mode we do a poll for all frames first nPollCnt++; if (!pcan232obj->m_can232obj.m_bAuto && (nPollCnt > 5)) { char szCmd[5]; sprintf(szCmd, "A\r"); pcan232obj->m_can232obj.m_comm.comm_puts(szCmd, strlen(szCmd), true); nPollCnt = 0; } UNLOCK_MUTEX(pcan232obj->m_can232ObjMutex); if (!bActivity) SLEEP(100); bActivity = false; } // while( pcan232obj->m_can232obj.m_bRun ) pthread_exit(&rv); }
CDllDrvObj::CDllDrvObj() : lpvMem(arrayObj), hMapObject(NULL) { m_instanceCounter = 0; #ifdef WIN32 m_objMutex = CreateMutex( NULL, false, "__CANAL_IXXATVCI_MUTEX__" ); DWORD dwResult = LOCK_MUTEX(m_objMutex); if(dwResult == WAIT_ABANDONED) { UNLOCK_MUTEX(m_objMutex); CloseHandle(m_objMutex); throw "error mutex abandoned"; } // Create a named file mapping object hMapObject = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security attributes PAGE_READWRITE, // read/write access 0, // size: high 32-bits sizeof(long) * CANAL_USB2CAN_DRIVER_MAX_OPEN, // size: low 32-bits TEXT("dllusb2canfilemap")); // name of map object if (hMapObject == NULL) { UNLOCK_MUTEX(m_objMutex); CloseHandle(m_objMutex); throw "error createfilemapping"; } // The first process to attach initializes memory bool fInit = (GetLastError() != ERROR_ALREADY_EXISTS); // Get a pointer to the file-mapped shared memory lpvMem = static_cast<long*>(MapViewOfFile( hMapObject, // object to map view of FILE_MAP_WRITE, // read/write access 0, // high offset: map from 0, // low offset: beginning 0)); // default: map entire file if (lpvMem == NULL) { UNLOCK_MUTEX(m_objMutex); CloseHandle(m_objMutex); throw "error createfilemapping"; } // Initialize memory if this is the first process if (fInit) { memset(lpvMem, 0, sizeof(long)*CANAL_USB2CAN_DRIVER_MAX_OPEN); } #else pthread_mutex_init( &m_objMutex, NULL ); //DL: I'm afraid I don't know how to open a memory mapped file under Linux so for lpvMem by default uses a static array address #endif // Init the driver array for ( int i = 0; i<CANAL_USB2CAN_DRIVER_MAX_OPEN; i++ ) { m_drvObjArray[i] = NULL; } UNLOCK_MUTEX( m_objMutex ); }