Added NULL pointer guard check before
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-otp.c
index 0c61c19..64f0a38 100644 (file)
@@ -120,10 +120,9 @@ void server_init_cb(GObject *object, GAsyncResult *res,
                        g_clear_error(&error);
                        status = BLUETOOTH_ERROR_INTERNAL;
                }
-       }
-
-       if (result) {
+       } else {
                g_variant_get(result, "(i)", &status);
+               g_variant_unref(result);
        }
 
        BT_DBG("Status [%d]", status);
@@ -141,7 +140,7 @@ void server_init_cb(GObject *object, GAsyncResult *res,
                param);
 
        out_param = g_variant_new_from_data((const GVariantType*)"i",
-                               result, sizeof(int), TRUE, NULL, NULL);
+                               &status, sizeof(int), TRUE, NULL, NULL);
 
        if (req_info) {
                g_dbus_method_invocation_return_value(req_info->context,
@@ -149,8 +148,6 @@ void server_init_cb(GObject *object, GAsyncResult *res,
 
                _bt_delete_request_list(req_info->req_id);
        }
-
-       g_variant_unref(result);
 }
 
 int bt_otp_server_init(int request_id, const char *directory)
@@ -201,10 +198,9 @@ void server_deinit_cb(GObject *object, GAsyncResult *res,
                        g_clear_error(&error);
                        status = BLUETOOTH_ERROR_INTERNAL;
                }
-       }
-
-       if (result) {
+       } else {
                g_variant_get(result, "(i)", &status);
+               g_variant_unref(result);
        }
 
        BT_DBG("Status [%d]", status);
@@ -220,7 +216,7 @@ void server_deinit_cb(GObject *object, GAsyncResult *res,
 
        if (req_info) {
                out_param = g_variant_new_from_data((const GVariantType*)"i",
-                               result, sizeof(int), TRUE, NULL, NULL);
+                               &status, sizeof(int), TRUE, NULL, NULL);
 
                g_dbus_method_invocation_return_value(req_info->context,
                                g_variant_new("(iv)", status, out_param));
@@ -228,8 +224,6 @@ void server_deinit_cb(GObject *object, GAsyncResult *res,
                _bt_delete_request_list(req_info->req_id);
        }
 
-       g_variant_unref(result);
-
        if (otp_gproxy) {
                g_object_unref(otp_gproxy);
                otp_gproxy = NULL;
@@ -260,9 +254,10 @@ int bt_otp_server_deinit(int request_id)
 int __get_handle_length(char *handle)
 {
        int i = 0;
-       while (handle && (handle[i] != '\0')) {
+
+       while (handle && (handle[i] != '\0'))
                i++;
-       }
+
        return i;
 }
 
@@ -297,13 +292,31 @@ static void __bt_otp_remove_read_info(bt_otp_read_req_info *info)
        g_free(info);
 }
 
+static int __bluetooth_get_att_error_code(GError *error)
+{
+       int att_ecode = 0;
+       int len;
+       char *str = NULL;
+
+       BT_ERR("Error : %s", error->message);
+       str = g_strrstr(error->message, "ATT error: 0x");
+       if (str) {
+               len = strlen(str);
+               att_ecode =  g_ascii_xdigit_value(str[len-2]) << 4;
+               att_ecode += g_ascii_xdigit_value(str[len-1]);
+       } else
+               return BLUETOOTH_ATT_ERROR_INTERNAL;
+
+       return att_ecode;
+}
+
 static void __bt_otp_read_char_cb(GObject *source_object,
                        GAsyncResult *res, gpointer user_data)
 {
        bt_gatt_char_descriptor_property_t att_value =  { 0, };
        GDBusConnection *system_gconn = NULL;
        GVariant *var_data, *param = NULL;
-       int result = BLUETOOTH_ERROR_NONE;
+       int result = BLUETOOTH_ATT_ERROR_NONE;
        bt_otp_read_req_info *info = NULL;
        GByteArray *gp_byte_array = NULL;
        request_info_t *req_info = NULL;
@@ -312,7 +325,7 @@ static void __bt_otp_read_char_cb(GObject *source_object,
        char *otp_data = NULL;
        GVariant *out_param1;
        GError *error = NULL;
-       guint8 g_byte, att_error_code;
+       guint8 g_byte;
        char *handle;
 
        BT_DBG("+");
@@ -324,20 +337,12 @@ static void __bt_otp_read_char_cb(GObject *source_object,
        value = g_dbus_connection_call_finish(system_gconn, res, &error);
 
        if (error) {
-               BT_ERR("Error : %s \n", error->message);
+               result = __bluetooth_get_att_error_code(error);
                att_value.val_len = 0;
-               result = BLUETOOTH_ERROR_INTERNAL;
                goto dbus_return;
        }
 
-       g_variant_get(value, "(yay)", &att_error_code, &iter);
-
-       if (att_error_code != 0) {
-               BT_ERR("ATT err code : [%d]", att_error_code);
-               att_value.val_len = 0;
-               result = att_error_code;
-               goto dbus_return;
-       }
+       g_variant_get(value, "(ay)", &iter);
 
        gp_byte_array = g_byte_array_new();
 
@@ -360,11 +365,10 @@ dbus_return:
                _bt_send_event_to_dest(info->sender, BT_OTP_EVENT,
                                BLUETOOTH_EVENT_OTP_READ_CHAR_VAL,
                                param);
+               req_info = _bt_get_request_info(info->req_id);
+               __bt_otp_remove_read_info(info);
        }
 
-       req_info = _bt_get_request_info(info->req_id);
-       __bt_otp_remove_read_info(info);
-
        if (req_info == NULL) {
                BT_ERR("OTP data read Request not found!!");
                goto done;
@@ -401,7 +405,7 @@ int _bt_otp_read_characteristic_value(int request_id, char *sender, char *handle
 {
        GDBusConnection *conn;
        bt_otp_read_req_info *info = NULL;
-       char *charc_handle = g_strdup(handle);
+       char *charc_handle = NULL;
        GVariantBuilder *builder = NULL;
        guint16 offset = 0;
 
@@ -415,8 +419,7 @@ int _bt_otp_read_characteristic_value(int request_id, char *sender, char *handle
 
        /* If OTP data read already pending on same Server, then return In progress */
        if (__bt_otp_get_read_info(handle) != NULL) {
-               BT_ERR("Read Req is ongoing in remote server [%s]", charc_handle);
-               g_free(charc_handle);
+               BT_ERR("Read Req is ongoing in remote server [%s]", handle);
                return BLUETOOTH_ERROR_IN_PROGRESS;
        }
 
@@ -425,13 +428,15 @@ int _bt_otp_read_characteristic_value(int request_id, char *sender, char *handle
        g_variant_builder_add(builder, "{sv}", "offset",
                g_variant_new("q", offset));
 
+       charc_handle = g_strdup(handle);
+
        g_dbus_connection_call(conn,
                        BT_BLUEZ_NAME,
                        handle,
                        GATT_CHAR_INTERFACE,
                        "ReadValue",
                        g_variant_new("(a{sv})", builder),
-                       G_VARIANT_TYPE("(yay)"),
+                       G_VARIANT_TYPE("(ay)"),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1,
                        NULL,
@@ -578,7 +583,7 @@ done:
 int _bt_otp_enable_notification(int request_id, char *sender, char *handle)
 {
        bt_otp_notification_info *info = NULL;
-       char *charc_handle = g_strdup(handle);
+       char *charc_handle = NULL;
        GDBusConnection *conn;
 
        BT_CHECK_PARAMETER(handle, return);
@@ -591,10 +596,11 @@ int _bt_otp_enable_notification(int request_id, char *sender, char *handle)
 
        if (__bt_otp_get_notification_info(handle) != NULL) {
                BT_ERR("Activation is already ongoing for same remote server");
-               g_free(charc_handle);
                return BLUETOOTH_ERROR_IN_PROGRESS;
        }
 
+       charc_handle = g_strdup(handle);
+
        BT_INFO("Start Notify to Bluez");
        g_dbus_connection_call(conn,
                        BT_BLUEZ_NAME,
@@ -626,27 +632,17 @@ static void __bt_otp_write_request_cb(GObject *source_object,
        GVariant *value = NULL;
        GVariant *param = NULL;
        GVariant *out_param1 = NULL;
-       int result = BLUETOOTH_ERROR_NONE;
+       int result = BLUETOOTH_ATT_ERROR_NONE;
        char *handle = NULL;
        bt_otp_notification_info *info = NULL;
        request_info_t *req_info = NULL;
-       guint8 att_ecode = 0;
        BT_DBG("+");
 
        system_gconn = _bt_gdbus_get_system_gconn();
        value = g_dbus_connection_call_finish(system_gconn, res, &error);
 
-       if (error) {
-               BT_ERR("Error : %s \n", error->message);
-               /* Process error->message to narrow down the att_ecode */
-               result = BLUETOOTH_ERROR_INTERNAL;
-       } else {
-               g_variant_get(value, "(y)", &att_ecode);
-               if (att_ecode) {
-                       result =  att_ecode;
-                       BT_ERR("ATT Error code: %d \n", att_ecode);
-               }
-       }
+       if (error)
+               result = __bluetooth_get_att_error_code(error);
 
        handle = (char *)user_data;
        info = __bt_otp_get_notification_info(handle);
@@ -654,7 +650,7 @@ static void __bt_otp_write_request_cb(GObject *source_object,
                req_info = _bt_get_request_info(info->req_id);
 
        /* Is Activation request failed for any reason, reset timer */
-       if (result != BLUETOOTH_ERROR_NONE && info != NULL) {
+       if (result != BLUETOOTH_ATT_ERROR_NONE && info != NULL) {
                BT_ERR("Activation Request failed");
                /* Remove Indication Info */
                __bt_otp_remove_notification_info(info);
@@ -739,7 +735,7 @@ int _bt_otp_write_characteristic_value(int request_id, char *sender, char *handl
        guint16 offset = 0;
        bt_otp_notification_info *info = NULL;
        GDBusConnection *conn;
-       char *charc_handle = g_strdup(handle);
+       char *charc_handle = NULL;
        int i;
 
        BT_DBG("+");
@@ -757,15 +753,13 @@ int _bt_otp_write_characteristic_value(int request_id, char *sender, char *handl
        info = __bt_otp_get_notification_info(handle);
        if (info && info->notification_timeout_id > 0) {
                BT_ERR("Write Request is already ongoing in remote server");
-               g_free(charc_handle);
                return BLUETOOTH_ERROR_IN_PROGRESS;
        }
 
        builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
-       for (i = 0; i < length; i++) {
+       for (i = 0; i < length; i++)
                g_variant_builder_add(builder1, "y", param[i]);
-       }
 
        val = g_variant_new("ay", builder1);
        builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
@@ -775,6 +769,8 @@ int _bt_otp_write_characteristic_value(int request_id, char *sender, char *handl
 
        options = g_variant_new("a{sv}", builder2);
 
+       charc_handle = g_strdup(handle);
+
        /* Activate Control Point */
        g_dbus_connection_call(conn,
                        BT_BLUEZ_NAME,
@@ -819,25 +815,31 @@ void _bt_otp_check_indication(const char *path, GVariant *msg)
        info = __bt_otp_get_notification_info((char *)path);
 
        if (info) {
-               g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
-               len = g_variant_get_size(value);
-               BT_DBG("Indication data from Server len[%d]", len);
-               if (len > 0) {
-                       buffer = (unsigned char *)g_variant_get_data(value);
-                       /* DEBUG */
-                       for (i = 0; i < len; i++)
-                               BT_DBG("%.2x", buffer[i]);
-               }
-
                /* Reset Timer */
                if (info->notification_timeout_id > 0)
                        g_source_remove(info->notification_timeout_id);
 
-               /* Send Indication & info removed internally */
-               __bt_otp_send_indication_event(info, buffer, len, BLUETOOTH_ERROR_NONE);
+               g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
+
+               if (value) {
+                       len = g_variant_get_size(value);
+                       BT_DBG("Indication data from Server len[%d]", len);
+                       if (len > 0) {
+                               buffer = (unsigned char *)g_variant_get_data(value);
+                               /* DEBUG */
+                               for (i = 0; i < len; i++)
+                                       BT_DBG("%.2x", buffer[i]);
+                       }
+
+                       /* Send Indication & info removed internally */
+                       __bt_otp_send_indication_event(info, buffer, len, BLUETOOTH_ERROR_NONE);
 
-               if (value)
                        g_variant_unref(value);
+               } else {
+                       BT_ERR("No Indication data from Server");
+                       /* Send Error Indication & info removed internally */
+                       __bt_otp_send_indication_event(info, NULL, 0, BLUETOOTH_ERROR_INTERNAL);
+               }
        }
        BT_DBG("-");
 }