telldus/driver/libtelldus-core/win/Device.cpp

117 lines
2.7 KiB
C++

#include "../Device.h"
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
#include "../ftd2xx.h"
int getDongleIndex();
using namespace TelldusCore;
/*
* Send message to the USB dongle
*/
int Device::send(const std::string &strMessage){
std::string msgBack;
ULONG bytesWritten, bytesRead;
char *tempMessage;
int intDongleIndex;
char in;
FT_STATUS ftStatus = FT_OK;
FT_HANDLE fthHandle = 0;
intDongleIndex = getDongleIndex();
if (intDongleIndex < 0) {
return TELLSTICK_ERROR_NOT_FOUND;
}
ftStatus = FT_Open(intDongleIndex, &fthHandle);
ftStatus = FT_SetBaudRate(fthHandle, 9600); //always 9600
FT_SetTimeouts(fthHandle,5000,0);
tempMessage = (char *)malloc(sizeof(char) * (strMessage.size()));
for(unsigned int i = 0; i < strMessage.size(); ++i) {
tempMessage[i] = strMessage[i];
}
ftStatus = FT_Write(fthHandle, tempMessage, (DWORD)strMessage.length(), &bytesWritten);
free(tempMessage);
bool c = true;
while(c) {
ftStatus = FT_Read(fthHandle,&in,1,&bytesRead);
if (ftStatus == FT_OK) {
if (bytesRead == 1) {
msgBack.append(1, in);
if (in == '\n') {
break;
}
} else { //Timeout
c = false;
}
} else { //Error
c = false;
}
}
ftStatus = FT_Close(fthHandle);
if (!c) {
return TELLSTICK_ERROR_COMMUNICATION;
}
return TELLSTICK_SUCCESS;
}
/*
* Get the device index of the Telldus dongle (the first one if many are connected)
* -1 if no such device exists
*/
int getDongleIndex(){
int intReturn = -1;
FT_HANDLE fthHandle = 0;
FT_STATUS ftStatus = FT_OK;
try{
DWORD dwNumberOfDevices = 0;
#ifndef _WINDOWS
FT_SetVIDPID(0x1781, 0x0C30);
#endif
ftStatus = FT_CreateDeviceInfoList(&dwNumberOfDevices);
if (ftStatus == FT_OK) {
for (int i = 0; i < (int)dwNumberOfDevices; i++) {
FT_PROGRAM_DATA pData;
char ManufacturerBuf[32];
char ManufacturerIdBuf[16];
char DescriptionBuf[64];
char SerialNumberBuf[16];
pData.Signature1 = 0x00000000;
pData.Signature2 = 0xffffffff;
pData.Version = 0x00000002; // EEPROM structure with FT232R extensions
pData.Manufacturer = ManufacturerBuf;
pData.ManufacturerId = ManufacturerIdBuf;
pData.Description = DescriptionBuf;
pData.SerialNumber = SerialNumberBuf;
ftStatus = FT_Open(i, &fthHandle);
ftStatus = FT_EE_Read(fthHandle, &pData);
if(ftStatus == FT_OK){
if(pData.VendorId == 6017 && pData.ProductId == 3120){
intReturn = i;
ftStatus = FT_Close(fthHandle);
break;
}
}
ftStatus = FT_Close(fthHandle);
}
}
}
catch(...){
throw;
}
return intReturn;
}