40 #define __FILE_ID__ "ypkt_osx" 44 #include <sys/types.h> 46 #include <sys/sysctl.h> 55 #define YOCTO_LOCK_PIPE "/tmp/.yoctolock" 59 static int yReserveGlobalAccess(
yContextSt *ctx,
char *errmsg)
62 int chk_val, mypid, usedpid = 0;
65 mkfifo(YOCTO_LOCK_PIPE, 0600);
66 fd = open(YOCTO_LOCK_PIPE, O_RDWR|O_NONBLOCK);
74 mypid = (int) getpid();
75 res = read(fd, &chk_val,
sizeof(chk_val));
76 if (res ==
sizeof(chk_val)) {
83 write(fd, &chk_val,
sizeof(chk_val));
99 static void yReleaseGlobalAccess(
yContextSt *ctx)
102 int fd = open(YOCTO_LOCK_PIPE,O_RDWR|O_NONBLOCK);
104 read(fd,&chk_val,
sizeof(chk_val));
108 static void *event_thread(
void *
param)
112 ctx->usb_run_loop = CFRunLoopGetCurrent();
115 HALLOG(
"Start event_thread run loop\n");
117 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 10, FALSE);
120 HALLOG(
"event_thread run loop stoped\n");
126 static int setupHIDManager(
yContextSt *ctx, OSX_HID_REF *hid,
char *errmsg)
129 CFMutableDictionaryRef dictionary;
130 CFNumberRef Vendorid;
135 hid->manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
137 dictionary = CFDictionaryCreateMutable(kCFAllocatorDefault,1,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
138 Vendorid = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &c_vendorid );
139 CFDictionarySetValue( dictionary, CFSTR( kIOHIDVendorIDKey ), Vendorid );
142 IOHIDManagerSetDeviceMatching(hid->manager, dictionary );
144 CFRelease(dictionary);
146 IOHIDManagerScheduleWithRunLoop(hid->manager, ctx->usb_run_loop, kCFRunLoopDefaultMode);
149 tIOReturn = IOHIDManagerOpen(hid->manager, kIOHIDOptionsTypeNone );
150 if(kIOReturnSuccess != tIOReturn ||CFGetTypeID(hid->manager) != IOHIDManagerGetTypeID()){
151 HALLOG(
"Unable to Open HID Manager");
160 static void stopHIDManager(OSX_HID_REF *hid)
163 IOHIDManagerClose(hid->manager, kIOHIDOptionsTypeNone );
164 CFRelease( hid->manager);
174 size_t size =
sizeof(str);
175 YPROPERR(yReserveGlobalAccess(ctx, errmsg));
177 if (sysctlbyname(
"kern.osrelease", str, &size, NULL, 0) ==0){
187 if (numver >= 13 && numver < 15){
193 pthread_create(&ctx->usb_thread, NULL, event_thread, ctx);
198 if (
YISERR(setupHIDManager(ctx, &ctx->hid,errmsg))) {
207 stopHIDManager(&ctx->hid);
211 CFRunLoopStop(ctx->usb_run_loop);
213 pthread_join(ctx->usb_thread,NULL);
216 yReleaseGlobalAccess(ctx);
224 static u32 get_int_property(IOHIDDeviceRef device, CFStringRef key)
229 ref = IOHIDDeviceGetProperty(device, key);
231 if (CFGetTypeID(ref) == CFNumberGetTypeID() && CFNumberGetValue((CFNumberRef) ref, kCFNumberSInt32Type, &value)) {
239 static void get_txt_property(IOHIDDeviceRef device,
char *buffer,u32 maxlen, CFStringRef key)
244 ref = IOHIDDeviceGetProperty(device, key);
246 if (CFGetTypeID(ref) == CFStringGetTypeID()) {
248 CFStringEncoding encodingMethod;
249 encodingMethod = CFStringGetSystemEncoding();
251 str = CFStringGetCStringPtr(ref, encodingMethod);
255 encodingMethod = kCFStringEncodingUTF8;
256 str = CFStringGetCStringPtr(ref, encodingMethod);
260 CFIndex cflength = CFStringGetLength(ref)*2+2;
261 char *tmp_str =
yMalloc( (u32)cflength);
262 if (!CFStringGetCString(ref, tmp_str, cflength, kCFStringEncodingUTF8 )) {
267 if(cflength>maxlen-1){
270 memcpy(buffer,tmp_str,cflength);
279 memcpy(buffer,str,len);
291 static IOHIDDeviceRef* getDevRef(OSX_HID_REF *hid, CFIndex *deviceCount)
294 CFSetRef deviceCFSetRef;
295 IOHIDDeviceRef *dev_refs=NULL;
300 deviceCFSetRef = IOHIDManagerCopyDevices(hid->manager);
302 if (deviceCFSetRef!= NULL) {
304 *deviceCount = CFSetGetCount( deviceCFSetRef );
305 dev_refs =
yMalloc(
sizeof(IOHIDDeviceRef) * (u32)*deviceCount );
307 CFSetGetValues( deviceCFSetRef, (
const void **) dev_refs );
318 IOHIDDeviceRef *dev_refs;
321 dev_refs = getDevRef(&
yContext->hid, &deviceCount);
322 if(dev_refs == NULL) {
330 memset(*ifaces, 0 ,nbifaceAlloc *
sizeof(
yInterfaceSt));
331 for(deviceIndex=0 ; deviceIndex < deviceCount ;deviceIndex++){
334 IOHIDDeviceRef dev = dev_refs[deviceIndex];
336 vendorid = get_int_property(dev,CFSTR(kIOHIDVendorIDKey));
337 deviceid = get_int_property(dev,CFSTR(kIOHIDProductIDKey));
339 if(*nbifaceDetect == nbifaceAlloc){
348 iface = *ifaces + *nbifaceDetect;
353 HALENUMLOG(
"work on interface %d (%x:%x:%s)\n",deviceIndex,vendorid,deviceid,iface->
serial);
382 static void Handle_IOHIDDeviceIOHIDReportCallback(
386 IOHIDReportType inType,
389 CFIndex InReportLength)
393 memset(&iface->tmprxpkt,0xff,
sizeof(
USB_Packet));
402 IOHIDDeviceRef *dev_refs;
410 dev_refs = getDevRef(&iface->hid, &deviceCount);
412 dev_refs = getDevRef(&
yContext->hid, &deviceCount);
414 if(dev_refs == NULL) {
419 for(i=0 ; i < deviceCount ;i++){
422 IOHIDDeviceRef dev = dev_refs[i];
423 vendorid = get_int_property(dev,CFSTR(kIOHIDVendorIDKey));
424 deviceid = get_int_property(dev,CFSTR(kIOHIDProductIDKey));
428 get_txt_property(dev,serial,
YOCTO_SERIAL_LEN * 2, CFSTR(kIOHIDSerialNumberKey));
430 HALLOG(
"right Interface detected (%x:%x:%s)\n",vendorid,deviceid,iface->
serial);
437 if (i == deviceCount) {
441 IOReturn ret = IOHIDDeviceOpen(iface->devref, kIOHIDOptionsTypeNone);
442 if (ret != kIOReturnSuccess) {
443 YSPRINTF(str,32,
"Unable to open device (0x%x)",ret);
452 sprintf(str,
"yocto_%p", iface->devref);
453 iface->run_loop_mode = CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);
455 IOHIDDeviceScheduleWithRunLoop(iface->devref,
yContext->usb_run_loop, iface->run_loop_mode);
456 IOHIDDeviceRegisterInputReportCallback( iface->devref,
457 (u8*) &iface->tmprxpkt,
459 &Handle_IOHIDDeviceIOHIDReportCallback,
470 if (i==SETUPED_IFACE_CACHE_SIZE) {
485 while (pktitem!=NULL){
486 if(iface->devref==NULL){
490 res = IOHIDDeviceSetReport(iface->devref,
491 kIOHIDReportTypeOutput,
495 if (res != kIOReturnSuccess) {
496 dbglog(
"IOHIDDeviceSetReport failed with 0x%x\n", res);
517 YASSERT(i<SETUPED_IFACE_CACHE_SIZE);
518 if(iface->devref!=NULL){
519 IOHIDDeviceRegisterInputReportCallback(iface->devref,
520 (u8*) &iface->tmprxpkt,
524 IOHIDDeviceClose(iface->devref, kIOHIDOptionsTypeNone);
530 CFRelease(iface->run_loop_mode);
532 stopHIDManager(&iface->hid);
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
#define YERRMSG(code, message)
void yInitializeCriticalSection(yCRITICAL_SECTION *cs)
int yyySetup(yInterfaceSt *iface, char *errmsg)
int yyyUSBGetInterfaces(yInterfaceSt **ifaces, int *nbifaceDetect, char *errmsg)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
YRETCODE yPktQueuePopH2D(yInterfaceSt *iface, pktItem **pkt)
int yyyUSB_init(yContextSt *ctx, char *errmsg)
#define SETUPED_IFACE_CACHE_SIZE
#define HALENUMLOG(fmt, args...)
int yyySignalOutPkt(yInterfaceSt *iface, char *errmsg)
int yyyUSB_stop(yContextSt *ctx, char *errmsg)
#define HALLOG(fmt, args...)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
char serial[YOCTO_SERIAL_LEN *2]
yInterfaceSt * setupedIfaceCache[SETUPED_IFACE_CACHE_SIZE]
YRETCODE yPktQueuePushD2H(yInterfaceSt *iface, const USB_Packet *pkt, char *errmsg)
void yDeleteCriticalSection(yCRITICAL_SECTION *cs)
void yPktQueueFree(pktQueue *q)
void yyyPacketShutdown(yInterfaceSt *iface)
struct _yInterfaceSt::@69 flags
#define YCTX_OSX_MULTIPLES_HID
char serial[YOCTO_SERIAL_LEN]
#define yMemcpy(dst, src, size)
int yyyOShdlCompare(yPrivDeviceSt *dev, yInterfaceSt *newiface)
void yPktQueueInit(pktQueue *q)