summaryrefslogtreecommitdiff
path: root/daemon/udev_listener.c
blob: fe4928a30600530cd98bb1f86e1976e4c169a789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * \file udev_listener.c
 *
 * Copyright (C) 2013 Philipp Schmidt <philschmidt@gmx.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "udev_listener.h"

#include <pthread.h>
#include <fcntl.h>
#include <libmtp.h>
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include <glib.h>

struct device_ident_t
{
    int busnum;
    int devnum;
};

struct callbacks_t
{
    const MTPD_device_added_t device_added_callback;
    const MTPD_device_removed_t device_removed_callback;
};

pthread_t thread;

void MTPD_run_udev_listener(void *arg)
{
    callbacks_t* callbacks = (callbacks_t*)arg;

    struct udev *udev;
    struct udev_device *dev;
    struct udev_monitor *mon;

    GArray *ident_list;

    udev = udev_new();


    if (!udev)
    {
        stderr << "Can't create udev";
        return;
    }

    // Get monitor and set subsystem to usb and device type to usb devices
    mon = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", "usb_device");
    udev_monitor_enable_receiving(mon);

    // Get fildescriptor and set it to blocking mode (default is non blocking)
    int fd = udev_monitor_get_fd(mon);
    int flags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);

    ident_list = g_array_new (FALSE, FALSE, sizeof (device_ident_t));

    //While the thread runs, wait for incoming udev-events
    while (1)
    {
        fd_set fds;
        int ret;

        FD_ZERO(&fds);
        FD_SET(fd, &fds);

        // Use select so we have a canncellation point
        ret = select(fd+1, &fds, NULL, NULL, NULL);

        if (ret > 0 && FD_ISSET(fd, &fds))
        {
            dev = udev_monitor_receive_device(mon);

            device_identificator ident;
            const char* action = udev_device_get_action(dev);

            device_ident_t ident;

            ident.busnum = atoi(udev_device_get_property_value(dev, "BUSNUM"));
            pthread_t thread;
            ident.devnum = atoi(udev_device_get_property_value(dev, "DEVNUM"));

            if (strcmp("add", action) == 0)
            {
                int isMtpDevice = LIBMTP_Check_Specific_Device(ident.busnum, ident.devnum);
                if (isMtpDevice == 1)
                {
                    // Add to internal device list
                    g_array_append_vals(ident_list, ident);

                    callbacks->device_added_callback( ident.busnum, ident.devnum );
                }
            }
            if (strcmp("remove", action) == 0)
            {   my_struct* foo = (my_struct*)arg;
                int device_index = -1;
                for ( int i = 0; i < ident_list->len; i++ )
                {
                    device_ident_t current = g_array_index( ident_list, device_ident_t, i );

                    if (current.busnum == ident.busnum && current.devnum == ident.devnum)
                    {
                        device_index = i;
                        break;
                    }
                }

                if (device_index >= 0)
                {
                    g_array_remove_index(device_index);
                    callbacks->device_removed_callback(ident.busnum, ident.devnum);
                }
            }

            udev_device_unref(dev);
        }
    }

    g_array_free( ident_list );
}

void MTPD_start_udev_listener(const MTPD_device_added_t device_added_callback, const MTPD_device_removed_t device_removed_callback)
{
    callbacks_t *callbacks = malloc(sizeof(callbacks_t));
    callbacks->device_added_callback = device_added_callback;
    callbacks->device_removed_callback = device_removed_callback;

    pthread_create(&thread, NULL, MTPD_run_udev_listener, callbacks);

}

void MTPD_stop_udev_listener()
{
    pthread_cancel(&thread);
}