| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include "../biosig-dev.h" |
| 23 |
|
| 24 |
#define min(a,b) (((a) < (b)) ? (a) : (b)) |
| 25 |
#define max(a,b) (((a) > (b)) ? (a) : (b)) |
| 26 |
|
| 27 |
#ifdef __cplusplus |
| 28 |
extern "C" { |
| 29 |
#endif |
| 30 |
|
| 31 |
int sopen_SCP_write(HDRTYPE* hdr) { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
uint8_t* ptr; |
| 40 |
uint8_t* PtrCurSect; |
| 41 |
int curSect; |
| 42 |
uint32_t len; |
| 43 |
uint16_t crc; |
| 44 |
uint32_t i; |
| 45 |
uint32_t sectionStart; |
| 46 |
struct tm* T0_tm; |
| 47 |
double AVM, avm; |
| 48 |
uint16_t avm16; |
| 49 |
struct aecg* aECG; |
| 50 |
|
| 51 |
assert(hdr != NULL); |
| 52 |
assert(hdr->TYPE == SCP_ECG); |
| 53 |
|
| 54 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) : V%f\n",__FILE__,__LINE__,hdr->VERSION); |
| 55 |
|
| 56 |
if ((fabs(hdr->VERSION - 1.3)<0.01) && (fabs(hdr->VERSION-2.0)<0.01) && (fabs(hdr->VERSION-3.0)<0.01)) |
| 57 |
fprintf(stderr,"Warning SOPEN (SCP-WRITE): Version %f not supported\n",hdr->VERSION); |
| 58 |
|
| 59 |
uint8_t versionSection = (hdr->VERSION < 3.0) ? 20 : 29; |
| 60 |
uint8_t versionProtocol = versionSection; |
| 61 |
|
| 62 |
if (hdr->aECG==NULL) { |
| 63 |
fprintf(stderr,"Warning SOPEN_SCP_WRITE: No aECG info defined\n"); |
| 64 |
hdr->aECG = malloc(sizeof(struct aecg)); |
| 65 |
aECG = (struct aecg*)hdr->aECG; |
| 66 |
aECG->diastolicBloodPressure=0.0; |
| 67 |
aECG->systolicBloodPressure=0.0; |
| 68 |
aECG->MedicationDrugs="/0"; |
| 69 |
aECG->ReferringPhysician="/0"; |
| 70 |
aECG->LatestConfirmingPhysician="/0"; |
| 71 |
aECG->Diagnosis="/0"; |
| 72 |
aECG->EmergencyLevel=0; |
| 73 |
#if (BIOSIG_VERSION > 10500) |
| 74 |
aECG->Section8.NumberOfStatements = 0; |
| 75 |
aECG->Section8.Statements = NULL; |
| 76 |
aECG->Section11.NumberOfStatements = 0; |
| 77 |
aECG->Section11.Statements = NULL; |
| 78 |
#endif |
| 79 |
} |
| 80 |
else |
| 81 |
aECG = (struct aecg*)hdr->aECG; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
aECG->Section1.Tag14.INST_NUMBER = 0; |
| 87 |
aECG->Section1.Tag14.DEPT_NUMBER = 0; |
| 88 |
aECG->Section1.Tag14.DEVICE_ID = 0; |
| 89 |
aECG->Section1.Tag14.DeviceType = 0; |
| 90 |
aECG->Section1.Tag14.MANUF_CODE = 255; |
| 91 |
aECG->Section1.Tag14.MOD_DESC = "Cart1"; |
| 92 |
aECG->Section1.Tag14.VERSION = versionSection; |
| 93 |
aECG->Section1.Tag14.PROT_COMP_LEVEL = 0xA0; |
| 94 |
|
| 95 |
aECG->Section1.Tag14.LANG_SUPP_CODE = (versionSection < 25) ? 0x00 : 0x37; |
| 96 |
aECG->Section1.Tag14.ECG_CAP_DEV = 0xD0; |
| 97 |
aECG->Section1.Tag14.MAINS_FREQ = 0; |
| 98 |
aECG->Section1.Tag14.ANAL_PROG_REV_NUM = ""; |
| 99 |
aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV = ""; |
| 100 |
aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID = ""; |
| 101 |
aECG->Section1.Tag14.ACQ_DEV_SCP_SW = "OpenECG XML-SCP 1.00"; |
| 102 |
aECG->Section1.Tag14.ACQ_DEV_MANUF = "Manufacturer"; |
| 103 |
|
| 104 |
aECG->Section5.Length = 0; |
| 105 |
aECG->Section6.Length = 0; |
| 106 |
aECG->Section7.Length = 0; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
aECG->FLAG.HUFFMAN = 0; |
| 111 |
aECG->FLAG.REF_BEAT= 0; |
| 112 |
aECG->FLAG.DIFF = 0; |
| 113 |
aECG->FLAG.BIMODAL = 0; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
typeof(hdr->NS) NS = 0, k; |
| 123 |
for (k=0; k < hdr->NS; k++) { |
| 124 |
CHANNEL_TYPE *CH=hdr->CHANNEL+k; |
| 125 |
|
| 126 |
if ( CH->LeadIdCode > 255) CH->OnOff = 0; |
| 127 |
if ( (CH->PhysDimCode & 0xffe0) != PhysDimCode("V")) CH->OnOff = 0; |
| 128 |
|
| 129 |
if (CH->OnOff != 1) continue; |
| 130 |
NS++; |
| 131 |
} |
| 132 |
|
| 133 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) : v%f VERSION=%i\n",__FILE__,__LINE__, hdr->VERSION, versionSection); |
| 134 |
|
| 135 |
ptr = (uint8_t*)hdr->AS.Header; |
| 136 |
|
| 137 |
int NSections = (versionSection < 25) ? 12 : 19; |
| 138 |
|
| 139 |
sectionStart = 6+16+NSections*10; |
| 140 |
ptr = (uint8_t*)realloc(ptr,sectionStart); |
| 141 |
memset(ptr,0,sectionStart); |
| 142 |
|
| 143 |
uint32_t curSectLen; |
| 144 |
for (curSect=NSections-1; curSect>=0; curSect--) { |
| 145 |
|
| 146 |
curSectLen = 0; |
| 147 |
|
| 148 |
|
| 149 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) : Section %i/%i %i %p\n",__FILE__,__LINE__,curSect,NSections,sectionStart,ptr); |
| 150 |
|
| 151 |
if (curSect==0) |
| 152 |
{ |
| 153 |
hdr->HeadLen = sectionStart; |
| 154 |
ptr = (uint8_t*)realloc(ptr,hdr->HeadLen); |
| 155 |
|
| 156 |
curSectLen = 16; |
| 157 |
sectionStart = 6; |
| 158 |
|
| 159 |
curSectLen += NSections*10; |
| 160 |
} |
| 161 |
else if (curSect==1) |
| 162 |
{ |
| 163 |
ptr = (uint8_t*)realloc(ptr,sectionStart+10000); |
| 164 |
PtrCurSect = ptr+sectionStart; |
| 165 |
curSectLen = 16; |
| 166 |
char *nextPartOfPatientName=hdr->Patient.Name; |
| 167 |
|
| 168 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 0 \n"); |
| 169 |
|
| 170 |
|
| 171 |
if (!hdr->FLAG.ANONYMOUS && (nextPartOfPatientName != NULL) && strlen(nextPartOfPatientName)) |
| 172 |
{ |
| 173 |
*(ptr+sectionStart+curSectLen) = 0; |
| 174 |
len = strcspn(nextPartOfPatientName, "\x1f"); |
| 175 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 176 |
strncpy((char*)ptr+sectionStart+curSectLen+3,nextPartOfPatientName,len); |
| 177 |
nextPartOfPatientName += len+1; |
| 178 |
curSectLen += len+3; |
| 179 |
} |
| 180 |
|
| 181 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 1 \n"); |
| 182 |
|
| 183 |
|
| 184 |
if (!hdr->FLAG.ANONYMOUS && (nextPartOfPatientName != NULL) && strlen(nextPartOfPatientName)) |
| 185 |
{ |
| 186 |
*(ptr+sectionStart+curSectLen) = 1; |
| 187 |
len = strcspn(nextPartOfPatientName, "\x1f"); |
| 188 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 189 |
strncpy((char*)ptr+sectionStart+curSectLen+3,nextPartOfPatientName,len); |
| 190 |
nextPartOfPatientName += len+1; |
| 191 |
curSectLen += len+3; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 2 \n"); |
| 196 |
|
| 197 |
|
| 198 |
if (strlen(hdr->Patient.Id)>0) { |
| 199 |
*(ptr+sectionStart+curSectLen) = 2; |
| 200 |
len = strlen(hdr->Patient.Id) + 1; |
| 201 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 202 |
strncpy((char*)ptr+sectionStart+curSectLen+3,hdr->Patient.Id,len); |
| 203 |
curSectLen += len+3; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 3 \n"); |
| 208 |
|
| 209 |
if (!hdr->FLAG.ANONYMOUS && (nextPartOfPatientName != NULL) && strlen(nextPartOfPatientName)) |
| 210 |
{ |
| 211 |
*(ptr+sectionStart+curSectLen) = 3; |
| 212 |
len = strcspn(nextPartOfPatientName, "\x1f"); |
| 213 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 214 |
strncpy((char*)ptr+sectionStart+curSectLen+3,nextPartOfPatientName,len); |
| 215 |
nextPartOfPatientName += len+1; |
| 216 |
curSectLen += len+3; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if ((hdr->Patient.Birthday) > 0) { |
| 221 |
T0_tm = gdf_time2tm_time(hdr->Patient.Birthday); |
| 222 |
|
| 223 |
*(ptr+sectionStart+curSectLen) = 5; |
| 224 |
leu16a(4, ptr+sectionStart+curSectLen+1); |
| 225 |
leu16a(T0_tm->tm_year+1900, ptr+sectionStart+curSectLen+3); |
| 226 |
*(ptr+sectionStart+curSectLen+5) = (uint8_t)(T0_tm->tm_mon + 1); |
| 227 |
*(ptr+sectionStart+curSectLen+6) = (uint8_t)(T0_tm->tm_mday); |
| 228 |
curSectLen += 7; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if (hdr->Patient.Height>0.0) { |
| 233 |
*(ptr+sectionStart+curSectLen) = 6; |
| 234 |
leu16a(3, ptr+sectionStart+curSectLen+1); |
| 235 |
leu16a(hdr->Patient.Height, ptr+sectionStart+curSectLen+3); |
| 236 |
*(ptr+sectionStart+curSectLen+5) = 1; |
| 237 |
curSectLen += 6; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
if (hdr->Patient.Weight>0.0) { |
| 242 |
*(ptr+sectionStart+curSectLen) = 7; |
| 243 |
leu16a(3, ptr+sectionStart+curSectLen+1); |
| 244 |
leu16a(hdr->Patient.Weight, ptr+sectionStart+curSectLen+3); |
| 245 |
*(ptr+sectionStart+curSectLen+5) = 1; |
| 246 |
curSectLen += 6; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if (hdr->Patient.Sex != 0) { |
| 251 |
*(ptr+sectionStart+curSectLen) = 8; |
| 252 |
leu16a(1, ptr+sectionStart+curSectLen+1); |
| 253 |
*(ptr+sectionStart+curSectLen+3) = hdr->Patient.Sex; |
| 254 |
curSectLen += 4; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if (aECG->systolicBloodPressure>0.0) { |
| 259 |
*(ptr+sectionStart+curSectLen) = 11; |
| 260 |
leu16a(2, ptr+sectionStart+curSectLen+1); |
| 261 |
leu16a((uint16_t)aECG->systolicBloodPressure, ptr+sectionStart+curSectLen+3); |
| 262 |
curSectLen += 5; |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
if (aECG->diastolicBloodPressure>0.0) { |
| 267 |
*(ptr+sectionStart+curSectLen) = 12; |
| 268 |
leu16a(2, ptr+sectionStart+curSectLen+1); |
| 269 |
leu16a((uint16_t)aECG->diastolicBloodPressure, ptr+sectionStart+curSectLen+3); |
| 270 |
curSectLen += 5; |
| 271 |
}; |
| 272 |
|
| 273 |
aECG->Diagnosis=""; |
| 274 |
len = strlen(aECG->Diagnosis); |
| 275 |
if (len>0) { |
| 276 |
*(ptr+sectionStart+curSectLen) = 13; |
| 277 |
len = min(64,len+1); |
| 278 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 279 |
strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->Diagnosis,len); |
| 280 |
curSectLen += 3+len; |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 14 \n"); |
| 285 |
|
| 286 |
|
| 287 |
*(ptr+sectionStart+curSectLen) = 14; |
| 288 |
|
| 289 |
|
| 290 |
memset(ptr+sectionStart+curSectLen+3,0,41); |
| 291 |
|
| 292 |
curSectLen += 3; |
| 293 |
leu16a(aECG->Section1.Tag14.INST_NUMBER, ptr+sectionStart+curSectLen); |
| 294 |
leu16a(aECG->Section1.Tag14.DEPT_NUMBER, ptr+sectionStart+curSectLen+2); |
| 295 |
leu16a(aECG->Section1.Tag14.DEVICE_ID, ptr+sectionStart+curSectLen+4); |
| 296 |
*(ptr+sectionStart+curSectLen+ 6) = aECG->Section1.Tag14.DeviceType; |
| 297 |
*(ptr+sectionStart+curSectLen+ 7) = aECG->Section1.Tag14.MANUF_CODE; |
| 298 |
strncpy((char*)(ptr+sectionStart+curSectLen+8), aECG->Section1.Tag14.MOD_DESC, 6); |
| 299 |
*(ptr+sectionStart+curSectLen+14) = versionSection; |
| 300 |
*(ptr+sectionStart+curSectLen+14) = aECG->Section1.Tag14.VERSION; |
| 301 |
*(ptr+sectionStart+curSectLen+15) = aECG->Section1.Tag14.PROT_COMP_LEVEL; |
| 302 |
*(ptr+sectionStart+curSectLen+16) = aECG->Section1.Tag14.LANG_SUPP_CODE; |
| 303 |
*(ptr+sectionStart+curSectLen+17) = aECG->Section1.Tag14.ECG_CAP_DEV; |
| 304 |
*(ptr+sectionStart+curSectLen+18) = aECG->Section1.Tag14.MAINS_FREQ; |
| 305 |
*(ptr+sectionStart+curSectLen+35) = strlen(aECG->Section1.Tag14.ANAL_PROG_REV_NUM)+1; |
| 306 |
uint16_t len1 = 36; |
| 307 |
|
| 308 |
char* tmp; |
| 309 |
tmp = aECG->Section1.Tag14.ANAL_PROG_REV_NUM; |
| 310 |
len = min(25, strlen(tmp) + 1); |
| 311 |
strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); |
| 312 |
len1 += len; |
| 313 |
|
| 314 |
tmp = aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV; |
| 315 |
len = min(25, strlen(tmp) + 1); |
| 316 |
strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); |
| 317 |
len1 += len; |
| 318 |
|
| 319 |
tmp = aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID; |
| 320 |
len = min(25, strlen(tmp) + 1); |
| 321 |
strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); |
| 322 |
len1 += len; |
| 323 |
|
| 324 |
tmp = aECG->Section1.Tag14.ACQ_DEV_SCP_SW; |
| 325 |
len = min(25, strlen(tmp) + 1); |
| 326 |
strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); |
| 327 |
len1 += len; |
| 328 |
|
| 329 |
tmp = aECG->Section1.Tag14.ACQ_DEV_MANUF; |
| 330 |
len = min(25, strlen(tmp) + 1); |
| 331 |
strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); |
| 332 |
len1 += len; |
| 333 |
|
| 334 |
leu16a(len1, ptr+sectionStart+curSectLen+1-3); |
| 335 |
curSectLen += len1; |
| 336 |
|
| 337 |
|
| 338 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 16 \n"); |
| 339 |
len = hdr->ID.Hospital ? strlen(hdr->ID.Hospital) : 0; |
| 340 |
if (len > 0) { |
| 341 |
*(ptr+sectionStart+curSectLen) = 16; |
| 342 |
len = min(64,len+1); |
| 343 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 344 |
strncpy((char*)(ptr+sectionStart+curSectLen+3),hdr->ID.Hospital,len); |
| 345 |
curSectLen += 3+len; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 20 \n"); |
| 350 |
len = aECG->ReferringPhysician ? strlen(aECG->ReferringPhysician) : 0; |
| 351 |
if (len > 0) { |
| 352 |
*(ptr+sectionStart+curSectLen) = 20; |
| 353 |
len = min(64,len+1); |
| 354 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 355 |
strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->ReferringPhysician,len); |
| 356 |
curSectLen += 3+len; |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 21 \n"); |
| 361 |
len = aECG->MedicationDrugs ? strlen(aECG->MedicationDrugs) : 0; |
| 362 |
if (len>0) { |
| 363 |
*(ptr+sectionStart+curSectLen) = 21; |
| 364 |
len = min(64,len+1); |
| 365 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 366 |
strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->MedicationDrugs,len); |
| 367 |
curSectLen += 3+len; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 22 \n"); |
| 372 |
len = hdr->ID.Technician ? strlen(hdr->ID.Technician) : 0; |
| 373 |
if (len > 0) { |
| 374 |
*(ptr+sectionStart+curSectLen) = 22; |
| 375 |
len = min(64,len+1); |
| 376 |
leu16a(len, ptr+sectionStart+curSectLen+1); |
| 377 |
strncpy((char*)(ptr+sectionStart+curSectLen+3),hdr->ID.Technician,len); |
| 378 |
curSectLen += 3+len; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 24 \n"); |
| 383 |
*(ptr+sectionStart+curSectLen) = 24; |
| 384 |
leu16a(1, ptr+sectionStart+curSectLen+1); |
| 385 |
*(ptr+sectionStart+curSectLen+3) = aECG->EmergencyLevel; |
| 386 |
curSectLen += 4; |
| 387 |
|
| 388 |
|
| 389 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 25 \n"); |
| 390 |
|
| 391 |
#if __FreeBSD__ || __APPLE__ || __NetBSD__ |
| 392 |
T0_tm = gdf_time2tm_time(hdr->T0); |
| 393 |
T0_tm->tm_gmtoff = hdr->tzmin*60; |
| 394 |
#else |
| 395 |
gdf_time T1 = hdr->T0; |
| 396 |
T1 += (int32_t)ldexp(timezone/86400.0,32); |
| 397 |
T0_tm = gdf_time2tm_time(T1); |
| 398 |
#endif |
| 399 |
|
| 400 |
*(ptr+sectionStart+curSectLen) = 25; |
| 401 |
leu16a(4, ptr+sectionStart+curSectLen+1); |
| 402 |
leu16a(T0_tm->tm_year+1900, ptr+sectionStart+curSectLen+3); |
| 403 |
*(ptr+sectionStart+curSectLen+5) = (uint8_t)(T0_tm->tm_mon + 1); |
| 404 |
*(ptr+sectionStart+curSectLen+6) = (uint8_t)T0_tm->tm_mday; |
| 405 |
curSectLen += 7; |
| 406 |
|
| 407 |
|
| 408 |
*(ptr+sectionStart+curSectLen) = 26; |
| 409 |
leu16a(3, ptr+sectionStart+curSectLen+1); |
| 410 |
*(ptr+sectionStart+curSectLen+3) = (uint8_t)T0_tm->tm_hour; |
| 411 |
*(ptr+sectionStart+curSectLen+4) = (uint8_t)T0_tm->tm_min; |
| 412 |
*(ptr+sectionStart+curSectLen+5) = (uint8_t)T0_tm->tm_sec; |
| 413 |
curSectLen += 6; |
| 414 |
|
| 415 |
if (NS>0) { |
| 416 |
CHANNEL_TYPE *CH = hdr->CHANNEL; |
| 417 |
while (CH->OnOff != 1) CH++; |
| 418 |
|
| 419 |
|
| 420 |
*(ptr+sectionStart+curSectLen) = 27; |
| 421 |
leu16a(2, ptr+sectionStart+curSectLen+1); |
| 422 |
leu16a((uint16_t)CH->HighPass, ptr+sectionStart+curSectLen+3); |
| 423 |
curSectLen += 5; |
| 424 |
|
| 425 |
|
| 426 |
*(ptr+sectionStart+curSectLen) = 28; |
| 427 |
leu16a(2, ptr+sectionStart+curSectLen+1); |
| 428 |
leu16a((uint16_t)CH->LowPass, ptr+sectionStart+curSectLen+3); |
| 429 |
curSectLen += 5; |
| 430 |
|
| 431 |
|
| 432 |
uint8_t bitmap = 0; |
| 433 |
if (fabs(CH->LowPass-60.0)<0.01) |
| 434 |
bitmap = 1; |
| 435 |
else if (fabs(CH->LowPass-50.0)<0.01) |
| 436 |
bitmap = 2; |
| 437 |
else |
| 438 |
bitmap = 0; |
| 439 |
*(ptr+sectionStart+curSectLen) = 29; |
| 440 |
leu16a(1, ptr+sectionStart+curSectLen+1); |
| 441 |
*(ptr+sectionStart+curSectLen+3) = bitmap; |
| 442 |
curSectLen += 4; |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"Section 1 Tag 32 \n"); |
| 448 |
|
| 449 |
*(ptr+sectionStart+curSectLen) = 32; |
| 450 |
leu16a(2, ptr+sectionStart+curSectLen+1); |
| 451 |
if (hdr->Patient.Impairment.Heart==1) { |
| 452 |
*(ptr+sectionStart+curSectLen+3) = 0; |
| 453 |
*(ptr+sectionStart+curSectLen+4) = 1; |
| 454 |
curSectLen += 5; |
| 455 |
} |
| 456 |
else if (hdr->Patient.Impairment.Heart==3) { |
| 457 |
*(ptr+sectionStart+curSectLen+3) = 0; |
| 458 |
*(ptr+sectionStart+curSectLen+4) = 42; |
| 459 |
curSectLen += 5; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
*(ptr+sectionStart+curSectLen) = 34; |
| 464 |
leu16a(5, ptr+sectionStart+curSectLen+1); |
| 465 |
lei16a(hdr->tzmin, ptr+sectionStart+curSectLen+3); |
| 466 |
lei16a(0, ptr+sectionStart+curSectLen+5); |
| 467 |
curSectLen += 8; |
| 468 |
|
| 469 |
|
| 470 |
*(ptr+sectionStart+curSectLen) = 255; |
| 471 |
leu16a(0, ptr+sectionStart+curSectLen+1); |
| 472 |
curSectLen += 3; |
| 473 |
|
| 474 |
|
| 475 |
if (curSectLen & 1) { |
| 476 |
*(ptr+sectionStart+curSectLen++) = 0; |
| 477 |
} |
| 478 |
|
| 479 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"End-of-Section %i %p\n",curSect,ptr); |
| 480 |
|
| 481 |
} |
| 482 |
else if (curSect==2) |
| 483 |
{ |
| 484 |
} |
| 485 |
else if (curSect==3) |
| 486 |
{ |
| 487 |
ptr = (uint8_t*)realloc(ptr,sectionStart+16+2+9*NS+1); |
| 488 |
PtrCurSect = ptr+sectionStart; |
| 489 |
curSectLen = 16; |
| 490 |
|
| 491 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...) data.size=[%d,%d]\n",__FILE__,__LINE__,__func__,hdr->data.size[0],hdr->data.size[1]); |
| 492 |
|
| 493 |
|
| 494 |
*(ptr+sectionStart+curSectLen++) = NS; |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
*(ptr+sectionStart+curSectLen++) = (NS<<3) | 0x04; |
| 501 |
uint32_t SPR = hdr->SPR * hdr->NRec; |
| 502 |
for (i = 0; i < hdr->NS; i++) { |
| 503 |
CHANNEL_TYPE *CH=hdr->CHANNEL+i; |
| 504 |
if (CH->OnOff != 1) continue; |
| 505 |
|
| 506 |
leu32a(1L, ptr+sectionStart+curSectLen); |
| 507 |
leu32a(SPR, ptr+sectionStart+curSectLen+4); |
| 508 |
*(ptr+sectionStart+curSectLen+8) = (uint8_t)CH->LeadIdCode; |
| 509 |
curSectLen += 9; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
if ((curSectLen % 2) != 0) { |
| 514 |
*(ptr+sectionStart+curSectLen++) = 0; |
| 515 |
} |
| 516 |
memset(ptr+sectionStart+10,0,6); |
| 517 |
} |
| 518 |
else if (curSect==4) |
| 519 |
{ |
| 520 |
} |
| 521 |
else if (curSect==5) |
| 522 |
{ |
| 523 |
curSectLen = 0; |
| 524 |
aECG->Section5.StartPtr = sectionStart; |
| 525 |
aECG->Section5.Length = curSectLen; |
| 526 |
} |
| 527 |
else if (curSect==6) |
| 528 |
{ |
| 529 |
|
| 530 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): Section %d v%3.1g %i,%i \n",__func__,__LINE__,curSect, versionSection*0.1,(int)aECG->Section6.StartPtr,(int)aECG->Section6.Length); |
| 531 |
|
| 532 |
if (versionSection < 25) |
| 533 |
{ |
| 534 |
uint16_t GDFTYP = 3; |
| 535 |
size_t SZ = GDFTYP_BITS[GDFTYP]>>3; |
| 536 |
typeof(hdr->NS) i=0; |
| 537 |
for (i = 0; i < hdr->NS; i++) { |
| 538 |
CHANNEL_TYPE *hc=hdr->CHANNEL+i; |
| 539 |
if (hc->OnOff != 1) continue; |
| 540 |
hc->GDFTYP = GDFTYP; |
| 541 |
} |
| 542 |
ptr = (uint8_t*)realloc(ptr,sectionStart+16+6+2*NS+SZ*(hdr->data.size[0]*hdr->data.size[1])); |
| 543 |
|
| 544 |
PtrCurSect = ptr+sectionStart; |
| 545 |
curSectLen = 16; |
| 546 |
|
| 547 |
|
| 548 |
char flagfirst = 1; |
| 549 |
for (i = 0; i < hdr->NS; i++) { |
| 550 |
CHANNEL_TYPE *CH=hdr->CHANNEL+i; |
| 551 |
|
| 552 |
if (VERBOSE_LEVEL>8) fprintf(stdout,"%s (line %i): %d#%d %g/%g \n",__func__,__LINE__,CH->OnOff,i,avm,AVM); |
| 553 |
|
| 554 |
if (CH->OnOff != 1) continue; |
| 555 |
|
| 556 |
|
| 557 |
avm = CH->Cal * 1e9 * PhysDimScale(CH->PhysDimCode); |
| 558 |
if (flagfirst) { |
| 559 |
|
| 560 |
AVM = avm; |
| 561 |
flagfirst=0; |
| 562 |
continue; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
if (fabs((AVM - avm)/AVM) > 1e-14) |
| 567 |
fprintf(stderr,"Warning SOPEN (SCP-WRITE): scaling factors differ between channel #1 and #%i. Scaling factor of 1st channel is used.\n",i+1); |
| 568 |
}; |
| 569 |
avm16 = lrint(AVM); |
| 570 |
leu16a(avm16, ptr+sectionStart+curSectLen); |
| 571 |
avm = leu16p(ptr+sectionStart+curSectLen); |
| 572 |
|
| 573 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): avm:%g avm16:%d AVM:%g\n", __func__, __LINE__, avm, avm16, AVM); |
| 574 |
|
| 575 |
curSectLen += 2; |
| 576 |
if (fabs((AVM - avm)/AVM)>1e-14) |
| 577 |
fprintf(stderr,"Warning SOPEN (SCP-WRITE): Scaling factor has been truncated (%f instead %f).\n",avm,AVM); |
| 578 |
|
| 579 |
|
| 580 |
AVM = 1e6/hdr->SampleRate; |
| 581 |
avm16 = lrint(AVM); |
| 582 |
leu16a(avm16, ptr+sectionStart+curSectLen); |
| 583 |
avm = leu16p(ptr+sectionStart+curSectLen); |
| 584 |
curSectLen += 2; |
| 585 |
if (fabs((AVM - avm)/AVM)>1e-14) |
| 586 |
fprintf(stderr,"Warning SOPEN (SCP-WRITE): Sampling interval has been truncated (%f instead %f us).\n",avm,AVM); |
| 587 |
|
| 588 |
|
| 589 |
*(ptr+sectionStart+curSectLen++) = 0; |
| 590 |
|
| 591 |
|
| 592 |
*(ptr+sectionStart+curSectLen++) = 0; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
for (i = 0; i < NS; i++) { |
| 602 |
leu16a((uint16_t)hdr->data.size[0]*2, ptr+sectionStart+curSectLen); |
| 603 |
avm = leu16p(ptr+sectionStart+curSectLen); |
| 604 |
AVM = hdr->SPR*hdr->NRec*2; |
| 605 |
if (fabs((AVM - avm)/AVM)>1e-14) |
| 606 |
fprintf(stderr,"Warning SOPEN (SCP-WRITE): Block length truncated (%f instead %f us).\n",avm,AVM); |
| 607 |
curSectLen += 2; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
for (i = 0; i < hdr->NS; i++) { |
| 612 |
hdr->CHANNEL[i].SPR *= hdr->NRec; |
| 613 |
}; |
| 614 |
hdr->NRec = 1; |
| 615 |
|
| 616 |
|
| 617 |
curSectLen += SZ * hdr->SPR*hdr->NRec * NS; |
| 618 |
|
| 619 |
|
| 620 |
if ((curSectLen % 2) != 0) { |
| 621 |
fprintf(stderr,"Warning Section 6 has an odd length\n"); |
| 622 |
*(ptr+sectionStart+curSectLen++) = 0; |
| 623 |
} |
| 624 |
|
| 625 |
memset(ptr+sectionStart+10,0,6); |
| 626 |
aECG->Section6.StartPtr = sectionStart; |
| 627 |
aECG->Section6.Length = curSectLen; |
| 628 |
} |
| 629 |
} |
| 630 |
else if (curSect==7) |
| 631 |
{ |
| 632 |
if (hdr->SCP.Section7 != NULL) { |
| 633 |
curSectLen = hdr->SCP.Section7Length+16; |
| 634 |
ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen); |
| 635 |
PtrCurSect = ptr+sectionStart; |
| 636 |
memcpy(PtrCurSect+16,hdr->SCP.Section7,hdr->SCP.Section7Length); |
| 637 |
} |
| 638 |
} |
| 639 |
else if (curSect==8) |
| 640 |
{ |
| 641 |
if (hdr->SCP.Section8 != NULL) { |
| 642 |
curSectLen = hdr->SCP.Section8Length+16; |
| 643 |
ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen); |
| 644 |
PtrCurSect = ptr+sectionStart; |
| 645 |
memcpy(PtrCurSect,hdr->SCP.Section8,hdr->SCP.Section8Length); |
| 646 |
} |
| 647 |
} |
| 648 |
else if (curSect==9) |
| 649 |
{ |
| 650 |
if (hdr->SCP.Section9 != NULL) { |
| 651 |
curSectLen = hdr->SCP.Section9Length+16; |
| 652 |
ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen); |
| 653 |
PtrCurSect = ptr+sectionStart; |
| 654 |
memcpy(PtrCurSect,hdr->SCP.Section9,hdr->SCP.Section9Length); |
| 655 |
} |
| 656 |
} |
| 657 |
else if (curSect==10) |
| 658 |
{ |
| 659 |
if (hdr->SCP.Section10 != NULL) { |
| 660 |
curSectLen = hdr->SCP.Section10Length+16; |
| 661 |
ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen); |
| 662 |
PtrCurSect = ptr+sectionStart; |
| 663 |
memcpy(PtrCurSect+16,hdr->SCP.Section10,hdr->SCP.Section10Length); |
| 664 |
} |
| 665 |
} |
| 666 |
else if (curSect==11) |
| 667 |
{ |
| 668 |
if (hdr->SCP.Section11 != NULL) { |
| 669 |
curSectLen = hdr->SCP.Section11Length+16; |
| 670 |
ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen); |
| 671 |
PtrCurSect = ptr+sectionStart; |
| 672 |
memcpy(PtrCurSect+16,hdr->SCP.Section11,hdr->SCP.Section11Length); |
| 673 |
} |
| 674 |
} |
| 675 |
else if (curSect==12) |
| 676 |
{ |
| 677 |
if (versionSection > 25 ) |
| 678 |
{ |
| 679 |
uint16_t gdftyp= 0; |
| 680 |
uint8_t bps = 0; |
| 681 |
|
| 682 |
uint16_t PhysDimBaseCode = 4256; |
| 683 |
for (k = 0; k < hdr->NS; k++) { |
| 684 |
CHANNEL_TYPE *hc = hdr->CHANNEL+k; |
| 685 |
if (hc->OnOff == 1) { |
| 686 |
if (bps < GDFTYP_BITS[gdftyp]) { |
| 687 |
bps = GDFTYP_BITS[hc->GDFTYP]; |
| 688 |
gdftyp = hc->GDFTYP; |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
if (NS>255) { |
| 694 |
NS=255; |
| 695 |
fprintf(stderr,"Warning SOPEN (SCP-WRITE): Number of channels exceeds 255, limited to 255.\n"); |
| 696 |
} |
| 697 |
|
| 698 |
bps = GDFTYP_BITS[gdftyp]>>3; |
| 699 |
double DigMax = ldexp(1.0, GDFTYP_BITS[gdftyp]-1)-1; |
| 700 |
|
| 701 |
int ns=0; |
| 702 |
for (k=0; k < hdr->NS; k++) { |
| 703 |
CHANNEL_TYPE *hc = hdr->CHANNEL+k; |
| 704 |
if (hc->OnOff > 0) { |
| 705 |
if (ns > 255) { |
| 706 |
hc->OnOff = 0; |
| 707 |
continue; |
| 708 |
} |
| 709 |
ns++; |
| 710 |
|
| 711 |
double scale = PhysDimScale(hc->PhysDimCode); |
| 712 |
hc->GDFTYP = gdftyp; |
| 713 |
hc->PhysMax = max(fabs(hc->PhysMax),fabs(-hc->PhysMin))*scale*1e9; |
| 714 |
hc->PhysMin = -hc->PhysMax; |
| 715 |
hc->Off = 0.0; |
| 716 |
hc->Cal = ceil(hc->PhysMax / hc->DigMax); |
| 717 |
|
| 718 |
hc->DigMin = -hc->DigMax; |
| 719 |
hc->PhysDimCode = 4276; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
curSectLen = 16 + 70 + NS * (4 + hdr->SPR*hdr->NRec * bps); |
| 724 |
curSectLen+= (curSectLen & 1); |
| 725 |
|
| 726 |
ptr = (uint8_t*)realloc(ptr, sectionStart + curSectLen); |
| 727 |
memset(ptr + sectionStart, 0, 16+70); |
| 728 |
|
| 729 |
PtrCurSect = ptr + sectionStart + 16; |
| 730 |
leu32a((uint32_t)hdr->SampleRate, PtrCurSect); |
| 731 |
PtrCurSect[4] = (uint8_t)NS; |
| 732 |
leu32a((uint32_t)(hdr->NRec*hdr->SPR), PtrCurSect + 5); |
| 733 |
PtrCurSect[9] = (uint8_t)bps; |
| 734 |
|
| 735 |
|
| 736 |
#if __FreeBSD__ || __APPLE__ || __NetBSD__ |
| 737 |
T0_tm = gdf_time2tm_time(hdr->T0); |
| 738 |
T0_tm->tm_gmtoff = hdr->tzmin*60; |
| 739 |
#else |
| 740 |
gdf_time T1 = hdr->T0; |
| 741 |
T1 += (int32_t)ldexp(timezone/86400.0,32); |
| 742 |
T0_tm = gdf_time2tm_time(T1); |
| 743 |
#endif |
| 744 |
|
| 745 |
leu16a(T0_tm->tm_year+1900, PtrCurSect+10); |
| 746 |
PtrCurSect[12] = (uint8_t)(T0_tm->tm_mon + 1); |
| 747 |
PtrCurSect[13] = (uint8_t)T0_tm->tm_mday; |
| 748 |
PtrCurSect[14] = (uint8_t)T0_tm->tm_hour; |
| 749 |
PtrCurSect[15] = (uint8_t)T0_tm->tm_min; |
| 750 |
PtrCurSect[16] = (uint8_t)T0_tm->tm_sec; |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
PtrCurSect += 70; |
| 755 |
for (k=0,ns=0; k < hdr->NS; k++) { |
| 756 |
CHANNEL_TYPE *hc = hdr->CHANNEL+k; |
| 757 |
if (hc->OnOff > 0) { |
| 758 |
PtrCurSect[ns*4] = hc->LeadIdCode; |
| 759 |
leu16a(hc->Cal, PtrCurSect+1+ns*4); |
| 760 |
PtrCurSect[3+ns*4] = 0; |
| 761 |
ns++; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
PtrCurSect += 4*NS; |
| 767 |
} |
| 768 |
aECG->Section12.StartPtr = sectionStart; |
| 769 |
aECG->Section12.Length = curSectLen; |
| 770 |
} |
| 771 |
else { |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
leu16a(curSect, ptr+curSect*10+6+16); |
| 776 |
leu32a(curSectLen, ptr+curSect*10+6+16+2); |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
if (curSectLen>0) { |
| 781 |
|
| 782 |
leu32a(sectionStart+1, ptr+curSect*10+6+16+6); |
| 783 |
|
| 784 |
|
| 785 |
leu16a(curSect, ptr+sectionStart+2); |
| 786 |
leu32a(curSectLen, ptr+sectionStart+4); |
| 787 |
ptr[sectionStart+8] = versionSection; |
| 788 |
ptr[sectionStart+9] = versionProtocol; |
| 789 |
if (curSect==0) { |
| 790 |
memcpy(ptr+16,"SCPECG",6); |
| 791 |
} |
| 792 |
else { |
| 793 |
memset(ptr+sectionStart+10,0,6); |
| 794 |
} |
| 795 |
crc = CRCEvaluate(ptr+sectionStart+2,curSectLen-2); |
| 796 |
leu16a(crc, ptr+sectionStart); |
| 797 |
} |
| 798 |
sectionStart += curSectLen; |
| 799 |
} |
| 800 |
|
| 801 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %d): %s(...)\n",__FILE__,__LINE__,__func__); |
| 802 |
|
| 803 |
|
| 804 |
if (versionSection < 25) |
| 805 |
hdr->AS.rawdata = ptr+aECG->Section6.StartPtr+16+6+2*NS; |
| 806 |
else |
| 807 |
hdr->AS.rawdata = ptr+aECG->Section12.StartPtr+16+70+4*NS; |
| 808 |
|
| 809 |
hdr->AS.Header = ptr; |
| 810 |
|
| 811 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %i,%i %i,%i \n",__func__,__LINE__,(int)aECG->Section6.StartPtr,(int)aECG->Section6.Length,(int)aECG->Section12.StartPtr,(int)aECG->Section12.Length); |
| 812 |
|
| 813 |
return(0); |
| 814 |
} |
| 815 |
|
| 816 |
#ifdef __cplusplus |
| 817 |
} |
| 818 |
#endif |
| 819 |
|