router: fix for bluetooth disappearing causing assert in combine
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / node.c
1 #include <stdio.h>
2
3 #include <pulsecore/pulsecore-config.h>
4
5 #include <pulsecore/hashmap.h>
6 #include <pulsecore/idxset.h>
7 #include <pulsecore/strbuf.h>
8
9
10 #include "node.h"
11 #include "discover.h"
12 #include "router.h"
13
14 struct pa_nodeset {
15     pa_idxset *nodes;
16 };
17
18
19 pa_nodeset *pa_nodeset_init(struct userdata *u)
20 {
21     pa_nodeset *ns;
22
23     pa_assert(u);
24
25     ns = pa_xnew0(pa_nodeset, 1);
26     ns->nodes = pa_idxset_new(pa_idxset_trivial_hash_func,
27                               pa_idxset_trivial_compare_func);
28     return ns;
29 }
30
31 void pa_nodeset_done(struct userdata *u)
32 {
33     pa_nodeset *ns;
34
35     if (u && (ns = u->nodeset)) {
36         pa_idxset_free(ns->nodes, NULL, NULL);
37         free(ns);
38     }    
39 }
40
41
42 mir_node *mir_node_create(struct userdata *u, mir_node *data)
43 {
44     pa_nodeset *ns;
45     mir_node *node;
46
47     pa_assert(u);
48     pa_assert(data);
49     pa_assert_se((ns = u->nodeset));
50     pa_assert(data->key);
51     pa_assert(data->paname);
52     
53     node = pa_xnew0(mir_node, 1);
54
55     node->key       = pa_xstrdup(data->key);
56     node->direction = data->direction;
57     node->implement = data->implement;
58     node->channels  = data->channels;
59     node->location  = data->location;
60     node->privacy   = data->privacy;
61     node->type      = data->type;
62     node->visible   = data->visible;
63     node->available = data->available;
64     node->amname    = pa_xstrdup(data->amname ? data->amname : data->paname);
65     node->amdescr   = pa_xstrdup(data->amdescr ? data->amdescr : "");
66     node->amid      = data->amid;
67     node->paname    = pa_xstrdup(data->paname);
68     node->paidx     = data->paidx;
69     node->mux       = data->mux;
70     node->stamp     = data->stamp;
71     MIR_DLIST_INIT(node->rtentries);
72     
73     if (node->implement == mir_device) {
74         node->pacard.index   = data->pacard.index;
75         if (data->pacard.profile)
76             node->pacard.profile = pa_xstrdup(data->pacard.profile);
77         if (data->paport)
78             node->paport = pa_xstrdup(data->paport);
79     }
80
81     pa_idxset_put(ns->nodes, node, &node->index);
82
83     mir_router_register_node(u, node);
84     
85     return node;
86 }
87
88 void mir_node_destroy(struct userdata *u, mir_node *node)
89 {
90     pa_nodeset *ns;
91
92     pa_assert(u);
93     pa_assert_se((ns = u->nodeset));
94
95     if (node) {
96         mir_router_unregister_node(u, node);
97
98         pa_idxset_remove_by_index(ns->nodes, node->index);
99
100         pa_xfree(node->key);
101         pa_xfree(node->amname);
102         pa_xfree(node->amdescr);
103         pa_xfree(node->paname);
104         pa_xfree(node->pacard.profile);
105         pa_xfree(node->paport);
106
107         pa_xfree(node);
108     }
109 }
110
111 mir_node *mir_node_find_by_index(struct userdata *u, uint32_t nodidx)
112 {
113     pa_nodeset *ns;
114     mir_node *node;
115
116     pa_assert(u);
117     pa_assert_se((ns = u->nodeset));
118
119     node = pa_idxset_get_by_index(ns->nodes, nodidx);
120
121     return node;
122 }
123
124 int mir_node_print(mir_node *node, char *buf, int len)
125 {
126     char *p, *e;
127     char mux[256];
128
129     pa_assert(node);
130     pa_assert(buf);
131     pa_assert(len > 0);
132
133     pa_multiplex_print(node->mux, mux, sizeof(mux));
134
135     e = (p = buf) + len;
136
137 #define PRINT(f,v) if (p < e) p += snprintf(p, e-p, f "\n", v)
138
139     PRINT("   index         : %u"  ,  node->index);
140     PRINT("   key           : '%s'",  node->key ? node->key : "");
141     PRINT("   direction     : %s"  ,  mir_direction_str(node->direction));
142     PRINT("   implement     : %s"  ,  mir_implement_str(node->implement));
143     PRINT("   channels      : %u"  ,  node->channels);
144     PRINT("   location      : %s"  ,  mir_location_str(node->location));
145     PRINT("   privacy       : %s"  ,  mir_privacy_str(node->privacy));
146     PRINT("   type          : %s"  ,  mir_node_type_str(node->type));
147     PRINT("   visible       : %s"  ,  node->visible ? "yes" : "no");
148     PRINT("   available     : %s"  ,  node->available ? "yes" : "no");
149     PRINT("   amname        : '%s'",  node->amname ? node->amname : "");
150     PRINT("   amdescr       : '%s'",  node->amdescr ? node->amdescr : "");
151     PRINT("   amid          : %u"  ,  node->amid);
152     PRINT("   paname        : '%s'",  node->paname ? node->paname : "");
153     PRINT("   paidx         : %u"  ,  node->paidx);
154     PRINT("   pacard.index  : %u"  ,  node->pacard.index);
155     PRINT("   pacard.profile: '%s'",  node->pacard.profile ?
156                                       node->pacard.profile : "");
157     PRINT("   paport        : '%s'",  node->paport ? node->paport : "");
158     PRINT("   mux           : %s"  ,  mux);
159     PRINT("   stamp         : %u"  ,  node->stamp);
160
161 #undef PRINT
162
163     return p - buf;
164 }
165
166 const char *mir_direction_str(mir_direction direction)
167 {
168     switch (direction) {
169     case mir_direction_unknown:  return "unknown";
170     case mir_input:              return "input";
171     case mir_output:             return "output";
172     default:                     return "< ??? >";
173     }
174 }
175
176 const char *mir_implement_str(mir_implement implement)
177 {
178     switch (implement) {
179     case mir_implementation_unknown:  return "unknown";
180     case mir_device:                  return "device";
181     case mir_stream:                  return "stream";
182     default:                          return "< ??? >";
183     }
184 }
185
186 const char *mir_location_str(mir_location location)
187 {
188     switch (location) {
189     case mir_location_unknown:  return "unknown";
190     case mir_internal:          return "internal";
191     case mir_external:          return "external";
192     default:                    return "< ??? >";
193     }
194
195
196
197 const char *mir_node_type_str(mir_node_type type)
198 {
199     switch (type) {
200     case mir_node_type_unknown:   return "Unknown";
201     case mir_radio:               return "Radio";
202     case mir_player:              return "Player";
203     case mir_navigator:           return "Navigator";
204     case mir_game:                return "Game";
205     case mir_browser:             return "Browser";
206     case mir_phone:               return "Phone";
207     case mir_event:               return "Event";
208     case mir_speakers:            return "Speakers";
209     case mir_microphone:          return "Microphone";
210     case mir_jack:                return "Line";
211     case mir_spdif:               return "SPDIF";
212     case mir_hdmi:                return "HDMI";
213     case mir_wired_headset:       return "Wired Headset";
214     case mir_wired_headphone:     return "Wired Headphone";
215     case mir_usb_headset:         return "USB Headset";
216     case mir_bluetooth_sco:       return "Bluetooth Handsfree";
217     case mir_bluetooth_a2dp:      return "Bluetooth Stereo";
218     default:                      return "<user defined>";
219     }
220 }
221
222
223 const char *mir_privacy_str(mir_privacy privacy)
224 {
225     switch (privacy) {
226     case mir_privacy_unknown:  return "<unknown>";
227     case mir_public:           return "public";
228     case mir_private:          return "private";
229     default:                   return "< ??? >";
230     }
231 }
232
233                                   
234 /*
235  * Local Variables:
236  * c-basic-offset: 4
237  * indent-tabs-mode: nil
238  * End:
239  *
240  */