bluetooth: Use enum to represent form factors

Avoid using strings only to represent form factors in the bluetooth-util
API and instead use a new dedicated enum type: pa_bt_form_factor_t.
This commit is contained in:
Mikel Astiz 2013-03-07 11:32:21 +01:00 committed by David Henningsson
parent 278ceb453f
commit 11d7a2d197
3 changed files with 57 additions and 17 deletions

View file

@ -1800,26 +1800,26 @@ pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hoo
return &y->hooks[hook]; return &y->hooks[hook];
} }
const char*pa_bluetooth_get_form_factor(uint32_t class) { pa_bt_form_factor_t pa_bluetooth_get_form_factor(uint32_t class) {
unsigned i; unsigned i;
const char *r; pa_bt_form_factor_t r;
static const char * const table[] = { static const pa_bt_form_factor_t table[] = {
[1] = "headset", [1] = PA_BT_FORM_FACTOR_HEADSET,
[2] = "hands-free", [2] = PA_BT_FORM_FACTOR_HANDSFREE,
[4] = "microphone", [4] = PA_BT_FORM_FACTOR_MICROPHONE,
[5] = "speaker", [5] = PA_BT_FORM_FACTOR_SPEAKER,
[6] = "headphone", [6] = PA_BT_FORM_FACTOR_HEADPHONE,
[7] = "portable", [7] = PA_BT_FORM_FACTOR_PORTABLE,
[8] = "car", [8] = PA_BT_FORM_FACTOR_CAR,
[10] = "hifi" [10] = PA_BT_FORM_FACTOR_HIFI
}; };
if (((class >> 8) & 31) != 4) if (((class >> 8) & 31) != 4)
return NULL; return PA_BT_FORM_FACTOR_UNKNOWN;
if ((i = (class >> 2) & 63) >= PA_ELEMENTSOF(table)) if ((i = (class >> 2) & 63) >= PA_ELEMENTSOF(table))
r = NULL; r = PA_BT_FORM_FACTOR_UNKNOWN;
else else
r = table[i]; r = table[i];
@ -1829,6 +1829,31 @@ const char*pa_bluetooth_get_form_factor(uint32_t class) {
return r; return r;
} }
const char *pa_bt_form_factor_to_string(pa_bt_form_factor_t ff) {
switch (ff) {
case PA_BT_FORM_FACTOR_UNKNOWN:
return "unknown";
case PA_BT_FORM_FACTOR_HEADSET:
return "headset";
case PA_BT_FORM_FACTOR_HANDSFREE:
return "hands-free";
case PA_BT_FORM_FACTOR_MICROPHONE:
return "microphone";
case PA_BT_FORM_FACTOR_SPEAKER:
return "speaker";
case PA_BT_FORM_FACTOR_HEADPHONE:
return "headphone";
case PA_BT_FORM_FACTOR_PORTABLE:
return "portable";
case PA_BT_FORM_FACTOR_CAR:
return "car";
case PA_BT_FORM_FACTOR_HIFI:
return "hifi";
}
pa_assert_not_reached();
}
char *pa_bluetooth_cleanup_name(const char *name) { char *pa_bluetooth_cleanup_name(const char *name) {
char *t, *s, *d; char *t, *s, *d;
bool space = false; bool space = false;

View file

@ -154,7 +154,20 @@ void pa_bluetooth_transport_set_speaker_gain(pa_bluetooth_transport *t, uint16_t
pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook); pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook);
const char* pa_bluetooth_get_form_factor(uint32_t class); typedef enum pa_bt_form_factor {
PA_BT_FORM_FACTOR_UNKNOWN,
PA_BT_FORM_FACTOR_HEADSET,
PA_BT_FORM_FACTOR_HANDSFREE,
PA_BT_FORM_FACTOR_MICROPHONE,
PA_BT_FORM_FACTOR_SPEAKER,
PA_BT_FORM_FACTOR_HEADPHONE,
PA_BT_FORM_FACTOR_PORTABLE,
PA_BT_FORM_FACTOR_CAR,
PA_BT_FORM_FACTOR_HIFI,
} pa_bt_form_factor_t;
pa_bt_form_factor_t pa_bluetooth_get_form_factor(uint32_t class);
const char *pa_bt_form_factor_to_string(pa_bt_form_factor_t ff);
char *pa_bluetooth_cleanup_name(const char *name); char *pa_bluetooth_cleanup_name(const char *name);

View file

@ -2147,7 +2147,7 @@ static int add_card(struct userdata *u) {
bool b; bool b;
pa_card_profile *p; pa_card_profile *p;
enum profile *d; enum profile *d;
const char *ff; pa_bt_form_factor_t ff;
char *n; char *n;
const char *default_profile; const char *default_profile;
const pa_bluetooth_device *device = u->device; const pa_bluetooth_device *device = u->device;
@ -2167,8 +2167,10 @@ static int add_card(struct userdata *u) {
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez"); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez");
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound"); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound");
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_BUS, "bluetooth"); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_BUS, "bluetooth");
if ((ff = pa_bluetooth_get_form_factor(device->class)))
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, ff); if ((ff = pa_bluetooth_get_form_factor(device->class)) != PA_BT_FORM_FACTOR_UNKNOWN)
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, pa_bt_form_factor_to_string(ff));
pa_proplist_sets(data.proplist, "bluez.path", device->path); pa_proplist_sets(data.proplist, "bluez.path", device->path);
pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) device->class); pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) device->class);
pa_proplist_sets(data.proplist, "bluez.name", device->name); pa_proplist_sets(data.proplist, "bluez.name", device->name);