static void is_indicator_dispose(GObject *object) { IsIndicator *self = (IsIndicator *)object; IsIndicatorPrivate *priv = self->priv; IsManager *manager; GSList *sensors, *_list; manager = is_application_get_manager(priv->application); g_signal_handlers_disconnect_by_func(manager, sensor_enabled, self); g_signal_handlers_disconnect_by_func(manager, sensor_disabled, self); g_signal_handlers_disconnect_by_func(manager, sensor_added, self); /* fake disabling of any sensors */ sensors = is_manager_get_enabled_sensors_list(manager); for (_list = sensors; _list != NULL; _list = _list->next) { IsSensor *sensor = IS_SENSOR(_list->data); _sensor_disabled(IS_SENSOR(_list->data), self); g_object_unref(sensor); } g_slist_free(sensors); #if !HAVE_APPINDICATOR g_object_set_data(G_OBJECT(self), "indicator-menu", NULL); #endif G_OBJECT_CLASS(is_indicator_parent_class)->dispose(object); }
static gboolean fake_add_enable_sensors(IsIndicator *self) { IsManager *manager; GSList *sensors, *_list; gint i = 0; manager = is_application_get_manager(self->priv->application); /* fake addition of any sensors */ sensors = is_manager_get_all_sensors_list(manager); for (_list = sensors; _list != NULL; _list = _list->next) { IsSensor *sensor = IS_SENSOR(_list->data); sensor_added(manager, IS_SENSOR(_list->data), self); g_object_unref(sensor); } g_slist_free(sensors); /* fake enabling of any sensors */ sensors = is_manager_get_enabled_sensors_list(manager); for (_list = sensors; _list != NULL; _list = _list->next) { IsSensor *sensor = IS_SENSOR(_list->data); sensor_enabled(manager, IS_SENSOR(_list->data), i++, self); g_object_unref(sensor); } g_slist_free(sensors); return FALSE; }
void is_temperature_sensor_set_scale(IsTemperatureSensor *self, IsTemperatureSensorScale scale) { IsTemperatureSensorPrivate *priv; g_return_if_fail(IS_IS_TEMPERATURE_SENSOR(self)); g_return_if_fail(scale == IS_TEMPERATURE_SENSOR_SCALE_CELSIUS || scale == IS_TEMPERATURE_SENSOR_SCALE_FAHRENHEIT); priv = self->priv; if (scale != priv->scale) { gdouble value = is_sensor_get_value(IS_SENSOR(self)); gdouble alarm_value = is_sensor_get_alarm_value(IS_SENSOR(self)); gdouble low_value = is_sensor_get_low_value(IS_SENSOR(self)); gdouble high_value = is_sensor_get_high_value(IS_SENSOR(self)); /* convert from current scale to new */ switch (priv->scale) { case IS_TEMPERATURE_SENSOR_SCALE_CELSIUS: value = celcius_to_fahrenheit(value); alarm_value = celcius_to_fahrenheit(alarm_value); low_value = celcius_to_fahrenheit(low_value); high_value = celcius_to_fahrenheit(high_value); break; case IS_TEMPERATURE_SENSOR_SCALE_FAHRENHEIT: value = fahrenheit_to_celcius(value); alarm_value = fahrenheit_to_celcius(alarm_value); low_value = fahrenheit_to_celcius(low_value); high_value = fahrenheit_to_celcius(high_value); break; case IS_TEMPERATURE_SENSOR_SCALE_INVALID: case NUM_IS_TEMPERATURE_SENSOR_SCALE: default: g_assert_not_reached(); break; } priv->scale = scale; is_sensor_set_units(IS_SENSOR(self), is_temperature_sensor_scale_to_string(priv->scale)); /* set all in one go */ g_object_set(self, "value", value, "alarm-value", alarm_value, "low-value", low_value, "high-value", high_value, NULL); } }
static void is_max_plugin_deactivate(PeasActivatable *activatable) { IsMaxPlugin *self = IS_MAX_PLUGIN(activatable); IsMaxPluginPrivate *priv = self->priv; IsManager *manager; GSList *sensors, *_list; is_debug("max", "dettaching from signals"); manager = is_application_get_manager(priv->application); is_manager_remove_path(manager, MAX_SENSOR_PATH); sensors = is_manager_get_enabled_sensors_list(manager); for (_list = sensors; _list != NULL; _list = _list->next) { IsSensor *sensor = IS_SENSOR(_list->data); on_sensor_disabled(manager, sensor, self); g_object_unref(sensor); } g_slist_free(sensors); g_signal_handlers_disconnect_by_func(manager, G_CALLBACK(on_sensor_enabled), self); g_signal_handlers_disconnect_by_func(manager, G_CALLBACK(on_sensor_disabled), self); }
void is_temperature_sensor_set_celsius_value(IsTemperatureSensor *self, gdouble value) { IsTemperatureSensorPrivate *priv; g_return_if_fail(IS_IS_TEMPERATURE_SENSOR(self)); priv = self->priv; switch (priv->scale) { case IS_TEMPERATURE_SENSOR_SCALE_CELSIUS: break; case IS_TEMPERATURE_SENSOR_SCALE_FAHRENHEIT: value = celcius_to_fahrenheit(value); break; case IS_TEMPERATURE_SENSOR_SCALE_INVALID: case NUM_IS_TEMPERATURE_SENSOR_SCALE: default: g_assert_not_reached(); } is_sensor_set_value(IS_SENSOR(self), value); }
static void is_temperature_sensor_init(IsTemperatureSensor *self) { IsTemperatureSensorPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(self, IS_TYPE_TEMPERATURE_SENSOR, IsTemperatureSensorPrivate); self->priv = priv; /* initialise scale to celcius */ priv->scale = IS_TEMPERATURE_SENSOR_SCALE_CELSIUS; is_sensor_set_units(IS_SENSOR(self), is_temperature_sensor_scale_to_string(priv->scale)); }
static void on_sensor_disabled(IsManager *manager, IsSensor *sensor, gpointer data) { IsMaxPlugin *self = (IsMaxPlugin *)data; IsMaxPluginPrivate *priv = self->priv; // don't bother monitoring non-temperature sensors if (IS_IS_TEMPERATURE_SENSOR(sensor)) { is_debug("max", "sensor disabled: %s", is_sensor_get_label(sensor)); g_signal_handlers_disconnect_by_func(sensor, G_CALLBACK(on_sensor_value_notify), self); if (priv->max == sensor) { // get all sensors and find the one with the maximum value and switch to // this GSList *sensors, *_list; priv->max = NULL; priv->max_value = 0.0; is_sensor_set_label(priv->sensor, "Δ"); is_sensor_set_icon(priv->sensor, IS_STOCK_CHIP); is_sensor_set_value(priv->sensor, 0.0); is_sensor_set_units(priv->sensor, ""); is_sensor_set_digits(priv->sensor, 1); sensors = is_manager_get_enabled_sensors_list(manager); for (_list = sensors; _list != NULL; _list = _list->next) { if (IS_IS_TEMPERATURE_SENSOR(_list->data)) { on_sensor_value_notify(IS_SENSOR(_list->data), NULL, self); } } } } }
static void sensor_menu_item_toggled(GtkMenuItem *menu_item, IsIndicator *self) { IsSensor *sensor; gboolean active = FALSE; g_return_if_fail(IS_IS_INDICATOR(self)); /* only set as primary if was toggled to active state since we may have * untoggled it instead */ active = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_item)); if (active) { sensor = IS_SENSOR(g_object_get_data(G_OBJECT(menu_item), "sensor")); is_debug("indicator", "Sensor %s menu-item toggled to active - setting as primary sensor", is_sensor_get_path(sensor)); is_indicator_set_primary_sensor_path(self, is_sensor_get_path(sensor)); } }
static void is_max_plugin_activate(PeasActivatable *activatable) { IsMaxPlugin *self = IS_MAX_PLUGIN(activatable); IsMaxPluginPrivate *priv = self->priv; IsManager *manager; GSList *sensors, *_list; int i = 0; manager = is_application_get_manager(priv->application); // create our virtual sensor which mimics the current highest value sensor's // value and label is_debug("max", "creating virtual sensor"); priv->sensor = is_sensor_new(MAX_SENSOR_PATH); is_sensor_set_label(priv->sensor, "Δ"); is_sensor_set_icon(priv->sensor, IS_STOCK_CHIP); is_sensor_set_value(priv->sensor, 0.0); is_sensor_set_units(priv->sensor, ""); is_sensor_set_digits(priv->sensor, 1); is_manager_add_sensor(manager, priv->sensor); is_debug("max", "attaching to signals"); sensors = is_manager_get_enabled_sensors_list(manager); for (_list = sensors; _list != NULL; _list = _list->next) { IsSensor *sensor = IS_SENSOR(_list->data); on_sensor_enabled(manager, sensor, i, self); g_object_unref(sensor); i++; } g_slist_free(sensors); g_signal_connect(manager, "sensor-enabled", G_CALLBACK(on_sensor_enabled), self); g_signal_connect(manager, "sensor-disabled", G_CALLBACK(on_sensor_disabled), self); }
static void update_sensor_value(IsTemperatureSensor *sensor, IsUdisksPlugin *self) { IsUdisksPluginPrivate *priv; gchar *device, *path; GDBusProxy *proxy; GVariant *var; GError *error = NULL; SkDisk *sk_disk; const gchar *blob; gsize len; guint64 temperature; gdouble value; priv = self->priv; device = g_path_get_basename(is_sensor_get_path(IS_SENSOR(sensor))); path = g_strdup_printf("%s/devices/%s", UDISKS_OBJECT_PATH, device); proxy = g_dbus_proxy_new_sync(priv->connection, G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, NULL, UDISKS_BUS_NAME, path, UDISKS_DEVICE_INTERFACE_NAME, NULL, &error); g_free(path); g_free(device); if (!proxy) { g_prefix_error(&error, _("Error reading new SMART data for sensor %s"), is_sensor_get_path(IS_SENSOR(sensor))); is_sensor_emit_error(IS_SENSOR(sensor), error); g_error_free(error); goto out; } /* update smart data */ var = g_variant_new_strv(NULL, 0); var = g_dbus_proxy_call_sync(proxy, "DriveAtaSmartRefreshData", g_variant_new_tuple(&var, 1), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!var) { g_prefix_error(&error, _("Error refreshing SMART data for sensor %s"), is_sensor_get_path(IS_SENSOR(sensor))); is_sensor_emit_error(IS_SENSOR(sensor), error); g_error_free(error); g_object_unref(proxy); goto out; } g_variant_unref(var); var = g_dbus_proxy_get_cached_property(proxy, "DriveAtaSmartBlob"); if (!var) { is_debug("udisks", "unable to get atasmartblob for sensor %s", is_sensor_get_path(IS_SENSOR(sensor))); g_object_unref(proxy); goto out; } g_object_unref(proxy); /* can't unref var until done with blob */ blob = g_variant_get_fixed_array(var, &len, sizeof(gchar)); if (!blob) { /* this can occur if udisks doesn't update immediately, * ignore */ g_variant_unref(var); goto out; } sk_disk_open(NULL, &sk_disk); sk_disk_set_blob(sk_disk, blob, len); if (sk_disk_smart_get_temperature(sk_disk, &temperature) < 0) { is_debug("udisks", "Error getting temperature from AtaSmartBlob for sensor %s", is_sensor_get_path(IS_SENSOR(sensor))); sk_disk_free(sk_disk); g_variant_unref(var); /* TODO: emit error */ goto out; } sk_disk_free(sk_disk); g_variant_unref(var); /* Temperature is in mK, so convert it to K first */ temperature /= 1000; value = (gdouble)temperature - 273.15; is_temperature_sensor_set_celsius_value(sensor, value); out: return; }
// Entry point of track-server void track_server_entry() { RegisterAs(TRACK_SERVER); // Initialize the track track_init(); // Initialize the reservation list reservation_init(train_reservations); // So we can update our own cached copy of the switch table int switch_courier_tid = CreateSwitchCourier(PRIORITY_HIGHEST - 1); int sender_tid; // Message buffer large enough to hold the largest message request char message_buffer[32]; while (1) { Receive(&sender_tid, message_buffer, sizeof(message_buffer)); if (sender_tid == switch_courier_tid) { // This is an update from the switch server, record the switch that changed // so that our cached table of switches is in sync int reply = 0; Reply(sender_tid, (char*)&reply, sizeof(reply)); struct switch_report* report = (struct switch_report*)message_buffer; switch_table[(int)report->sw] = report->direction; } else { // API requests to the track_server struct track_reply_message reply = {-1, -1}; int op = *(int*)message_buffer; switch(op) { // Handle track reservation requests case TRACK_OP_RESERVATION: { int reply = -1; struct reservation_request_message* reservation_msg = (struct reservation_request_message*)message_buffer; // Make the location be a sensor and positive offset that fits in the edge if (normalize_location(track, switch_table, &reservation_msg->position) != -1) { // Check if we can reserve this space for the train int status = reservation_verify(reservation_msg->train, &(reservation_msg->position), reservation_msg->length, reservation_msg->branch_safety); reply = status; if (status == 0) { // We succeeded. Reserve the track // Find the train's reservation_node struct reservation_node* train_r = get_reservation(train_reservations, reservation_msg->train); if (train_r != 0) { clear_train_reservations(train_r); // Add new reservation reservation_insert(train_r, &(reservation_msg->position), reservation_msg->length, reservation_msg->branch_safety); } } else if (status == -2) { // Reached max reservation } } else { Printf("Could not normalize\r"); } Reply(sender_tid, (char*)&reply, sizeof(reply)); } break; case TRACK_OP_RESERVATION_STRING: { struct reservation_request_message* reservation_msg = (struct reservation_request_message*)message_buffer; struct reservation_node* train_r = get_reservation(train_reservations, reservation_msg->train); char buf[300]; *buf = 0; // null termination character int len = 0; if (train_r != 0) { len = reservation_print(train_r, buf); } Reply(sender_tid, (char*)buf, len); // +1 for null termination char } break; case TRACK_OP_NODE_IS_RESERVED: { struct reservation_request_message* reservation_msg = (struct reservation_request_message*)message_buffer; int reserved = node_is_reserved(reservation_msg->train, reservation_msg->position.node); Reply(sender_tid, (char*)&reserved, sizeof(reserved)); } break; // Handle requests for calculating the distance between 2 nodes case TRACK_OP_TRACK_DISTANCE: { struct track_request_message* track_msg = (struct track_request_message*)message_buffer; if (track_msg->node1 < TRACK_MAX && track_msg->node2 < TRACK_MAX) { reply.distance = dist_between_nodes(track_msg->node1, track_msg->node2); } Reply(sender_tid, (char*)&reply, sizeof(reply)); } break; case TRACK_OP_TRACK_DISTANCE_UNTIL_GOAL: { struct track_request_message* track_msg = (struct track_request_message*)message_buffer; int distance = 0; if (track_msg->node1 < TRACK_MAX && track_msg->node2 < TRACK_MAX) { distance = dist_until_goal(track_msg->node1, track_msg->offset1, track_msg->node2, track_msg->offset2, track_msg->distance); } Reply(sender_tid, (char*)&distance, sizeof(distance)); } break; // Handle requests for getting the next sensor on the track case TRACK_OP_TRACK_NEXT_SENSOR: { struct track_request_message* track_msg = (struct track_request_message*)message_buffer; if (track_msg->node1 >= A1 && track_msg->node1 <= E16) { int distance; int next_sensor = next_sensor_node(track_msg->node1, track_msg->node2, &distance); reply.node = next_sensor; reply.distance = distance; } Reply(sender_tid, (char*)&reply, sizeof(reply)); } break; case TRACK_OP_NORMALIZE: { struct track_request_message *msg = (struct track_request_message*)message_buffer; struct location loc; loc.node = msg->node1; loc.offset = msg->offset1; if (normalize_location(track, switch_table, &loc) == -1) { Reply(sender_tid, (char*)&loc, 0); // reply size 0 means error! } Reply(sender_tid, (char*)&loc, sizeof(loc)); } break; // Handle requests for getting the reverse node of a given node case TRACK_OP_TRACK_REVERSE_NODE: { struct track_request_message* track_msg = (struct track_request_message*)message_buffer; if (track_msg->node1 < TRACK_MAX) { reply.node = track[track_msg->node1].reverse->num; } Reply(sender_tid, (char*)&reply, sizeof(reply)); } break; case TRACK_OP_ROUTE: { struct track_request_message* track_msg = (struct track_request_message*)message_buffer; int train = track_msg->train; int start_node_num = track_msg->node1; int end_node_num = track_msg->node2; short path[TRACK_MAX]; int num_nodes = 0; if (IS_SENSOR(start_node_num) && IS_SENSOR(end_node_num)) { struct track_node *start_node = &track[start_node_num]; struct track_node *end_node = &track[end_node_num]; mark_path(train, track, start_node, end_node); if (end_node->routing_info.visited == 1) { // we have a path! // store all nodes, from bottom up struct track_node *cur_node = end_node; struct location loc; while (cur_node->routing_info.previous != 0 && cur_node != start_node) { node_to_location(cur_node, &loc); path[num_nodes] = loc.node; num_nodes++; if (cur_node->routing_info.previous == cur_node->reverse) { // the next node is the reverse of this node, so we squeeze in a "REVERSE" in the path path[num_nodes] = -99; // magic number for REVERSE commands num_nodes++; } cur_node = cur_node->routing_info.previous; } // we didn't store the beginning node in our path, so we should do that now if (cur_node == start_node) { node_to_location(start_node, &loc); path[num_nodes] = loc.node; num_nodes++; } } else { Printf("Could not find route! num_nodes = %d\r", num_nodes); } } Reply(sender_tid, (char*)path, sizeof(short) * num_nodes); } break; case TRACK_OP_SWITCH_DIRECTION: { struct track_request_message* track_msg = (struct track_request_message*)message_buffer; int switch_node = track_msg->node1; int next_node = track_msg->node2; AssertF(track[switch_node].type == NODE_BRANCH, "TrackSwitchDirection not given a switch node! Given node num %d, type %d", track[switch_node].num, track[switch_node].type); int reserved = node_is_reserved(track_msg->train, switch_node); if (reserved) { // Printf("Attempting to switch %s if reserved .. \r", track[switch_node].name); int direction = -1; if (track[switch_node].edge[DIR_CURVED].dest == &(track[next_node])) { direction = SWITCH_CURVED; } else { direction = SWITCH_STRAIGHT; } SwitchSetDirection(track[switch_node].num, direction, WhoIs(SWITCH_SERVER)); switch_table[track[switch_node].num] = direction; } Reply(sender_tid, (char*)&reserved, sizeof(reserved)); } break; default: AssertF(0, "Invalid message %d to the track server from %d", op, sender_tid); Reply(sender_tid, (char*)&reply, sizeof(reply)); break; } } } Assert("Track server is quitting"); Exit(); }