static void *worker(void *data) { queue_t *q = data; piece_t *p; SHA_CTX c; while ((p = get_full(q))) { SHA1_Init(&c); SHA1_Update(&c, p->data, p->len); SHA1_Final(p->dest, &c); put_free(q, p, 1); } return NULL; }
static void *spi_thread(void *arg) { spi_connection_t *sc = (spi_connection_t *)arg; spi_data_t *data; int res, data_read; uint64_t start, end; data_read = 0; while (1) { lock(sc); data = __get_free(sc); while (data == NULL) { pthread_cond_wait(&sc->cond, &sc->lock); data = __get_free(sc); } unlock(sc); /* reading always blocks until at least the packet header is ready. */ start = get_utime(); again: res = read(sc->fd, data->rx_buf + data_read, COMM_BUF_SIZE - data_read); if (res <= 0) { printf("res %d, data_read %d, COMM_BUF_SIZE %d\n", res, data_read, COMM_BUF_SIZE); perror("read spi device"); put_free(sc, data); } else { data_read += res; if (data_read < data->rx_buf[3] + 6 && data_read < COMM_BUF_SIZE) goto again; end = get_utime(); calc_stats(end-start, STAT_READ); data->data_ready = data_read; data->r_idx = 0; data_read = 0; //print_data(data); put_pending_delete(sc, data); } } return NULL; }
int spi_dev_channel_create( comm_channel_t *channel ) { spi_connection_t *sc; int i; if( channel->data != NULL ) { fprintf( stderr, "WARNING: SPI channel already initialized\n" ); return( -1 ); } sc = malloc( sizeof( spi_connection_t ) ); if( !sc ) { fprintf( stderr, "ERROR in %s %d: memory allocation\n", __FILE__, __LINE__ ); return( -1 ); } memset( sc, 0, sizeof( spi_connection_t ) ); pthread_mutex_init(&connection.lock, NULL); pthread_cond_init(&connection.cond, NULL); for( i = 0; i < NUM_SPI_DATA; ++i ) { put_free(sc, sc->data[i]); } channel->type = CH_SPI; channel->transmit = spi_transmit; channel->receive = spi_receive; channel->start = spi_start; channel->flush = spi_flush; channel->poll = spi_poll; channel->data = sc; return( 0 ); }
static void read_files(metafile_t *m, queue_t *q, unsigned char *pos) { int fd; /* file descriptor */ flist_t *f; /* pointer to a place in the file list */ ssize_t r = 0; /* number of bytes read from file(s) into the read buffer */ #ifndef NO_HASH_CHECK off_t counter = 0; /* number of bytes hashed should match size when done */ #endif piece_t *p = get_free(q, m->piece_length); /* go through all the files in the file list */ for (f = m->file_list; f; f = f->next) { /* open the current file for reading */ if ((fd = open(f->path, OPENFLAGS)) == -1) { fprintf(stderr, "Error opening '%s' for reading: %s\n", f->path, strerror(errno)); exit(EXIT_FAILURE); } while (1) { ssize_t d = read(fd, p->data + r, m->piece_length - r); if (d < 0) { fprintf(stderr, "Error reading from '%s': %s\n", f->path, strerror(errno)); exit(EXIT_FAILURE); } if (d == 0) /* end of file */ break; r += d; if (r == m->piece_length) { p->dest = pos; p->len = m->piece_length; put_full(q, p); pos += SHA_DIGEST_LENGTH; #ifndef NO_HASH_CHECK counter += r; #endif r = 0; p = get_free(q, m->piece_length); } } /* now close the file */ if (close(fd)) { fprintf(stderr, "Error closing '%s': %s\n", f->path, strerror(errno)); exit(EXIT_FAILURE); } } /* finally append the hash of the last irregular piece to the hash string */ if (r) { p->dest = pos; p->len = r; put_full(q, p); } else put_free(q, p, 0); #ifndef NO_HASH_CHECK counter += r; if (counter != m->size) { fprintf(stderr, "Counted %" PRIoff " bytes, " "but hashed %" PRIoff " bytes. " "Something is wrong...\n", m->size, counter); exit(EXIT_FAILURE); } #endif }