Beispiel #1
0
int __cdecl main(int argc, char **argv)
{
	WORD 			wVersionRequested = 0x202;
	WSADATA 		wsaData;
	SOCKET 			LocalSocket = INVALID_SOCKET;
	SOCKADDR_BTH    SockAddrBthServer;
	char			*Faked_RemoteBTName = "faked_btdevice";
	char			*RemoteBTName = "btdevice";
	char 			*sendbuf = "Hello from DLL\n";
	int 			iResult;

	// =========================================
	// Initialize Winsock
	// =========================================
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		printf("[BTClient] WSAStartup failed with error: %d\n", iResult);
		return 1;
	}
	printf("[BTClient] Initialized Winsock\n");

	// =========================================
	// Look for server address using Name
	// =========================================
	printf("[BTClient] calling NameToBthAddr with %s\n", RemoteBTName);
	iResult = NameToBthAddr(RemoteBTName, &SockAddrBthServer);
	if (iResult == CXN_ERROR) {
		printf("[BTClient] NameToBthAddr failed\n");
		WSACleanup();
		return 1;
	}
	else {
		printf("[BTClient] %s's MAC is 0x%llX \n", RemoteBTName, SockAddrBthServer.btAddr);
	}


	// After the btAddr field is set (found using NameToBthAddr function),
	// set the other parameters
	SockAddrBthServer.addressFamily = AF_BTH;
	SockAddrBthServer.serviceClassId = SerialPortServiceClass_UUID;
	SockAddrBthServer.port = 0; // was BT_PORT_ANY before name lookup

	// =========================================
	// Create a SOCKET for connecting to server
	// =========================================
	LocalSocket = socket(AF_BTH,				//ai_family
		SOCK_STREAM, 		//ai_socktype
		BTHPROTO_RFCOMM);	//ai_protocol
	if (LocalSocket == INVALID_SOCKET) {
		printf("[BTClient] socket failed with error: %ld\n", WSAGetLastError());
		WSACleanup();
		return 1;
	}
	printf("[BTClient] Created local socket = %d\n", LocalSocket);
	// =========================================
	// Connect to server
	// =========================================
	printf("[BTClient] Attemping to connect\n");
	iResult = connect(LocalSocket, (struct sockaddr *) &SockAddrBthServer, sizeof(SOCKADDR_BTH));
	if (iResult == SOCKET_ERROR) {
		printf("[BTClient] Connect failed with error: %d\n", WSAGetLastError());
		closesocket(LocalSocket);
		return 1;
	}
	printf("[BTClient] Connected to server. Sending message to server\n");

	// =========================================
	// Send an initial buffer
	// =========================================
	iResult = send(LocalSocket, sendbuf, (int)strlen(sendbuf), 0);
	if (iResult == SOCKET_ERROR) {
		printf("[BTClient] send failed with error: %d\n", WSAGetLastError());
		closesocket(LocalSocket);
		WSACleanup();
		return 1;
	}
	printf("[BTClient] Message to server was sent\n");

	// =========================================
	// cleanup
	// =========================================
	printf("[BTClient] Cleaning up\n");
	closesocket(LocalSocket);
	WSACleanup();

	return 0;
}
int main(int argc, char *argv[])
{
	ULONG      ulRetCode = 0;
	WSADATA    WSAData = { 0 };
	ULONGLONG  ululRemoteBthAddr = 0;

	//ShowCmdLineHelp();
	// Parse the command line
	if ((ulRetCode = ParseCmdLine(argc, argv)) == 0)
	{
		// Ask for Winsock version 2.2.
		if ((ulRetCode = WSAStartup(MAKEWORD(2, 2), &WSAData)) != 0)
		{
			printf("-FATAL- | Unable to initialize Winsock version 2.2\n");
			goto CleanupAndExit;
		}
		else
			printf("WSAStartup() is OK!\n");

		if (g_szRemoteName[0] != '\0')
		{
			printf("Running in the client mode...\n");
			// Get address from name of the remote device and run the application in client mode
			if ((ulRetCode = NameToBthAddr(g_szRemoteName, (BTH_ADDR *)&ululRemoteBthAddr)) != 0)
			{
				printf("-FATAL- | Unable to get address of the remote radio having name %s\n", g_szRemoteName);
				goto CleanupAndExit;
			}

			ulRetCode = RunClientMode(ululRemoteBthAddr, g_ulMaxCxnCycles);
		}
		else if (g_szRemoteAddr[0] != '\0')
		{
			printf("Running in the client mode...\n");
			// Get address from formatted address-string of the remote device and run the application in client mode
			//  should be calling the WSAStringToAddress()
			if (0 != (ulRetCode = AddrStringToBtAddr(g_szRemoteAddr, (BTH_ADDR *)&ululRemoteBthAddr)))
			{
				printf("-FATAL- | Unable to get address of the remote radio having formatted address-string %s\n", g_szRemoteAddr);
				goto CleanupAndExit;
			}

			ulRetCode = RunClientMode(ululRemoteBthAddr, g_ulMaxCxnCycles);
		}
		else
		{
			// No remote name/address specified.  Run the application in server mode
			printf("No remote name/address specified, running the server mode...\n");
			ulRetCode = RunServerMode(g_ulMaxCxnCycles);
		}
	}
	else if (ulRetCode == 1)
	{
		// Command line syntax error.  Display cmd line help
		ShowCmdLineHelp();
	}
	else
	{
		printf("-FATAL- | Error in parsing the command line!\n");
	}

CleanupAndExit:
	return (int)(ulRetCode);
}