Exemple #1
0
/**********************************************************************
*
*
*   Module:		TCPServerPTUCallback
*
*   Abstract:   Invoked when PTU application sends data to target.
*
*   Globals:	NONE
*
*   Parameters:	aBuffer - pointer to the received array of byte(s)
*   			aNumBytes - number of bytes received
*   			aClientSocketId - client socket id (used to send response to)
*
*   IN:			NONE
*
* Return Value : NONE
*
*
*
* Functional Description :  This function is invoked when a message is received
* 							from a PTU client. It executes a simple state machine
* 							Once a SOM character is received, it echos the SOM
* 							character (SOM is based on machine "endianness"). It then
* 							waits for a message request from the PTU client. Once
* 							the entire message is received, it processes it just
* 							as the serial connection would.
*
*
*   Date & Author:	10/26/15 - Dave Smail
*   Description:    Initial Release which includes overhauled TCP/IP
*					implementation for PTU
*
*****************************************************************************/
static void TCPServerPTUCallback (char *aBuffer, int aNumBytes, int aClientSocketId)
{
	INT_8  sendSOM = THE_SOM;
	INT_16  bytesSent;
	UINT_16 i;
	PtuClientInfo *ptuClientInfoPtr = NULL;

	debugPrintf ("PTU Server Handler invoked: SocketId = %d, # Bytes in Msg = %d, Msg = %s\n", aClientSocketId, aNumBytes, aBuffer);

#ifdef TEST_ON_PC
	printf ("RX: ");
	for (i = 0; i < aNumBytes; i++)
	{
		printf ("%3u ", aBuffer[i] & 0xFF);
	}
	printf ("\n\r");
#endif

	i = 0;
	/* Get the pointer to the proper client socket info */
	while (i < MAX_CLIENTS_PER_SERVER)
	{
		if (aClientSocketId == mPtuClientInfo[i].socketId)
		{
			ptuClientInfoPtr = &mPtuClientInfo[i];
			break;
		}
		i++;
	}

	/* Coundn't find the client... exit function */
	if (ptuClientInfoPtr == NULL)
	{
		os_io_printf("TCP ERROR: could not retrieve ptu client info\n");
		return;
	}

	/* TODO need to change to support stream of data instead of packets (i.e. need to
	 * assume a byte at a time is received. So far no issues.
	 */
	switch (ptuClientInfoPtr->state)
	{
		case WAIT_FOR_SOM:
		default:
			if (aNumBytes == 1)
			{
				/* Verify SOM received */
				if (aBuffer[0] == SYNC_SOM)
				{
					debugPrintf ("SYNC_SOM received\n");
					bytesSent = 0;
					while (bytesSent != 1)
					{
						/*  Send a Start Of Message out to Ethernet port. */
						bytesSent = os_ip_send (aClientSocketId, (const char*)&sendSOM, 1, 0);
					}
					debugPrintf ("Sent THE_SOM; id = 3\n");
					ptuClientInfoPtr->rxBufferIndex = 0;
					ptuClientInfoPtr->state = WAIT_FOR_COMMAND;
				}
				else
				{
					os_io_printf ("TCP ERROR: SOM not received when expected\n");
				}
			}
			else
			{
				os_io_printf ("TCP ERROR: More bytes received than just SYNC_SOM\n");
			}
			break;

		case WAIT_FOR_COMMAND:
			/* Need to be able to handle a SyncSOM only just like the serial even though
			 * a message from the PC is expected */
			if (aNumBytes == 1)
			{
				/* Verify SOM received */
				if (aBuffer[0] == SYNC_SOM)
				{
					debugPrintf ("SYNC_SOM received when message packet expected\n");
					bytesSent = 0;
					while (bytesSent != 1)
					{
						/*  Send a Start Of Message out to Ethernet port. */
						bytesSent = os_ip_send (aClientSocketId, (const char*)&sendSOM, 1, 0);
					}
					debugPrintf ("Sent THE_SOM; id = 3\n");
					ptuClientInfoPtr->rxBufferIndex = 0;
				}
			}
			/* Get the PTU command packet */
			else if (TCPPtuGetDataPacket ( (Header_t *)&Request, aBuffer, aNumBytes, ptuClientInfoPtr->rxBufferIndex) == TCP_MSG_GOOD)
			{
				/*  Send a Start Of Message out to Ethernet port. */
				if (((Header_t *)&Request)->PacketType != TERMINATECONNECTION)
				{
					/* If a smart PTU packet has been received, set comm_type to TCP/IP	*/
					/* call Message_Manager to process the packet.		                */
					ComDevice = TCPIP;
					MessageManager ((Header_t *)&Request);
					debugPrintf ("Message Type = %d\n",((Header_t *)&Request)->PacketType);
				}
				else
				{
					debugPrintf ("TERMINATECONNECTION received from PC\n");
				}
				debugPrintf ("Entire command packet received from PC\n");
				ptuClientInfoPtr->state = WAIT_FOR_SOM;
			}
			else
			{
				/* wait for the entire packet; increment the buffer index by the number of bytes just received */
				ptuClientInfoPtr->rxBufferIndex += aNumBytes;
				debugPrintf ("Entire TCP packet not received; adjusting buffer index\n");
			}
			break;
	}
}
message_manager::suppress::suppress():
	m_owner(MessageManager())
{
	InterlockedIncrement(&m_owner.m_suppressions);
}
static LRESULT CALLBACK WndProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
	case WM_CLOSE:
		DestroyWindow(Hwnd);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_DEVICECHANGE:
		{
			//bool Arrival=false;
			switch(wParam)
			{
			case DBT_DEVICEARRIVAL:
				//Arrival=true;
			case DBT_DEVICEREMOVECOMPLETE:
				{

					auto Pbh = reinterpret_cast<PDEV_BROADCAST_HDR>(lParam);
					if(Pbh->dbch_devicetype==DBT_DEVTYP_VOLUME)
					{
						// currently we don't care what actually happened, "just a notification" is OK

						//auto Pdv=reinterpret_cast<PDEV_BROADCAST_VOLUME>(Pbh);
						//bool Media = Pdv->dbcv_flags & DBTF_MEDIA != 0;
						MessageManager().notify(update_devices);
					}
				}
				break;

			}
		}
		break;

	case WM_SETTINGCHANGE:
		if(lParam)
		{
			if (!StrCmp(reinterpret_cast<LPCWSTR>(lParam),L"Environment"))
			{
				if (Global->Opt->UpdateEnvironment) 
				{
					MessageManager().notify(update_environment);
				}
			}
			else if (!StrCmp(reinterpret_cast<LPCWSTR>(lParam),L"intl"))
			{
				MessageManager().notify(update_intl);
			}
		}
		break;

	case WM_POWERBROADCAST:
		switch(wParam)
		{
		case PBT_APMPOWERSTATUSCHANGE: // change status

		case PBT_POWERSETTINGCHANGE:   // change percent
			MessageManager().notify(update_power);
			break;
		// TODO:
		// PBT_APMSUSPEND & PBT_APMRESUMEAUTOMATIC handlers

		}

		break;

	}
	return DefWindowProc(Hwnd, Msg, wParam, lParam);
}