示例#1
0
int GSkipBaseDevice::OSClearCmdRespPacketQueue()
{
    int nReturn = 0;

    if (LockDevice(1) && IsOKToUse())
    {
        nReturn = local_ClearPacketQueue(this, kCommandPipe);
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nReturn;
}
示例#2
0
int GSkipBaseDevice::OSCmdRespPacketsAvailable(void)
{
    int nReturn = 0;

    if (LockDevice(1) && IsOKToUse())
    {
        nReturn = local_PacketsAvailable(this, kCommandPipe);
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nReturn;
}
示例#3
0
int GSkipBaseDevice::OSClearMeasurementPacketQueue()
{
    int nReturn = 0;

    if (LockDevice(1) && IsOKToUse())
    {
        nReturn = local_ClearPacketQueue(this, kMeasurementPipe);
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nReturn;
}
示例#4
0
int GSkipBaseDevice::OSClose(void)
{
    if (LockDevice(1) && IsOKToUse())
    {
        TUSBBulkDevice usbDevice = static_cast<TUSBBulkDevice>(GetOSData());
        if ((usbDevice != NULL) && IsOpen())
            VST_CloseUSBPort((VST_USBBulkDevice *)usbDevice);
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return kResponse_OK;
}
示例#5
0
int GSkipBaseDevice::OSReadCmdRespPackets(void * pBuffer, int * pIONumPackets, int nBufferSizeInPackets)
{
    int nReturn = 0;

    if (LockDevice(1) && IsOKToUse())
    {
        nReturn = local_ReadPackets(this, pBuffer, pIONumPackets, nBufferSizeInPackets, kCommandPipe);
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nReturn;
}
示例#6
0
void GSkipBaseDevice::OSDestroy(void)
{
    if (LockDevice(1) && IsOKToUse())
    {
        TUSBBulkDevice usbDevice = static_cast<TUSBBulkDevice>(GetOSData());
        if (usbDevice != NULL)
        {
            VST_CloseUSBPort((VST_USBBulkDevice *)usbDevice);
            VST_ReleaseUSBBulkDevice((VST_USBBulkDevice *)usbDevice);
        }
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);
}
示例#7
0
int GSkipBaseDevice::OSMeasurementPacketsAvailable(unsigned char *pNumMeasurementsInLastPacket)
{
    int nReturn = 0;

    if (LockDevice(1) && IsOKToUse())
    {
        nReturn = local_PacketsAvailable(this, kMeasurementPipe);
        if (pNumMeasurementsInLastPacket != NULL)
            *pNumMeasurementsInLastPacket = local_NumLastMeasurements(this);
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nReturn;
}
示例#8
0
int GSkipBaseDevice::OSClearIO(void)
{
    int nResult = kResponse_Error;

    if (LockDevice(1) && IsOKToUse())
    {
        nResult = OSClearMeasurementPacketQueue();
        if (nResult != kResponse_Error)
            nResult = GSkipBaseDevice::OSClearCmdRespPacketQueue();
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nResult;
}
示例#9
0
int GSkipBaseDevice::OSOpen(GPortRef * /*pPortRef*/)
{
    int nResult = kResponse_Error;

    if (LockDevice(1) && IsOKToUse())
    {
        TUSBBulkDevice usbDevice = static_cast<TUSBBulkDevice>(GetOSData());

        // port may be NULL if initialization failed
        if (usbDevice == NULL)
            return kResponse_Error;

        int err = VST_OpenUSBPortForIO((VST_USBBulkDevice *)usbDevice);
        if (err == (int)kCFM_IOExclusiveAccess)
        {
            // If another app (including Classic) has the device we
            // can simply retry a few times.  The driver should be able to get a lock
            // on the device at some point.
            int nTry = 0;
            while ((err == (int)kCFM_IOExclusiveAccess) && (nTry < 4))
            {
                err = VST_OpenUSBPortForIO((VST_USBBulkDevice *)usbDevice);
                nTry++;
            }
        }

        if (err == 0)
            nResult = kResponse_OK;
        else
        {
            cppsstream ssErr;
            ssErr << "GUSBDirectTempDevice:OSOpen -- Error: " << err;
            GSTD_LOG(ssErr.str());
        }

        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nResult;
}
示例#10
0
int GSkipBaseDevice::OSWriteCmdPackets(void * pBuffer, int nNumPackets)
{
    int nResult = kResponse_Error;
    UInt32 nIONumBytes = nNumPackets * sizeof(GSkipPacket);

    if (LockDevice(1) && IsOKToUse())
    {
        TUSBBulkDevice usbDevice = static_cast<TUSBBulkDevice>(GetOSData());
        if (usbDevice != NULL)
        {
            int err = VST_WriteBytes((VST_USBBulkDevice *)usbDevice, pBuffer, nIONumBytes);
            if (err == 0)
                nResult = kResponse_OK;
        }
        UnlockDevice();
    }
    else
        GSTD_ASSERT(0);

    return nResult;
}
示例#11
0
StringVector GSkipBaseDevice::OSGetAvailableDevicesOfType(int nVendorID, int nProductID)
{
    StringVector vPortNames;
    VST_USBSpecArrayRef specArray;
    VST_GetUSBSpecsForDevice(nVendorID, nProductID, true, &specArray);
    if (specArray)
    {
        int nNumUSBPorts = CFArrayGetCount(specArray);
        for (int i = 0; i < nNumUSBPorts; i++)
        {
            VST_USBSpec *pUSBSpec = (VST_USBSpec *)CFArrayGetValueAtIndex(specArray, (CFIndex)i);
            GSTD_ASSERT(pUSBSpec != NULL);

            // cppsstream (aka std::stringstream) doesn't work here when optimizations are turned on!
            // BUT! : std::strstream ( the alternative that was tried instead)'s str() function returns a string w/o null termination, so
            // instantiating a cppstring with it gives undesired results
            // So, for now, we'll use cppsstream, and make sure that optimisations are OFF. blech
            //std::strstream ss;

            cppsstream ss;

            UInt32 nLocationID;
            VST_GetUSBSpecLocation(pUSBSpec, &nLocationID);

            ss << "0x" << hex;
            ss << nLocationID;
            ss << " (USB)";	// Legacy support: LP uses this suffix to distinguish the "location" from serial ports.

// debug
//			cppstring temp;
//			temp = ss.str();

            vPortNames.push_back(ss.str());
        }
    }
    VST_ReleaseUSBSpecArray(specArray);

    return vPortNames;
}
示例#12
0
long GSkipDevice::SendInitCmdAndGetResponse(
	void *pParams,		//[in] ptr to cmd specific parameter block, may be NULL.
	long nParamBytes,	//[in] # of bytes in (*pParams).
	void *pRespBuf,		//[out] ptr to destination buffer, may be NULL.
	long *pnRespBytes,  //[in, out] size of of dest buffer on input, size of response on output, may be NULL if pRespBuf is NULL.
	long nTimeoutMs /* = 1000 */,//[in] # of milliseconds to wait before giving up.
	bool *pExitFlag /* = NULL */)//[in] ptr to flag that another thread can set to force early exit. 
						//		THIS FLAG MUST BE FALSE FOR THIS ROUTINE TO RUN.
						//		Ignore this if NULL.
{
	long nResult = kResponse_Error;
	unsigned char initStatus;
	long initStatusLength = sizeof(initStatus);

	if (LockDevice(1) && IsOKToUse())
	{
		nResult = kResponse_OK; 
		long maxNumRetries = (nTimeoutMs + SKIP_TIMEOUT_MS_CMD_ID_INIT_WITH_BUSY_STATUS - 1)/SKIP_TIMEOUT_MS_CMD_ID_INIT_WITH_BUSY_STATUS;
		if (maxNumRetries > 1)
			nTimeoutMs = SKIP_TIMEOUT_MS_CMD_ID_INIT_WITH_BUSY_STATUS;
		bool bSuccess = false;
		long numRetries;
		GSkipInitParams initParams;
		initParams.reportErrorWhilePoweringUpFlag = 1;
		if (pParams)
			GSTD_ASSERT(nParamBytes == sizeof(initParams));
		else
		{
			pParams = &initParams;
			nParamBytes = sizeof(initParams);
		}

		//Send multiple init commands until the timeout is reached because Skip may ignore commands sent before it is done
		//powering up.
		for (numRetries = 0; (numRetries < maxNumRetries) && (kResponse_OK == nResult) && (!bSuccess); numRetries++)
		{
			nResult = SendCmd(SKIP_CMD_ID_INIT,	pParams, nParamBytes);

			if (kResponse_OK == nResult)
			{
				unsigned int nTimeCmdSent = GUtils::OSGetTimeStamp();

				initStatusLength = sizeof(initStatus);
				initStatus = SKIP_STATUS_CMD_NOT_SUPPORTED;
				if (kResponse_OK == GetInitCmdResponse(&initStatus, &initStatusLength, nTimeoutMs, pExitFlag))
					bSuccess = true;
				else if ((sizeof(initStatus) == initStatusLength) && (SKIP_STATUS_ERROR_SLAVE_POWERUP_INIT == initStatus))
				{
					unsigned int nTimeNow = GUtils::OSGetTimeStamp();
					unsigned int nElapsedTimeMs = nTimeNow - nTimeCmdSent;
					if (nElapsedTimeMs < nTimeoutMs)
						GUtils::Sleep(nTimeoutMs - nElapsedTimeMs);
				}
			}
			else
				initStatusLength = 0;
		}

		if (!bSuccess)
			nResult = kResponse_Error;

		UnlockDevice();
	}
	else
		GSTD_ASSERT(0);	// Can't use this device -- some other thread has it open!

	if (pRespBuf && pnRespBytes)
	{
		if (((*pnRespBytes) >= initStatusLength) && (initStatusLength > 0))
		{
			(*pnRespBytes) = initStatusLength;
			memcpy(pRespBuf, &initStatus, initStatusLength);
		}
		else
			(*pnRespBytes) = 0;
	}

	return nResult;
}
int GThread::Main(void *pGThreadObject) // pointer to this GThread object
{ // "main" routine for this thread
	GThread * pThread = static_cast<GThread *> (pGThreadObject);
	int nResult = 0;
	
	if (pThread != NULL)
	{
		StdThreadFunctionPtr pStdFunction = pThread->GetThreadFunction();
		StdThreadFunctionPtr pStopFunction = pThread->GetStopFunction();
		StdThreadFunctionPtr pStartFunction = pThread->GetStartFunction();
		StdThreadFunctionPtr pLockFunction = pThread->GetLockFunction();
		StdThreadFunctionPtr pUnlockFunction = pThread->GetUnlockFunction();
		void * pParam = pThread->GetThreadParam();

		if ((pLockFunction == NULL) || pLockFunction(pParam))
		{
			if (pStdFunction != NULL)
			{
				do
				{
					// Test for thread death before calling function (brain and such may not exist during shutdown)
					if (! pThread->IsOneShot() && pThread->IsKillThread() )	
						break;
					
					// Call SetStop(true) if you need the stop function to execute. It is seperate from OSStopThread.
					if (pThread->IsStop())
					{
						if (pStopFunction != NULL)
							pStopFunction(pParam);
						pThread->SetStop(false);
					}

					// Call SetStart(true) if you need the start function to execute. It is seperate from OSStartThread.
					if (pThread->IsStart())
					{
						if (pStartFunction != NULL)
							pStartFunction(pParam);
						pThread->SetStart(false);
					}

					// Call worker thread function
					if (pStdFunction != NULL)
						nResult = (int) pStdFunction(pParam);
					if (nResult == kResponse_Error || pThread->IsOneShot())	// exit thread on error
						break;

					// Let other threads have a crack at the CPU
					GThread::OSYield();
					GUtils::Sleep(30);

				} while (true);	// Loop until thread death flag is set, or worker function returns break value
			}

			if ((pUnlockFunction != NULL) && !pUnlockFunction(pParam))
				GSTD_ASSERT(0);
		}
		else
			GSTD_ASSERT(0);
			
		pThread->SetThreadAlive(false);
	}

	return nResult;
}