Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #19 from Ordissimo/add-conf-file
Browse files Browse the repository at this point in the history
Add conf file fort sane.
  • Loading branch information
Ordissimo authored Aug 25, 2021
2 parents f551c18 + a0fa5da commit 775be72
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 11 deletions.
9 changes: 9 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
libmfp2-canon (0.3-gtk3+subnet+bookworm1) bookworm; urgency=medium

* Add its device in /etc/sane.d/canon_pixma.conf
Allows you to use your device even if it is not
on the same network
Limitation of scangearmp2.

-- Thierry HUCHARD (cle ubuntu) <thierry@substantiel.fr> Wed, 25 Aug 2021 11:08:38 +0200

libmfp2-canon (0.2+2104ubuntu3) hirsute; urgency=medium

* Adf works
Expand Down
12 changes: 12 additions & 0 deletions etc/sane.d/canon_pixma.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Backend configuration file sane-canon_pixma
#
# Two configurations exist:
# 1 - With the IP address, for new generation devices
# 2 - With IP and MAC address, for older generation devices
#
# No space at the beginning of the line
#
# Example :
# device 127.234.1.124 A2:3E:5F:9C:0A:6b
# device 17.234.1.24

45 changes: 45 additions & 0 deletions src/canon_mfp_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#define CANONMUD (9600)

#define CANON_CONFIG_FILE "canon_mfp2.conf"
#define SANE_CONFIG_FILE "/etc/sane.d/canon_pixma.conf"

#include "canon_mfp_tools.h"

Expand All @@ -56,6 +57,8 @@
extern int is_flatbed;
extern int is_adf;
extern int is_duplex;
extern int manual_len;
extern CNNLNICINFO *manual_nic;

/*---------------------------------------------------------------------------------------*/
/* definitions for CIJSC ----> */
Expand Down Expand Up @@ -480,6 +483,48 @@ CMT_Status CIJSC_init( void *cnnl_callback )
/* initialize libUSB */
cmt_libusb_init();

fp = cmt_conf_file_open( SANE_CONFIG_FILE );
if ( fp ) {
char line[1024] = { 0 }; // char*)calloc(1, sizeof(1024)); //[PATH_MAX];
char *tmp = NULL;
char *ip_str = NULL;
char *mac_str = NULL;
manual_len = 0;
manual_nic = NULL;
/* Set Network device list */
// while ( ( len = cmt_conf_file_read_line( line, sizeof(line), fp ) ) >= 0 ) {
while ( fgets (line, 1024, fp) != NULL) {
if (strncmp(line, "device", 6) == 0) {
tmp = line;
tmp = (char*)cmt_config_get_string(tmp + 6, &ip_str);
if (!ip_str || !*ip_str) {
continue;
}
DBGMSG ("IP Adress Device [%s].\n", ip_str);
CNNLNICINFO info;
if (!cmt_convert_ipadress_to_array(ip_str, &info)) {
continue;
}
if (*tmp) {
tmp = (char*)cmt_config_get_string(tmp, &mac_str);
if (!cmt_convert_macadress_to_array(mac_str, &info)) {
info.macaddr[0] = 0;
}
else {
DBGMSG ("Mac Adress Device [%s].\n", mac_str);
}
}
if (manual_nic == NULL)
manual_nic = (CNNLNICINFO*) calloc(1, sizeof(CNNLNICINFO));
else
manual_nic = (CNNLNICINFO*) realloc(manual_nic, sizeof(CNNLNICINFO) * (manual_len + 1));
manual_nic[manual_len] = info;
manual_len += 1;
}
}
fclose (fp);
}

/* initialize Network */
cmt_network_init( cnnl_callback );

Expand Down
136 changes: 129 additions & 7 deletions src/canon_mfp_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static LIB_USB_DEV libusbdev[LIBUSB_DEV_MAX];

static struct libusb_device **g_devlist = NULL; /* device list */
static struct libusb_context *g_context = NULL; /* libusb context */
int manual_len = 0;
CNNLNICINFO *manual_nic = NULL;

static char *
get_cnmslibpath(void)
Expand Down Expand Up @@ -144,14 +146,20 @@ static NETWORK_DEV network2dev[NETWORK_DEV_MAX];
*/
FILE *cmt_conf_file_open(const char *conf)
{
char *path = get_cnmslibpath();
char *path = NULL;
char dst[PATH_MAX];
FILE *fp = NULL;

if ( !conf ) return NULL;

memset( dst, 0, sizeof(dst) );
snprintf( dst, sizeof(dst), "%s/%s", path, conf );
if (*conf == '/') {
snprintf( dst, sizeof(dst), "%s", conf );
}
else {
path = get_cnmslibpath();
snprintf( dst, sizeof(dst), "%s/%s", path, conf );
}
DBGMSG( " conf file \"%s\".\n", dst );
fp = fopen( dst, "r" );
if (fp) {
Expand Down Expand Up @@ -288,6 +296,94 @@ int cmt_get_device_info( char *line, int len, CANON_Device *c_dev )
return ret;
}

char *
cmt_config_skip_whitespace (char *str)
{
while (str && *str && isspace (*str))
++str;
return str;
}

char *
cmt_config_get_string (char *str, char **string_const) {
char *start;
size_t len;
str = cmt_config_skip_whitespace (str);
if (*str == '"')
{
start = ++str;
while (*str && *str != '"')
++str;
len = str - start;
if (*str == '"')
++str;
else
start = 0;
/* final double quote is missing */
} else {
start = str;
while (*str && !isspace (*str)) ++str;
len = str - start;
}
if (start)
*string_const = strndup (start, len);
else
*string_const = 0;
return str;
}

int
cmt_convert_macadress_to_array(char *str, CNNLNICINFO* info)
{
uint8_t values[6] = { 0 };
if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&values[0],
&values[1],
&values[2],
&values[3],
&values[4],
&values[5]) < 6)
{
return 0;
}
info->macaddr[0] = values[0];
info->macaddr[1] = values[1];
info->macaddr[2] = values[2];
info->macaddr[3] = values[3];
info->macaddr[4] = values[4];
info->macaddr[5] = values[5];
return 1;
}

int
cmt_convert_ipadress_to_array(char *str, CNNLNICINFO* info)
{
char *tmp = str;
char *tmp2 = str;
int cpt = 0;
short oct[4] = { 0 };

while ((tmp2 = strchr(tmp, '.')) != NULL) {
*tmp2 = 0;
oct[cpt++] = atoi((const char*)tmp);
*tmp2 = '.';
tmp2++;
tmp = tmp2;
}
if (tmp) {
oct[cpt++] = atoi((const char*)tmp);
}
if (cpt < 4)
{
return 0;
}
info->ipaddr[0] = oct[0];
info->ipaddr[1] = oct[1];
info->ipaddr[2] = oct[2];
info->ipaddr[3] = oct[3];
return 1;
}

/*
find canon_mfp usb device.
*/
Expand Down Expand Up @@ -828,10 +924,18 @@ void cmt_network_init( void *cnnl_callback )
}
timeout_msec = ( found_cache ) ? found_cache * 1000 : 5000;
DBGMSG( " cache num = %d, timeout = %d msec\n", found_cache, timeout_msec );



// find printers
memset(nic, 0x00, sizeof(CNNLNICINFO)*max);
if( CNNL_SearchPrintersEx( hmdl, nic, CACHE_PATH, max, &found, cnnl_mode, 1, timeout_msec ) == CNNL_RET_SUCCESS ){
for (j = 0; j < manual_len; j++) {
if (manual_nic[j].macaddr[0] != 0) {
nic[found] = manual_nic[j];
found += 1;
break;
}
}
for (j=0; j<found; j++){

memset(ipaddr, 0x00, STRING_SHORT);
Expand Down Expand Up @@ -1209,13 +1313,31 @@ void cmt_network2_init( void *cnnl_callback )
goto error;
}

DBGMSG( "CNNET2_Search ->\n" );
num = CNNET2_Search( instance, NULL, NULL, NULL );
if ( num < CNNET2_ERROR_CODE_SUCCESS ) {
DBGMSG( "CNNET2_Search ->\n" );
for (i = 0; i < manual_len; i++) {
if (manual_nic[i].macaddr[0] != 0) continue;
char strip[16] = { 0 };
snprintf(strip, sizeof(strip), "%d.%d.%d.%d",
manual_nic[i].ipaddr[0],
manual_nic[i].ipaddr[1],
manual_nic[i].ipaddr[2],
manual_nic[i].ipaddr[3]);
num = CNNET2_Search( instance, strip, NULL, NULL );
DBGMSG( "CNNET2_Search Add manual IP -> [%s]\n", strip );
if ( num < CNNET2_ERROR_CODE_SUCCESS ) {
DBGMSG( "Error.\n" );
continue;
}
break;
}
if (num == 0) {
num = CNNET2_Search( instance, NULL, NULL, NULL );
if ( num < CNNET2_ERROR_CODE_SUCCESS ) {
DBGMSG( "Error.\n" );
goto error;
}
}

if ( num > 0 ) {
DBGMSG( "CNNET2_Search : %d printer(s) found.\n", num );
if ( (infoList = malloc( sizeof(tagSearchPrinterInfo) * num )) == NULL )
Expand Down
4 changes: 4 additions & 0 deletions src/canon_mfp_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ typedef struct {
FILE *cmt_conf_file_open(const char *conf);
int cmt_conf_file_read_line(char *line, int size, FILE *fp);
int cmt_get_device_info( char *line, int len, CANON_Device *c_dev );
int cmt_convert_macadress_to_array(char *str, CNNLNICINFO* infos);
int cmt_convert_ipadress_to_array(char *str, CNNLNICINFO* infos);
char *cmt_config_get_string (char *str, char **string_const);
char *cmt_config_skip_whitespace (char *str);

char *cmt_find_device_usb( CANON_Device *c_dev, int *index );
char *cmt_find_device_net( CANON_Device *c_dev, int *index );
Expand Down
1 change: 0 additions & 1 deletion src/errordlg.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ static CIJSC_ERROR_INDEX_TABLE error_index_table[] = {
{ BERRCODE_SCANNER_CONNECT_FAILED_LAN, CIJSC_ERROR_CONNECT_FAILED },
{ BERRCODE_SFS_LOCKED, CIJSC_ERROR_SFS_LOCKED },
{ BERRCODE_SFS_INITIALIZE, CIJSC_ERROR_SFS_INITIALIZE },

{ BERRCODE_SAVE_NO_FILE_NAME, CIJSC_ERROR_SAVE_NO_FILE_NAME },
{ BERRCODE_SAVE_OVERWRITE, CIJSC_ERROR_SAVE_OVERWRITE },
{ BERRCODE_SAVE_NO_ACCESS, CIJSC_ERROR_SAVE_NO_ACCESS },
Expand Down
2 changes: 1 addition & 1 deletion src/file_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
/* #define __CNMS_DEBUG_FILE_CONTROL__ */

/* Ver.2.20 */
#define TEMP_FILE_FULL_NAME "/var/tmp/cnms_tmp_file_XXXXXX\0"
#define TEMP_FILE_FULL_NAME "/tmp/cnms_tmp_file_XXXXXX\0"


/* for FileControlReadFileEx... */
Expand Down
5 changes: 3 additions & 2 deletions src/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@
# define N_(String) (String)
#endif


// #define _SGMP_DEBUG
/*
#define _SGMP_DEBUG
#define _SGMP_DEBUG_VERBOSE_
*/


/*
Expand Down

0 comments on commit 775be72

Please sign in to comment.