add pa_ prefix to all identifiers.
[profile/ivi/pulseaudio.git] / src / pdispatch.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "pdispatch.h"
5 #include "protocol-native-spec.h"
6
7 struct reply_info {
8     struct pa_pdispatch *pdispatch;
9     struct reply_info *next, *previous;
10     int (*callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
11     void *userdata;
12     uint32_t tag;
13     void *mainloop_timeout;
14 };
15
16 struct pa_pdispatch {
17     struct pa_mainloop_api *mainloop;
18     const struct pa_pdispatch_command *command_table;
19     unsigned n_commands;
20     struct reply_info *replies;
21 };
22
23 static void reply_info_free(struct reply_info *r) {
24     assert(r && r->pdispatch && r->pdispatch->mainloop);
25
26     if (r->pdispatch)
27         r->pdispatch->mainloop->cancel_time(r->pdispatch->mainloop, r->mainloop_timeout);
28
29     if (r->previous)
30         r->previous->next = r->next;
31     else
32         r->pdispatch->replies = r->next;
33
34     if (r->next)
35         r->next->previous = r->previous;
36     
37     free(r);
38 }
39
40 struct pa_pdispatch* pa_pdispatch_new(struct pa_mainloop_api *mainloop, const struct pa_pdispatch_command*table, unsigned entries) {
41     struct pa_pdispatch *pd;
42     assert(mainloop);
43
44     assert((entries && table) || (!entries && !table));
45     
46     pd = malloc(sizeof(struct pa_pdispatch));
47     assert(pd);
48     pd->mainloop = mainloop;
49     pd->command_table = table;
50     pd->n_commands = entries;
51     pd->replies = NULL;
52     return pd;
53 }
54
55 void pa_pdispatch_free(struct pa_pdispatch *pd) {
56     assert(pd);
57     while (pd->replies)
58         reply_info_free(pd->replies);
59     free(pd);
60 }
61
62 int pa_pdispatch_run(struct pa_pdispatch *pd, struct pa_packet*packet, void *userdata) {
63     uint32_t tag, command;
64     assert(pd && packet);
65     struct pa_tagstruct *ts = NULL;
66     assert(pd && packet && packet->data);
67
68     if (packet->length <= 8)
69         goto fail;
70
71     ts = pa_tagstruct_new(packet->data, packet->length);
72     assert(ts);
73     
74     if (pa_tagstruct_getu32(ts, &command) < 0 ||
75         pa_tagstruct_getu32(ts, &tag) < 0)
76         goto fail;
77
78     if (command == PA_COMMAND_ERROR || command == PA_COMMAND_REPLY) {
79         struct reply_info *r;
80         int done = 0;
81
82         for (r = pd->replies; r; r = r->next) {
83             if (r->tag == tag) {
84                 int ret = r->callback(r->pdispatch, command, tag, ts, r->userdata);
85                 reply_info_free(r);
86                 
87                 if (ret < 0)
88                     goto fail;
89                 
90                 done = 1;
91                 break;
92             }
93         }
94
95         if (!done)
96             goto fail;
97
98     } else if (pd->command_table && command < pd->n_commands) {
99         const struct pa_pdispatch_command *c = pd->command_table+command;
100
101         if (!c->proc)
102             goto fail;
103         
104         if (c->proc(pd, command, tag, ts, userdata) < 0)
105             goto fail;
106     } else
107         goto fail;
108     
109     pa_tagstruct_free(ts);    
110         
111     return 0;
112
113 fail:
114     if (ts)
115         pa_tagstruct_free(ts);    
116
117     return -1;
118 }
119
120 static void timeout_callback(struct pa_mainloop_api*m, void *id, const struct timeval *tv, void *userdata) {
121     struct reply_info*r = userdata;
122     assert (r && r->mainloop_timeout == id && r->pdispatch && r->pdispatch->mainloop == m && r->callback);
123
124     r->callback(r->pdispatch, PA_COMMAND_TIMEOUT, r->tag, NULL, r->userdata);
125     reply_info_free(r);
126 }
127
128 void pa_pdispatch_register_reply(struct pa_pdispatch *pd, uint32_t tag, int timeout, int (*cb)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata), void *userdata) {
129     struct reply_info *r;
130     struct timeval tv;
131     assert(pd && cb);
132
133     r = malloc(sizeof(struct reply_info));
134     assert(r);
135     r->pdispatch = pd;
136     r->callback = cb;
137     r->userdata = userdata;
138     r->tag = tag;
139
140     gettimeofday(&tv, NULL);
141     tv.tv_sec += timeout;
142
143     r->mainloop_timeout = pd->mainloop->source_time(pd->mainloop, &tv, timeout_callback, r);
144     assert(r->mainloop_timeout);
145
146     r->previous = NULL;
147     r->next = pd->replies;
148     if (r->next)
149         r->next->previous = r;
150     pd->replies = r;
151 }