Untitled
unknown
plain_text
a year ago
5.3 kB
19
Indexable
#include "mainwindow.h"
#include <QApplication>
#include "ftd2xx.h"
#include "LibFT4222.h"
#include "FTChipID.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
void readChipID( FT_HANDLE ftHandle0, char* chipID)
{
unsigned short MTP_Data[200];
unsigned char AddressCounter = 0x00; // Variable for address value (8-bit)
unsigned short myData = 0x0000; // Used to hold current data value (16-bit)
unsigned short EndAddr = 0x7F; // End addr of MTP here
unsigned char ChipIdLowerAddress = 0x43; // Confidential address provided under NDA
unsigned char ChipIdUpperAddress = 0x44; // Confidential address provided under NDA
unsigned short ChipIdLowerData = 0;
unsigned short ChipIdUpperData = 0;
unsigned long Full_ChipId;
AddressCounter = 0x00;
// Read all addresses first of all and store their content in an array
// (makes it less obvious on USB bus which locations we read)
while(AddressCounter < EndAddr)
{
// Read the word from MTP
FT_ReadEE(ftHandle0, AddressCounter, &myData); // FT_ReadEE &myData // FT_WriteEE 0xFF
// printf("Memory location %x is... %x \n", AddressCounter, myData);
MTP_Data[AddressCounter] = myData;
AddressCounter ++;
}
// Now extract the content of the two chip ID locations
ChipIdLowerData = MTP_Data[ChipIdLowerAddress];
ChipIdUpperData = MTP_Data[ChipIdUpperAddress];
// Combine into a single value
Full_ChipId = 0;
Full_ChipId = ((ChipIdUpperData << 16) | ChipIdLowerData);
// Print the results for debug purposes
sprintf(chipID, "%X", (unsigned int)Full_ChipId);
}
int calculateMD5( char *input, char *output)
{
FILE *pipe;
char cmd[40];
snprintf(cmd, sizeof(cmd), "%s %s %s", "echo -n ", input, " | md5sum");
char line[36];
pipe = popen(cmd, "r");
if (pipe != NULL)
{
while (fgets(line, sizeof(line), pipe) != NULL) {
snprintf(output, sizeof(line), "%s", line);
output[strlen(output)-4] = 0;
break;
}
pclose(pipe);
}
return 1;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
unsigned char * pucUAdata;
DWORD dwUASize, dwUARead;
FT_HANDLE ftHandle0;
FT_STATUS ftStatus;
static FT_PROGRAM_DATA Data;
int iport = 0;
DWORD numDevs = 0;
ftStatus = FT_CreateDeviceInfoList(&numDevs);
if (ftStatus != FT_OK)
{
printf("FT_CreateDeviceInfoList failed (error code %d)\n",
(int)ftStatus);
return 1;
}
if (numDevs == 0)
{
printf("No devices connected.\n");
return 1;
}
iport = numDevs-1;
printf("opening port %d\n", iport);
ftStatus = FT_Open(iport, &ftHandle0);
if(ftStatus == FT_OK) {
printf("ftHandle0 = %p\n", ftHandle0);
}
else {
/*
This can fail if the ftdi_sio driver is loaded
use lsmod to check this and rmmod ftdi_sio to remove
also rmmod usbserial
*/
printf("FT_Open(%d) failed\n", iport);
return 1;
}
ftStatus = FT_EE_UASize(ftHandle0, &dwUASize);
if(ftStatus == FT_OK)
printf("dwUASize = %d\n", (int)dwUASize);
else {
printf("Could not read UA size\n");
FT_Close(ftHandle0);
return 1;
}
pucUAdata = (unsigned char *)malloc(dwUASize);
if(pucUAdata == NULL) {
printf("Out of resources\n");
FT_Close(ftHandle0);
return 1;
}
ftStatus = FT_EE_UARead(ftHandle0, pucUAdata, dwUASize, &dwUARead);
if(ftStatus == FT_OK) {
Data.Signature1 = 0x00000000;
Data.Signature2 = 0xffffffff;
Data.Manufacturer = (char *)malloc(256); /* E.g "FTDI" */
Data.ManufacturerId = (char *)malloc(256); /* E.g. "FT" */
Data.Description = (char *)malloc(256); /* E.g. "USB HS Serial Converter" */
Data.SerialNumber = (char *)malloc(256); /* E.g. "FT000001" if fixed, or NULL */
if (Data.Manufacturer == NULL ||
Data.ManufacturerId == NULL ||
Data.Description == NULL ||
Data.SerialNumber == NULL)
{
printf("Failed to allocate memory.\n");
}
ftStatus = FT_EE_Read(ftHandle0, &Data);
if(ftStatus == FT_OK) {
printf("FT_EE_Read succeeded.\n\n");
char *chipID = (char *)malloc(256);
readChipID(ftHandle0, chipID);
printf("ChipID string --> %s \n", chipID);
char *md5Result = (char *)malloc(256);
calculateMD5(chipID, md5Result); // Data.SerialNumber
const char *dnm = (char *)pucUAdata;
int result = strcmp(dnm, md5Result);
if( result == 0 ) // md5sum is correct
{
// Send MessageOverXML to UIS or VMS
printf("MD5SUM Correct.\n" );
}
}
}
else{
printf("could not read UA\n");
}
free(pucUAdata);
FT_Close(ftHandle0);
return 0;
return a.exec();
}
Editor is loading...
Leave a Comment