class TCidDecoder
{

public:
#define LO_NIBBLE(val) (val & 0x0F)
#define HI_NIBBLE(val) ((val >> 4) & 0x0F)
#define IS_ASCII(val) (val >= 0x20 && val < 0x7F)

public:
TCidDecoder(TDesC8& aBuf, TBool aSDCard) {iBuf.Copy(aBuf); iSDCard = aSDCard;};

// constructor which tries to deduce whether card is an MMC or SD card
TCidDecoder(TDesC8& aBuf) 
{
iBuf.Copy(aBuf); 
iSDCard = ETrue;
if (HI_NIBBLE(iBuf[2]) != 0) 
iSDCard = EFalse;
TInt sdMonth = LO_NIBBLE(iBuf[1]);
if (sdMonth == 0 || sdMonth > 12)
iSDCard = EFalse;

// get this year so we can check for daft years
TTime now;
now.HomeTime();
TDateTime dateTime = now.DateTime();
TInt year = dateTime.Year();

TInt sdYear = HI_NIBBLE(iBuf[1]) + (LO_NIBBLE(iBuf[2]) << 4) + 2000;
if (sdYear > year) // stupid year ?
iSDCard = EFalse;

// // Oid should be ascii for SD and binary for MMC - but this isn't always true
// if (!IS_ASCII(iBuf[13]) || !IS_ASCII(iBuf[14]))
// iSDCard = EFalse;
};

void ManufacturingDate(TInt& aYear, TInt& aMonth) 
{
if (iSDCard)
{
aYear = HI_NIBBLE(iBuf[1]) + (LO_NIBBLE(iBuf[2]) << 4) + 2000; aMonth = LO_NIBBLE(iBuf[1]);
}
else
{
aYear = LO_NIBBLE(iBuf[1]) + 1997; aMonth = HI_NIBBLE(iBuf[1]);
}
}

TUint32 SerialNumber() 
{
if (iSDCard)
return( (iBuf[6]<<24) | (iBuf[5]<<16) | (iBuf[4]<<8) | (iBuf[3]) );
else
return( (iBuf[5]<<24) | (iBuf[4]<<16) | (iBuf[3]<<8) | (iBuf[2]) );
}

void ProductRevision(TInt& aMajor, TInt& aMinor) 
{
if (iSDCard)
{
aMajor = (iBuf[7] & 0xF0) >> 4; aMinor = iBuf[7] & 0x0F;
}
else
{
aMajor = (iBuf[6] & 0xF0) >> 4; aMinor = iBuf[6] & 0x0F;
}
}

void ProductName(TBuf<6>& aName) 
{
aName.SetLength(5); 
aName[0] = iBuf[12]; aName[1] = iBuf[11]; aName[2] = iBuf[10]; aName[3] = iBuf[9]; aName[4] = iBuf[8];
if (!iSDCard)
aName.Append(iBuf[7]);
};

void Oid(TBuf<2>& aOid) {aOid.SetLength(2); aOid[0] = iBuf[14]; aOid[1] = iBuf[13];};
TUint8 ManufactureID() {return iBuf[15];}
TBool IsSdCard() { return iSDCard;};

public:
TBuf8<16> iBuf;
TBool iSDCard// MMC card if false, SD card otherwise
};
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()