/** Function GetDevices * @brief Request all devices from SANE * * @param[out] device_list pointer to -> NULL terminated array of SANE_Device's * @param[out] size the number of devices * @return 0 OK - else error * */ int GetDevices(const SANE_Device *** device_list, int *size) { int status, s = 0; const SANE_Device **dl = NULL; printf(PRFX "Scanning SANE devices..."); fflush(NULL); status = sane_get_devices(&dl, SANE_FALSE); if (status != SANE_STATUS_GOOD) { SANE_msg(oyMSG_WARN, 0, "%s()\n Cannot get sane devices: %s\n", __func__, sane_strstatus(status)); fflush(NULL); return 1; } *device_list = dl; while (dl[s]) s++; *size = s; printf("OK [%d]\n", s); fflush(NULL); return 0; }
void FindSaneDevicesThread::run() { SANE_Device const **devList; //SANE_Int version; SANE_Status status; // This is unfortunately not very reliable as many back-ends do not refresh // the device list after the sane_init() call... status = sane_get_devices(&devList, SANE_FALSE); m_deviceList.clear(); if (status == SANE_STATUS_GOOD) { int i = 0; KSaneWidget::DeviceInfo tmp; while(devList[i] != 0) { tmp.name = devList[i]->name; tmp.vendor = devList[i]->vendor; tmp.model = devList[i]->model; tmp.type = devList[i]->type; m_deviceList << tmp; i++; } } }
SANE_Status sane_open (SANE_String_Const name, SANE_Handle * h) { struct device *dev; DBG (3, "%s: '%s'\n", __FUNCTION__, name); if (!devlist) sane_get_devices (NULL, SANE_TRUE); if (!name || !*name) { /* special case of empty name: open first available device */ for (dev = devices_head; dev; dev = dev->next) { if (dev->dn != -1) { if (sane_open (dev->sane.name, h) == SANE_STATUS_GOOD) return SANE_STATUS_GOOD; } } } else { for (dev = devices_head; dev; dev = dev->next) { if (strcmp(name, dev->sane.name) == 0) { *h = dev; return dev->io->dev_open(dev); } } } return SANE_STATUS_INVAL; }
static PyObject *getDevices (PyObject * self, PyObject * args) { const SANE_Device **device_list; SANE_Status st; PyObject *list; int local_only=SANE_FALSE, i; if (!PyArg_ParseTuple (args, "|i", &local_only)) raiseError("Invalid arguments"); st = sane_get_devices (&device_list, local_only); if (st != SANE_STATUS_GOOD) return raiseSaneError (st); if (!(list = PyList_New (0))) return raiseError("Unable to allocate device list."); for (i=0; device_list[i]; i++) { PyList_Append (list, Py_BuildValue ("ssss", device_list[i]->name, device_list[i]->vendor, device_list[i]->model, device_list[i]->type)); } return list; }
static void build_menu(Widget widget, XtPointer clientdata, XtPointer call_data) { WidgetList children,wlist; Cardinal nchildren; const SANE_Device **list; Widget push; XmString str; char action[256]; int rc,i; /* del old */ XtVaGetValues(widget, XtNchildren,&children, XtNnumChildren,&nchildren, NULL); wlist = malloc(sizeof(Widget*)*nchildren); memcpy(wlist,children,sizeof(Widget*)*nchildren); for (i = 0; i < nchildren; i++) XtDestroyWidget(wlist[i]); free(wlist); /* create new */ if (SANE_STATUS_GOOD != (rc = sane_init(NULL,NULL))) { fprintf(stderr,"sane_init: %s\n",sane_strstatus(rc)); goto done; } sane_get_devices(&list,0); if (NULL == list[0]) goto done; for (i = 0; list[i] != NULL; i++) { if (debug) fprintf(stderr,"sane dev: %s | %s | %s | %s\n", list[i]->name, list[i]->vendor, list[i]->model, list[i]->type); str = XmStringGenerate((char*)list[i]->model, NULL, XmMULTIBYTE_TEXT, NULL); push = XtVaCreateManagedWidget(list[i]->name, xmPushButtonWidgetClass,widget, XmNlabelString,str, NULL); XmStringFree(str); sprintf(action,"Scan(%s)",list[i]->name); XtAddCallback(push,XmNactivateCallback,action_cb,strdup(action)); } done: sane_exit(); }
SANE_Status sane_open (SANE_String_Const name, SANE_Handle * h) { struct device_s *dev; int ret; if(!devlist_head) sane_get_devices(NULL,(SANE_Bool)0); dev = devlist_head; if (strlen (name)) for (; dev; dev = dev->next) if (!strcmp (name, dev->devname)) break; if (!dev) { DBG(1,"Unable to find device %s\n",name); return SANE_STATUS_INVAL; } DBG(1,"Found device %s\n",name); /* Now open the usb device */ ret = sanei_usb_open (name, &(dev->dn)); if (ret != SANE_STATUS_GOOD) { DBG(1,"Unable to open device %s\n",name); return ret; } /* Claim the first interface */ ret = sanei_usb_claim_interface (dev->dn, 0); if (ret != SANE_STATUS_GOOD) { sanei_usb_close (dev->dn); /* if we cannot claim the interface, this is because someone else is using it */ DBG(1,"Unable to claim scanner interface on device %s\n",name); return SANE_STATUS_DEVICE_BUSY; } #ifdef HAVE_SANEI_USB_SET_TIMEOUT sanei_usb_set_timeout (30000); /* 30s timeout */ #endif *h = dev; return SANE_STATUS_GOOD; }
/** * cd_sane_client_refresh: **/ static void cd_sane_client_refresh (CdMainPrivate *priv) { CdMainDev *tmp; const SANE_Device **device_list = NULL; gint idx; guint i; SANE_Status status; status = sane_init (NULL, NULL); if (status != SANE_STATUS_GOOD) { g_warning ("failed to init SANE: %s", sane_strstatus (status)); goto out; } /* get scanners on the local server */ status = sane_get_devices (&device_list, TRUE); if (status != SANE_STATUS_GOOD) { g_warning ("failed to get devices from SANE: %s", sane_strstatus (status)); goto out; } /* nothing */ if (device_list == NULL || device_list[0] == NULL) goto out; /* add them */ for (idx = 0; device_list[idx] != NULL; idx++) cd_sane_client_add (priv, device_list[idx]); /* remove any that are invalid */ for (i = 0; i < priv->array->len; i++) { tmp = g_ptr_array_index (priv->array, i); if (tmp->valid) continue; cd_sane_client_remove (priv, tmp->device); } out: g_main_loop_quit (priv->loop); }
/* Open device, return the device handle */ SANE_Status sane_open (SANE_String_Const devname, SANE_Handle * handle) { unsigned i, j, id = 0; struct scanner *s; SANE_Int h, bus; SANE_Status st = SANE_STATUS_GOOD; if (!devlist) { st = sane_get_devices (NULL, 0); if (st) return st; } for (i = 0; devlist[i]; i++) { if (!strcmp (devlist[i]->name, devname)) break; } if (!devlist[i]) return SANE_STATUS_INVAL; for (j = 0; j < sizeof (known_devices) / sizeof (known_devices[0]); j++) { if (!strcmp (devlist[i]->model, known_devices[j].scanner.model)) { id = known_devices[j].id; break; } } st = sanei_usb_open (devname, &h); if (st == SANE_STATUS_ACCESS_DENIED) return st; if (st) { st = sanei_scsi_open (devname, &h, kvs40xx_sense_handler, NULL); if (st) { return st; } bus = SCSI; } else { bus = USB; st = sanei_usb_claim_interface (h, 0); if (st) { sanei_usb_close (h); return st; } } s = malloc (sizeof (struct scanner)); if (!s) return SANE_STATUS_NO_MEM; memset (s, 0, sizeof (struct scanner)); s->buffer = malloc (MAX_READ_DATA_SIZE + BULK_HEADER_SIZE); if (!s->buffer) return SANE_STATUS_NO_MEM; s->file = h; s->bus = bus; s->id = id; strcpy (s->name, devname); *handle = s; for (i = 0; i < 3; i++) { st = kvs40xx_test_unit_ready (s); if (st) { if (s->bus == SCSI) { sanei_scsi_close (s->file); st = sanei_scsi_open (devname, &h, kvs40xx_sense_handler, NULL); if (st) return st; } else { sanei_usb_release_interface (s->file, 0); sanei_usb_close (s->file); st = sanei_usb_open (devname, &h); if (st) return st; st = sanei_usb_claim_interface (h, 0); if (st) { sanei_usb_close (h); return st; } } s->file = h; } else break; } if (i == 3) return SANE_STATUS_DEVICE_BUSY; if (id == KV_S4085C || id == KV_S4065C) { char str[16]; st = inquiry (s, str); if (st) goto err; if (id == KV_S4085C) s->id = !strcmp (str, "KV-S4085CL") ? KV_S4085CL : KV_S4085CW; else s->id = !strcmp (str, "KV-S4065CL") ? KV_S4065CL : KV_S4065CW; } kvs40xx_init_options (s); st = kvs40xx_set_timeout (s, s->val[FEED_TIMEOUT].w); if (st) goto err; return SANE_STATUS_GOOD; err: sane_close (s); return st; }
extern char *internalGetScannerList(char *lang) { char *answer = NULL; SANE_Status status; const SANE_Device **SANE_device_list; int scanOK = SANE_FALSE; char *deviceList; status = sane_get_devices (&SANE_device_list, SANE_TRUE); if(status == SANE_STATUS_GOOD) { if (SANE_device_list && SANE_device_list[0]) { scanOK = SANE_TRUE; o_log(DEBUGM, "device(s) found"); } else { o_log(INFORMATION, "No devices found"); } } else { o_log(WARNING, "Checking for devices failed"); } if(scanOK == SANE_TRUE) { int i = 0; char *replyTemplate; replyTemplate = o_strdup("<Device><vendor>%s</vendor><model>%s</model><type>%s</type><name>%s</name><host>%s</host></Device>"); deviceList = o_strdup(""); for (i=0 ; SANE_device_list[i] ; i++) { char *vendor, *model, *type, *name; char *scannerHost; vendor = o_strdup(SANE_device_list[i]->vendor); model = o_strdup(SANE_device_list[i]->model); type = o_strdup(SANE_device_list[i]->type); name = o_strdup(SANE_device_list[i]->name); propper(vendor); propper(model); propper(type); // Find location of the device if ( name && name == strstr(name, "net:") ) { struct sockaddr_in sa; char *ipandmore, *ip; char host[NI_MAXHOST]; char service[NI_MAXSERV]; int len; // Ignore the 'net:' part ipandmore = name + 4; // Find the length of the address part len = strstr(ipandmore, ":") - ipandmore; // Load 'ip' with the network addres ip = malloc(1+(size_t)len); (void) strncpy(ip,ipandmore,(size_t)len); ip[len] = '\0'; // Convert into an inet address memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr( ip ); // Lookup hostname from address o_log(DEBUGM, "Going to lookup: %s", ip); if ( getnameinfo((struct sockaddr *)&sa, sizeof sa, host, sizeof host, service, sizeof service, NI_NAMEREQD) == 0 ) { o_log(DEBUGM, "found host: %s", host); scannerHost = o_strdup(host); } else { o_log(DEBUGM, "Could not get hostname"); scannerHost = o_strdup(ip); } // Clean up free(ip); } else { scannerHost = o_strdup( getString("LOCAL_opendias_server", lang) ); } // Build Reply // o_concatf(&deviceList, replyTemplate, vendor, model, type, name, scannerHost); free(vendor); free(model); free(type); free(name); free(scannerHost); } free(replyTemplate); if(deviceList) { // The escaped string placeholder will be interprited in the sane dispatcher client answer = o_printf("<?xml version='1.0' encoding='utf-8'?>\n<Response><ScannerList%%s><Devices>%s</Devices></ScannerList></Response>", deviceList); free(deviceList); } else { // No devices // The escaped string placeholder will be interprited in the sane dispatcher client answer = o_strdup( "<?xml version='1.0' encoding='utf-8'?>\n<Response><ScannerList%s></ScannerList></Response>"); } } else { // sane failed. // The escaped string placeholder will be interprited in the sane dispatcher client answer = o_strdup( "<?xml version='1.0' encoding='utf-8'?>\n<Response><ScannerList%s></ScannerList></Response>"); } return answer; }
void testsane (const char *dev_name) { int hlp, x; SANE_Status bla; SANE_Int blubb; SANE_Handle hand; SANE_Parameters pars; const SANE_Option_Descriptor *sod; const SANE_Device **device_list; char buffer[2048]; bla = sane_init (&blubb, auth_callback); fprintf (stderr, "Init : stat=%d ver=%x\nPress Enter to continue...", bla, blubb); getchar (); if (bla != SANE_STATUS_GOOD) return; bla = sane_get_devices (&device_list, SANE_FALSE); fprintf (stderr, "GetDev : stat=%s\n", sane_strstatus (bla)); if (bla != SANE_STATUS_GOOD) return; bla = sane_open (dev_name, &hand); fprintf (stderr, "Open : stat=%s hand=%p\n", sane_strstatus (bla), hand); if (bla != SANE_STATUS_GOOD) return; bla = sane_set_io_mode (hand, 0); fprintf (stderr, "SetIoMode : stat=%s\n", sane_strstatus (bla)); for (hlp = 0; hlp < 9999; hlp++) { sod = sane_get_option_descriptor (hand, hlp); if (sod == NULL) break; fprintf (stderr, "Gopt(%d) : stat=%p\n", hlp, sod); fprintf (stderr, "name : %s\n", sod->name); fprintf (stderr, "title: %s\n", sod->title); fprintf (stderr, "desc : %s\n", sod->desc); fprintf (stderr, "type : %d\n", sod->type); fprintf (stderr, "unit : %d\n", sod->unit); fprintf (stderr, "size : %d\n", sod->size); fprintf (stderr, "cap : %d\n", sod->cap); fprintf (stderr, "ctyp : %d\n", sod->constraint_type); switch (sod->constraint_type) { case SANE_CONSTRAINT_NONE: break; case SANE_CONSTRAINT_STRING_LIST: fprintf (stderr, "stringlist:\n"); break; case SANE_CONSTRAINT_WORD_LIST: fprintf (stderr, "wordlist (%d) : ", sod->constraint.word_list[0]); for (x = 1; x <= sod->constraint.word_list[0]; x++) fprintf (stderr, " %d ", sod->constraint.word_list[x]); fprintf (stderr, "\n"); break; case SANE_CONSTRAINT_RANGE: fprintf (stderr, "range: %d-%d %d \n", sod->constraint.range->min, sod->constraint.range->max, sod->constraint.range->quant); break; } } bla = sane_get_parameters (hand, &pars); fprintf (stderr, "Parm : stat=%s form=%d,lf=%d,bpl=%d,pixpl=%d,lin=%d,dep=%d\n", sane_strstatus (bla), pars.format, pars.last_frame, pars.bytes_per_line, pars.pixels_per_line, pars.lines, pars.depth); if (bla != SANE_STATUS_GOOD) return; bla = sane_start (hand); fprintf (stderr, "Start : stat=%s\n", sane_strstatus (bla)); if (bla != SANE_STATUS_GOOD) return; do { bla = sane_read (hand, buffer, sizeof (buffer), &blubb); /*printf("Read : stat=%s len=%d\n",sane_strstatus (bla),blubb); */ if (bla != SANE_STATUS_GOOD) { if (bla == SANE_STATUS_EOF) break; return; } fwrite (buffer, 1, blubb, stdout); } while (1); sane_cancel (hand); fprintf (stderr, "Cancel.\n"); sane_close (hand); fprintf (stderr, "Close\n"); for (hlp = 0; hlp < 20; hlp++) fprintf (stderr, "STRS %d=%s\n", hlp, sane_strstatus (hlp)); fprintf (stderr, "Exit.\n"); }