Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
i need to send an event to the server...
the event which i hav to send is...
d->asyncEvent.printOn(std::cout);
of the prog given below..

#include "Mouse3D.h"
#include "DevicesManager.h"
#include"Event.h"
#include <iostream>
using namespace hid;

namespace connexion_3d
{
const UInt32 Usage::MULTIAXIS_CONTROLLER_USAGE = 0x08;
const UInt32 Usage::PRIMARY_USAGE_PAGE = 0x01;
const UInt32 Usage::USAGE_PAGE_BUTTON = 0x09;
const UInt32 Usage::USAGE_X = 0x30;
const UInt32 Usage::USAGE_Y = 0x31;
const UInt32 Usage::USAGE_Z = 0x32;
const UInt32 Usage::USAGE_RX = 0x33;
const UInt32 Usage::USAGE_RY = 0x34;
const UInt32 Usage::USAGE_RZ = 0x35;
const UInt32 Usage::USAGE_PAGE_LED = 0x08;
const UInt32 Usage::USAGE_GENERIC_INDICATOR = 0x4B;

Usage::Usage():
txMin(0), txMax(0), rxMin(0), rxMax(0),
tyMin(0), tyMax(0), ryMin(0), ryMax(0),
tzMin(0), tzMax(0), rzMin(0), rzMax(0),
buttonsNumber(0)
{}




const unsigned int Mouse3D::QUEUE_SZ = 256;
const unsigned char Mouse3D::LED_ON = 0x01;
const unsigned char Mouse3D::LED_OFF = 0x00;
Mouse3D::Mouse3D(const IOHIDDeviceRef& _device):device(_device),usages(), queue(HIDDevice::makeQueue(_device, QUEUE_SZ)), valid(false), ledElement(NULL), isOpen(false), asyncEvent()
{
setup();
}

Mouse3D::~Mouse3D()
{
releaseQueue();
}

void Mouse3D:: releaseQueue()
{
if (queue)
{
IOHIDQueueStop(queue);
CFRelease(queue);
queue = NULL;
}
}

void Mouse3D::setup()
{
if (device && queue)
{
if (setupElements())
{
asyncEvent.buttons.resize(usages.buttonsNumber);
IOHIDQueueStart(queue);
valid = true;
}
}
}

void Mouse3D::open()
{
isOpen = true;
ledOn();
}

void Mouse3D::close()
{
isOpen = false;
ledOff();
}

unsigned short Mouse3D::buttonsNumber() const
{
return usages.buttonsNumber;
}

const Usage& Mouse3D::usage() const
{
return usages;
}

bool Mouse3D::ledOn() const
{
return writeOnLed(LED_ON);
}

bool Mouse3D::ledOff() const
{
return writeOnLed(LED_OFF);
}

bool Mouse3D::writeOnLed(unsigned char v) const
{
if (ledElement)
{
IOHIDValueRef vref = IOHIDValueCreateWithIntegerValue( kCFAllocatorDefault, ledElement, 0, v );
if ( vref )
{
IOReturn tIOReturn = IOHIDDeviceSetValue(device, ledElement, vref );
CFRelease( vref );
return kIOReturnSuccess == tIOReturn;
}
}
return false;
}

bool Mouse3D::setupElements()
{
CFArrayRef elements = IOHIDDeviceCopyMatchingElements( device, NULL, 0 );
if ( elements )
{
usages.buttonsNumber = 0;
CFIndex idx, cnt = CFArrayGetCount( elements );
for ( idx = 0; idx < cnt; idx++ )
{
IOHIDElementRef eref = ( IOHIDElementRef ) CFArrayGetValueAtIndex( elements, idx );
if ( eref )
{
if (!HIDIsValidElement(eref)) continue;

uint32_t usagePage = IOHIDElementGetUsagePage( eref );
uint32_t usage = IOHIDElementGetUsage( eref );

IOHIDElementType eleType = IOHIDElementGetType( eref );
if ( eleType == kIOHIDElementTypeOutput )
{
if (usagePage== Usage::USAGE_PAGE_LED && usage == Usage::USAGE_GENERIC_INDICATOR)
ledElement = eref;
}

if ( eleType > kIOHIDElementTypeInput_ScanCodes ) continue; // skip non-input element types


if ( !usagePage || !usage ) continue;
if ( -1 == usage ) continue;

//HIDDumpElementInfo(eref);
switch (usagePage)
{
case Usage::PRIMARY_USAGE_PAGE:
switch(usage)
{
case Usage::USAGE_X:
IOHIDQueueAddElement(queue, eref);
usages.txMin = IOHIDElementGetLogicalMin( eref );
usages.txMax = IOHIDElementGetLogicalMax( eref );
break;

case Usage::USAGE_Y:
IOHIDQueueAddElement(queue, eref);
usages.tyMin = IOHIDElementGetLogicalMin( eref );
usages.tyMax = IOHIDElementGetLogicalMax( eref );
break;

case Usage::USAGE_Z:
IOHIDQueueAddElement(queue, eref);
usages.tzMin = IOHIDElementGetLogicalMin( eref );
usages.tzMax = IOHIDElementGetLogicalMax( eref );
break;

case Usage::USAGE_RX:
IOHIDQueueAddElement(queue, eref);
usages.rxMin = IOHIDElementGetLogicalMin( eref );
usages.rxMax = IOHIDElementGetLogicalMax( eref );
break;

case Usage::USAGE_RY:
IOHIDQueueAddElement(queue, eref);
usages.ryMin = IOHIDElementGetLogicalMin( eref );
usages.ryMax = IOHIDElementGetLogicalMax( eref );
break;

case Usage::USAGE_RZ:
IOHIDQueueAddElement(queue, eref);
usages.rzMin = IOHIDElementGetLogicalMin( eref );
usages.rzMax = IOHIDElementGetLogicalMax( eref );
break;
}
break;

case Usage::USAGE_PAGE_BUTTON:
IOHIDQueueAddElement(queue, eref);
usages.buttonsNumber++;
break;
}
}
}
CFRelease(elements);
return true;
}
return false;
}


bool Mouse3D::read(Event3D& event) const
{
if (!(queue && device)) return false;

IOHIDValueRef vref = IOHIDQueueCopyNextValue(queue);
if (!vref) return false;
CFRelease(vref);

CFIndex value = IOHIDValueGetIntegerValue(vref);

IOHIDElementRef eref = IOHIDValueGetElement( vref );
if ( !eref ) return false;

uint32_t usagePage = IOHIDElementGetUsagePage( eref );
uint32_t usage = IOHIDElementGetUsage( eref );

if ( !usagePage || !usage ) return false;
if ( -1 == usage ) return false;

switch (usagePage)
{
case Usage::PRIMARY_USAGE_PAGE:
switch(usage)
{
case Usage::USAGE_X: event.tx = value; break;
case Usage::USAGE_Y: event.ty = value; break;
case Usage::USAGE_Z: event.tz = value; break;
case Usage::USAGE_RX: event.rx = value; break;
case Usage::USAGE_RY: event.ry = value; break;
case Usage::USAGE_RZ: event.rz = value; break;
}
break;

case Usage::USAGE_PAGE_BUTTON:
event.buttons[usage-1] = (value == 1); break;
break;
}
return true;
}

std::string Mouse3D::manufacturerString()
{
return HIDDevice::toStdString(IOHIDDevice_GetManufacturer(device));
}

std::string Mouse3D::productString()
{
return HIDDevice::toStdString(IOHIDDevice_GetProduct(device));
}

IOHIDDeviceRef Mouse3D::hidDevice()
{
return device;
}

void Mouse3D::hidDevice(IOHIDDeviceRef _device)
{
releaseQueue();

device = _device;
queue = HIDDevice::makeQueue(_device, QUEUE_SZ);
valid = false;
ledElement = NULL;

setup();
if (isOpen) open();
}

bool Mouse3D::isValid() const
{
return valid;
}

void Mouse3D::invalid()
{
valid = false;
}



void Mouse3D::deviceValueCallback( void * inContext, IOReturn inResult, void * inSender, IOHIDValueRef inIOHIDValueRef )
{
//#pragma unused( inSender )
IOHIDElementRef eref = IOHIDValueGetElement( inIOHIDValueRef );
if (!eref) return;



IOHIDDeviceRef tIOHIDDeviceRef = IOHIDElementGetDevice(eref);
if (!tIOHIDDeviceRef) return;

Mouse3D *d = ( Mouse3D* ) inContext;
if (!d) return;

std::cout << std::endl;

UInt32 usagePage = IOHIDElementGetUsagePage( eref );
UInt32 usage = IOHIDElementGetUsage( eref );

if ( !usagePage || !usage ) return;
if ( -1 == usage ) return;

//HIDDumpElementInfo(eref);
CFIndex value = IOHIDValueGetIntegerValue(inIOHIDValueRef);

switch(usagePage)
{
case Usage::PRIMARY_USAGE_PAGE:
switch(usage)
{
case Usage::USAGE_X: d->asyncEvent.tx = value; break;
case Usage::USAGE_Y: d->asyncEvent.ty = value; break;
case Usage::USAGE_Z: d->asyncEvent.tz = value; break;
case Usage::USAGE_RX: d->asyncEvent.rx = value; break;
case Usage::USAGE_RY: d->asyncEvent.ry = value; break;
case Usage::USAGE_RZ: d->asyncEvent.rz = value; break;
}
break;

case Usage::USAGE_PAGE_BUTTON:
d->asyncEvent.buttons[usage-1] = (value == 1); break;
break;
}

d->asyncEvent.printOn(std::cout);

}
}









to the server....
the server program is..


#include <iostream>





#include "jmp_tcp/TcpServer.h"
#include "3d_connexion_poc/Protocol.h"
#include "cfsocket/CFTcpServer.h"

using namespace jmp_tcp;

class ServerTest : public TcpServer
{

protected:

virtual void handleConnection(int cfd)
{
char buff[256];

std::cout << "handles a new connection" << std::endl;
while(true)
{
std::cout << " new read " << std::endl;

bzero((char *) buff, sizeof(buff));
int length = recv(cfd, buff, sizeof(buff), 0);
std::cout << " length " << length << std::endl;

if (length <=0) break;
else
{
send(cfd, buff, length, 0);
std::cout << buff << std::endl;
if (strcmp(buff, "by")==0) break;
}

}
std::cout << "terminate a connection" << std::endl;
}
};

void dynamicPort()
{
ServerTest server;
int port = 8000;
while(!server.setup(port) && port <9000)
{
std::cout << std::endl << "server setup failed for port "<< port << std::endl;
port++;
}

std::cout << std::endl << "server running" << std::endl;
server.acceptConnections();
}

void staticPort()
{
ServerTest server;
int port = 8000;
if(!server.setup(port))
{
std::cout << std::endl << "server setup failed"<< std::endl;
return;
}

std::cout << std::endl << "server running" << std::endl;
server.acceptConnections();
}

void testProtocol()
{
Protocol<int> p;
p.setElementValue("x", 10);
p.setElementValue("y", 23);
p.setElementValue("z",544);

p.printOn(std::cout << std::endl);

int length=0;
const char* frame = p.getBuffer(length);
p.setBuffer(frame, length);

p.printOn(std::cout << std::endl);
}

void testCFTcpServer()
{
CFTcpServer server;
int port = 8000;
if(!server.setup(port))
{
std::cout << std::endl << "server setup failed"<< std::endl;
return;
}
std::cout << std::endl << "server running"<< std::endl;



CFRunLoopRun();
}

int main (int argc, char * const argv[])
{
//staticPort();
//dynamicPort();
//testProtocol();
testCFTcpServer();

return 0;
}


and the client program is..


C#
#include <iostream>
#include "jmp_tcp/TcpClient.h"
using namespace jmp_tcp;

int main (int argc, char * const argv[]) {
    TcpClient client;
    if(!client.connectByName("localhost", 8000))
    {
        std::cout << std::endl << "client connection failed"<< std::endl;
        return 0;
    }
    std::string msg;
    char buff[256];
    while(msg != "by")
    {
        std::cin >> msg;
        if(msg.length() >0)
        {
            bzero((char *) buff, sizeof(buff));
            if (!client.send(msg.c_str(), msg.length()))
            {
                std::cout<< std::endl << "send error";
                break;
            }
            int len = client.read(buff, sizeof(buff));
            std::cout << std::endl << len << " " << buff << std::endl;
            if (len <=0) break;
            else
            {
                std::cout << std::endl << len << " " << buff << std::endl;
            }
        }
    }
    client.disconnect();


return 0;
}


how should i link this..
Posted
Comments
Richard MacCutchan 11-Oct-10 8:30am    
Please format your code properly, and explain what your problem is. It is unlikely that people will have the time to read through all the above code to try and figure out where it is going wrong.
bhawna.gopalka 11-Oct-10 8:36am    
i need to send this event d->asyncEvent.printOn(std::cout);
to the server.. and link it using tcp....hav to some modification in the followng part of the program..

void testCFTcpServer()
{
CFTcpServer server;
int port = 8000;
if(!server.setup(port))
{
std::cout << std::endl << "server setup failed"<< std::endl;
return;
}
std::cout << std::endl << "server running"<< std::endl;



CFRunLoopRun();
}
Sandeep Mewara 11-Oct-10 10:00am    
Please format your code again. Only post important code snippet. No one is going to go through all the code.

1 solution

if(!(server.setup(port)))
{
std::cout << std::endl << "server setup failed";
return;
}

If you need to send it to the server it must come from the client app.

You don't have a Mouse3D varible in you client app. such as:

C#
Mouse3D *d = ( Mouse3D* ) inContext;
if (!d) return;

Then you might fill the std::cout buffer with the text then send that to the server.

Or you might make *d a global varible, and work with it that way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900