Example #1
0
void
light_handler(REQUEST* request, RESPONSE* response)
{
  read_light_sensor(&light_photosynthetic, &light_solar);
  sprintf(temp,"%u;%u", light_photosynthetic, light_solar);

  char etag[4] = "ABCD";
  rest_set_header_content_type(response, TEXT_PLAIN);
  rest_set_header_etag(response, etag, sizeof(etag));
  rest_set_response_payload(response, temp, strlen(temp));
}
static DBusHandlerResult
filter_function (DBusConnection *connection, DBusMessage *message, void *userdata)
{
	DBusError err;
	DBusMessage *reply;
        const char *udi;

        udi = dbus_message_get_path (message);

	/*dbg ("filter_function: sender=%s destination=%s obj_path=%s interface=%s method=%s", 
	     dbus_message_get_sender (message), 
	     dbus_message_get_destination (message), 
	     dbus_message_get_path (message), 
	     dbus_message_get_interface (message),
	     dbus_message_get_member (message));*/

	reply = NULL;
	dbus_error_init (&err);

	if (dbus_message_is_method_call (message, 
					 "org.freedesktop.Hal.Device.LaptopPanel", 
					 "SetBrightness")) {
		int brightness;

                if (!check_priv (halctx, connection, message, udi, "org.freedesktop.hal.power-management.lcd-panel"))
                        goto error;

		if (dbus_message_get_args (message, 
					   &err,
					   DBUS_TYPE_INT32, &brightness,
					   DBUS_TYPE_INVALID)) {
			/* dbg ("setting brightness %d", brightness); */
			if (brightness < 0 || brightness > 228) {
				reply = dbus_message_new_error (message,
								"org.freedesktop.Hal.Device.LaptopPanel.Invalid",
								"Brightness has to be between 0 and 228!");

			} else {
				int return_code;

				write_backlight (brightness + 27);

				reply = dbus_message_new_method_return (message);
				if (reply == NULL)
					goto error;

				return_code = 0;
				dbus_message_append_args (reply,
							  DBUS_TYPE_INT32, &return_code,
							  DBUS_TYPE_INVALID);
			}

			dbus_connection_send (connection, reply, NULL);
		}
		
	} else if (dbus_message_is_method_call (message, 
						"org.freedesktop.Hal.Device.LaptopPanel", 
						"GetBrightness")) {
		int brightness;

                if (!check_priv (halctx, connection, message, udi, "org.freedesktop.hal.power-management.lcd-panel"))
                        goto error;

		if (dbus_message_get_args (message, 
					   &err,
					   DBUS_TYPE_INVALID)) {

			brightness = read_backlight () - 27;
			if (brightness < 0)
				brightness = 0;
			if (brightness > 228)
				brightness = 228;

			/* dbg ("getting brightness, it's %d", brightness); */

			reply = dbus_message_new_method_return (message);
			if (reply == NULL)
				goto error;

			dbus_message_append_args (reply,
						  DBUS_TYPE_INT32, &brightness,
						  DBUS_TYPE_INVALID);
			dbus_connection_send (connection, reply, NULL);
		}
		
	} else if (dbus_message_is_method_call (message, 
						"org.freedesktop.Hal.Device.LightSensor", 
						"GetBrightness")) {
		int brightness[2];

                if (!check_priv (halctx, connection, message, udi, "org.freedesktop.hal.power-management.light-sensor"))
                        goto error;

		brightness[0] = read_light_sensor (FALSE); /* right */
		brightness[1] = read_light_sensor (TRUE); /* left */

		if (brightness[0] == -1 || brightness[1] == -1) {
			reply = dbus_message_new_error (message,
							"org.freedesktop.Hal.Device.LightSensors.Error",
							"Error poking hardware");
			dbus_connection_send (connection, reply, NULL);
		} else {
			int (*pb)[] = &brightness;

			reply = dbus_message_new_method_return (message);
			if (reply == NULL)
				goto error;
			
			dbus_message_append_args (reply,
						  DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &pb, 2,
						  DBUS_TYPE_INVALID);
			dbus_connection_send (connection, reply, NULL);
		}
	} else if (dbus_message_is_method_call (message, 
						"org.freedesktop.Hal.Device.KeyboardBacklight", 
						"GetBrightness")) {

                if (!check_priv (halctx, connection, message, udi, "org.freedesktop.hal.power-management.keyboard-backlight"))
                        goto error;

		/* I can't get this working so just cache last SetBrightness value :-/ */
		if (last_keyboard_brightness == -1 ) {
			reply = dbus_message_new_error (message,
							"org.freedesktop.Hal.Device.KeyboardBacklight.Error",
							"Error poking hardware");
			dbus_connection_send (connection, reply, NULL);
		} else {
			reply = dbus_message_new_method_return (message);
			if (reply == NULL)
				goto error;
			
			dbus_message_append_args (reply,
						  DBUS_TYPE_INT32, &last_keyboard_brightness,
						  DBUS_TYPE_INVALID);
			dbus_connection_send (connection, reply, NULL);
		}
#if 0
		int brightness;

		brightness = read_keyboard_backlight ();

		if (brightness == -1) {
			reply = dbus_message_new_error (message,
							"org.freedesktop.Hal.Device.KeyboardBacklight.Error",
							"Error poking hardware");
			dbus_connection_send (connection, reply, NULL);
		} else {
			reply = dbus_message_new_method_return (message);
			if (reply == NULL)
				goto error;
			
			dbus_message_append_args (reply,
						  DBUS_TYPE_INT32, &brightness,
						  DBUS_TYPE_INVALID);
			dbus_connection_send (connection, reply, NULL);
		}
#endif
	} else if (dbus_message_is_method_call (message, 
						"org.freedesktop.Hal.Device.KeyboardBacklight", 
						"SetBrightness")) {
		int brightness;

                if (!check_priv (halctx, connection, message, udi, "org.freedesktop.hal.power-management.keyboard-backlight"))
                        goto error;

		if (dbus_message_get_args (message, 
					   &err,
					   DBUS_TYPE_INT32, &brightness,
					   DBUS_TYPE_INVALID)) {
			/*dbg ("setting keyboard brightness %d", brightness);*/
			if (brightness < 0 || brightness > 255) {
				reply = dbus_message_new_error (message,
								"org.freedesktop.Hal.Device.KeyboardBacklight.Invalid",
								"Brightness has to be between 0 and 255!");

			} else {
				set_keyboard_backlight (brightness);
				last_keyboard_brightness = brightness;

				reply = dbus_message_new_method_return (message);
				if (reply == NULL)
					goto error;
			}

			dbus_connection_send (connection, reply, NULL);
		}

	}
	
error:
	if (reply != NULL)
		dbus_message_unref (reply);

	LIBHAL_FREE_DBUS_ERROR (&err);

	return DBUS_HANDLER_RESULT_HANDLED;
}