Esempio n. 1
0
bool BthPort::Connect() {
    int iResult;

    mSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    if (mSocket == INVALID_SOCKET) {
        DWORD dwError = WSAGetLastError();
        StartupStore(_T("... Bluetooth Port %u Unable to create socket, error=%lu%s"), GetPortIndex() + 1, dwError, NEWLINE); // 091117

        return false;
    }

    SOCKADDR_BTH sa;
    memset(&sa, 0, sizeof (sa));
    sa.addressFamily = AF_BTH;
    sa.serviceClassId = RFCOMM_PROTOCOL_UUID;
    sa.btAddr = StrToBTAddr(GetPortName());

    iResult = connect(mSocket, (SOCKADDR*) & sa, sizeof (sa));
    if (iResult == SOCKET_ERROR) {
        DWORD dwError = WSAGetLastError();
        StartupStore(_T("... Bluetooth Port %u <%s> Unable connect, error=%lu%s"), GetPortIndex() + 1, GetPortName(), dwError, NEWLINE); // 091117

        return false;
    }

    StartupStore(_T(". Bluetooth Port %u Connect <%s> OK%s"), GetPortIndex() + 1, GetPortName(), NEWLINE);

    return true;
}
Esempio n. 2
0
VOID ShowTcpTable()
{
    PMIB_TCPTABLE tcpTable;
    DWORD error, dwSize;
    DWORD i;
    CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN];
    CHAR RemoteIp[HOSTNAMELEN], RemotePort[PORTNAMELEN];
    CHAR Host[ADDRESSLEN];
    CHAR Remote[ADDRESSLEN];

    /* Get the table of TCP endpoints */
    dwSize = sizeof (MIB_TCPTABLE);
    /* Should also work when we get new connections between 2 GetTcpTable()
     * calls: */
    do
    {
        tcpTable = (PMIB_TCPTABLE) HeapAlloc(GetProcessHeap(), 0, dwSize);
        error = GetTcpTable(tcpTable, &dwSize, TRUE);
        if ( error != NO_ERROR )
            HeapFree(GetProcessHeap(), 0, tcpTable);
    }
    while  ( error == ERROR_INSUFFICIENT_BUFFER );

    if (error != NO_ERROR)
    {
        printf("Failed to snapshot TCP endpoints.\n");
        DoFormatMessage(error);
        exit(EXIT_FAILURE);
    }

    /* Dump the TCP table */
    for (i = 0; i < tcpTable->dwNumEntries; i++)
    {
        /* If we aren't showing all connections, only display established, close wait
         * and time wait. This is the default output for netstat */
        if (bDoShowAllCons || (tcpTable->table[i].dwState ==  MIB_TCP_STATE_ESTAB)
            || (tcpTable->table[i].dwState ==  MIB_TCP_STATE_CLOSE_WAIT)
            || (tcpTable->table[i].dwState ==  MIB_TCP_STATE_TIME_WAIT))
        {
            /* I've split this up so it's easier to follow */
            GetIpHostName(TRUE, tcpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN);
            GetPortName(tcpTable->table[i].dwLocalPort, "tcp", HostPort, PORTNAMELEN);
            GetIpHostName(FALSE, tcpTable->table[i].dwRemoteAddr, RemoteIp, HOSTNAMELEN);
            GetPortName(tcpTable->table[i].dwRemotePort, "tcp", RemotePort, PORTNAMELEN);

            sprintf(Host, "%s:%s", HostIp, HostPort);
            sprintf(Remote, "%s:%s", RemoteIp, RemotePort);

            _tprintf(_T("  %-6s %-22s %-22s %s\n"), _T("TCP"),
            Host, Remote, TcpState[tcpTable->table[i].dwState]);
        }
    }
    HeapFree(GetProcessHeap(), 0, tcpTable);
}
Esempio n. 3
0
/* virtual */ void
CFlowInspectorDefault::NotifyFlow(IFlowGraph * pGraph, const SFlowAddress from, const SFlowAddress to)
{
	static char msg[256];

	if (!m_bProcessing) 
		return;
	if (m_bPaused) 
		return;

	if (RunFilters(pGraph, from, to) == false)
		return;

	string name;
	TFlowRecord rec;
	GetGraphNameAndType (pGraph, name, rec.m_type);
	rec.m_from = from;
	rec.m_to = to;
	rec.m_pGraph = pGraph;
	rec.m_tstamp = m_currentTime;

	const TFlowInputData *data = pGraph->GetInputValue(to.node, to.port);
	if (0 != data) 
	{
		rec.m_data = *data;
	}
	string val;
	rec.m_data.GetValueWithConversion(val);

	_snprintf(msg, sizeof(msg)-1, "0x%p %s [%s:%s] -> [%s:%s] Val=%s",
		(const IFlowGraph*) rec.m_pGraph,
		name.c_str(),
		GetNodeName(rec.m_pGraph, rec.m_from).c_str(),
		GetPortName(rec.m_pGraph, rec.m_from),
		GetNodeName(rec.m_pGraph, rec.m_to).c_str(),
		GetPortName(rec.m_pGraph, rec.m_to), val.c_str());
	msg[sizeof(msg)-1] = '\0'; // safe terminate
	rec.m_message = msg;

	if (CFlowSystemCVars::Get().m_inspectorLog != 0)
	{
		CryLogAlways("[fgi] %s", msg);
	}

	m_curRecords.push_back(rec);
}
Esempio n. 4
0
bool Serial::Open()
{
	if (_file)
		return false;

	std::wstring path = L"\\\\.\\";
	path += GetPortName();
		
	HANDLE file = CreateFile(path.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
	if (!file || file == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox(L"CreateFile failed");
		return false;
	}
	DCB dcb;
	::memset(&dcb, 0, sizeof dcb);
	dcb.fBinary = true;
	dcb.BaudRate = UsbSpeed;
	dcb.ByteSize = 8;
	if (!SetCommState(file, &dcb))
	{
		AfxMessageBox(L"SetCommState failed");
		return false;
	}

	COMMTIMEOUTS timeouts;
	if (!GetCommTimeouts(file, &timeouts))
	{
		AfxMessageBox(L"GetCommTimeouts failed");
		return false;
	}

	//timeouts.ReadIntervalTimeout = 5;
	//timeouts.ReadTotalTimeoutConstant = 5;
	//timeouts.ReadTotalTimeoutMultiplier = 10;

	timeouts.ReadIntervalTimeout = 1;
	timeouts.ReadTotalTimeoutConstant = 1;
	timeouts.ReadTotalTimeoutMultiplier = 1;

	if (!SetCommTimeouts(file, &timeouts))
	{
		AfxMessageBox(L"SetCommTimeouts failed");
		return false;
	}

	_file = file;
	_abort = false;
	_thread = std::thread(&Serial::Go, this);

	return true;
}
Esempio n. 5
0
ResultVal<bool> Interface::SyncRequest() {
    u32* cmd_buff = Kernel::GetCommandBuffer();
    auto itr = m_functions.find(cmd_buff[0]);

    if (itr == m_functions.end() || itr->second.func == nullptr) {
        std::string function_name = (itr == m_functions.end())
                                        ? Common::StringFromFormat("0x%08X", cmd_buff[0])
                                        : itr->second.name;
        LOG_ERROR(
            Service, "unknown / unimplemented %s",
            MakeFunctionString(function_name.c_str(), GetPortName().c_str(), cmd_buff).c_str());

        // TODO(bunnei): Hack - ignore error
        cmd_buff[1] = 0;
        return MakeResult<bool>(false);
    }
    LOG_TRACE(Service, "%s",
              MakeFunctionString(itr->second.name, GetPortName().c_str(), cmd_buff).c_str());

    itr->second.func(this);

    return MakeResult<bool>(false); // TODO: Implement return from actual function
}
Esempio n. 6
0
bool ComPort::StartRxThread() {
    StopEvt.reset();


    // Create a read thread for reading data from the communication port.
    ReadThread.start(*this);
    ReadThread.setPriority(Poco::Thread::PRIO_NORMAL); //THREAD_PRIORITY_ABOVE_NORMAL

    if(!ReadThread.isRunning()) {
        // Could not create the read thread.
        StartupStore(_T(". ComPort %u <%s> Failed to start Rx Thread%s"), (unsigned)(GetPortIndex() + 1), GetPortName(), NEWLINE);

        // LKTOKEN  _@M761_ = "Unable to Start RX Thread on Port"
        StatusMessage(mbOk, TEXT("Error"), TEXT("%s %s"), MsgToken(761), GetPortName());
        //DWORD dwError = GetLastError();
        return false;
    }
    return true;
}
Esempio n. 7
0
VOID ShowUdpTable()
{
    PMIB_UDPTABLE udpTable;
    DWORD error, dwSize;
    DWORD i;
    CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN];
    CHAR Host[ADDRESSLEN];

    /* Get the table of UDP endpoints */
    dwSize = 0;
    error = GetUdpTable(NULL, &dwSize, TRUE);
    if (error != ERROR_INSUFFICIENT_BUFFER)
    {
        printf("Failed to snapshot UDP endpoints.\n");
        DoFormatMessage(error);
        exit(EXIT_FAILURE);
    }
    udpTable = (PMIB_UDPTABLE) HeapAlloc(GetProcessHeap(), 0, dwSize);
    error = GetUdpTable(udpTable, &dwSize, TRUE);
    if (error)
    {
        printf("Failed to snapshot UDP endpoints table.\n");
        DoFormatMessage(error);
        HeapFree(GetProcessHeap(), 0, udpTable);
        exit(EXIT_FAILURE);
    }

    /* Dump the UDP table */
    for (i = 0; i < udpTable->dwNumEntries; i++)
    {

        /* I've split this up so it's easier to follow */
        GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN);
        GetPortName(udpTable->table[i].dwLocalPort, "tcp", HostPort, PORTNAMELEN);

        sprintf(Host, "%s:%s", HostIp, HostPort);

        _tprintf(_T("  %-6s %-22s %-22s\n"), _T("UDP"), Host,  _T("*:*"));
    }

    HeapFree(GetProcessHeap(), 0, udpTable);
}
Esempio n. 8
0
bool GOrgueMidiRtInPort::Open(int channel_shift)
{
	Close();
	if (!m_port)
		return false;
	try
	{
		for (unsigned i = 0; i <  m_port->getPortCount(); i++)
		{
			if (m_Name == m_Prefix + wxString::FromAscii(m_port->getPortName(i).c_str()))
			{
				m_port->openPort(i, (const char*)GetPortName().fn_str());
				m_IsActive = true;
				break;
			}
		}
	}
	catch (RtMidiError &e)
	{
		wxString error = wxString::FromAscii(e.getMessage().c_str());
		wxLogError(_("RtMidi error: %s"), error.c_str());
	}
	return GOrgueMidiInPort::Open(channel_shift);
}
Esempio n. 9
0
bool GpsIdPort::Initialize() {
    _hLoc = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    _hState = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    _hGPS = ::GPSOpenDevice(_hLoc, _hState, NULL, 0);
    if (0 == _hGPS) {
        StartupStore(_T("Unable to Open GPS Intermediate driver %s"), NEWLINE);    
        return false;
    }
    SetPortStatus(CPS_OPENOK);

    GPS_DEVICE dev = {0};
    GPSResetData(dev);
    GPSGetDeviceState(&dev);
    StartupStore(_T("GPSID : DeviceState: %lX, ServiceState: %lX%s"), dev.dwDeviceState, dev.dwServiceState, NEWLINE);
//    StartupStore(_T("GPSID : LastDataTime: %s%s"), dev.ftLastDataReceived, NEWLINE);
    StartupStore(_T("GPSID : DrvPrefix; %s%s"), dev.szGPSDriverPrefix, NEWLINE);
    StartupStore(_T("GPSID : MxPrefix %s%s"), dev.szGPSMultiplexPrefix, NEWLINE);
    StartupStore(_T("GPSID : Name:%s%s"), dev.szGPSFriendlyName, NEWLINE);

    if (!StartRxThread()) {
        StartupStore(_T(". ComPort %u <%s> Failed to start Rx Thread%s"), GetPortIndex() + 1, GetPortName(), NEWLINE);
        return false;
    }
    return true;
}
Esempio n. 10
0
bool SerialPort::Initialize() {
    TCHAR sysPortName[20];
    DCB PortDCB;

#if (WINDOWSPC>0)
    // Do not use COMn , use \\.\COMnn  on PC version
    _stprintf(sysPortName, _T("\\\\.\\%s"), GetPortName());
#else
    // Do not use COMn , use COMn:  on WinCE version
    _stprintf(sysPortName, _T("%s:"), GetPortName());
#endif
    StartupStore(_T(". ComPort %u Initialize <%s> speed=%u bit=%u %s"),GetPortIndex()+1,GetPortName(),_dwPortSpeed,8-_dwPortBit,NEWLINE);

    hPort = CreateFile(sysPortName, // Pointer to the name of the port
            GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode
            0, // Share mode
            NULL, // Pointer to the security attribute
            OPEN_EXISTING, // How to open the serial port
            FILE_ATTRIBUTE_NORMAL, // Port attributes
            NULL); // Handle to port with attribute
    // to copy

    if (hPort == INVALID_HANDLE_VALUE) {
        DWORD dwError = GetLastError();
        StartupStore(_T("... ComPort %u Init failed, error=%u%s"), GetPortIndex() + 1, dwError, NEWLINE); // 091117
        StatusMessage(MB_OK, NULL, TEXT("%s %s"), gettext(TEXT("_@M762_")), GetPortName());

        goto failed;
    }
    StartupStore(_T(". ComPort %u  <%s> is now open%s"), GetPortIndex() + 1, GetPortName(), NEWLINE);


    PortDCB.DCBlength = sizeof (DCB);
    // Get the default port setting information.
    if (GetCommState(hPort, &PortDCB)==0) {
    	StartupStore(_T("... ComPort %u GetCommState failed, error=%u%s"),GetPortIndex()+1,GetLastError(),NEWLINE);
        // @M759 = Unable to Change Settings on Port
        StatusMessage(MB_OK|MB_ICONINFORMATION, NULL, TEXT("%s %s"), gettext(TEXT("_@M759_")), GetPortName());
        goto failed;
    }
    // Change the DCB structure settings.
    PortDCB.BaudRate = _dwPortSpeed; // Current baud 
    PortDCB.fBinary = TRUE; // Binary mode; no EOF check 
    PortDCB.fParity = TRUE; // Enable parity checking  
    PortDCB.fOutxCtsFlow = FALSE; // CTS output flow control: when TRUE, and CTS off, output suspended
    PortDCB.fOutxDsrFlow = FALSE; // DSR output flow control 
    PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
    
    PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity 
    PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx 
    PortDCB.fOutX = FALSE; // No XON/XOFF out flow control 
    PortDCB.fInX = FALSE; // No XON/XOFF in flow control 
    PortDCB.fErrorChar = FALSE; // Disable error replacement 
    PortDCB.fNull = FALSE; // Disable null removal
    PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control 

    PortDCB.fAbortOnError = FALSE;
    switch (_dwPortBit) {
        case bit7E1:
            PortDCB.ByteSize = 7;
            PortDCB.Parity = EVENPARITY;
            break;
        case bit8N1:
        default:
            PortDCB.ByteSize = 8;
            PortDCB.Parity = NOPARITY;
            break;
    }

    PortDCB.StopBits = ONESTOPBIT;
    PortDCB.EvtChar = '\n'; // wait for end of line, RXFLAG should detect it

    if (!SetCommState(hPort, &PortDCB)) {
        DWORD dwError = GetLastError();
        StartupStore(_T("... ComPort %u Init <%s> change setting FAILED, error=%u%s"), GetPortIndex() + 1, GetPortName(), dwError, NEWLINE); // 091117
		// @M759 = Unable to Change Settings on Port
    	StatusMessage(MB_OK, TEXT("Error"), TEXT("%s %s"), gettext(TEXT("_@M759_")), GetPortName());

        goto failed;
    }

    if (SetRxTimeout(RXTIMEOUT) == -1) {
        DWORD dwError = GetLastError();
        StartupStore(_T("... ComPort %u Init <%s> change TimeOut FAILED, error=%u%s"), GetPortIndex() + 1, GetPortName(), dwError, NEWLINE); // 091117
        // LKTOKEN  _@M760_ = "Unable to Set Serial Port Timers" 
        StatusMessage(MB_OK, TEXT("Error"), TEXT("%s %s"), gettext(TEXT("_@M760_")), GetPortName());        

        goto failed;
    }

    SetupComm(hPort, 1024, 1024);

    // Direct the port to perform extended functions SETDTR and SETRTS
    // SETDTR: Sends the DTR (data-terminal-ready) signal.
    // SETRTS: Sends the RTS (request-to-send) signal. 
    EscapeCommFunction(hPort, SETDTR);
    EscapeCommFunction(hPort, SETRTS);

    if(!ComPort::Initialize()) {
        // no need to log failed of StartRxThread it's already made in ComPort::Initialize();
        goto failed;
    }

    StartupStore(_T(". ComPort %u Init <%s> end OK%s"), GetPortIndex() + 1, GetPortName(), NEWLINE);
    return true;
        
failed:
    if(hPort != INVALID_HANDLE_VALUE) {
        if (!CloseHandle(hPort)) {
            StartupStore(_T("... ComPort %u Init <%s> close failed, error=%u%s"),GetPortIndex() + 1, GetPortName(),NEWLINE);
        } else {
            StartupStore(_T("... ComPort %u Init <%s> closed%s"),GetPortIndex() + 1, GetPortName(),NEWLINE);
        }
        hPort = INVALID_HANDLE_VALUE;

#if (WINDOWSPC>0) || NEWCOMM // 091206
        Sleep(2000); // needed for windows bug
#endif
#if !(WINDOWSPC>0)
        if (_PollingMode) Sleep(2000);
#endif
    }
    return false;
}
Esempio n. 11
0
bool ComPort::Initialize() {
    if(!StartRxThread()) {
        StartupStore(_T(". ComPort %u <%s> Failed to start Rx Thread%s"), (unsigned)(GetPortIndex() + 1), GetPortName(), NEWLINE);
        return false;
    }
    return true;
}