Пример #1
0
int main(int argc, char* argv[])
{
    int hotplug_sock = init_hotplug_sock();
    while(1)
    {
        char buf[UEVENT_BUFFER_SIZE*2] = {0};
        //ssize_t recv(int socket, void *buffer, size_t length, int flags);
        recv(hotplug_sock, &buf, sizeof(buf), 0); //config snl(sockaddr) -> socket -> setsockopt -> bind -> recv(connection-mode)
        printf("%s\n", buf);
    }
    
    return 0;
}
Пример #2
0
int main(int argc, char* argv[]) 
{ 
    int hotplug_sock = init_hotplug_sock(); 

    while(1) 
    { 
        /* Netlink message buffer */
        char buf[UEVENT_BUFFER_SIZE * 2] = {0};

        recv(hotplug_sock, &buf, sizeof(buf), 0); 
        printf("%s\n", buf); 
        /* USB 设备的插拔会出现字符信息,通过比较不同的信息确定特定设备的插拔,在这添加比较代码 */
    }
    
    return 0; 
} 
Пример #3
0
void UsbHubStudy::go() {

    printf("# init mode\n");
    printf("请将USB存储设备插拔同一个Hub的不同USB口, 然后退出.\nPS: 不得插拔不同的Hub, 如果你这样做了, 请退出后再试. 总之init模式下, 保证只有需要检测的Hub发生变化.\n\n");

    // 读取消息数组
    char buf[UEVENT_BUFFER_SIZE * 2] = {0};
    // 串口socket
    int hotplug_sock = init_hotplug_sock();
    while(1)
    {
        // 清空
        memset(buf, 0x0, sizeof(buf));
        // 阻塞读取
        recv(hotplug_sock, &buf, sizeof(buf), 0);
#ifdef _DEBUG_
        printf("debug:recv:%s\n", buf);
#endif
        string strBuf = buf;
        // 解析出开头是add, remove, 末尾有tty
        if(0 == strBuf.find("add@") && strBuf.find("tty/") != std::string::npos)
        {
#ifdef _DEBUG_
            printf("debug:add:%s\n", strBuf.c_str());
#endif
            strBuf = strBuf.substr(4);
            work(strBuf);
        }
        else if(0 == strBuf.find("remove@") && strBuf.find("tty/") != std::string::npos)
        {
#ifdef _DEBUG_
            printf("debug:remove:%s\n", strBuf.c_str());
#endif
            strBuf = strBuf.substr(7);
            work(strBuf);
        }
    }
}
Пример #4
0
void *hotPlugThread(void *param)
{
    int i = 0;
    int ret = 0;
    char buf[1024] = {0};
    char devName[128] = {0};
    char devPath[124] = {0};
    char mountPath[128] = {0};
    char cmd[128] = {0};

    int hotplug_sock;
    int sum,size;
    char *str;

#if 0
    unsigned char uuid[16] = {0};

    setDiskVolumeName("/dev/sda1", "abcd");

    getDiskPartitionInfo("/dev/sda1", uuid, NULL, NULL);

    checkFallocateFile("/disk/disk0000", 1024*1024*1024);
#endif

    hotplug_sock = init_hotplug_sock();

    while(1)
    {
        sum = 0;
        memset(buf, 0, sizeof(buf));
        size = recv(hotplug_sock, buf, sizeof(buf), 0);
        	//	fprintf(stderr,"size=%d, buf=[%s], strlen(buf)=%d\n", size, buf, strlen(buf));
        while(sum < size)
        {
            str = buf + sum;
            sum += strlen(str);
            buf[sum] = '\n';
        }
        buf[sum] = 0;

      //  fprintf(stderr,"buf=[%s]\n\n", buf);

        if(((strstr(buf, "ACTION=add") != NULL) && (strstr(buf, "DEVTYPE=disk") != NULL))||((strstr(buf, "ACTION=add") != NULL) && (strstr(buf, "DEVTYPE=partition") != NULL)))
        {
            if((strstr(buf, "NPARTS=0") != NULL) || (strstr(buf, "PARTN=") != NULL))
            {
                memset(devName, 0, sizeof(devName));
                if(getDevName(buf, devName) == 0)
                {
                    fprintf(stderr,"add devName = [%s]\r\n", devName);
                    if(strstr(buf, "DEVPATH=/devices/platform/hiusb-ehci") != NULL)
                    {//u盘
                        for(i = 0; i < 8; i++)
                        {
                            sprintf(devPath, "/dev/%s", devName);
                            sprintf(mountPath, "/usb/usb%04d", i);
                            ret = checkDiskPartitionIsMount(devPath, mountPath);
                            if(ret == 0x00)
                            {
                                sprintf(cmd, "mount %s %s", devPath, mountPath);
                                ret=pox_system(cmd);
                                ret=iCheckSystemReturnValue(ret);
                                if(0!=ret)
                                    continue;
                            }
                            else if(ret == 0x03)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }

        if(((strstr(buf,"ACTION=remove")!=NULL) && (strstr(buf, "DEVTYPE=disk") != NULL))||((strstr(buf, "ACTION=remove") != NULL) && (strstr(buf, "DEVTYPE=partition") != NULL)))
        {
            if((strstr(buf, "NPARTS=0") != NULL) || (strstr(buf, "PARTN=") != NULL))
            {
                if(getDevName(buf, devName) == 0)
                {
                    printf("remove devName = [%s]\r\n", devName);
                    if(strstr(buf, "DEVPATH=/devices/platform/hiusb-ehci") != NULL)
                    {//u
                        sprintf(devPath, "/dev/%s", devName);
                        sprintf(cmd, "umount %s", devPath);
                        int i ;
                        for(i=0;i!=4;++i)
                        {
                            ret=pox_system(cmd);
                            ret=iCheckSystemReturnValue(ret);
                            if(0==ret)
                            {
                                break;
                            }
                            fprintf(stderr,"remove devName = [%s] fail \r\n", devName);
                            sleep(1);
                        }
                        umount(devPath);
                        umount(devPath);
                    }

                    else if(strstr(buf, "DEVPATH=/devices/platform/ahci") != NULL)
                    {//sata硬盘

                        fprintf(stderr , "\n===============%s \n", buf);

                        sprintf(devPath, "/dev/%s", devName);
                        sprintf(cmd, "umount -f %s", devPath);

                        fprintf(stderr, "================cmd %s \n", cmd);
                        system(cmd);
                        system(cmd);
                    }
		else
		{
			umount("/usb/usb0000");
			umount("/usb/usb0001");
			umount("/usb/usb0002");
			umount("/usb/usb0003");
		}
                }
            }
        }
        usleep(400*1000);
    }

    return NULL;
}
Пример #5
0
int main(int argc, char *argv[])
{
	int hotplug_sock = init_hotplug_sock();
	struct udev *udev;
	struct udev_monitor *mon;
	
	struct udv_device *dev;
	const char *path;
	const char *dev_node;
	const char *action;
		

	udev = udev_new();
	if (udev == NULL) {
		printf("new udev error!\n");
		return -1;
	}

	mon = udev_monitor_new_from_netlink(udev, "udev");
	if (mon == NULL) {
		printf("create mon error!\n");
		return -1;
	}

	if (udev_monitor_filter_add_match_subsystem_devtype(mon,
							    "block",
							    "disk") < 0) {
		printf("udev monitor add match failed!\n");
		udev_unref(udev);
		return -1;
	}

	udev_monitor_set_receive_buffer_size(mon, 128*1024);
	if (udev_monitor_enable_receiving(mon) < 0) {
		printf("monitor enable failed!\n");
		udev_monitor_unref(mon);
		udev_unref(udev);
		return -1;
	}
	struct udev_enumerate *uenum;
	struct udev_list_entry *devs, *dev_list;
	uenum = udev_enumerate_new(udev);
	if (uenum == NULL) {
		printf("uenum create failed.\n");
		return -1;
	}
	if (udev_enumerate_add_match_subsystem(uenum, "block") < 0) {
		printf("uenum add match subsystem failed.\n");
		return -1;
	}
	if (udev_enumerate_scan_devices(uenum) < 0) {
		printf ("uenum scan devices failed.\n");
		return -1;
	}
	devs = udev_enumerate_get_list_entry(uenum);
	udev_list_entry_foreach(dev_list, devs) {


		path = udev_list_entry_get_name(dev_list);
		if (path == NULL)
			continue;
		dev = udev_device_new_from_syspath(udev, path);
		if (dev == NULL)
			continue;
		dev_node = udev_device_get_devnode(dev);
		if (dev_node == NULL)
			continue;

		printf("=====enume  path:%s, node:%s\n", path, dev_node);
	}