Fix some vulnerability (Null Pointer Dereference) defects
[platform/core/multimedia/libmm-sound.git] / mm_sound_device.c
index 74db967..ad2fa62 100644 (file)
@@ -30,8 +30,8 @@
 
 #define VOLUME_TYPE_LEN 64
 
-mm_sound_device_list_t g_device_list;
-pthread_mutex_t g_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
+static mm_sound_device_list_t g_device_list;
+static pthread_mutex_t g_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int _check_for_valid_mask(int flags)
 {
@@ -91,6 +91,8 @@ static int __convert_device_type_to_enum(char *device_type, mm_sound_device_type
                *device_type_enum = MM_SOUND_DEVICE_TYPE_MIRRORING;
        } else if (!strncmp(device_type, "usb-audio", VOLUME_TYPE_LEN)) {
                *device_type_enum = MM_SOUND_DEVICE_TYPE_USB_AUDIO;
+       } else if (!strncmp(device_type, "network", VOLUME_TYPE_LEN)) {
+               *device_type_enum = MM_SOUND_DEVICE_TYPE_NETWORK;
        } else {
                ret = MM_ERROR_INVALID_ARGUMENT;
                debug_error("not supported device_type(%s), err(0x%08x)", device_type, ret);
@@ -204,6 +206,38 @@ int mm_sound_remove_device_state_changed_callback(unsigned int id)
        return ret;
 }
 
+EXPORT_API
+int mm_sound_add_device_running_changed_callback(int flags, mm_sound_device_running_changed_cb func, void *user_data, unsigned int *id)
+{
+       int ret = MM_ERROR_NONE;
+
+       if (func == NULL || id == NULL) {
+               debug_error("argument is not valid");
+               return MM_ERROR_INVALID_ARGUMENT;
+       }
+       ret = _check_for_valid_mask(flags);
+       if (ret == MM_ERROR_NONE) {
+               ret = mm_sound_client_add_device_running_changed_callback(flags, func, user_data, id);
+               if (ret < 0) {
+                       debug_error("Could not add device running changed callback, ret = %x", ret);
+               }
+       }
+
+       return ret;
+}
+
+EXPORT_API
+int mm_sound_remove_device_running_changed_callback(unsigned int id)
+{
+       int ret = MM_ERROR_NONE;
+
+       ret = mm_sound_client_remove_device_running_changed_callback(id);
+       if (ret < 0) {
+               debug_error("Could not remove device running changed callback, ret = %x", ret);
+       }
+
+       return ret;
+}
 
 EXPORT_API
 int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDeviceList_t *device_list)
@@ -334,7 +368,7 @@ int mm_sound_get_next_device(MMSoundDeviceList_t device_list, MMSoundDevice_t *d
                        device_list_t->list = node;
 
                *device = (mm_sound_device_t*)node->data;
-               debug_log("next device[0x%x]", *device);
+               debug_log("next device[%p]", *device);
        }
        return ret;
 }
@@ -357,7 +391,7 @@ int mm_sound_get_prev_device(MMSoundDeviceList_t device_list, MMSoundDevice_t *d
        } else {
                device_list_t->list = node;
                *device = (mm_sound_device_t*)node->data;
-               debug_log("previous device[0x%x]", *device);
+               debug_log("previous device[%p]", *device);
        }
        return ret;
 }
@@ -371,7 +405,7 @@ int mm_sound_get_device_type(MMSoundDevice_t device_h, mm_sound_device_type_e *t
                return MM_ERROR_INVALID_ARGUMENT;
        }
        __convert_device_type_to_enum(device->type, type);
-       debug_log("device_handle:0x%x, type:%d", device, *type);
+       debug_log("device_handle:%p, type:%d", device, *type);
 
        return MM_ERROR_NONE;
 }
@@ -385,7 +419,7 @@ int mm_sound_get_device_io_direction(MMSoundDevice_t device_h, mm_sound_device_i
                return MM_ERROR_INVALID_ARGUMENT;
        }
        *io_direction = device->io_direction;
-       debug_log("device_handle:0x%x, io_direction:%d (1:IN,2:OUT,3:INOUT)", device, *io_direction);
+       debug_log("device_handle:%p, io_direction:%d (1:IN,2:OUT,3:INOUT)", device, *io_direction);
 
        return MM_ERROR_NONE;
 }
@@ -399,7 +433,7 @@ int mm_sound_get_device_id(MMSoundDevice_t device_h, int *id)
                return MM_ERROR_INVALID_ARGUMENT;
        }
        *id = device->id;
-       debug_log("device_handle:0x%x, id:%d", device, *id);
+       debug_log("device_handle:%p, id:%d", device, *id);
 
        return MM_ERROR_NONE;
 }
@@ -413,7 +447,7 @@ int mm_sound_get_device_state(MMSoundDevice_t device_h, mm_sound_device_state_e
                return MM_ERROR_INVALID_ARGUMENT;
        }
        *state = device->state;
-       debug_log("device_handle:0x%x, state:%d (0:INACTIVATED,1:ACTIVATED)", device, *state);
+       debug_log("device_handle:%p, state:%d (0:INACTIVATED,1:ACTIVATED)", device, *state);
 
        return MM_ERROR_NONE;
 }
@@ -427,7 +461,7 @@ int mm_sound_get_device_name(MMSoundDevice_t device_h, char **name)
                return MM_ERROR_INVALID_ARGUMENT;
        }
        *name = device->name;
-       debug_log("device_handle:0x%x, name:%s", device, *name);
+       debug_log("device_handle:%p, name:%s", device, *name);
 
        return MM_ERROR_NONE;
 }
@@ -441,7 +475,7 @@ int mm_sound_get_device_vendor_id(MMSoundDevice_t device_h, int *vendor_id)
                return MM_ERROR_INVALID_ARGUMENT;
        }
        *vendor_id = device->vendor_id;
-       debug_log("device_handle:0x%x, vendor id:%04x", device, *vendor_id);
+       debug_log("device_handle:%p, vendor id:%04x", device, *vendor_id);
 
        return MM_ERROR_NONE;
 }
@@ -455,7 +489,7 @@ int mm_sound_get_device_product_id(MMSoundDevice_t device_h, int *product_id)
                return MM_ERROR_INVALID_ARGUMENT;
        }
        *product_id = device->product_id;
-       debug_log("device_handle:0x%x, product id:%04x", device, *product_id);
+       debug_log("device_handle:%p, product id:%04x", device, *product_id);
 
        return MM_ERROR_NONE;
 }
@@ -495,3 +529,39 @@ int mm_sound_is_stream_on_device(int stream_id, MMSoundDevice_t device_h, bool *
 
        return ret;
 }
+
+EXPORT_API
+int mm_sound_is_stream_on_device_by_id(int stream_id, int device_id, bool *is_on)
+{
+       int ret = MM_ERROR_NONE;
+       bool _is_on = false;
+
+       if (!is_on) {
+               debug_error("invalid argument");
+               return MM_ERROR_INVALID_ARGUMENT;
+       }
+
+       if ((ret = mm_sound_client_is_stream_on_device(stream_id, device_id, &_is_on)) < 0) {
+               debug_error("Failed to query is stream on");
+               return MM_ERROR_SOUND_INTERNAL;
+       }
+
+       debug_log("device(%d) %s stream(%d)", device_id, _is_on ? "has" : "doesn't have", stream_id);
+       *is_on = _is_on;
+
+       return ret;
+}
+
+EXPORT_API
+int mm_sound_is_device_running(MMSoundDevice_t device_h, bool *is_running)
+{
+       mm_sound_device_t *device = (mm_sound_device_t*)device_h;
+       if (!device || !is_running) {
+               debug_error("invalid argument");
+               return MM_ERROR_INVALID_ARGUMENT;
+       }
+       *is_running = device->is_running;
+       debug_log("device_handle:0x%p, running:%d", device, *is_running);
+
+       return MM_ERROR_NONE;
+}