| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <ctype.h> |
| 32 |
#include <inttypes.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include "../biosig-dev.h" |
| 37 |
|
| 38 |
#ifdef WITH_FEF |
| 39 |
#include "SessionArchiveSection.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
EXTERN_C void sopen_fef_read(HDRTYPE* hdr) { |
| 44 |
|
| 45 |
|
| 46 |
#ifndef WITH_FEF |
| 47 |
|
| 48 |
B4C_ERRNUM = B4C_FORMAT_UNSUPPORTED; |
| 49 |
B4C_ERRMSG = "ASN1/FEF currently not supported"; |
| 50 |
|
| 51 |
#else |
| 52 |
|
| 53 |
fprintf(stdout,"ASN1: Warning FEF/ASN.1 decoding very experimental. Tested only with TaskForceMonitor (TFM). You are warned. \n"); |
| 54 |
|
| 55 |
static asn_TYPE_descriptor_t *pduType = &asn_DEF_SessionArchiveSection; |
| 56 |
SessionArchiveSection_t *SAS = NULL; |
| 57 |
SessionTestSection_t *STS = NULL; |
| 58 |
SessionPhaseSection_t *SPS = NULL; |
| 59 |
asn_codec_ctx_t *opt_codec_ctx = 0; |
| 60 |
asn_dec_rval_t rval; |
| 61 |
long ival; |
| 62 |
double val; |
| 63 |
|
| 64 |
|
| 65 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"ASN1: BER DECODING\n"); |
| 66 |
|
| 67 |
size_t pos =32; |
| 68 |
rval = ber_decode(0, pduType, (void **)&SAS, hdr->AS.Header+32, hdr->HeadLen-32); |
| 69 |
|
| 70 |
pos += rval.consumed; |
| 71 |
|
| 72 |
|
| 73 |
hdr->aECG = malloc(sizeof(struct asn1)); |
| 74 |
{ |
| 75 |
struct asn1 *asn1info = (struct asn1*)hdr->aECG; |
| 76 |
asn1info->pduType = pduType; |
| 77 |
asn1info->SAS = SAS; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%i/%i\nASN1: BER DECODING DONE\n",(unsigned)pos,hdr->HeadLen); |
| 90 |
|
| 91 |
if (VERBOSE_LEVEL>8) { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
asn_fprint(stdout, &asn_DEF_FEFString, &SAS->s_archive_id); |
| 96 |
asn_fprint(stdout, &asn_DEF_FEFString, &SAS->s_archive_name); |
| 97 |
asn_fprint(stdout, &asn_DEF_FEFString, SAS->s_archive_comments); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
asn_fprint(stdout, &asn_DEF_ManufacturerSpecificSection, SAS->manufacturerspecific); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
asn_fprint(stdout, &asn_DEF_HealthCareProviderSection, SAS->healthcareprovider); |
| 108 |
asn_fprint(stdout, &asn_DEF_PatientDemographicsSection, &SAS->demographics); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if (!asn_INTEGER2long(SAS->demographics.sex, &ival)) |
| 115 |
hdr->Patient.Sex = ival; |
| 116 |
|
| 117 |
{ |
| 118 |
if (!asn_REAL2double(&SAS->demographics.patientweight->value, &val)) |
| 119 |
hdr->Patient.Weight = (uint8_t)val; |
| 120 |
if (!asn_REAL2double(&SAS->demographics.patientheight->value, &val)) |
| 121 |
hdr->Patient.Height = (uint8_t)val; |
| 122 |
|
| 123 |
char *str1 = (char*)SAS->demographics.characternamegroup->givenname.buf; |
| 124 |
char *str2 = (char*)SAS->demographics.characternamegroup->middlename.buf; |
| 125 |
char *str3 = (char*)SAS->demographics.characternamegroup->familyname.buf; |
| 126 |
size_t l1 = strlen(str1); |
| 127 |
size_t l2 = strlen(str2); |
| 128 |
size_t l3 = strlen(str3); |
| 129 |
|
| 130 |
if (l1 <= MAX_LENGTH_NAME) { |
| 131 |
strcpy(hdr->Patient.Name, str1); |
| 132 |
} |
| 133 |
if (l1+l2+1 <= MAX_LENGTH_NAME) { |
| 134 |
hdr->Patient.Name[l1]=' '; |
| 135 |
strcpy(hdr->Patient.Name+1 + l1, str2); |
| 136 |
} |
| 137 |
if (l1+l2+l3+2 <= MAX_LENGTH_NAME) { |
| 138 |
hdr->Patient.Name[l1+l2+1]=' '; |
| 139 |
strcpy(hdr->Patient.Name+2+l1+l2, str3); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Number of TestSections %i\n",SAS->sessions.list.count); |
| 150 |
|
| 151 |
size_t N = SAS->sessions.list.count; |
| 152 |
if (N>1) |
| 153 |
biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Error SOPEN(FEF): multiple test sections are not supported, yet"); |
| 154 |
|
| 155 |
STS = SAS->sessions.list.array[0]; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
strncpy(hdr->ID.Manufacturer._field, (const char*)STS->medicaldevicesystem.systemmodel.manufacturer.buf, MAX_LENGTH_MANUF); |
| 161 |
int LEN = strlen(hdr->ID.Manufacturer._field)+1; |
| 162 |
hdr->ID.Manufacturer.Name = hdr->ID.Manufacturer._field; |
| 163 |
|
| 164 |
strncpy(hdr->ID.Manufacturer._field+LEN, (const char*)STS->medicaldevicesystem.systemmodel.model_number.buf, MAX_LENGTH_MANUF-LEN); |
| 165 |
hdr->ID.Manufacturer.Model = hdr->ID.Manufacturer._field+LEN; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
N = STS->phases.list.count; |
| 174 |
if (N>1) |
| 175 |
biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Error SOPEN(FEF): multiple test sections are not supported, yet"); |
| 176 |
|
| 177 |
SPS = STS->phases.list.array[0]; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
N = SPS->descriptivedata.realtimesadescs->list.count; |
| 186 |
|
| 187 |
hdr->NS = N; |
| 188 |
hdr->CHANNEL = (CHANNEL_TYPE*)calloc(hdr->NS,sizeof(CHANNEL_TYPE)); |
| 189 |
typeof(hdr->NS) k; |
| 190 |
uint64_t FsN=1,FsD=1; |
| 191 |
for (k=0; k<hdr->NS; k++) { |
| 192 |
|
| 193 |
RealTimeSampleArrayDescriptiveDataSection_t *RTSADDS = SPS->descriptivedata.realtimesadescs->list.array[k]; |
| 194 |
if (VERBOSE_LEVEL>8) { |
| 195 |
fprintf(stdout,"[FEF 212] #=%i/%i\n",k,hdr->NS); |
| 196 |
asn_fprint(stdout, &asn_DEF_RealTimeSampleArrayDescriptiveDataSection, RTSADDS); |
| 197 |
} |
| 198 |
|
| 199 |
CHANNEL_TYPE *hc = hdr->CHANNEL+k; |
| 200 |
|
| 201 |
strncpy(hc->Label, (const char*)RTSADDS->labelstring->buf, MAX_LENGTH_LABEL); |
| 202 |
|
| 203 |
if (!asn_INTEGER2long(RTSADDS->unitcode,&ival)) |
| 204 |
hc->PhysDimCode= (uint16_t)ival; |
| 205 |
else |
| 206 |
hc->PhysDimCode= 0; |
| 207 |
|
| 208 |
if (!hc->PhysDimCode) |
| 209 |
hc->PhysDimCode = PhysDimCode((const char*)RTSADDS->unitlabelstring->buf); |
| 210 |
|
| 211 |
|
| 212 |
long int n=0, d=0; |
| 213 |
asn_INTEGER2long(&RTSADDS->sampleperiod.denominator, &d); |
| 214 |
asn_INTEGER2long(&RTSADDS->sampleperiod.numerator,&n); |
| 215 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"#%i: %li %li\n",k,n,d); |
| 216 |
if (n && d) { |
| 217 |
FsN = lcm(FsN,n); |
| 218 |
FsD = lcm(FsD,d); |
| 219 |
} |
| 220 |
hc->SPR = d; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
hc->PhysMax = +100; |
| 226 |
hc->PhysMin = -100; |
| 227 |
hc->DigMax = +2047; |
| 228 |
hc->DigMin = -2048; |
| 229 |
if (asn_REAL2double(&RTSADDS->scaleandrangespec.lowerabsolutevalue,&hc->PhysMin) |
| 230 |
|| asn_REAL2double(&RTSADDS->scaleandrangespec.upperabsolutevalue,&hc->PhysMax) |
| 231 |
|| asn_REAL2double(&RTSADDS->scaleandrangespec.lowervaluescaled,&hc->DigMin) |
| 232 |
|| asn_REAL2double(&RTSADDS->scaleandrangespec.uppervaluescaled,&hc->DigMax) ) |
| 233 |
fprintf(stderr,"Warning (FEF): scaling factors in channel %i not available\n",k+1); |
| 234 |
|
| 235 |
hc->Cal = (hc->PhysMax-hc->PhysMin)/(hc->DigMax-hc->DigMin); |
| 236 |
hc->Off = hc->PhysMin-hc->Cal*hc->DigMin; |
| 237 |
|
| 238 |
|
| 239 |
long int bits=-1,dt=-1; |
| 240 |
asn_INTEGER2long(&RTSADDS->saspecification.storagesize,&bits); |
| 241 |
asn_INTEGER2long(&RTSADDS->saspecification.storagedatatype,&dt); |
| 242 |
hc->GDFTYP = 0; |
| 243 |
if (dt==2) |
| 244 |
{ |
| 245 |
if (bits==32) |
| 246 |
hc->GDFTYP = 16; |
| 247 |
else if (bits==64) |
| 248 |
hc->GDFTYP = 17; |
| 249 |
else if (bits==128) |
| 250 |
hc->GDFTYP = 18; |
| 251 |
} |
| 252 |
else if (dt==1) |
| 253 |
{ |
| 254 |
if (bits==8) hc->GDFTYP = 1; |
| 255 |
else if (bits==16) hc->GDFTYP = 3; |
| 256 |
else if (bits==24) hc->GDFTYP = 255+24; |
| 257 |
else if (bits==32) hc->GDFTYP = 5; |
| 258 |
else if (bits==64) hc->GDFTYP = 7; |
| 259 |
} |
| 260 |
else if (dt==0) |
| 261 |
{ |
| 262 |
if (bits==8) hc->GDFTYP = 2; |
| 263 |
else if (bits==16) hc->GDFTYP = 4; |
| 264 |
else if (bits==24) hc->GDFTYP = 511+24; |
| 265 |
else if (bits==32) hc->GDFTYP = 6; |
| 266 |
else if (bits==64) hc->GDFTYP = 8; |
| 267 |
} |
| 268 |
long int arraysize=0; |
| 269 |
asn_INTEGER2long(&RTSADDS->saspecification.storagesize,&arraysize); |
| 270 |
if (!arraysize) hc->SPR = arraysize; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if (RTSADDS->sasignalfrequency) { |
| 281 |
double d; |
| 282 |
asn_REAL2double(&RTSADDS->sasignalfrequency->lowedgefreq, &d); |
| 283 |
hc->HighPass = d; |
| 284 |
asn_REAL2double(&RTSADDS->sasignalfrequency->highedgefreq, &d); |
| 285 |
hc->LowPass = d; |
| 286 |
} else |
| 287 |
{ |
| 288 |
hc->HighPass = -1; |
| 289 |
hc->LowPass = INFINITY; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
hc->LeadIdCode= 0; |
| 296 |
strcpy(hc->Transducer, ""); |
| 297 |
hc->bi = 0; |
| 298 |
hc->OnOff = 1; |
| 299 |
hc->Notch = -1; |
| 300 |
hc->Impedance = INFINITY; |
| 301 |
hc->XYZ[0] = 0.0; |
| 302 |
hc->XYZ[1] = 0.0; |
| 303 |
hc->XYZ[2] = 0.0; |
| 304 |
} |
| 305 |
size_t d = gcd(FsD,FsN); |
| 306 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Fs=%i/%i\n",(unsigned)FsN,(unsigned)FsD); |
| 307 |
hdr->SampleRate = ((double)(FsD/d))/((double)(FsN/d)); |
| 308 |
hdr->SPR = FsD; |
| 309 |
|
| 310 |
|
| 311 |
N = SPS->measureddata.list.count; |
| 312 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Number of MeasuredData %i\n",(unsigned)N); |
| 313 |
|
| 314 |
|
| 315 |
for (k=0; k<N; k++) |
| 316 |
{ |
| 317 |
long int nrec,spr,n,d; |
| 318 |
|
| 319 |
MeasuredDataSection_t *MDS = SPS->measureddata.list.array[k]; |
| 320 |
|
| 321 |
|
| 322 |
unsigned n2; |
| 323 |
size_t listlen = MDS->realtimesas->list.count; |
| 324 |
hdr->SPR = 1; |
| 325 |
for (n2=0; n2 <listlen ; n2++) { |
| 326 |
CHANNEL_TYPE *hc = hdr->CHANNEL+n2; |
| 327 |
RealTimeSampleArrayMeasuredDataSection_t *RTSAMDS = MDS->realtimesas->list.array[n2]; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
asn_INTEGER2long(&RTSAMDS->numberofsubblocks,&nrec); |
| 332 |
asn_INTEGER2long(&RTSAMDS->subblocklength.numerator,&n); |
| 333 |
asn_INTEGER2long(&RTSAMDS->subblocklength.denominator,&d); |
| 334 |
asn_INTEGER2long(&RTSAMDS->subblocksize,&spr); |
| 335 |
|
| 336 |
|
| 337 |
if (VERBOSE_LEVEL>7) |
| 338 |
fprintf(stdout,"%i/%i blk: %i/%i #subblocks:%i subblocklength:%i/%i subblocksize:%i size:%i \n",(int)n2,(int)listlen,(int)k,(int)N,(int)nrec,(int)n,(int)d,(int)spr,(int)RTSAMDS->data.size); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
hc->bufptr = RTSAMDS->data.buf; |
| 343 |
hc->SPR = (RTSAMDS->data.size<<3)/GDFTYP_BITS[hc->GDFTYP]; |
| 344 |
if (hdr->SPR < hc->SPR) hdr->SPR = hc->SPR; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
for (k=0; k<hdr->NS; k++) { |
| 349 |
CHANNEL_TYPE *hc = hdr->CHANNEL+k; |
| 350 |
double d = ((double)hdr->SPR)/hc->SPR; |
| 351 |
hc->OnOff = (d==floor(d)) && hc->SPR>8; |
| 352 |
hc->bi = 0; |
| 353 |
hc->bi8 = 0; |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
hdr->NRec = 1; |
| 358 |
hdr->AS.first = 0; |
| 359 |
hdr->AS.length = 1; |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
if (VERBOSE_LEVEL>7) |
| 364 |
for (k=0; k<SAS->notes->list.count; k++) { |
| 365 |
asn_fprint(stdout, &asn_DEF_SessionNotesSection, SAS->notes->list.array[k++]); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
char errbuf[128]; |
| 380 |
size_t errlen = sizeof(errbuf); |
| 381 |
if(asn_check_constraints(pduType, SAS, |
| 382 |
errbuf, &errlen)) { |
| 383 |
fprintf(stdout, "SOPEN_FEF_READ: ASN.1 constraint " |
| 384 |
"check failed: %s\n", errbuf); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
#endif |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
EXTERN_C void sclose_fef_read(HDRTYPE* hdr) { |
| 395 |
|
| 396 |
if (VERBOSE_LEVEL>7) |
| 397 |
fprintf(stdout,"sclose_FEF_read\n"); |
| 398 |
#ifdef WITH_FEF |
| 399 |
|
| 400 |
if (hdr->aECG) { |
| 401 |
struct asn1 *asn1info = (struct asn1*)hdr->aECG; |
| 402 |
ASN_STRUCT_FREE((*(asn_TYPE_descriptor_t *)(asn1info->pduType)), ((SessionArchiveSection_t*)asn1info->SAS)); |
| 403 |
free(hdr->aECG); |
| 404 |
hdr->aECG = NULL; |
| 405 |
} |
| 406 |
#endif |
| 407 |
} |