/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * vim: set ts=8 sts=4 et sw=4 tw=99: * * SleepLib RemStar M-Series Loader Implementation * * Copyright (c) 2011 Mark Watkins * * This file is subject to the terms and conditions of the GNU General Public * License. See the file COPYING in the main directory of the Linux * distribution for more details. */ #include #include #include "mseries_loader.h" extern QProgressBar *qprogress; MSeries::MSeries(Profile *p,MachineID id) :CPAP(p,id) { m_class=mseries_class_name; properties[STR_PROP_Brand]="Respironics"; properties[STR_PROP_Model]=STR_MACH_MSeries; } MSeries::~MSeries() { } MSeriesLoader::MSeriesLoader() { epoch=QDateTime(QDate(2000,1,1),QTime(0,0,0),Qt::UTC).toTime_t(); epoch-=QDateTime(QDate(1970,1,1),QTime(0,0,0),Qt::UTC).toTime_t(); } MSeriesLoader::~MSeriesLoader() { } //struct MSeriesHeader { // quint8 b1; //0x52 // quint32 a32; //0x00000049 // quint16 u16[8]; // quint8 b2; //0x02 // char setname[16]; // char firstname[25]; // char lastname[25]; // char serial[50]; // quint16 b3; //0x00 // quint16 b4; //0x66 // quint16 b5; //0xff //} __attribute__((packed)); /* blockLayoutOffsets { cardInformationBlock = 0, brandID = 0, cardType = 2, cardVersion = 3 startUIDB = 4 endUIDB = 6, startCPB = 8, endCPB = 10, startCDCB = 12, endCDCB = 14, startCDB = 0x10, endCDB = 0x12, checksum = 20, userIDBlock = 0x15 personalID = 1, patientFName = 0x11, patientLName = 0x2a, serialNumber = 0x43, modelNumber = 0x4d, textData = 0x57 checksum = 0x77, cardPrescriptionBlock = 0x8d, cardDataControlBlock = 0xa3, validFlagOne = 3, headPtrOne = 4, tailPtrOne = 6, cdbChecksumOne = 8, validFlagTwo = 9 headPtrTwo = 10, tailPtrTwo = 12, cdbChecksumTwo = 14, cardDataBlock = 0xb2, basicCompliance = 1, fosq = 2, Invalid = 0xff, sleepProfile = 8, sleepProfile2 = 10, sleepProfile3 = 14, sleepTrend = 9, sleepTrend2 = 11, sleepTrend3 = 15, smartAutoCPAPProfile = 3, smartAutoCPAPTrend = 4, ventCompliance2 = 13, ventilatorCompliance = 7, ventilatorProfile = 6, ventProfile2 = 12 startChar = 0xfe, stopChar = 0x7f */ int MSeriesLoader::Open(QString & path,Profile *profile) { Q_UNUSED(profile); // Until a smartcard reader is written, this is not an auto-scanner.. it just opens a block file.. QFile file(path); if (!file.exists()) return 0; if (file.size()!=32768) // Check filesize matches smartcard? return 0; if (!file.open(QFile::ReadOnly)) { qDebug() << "Couldn't open M-Series file:" << path; return 0; } QByteArray block=file.readAll(); // Thanks to Phil Gillam for the pointers on this one.. const unsigned char * cardinfo=(unsigned char *)block.data(); quint16 magic=cardinfo[0] << 8 | cardinfo[1]; if (magic!=0x5249) { // "RI" Respironics Magic number return 0; } //quint8 cardtype=cardinfo[2]; //quint8 cardver=cardinfo[3]; quint16 user_offset=(cardinfo[4] << 8) | cardinfo[5]; //quint16 rx_offset=(cardinfo[8] << 8) | cardinfo[9]; quint16 control_offset=(cardinfo[12] << 8) | cardinfo[13]; //quint16 data_offset=(cardinfo[16] << 8) | cardinfo[17]; const char * userinfo=block.data()+user_offset; QString setname=QString(userinfo+0x1); QString firstname=QString(userinfo+0x11); QString lastname=QString(userinfo+0x2a); QString serial=QString(userinfo+0x43); serial.truncate(10); QString model=QString(userinfo+0x4d); QString textdata=QString(userinfo+0x57); quint8 userinfochk=*(userinfo+0x77); quint8 tmp=0; for (int i=0;i<0x77;i++) { tmp+=userinfo[i]; } if (tmp!=userinfochk) { qDebug() << "MSeries UserInfo block checksum failure" << path; } //const unsigned char * rxblock=(unsigned char *)block.data()+rx_offset; unsigned char * controlblock=(unsigned char *)block.data()+control_offset; quint16 count=controlblock[0] << 8 | controlblock[1]; // number of control blocks if (controlblock[1]!=controlblock[2]) { qDebug() << "Control block count does not match." << path; } QList head, tail; controlblock+=3; quint16 datastarts,dataends,tmp16,h16,t16; if (controlblock[0]) { datastarts=controlblock[1] | (controlblock[2] << 8); dataends=controlblock[3] | (controlblock[4] << 8); } controlblock+=6; if (controlblock[0]) { if ((controlblock[1] | (controlblock[2] << 8))!=datastarts) { qDebug() << "Non matching card size start identifier" << path; } if ((controlblock[3] | (controlblock[4] << 8))!=dataends) { qDebug() << "Non matching card size end identifier" << path; } } controlblock+=6; count-=2; tmp16=controlblock[0] | controlblock[1] << 8; controlblock+=2; for (int i=0;i ml=profile->GetMachines(MT_CPAP); bool found=false; QList::iterator i; for (i=ml.begin(); i!=ml.end(); i++) { if (((*i)->GetClass()==mseries_class_name) && ((*i)->properties[STR_PROP_Serial]==serial)) { MachList[serial]=*i; found=true; break; } } if (found) return *i; Machine *m=new MSeries(profile,0); MachList[serial]=m; profile->AddMachine(m); m->properties[STR_PROP_Serial]=serial; m->properties[STR_PROP_DataVersion]=QString::number(mseries_data_version); QString path="{"+STR_GEN_DataFolder+"}/"+m->GetClass()+"_"+serial+"/"; m->properties[STR_PROP_Path]=path; m->properties[STR_PROP_BackupPath]=path+"Backup/"; return m; } bool mseries_initialized=false; void MSeriesLoader::Register() { if (mseries_initialized) return; qDebug() << "Registering RemStar M-Series Loader"; RegisterLoader(new MSeriesLoader()); //InitModelMap(); mseries_initialized=true; }