diff --git a/server/gdial-ssdp.c b/server/gdial-ssdp.c index 4cf34433..4b9f2119 100644 --- a/server/gdial-ssdp.c +++ b/server/gdial-ssdp.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -74,6 +75,7 @@ static const char ssdp_device_xml_template[] = "" static gchar *dd_xml_response_str_ = NULL; static gchar *app_friendly_name = NULL; static gchar *app_random_uuid = NULL; +static pthread_mutex_t ssdpServerEventSync = PTHREAD_MUTEX_INITIALIZER; static void ssdp_http_server_callback(SoupServer *server, SoupMessage *msg, const char *path, GHashTable *query, SoupClientContext *client, gpointer user_data) { /* @@ -85,6 +87,8 @@ static void ssdp_http_server_callback(SoupServer *server, SoupMessage *msg, cons GDIAL_DEBUG("warning: SSDP HTTP Method is not GET"); return; } + + pthread_mutex_lock(&ssdpServerEventSync); /* * there is no variant here, so we can cache the response. */ @@ -94,24 +98,54 @@ static void ssdp_http_server_callback(SoupServer *server, SoupMessage *msg, cons const gchar *manufacturer= gdial_plat_dev_get_manufacturer(); const gchar *model = gdial_plat_dev_get_model(); - if (manufacturer == NULL) {manufacturer = gdial_options_->manufacturer;} - if (model == NULL) {model = gdial_options_->model_name;} - if(gdial_options_->feature_friendlyname && strlen(app_friendly_name)) + if (manufacturer == NULL) { + manufacturer = gdial_options_->manufacturer; + } + + if (model == NULL) { + model = gdial_options_->model_name; + } + + if(gdial_options_->feature_friendlyname && app_friendly_name && strlen(app_friendly_name)) + { dd_xml_response_str_ = g_strdup_printf(ssdp_device_xml_template, app_friendly_name, manufacturer, model, gdial_options_->uuid); + } else + { dd_xml_response_str_ = g_strdup_printf(ssdp_device_xml_template, gdial_options_->friendly_name, manufacturer, model, gdial_options_->uuid); + } - dd_xml_response_str_len = strlen(dd_xml_response_str_); + if ( dd_xml_response_str_ ) { + dd_xml_response_str_len = strlen(dd_xml_response_str_); + } + else { + GDIAL_LOGERROR("Failed to allocate memory for dd.xml response"); + soup_message_set_status(msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); + } } - gchar *application_url_str = g_strdup_printf("http://%s:%d/%s/", iface_ipv4_address, GDIAL_REST_HTTP_PORT,app_random_uuid); - soup_message_headers_replace (msg->response_headers, "Application-URL", application_url_str); - g_free(application_url_str); - application_url_str = NULL; - soup_message_set_response(msg, "text/xml; charset=utf-8", SOUP_MEMORY_STATIC, dd_xml_response_str_, dd_xml_response_str_len); - soup_message_set_status(msg, SOUP_STATUS_OK); - GDIAL_CHECK("Content-Type:text/xml"); - GDIAL_CHECK("Application-URL: exist"); + if ( dd_xml_response_str_ ) { + gchar *application_url_str = g_strdup_printf("http://%s:%d/%s/", iface_ipv4_address, GDIAL_REST_HTTP_PORT,app_random_uuid); + + if ( application_url_str ) + { + soup_message_headers_replace (msg->response_headers, "Application-URL", application_url_str); + g_free(application_url_str); + application_url_str = NULL; + + soup_message_set_response(msg, "text/xml; charset=utf-8", SOUP_MEMORY_STATIC, dd_xml_response_str_, dd_xml_response_str_len); + soup_message_set_status(msg, SOUP_STATUS_OK); + + GDIAL_CHECK("Content-Type:text/xml"); + GDIAL_CHECK("Application-URL: exist"); + } + else + { + GDIAL_LOGERROR("Failed to allocate memory for response_headers"); + soup_message_set_status(msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); + } + } + pthread_mutex_unlock(&ssdpServerEventSync); } void gdial_ssdp_networkstandbymode_handler(const bool nwstandby) @@ -138,6 +172,12 @@ int gdial_ssdp_new(SoupServer *ssdp_http_server, GDialOptions *options, const gc g_return_val_if_fail(options != NULL, -1); g_return_val_if_fail(options->iface_name != NULL, -1); + if (0 != pthread_mutex_init(&ssdpServerEventSync, NULL)) + { + GDIAL_LOGERROR("Failed to initializing mutex"); + return EXIT_FAILURE; + } + gdial_options_ = options; GDIAL_LOGINFO("gdial_options_->friendly_name[%p]",gdial_options_->friendly_name); @@ -273,6 +313,7 @@ int gdial_ssdp_destroy() { g_object_unref(ssdp_client_); ssdp_client_ = NULL; } + pthread_mutex_destroy(&ssdpServerEventSync); GDIAL_LOGTRACE("Exiting ..."); return 0; } @@ -287,15 +328,20 @@ int gdial_ssdp_set_available(bool activation_status, const gchar *friendlyname) int gdial_ssdp_set_friendlyname(const gchar *friendlyname) { - if(gdial_options_ && gdial_options_->feature_friendlyname && friendlyname) - { - if (app_friendly_name != NULL) g_free(app_friendly_name); - app_friendly_name = g_strdup(friendlyname); - GDIAL_LOGINFO("gdial_ssdp_set_friendlyname app_friendly_name :%s ",app_friendly_name); - if (dd_xml_response_str_!= NULL){ - g_free(dd_xml_response_str_); - dd_xml_response_str_ = NULL; - } - } - return 0; -} + pthread_mutex_lock(&ssdpServerEventSync); + if(gdial_options_ && gdial_options_->feature_friendlyname && friendlyname) + { + if (app_friendly_name != NULL) { + g_free(app_friendly_name); + } + app_friendly_name = g_strdup(friendlyname); + + GDIAL_LOGINFO("gdial_ssdp_set_friendlyname app_friendly_name :%s ",app_friendly_name); + if (dd_xml_response_str_!= NULL){ + g_free(dd_xml_response_str_); + dd_xml_response_str_ = NULL; + } + } + pthread_mutex_unlock(&ssdpServerEventSync); + return 0; +} \ No newline at end of file