/*===========================================================================* * driver_init * *===========================================================================*/ void driver_init(void) { /* Initialize the driver layer. */ int r; memset(driver, 0, sizeof(driver)); /* Endpoints unknown. */ driver[DRIVER_MAIN].endpt = NONE; driver[DRIVER_BACKUP].endpt = NONE; /* Get disk driver's and this proc's endpoint. */ driver[DRIVER_MAIN].label = MAIN_LABEL; driver[DRIVER_MAIN].minor = MAIN_MINOR; /* No up received yet but expected when the driver starts. */ driver[DRIVER_MAIN].up_event = UP_EXPECTED; driver[DRIVER_BACKUP].up_event = UP_EXPECTED; r = ds_retrieve_label_endpt(driver[DRIVER_MAIN].label, &driver[DRIVER_MAIN].endpt); if (r != OK) { printf("Filter: failed to get main disk driver's endpoint: " "%d\n", r); bad_driver(DRIVER_MAIN, BD_DEAD, EFAULT); check_driver(DRIVER_MAIN); } else if (driver_open(DRIVER_MAIN) != OK) { panic("unhandled driver_open failure"); } if(USE_MIRROR) { driver[DRIVER_BACKUP].label = BACKUP_LABEL; driver[DRIVER_BACKUP].minor = BACKUP_MINOR; if(!strcmp(driver[DRIVER_MAIN].label, driver[DRIVER_BACKUP].label)) { panic("same driver: not tested"); } r = ds_retrieve_label_endpt(driver[DRIVER_BACKUP].label, &driver[DRIVER_BACKUP].endpt); if (r != OK) { printf("Filter: failed to get backup disk driver's " "endpoint: %d\n", r); bad_driver(DRIVER_BACKUP, BD_DEAD, EFAULT); check_driver(DRIVER_BACKUP); } else if (driver_open(DRIVER_BACKUP) != OK) { panic("unhandled driver_open failure"); } } }
/* Zoom - to region */ int zoom_region(void) { struct Cell_head win; char *mapset; G_debug(2, "zoom_region()"); driver_open(); mapset = G_find_file2("windows", var_getc(VAR_ZOOM_REGION), NULL); if (mapset == NULL) { G_warning(_("Cannot find window '%s'"), var_getc(VAR_ZOOM_REGION)); return 0; } G__get_window(&win, "windows", var_getc(VAR_ZOOM_REGION), mapset); G_put_window(&win); G_set_window(&win); display_redraw(); driver_close(); G_debug(3, "zoom_region(): End"); return 1; }
int main (int argc, char **argv) { SensorData *data; GUdevClient *client; int ret = 0; const gchar * const subsystems[] = { "iio", "input", "platform", NULL }; guint i; /* g_setenv ("G_MESSAGES_DEBUG", "all", TRUE); */ data = g_new0 (SensorData, 1); data->previous_orientation = ORIENTATION_UNDEFINED; data->uses_lux = TRUE; /* Set up D-Bus */ setup_dbus (data); client = g_udev_client_new (subsystems); if (!find_sensors (client, data)) { g_debug ("Could not find any supported sensors"); return 0; } g_signal_connect (G_OBJECT (client), "uevent", G_CALLBACK (sensor_changes), data); for (i = 0; i < NUM_SENSOR_TYPES; i++) { data->clients[i] = create_clients_hash_table (); if (!driver_type_exists (data, i)) continue; if (!driver_open (DRIVER_FOR_TYPE(i), DEVICE_FOR_TYPE(i), driver_type_to_callback_func (data->drivers[i]->type), data)) { DRIVER_FOR_TYPE(i) = NULL; g_clear_object (&DEVICE_FOR_TYPE(i)); } } if (!any_sensors_left (data)) goto out; data->init_done = TRUE; if (data->connection) { send_dbus_event (data, PROP_ALL); send_dbus_event (data, PROP_ALL_COMPASS); } data->loop = g_main_loop_new (NULL, TRUE); g_main_loop_run (data->loop); out: free_orientation_data (data); return ret; }
static int context_open_unlocked(ca_context *c) { int ret; ca_return_val_if_fail(!ca_detect_fork(), CA_ERROR_FORKED); ca_return_val_if_fail(c, CA_ERROR_INVALID); if (c->opened) return CA_SUCCESS; if ((ret = driver_open(c)) == CA_SUCCESS) c->opened = TRUE; return ret; }
/*===========================================================================* * check_driver * *===========================================================================*/ int check_driver(int which) { /* See if the given driver has been troublesome, and if so, deal with * it. */ int problem, tell_rs; int r, retries = 0; problem = driver[which].problem; if (problem == BD_NONE) return OK; do { if(retries) { #if DEBUG printf("Filter: check_driver: retry number %d\n", retries); #endif problem = BD_PROTO; } retries++; driver[which].problem = BD_NONE; /* Decide what to do: continue operation, restart the driver, * or return an error. */ r = check_problem(which, problem, retries, &tell_rs); if (r != EAGAIN) return r; /* Restarting the driver it is. First tell RS (if necessary), * then wait for the new driver instance to come up. */ restart_driver(which, tell_rs); /* Finally, open the device on the new driver */ } while (driver_open(which) != OK); #if DEBUG printf("Filter: check_driver restarted driver %d, endpoint %d\n", which, driver[which].endpt); #endif return OK; }
/* Zoom - default region */ int zoom_default(void) { struct Cell_head defwin; G_debug(2, "zoom_default()"); driver_open(); G_get_default_window(&defwin); G_put_window(&defwin); G_set_window(&defwin); display_redraw(); driver_close(); G_debug(3, "zoom_default(): End"); return 1; }
/* Zoom - in / out (centre unchanged) */ int zoom_centre(double factor) { double xc, yc, dx, dy; G_debug(2, "zoom_centre()"); driver_open(); G_debug(2, "1 n = %f s = %f", window.north, window.south); dx = (window.east - window.west) / 2; dy = (window.north - window.south) / 2; xc = (window.east + window.west) / 2; yc = (window.north + window.south) / 2; G_debug(2, " yc = %f dy = %f", yc, dy); window.north = yc + dy * factor; window.south = yc - dy * factor; window.east = xc + dx * factor; window.west = xc - dx * factor; G_debug(2, "2 n = %f s = %f", window.north, window.south); G_adjust_Cell_head(&window, 0, 0); G_debug(2, "3 n = %f s = %f", window.north, window.south); G_put_window(&window); G_set_window(&window); display_redraw(); driver_close(); G_debug(3, "zoom_centre(): End"); return 1; }
int main(int argc, char **argv) { struct stat s; char path[PATH_MAX]; char inipath[PATH_MAX]; const char *opt_directory_name = NULL; const char *opt_driver_name = NULL; int opt_driver_type = DRIVER_SERIAL; const char *opt_device_name = NULL; int opt_format_list[5] = { 0 /* FORMAT_UNKNOWN */ }; int opt_format_index = 0; BUF *files; int opt_time = 0; int opt_user = 0; int opt_listen = 0; int ch; int ok; char *ap; int format; int ret; int i; snprintf(inipath, PATH_MAX, "%s/.s725rc", getenv("HOME")); yyin = fopen(inipath, "r"); if (yyin != NULL) { conf_filename = inipath; ret = yyparse(); if (ret != 0) exit(1); if (conf_driver_type != DRIVER_UNKNOWN) opt_driver_type = conf_driver_type; if (conf_format_type != FORMAT_UNKNOWN) opt_format_list[opt_format_index] = conf_format_type; if (conf_device_name != NULL) opt_device_name = conf_device_name; if (conf_directory_name != NULL) opt_directory_name = conf_directory_name; } while ((ch = getopt(argc, argv, "d:D:f:hlo:rtuv")) != -1) { switch (ch) { case 'd': opt_driver_name = optarg; opt_driver_type = driver_name_to_type(opt_driver_name); if (opt_driver_type == DRIVER_UNKNOWN) fatalx("unknown driver type: %s", opt_driver_name); break; case 'D': opt_device_name = optarg; break; case 'f': opt_directory_name = optarg; break; case 'l': opt_listen = 1; break; case 'o': ap = optarg; if (opt_format_index < (sizeof(opt_format_list) / sizeof(opt_format_list[0]))) { if ((format = format_from_str(ap)) != FORMAT_UNKNOWN) opt_format_list[opt_format_index++] = format; else fatalx("unknown output format: %s", ap); } break; case 't': opt_time = 1; break; case 'u': opt_user = 1; break; case 'v': log_add_level(); break; case 'h': usage(); return 0; break; default: usage(); return 1; } } if (opt_driver_type == DRIVER_SERIAL) { if (!opt_device_name) fatalx("device name required for %s driver", driver_type_to_name(opt_driver_type)); } if (! (opt_time || opt_user)) { if (opt_format_list[0] == FORMAT_UNKNOWN) fatalx("no output format specified"); } for (i = 0; i < sizeof(opt_format_list) / sizeof(opt_format_list[0]); ++i) { log_info("format: %s", format_to_str(opt_format_list[i])); } log_info("driver name: %s", driver_type_to_name(opt_driver_type)); log_info("driver type: %d", opt_driver_type); log_info("device name: %s", opt_device_name ? opt_device_name : ""); log_info("directory name: %s", opt_directory_name ? opt_directory_name : ""); ok = driver_init(opt_driver_type, opt_device_name); if (ok != 1) fatalx("driver_init failed"); if (opt_directory_name) { opt_directory_name = realpath(opt_directory_name, path); } else { getcwd(path, sizeof(path)); opt_directory_name = path; } if (!opt_directory_name) fatalx("could not resolve path. check -f"); if (access(opt_directory_name, R_OK | W_OK | X_OK) != 0) fatalx("unable to access directory: %s", opt_directory_name); if (stat(opt_directory_name, &s) != 0) fatalx("unable to stat directory: %s", opt_directory_name); if (! S_ISDIR(s.st_mode)) fatalx("not a directory directory: %s", opt_directory_name); if (driver_open() < 0) fatalx("unable to open port: %s", strerror(errno)); if (opt_time) { time_get(); driver_close(); return 0; } if (opt_user) { user_get(); driver_close(); return 0; } files = buf_alloc(0); if (opt_listen) { ret = files_listen(files); } else { ret = files_get(files); } if (ret) { for (i = 0; i < sizeof(opt_format_list) / sizeof(opt_format_list[0]); ++i) { if (opt_format_list[i] != FORMAT_UNKNOWN) { write_hrm_data(files, opt_directory_name, opt_format_list[i]); } } } buf_free(files); driver_close(); return 0; }
/*===========================================================================* * check_problem * *===========================================================================*/ static int check_problem(int which, int problem, int retries, int *tell_rs) { /* A problem has occurred with a driver. Update statistics, and decide * what to do. If EAGAIN is returned, the driver should be restarted; * any other result will be passed up. */ #if DEBUG printf("Filter: check_problem processing driver %d, problem %d\n", which, problem); #endif problem_stats[problem]++; if(new_driver_ep(which)) { #if DEBUG printf("Filter: check_problem: noticed a new driver\n"); #endif if(driver_open(which) == OK) { #if DEBUG2 printf("Filter: open OK -> no recovery\n"); #endif return OK; } else { #if DEBUG2 printf("Filter: open not OK -> recovery\n"); #endif problem = BD_PROTO; problem_stats[problem]++; } } /* If the driver has died, we always need to restart it. If it has * been giving problems, we first retry the request, up to N times, * after which we kill and restart the driver. We restart the driver * up to M times, after which we remove the driver from the mirror * configuration. If we are not set up to do mirroring, we can only * do one thing, and that is continue to limp along with the bad * driver.. */ switch(problem) { case BD_PROTO: case BD_DATA: driver[which].retries++; #if DEBUG printf("Filter: disk driver %d has had " "%d/%d retry attempts, %d/%d kills\n", which, driver[which].retries, NR_RETRIES, driver[which].kills, NR_RESTARTS); #endif if (driver[which].retries < NR_RETRIES) { if(retries == 1) { #if DEBUG printf("Filter: not restarting; retrying " "(retries %d/%d, kills %d/%d)\n", driver[which].retries, NR_RETRIES, driver[which].kills, NR_RESTARTS); #endif return OK; } #if DEBUG printf("Filter: restarting (retries %d/%d, " "kills %d/%d, internal retry %d)\n", driver[which].retries, NR_RETRIES, driver[which].kills, NR_RESTARTS, retries); #endif } #if DEBUG printf("Filter: disk driver %d has reached error " "threshold, restarting driver\n", which); #endif *tell_rs = (driver[which].up_event != UP_PENDING); break; case BD_DEAD: /* Can't kill that which is already dead.. */ *tell_rs = 0; break; default: panic("invalid problem: %d", problem); } /* At this point, the driver will be restarted. */ driver[which].retries = 0; driver[which].kills++; if (driver[which].kills < NR_RESTARTS) return EAGAIN; /* We've reached the maximum number of restarts for this driver. */ if (USE_MIRROR) { printf("Filter: kill threshold reached, disabling mirroring\n"); USE_MIRROR = 0; if (which == DRIVER_MAIN) { driver[DRIVER_MAIN] = driver[DRIVER_BACKUP]; /* This is not necessary. */ strcpy(MAIN_LABEL, BACKUP_LABEL); MAIN_MINOR = BACKUP_MINOR; } driver[DRIVER_BACKUP].endpt = NONE; return OK; } else { /* We tried, we really did. But now we give up. Tell the user. */ printf("Filter: kill threshold reached, returning error\n"); if (driver[which].error == EAGAIN) return EIO; return driver[which].error; } }
int write_line(struct Map_info *Map, int type, struct line_pnts *Points) { int i, field, cat, ret; static int first_form = 1; char *form; struct line_cats *Cats; struct field_info *Fi; dbString html; Cats = Vect_new_cats_struct(); db_init_string(&html); cat = var_geti(VAR_CAT); if (var_geti(VAR_CAT_MODE) != CAT_MODE_NO && cat > 0 && var_geti(VAR_FIELD) > 0) { Vect_cat_set(Cats, var_geti(VAR_FIELD), cat); G_debug(2, "write field = %d cat = %d", var_geti(VAR_FIELD), var_geti(VAR_CAT)); if (cat_max_get(var_geti(VAR_FIELD)) < var_geti(VAR_CAT)) { cat_max_set(var_geti(VAR_FIELD), var_geti(VAR_CAT)); } } ret = Vect_write_line(Map, type, Points, Cats); for (i = 0; i < Vect_get_num_updated_lines(Map); i++) G_debug(2, "Updated line: %d", Vect_get_updated_line(Map, i)); for (i = 0; i < Vect_get_num_updated_nodes(Map); i++) G_debug(2, "Updated node: %d", Vect_get_updated_node(Map, i)); /* Reset category (this automatically resets cat for next not used) */ var_seti(VAR_FIELD, var_geti(VAR_FIELD)); if (var_geti(VAR_CAT_MODE) != CAT_MODE_NO && var_geti(VAR_INSERT) && cat > 0) { G_debug(2, "Insert new record"); db_set_string(&html, "<HTML><HEAD><TITLE>"); db_append_string(&html, _("Form")); db_append_string(&html, "</TITLE><BODY>"); field = var_geti(VAR_FIELD); ret = new_record(field, cat); if (ret == -1) { return -1; } else if (ret == 0) { db_append_string(&html, _("New record was created.<BR>")); } else { /* record already existed */ db_append_string(&html, _("Record for this category already existed.<BR>")); } /* Open form */ Fi = Vect_get_field(Map, field); if (Fi == NULL) { return -1; } F_generate(Fi->driver, Fi->database, Fi->table, Fi->key, cat, NULL, NULL, F_EDIT, F_HTML, &form); db_append_string(&html, form); db_append_string(&html, "</BODY></HTML>"); /* Note: F_open() must be run first time with closed monitor, otherwise next * attempt to open driver hangs until form child process is killed */ if (first_form) driver_close(); F_clear(); F_open(_("Attributes"), db_get_string(&html)); if (first_form) { driver_open(); first_form = 0; } G_free(form); db_free_string(&html); } return 0; }
static void sensor_changes (GUdevClient *client, gchar *action, GUdevDevice *device, SensorData *data) { guint i; if (g_strcmp0 (action, "remove") == 0) { for (i = 0; i < NUM_SENSOR_TYPES; i++) { GUdevDevice *dev = DEVICE_FOR_TYPE(i); if (!dev) continue; if (g_strcmp0 (g_udev_device_get_sysfs_path (device), g_udev_device_get_sysfs_path (dev)) == 0) { g_debug ("Sensor type %s got removed (%s)", driver_type_to_str (i), g_udev_device_get_sysfs_path (dev)); g_clear_object (&DEVICE_FOR_TYPE(i)); DRIVER_FOR_TYPE(i) = NULL; g_clear_pointer (&data->clients[i], g_hash_table_unref); data->clients[i] = create_clients_hash_table (); send_driver_changed_dbus_event (data, i); } } if (!any_sensors_left (data)) g_main_loop_quit (data->loop); } else if (g_strcmp0 (action, "add") == 0) { guint i; for (i = 0; i < G_N_ELEMENTS(drivers); i++) { SensorDriver *driver = (SensorDriver *) drivers[i]; if (!driver_type_exists (data, driver->type) && driver_discover (driver, device)) { g_debug ("Found hotplugged device %s of type %s at %s", g_udev_device_get_sysfs_path (device), driver_type_to_str (driver->type), driver->name); if (driver_open (driver, device, driver_type_to_callback_func (driver->type), data)) { GHashTable *ht; DEVICE_FOR_TYPE(driver->type) = g_object_ref (device); DRIVER_FOR_TYPE(driver->type) = (SensorDriver *) driver; send_driver_changed_dbus_event (data, driver->type); ht = data->clients[driver->type]; if (g_hash_table_size (ht) > 0) driver_set_polling (DRIVER_FOR_TYPE(driver->type), TRUE); } break; } } } }