Debian Codegraph @

biosig4c++/t240/sopen_fef_read.c

raw symbols SCIP
1 /*
2
3 sandbox is used for development and under constraction work
4 The functions here are either under construction or experimental.
5 The functions will be either fixed, then they are moved to another place;
6 or the functions are discarded. Do not rely on the interface in this function
7
8
9 $Id$
10 Copyright (C) 2009,2012 Alois Schloegl <[email protected]>
11 This file is part of the "BioSig for C/C++" repository
12 (biosig4c++) at http://biosig.sf.net/
13
14 BioSig is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 3
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
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; /* Decoded structure */
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 /* backup info for proper freeing of memery */
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 while (pos<hdr->HeadLen) {
82
83 fprintf(stdout,"%i/%i\n",pos,hdr->HeadLen);
84
85 rval = ber_decode(opt_codec_ctx, pduType, (void **)&SAS, hdr->AS.Header+pos, hdr->HeadLen-32);
86 pos += rval.consumed;
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 // asn_fprint(stdout, &asn_DEF_SessionArchiveSection, SAS);
93
94 // SAS->s_archive_id
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 Manufacturer information
101 **********************************/
102 asn_fprint(stdout, &asn_DEF_ManufacturerSpecificSection, SAS->manufacturerspecific);
103
104 /**********************************
105 Health Care provider
106 **********************************/
107 asn_fprint(stdout, &asn_DEF_HealthCareProviderSection, SAS->healthcareprovider);
108 asn_fprint(stdout, &asn_DEF_PatientDemographicsSection, &SAS->demographics);
109 }
110
111 /**********************************
112 demographic information
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); // Flawfinder: ignore
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); // Flawfinder: ignore
140 }
141 }
142
143 /**********************************
144 Test Section information
145 **********************************/
146
147 // asn_fprint(stdout, &asn_DEF_SessionTestSection, SAS->sessions.list.array[0]);
148 // asn_fprint(stdout, &asn_DEF_SessionTestSection, SAS->sessions.list.array[k]);
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 // asn_fprint(stdout, &asn_DEF_SessionTestSection, STS);
157
158 /******* Manufacturer ************/
159 // asn_fprint(stdout, &asn_DEF_MedicalDeviceSystemSection, &STS->medicaldevicesystem);
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 /******* Multimedia ************/
168
169 /******* Session Phase Section SPS ************/
170
171 // asn_fprint(stdout, &asn_DEF_SessionTestSection, SAS->sessions.list.array[0]);
172 // asn_fprint(stdout, &asn_DEF_SessionTestSection, SAS->sessions.list.array[k]);
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 // asn_fprint(stdout, &asn_DEF_SessionPhaseSection, SPS);
179 // asn_fprint(stdout, &asn_DEF_DescriptiveDataSection, &SPS->descriptivedata);
180
181 /**************** Descriptive Data Section DescDS ****************/
182
183 // SPS->descriptivedata->realtimesadescs
184 // asn_fprint(stdout, &asn_DEF_SessionTestSection, SAS->sessions.list.array[0]);
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 //******************* samplerate *****************/
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 // double Fs = (double)n / d;
223
224 //******************* scaling *****************/
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 //******************* bits *****************/
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) // ieee 754
244 {
245 if (bits==32)
246 hc->GDFTYP = 16; // float32
247 else if (bits==64)
248 hc->GDFTYP = 17; // float64
249 else if (bits==128)
250 hc->GDFTYP = 18; // float128
251 }
252 else if (dt==1) // signed
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) // unsigned
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 // else
272 /* Number of samples of this signal in a subblock of SAMDB.
273 Arraysize is actually redundant information in other cases
274 than distribution sample array. Therefore, if arraysize = 0,
275 the number of samples is calculated as <subblocklength>
276 divided by <sampleperiod> of the signal.
277 */
278
279 //******************* filters *****************/
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 // TODO: MetricCalEntry, MetricCalType,
293
294 // TODO:
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 /************** Measured Data Section MeasDS ***********************/
311 N = SPS->measureddata.list.count;
312 if (VERBOSE_LEVEL>7) fprintf(stdout,"Number of MeasuredData %i\n",(unsigned)N);
313 // asn_fprint(stdout, &asn_DEF_SessionPhaseSection, SPS);
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 // asn_fprint(stdout, &asn_DEF_RealTimeSampleArrayMeasuredDataSection, RTSAMDS);
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 // if (VERBOSE_LEVEL>7)
329 // asn_fprint(stdout, &asn_DEF_RealTimeSampleArrayMeasuredDataSection, MDS->realtimesas->list.array[n2]);
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 // &RTSAMDS->metriclist //
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 //RTSAMDS->data->buf
340 //RTSAMDS->data.size
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; // do not show non-signal data. TODO: use this data to decode event information
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 /****** Notes Section information SNS **********************************/
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 /******* post checks ************/
369
370 /*
371 if(errno) {
372 fprintf(stdout,"Error FEF: %s\n",strerror(errno));
373 // Error message is already printed
374 // exit(-1);
375 }
376 */
377
378 /* Check ASN.1 constraints */
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 // asn_fprint(stdout, pduType, structure);
388 // ASN_STRUCT_FREE(*pduType, SAS);
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 }