aboutsummaryrefslogtreecommitdiffstats
path: root/src/platform-info-devices.c
blob: 785423b9e9291b7e08509b58887907c5498f7cd3 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * Copyright (C) 2020 "IoT.bzh"
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <libudev.h>
#include <unistd.h>
#include "platform-info-devices.h"

#define JSON_NEW_CONST_KEY \
    (JSON_C_OBJECT_KEY_IS_CONSTANT | JSON_C_OBJECT_ADD_KEY_IS_NEW)
#define UNUSED(x) (void)(x);

static void*        pinfo_device_client_new(void* req_ctx);
static void         pinfo_device_client_free(void* client_ctx);
static void*        pinfo_device_monitor_loop(pinfo_client_ctx_t* ctx);
static void         pinfo_device_monitor_detect(pinfo_client_ctx_t* ctx, struct json_object* jdevice);
static json_object* pinfo_device_udevice_to_jdevice(struct udev_device* udevice, struct json_object* jmask);
static void         pinfo_device_jdev_destructor(json_object* jdevice, void* udevice);
static int          pinfo_device_filter_monitoring(pinfo_client_ctx_t* ctx);
static void         pinfo_device_filter_scan(struct udev_enumerate* udev_enum, struct json_object* jfilter);
static json_object* pinfo_device_udevice_to_jlist(
                                    struct udev_device* udevice,
                                    struct udev_list_entry*(*udevice_elist)(struct udev_device*),
                                    const char*(*elist_val_getter)(struct udev_device*,const char*),
                                    const unsigned int jflags);
static json_object* pinfo_device_udevice_to_jlist_mask(
                                    struct udev_device* udevice,
                                    const char*(*udevice_val_getter)(struct udev_device*,const char*),
                                    json_object* jkeys,
                                    unsigned jcpy_flags);

static void
pinfo_device_monitor_detect(pinfo_client_ctx_t* ctx, struct json_object* jdevice)
{
	afb_event_push((afb_event_t)ctx->ev_devs_changed,jdevice);
}

int
pinfo_device_monitor(afb_req_t req) {
    int res = PINFO_ERR;

    if(afb_req_context(req,0,pinfo_device_client_new,pinfo_device_client_free,req)) {
        res = PINFO_OK;
    }

    return res;
}

static void
pinfo_device_client_free(void* client_ctx) {
	if(client_ctx) {
		pinfo_client_ctx_t* ctx = (pinfo_client_ctx_t*)client_ctx;
		if(ctx) {
			pthread_cancel(ctx->th);
			udev_monitor_unref(ctx->umon_hndl);
			udev_unref(ctx->udev_ctx);
			afb_event_unref((afb_event_t)ctx->ev_devs_changed);
			json_object_put(ctx->filter);
			json_object_put(ctx->mask);
			ctx->api_ctx->client_count--;
			AFB_DEBUG("Client context released, client count: %d",ctx->api_ctx->client_count);
		}
	}
}

static void*
pinfo_device_client_new(void* req_ctx) {
    pinfo_client_ctx_t* ctx = NULL;
	afb_req_t req = (afb_req_t)(req_ctx);

	if(req) {
        ctx = malloc(sizeof(*ctx));
        if(ctx) {
            ctx->udev_ctx =  udev_new();
            if(ctx->udev_ctx) {
                ctx->umon_hndl = udev_monitor_new_from_netlink(ctx->udev_ctx,"udev");
                ctx->filter = NULL;
                ctx->mask = NULL;
                ctx->umon_cb = NULL;

                if(ctx->umon_hndl) {
                    json_object* jval = NULL;
                    json_object* jargs = afb_req_json(req);
                    pthread_attr_t attr;

                    ctx->ev_devs_changed = afb_api_make_event(req->api,"device_changed");
                    if(afb_event_is_valid(ctx->ev_devs_changed)) {
                        if(afb_req_subscribe(req,ctx->ev_devs_changed) == PINFO_OK) {
                            ctx->umon_cb = (void(*)(void*,struct json_object*))pinfo_device_monitor_detect;
                            if(json_object_object_get_ex(jargs,"filter",&jval) &&
                                json_object_is_type(jval,json_type_object)) {
                                json_object_deep_copy(jval,&ctx->filter,NULL);
                            }

                            if(json_object_object_get_ex(jargs,"mask",&jval) &&
                                json_object_is_type(jval,json_type_object)) {
                                json_object_deep_copy(jval,&ctx->mask,NULL);
                            }

                            if(pinfo_device_filter_monitoring(ctx) == PINFO_OK) {
                                pthread_attr_init(&attr);
                                pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
                                if(pthread_create(&ctx->th,&attr,
                                (void*(*)(void*))&pinfo_device_monitor_loop,(void*)ctx) == PINFO_OK) {
                                    pinfo_api_ctx_t* api_ctx = NULL;
                                    api_ctx = (pinfo_api_ctx_t*)afb_api_get_userdata(req->api);
                                    AFB_REQ_DEBUG(req,"New client event-loop have been started ...");
                                    //New client (new session) get a pointer of API context
                                    //to access global(API) context
                                    if(api_ctx) {
                                        ctx->api_ctx = api_ctx;
                                        api_ctx->client_count++;
                                        AFB_REQ_INFO(req,"New client context allocated, client count: %d",
                                        api_ctx->client_count);
                                    }
                                } else {
                                    AFB_REQ_ERROR(req,"Failed to run the new session monitoring thread.");
                                    pinfo_device_client_free(ctx);
                                    ctx = NULL;
                                }
                                pthread_attr_destroy(&attr);
                            } else {
                                AFB_REQ_ERROR(req,"Failed to apply the device monitoring filters.");
                            }
                        } else {
                            AFB_REQ_ERROR(req,"New client event subscribing failed.");
                        }
                    } else {
                        AFB_REQ_ERROR(req,"Invalid AFB event generated for new client.");
                    }
                } else {
                    AFB_REQ_ERROR(req,
                    "Failed to generate new udev monitoring object for the new client.");
                }
            } else {
                AFB_REQ_ERROR(req,
                "Failed to generate new udev object for the new client.");
            }
        } else {
            AFB_REQ_ERROR(req,"Failed to allocate memory for new client context.");
        }
    }

    return (void*)ctx;
}

static void
pinfo_device_jdev_destructor(struct json_object* jdevice, void* udevice)
{
    UNUSED(jdevice)
    udev_device_unref((struct udev_device*)udevice);
}

static int
pinfo_device_filter_monitoring(pinfo_client_ctx_t* ctx) {
    int res = PINFO_OK;

    if(ctx && ctx->umon_hndl &&
    json_object_is_type(ctx->filter,json_type_object)) {
        json_object* jprop_filter = NULL;
        json_object* jval = NULL;

        if(json_object_object_get_ex(ctx->filter,"properties",&jprop_filter)) {
            const char* subsys_str = NULL;
            const char* devtype_str = NULL;

            if(json_object_object_get_ex(jprop_filter,"SUBSYSTEM",&jval) &&
            json_object_is_type(jval,json_type_string)) {
                subsys_str = json_object_get_string(jval);
            }
            jval = NULL;
            if(json_object_object_get_ex(jprop_filter,"DEVTYPE",&jval) &&
            json_object_is_type(jval,json_type_string)) {
                devtype_str = json_object_get_string(jval);
            }
            if(devtype_str || subsys_str) {
                res |= udev_monitor_filter_add_match_subsystem_devtype(
                        ctx->umon_hndl,subsys_str,devtype_str);
            }
        }

        if(json_object_object_get_ex(ctx->filter,"tags",&jval)) {
            if(json_object_is_type(jval,json_type_array)) {
                const size_t tags_count = json_object_array_length(jval);

                if(tags_count > 0) {
                    int tag_idx = 0;
                    json_object *tag_item = NULL;

                    for(tag_item = json_object_array_get_idx(jval,0);
                        tag_item && tag_idx < tags_count;
                        tag_item = json_object_array_get_idx(jval,++tag_idx)) {

                        if(json_object_is_type(tag_item,json_type_string)) {
                            res |= udev_monitor_filter_add_match_tag(
                                ctx->umon_hndl,
                                json_object_get_string(tag_item)
                            );
                        } else {
                            AFB_WARNING("Client passed invalid tag type,\
                            avoid the tag expression");
                        }
                    }
                } else {
                    //Empty tags array
                }
            } else {
                AFB_WARNING("Client passed invalid value for 'tags' field,\
                the value type should be a json array with json string items,\
                avoid the tags filtering");
            }
        }
    }

    return res;
}


static json_object*
pinfo_device_udevice_to_jlist(  struct udev_device* udevice,
                                struct udev_list_entry*(*udevice_elist)(struct udev_device*),
                                const char*(*elist_val_get)(struct udev_device*,const char*),
                                const unsigned int jflags) {
    json_object* jobject = NULL;

    if(udevice && udevice_elist) {
        struct udev_list_entry* elist = udevice_elist(udevice);
        struct udev_list_entry* elist_head = NULL;

        jobject = json_object_new_object();
        udev_list_entry_foreach(elist_head,elist) {
            const char* skey = udev_list_entry_get_name(elist_head);
            const char* svalue = elist_val_get(udevice,skey);

            if(skey && svalue) {
                json_object_object_add_ex(jobject,skey,json_object_new_string(svalue),jflags);
            }
        }
    }

    return jobject;
}

static json_object*
pinfo_device_udevice_to_jlist_mask( struct udev_device* udevice,
                                    const char*(*udevice_val_getter)(struct udev_device*,const char*),
                                    json_object* jkeys,
                                    unsigned jcpy_flags) {
    json_object* jdev = NULL;

    if(json_object_is_type(jkeys,json_type_array)) {
        const int keys_count = json_object_array_length(jkeys);
        if(keys_count > 0) {
            int key_idx = 0;
            jdev = json_object_new_object();

            for(;key_idx < keys_count; ++key_idx) {
                const char* skey = json_object_get_string(
                        json_object_array_get_idx(jkeys,key_idx));
                const char* svalue = udevice_val_getter(udevice,skey);

                if(svalue) {
                    json_object_object_add_ex(jdev,skey,json_object_new_string(svalue),jcpy_flags);
                }
            }
        }
    }

    return jdev;
}

static json_object*
pinfo_device_udevice_to_jdevice(struct udev_device* udevice, json_object* jmask) {
    json_object *jdev = NULL;

    if(udevice) {
        json_object* jprops = NULL;
        json_object* jattrs = NULL;

        jdev = json_object_new_object();
        if(json_object_is_type(jmask,json_type_object)) {
            json_object* jprops_mask = NULL;
            json_object* jattrs_mask = NULL;

            if(json_object_object_get_ex(jmask,"properties",&jprops_mask)) {
                jprops = pinfo_device_udevice_to_jlist_mask(
                    udevice,
                    udev_device_get_property_value,
                    jprops_mask,
                    JSON_NEW_CONST_KEY
                );
            }
            if(json_object_object_get_ex(jmask,"attributes",&jattrs_mask)) {
                jattrs = pinfo_device_udevice_to_jlist_mask(
                    udevice,
                    udev_device_get_sysattr_value,
                    jattrs_mask,
                    JSON_NEW_CONST_KEY
                );
            }
        }

        if(!jprops) {
            jprops = pinfo_device_udevice_to_jlist(
                udevice,
                udev_device_get_properties_list_entry,
                udev_device_get_property_value,
                JSON_NEW_CONST_KEY
            );
        }

        if(!jattrs) {
            jattrs = pinfo_device_udevice_to_jlist(
                udevice,
                udev_device_get_sysattr_list_entry,
                udev_device_get_sysattr_value,
                JSON_NEW_CONST_KEY
            );
        }

        if(json_object_is_type(jprops,json_type_object) &&
        json_object_object_length(jprops) > 0) {
            json_object_object_add_ex(
                jdev,
                "properties",
                jprops,
                JSON_NEW_CONST_KEY
            );
        }

        if(json_object_is_type(jattrs,json_type_object) &&
        json_object_object_length(jattrs) > 0) {
            json_object_object_add_ex(
                jdev,
                "attributes",
                jattrs,
                JSON_NEW_CONST_KEY
            );
        }

        json_object_set_userdata(jdev,udevice,(json_object_delete_fn*)pinfo_device_jdev_destructor);
    }

    return jdev;
}

static void*
pinfo_device_monitor_loop(pinfo_client_ctx_t* ctx) {
    if(ctx && ctx->umon_hndl) {
        int fd;
        int res = 0;

        res = udev_monitor_enable_receiving(ctx->umon_hndl);

        if(res >= 0) {
            fd = udev_monitor_get_fd(ctx->umon_hndl);
            while (1) {
                int ret;
                fd_set fds;

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

                ret = select(fd+1, &fds, NULL, NULL, NULL);
                if(ret > 0 && FD_ISSET(fd,&fds)) {
                    struct udev_device* detected_dev = NULL;

                    detected_dev = udev_monitor_receive_device(ctx->umon_hndl);
                    if(detected_dev) {
                        json_object* jdetected_dev = NULL;
                        jdetected_dev = pinfo_device_udevice_to_jdevice(detected_dev,ctx->mask);
                        if(jdetected_dev) {
                            ctx->umon_cb(ctx,jdetected_dev);
                        }
                    }
                }
            }
        }
    }
    return NULL;
}

static void
pinfo_device_filter_scan(struct udev_enumerate* udev_enum, json_object* jfilter) {
    if(udev_enum && jfilter) {
        if(json_object_is_type(jfilter,json_type_object)) {
            json_object* jval = NULL;

			if(json_object_object_get_ex(jfilter,"tags",&jval)) {
				if(json_object_is_type(jval,json_type_array)) {
					int tag_idx = 0;
					const int tags_count = json_object_array_length(jval);

					if(tags_count > 0) {
                        json_object* jtag = NULL;

                        for(jtag = json_object_array_get_idx(jval,0);
                            jtag && tag_idx < tags_count;
                            jtag = json_object_array_get_idx(jtag,++tag_idx)) {
                            udev_enumerate_add_match_tag(udev_enum,json_object_get_string(jtag));
                        }
					} else {
						//Empty json array for tags array
					}
				} else if(json_object_is_type(jval,json_type_string)) {
					udev_enumerate_add_match_tag(udev_enum,json_object_get_string(jval));
				} else {
                    AFB_WARNING("Client passed invalid value for 'tags' field,\
                    the value type should be a json array with json string items\
                    or a json string, avoid the tags filtering");
				}
			}

			if(json_object_object_get_ex(jfilter,"properties",&jval) &&
            json_object_is_type(jval,json_type_object)) {
                if(json_object_object_length(jval) > 0) {
                    json_object_object_foreach(jval,key,value) {
                        udev_enumerate_add_match_property(udev_enum,key,json_object_get_string(value));
                    }
                }
			}

			if(json_object_object_get_ex(jfilter,"attributes",&jval) &&
            json_object_is_type(jval,json_type_object)) {
                if(json_object_object_length(jval) > 0) {
                    json_object_object_foreach(jval,key,value) {
                        udev_enumerate_add_match_sysattr(udev_enum,key,json_object_get_string(value));
                    }
                }
			}
        }
    }
}

json_object*
pinfo_device_scan(json_object *jfilter, json_object* jmask) {
    json_object* jdevs_arr = NULL;
    struct udev* udev_ctx = NULL;

    udev_ctx = udev_new();
    jdevs_arr = json_object_new_array();

    if(udev_ctx) {
        struct udev_enumerate *dev_enum = NULL;

        dev_enum = udev_enumerate_new(udev_ctx);
        if(dev_enum) {
            struct udev_list_entry *dev_elist = NULL;
            struct udev_list_entry *dev_elist_head = NULL;
            pinfo_device_filter_scan(dev_enum,jfilter);

            udev_enumerate_scan_devices(dev_enum);
            dev_elist = udev_enumerate_get_list_entry(dev_enum);
            if(dev_elist) {
                struct udev_device *udevice = NULL;
                struct json_object *jdevice = NULL;
                int udev_num = 0;
                int jdev_num = 0;

                udev_list_entry_foreach(dev_elist_head,dev_elist) {
                    const char* path = udev_list_entry_get_name(dev_elist_head);
                    udev_num++;
                    udevice = udev_device_new_from_syspath((struct udev*)udev_ctx,path);
                    jdevice = pinfo_device_udevice_to_jdevice(udevice,jmask);
                    if(jdevice) {
                        json_object_array_add(jdevs_arr,jdevice);
                        jdev_num++;
                    } else {
                        udev_device_unref(udevice);
                    }
                }
                AFB_INFO("[SCAN]: %d device detected, %d device info reported back.",udev_num,jdev_num);
            } else {
                AFB_WARNING("No device found or enumeration failed");
            }
        } else {
            AFB_DEBUG("Unable to allocate enumeration object.");
        }
        udev_enumerate_unref(dev_enum);
    }

    return jdevs_arr;
}