aboutsummaryrefslogtreecommitdiffstats
path: root/Controler-afb/ctl-events.c
blob: f4448481952ff2a05780e2119aba33671398b545 (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
/*
 * Copyright (C) 2016 "IoT.bzh"
 * Author Fulup Ar Foll <fulup@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.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <systemd/sd-event.h>

#include "ctl-binding.h"

static afb_event afbevt;

STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) {
    TimerHandleT *timerHandle = (TimerHandleT*) handle;
    int done;
    uint64_t usec;

    // Rearm timer if needed
    timerHandle->count --;
    if (timerHandle->count == 0) sd_event_source_unref(source);
    else {
        // otherwise validate timer for a new run    
        sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec);
        sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
        sd_event_source_set_time(source, usec + timerHandle->delay);
    }

    done= timerHandle->callback(timerHandle->context);
    if (!done) goto OnErrorExit;
    
    return 0;

OnErrorExit:
    AFB_WARNING("TimerNext Callback Fail Tag=%s", timerHandle->context->label);
    return -1;  
}


STATIC int DoSendEvent (void *context) {
    AutoTestCtxT *ctx= (AutoTestCtxT*)context;
    json_object *ctlEventJ;
    
    if (ctx->value) ctx->value =0;
    else ctx->value =1;
    
    ctlEventJ = json_object_new_object();
    json_object_object_add(ctlEventJ,"action", json_object_new_string(ctx->label));
    json_object_object_add(ctlEventJ,"value" , json_object_new_int(ctx->value));
    int done = afb_event_push(afbevt, ctlEventJ);
    
    AFB_NOTICE ("DoSendEvent {action: '%s', value:%d} status=%d", ctx->label, ctx->value, done);
    
    return (done);
}

STATIC void TimerEvtStart(TimerHandleT *timerHandle, void *context) {
    uint64_t usec;
    
    // populate CB handle
    timerHandle->callback=DoSendEvent;
    timerHandle->context=context;
    
    // set a timer with ~250us accuracy 
    sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec);
    sd_event_add_time(afb_daemon_get_event_loop(), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay, 250, TimerNext, timerHandle);
}

PUBLIC afb_event TimerEvtGet(void) {
    return afbevt;
}


// Generated some fake event based on watchdog/counter
PUBLIC void ctlapi_event_test (afb_req request) {
    json_object *queryJ, *tmpJ;
    TimerHandleT *timerHandle = malloc (sizeof (TimerHandleT));
    AutoTestCtxT *context = calloc (1, sizeof (AutoTestCtxT));
    int done;
       
    queryJ= afb_req_json(request);
    
    // Closing call only has one parameter
    done=json_object_object_get_ex(queryJ, "closing", &tmpJ);
    if (done) return;
    
    done=json_object_object_get_ex(queryJ, "label", &tmpJ);
    if (!done) {
         afb_req_fail_f(request, "TEST-LABEL-MISSING", "label is mandatory for event_test");
        goto OnErrorExit;
    }
    context->label = strdup(json_object_get_string (tmpJ));
    
    json_object_object_get_ex(queryJ, "delay", &tmpJ);
    timerHandle->delay = json_object_get_int (tmpJ) * 1000;
    if (timerHandle->delay == 0) timerHandle->delay=DEFAULT_PAUSE_DELAY * 1000;
    
    json_object_object_get_ex(queryJ, "count", &tmpJ);
    timerHandle->count = json_object_get_int (tmpJ);
    if (timerHandle->count == 0) timerHandle->count=DEFAULT_TEST_COUNT;
    
    // start a lool timer  
    TimerEvtStart (timerHandle, context);
    
    afb_req_success(request, NULL, NULL);
    return;
    
 OnErrorExit:    
    return;
}

// Create Binding Event at Init
PUBLIC int TimerEvtInit () {
    
    // create binder event to send test pause/resume
    afbevt = afb_daemon_make_event("request");
    if (!afb_event_is_valid(afbevt)) {
        AFB_ERROR ("POLCTL_INIT: Cannot register ctl-events");
        return 1;
    }
    
    AFB_DEBUG ("Audio Control-Events Init Done");
    return 0;
}