Changeset 1df3018a in mainline for uspace/srv/audio/hound/hound.c
- Timestamp:
- 2012-07-13T03:24:17Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d93a5a6f
- Parents:
- d01e635
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/audio/hound/hound.c
rd01e635 r1df3018a 39 39 40 40 #include "hound.h" 41 #include "audio_client.h" 41 42 #include "audio_device.h" 42 43 #include "audio_sink.h" … … 46 47 #include "str_error.h" 47 48 49 #define FIND_BY_NAME(type) \ 50 do { \ 51 assert(list); \ 52 assert(name); \ 53 list_foreach(*list, it) { \ 54 audio_ ## type ## _t *dev = \ 55 audio_ ## type ## _list_instance(it); \ 56 if (str_cmp(name, dev->name) == 0) { \ 57 log_debug("%s with name %s is already present", \ 58 #type, name); \ 59 return NULL; \ 60 } \ 61 } \ 62 return NULL; \ 63 } while (0) 64 65 static audio_device_t * find_device_by_name(list_t *list, const char *name) 66 { 67 FIND_BY_NAME(device); 68 } 69 static audio_source_t * find_source_by_name(list_t *list, const char *name) 70 { 71 FIND_BY_NAME(source); 72 } 73 static audio_sink_t * find_sink_by_name(list_t *list, const char *name) 74 { 75 FIND_BY_NAME(sink); 76 } 77 48 78 int hound_init(hound_t *hound) 49 79 { 50 80 assert(hound); 81 fibril_mutex_initialize(&hound->list_guard); 51 82 list_initialize(&hound->devices); 83 list_initialize(&hound->sources); 52 84 list_initialize(&hound->available_sources); 53 85 list_initialize(&hound->sinks); 54 list_initialize(&hound->clients);55 86 return EOK; 56 87 } 57 88 58 int hound_add_device(hound_t *hound, service_id_t id, const char *name)89 int hound_add_device(hound_t *hound, service_id_t id, const char *name) 59 90 { 60 91 log_verbose("Adding device \"%s\", service: %zu", name, id); … … 67 98 68 99 list_foreach(hound->devices, it) { 69 audio_device_t *dev = list_audio_device_instance(it);100 audio_device_t *dev = audio_device_list_instance(it); 70 101 if (dev->id == id) { 71 102 log_debug("Device with id %zu is already present", id); … … 74 105 } 75 106 76 audio_device_t *dev = malloc(sizeof(audio_device_t)); 107 audio_device_t *dev = find_device_by_name(&hound->devices, name); 108 if (dev) { 109 log_debug("Device with name %s is already present", name); 110 return EEXISTS; 111 } 112 113 dev = malloc(sizeof(audio_device_t)); 77 114 if (!dev) { 78 115 log_debug("Failed to malloc device structure."); … … 80 117 } 81 118 const int ret = audio_device_init(dev, id, name); 119 free(name); 82 120 if (ret != EOK) { 83 121 log_debug("Failed to initialize new audio device: %s", … … 88 126 89 127 list_append(&dev->link, &hound->devices); 90 log_info("Added new device: '%s'", name);128 log_info("Added new device: '%s'", dev->name); 91 129 92 130 audio_source_t *source = audio_device_get_source(dev); 93 131 if (source) { 132 const int ret = hound_add_source(hound, source); 133 if (ret != EOK) { 134 log_debug("Failed to add device source: %s", 135 str_error(ret)); 136 audio_device_fini(dev); 137 return ret; 138 } 94 139 log_verbose("Added source: '%s'.", source->name); 95 list_append(&source->link, &hound->available_sources);96 140 } 97 141 98 142 audio_sink_t *sink = audio_device_get_sink(dev); 99 143 if (sink) { 144 const int ret = hound_add_sink(hound, sink); 145 if (ret != EOK) { 146 log_debug("Failed to add device sink: %s", 147 str_error(ret)); 148 audio_device_fini(dev); 149 return ret; 150 } 100 151 log_verbose("Added sink: '%s'.", sink->name); 101 list_append(&sink->link, &hound->sinks);102 152 } 103 153 … … 108 158 } 109 159 160 int hound_add_source(hound_t *hound, audio_source_t *source) 161 { 162 assert(hound); 163 if (!source || !source->name) { 164 log_debug("Invalid source specified."); 165 return EINVAL; 166 } 167 fibril_mutex_lock(&hound->list_guard); 168 if (find_source_by_name(&hound->sources, source->name)) { 169 log_debug("Source by that name already exists"); 170 fibril_mutex_unlock(&hound->list_guard); 171 return EEXISTS; 172 } 173 list_foreach(hound->sinks, it) { 174 audio_sink_t *sink = audio_sink_list_instance(it); 175 if (find_source_by_name(&sink->sources, source->name)) { 176 log_debug("Source by that name already exists"); 177 fibril_mutex_unlock(&hound->list_guard); 178 return EEXISTS; 179 } 180 } 181 list_append(&source->link, &hound->sources); 182 fibril_mutex_unlock(&hound->list_guard); 183 return EOK; 184 } 185 186 int hound_add_sink(hound_t *hound, audio_sink_t *sink) 187 { 188 assert(hound); 189 if (!sink || !sink->name) { 190 log_debug("Invalid source specified."); 191 return EINVAL; 192 } 193 fibril_mutex_lock(&hound->list_guard); 194 if (find_sink_by_name(&hound->sinks, sink->name)) { 195 log_debug("Sink by that name already exists"); 196 fibril_mutex_unlock(&hound->list_guard); 197 return EEXISTS; 198 } 199 list_append(&sink->link, &hound->sinks); 200 fibril_mutex_unlock(&hound->list_guard); 201 return EOK; 202 } 203 110 204 /** 111 205 * @}
Note:
See TracChangeset
for help on using the changeset viewer.