int CNIF_Network_Close() { CNNL_SessionEnd(hnd, 5, 30000); if (hnd != NULL){ CNNL_Close(hnd); } CNNL_Terminate(&hnd); return CN_LGMON_OK; }
int CNIF_Network_Open(const char *_deviceID, CNIF_INFO *_if_info) { unsigned long latest_version = CNNL_LIB_LATEST_VERSION; unsigned long regacy_version = 100; unsigned long param_size = sizeof(unsigned long); char ipaddr[STRING_SHORT]; fprintf(stderr, "DEBUG: CNIF_Network_Open DeviceID=%s\n", _deviceID); /* Init */ if (CNNL_Init(&hnd) != CNNL_RET_SUCCESS){ goto error_init; } if (CNNL_Config(hnd, CNNL_CONFIG_SET_VERSION, &latest_version, ¶m_size) != CNNL_RET_SUCCESS){ goto error_open; } /* --------------------------------------------------------------------- detect specified printers */ { int found=0; memset(ipaddr, 0x00, STRING_SHORT); if (CNNL_GetIPAddressEx(hnd, CACHE_PATH, _deviceID, ipaddr, STRING_SHORT, CNNET_SEARCH_AUTO, 1, 5000) == CNNL_RET_SUCCESS){ found=1; } /* not found */ if (found == 0) goto error_open; /* cancelled */ if (isCanceled) goto error_open; } /* --------------------------------------------------------------------- open printer */ if (CNNL_OpenEx(hnd, ipaddr, CNNET_TYPE_PRINTER, 10, 60000) != CNNL_RET_SUCCESS){ goto error_open; } // check wether the printer exists or not if (CNNL_CheckVersion(hnd, 10, 60000) != CNNL_RET_SUCCESS){ goto error_close; } // check command type int command = CNNL_COMMAND_SUPPORT; if (CNNL_GetCommandType(hnd, &command, 10, 60000) != CNNL_RET_SUCCESS){ goto error_close; } _if_info->isIVEC = (command == CNNL_COMMAND_SUPPORT)? 1 : 0; // switch functions if (command == CNNL_COMMAND_SUPPORT){ // support ivec commands } else { param_size = sizeof(unsigned long); if (CNNL_Config(hnd, CNNL_CONFIG_SET_VERSION, ®acy_version, ¶m_size) != CNNL_RET_SUCCESS){ goto error_close; } } return CN_LGMON_OK; error_close: CNNL_Close(hnd); error_open: CNNL_Terminate(&hnd); error_init: return CN_LGMON_ERROR; }
int CNIF_Network_Discover(int installer) { CNNLHANDLE hmdl = NULL; unsigned long latest_version = CNNL_LIB_LATEST_VERSION; unsigned long param_size = sizeof(unsigned long); CNNLNICINFO *nic; char model[STRING_SHORT]; char model2[STRING_SHORT]; char ipaddr[STRING_SHORT]; int i=0, j=0, max = MAX_COUNT_OF_PRINTERS, found=0; if (CNNL_Init(&hmdl) != CNNL_RET_SUCCESS) goto error; if (CNNL_Config(hmdl, CNNL_CONFIG_SET_VERSION, &latest_version, ¶m_size) != CNNL_RET_SUCCESS){ goto error; } // find printers nic = (CNNLNICINFO *)malloc(sizeof(CNNLNICINFO)*max); memset(nic, 0x00, sizeof(CNNLNICINFO)*max); if (CNNL_SearchPrintersEx(hmdl, nic, CACHE_PATH, max, &found, CNNET_SEARCH_BROADCAST, 1, (installer==1)? 5000:3000) == CNNL_RET_SUCCESS){ for (j=0; j<found; j++){ memset(model, 0x00, sizeof(model)); memset(model2, 0x00, sizeof(model2)); memset(ipaddr, 0x00, STRING_SHORT); snprintf(ipaddr, STRING_SHORT-1, "%d.%d.%d.%d", nic[j].ipaddr[0],nic[j].ipaddr[1],nic[j].ipaddr[2],nic[j].ipaddr[3]); if (CNNL_OpenEx(hmdl, ipaddr, CNNET_TYPE_PRINTER, 1, 3000) == CNNL_RET_SUCCESS){ if (CNNL_GetModelName(hmdl, model, STRING_SHORT-1, 1, 3000) == CNNL_RET_SUCCESS){ for (i=0; model[i]!='\0'; i++){ if (model[i] == ' '){ model2[i]='-'; } else { model2[i]=model[i]; } } if (installer != 1){ /* cups list device mode */ printf("network cnijbe2://Canon/?port=net&serial=%02X-%02X-%02X-%02X-%02X-%02X \"%s\" \"%s_%02X-%02X-%02X-%02X-%02X-%02X\"\n", nic[j].macaddr[0],nic[j].macaddr[1],nic[j].macaddr[2], nic[j].macaddr[3],nic[j].macaddr[4],nic[j].macaddr[5], model, model2, nic[j].macaddr[0],nic[j].macaddr[1],nic[j].macaddr[2], nic[j].macaddr[3],nic[j].macaddr[4],nic[j].macaddr[5]); } else { /* installer mode */ printf("network cnijbe2://Canon/?port=net&serial=%02X-%02X-%02X-%02X-%02X-%02X \"%s\" \"IP:%s\"\n", nic[j].macaddr[0],nic[j].macaddr[1],nic[j].macaddr[2], nic[j].macaddr[3],nic[j].macaddr[4],nic[j].macaddr[5], model, ipaddr); } } CNNL_Close(hmdl); } } } else { free(nic); goto error; } free(nic); CNNL_Terminate(&hmdl); return CN_LGMON_OK; error: if (hmdl!= NULL) CNNL_Terminate(&hmdl); return CN_LGMON_OK; }
/******************************************************************************* _ij_network_print ********************************************************************************/ static int _ij_network_print( const char *macaddr, const char *user, const char *job, const char *model_number, const int is_direct, FILE *fp ){ int errcode = 0; char ipaddr[STRING_SHORT]; char hostname[HOST_NAME_MAX+1]; char library_path[512]; char *rbuffer = NULL; char *p = NULL; void *lib_handle = NULL; uint32_t len = 0; int working = 0; int count = 0; int tmp_ret = CNNL_RET_SUCCESS; // clear buffer setbuf(stdout, NULL); setbuf(stderr, NULL); memset(ipaddr, 0x00, STRING_SHORT); memset(hostname, 0x00, HOST_NAME_MAX+1); // allocate memory if ((rbuffer=(char*)malloc(sizeof(char)*READ_BUFFER_SIZE)) == NULL){ goto error_init; } // get hostname if (gethostname(hostname, HOST_NAME_MAX) != 0){ errcode = 1; fprintf(stderr, "ERROR: cannot get hostname\n"); goto error_init; } hostname[(HOST_NAME_MAX > 31)? 31:HOST_NAME_MAX] = 0; mode = CNNL_JOB_PREPARING; init_printing: /* write to printer */ { unsigned long latest_version = CNNL_LIB_LATEST_VERSION; unsigned long regacy_version = 100; unsigned long param_size = sizeof(unsigned long); /* Init */ if (CNNL_Init(&hnd) != CNNL_RET_SUCCESS){ errcode = 1; fprintf(stderr, "ERROR: error occurred while preparing(%d)\n", __LINE__); goto error_init; } if (CNNL_Config(hnd, CNNL_CONFIG_SET_VERSION, &latest_version, ¶m_size) != CNNL_RET_SUCCESS){ goto error_open; } /* --------------------------------------------------------------------- find printers */ { int i, j, found=0; fprintf(stderr, "INFO: \n"); for (i=0; !(mode & CNNL_JOB_CANCELLED); i++){ memset(ipaddr, 0x00, STRING_SHORT); if (CNNL_GetIPAddressEx(hnd, CACHE_PATH, macaddr, ipaddr, STRING_SHORT, CNNET_SEARCH_AUTO, 1, 5000) == CNNL_RET_SUCCESS){ found=1; break; } fprintf(stderr, "INFO: printer not responding; will retry in %d seconds...\n", WAITTIME_FIND_PRINTERS); for (j=0; j<WAITTIME_FIND_PRINTERS; j++, sleep(1)){ // cancelled if ((mode & CNNL_JOB_CANCELLED)) goto error_init; } fprintf(stderr, "INFO: \n"); } /* not found */ if (found == 0){ errcode = 1; fprintf(stderr, "ERROR: printer not found; job failed\n"); goto error_init; } /* cancelled */ if ((mode & CNNL_JOB_CANCELLED)) goto error_init; } fprintf(stderr, "INFO: \n"); /* --------------------------------------------------------------------- open printer */ if (CNNL_OpenEx(hnd, ipaddr, CNNET_TYPE_PRINTER, 10, 60000) != CNNL_RET_SUCCESS){ errcode = 1; fprintf(stderr, "ERROR: error occurred while preparing(%d)\n", __LINE__); goto error_open; } // check wether the printer exists or not if (CNNL_CheckVersion(hnd, 10, 60000) != CNNL_RET_SUCCESS){ errcode = 1; fprintf(stderr, "ERROR: error occurred while preparing(%d)\n", __LINE__); goto error_close; } // check command type if (CNNL_GetCommandType(hnd, &command, 10, 60000) != CNNL_RET_SUCCESS){ errcode = 1; fprintf(stderr, "ERROR: error occurred while preparing(%d)\n", __LINE__); goto error_close; } // switch functions if (command == CNNL_COMMAND_IVEC){ // support ivec commands start_print = CheckStartPrint; end_print = CheckEndPrint; check_print = CheckExecutePrint; memset(library_path, 0x00, sizeof(library_path)); snprintf(library_path, sizeof(library_path)-1, CNCL_LIB_PATH, model_number); // load functions lib_handle = dlopen(library_path, RTLD_LAZY); if (lib_handle == NULL){ fprintf(stderr, "ERROR: cannot load library(%d)\n", __LINE__); errcode = 1; goto error_close; } makePrintCommand = dlsym(lib_handle, "CNCL_MakePrintCommand"); if (dlerror() != NULL){ fprintf(stderr, "ERROR: cannot load function(%d)\n", __LINE__); errcode = 1; goto error_close; } checkPrintCommand = dlsym(lib_handle, "CNCL_CheckPrintCommand"); if (dlerror() != NULL){ fprintf(stderr, "ERROR: cannot load function(%d)\n", __LINE__); errcode = 1; goto error_close; } getStatusCommand = dlsym(lib_handle, "CNCL_GetStatusCommand"); if (dlerror() != NULL){ fprintf(stderr, "ERROR: cannot load function(%d)\n", __LINE__); errcode = 1; goto error_close; } } else { param_size = sizeof(unsigned long); if (CNNL_Config(hnd, CNNL_CONFIG_SET_VERSION, ®acy_version, ¶m_size) != CNNL_RET_SUCCESS){ goto error_close; } // not support ivec commands start_print = CNNL_StartPrint; end_print = CNNL_EndPrint; check_print = CNNL_CheckPrint; } packet_size = get_packet_size(hnd, atoi(model_number)); /* --------------------------------------------------------------------- start session */ mode = CNNL_JOB_WAITING; { int j=0, ret; if (!(mode & CNNL_JOB_CANCELLED)){ // if job is not cancelled while (!(mode & CNNL_JOB_CANCELLED)){ // request to start session if ((ret=CNNL_SessionStart(hnd, user, hostname, job, 3, 9000)) == CNNL_RET_SUCCESS){ // succeed to start session mode=CNNL_JOB_PRINTING; break; } // failed to start session if (ret == CNNL_RET_BUSY){ for (j=0; j<WAITTIME_START_SESSION; j++, sleep(1)){ // cancelled CheckParentProcess(hnd); if ((mode & CNNL_JOB_CANCELLED)) goto error_close; } } else { fprintf(stderr, "INFO: cannot start printing; will retry in %d seconds...\n", WAITTIME_RETRY_SEARCH); for (j=0; j<WAITTIME_RETRY_SEARCH; j++, sleep(1)){ // cancelled CheckParentProcess(hnd); if ((mode & CNNL_JOB_CANCELLED)) goto error_init; } CNNL_Close(hnd); CNNL_Terminate(&hnd); hnd = NULL; goto init_printing; } } } if ((mode & CNNL_JOB_CANCELLED)){ goto error_job_cancelled; } } fprintf(stderr, "INFO: \n"); /* set timeout */ CNNL_SetTimeout(hnd, SESSION_TIMEOUT, 3, 15000); /* start printing */ if (!(mode & CNNL_JOB_ERROR) && start_print(hnd, 3, 9000) != CNNL_RET_SUCCESS){ errcode = 1; mode |= CNNL_JOB_ERROR; fprintf(stderr, "ERROR: error occurred while preparing(%d)\n", __LINE__); goto error_printing; } /* --------------------------------------------------------------------- print(after this line, response error is fatal; job is canceled) */ /* send start command... */ if (is_direct == 0 && command == CNNL_COMMAND_IVEC){ // IVEC dispatchCommandIVEC(hnd, CNCL_COMMAND_POWERON); if (dispatchCommandIVEC(hnd, CNCL_COMMAND_START1) < 0){ errcode = 1; mode |= CNNL_JOB_ERROR; fprintf(stderr, "ERROR: error occurred while printing(%d)\n", __LINE__); goto error_printing; } if (dispatchCommandIVEC(hnd, CNCL_COMMAND_START2) < 0){ errcode = 1; mode |= CNNL_JOB_ERROR; fprintf(stderr, "ERROR: error occurred while printing(%d)\n", __LINE__); goto error_printing; } } else { // standard commands } /* send print data... */ { // waiting for print data... fprintf(stderr, "INFO: waiting print data ...\n"); len = fread_with_retry(rbuffer, READ_BUFFER_SIZE, fp); if ((mode & CNNL_JOB_ERROR)){ errcode = 1; fprintf(stderr, "ERROR: error occurred while printing(%d)\n", __LINE__); goto error_close; } p = rbuffer; fprintf(stderr, "INFO: \n"); while (!(mode & CNNL_JOB_ERROR) && !(mode & CNNL_JOB_CANCELLED) && len > 0){ if (sendData(hnd, p, len, SEND_PRINT_DATA, fp) < 0){ errcode = 1; mode |= CNNL_JOB_ERROR; fprintf(stderr, "ERROR: error occurred while printing(%d)\n", __LINE__); goto error_printing; } len = fread_with_retry(rbuffer, READ_BUFFER_SIZE, fp); p = rbuffer; // check if printer is working if (working != 1){ tmp_ret = check_print(hnd, 3, 9000); if (tmp_ret == CNNL_RET_SUCCESS) working = 1; } usleep(40*1000); } if ((mode & CNNL_JOB_CANCELLED)) goto error_job_cancelled; } /* send end command... */ if (is_direct == 0 && command == CNNL_COMMAND_IVEC){ if (dispatchCommandIVEC(hnd, CNCL_COMMAND_END) < 0){ errcode = 1; mode |= CNNL_JOB_ERROR; fprintf(stderr, "ERROR: error occurred while printing(%d)\n", __LINE__); goto error_printing; } } /* wait until printer starts working (for utility) */ for (count=0; working!=1 && count<10; sleep(1)){ tmp_ret = check_print(hnd, 3, 6000); if (tmp_ret == CNNL_RET_SUCCESS){ break; } else if (tmp_ret == CNNL_RET_POWEROFF){ goto error_job_cancelled; } else { count++; } if ((mode & CNNL_JOB_CANCELLED)) goto error_job_cancelled; } /* wait until printer is idle */ if (!(mode & CNNL_JOB_ERROR) && end_print(hnd, 3, 9000) != CNNL_RET_SUCCESS){ // cannot get end of print errcode = 1; mode |= CNNL_JOB_ERROR; fprintf(stderr, "ERROR: error occurred while printing(%d)\n", __LINE__); goto error_printing; } /* --------------------------------------------------------------------- close printer */ error_job_cancelled: error_printing: if ((mode & CNNL_JOB_ERROR) || (mode & CNNL_JOB_CANCELLED)){ CNNL_SoftReset(hnd, 5, 30000); } CNNL_SessionEnd(hnd, 5, 30000); error_close: if (lib_handle != NULL) dlclose(lib_handle); CNNL_Close(hnd); error_open: CNNL_Terminate(&hnd); error_init: fprintf(stderr, "INFO: \n"); free(rbuffer); } // read buffer(flush) { unsigned char dummy; while (fread(&dummy, sizeof(unsigned char), 1, fp) > 0); } fflush(stdout); fflush(stderr); // wait for next job sleep(1); // clear error if job is cancelled if (mode & CNNL_JOB_CANCELLED) errcode = 0; return (errcode == 0)? 0: 1; }