Carplay 参考代码: int UdevIsCarplay(int nVid, int nPid)//int UsbModeSwitch(pUdevStatus pStatus) { ; if (nVid != APPLE_IDVENDOR){ ; } r = libusb_init(NULL); ) { pError("failed to initialise libusb\n"); ; } struct libusb_device_handle *devh = NULL; devh = libusb_open_device_with_vid_pid(NULL, nVid, nPid); if (NULL == devh) { pError("Could not find/open device(%s)\n", strerror(errno)); r = ;goto end; } //libusb_device *dev = libusb_get_device(devh); ];// if tmp is {0x01, 0x00, 0x00, 0x00} support carplay;else close carplay. memset(tmp, , ); r = libusb_control_transfer(devh, , , tmp, , );//Page 715 //r = libusb_control_transfer(devh, 0x40, 0x51, 0x01, 0, NULL, 0, 0);//Page 716 UsbModeSwitch ) { pError("UdevIsCarplay --- get support capabilities error !!! r = %d(%s)\n", r, strerror(errno)); r = ; } ) { pError("short write (%d)\n", r); r = ; } else { //int nTmp = tmp[0] + (int)(tmp[1]<<8) + (int)(tmp[2]<<16) + (int)(tmp[3]<<24); pInfo(],tmp[],tmp[],tmp[]); ]) r = ; } libusb_close(devh); end: libusb_exit(NULL); return r ; }
邹工: 参考代码如下: void* UdevEventMonitorThread(void* pParam) { int fd; struct udev_device *dev; struct udev_monitor *mon; pUdevContainer pUdevCon = (pUdevContainer)pParam; mon = udev_monitor_new_from_netlink(pUdevCon->m_pUdev, "udev");//"kernel"或"udev"。基于"kernel"的事件通知要早于"udev",但相关的设备结点未必创建完成 if (mon == NULL) { pError("udev_monitor_new_from_netlink FAILED \n"); return; } ) { pError("UdevAddFilter FAILED \n"); return; } ) { pError("udev_monitor_enable_receiving FAILED \n"); return; } UdevStatus status[];//status[0] is OTG atatus; status[1] is HOST atatus; //pUdevStatus status = calloc(2, sizeof(UdevStatus)); memset(status, , * sizeof(UdevStatus)); status[].m_eUdevStatus = status[].m_eUdevStatus = UDEVOTHER; /* Get the file descriptor (fd) for the monitor.This fd will get passed to select() */ fd = udev_monitor_get_fd(mon); ); struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); ]; pInfo("UdevEventMonitorThread --- start ok !!!!!!!!!!! \n"); /* This section will run continuously, calling usleep() at the end of each pass. This is to demonstrate how to use a udev_monitor in a non-blocking way. */ ) { ; //pError("epoll_wait event start!\n"); , -);//-1 is block,>=0 is noblock; //pWarn("epoll_wait event ok ret = %d!\n",ret); ; i < ret; i++) { /* Make the call to receive the device. select() ensured that this will not block. */ dev = udev_monitor_receive_device(mon); if (dev) { const char* szAction = udev_device_get_action(dev); const char* szDevNode = udev_device_get_devnode(dev); const char* szSysPath = udev_device_get_syspath(dev); int nStatus = SetUdevStatus(pUdevCon, szAction);//pStatus, if(UDEVOTHER == nStatus) { //pDebug("nStatus = 0(OTHER); goto end; } //pDebug("nActionStatus = %d\n",nStatus); int nPort = UdevPortJudge(pUdevCon, szSysPath, nStatus); if(OTHERPORT == nPort) { //pDebug("nPort = OTHERPORT(%d)\n", nPort); goto end; } pUdevStatus pStatus = &status[nPort-];//status+(nPort-1)*sizeof(UdevStatus); //pDebug(" status[%d]=(%d),pStatus->m_eUdevStatus(%d),pStatus->m_nChecked(%d)~~~~~~~~~~~~~~\n", nPort-1, nStatus,pStatus->m_eUdevStatus,pStatus->m_nChecked); if(nStatus == UDEVCHANGE) { pStatus->m_eUdevStatus = nStatus; print_all_properties(pUdevCon, pStatus, dev, "prop"); goto end; //pDebug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); } else if(nStatus == UDEVREMOVE) { if(UDEVREMOVE != pStatus->m_eUdevStatus)//插入USB上电可能没有检测到设备,导致拔出状态错误 { ResetStatus(pStatus); if(NULL != szDevNode) pUdevCon->m_pUdevEvent->UdevStatusProc(pUdevCon->m_eUdevPort, pStatus->m_eUdevStatus, USBNODEV, szDevNode); else pUdevCon->m_pUdevEvent->UdevStatusProc(pUdevCon->m_eUdevPort, pStatus->m_eUdevStatus, USBNODEV, "USBNODEV"); pUdevCon->m_eUdevPort = OTHERPORT; } goto end; } //ADD event pStatus->m_eUdevStatus = nStatus; )//nStatus == ADD { print_all_attributes(pStatus, dev, "attr"); ) { pInfo("[DM-SMING] %d\n", __LINE__); if(pStatus->m_eUdevType == USBHUB) pUdevCon->m_pUdevEvent->UdevStatusProc(nPort, nStatus, USBHUB, "USBHUB"); else if(pStatus->m_eUdevType == USBOTHER) pUdevCon->m_pUdevEvent->UdevStatusProc(nPort, nStatus, USBOTHER, "USBOTHER"); } ) { pInfo("[DM-SMING] %d\n", __LINE__); int nCheck = UdevCheckContinue(pStatus, szDevNode); ) { pInfo("[DM-SMING] %d\n", __LINE__); if(pStatus->m_eUdevType == USBHID) pUdevCon->m_pUdevEvent->UdevStatusProc(nPort, nStatus, USBHID, "USBHID"); else pUdevCon->m_pUdevEvent->UdevStatusProc(nPort, nStatus, pStatus->m_eUdevType, szDevNode); } } } end: udev_device_unref(dev); } else { pDebug("No Device from receive_device().\n");// An error occured. } } //else // usleep(100*1000); //fflush(stdout); } return; } static void print_all_properties(pUdevContainer pUdevCon, pUdevStatus pStatus, struct udev_device *device, const char *key) { struct udev_list_entry *properties; udev_list_entry_foreach(properties, udev_device_get_properties_list_entry(device)) { const char *szName = udev_list_entry_get_name(properties); const char *szValue = udev_list_entry_get_value(properties); if(NULL != szValue) pDebug(" [%s]{%s}==\"%s\"\n", key, szName, szValue); ) { ) { //通过回调给上层USBNORESPOND消息,设备节点为”USBNORESPOND“ pUdevCon->m_pUdevEvent->UdevStatusProc(pUdevCon->m_eUdevPort, pStatus->m_eUdevStatus, USBNORESPOND, "USBNORESPOND"); return; } } } }
库文件在附件中。
链接: http://pan.baidu.com/s/1qXSSB84 密码: ptgu