summaryrefslogtreecommitdiffstats
path: root/binding/iiodevices-binding.c
blob: 516b172e2d89f1c596827443013ed99c5f23dc55 (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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
 * Copyright (C) 2019 "IoT.bzh"
 * Author "Clément Bénier" <clement.benier@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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <systemd/sd-event.h>
#include <time.h>
#include <limits.h>

#include <json-c/json.h>
#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>

#include <iio.h>

#include "config_iiodevices.h"

/*structure in order to handle channels connections*/
struct channels {
    struct iio_channel *chn;
    enum iio_elements iioelts;
    char name[PATH_MAX];
    struct channels *next;
};

/*handle client subscription*/
struct client_sub {
    struct channels *channels;
    int index;
    int fd;
    uint64_t u_period;
    enum iio_elements iioelts;
    struct iio_device *dev;
    afb_event_t *event;
    struct iio_info *infos;
    json_object *jobject;
    struct client_sub *next;
};

/*gather all afb_event_t*/
struct event
{
    struct event *next;
    afb_event_t event;
    char tag[PATH_MAX];
};

/*events*/
static struct event *events = 0;
/*iio context*/
static struct iio_context *ctx = NULL;
/*clients*/
static struct client_sub * clients = NULL;
/*save last registered client*/
static struct client_sub * last_client = NULL;

/* Static definition of supported iiodevices */
static struct iio_info iio_infos[] = {
 { "16-001d", "acceleration", "accel"},
 { "16-001d", "gyroscope", "magn"},
 { "16-006b", "compass", "anglvel"},
 { "asm330lhh_accel", "acceleration", "accel"},
 { "asm330lhh_gyro", "gyroscope", "anglvel"},
};

/*get event by afb_event_t*/
static struct event *event_get_event(const afb_event_t *event)
{
    struct event *e = events;
    while(&e->event != event) {
        e = e->next;
    }
    return e;
}

/* searchs the event of tag */
static struct event *event_get(const char *tag)
{
	struct event *e = events;
	while(e && strcmp(e->tag, tag))
		e = e->next;
	return e;
}

/* creates the event of tag */
static afb_event_t* event_add(const char *tag)
{
	struct event *e;

	/* check valid tag */
	e = event_get(tag);
	if (e) return &e->event;

	/* creation */
	e = malloc(strlen(tag) + sizeof *e);
	if (!e) return NULL;
	strncpy(e->tag, tag, PATH_MAX);

	/* make the event */
	e->event = afb_api_make_event(afbBindingV3root, tag);
	if (!afb_event_is_valid(e->event)) { free(e); return NULL; }

	/* link */
	e->next = events;
	events = e;
	return &e->event;
}

/* deletes the event of event */
static int event_del(afb_event_t *event)
{
	struct event *e, **p;

	/* check exists */
    e = event_get_event(event);
    if (!e) return -1;

    /* unlink */
    p = &events;
    while(*p != e) p = &(*p)->next;
    *p = e->next;

    /* destroys */
    afb_event_unref(e->event);
    free(e);
	return 0;
}

/*initialise iio context*/
int init_context()
{
    ctx = iio_create_local_context();
    if(!ctx)
    {
        AFB_API_ERROR(afbBindingV3root, "cannot create local iio context");
        return -1;
    }
    return 0;
}

/*initialise device: find by its name*/
static void init_dev(struct client_sub *client)
{
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "client is null");
        return;
    }

    AFB_API_DEBUG(afbBindingV3root, "iio_context_find_device %s", client->infos->dev_name);
    client->dev =  iio_context_find_device(ctx, client->infos->dev_name);
    if(!client->dev) {
        AFB_API_ERROR(afbBindingV3root, "No %s device found", client->infos->dev_name);
        return;
    }
}

/*read static infos from channels*/
static int read_infos(struct client_sub* client)
{
    if(!client || !client->channels || !client->channels->chn) {
        AFB_API_ERROR(afbBindingV3root, "client or client->channels or client->channels->chn is null");
        return -1;
    }
    json_object *jobject = NULL;
    size_t size = 1000;
    char val[size];
    char keyfirst[size];

    struct channels *channel = client->channels;
    //read all infos
    while(channel) {
        for(int i = 0; i < iio_channel_get_attrs_count(channel->chn); i++) {
            const char *id_attr = iio_channel_get_attr(channel->chn, i);
            if(!id_attr) {
                AFB_API_ERROR(afbBindingV3root, "cannot get attr from channel %d", i);
                return -1;
            }
            if(strcasecmp(id_attr, "raw")) { //do not take raw infos
                iio_channel_attr_read(channel->chn, id_attr, val, 100);

                //first channel
                if(client->channels == channel) {
                    json_object *value = json_object_new_string(val);
                    json_object_object_add(client->jobject, id_attr, value);
                }
                else { //other channels: keep same value and stored other values
                    /*no id_attr: looking for an already changed id*/
                    if(!json_object_object_get_ex(client->jobject, id_attr, &jobject))
                    {
                        strncpy(keyfirst, id_attr, size);
                        set_channel_name(keyfirst, client->channels->iioelts);
                        if(!json_object_object_get_ex(client->jobject, keyfirst, &jobject)) {
                            AFB_API_WARNING(afbBindingV3root, "cannot find %s keyfirst in json object",
                                    keyfirst);
                        }
                    }

                    const char * valfisrt = json_object_get_string(jobject); //get first value channel
                    if(strcasecmp(val, valfisrt)) { //different value store it
                        char key[size];
                        strncpy(key, id_attr, size);
                        set_channel_name(key, channel->iioelts); //add channel suffix
                        json_object *value = json_object_new_string(val);
                        json_object_object_add(client->jobject, key, value);

                        //remove previous non unique key and replace it with suffix
                        strncpy(keyfirst, id_attr, size);
                        set_channel_name(keyfirst, client->channels->iioelts);
                        json_object *valuefirst = json_object_new_string(valfisrt);
                        json_object_object_add(client->jobject, keyfirst, valuefirst);
                        json_object_object_del(client->jobject, id_attr);
                    }
                }
            }
        }
        channel = channel->next;
    }
    return 0;
}

/*close file descriptor*/
static void close_fd(struct client_sub *client)
{
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "client is null pointer");
        return;
    }

    if(client->fd >= 0)
        close(client->fd);
}

/*deinit client channels*/
static void deinit_channels(struct client_sub *client)
{
    if(!client->channels) {
        AFB_API_ERROR(afbBindingV3root, "client->channels is null pointer");
        return;
    }

    struct channels *it_chn = client->channels;
    while(it_chn) {
        struct channels *tmp = it_chn->next;
        AFB_API_DEBUG(afbBindingV3root, "free it_chn=%s", it_chn->name);
        free(it_chn);
        it_chn = tmp;
    }
    client->channels = NULL;
}

/*looking for previous client: if client is first returns client*/
static struct client_sub* looking_for_previous(struct client_sub *client)
{
    if(!client || !clients) {
        AFB_API_ERROR(afbBindingV3root, "client or clients is null pointer");
        return NULL;
    }
    struct client_sub* prev = clients;
    while(prev->next != client) {
        prev = prev->next;
    }
    return prev;
}

/*deinit client sub*/
static void deinit_client_sub(struct client_sub *client)
{
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "client is null pointer");
        return;
    }

    struct client_sub *prev_client;
    if(client == clients) {//first one
        clients = client->next;
    }
    else {
        prev_client = looking_for_previous(client);
        if(!prev_client) {
            AFB_API_ERROR(afbBindingV3root, "cannot find previous client");
            exit(-1);
        }
        prev_client->next = client->next;
    }
    AFB_API_DEBUG(afbBindingV3root, "free client for %s", client->infos->id);
    free(client);
}

/*desallocate channels*/
static void desallocate_channels(sd_event_source* src,
        struct client_sub *client)
{
    afb_event_unref(*client->event);
    if(src) {
        sd_event_source_set_enabled(src, SD_EVENT_OFF);
        sd_event_source_unref(src);
    }
    close_fd(client);
    event_del(client->event);
    deinit_channels(client);
    deinit_client_sub(client);
}

/*!read data from channels*/
static int read_data(struct client_sub *client, sd_event_source* src)
{
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "client is null");
        return -1;
    }

    char val[10];
    struct channels *channel = client->channels;
    while(channel) { //iterate on client channels
        if(!channel->chn) {
            AFB_API_ERROR(afbBindingV3root, "chn is null for cl_chn=%s", channel->name);
            return -1;
        }

        iio_channel_attr_read(channel->chn, "raw", val, 10);
        int data = (int)strtol(val, NULL, 10);
        AFB_API_DEBUG(afbBindingV3root, "read_data: %s %d", iio_channel_get_id(channel->chn), data);

        json_object *value = json_object_new_int(data);
        json_object_object_add(client->jobject, channel->name, value);
        channel = channel->next;
    }

    /*if no more subscribers -> desallocate*/
    int nb_subscribers = afb_event_push(*client->event, json_object_get(client->jobject));
    AFB_API_DEBUG(afbBindingV3root, "nb_subscribers for %s is %d", afb_event_name(*client->event), nb_subscribers);
    if(nb_subscribers <= 0)
        desallocate_channels(src, client);
    return 0;
}

/*read data from file descriptor*/
static int read_data_push(sd_event_source* src, int fd, uint32_t revents, void* userdata)
{
    struct client_sub * client= (struct client_sub *)userdata;
    read_data(client, src);
    return 0;
}

/*read data from timer*/
static int read_data_timer(sd_event_source* src, uint64_t usec, void *userdata)
{
    struct client_sub *client = (struct client_sub *)userdata;

    /*set next time to trigger*/
    uint64_t usecs;
    sd_event_now(afb_api_get_event_loop(afbBindingV3root), CLOCK_MONOTONIC, &usecs);
    usecs += client->u_period;
    sd_event_source_set_time(src, usecs);

    read_data(client, src);
    return 0;
}

/*init event io: depending on the frequency, if no filled, triggered on file
 * descriptor */
static void init_event_io(struct client_sub *client)
{
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "client is null");
        return;
    }
    if(!client->channels) {
        AFB_API_ERROR(afbBindingV3root, "channel is null");
        return;
    }
    read_infos(client); //get unchanged infos
    sd_event_source *source = NULL;
    if(client->u_period <= 0) { //no given frequency
        char filename[PATH_MAX];
        snprintf(filename, PATH_MAX,
                IIODEVICE"%s/%s", iio_device_get_id(client->dev),
                iio_channel_attr_get_filename(client->channels->chn, "raw"));
        if((client->fd = open(filename, O_RDONLY)) < 0) {
            AFB_API_ERROR(afbBindingV3root, "cannot open %s file", filename);
            return;
        }

        sd_event_add_io(afb_api_get_event_loop(afbBindingV3root), &source, client->fd, EPOLLIN, read_data_push, client);
    }
    else { //frequency specified
        sd_event_add_time(afb_api_get_event_loop(afbBindingV3root), &source, CLOCK_MONOTONIC, 0, 1, read_data_timer, client);
        sd_event_source_set_enabled(source, SD_EVENT_ON);
    }
}

/*initialise client channel*/
static struct channels* set_channel(
        struct client_sub* client, struct channels *prev_chn, const enum iio_elements i)
{
    prev_chn->next = malloc(sizeof(struct channels));
    if(!prev_chn->next) {
        AFB_API_ERROR(afbBindingV3root, "alloc failed for channels");
        return NULL;
    }

    struct channels *chn = prev_chn->next;
    chn->next = NULL;
    chn->iioelts = i;

    /*set channel name with iio_elements*/
    strncpy(chn->name, client->infos->middlename, PATH_MAX);
    set_channel_name(chn->name, i);

    if(!(chn->chn = iio_device_find_channel(client->dev, chn->name, false))) {
        AFB_API_ERROR(afbBindingV3root, "cannot find %s channel", chn->name);
        free(chn);
        prev_chn->next = NULL;
        return prev_chn;
    }
    iio_channel_enable(chn->chn);
    return chn;
}

static void init_channel(struct client_sub* client, const unsigned int iioelts)
{
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "client is null");
        return;
    }
    AFB_API_DEBUG(afbBindingV3root, "size of channels=%d", get_iio_nb(iioelts));

    /*looking for the last channel: could be the first*/
    struct channels pre_index;
    pre_index.next = client->channels;
    struct channels *it_chn = &pre_index;
    while(it_chn->next) {
        it_chn = it_chn->next;
    }

    /*set channel*/
    for(int i = 1; i <= iioelts; i = i << 1) { //iterate on enumerations
        if(i == (i & iioelts)) {
            it_chn = set_channel(client, it_chn, i);
        }
    }
    client->channels = pre_index.next;
}

/*get period in microseconds from a frequency*/
static uint64_t get_period(const char* freq)
{
    double frequency = 0;
    if(freq)
        frequency = strtod(freq, NULL);
    return (frequency <= 0) ? 0 : (uint64_t)((1.0 / frequency) * 1000000);
}

/*check if it is needed to create a new client: if NULL return it is needed,
 * else it is no needed but can be needed to add channels if rest_iioelts > 0*/
static struct client_sub* is_new_client_sub_needed(struct iio_info* infos,
        uint64_t u_period, enum iio_elements *rest_iioelts)
{
    struct client_sub *it_client = clients;
    while(it_client) {
            if(it_client->infos == infos && u_period == it_client->u_period) {
                AFB_API_DEBUG(afbBindingV3root, "a client is matching");
                *rest_iioelts = ~it_client->iioelts & *rest_iioelts;
                it_client->iioelts |= *rest_iioelts;
                return it_client;
            }
            it_client = it_client->next;
    }
    return NULL;
}

/*add new client with new event*/
static struct client_sub *add_new_client(struct iio_info *infos,
        const enum iio_elements iioelts, const uint64_t u_period)
{
    struct client_sub *client = malloc(sizeof(struct client_sub));
    if(!client) {
        AFB_API_ERROR(afbBindingV3root, "cannot allocate client");
        return NULL;
    }
    //initialise client
    if(!clients) {
        clients = client;
        client->index = 0;
    }
    else {
        if(!last_client) {
            AFB_API_ERROR(afbBindingV3root, "last_client should not be null");
            return NULL;
        }
        last_client->next = client;
        client->index = last_client->index + 1;
    }

    char event_name[100];
    sprintf(event_name, "%s%d", infos->id, client->index);
    AFB_API_DEBUG(afbBindingV3root, "add_new_client with event_name=%s", event_name);

    client->channels = NULL;
    client->jobject = json_object_new_object();
    client->iioelts = iioelts;
    client->u_period = u_period;
    client->infos = infos;
    client->fd = -1;
    client->next = NULL;
    client->dev = NULL;
    client->event = event_add(event_name);

    last_client = client;
    return client;
}

/*subscribe verb*/
static void subscribe(afb_req_t request)
{
    init_context();
    const char *value = afb_req_value(request, "event");
    const char *s_iioelts = afb_req_value(request, "args");
    if(!s_iioelts) {
        afb_req_fail(request, "failed", "args is null");
        return;
    }
    const char *freq = afb_req_value(request, "frequency");

    if(!value || !s_iioelts) {
        afb_req_fail(request, "failed", "please, fill 'event' and 'args' fields");
        return;
    }

    AFB_API_INFO(afbBindingV3root, "subscription with: value=%s, s_iioelts=%s, freq=%s",
            value, s_iioelts, freq);

    bool found = false;
    for(int i = 0; i < sizeof(iio_infos)/sizeof(iio_infos[0]); i++) {
        if(!strcasecmp(value, iio_infos[i].id)) {
            found = true;

            uint64_t u_period = get_period(freq);
            enum iio_elements iioelts = (int)treat_iio_elts(s_iioelts);
            if(iioelts == 0){
                afb_req_fail(request, "failed", "wrong args");
                return;
            }
            struct client_sub* client = is_new_client_sub_needed(&iio_infos[i],
                    u_period, &iioelts);
            if(!client || iioelts > 0) { //no client found or new channels
                if(!client)
                    client = add_new_client(&iio_infos[i], iioelts, u_period);
                init_dev(client);
                if(!client->dev) {
                    afb_req_fail_f(request, "failed", "No %s device found", client->infos->dev_name);
                    deinit_client_sub(client);
                    return;
                }
                init_channel(client, iioelts);
                if(!client->channels) {
                    afb_req_fail(request, "failed", "No channels found");
                    return;
                }
                init_event_io(client);
            } else {
                init_dev(client);
            }
            //stored client in order to be get in unsubscription
            afb_req_context_set(request, client, NULL);

            if(!client->event) {
                    afb_req_fail(request, "failed", "No event found");
                    return;
            }
            if(afb_req_subscribe(request, *client->event) != 0) {
                afb_req_fail(request, "failed", "subscription failed");
                return;
            }
        }
    }
    if(!found) {
        afb_req_fail(request, "failed", "Invalid event");
        return;
    }
    afb_req_success(request, NULL, NULL);
}

/*unsubscribe verb*/
static void unsubscribe(afb_req_t request)
{
    init_context();
    const char *value = afb_req_value(request, "event");
    if(!value) {
        afb_req_fail(request, "failed", "please, fill 'event' fields");
        return;
    }

    bool found = false;
    if(value) {
        for(int i = 0; i < sizeof(iio_infos)/sizeof(iio_infos[0]); i++) {
            if(!strcasecmp(value, iio_infos[i].id)) {
                found = true;
                struct client_sub *client
                    = (struct client_sub *)afb_req_context_get(request);
                if(!client) {
                    AFB_API_ERROR(afbBindingV3root, "cannot find %s event, it seems that there was \
                            no subscription", value);
                    afb_req_fail_f(request, "failed", "cannot find %s event, it seems that there was \
                            no subscription", value);
                    return;
                }
                if(afb_req_unsubscribe(request, *client->event) != 0) {
                    afb_req_fail(request, "failed", "unsubscription failed");
                    return;
                }
            }
        }
        if(!found) {
            afb_req_fail(request, "failed", "Invalid event");
            return;
        }
    }
    afb_req_success(request, NULL, NULL);
}

/*list of api verbs*/
const afb_verb_t verbs[] = {
	    { .verb = "subscribe",		.session = AFB_SESSION_NONE, .callback = subscribe,		.info = "Subscribe for an event" },
	    { .verb = "unsubscribe",	.session = AFB_SESSION_NONE, .callback = unsubscribe,		.info = "Unsubscribe for an event" },
        { .verb=NULL }
};

/*binding configuration*/
const afb_binding_t afbBindingExport = {
        .info = "iio devices service",
        .api = "iiodevices",
        .verbs = verbs
};