summaryrefslogtreecommitdiffstats
path: root/src/dtmffifo.c
blob: 54a1f620836bcc6d71e85d2dbe6a7a386f773285 (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
#include <unistd.h>
#include <libdaemon/dlog.h>
#include <limits.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>

#include "dtmffifo.h"

struct dtmf_fifo* dtmf_fifo_new(void) {
    struct dtmf_fifo *d = NULL;
    char p[PATH_MAX];

    d = malloc(sizeof(struct dtmf_fifo));
    assert(d);
    memset(d, 0, sizeof(struct dtmf_fifo));
    d->fd = -1;

    d->dir = strdup("/tmp/ivamd.XXXXXX");
    assert(d->dir);

    if (!mkdtemp(d->dir)) {
        daemon_log(LOG_ERR, "Failed to create temporary directory '%s': %s", d->dir, strerror(errno));
        goto fail;
    }

    snprintf(p, sizeof(p), "%s/%s", d->dir, "dtmf");
    d->fname = strdup(p);
    assert(d->fname);

    if (mkfifo(d->fname, 0700) != 0) {
        daemon_log(LOG_ERR, "Failed to create FIFO '%s': %s", d->fname, strerror(errno));
        goto fail;
    }
    
    if ((d->fd = open(d->fname, O_RDWR|O_NDELAY)) < 0) {
        daemon_log(LOG_ERR, "Failed to open FIFO '%s': %s", d->fname, strerror(errno));
        goto fail;
    }
        
    daemon_log(LOG_INFO, "Sucessfully opened DTMF FIFO '%s'.", d->fname);
    
    return d;

fail:
    if (d)
        dtmf_fifo_free(d);

    return NULL;
}

void dtmf_fifo_free(struct dtmf_fifo *d) {
    assert(d);

    if (d->fd >= 0)
        close(d->fd);

    if (d->fname) {
        unlink(d->fname);
        free(d->fname);
    }
    
    if (d->dir) {
        rmdir(d->dir);
        free(d->dir);
    }
    
    free(d);
}

void dtmf_fifo_pass(struct dtmf_fifo *d, char c) {
    assert(d && d->fd >= 0);
    
    daemon_log(LOG_INFO, "Recieved DTMF character '%c'", c);

    if (write(d->fd, &c, 1) != 1)
        daemon_log(LOG_ERR, "Failed to write to DTMF FIFO: %s", strerror(errno));
}