source: titan/plugins/usbreset/usbreset.h @ 22975

Last change on this file since 22975 was 22975, checked in by nit, 10 years ago

[titan] update usb reset

File size: 2.2 KB
Line 
1#ifndef USBRESET_H
2#define USBRESET_H
3
4#define USBRESETPATH "/sys/bus/usb/drivers/usb"
5#define USBRESETUNBIND USBRESETPATH"/unbind"
6#define USBRESETBIND USBRESETPATH"/bind"
7
8int addusbreset(char* dirname, struct menulist** mlist)
9{
10        int ret = 0;
11        DIR *d;
12        char* tmpstr = NULL;
13
14        //Open the directory specified by dirname
15        d = opendir(dirname);
16
17        //Check it was opened
18        if(! d)
19        {
20                perr("Cannot open directory %s", dirname);
21                return 1;
22        }
23
24        while(1)
25        {
26                struct dirent* entry;
27                int path_length;
28                char path[PATH_MAX];
29
30                snprintf(path, PATH_MAX, "%s", dirname);
31                //Readdir gets subsequent entries from d
32                entry = readdir(d);
33
34                if(!entry) //There are no more entries in this directory, so break out of the while loop
35                        break;
36
37                //See if entry is a subdirectory of d
38                if(entry->d_type == DT_LNK)
39                {
40                        //Check that the directory is not d or d's parent
41                        if(entry->d_name != NULL && ostrcmp(entry->d_name, ".") != 0 && ostrcmp(entry->d_name, "..") != 0)
42                        {
43                                path_length = snprintf(path, PATH_MAX, "%s/%s/product", dirname, entry->d_name);
44                                if(path_length >= PATH_MAX)
45                                {
46                                        err("path length has got too long");
47                                        ret = 1;
48                                        break;
49                                }
50                               
51                                tmpstr = readsys(path, 1);
52                                if(tmpstr != NULL && path[1] == '-')
53                                {
54                                        tmpstr = ostract(tmpstr, " (", 1, 0);
55                                        tmpstr = ostract(tmpstr, entry->d_name, 1, 0);
56                                        tmpstr = ostract(tmpstr, ")", 1, 0);
57                                        struct menulist* m = addmenulist(mlist, tmpstr, NULL, NULL, 0, 0);
58                                        changemenulistparam(m, entry->d_name, NULL);
59                                }
60                                free(tmpstr); tmpstr = NULL;
61                               
62                        }
63                }
64        }
65
66        //After going through all the entries, close the directory
67        if(d && closedir(d))
68        {
69                perr("Could not close %s", dirname);
70                ret = 1;
71        }
72
73        return ret;
74}
75
76void screenusbreset()
77{
78        char* tmpstr = NULL;
79        struct menulist* mlist = NULL, *mbox = NULL;   
80        struct skin* load = getscreen("loading");
81       
82        addusbreset(USBRESETPATH, &mlist);
83       
84        mbox = menulistbox(mlist, NULL, "USB Reset", NULL, NULL, 1, 0);
85       
86        if(mbox != NULL)
87        {
88                if(mbox->param != NULL)
89                {
90                        drawscreen(load, 0, 0);
91               
92                        writesys(USBRESETUNBIND, mbox->param, 0); //unbind
93                        sleep(2);
94                        writesys(USBRESETBIND, mbox->param, 0); //bind
95                       
96                        clearscreen(load);
97                }
98        }
99       
100        freemenulist(mlist, 1); mlist = NULL;
101}
102
103#endif
Note: See TracBrowser for help on using the repository browser.