void FiffStreamThread::parseCommand(FiffTag::SPtr p_pTag) { if(p_pTag->size() >= 4) { qint32* t_pInt = (qint32*)p_pTag->data(); IOUtils::swap_intp(t_pInt); qint32 t_iCmd = t_pInt[0]; if(t_iCmd == MNE_RT_SET_CLIENT_ALIAS) { // // Set Client Alias // m_sDataClientAlias = QString(p_pTag->mid(4, p_pTag->size()-4)); printf("FiffStreamClient (ID %d): new alias = '%s'\r\n\n", m_iDataClientId, m_sDataClientAlias.toUtf8().constData()); } else if(t_iCmd == MNE_RT_GET_CLIENT_ID) { // // Send Client ID // printf("FiffStreamClient (ID %d): send client ID %d\r\n\n", m_iDataClientId, m_iDataClientId); writeClientId(); } else { printf("FiffStreamClient (ID %d): unknown command\r\n\n", m_iDataClientId); } } else { printf("FiffStreamClient (ID %d): unknown command\r\n\n", m_iDataClientId); } }
bool FiffTag::read_tag(FiffStream* p_pStream, FiffTag::SPtr& p_pTag, qint64 pos) { if (pos >= 0) { p_pStream->device()->seek(pos); } p_pTag = FiffTag::SPtr(new FiffTag()); // // Read fiff tag header from stream // *p_pStream >> p_pTag->kind; *p_pStream >> p_pTag->type; qint32 size; *p_pStream >> size; p_pTag->resize(size); *p_pStream >> p_pTag->next; // qDebug() << "read_tag" << " Kind:" << p_pTag->kind << " Type:" << p_pTag->type << " Size:" << p_pTag->size() << " Next:" << p_pTag->next; // // Read data when available // if (p_pTag->size() > 0) { p_pStream->readRawData(p_pTag->data(), p_pTag->size()); FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN); } if (p_pTag->next != FIFFV_NEXT_SEQ) p_pStream->device()->seek(p_pTag->next);//fseek(fid,tag.next,'bof'); return true; }
bool FiffTag::read_tag_data(FiffStream* p_pStream, FiffTag::SPtr& p_pTag, qint64 pos) { if(pos >= 0) { p_pStream->device()->seek(pos); } if(!p_pTag) return false; // // Read data when available // if (p_pTag->size() > 0) { p_pStream->readRawData(p_pTag->data(), p_pTag->size()); FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN); } if (p_pTag->next != FIFFV_NEXT_SEQ) p_pStream->device()->seek(p_pTag->next);//fseek(fid,tag.next,'bof'); return true; }
int ShmemSocket::receive_tag (FiffTag::SPtr& p_pTag) { struct sockaddr_un from; /* Address (not used) */ socklen_t fromlen; dacqDataMessageRec mess; /* This is the kind of message we receive */ int rlen; int data_ok = 0; p_pTag = FiffTag::SPtr(new FiffTag()); dacqShmBlock shmem = this->get_shmem(); dacqShmBlock shmBlock; dacqShmClient shmClient; int k; long read_loc = 0; if (m_iShmemSock < 0) return (OK); // // read from the socket // fromlen = sizeof(from); rlen = recvfrom(m_iShmemSock, (void *)(&mess), DATA_MESS_SIZE, 0, (sockaddr *)(&from), &fromlen); // qDebug() << "Mess Kind: " << mess.kind << " Type: " << mess.type << "Size: " << mess.size; // // Parse received message // if (rlen == -1) { printf("recvfrom");//dacq_perror("recvfrom"); this->close_socket (); return (FAIL); } /* A sanity check to survive at least some crazy messages */ if (mess.kind > 20000 || (unsigned long) mess.size > (size_t) 100000000) { printf("ALERT: Unreasonable data received, skipping! (size=%d)(kind=%d)", mess.kind, mess.size); //dacq_log("ALERT: Unreasonable data received, skipping! (size=%d)(kind=%d)", mess.kind, mess.size); mess.size = 0; mess.kind = FIFF_NOP; } p_pTag->kind = mess.kind; p_pTag->type = mess.type; p_pTag->next = 0; if ((unsigned long) mess.size > (size_t) 0) { p_pTag->resize(mess.size); } // qDebug() << mess.loc << " " << mess.size << " " << mess.shmem_buf << " " << mess.shmem_loc; if (mess.loc < 0 && (unsigned long) mess.size > (size_t) 0 && mess.shmem_buf < 0 && mess.shmem_loc < 0) { fromlen = sizeof(from); rlen = recvfrom(m_iShmemSock, (void *)p_pTag->data(), mess.size, 0, (sockaddr *)(&from), &fromlen); if (rlen == -1) { printf("recvfrom");//dacq_perror("recvfrom"); this->close_socket(); return (FAIL); } data_ok = 1; // if (mess.type == FIFFT_STRING) // data[mess.size] = '\0'; } else if ((unsigned long) mess.size > (size_t) 0) { /* * Copy data from shared memory */ if (mess.shmem_buf >= 0 && m_iShmemId/10000 > 0) { shmBlock = shmem + mess.shmem_buf; shmClient = shmBlock->clients; if (interesting_data(mess.kind)) { memcpy(p_pTag->data(),shmBlock->data,mess.size); data_ok = 1; #ifdef DEBUG printf("client # %d read shmem buffer # %d\n", m_iShmemId, mess.shmem_buf);//dacq_log("client # %d read shmem buffer # %d\n", id,mess.shmem_buf); #endif } /* * Indicate that this client has processed the data */ for (k = 0; k < SHM_MAX_CLIENT; k++,shmClient++) if (shmClient->client_id == m_iShmemId) shmClient->done = 1; } /* * Read data from file */ else { /* * Possibly read from shmem file */ if (m_iShmemId/10000 > 0 && mess.shmem_loc >= 0) { read_fd = shmem_fd; read_loc = mess.shmem_loc; } else { read_fd = fd; read_loc = mess.loc; } if (interesting_data(mess.kind)) { if (read_fif (read_fd,read_loc,mess.size,(char *)p_pTag->data()) == -1) { printf("Could not read data (tag = %d, size = %d, pos = %li)!\n", mess.kind,mess.size,read_loc);//dacq_log("Could not read data (tag = %d, size = %d, pos = %d)!\n", mess.kind,mess.size,read_loc); //dacq_log("%s\n",err_get_error()); } else { data_ok = 1; // if (mess.type == FIFFT_STRING) // data[mess.size] = '\0'; FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN); } } } } /* * Special case: close old input file */ if (mess.kind == FIFF_CLOSE_FILE) { if (fd != NULL) { printf("File to be closed (lib/FIFF_CLOSE_FILE).\n");//dacq_log("File to be closed (lib/FIFF_CLOSE_FILE).\n"); (void)fclose(fd); fd = NULL; } else printf("No file to close (lib/FIFF_CLOSE_FILE).\n");//dacq_log("No file to close (lib/FIFF_CLOSE_FILE).\n"); } /* * Another special case: open new input file */ else if (mess.kind == FIFF_NEW_FILE) { if (fd != NULL) { (void)fclose(fd); printf("File closed (lib/FIFF_NEW_FILE).\n");//dacq_log("File closed (lib/FIFF_NEW_FILE).\n"); } fd = open_fif((char *)p_pTag->data()); free (filename); filename = strdup((char *)p_pTag->data()); if (shmem_fd == NULL) shmem_fd = open_fif (SHM_FAIL_FILE); } if (p_pTag->size() <= 0) { data_ok = 0; return (FAIL); } return (OK); }
//************************************************************************************************************* //ToDo remove this function by swapping -> define little endian big endian, QByteArray void FiffTag::convert_tag_data(FiffTag::SPtr tag, int from_endian, int to_endian) { int np; int k,r;//,c; char *offset; fiff_int_t *ithis; fiff_short_t *sthis; fiff_long_t *lthis; float *fthis; double *dthis; // fiffDirEntry dethis; // fiffId idthis; // fiffChInfoRec* chthis;//FiffChInfo* chthis;//ToDo adapt parsing to the new class // fiffChPos cpthis; // fiffCoordTrans ctthis; // fiffDigPoint dpthis; fiffDataRef drthis; if (tag->data() == NULL || tag->size() == 0) return; if (from_endian == FIFFV_NATIVE_ENDIAN) from_endian = NATIVE_ENDIAN; if (to_endian == FIFFV_NATIVE_ENDIAN) to_endian = NATIVE_ENDIAN; if (from_endian == to_endian) return; if (fiff_type_fundamental(tag->type) == FIFFTS_FS_MATRIX) { if (from_endian == NATIVE_ENDIAN) convert_matrix_to_file_data(tag); else convert_matrix_from_file_data(tag); return; } switch (tag->type) { case FIFFT_INT : case FIFFT_JULIAN : case FIFFT_UINT : np = tag->size()/sizeof(fiff_int_t); for (ithis = (fiff_int_t *)tag->data(), k = 0; k < np; k++, ithis++) IOUtils::swap_intp(ithis); break; case FIFFT_LONG : case FIFFT_ULONG : np = tag->size()/sizeof(fiff_long_t); for (lthis = (fiff_long_t *)tag->data(), k = 0; k < np; k++, lthis++) IOUtils::swap_longp(lthis); break; case FIFFT_SHORT : case FIFFT_DAU_PACK16 : case FIFFT_USHORT : np = tag->size()/sizeof(fiff_short_t); for (sthis = (fiff_short_t *)tag->data(), k = 0; k < np; k++, sthis++) *sthis = IOUtils::swap_short(*sthis); break; case FIFFT_FLOAT : case FIFFT_COMPLEX_FLOAT : np = tag->size()/sizeof(fiff_float_t); for (fthis = (fiff_float_t *)tag->data(), k = 0; k < np; k++, fthis++) IOUtils::swap_floatp(fthis); break; case FIFFT_DOUBLE : case FIFFT_COMPLEX_DOUBLE : np = tag->size()/sizeof(fiff_double_t); for (dthis = (fiff_double_t *)tag->data(), k = 0; k < np; k++, dthis++) IOUtils::swap_doublep(dthis); break; case FIFFT_OLD_PACK : fthis = (float *)tag->data(); /* * Offset and scale... */ IOUtils::swap_floatp(fthis+0); IOUtils::swap_floatp(fthis+1); sthis = (short *)(fthis+2); np = (tag->size() - 2*sizeof(float))/sizeof(short); for (k = 0; k < np; k++,sthis++) *sthis = IOUtils::swap_short(*sthis); break; case FIFFT_DIR_ENTRY_STRUCT : // np = tag->size/sizeof(fiffDirEntryRec); // for (dethis = (fiffDirEntry)tag->data->data(), k = 0; k < np; k++, dethis++) { // dethis->kind = swap_int(dethis->kind); // dethis->type = swap_int(dethis->type); // dethis->size = swap_int(dethis->size); // dethis->pos = swap_int(dethis->pos); // } np = tag->size()/FiffDirEntry::storageSize(); for (k = 0; k < np; k++) { offset = (char*)tag->data() + k*FiffDirEntry::storageSize(); ithis = (fiff_int_t*) offset; ithis[0] = IOUtils::swap_int(ithis[0]);//kind ithis[1] = IOUtils::swap_int(ithis[1]);//type ithis[2] = IOUtils::swap_int(ithis[2]);//size ithis[3] = IOUtils::swap_int(ithis[3]);//pos } break; case FIFFT_ID_STRUCT : // np = tag->size/sizeof(fiffIdRec); // for (idthis = (fiffId)tag->data->data(), k = 0; k < np; k++, idthis++) { // idthis->version = swap_int(idthis->version); // idthis->machid[0] = swap_int(idthis->machid[0]); // idthis->machid[1] = swap_int(idthis->machid[1]); // idthis->time.secs = swap_int(idthis->time.secs); // idthis->time.usecs = swap_int(idthis->time.usecs); // } np = tag->size()/FiffId::storageSize(); for (k = 0; k < np; k++) { offset = (char*)tag->data() + k*FiffId::storageSize(); ithis = (fiff_int_t*) offset; ithis[0] = IOUtils::swap_int(ithis[0]);//version ithis[1] = IOUtils::swap_int(ithis[1]);//machid[0] ithis[2] = IOUtils::swap_int(ithis[2]);//machid[1] ithis[3] = IOUtils::swap_int(ithis[3]);//time.secs ithis[4] = IOUtils::swap_int(ithis[4]);//time.usecs } break; case FIFFT_CH_INFO_STRUCT : // np = tag->size/sizeof(fiffChInfoRec); // for (chthis = (fiffChInfoRec*)tag->data->data(), k = 0; k < np; k++, chthis++) { // chthis->scanNo = swap_int(chthis->scanNo); // chthis->logNo = swap_int(chthis->logNo); // chthis->kind = swap_int(chthis->kind); // swap_floatp(&chthis->range); // swap_floatp(&chthis->cal); // chthis->unit = swap_int(chthis->unit); // chthis->unit_mul = swap_int(chthis->unit_mul); // convert_ch_pos(&(chthis->chpos)); // } np = tag->size()/FiffChInfo::storageSize(); for (k = 0; k < np; k++) { offset = (char*)tag->data() + k*FiffChInfo::storageSize(); ithis = (fiff_int_t*) offset; fthis = (float*) offset; ithis[0] = IOUtils::swap_int(ithis[0]); //scanno ithis[1] = IOUtils::swap_int(ithis[1]); //logno ithis[2] = IOUtils::swap_int(ithis[2]); //kind IOUtils::swap_floatp(&fthis[3]); //range IOUtils::swap_floatp(&fthis[4]); //cal ithis[5] = IOUtils::swap_int(ithis[5]); //coil_type for (r = 0; r < 12; ++r) IOUtils::swap_floatp(&fthis[6+r]); //loc ithis[18] = IOUtils::swap_int(ithis[18]);//unit ithis[19] = IOUtils::swap_int(ithis[19]);//unit_mul } break; case FIFFT_CH_POS_STRUCT : // np = tag->size/sizeof(fiffChPosRec); // for (cpthis = (fiffChPos)tag->data->data(), k = 0; k < np; k++, cpthis++) // convert_ch_pos(cpthis); np = tag->size()/FiffChPos::storageSize(); for (k = 0; k < np; ++k) { offset = (char*)tag->data() + k*FiffChPos::storageSize(); ithis = (fiff_int_t*) offset; fthis = (float*) offset; ithis[0] = IOUtils::swap_int(ithis[0]); //coil_type for (r = 0; r < 12; r++) IOUtils::swap_floatp(&fthis[1+r]); //r0, ex, ey, ez } break; case FIFFT_DIG_POINT_STRUCT : // np = tag->size/sizeof(fiffDigPointRec); // for (dpthis = (fiffDigPoint)tag->data->data(), k = 0; k < np; k++, dpthis++) { // dpthis->kind = swap_int(dpthis->kind); // dpthis->ident = swap_int(dpthis->ident); // for (r = 0; r < 3; r++) // swap_floatp(&dpthis->r[r]); // } np = tag->size()/FiffDigPoint::storageSize(); for (k = 0; k < np; k++) { offset = tag->data() + k*FiffDigPoint::storageSize(); ithis = (fiff_int_t*) offset; fthis = (float*) offset; ithis[0] = IOUtils::swap_int(ithis[0]);//kind ithis[1] = IOUtils::swap_int(ithis[1]);//ident for (r = 0; r < 3; ++r) IOUtils::swap_floatp(&fthis[2+r]); //r } break; case FIFFT_COORD_TRANS_STRUCT : // np = tag->size/sizeof(fiffCoordTransRec); // for (ctthis = (fiffCoordTrans)tag->data->data(), k = 0; k < np; k++, ctthis++) { // ctthis->from = swap_int(ctthis->from); // ctthis->to = swap_int(ctthis->to); // for (r = 0; r < 3; r++) { // swap_floatp(&ctthis->move[r]); // swap_floatp(&ctthis->invmove[r]); // for (c = 0; c < 3; c++) { // swap_floatp(&ctthis->rot[r][c]); // swap_floatp(&ctthis->invrot[r][c]); // } // } // } np = tag->size()/FiffCoordTrans::storageSize(); for( k = 0; k < np; ++k) { offset = tag->data() + k*FiffCoordTrans::storageSize(); ithis = (fiff_int_t*)offset; fthis = (float*)offset; ithis[0] = IOUtils::swap_int(ithis[0]); ithis[1] = IOUtils::swap_int(ithis[1]); for (r = 0; r < 24; ++r) IOUtils::swap_floatp(&fthis[2+r]); } break; case FIFFT_DATA_REF_STRUCT : np = tag->size()/sizeof(fiffDataRefRec); for (drthis = (fiffDataRef)tag->data(), k = 0; k < np; k++, drthis++) { drthis->type = IOUtils::swap_int(drthis->type); drthis->endian = IOUtils::swap_int(drthis->endian); drthis->size = IOUtils::swap_long(drthis->size); drthis->offset = IOUtils::swap_long(drthis->offset); } break; default : break; } return; }
void FiffTag::convert_matrix_to_file_data(FiffTag::SPtr tag) /* * Assumes that the input is in the NATIVE_ENDIAN byte order and needs to be swapped to the other one */ { int ndim; int k; int *dimp,*data,kind,np; float *fdata; double *ddata; unsigned int tsize = tag->size(); if (fiff_type_fundamental(tag->type) != FIFFTS_FS_MATRIX) return; if (tag->data() == NULL) return; if (tsize < sizeof(fiff_int_t)) return; dimp = ((fiff_int_t *)(((char *)tag->data())+tag->size()-sizeof(fiff_int_t))); ndim = *dimp; IOUtils::swap_intp(dimp); if (fiff_type_matrix_coding(tag->type) == FIFFTS_MC_DENSE) { if (tsize < (ndim+1)*sizeof(fiff_int_t)) return; dimp = dimp - ndim; for (k = 0, np = 1; k < ndim; k++) { np = np*dimp[k]; IOUtils::swap_intp(dimp+k); } } else { if (tsize < (ndim+2)*sizeof(fiff_int_t)) return; if (ndim > 2) /* Not quite sure what to do */ return; dimp = dimp - ndim - 1; if (fiff_type_matrix_coding(tag->type) == FIFFTS_MC_CCS) np = dimp[0] + dimp[2] + 1; /* nz + n + 1 */ else if (fiff_type_matrix_coding(tag->type) == FIFFTS_MC_RCS) np = dimp[0] + dimp[1] + 1; /* nz + m + 1 */ else return; /* Don't know what to do */ for (k = 0; k < ndim+1; k++) IOUtils::swap_intp(dimp+k); } /* * Now convert data... */ kind = fiff_type_base(tag->type); if (kind == FIFFT_INT) { for (data = (int *)(tag->data()), k = 0; k < np; k++) IOUtils::swap_intp(data+k); } else if (kind == FIFFT_FLOAT) { for (fdata = (float *)(tag->data()), k = 0; k < np; k++) IOUtils::swap_floatp(fdata+k); } else if (kind == FIFFT_DOUBLE) { for (ddata = (double *)(tag->data()), k = 0; k < np; k++) IOUtils::swap_doublep(ddata+k); } else if (kind == FIFFT_COMPLEX_FLOAT) { for (fdata = (float *)(tag->data()), k = 0; k < 2*np; k++) IOUtils::swap_floatp(fdata+k); } else if (kind == FIFFT_COMPLEX_DOUBLE) { for (ddata = (double *)(tag->data()), k = 0; k < 2*np; k++) IOUtils::swap_doublep(ddata+k); } return; }