Beispiel #1
0
profile_plugin * init_profile()
{
    profile_plugin *plugin = g_new0(profile_plugin, 1);
    char *profile = NULL;
    profileval_t *values = NULL;

    if (!plugin) {
        return NULL;
    }

    
    /* let libprofile know the correct bus connection */
    profile_connection_set(bus_conn);
    
    /* get current profile */
    profile = profile_get_profile();

    /* start tracking profile changes */
    if (profile && subscribe_to_service()) {
        values = profile_get_values(profile);
    }
    else {
        g_free(plugin);
        plugin = NULL;
        goto end;
    }

    if (!profile_create_fact(profile, values)) {
        g_free(plugin);
        plugin = NULL;
        goto end;
    }

end:

    free(profile);
    profile_free_values(values);

    return plugin;
}
/**
 * Callback function used to answer clients's connection (MHD_AccessHandlerCallback)
 *
 * @param cls Custom value selected at callback registration time, used to initialize the done flag
 *	
 * @param connection The client connection 
 *
 * @param url The url on which the client made its request
 *
 * @param method The http method with which the client made its request (Only GET and PUT supported)
 *
 * @param version The HTTP version string (i.e. HTTP/1.1)
 *
 * @param upload_data Data beeing uploaded when receiving PUT
 *
 * @param upload_data_size Size of the data beeing uploaded when receiving PUT
 *
 * @param con_cls reference to a pointer, initially set to NULL, that this callback can set to some 
 *        address and that will be preserved by MHD for future calls for this request
 *
 * @return MHD_YES to pursue the request handling, MHD_NO in case of error with 
 *         the request, return value of the send_* functions otherwise
 */
int 
answer_to_connection ( void *cls, struct MHD_Connection *connection, 
                       const char *url, 
                       const char *method, const char *version, 
                       const char *upload_data, 
                       size_t *upload_data_size, void **con_cls		)
{
	Service *requested_service;
	char *xmlbuff = NULL;
	int ret;
	int *done = cls;
	static int aptr;
	const char *get_arg;
	struct sockaddr *addr;
	addr = MHD_get_connection_info (connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS)->client_addr;
	char IP[16];
	inet_ntop(addr->sa_family,addr->sa_data + 2, IP, 16);

	if( 0 == strcmp (method, MHD_HTTP_METHOD_GET) )
	{

		if ( &aptr != *con_cls )
		{
			/* do never respond on first call */
			*con_cls = &aptr;
			return MHD_YES;
		}
		*con_cls = NULL;


		if( strcmp(url,"/log") == 0 )
		{
			ret = subscribe_to_log_events(connection, con_cls, HPD_IS_UNSECURE_CONNECTION);
			Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
			return ret;
		}
		else if( strcmp(url,"/events") == 0 )
		{
			ret = set_up_server_sent_event_connection( connection, HPD_IS_UNSECURE_CONNECTION );
			if( ret == MHD_HTTP_NOT_FOUND ) 
			{
				Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
				return send_error (connection, MHD_HTTP_NOT_FOUND);
			}
			else 
			{
				Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
				return ret;
			}
		}
		else if( strcmp(url,"/devices") == 0 )
		{
			xmlbuff = get_xml_device_list();
			ret = send_xml (connection, xmlbuff);
			Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
			return ret;
		}
		else if( ( requested_service = matching_service (service_head, url) ) != NULL )
		{
			get_arg = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "x");
			if( get_arg != NULL )
			{
				if( strcmp(get_arg, "1") == 0 )
				{
					pthread_mutex_lock(requested_service->mutex);
					xmlbuff = extract_service_xml(requested_service);
					pthread_mutex_unlock(requested_service->mutex);
					ret = send_xml (connection, xmlbuff);
					Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, "x=1");
					return ret;
				}
			}

			get_arg = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "p");
			if( get_arg != NULL )
			{
				if( strcmp(get_arg, "1") == 0 )
				{
					ret = subscribe_to_service(connection, requested_service, con_cls, HPD_IS_UNSECURE_CONNECTION);
					Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, "p=1");
					return ret;
				}
			}

			pthread_mutex_lock( requested_service->mutex );
			ret = requested_service-> get_function(	requested_service, 
			                                        requested_service->get_function_buffer,
			                                        MHD_MAX_BUFFER_SIZE);
			if( ret != 0 )
				requested_service->get_function_buffer[ret] = '\0';
			else
			{
				pthread_mutex_unlock( requested_service->mutex );
				ret = send_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
				Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
				return ret;
			}

			xmlbuff = get_xml_value (requested_service->get_function_buffer);
			pthread_mutex_unlock(requested_service->mutex);
			ret = send_xml(connection, xmlbuff);
			Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
			return ret;
		}
		else
		{
			ret = send_error(connection, MHD_HTTP_NOT_FOUND);
			Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
			return ret;
		}
	}

	else if( 0 == strcmp(method, MHD_HTTP_METHOD_PUT) )
	{ 

		if( ( *con_cls ) == NULL )
		{
			if( *upload_data_size == 0 )
			{
				return MHD_YES; /* not ready yet */
			}	    
			/* Add a space for a '/0' in order to clear the end of the XML */
			char *put_data_temp = (char*)malloc((*upload_data_size)*sizeof(char)+1);
			memcpy(put_data_temp, upload_data, *upload_data_size);
			put_data_temp[*upload_data_size]='\0';
			*con_cls = put_data_temp;
			*upload_data_size = 0;
			return MHD_YES;
		}
		else
		{
			if( ( requested_service = matching_service (service_head, url) ) !=NULL )
			{
				pthread_mutex_lock(requested_service->mutex);
				if( requested_service->put_function != NULL && *con_cls != NULL )
				{
					char* _value = get_value_from_xml_value (*con_cls);
					if(_value == NULL)
					{
						pthread_mutex_unlock(requested_service->mutex);
						ret = send_error (connection, MHD_HTTP_BAD_REQUEST);
						Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
						return ret;
					}

					free(*con_cls);
					ret = requested_service-> put_function( requested_service, 
					                                        requested_service->get_function_buffer,
					                                        MHD_MAX_BUFFER_SIZE,
					                                        _value );
					free(_value);

					if( ret != 0 )
						requested_service->get_function_buffer[ret] = '\0';
					else
					{
						pthread_mutex_unlock( requested_service->mutex );
						ret = send_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
						Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
						return ret;
					}

					xmlbuff = get_xml_value (requested_service->get_function_buffer);
					pthread_mutex_unlock(requested_service->mutex);
					send_event_of_value_change (requested_service, requested_service->get_function_buffer);
					ret = send_xml (connection, xmlbuff);
					Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
					return ret;
				}
				else
				{
					pthread_mutex_unlock(requested_service->mutex);
					ret = send_error(connection, MHD_HTTP_BAD_REQUEST);
					Log (HPD_LOG_ONLY_REQUESTS, NULL, IP, method, url, NULL);
					return ret;
				}
			}
			else
				return send_error (connection, MHD_HTTP_NOT_FOUND);
		}
	}
	return MHD_NO;
}