static gw_status oam_pty_pkt_data_proc(GWTT_OAM_SESSION_INFO *pSeInf, char *pPayLoad, long lPayLen) { gw_status ret = GW_E_OK; //入口规则检查 if(NULL == pSeInf) { return GW_E_ERROR; } if(NULL == pPayLoad) { return GW_E_ERROR; } //连接状态判定以及连接验证 if((0 != gmCliPtyCtrl.lConnect) && (0 == memcmp(gmCliPtyCtrl.bSessionId, pSeInf->SessionID, 8))) { //获取命令(起始和长度) char *cmd_str = NULL; int cmd_len = 0; cmd_str = &pPayLoad[1]; //pPayLoad 的第0 位是报文类型 cmd_len = lPayLen -1; //命令合法性和与命令解析模块连接存在性检查 if((0 < lPayLen) && (0 < gmCliPtyCtrl.lFd) ) { //命令合法性和与命令解析模块连接存在性检查通过 //将命令发给命令解析模块 if(0xff != cmd_str[0]) //forbidden telnet option string { #if 0 int i = 0; for(i=0; i<cmd_len; i++) { //一次发送一个字符 //pty_write(gmCliPtyCtrl.lFd, cmd_str+i, 1); write_to_console(cmd_str+i, 1); } #else write_to_console(cmd_str, cmd_len); #endif } else { //是telnet 控制命令,不做处理 } } else { //命令合法性和与命令解析模块连接存在性检查不通过,不做处理 } } else { //不能与命令解析模块正常连接,不做处理 } return ret; }
//proper parsing of responce void Printer::processResponce(QString responce){ if(!this->connectionActive){ this->connectionActive=true; emit connected(true); } //add data to buffer inBuffer.append(responce); //send full responces to fifo if(inBuffer.contains("\n")){ responseBuffer.append(inBuffer.left(inBuffer.lastIndexOf("\n")+1).split("\n",QString::SkipEmptyParts)); } //clear buffer inBuffer.remove(0,inBuffer.lastIndexOf("\n")+1); //proccess all responces from fifo while(responseBuffer.size()>0){ QString lastResponse=responseBuffer.takeFirst(); emit write_to_console("Got responce: "+ lastResponse); //if its temp status if(lastResponse.contains("T:") || lastResponse.contains("B:")){ bool ok; double bed_temp = lastResponse.mid(lastResponse.indexOf("B:")+2,lastResponse.indexOf(" ",lastResponse.indexOf("B:"))-lastResponse.indexOf("B:")-2).toDouble(&ok); if(ok){ last_bed_temp=bed_temp; } double head_temp = lastResponse.mid(lastResponse.indexOf("T:")+2,lastResponse.indexOf(" ",lastResponse.indexOf("T:"))-lastResponse.indexOf("T:")-2).toDouble(&ok); if(ok){ last_head_temp=head_temp; } emit currentTemp(last_head_temp,0.0,last_bed_temp); } //if its response for position command //if we are printing then continue if(lastResponse.startsWith("ok")){ if(!this->isPaused){ emit clearToSend(); } } //if its error else if(lastResponse.startsWith("Error")){ } //resend line else if(lastResponse.toLower().startsWith("resend") || lastResponse.startsWith("rs")){ bool ok; int toResend; // toResend=QString::number(lastResponse.toLower().remove(QRegExp("[\\D]"))); toResend=lastResponse.toLower().remove(QRegExp("[\\D]")).toInt(&ok); if(ok){ this->resendFrom=toResend; } else{ write_to_console(tr("something is horribly wrong")); } } } }
//writing to port int Printer::writeToPort(QString command,int lineNum, bool calcCheckSum){ //analizing command for position tracking QStringList args=command.split(" "); if(command.contains("M104") || command.contains("M109")){ emit settingTemp1(command.mid(command.lastIndexOf("S")+1,command.length()-command.lastIndexOf("S")+1).toDouble()); } if(command.contains("M140") || command.contains("M190")){ emit settingTemp3(command.mid(command.lastIndexOf("S")+1,command.length()-command.lastIndexOf("S")+1).toDouble()); } if(command.contains("G1") || command.contains("G0")){ for(int i=0 ;i <args.size(); i++){ if(args.at(i).contains("X")) curr_pos.setX(args.at(i).mid(1).toDouble()); if(args.at(i).contains("Y")) curr_pos.setY(args.at(i).mid(1).toDouble()); if(args.at(i).contains("Z")){ curr_pos.setZ(args.at(i).mid(1).toDouble()); } } emit currentPosition(curr_pos); } if(this->isConnected()){ if(calcCheckSum){ command=QString("N")+QString::number(lineNum)+QString(" ")+command; command=command+QString("*")+checkSum(command); if(!command.contains("M110")){ this->sentLines.append(command); } else{ this->lineNum=0; } // int temp=rand()%10; // if(temp==0){ // command.replace(5,1,"b"); // qDebug() << "dodaje blad do" << command; // } } int bytes_written =portObj->write(command.toAscii()+"\n"); emit write_to_console(QString("Sending: "+command.toAscii()+"\n").replace("\n","\\n").replace("\r","\\r")); emit write_to_console("Written: "+QString::number(bytes_written)+" bytes"); writeNext=false; } else{ write_to_console(tr("Printer offline")); return -1; } return 0; }
/* write something to the log file, syslog, and possibly the console */ static void write_to_logs_and_console(char *buffer, unsigned long data_type, int display) { register int len = 0; register int x = 0; /* strip unnecessary newlines */ len = strlen(buffer); for(x = len - 1; x >= 0; x--) { if(buffer[x] == '\n') buffer[x] = '\x0'; else break; } /* write messages to the logs */ write_to_all_logs(buffer, data_type); /* write message to the console */ if(display == TRUE) { /* don't display warnings if we're just testing scheduling */ if(test_scheduling == TRUE && data_type == NSLOG_VERIFICATION_WARNING) return; write_to_console(buffer); } }
bool Printer::disconnectPort(){ this->portObj->close(); this->readTimer->stop(); emit write_to_console(tr("Printer disconnected")); emit connected(false); this->connectionActive=false; return true; }
static gw_status oam_pty_pkt_freereq_proc(GWTT_OAM_SESSION_INFO *pSeInf, char *pPayLoad, long lPayLen) { gw_status ret = GW_E_OK; //入口规则检查 if(NULL == pSeInf) { return GW_E_ERROR; } if(NULL == pPayLoad) { return GW_E_ERROR; } int connect_status; connect_status = gmCliPtyCtrl.lConnect; if(0 != connect_status) { //已存在连接 if(0 == memcmp(gmCliPtyCtrl.bSessionId, pSeInf->SessionID, 8)) { //验证通过 char bResBuf[8] = {0}; char bQuitCmd[8] = {0}; int i = 0; //通知olt 已接受断开连接请求 bResBuf[0] = CON_CTL_CODE_FREERES; bResBuf[1] = PTY_CONNECT_REQ_RESP_ACCEPT; gw_comm_onu_msg_send(CLI_PTY_TRANSMIT, gmCliPtyCtrl.lSerNo++, bResBuf, 2, pSeInf->SessionID); //延时 gw_thread_delay(100); //通知命令解析模块,要断开 strcpy(bQuitCmd, "quit\r"); for(i=0; i<strlen(bQuitCmd); i++) { write_to_console(bQuitCmd+i, 1); } } else { //验证未通过 } } else { //未存在连接 } return ret; }
bool Printer::connectPort(QString port, int baud){ emit write_to_console(tr("Connecting...")); if(port!=""){ PortSettings settings = {(BaudRateType)baud, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10}; delete this->portObj; this->portObj = new QextSerialPort(port,settings,QextSerialPort::Polling); if(this->portObj->open(QIODevice::ReadWrite | QIODevice::Unbuffered)){ emit write_to_console(tr("Printer connected")); send_now("M115"); this->readTimer->start(); return true; } else{ emit write_to_console(tr("Unable to connect")); emit connected(false); return false; } } else{ emit write_to_console(tr("Unknown port name")); emit connected(false); return false; } }
static int log_dispatch( int level, const char*file, int line, const char *func, char *buffer) { int r = 0; if (log_target == LOG_TARGET_NULL) return 0; /* Patch in LOG_DAEMON facility if necessary */ if ((level & LOG_FACMASK) == 0) level = log_facility | LOG_PRI(level); do { char *e; int k = 0; buffer += strspn(buffer, NEWLINE); if (buffer[0] == 0) break; if ((e = strpbrk(buffer, NEWLINE))) *(e++) = 0; k = write_to_console(level, file, line, func, buffer); if (k < 0) return k; buffer = e; } while (buffer); return r; }
static int log_dispatch( int level, int error, const char *file, int line, const char *func, const char *object_field, const char *object, char *buffer) { assert(buffer); if (log_target == LOG_TARGET_NULL) return -error; /* Patch in LOG_DAEMON facility if necessary */ if ((level & LOG_FACMASK) == 0) level = log_facility | LOG_PRI(level); if (error < 0) error = -error; do { char *e; int k = 0; buffer += strspn(buffer, NEWLINE); if (buffer[0] == 0) break; if ((e = strpbrk(buffer, NEWLINE))) *(e++) = 0; if (log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_JOURNAL) { k = write_to_journal(level, error, file, line, func, object_field, object, buffer); if (k < 0) { if (k != -EAGAIN) log_close_journal(); log_open_kmsg(); } } if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || log_target == LOG_TARGET_SYSLOG) { k = write_to_syslog(level, error, file, line, func, object_field, object, buffer); if (k < 0) { if (k != -EAGAIN) log_close_syslog(); log_open_kmsg(); } } if (k <= 0 && (log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_SAFE || log_target == LOG_TARGET_SYSLOG_OR_KMSG || log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_KMSG)) { k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer); if (k < 0) { log_close_kmsg(); log_open_console(); } } if (k <= 0) (void) write_to_console(level, error, file, line, func, object_field, object, buffer); buffer = e; } while (buffer); return -error; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //about window this->aboutWindow = new AboutWindow(); this->aboutWindow->hide(); this->aboutWindow->move(this->geometry().center()-this->aboutWindow->geometry().center()); //calibrate Dialog this->calibrateDialog = new CalibrateDialog(); this->calibrateDialog->hide(); this->calibrateDialog->move(this->geometry().center()-this->calibrateDialog->geometry().center()); //option Dialog this->optionDialog = new OptionDialog(); this->optionDialog->hide(); this->optionDialog->move(this->geometry().center()-this->optionDialog->geometry().center()); //slice dialog this->sliceDialog = new SliceDialog(ui->glWidget, this); this->sliceDialog->hide(); this->sliceDialog->move(this->geometry().center()-this->sliceDialog->geometry().center()); //macros window this->macrosWindow = new MacrosWindow(); this->macrosWindow->hide(); this->macrosWindow->move(this->geometry().center()-this->macrosWindow->geometry().center()); connect(this->macrosWindow, SIGNAL(buttonAdded(MacroButton*)), this, SLOT(addMacroBtn(MacroButton*))); connect(this->macrosWindow, SIGNAL(buttonRemoved(MacroButton*)), this, SLOT(removeMacroBtn(MacroButton*))); //sd card window this->sdCardWindow = new SDCardWindow(); this->sdCardWindow->hide(); this->sdCardWindow->move(this->geometry().center()-this->sdCardWindow->geometry().center()); connect(this->sdCardWindow, SIGNAL(sdFile_selected(QString)), this, SLOT(sdFile_selected(QString))); connect(this->optionDialog, SIGNAL(slicerPathChanged(QString)), this->sliceDialog, SLOT(updateSlicerPath(QString))); connect(this->optionDialog, SIGNAL(outputPathChanged(QString)), this->sliceDialog, SLOT(updateOutputPath(QString))); connect(this->optionDialog, SIGNAL(newSize(QVector3D)), this, SLOT(updatadeSize(QVector3D))); connect(this->optionDialog, SIGNAL(newList(QList<Material*>*)), this->sliceDialog, SLOT(setMaterialList(QList<Material*>*))); connect(this->sliceDialog, SIGNAL(fileSliced(QString)), this, SLOT(loadFile(QString))); //set version number this->setWindowTitle("YARRH v"+QString::number(VERSION_MAJOR)+"."+QString::number(VERSION_MINOR)+"."+QString::number(VERSION_REVISION)); this->aboutWindow->setVersion(VERSION_MAJOR,VERSION_MINOR,VERSION_REVISION); //setting up printer and its thread this->printerObj = new Printer(); QThread *qthread = new QThread(); //connecting ui to printer connect(printerObj, SIGNAL(write_to_console(QString)), ui->inConsole, SLOT(appendPlainText(QString)), Qt::QueuedConnection); connect(this->macrosWindow, SIGNAL(writeToPrinter(QString)), printerObj, SLOT(send_now(QString)),Qt::QueuedConnection); connect(ui->fanSpinBox, SIGNAL(valueChanged(int)), printerObj, SLOT(setFan(int)), Qt::QueuedConnection); ui->fanSpinBox->blockSignals(true); connect(this->printerObj, SIGNAL(SDFileList(QStringList)), this->sdCardWindow, SLOT(updateFileList(QStringList))); connect(this->printerObj, SIGNAL(uploadProgress(int,int)), this->sdCardWindow, SLOT(updateProgress(int,int))); this->sdCardWindow->setPrinter(this->printerObj); //connecting move btns connect(ui->homeX, SIGNAL(clicked()), printerObj, SLOT(homeX()), Qt::QueuedConnection); connect(ui->homeY, SIGNAL(clicked()), printerObj, SLOT(homeY()), Qt::QueuedConnection); connect(ui->homeAll, SIGNAL(clicked()), printerObj, SLOT(homeAll()), Qt::QueuedConnection); //connect monit temp checkbox connect(ui->graphGroupBox, SIGNAL(toggled(bool)), printerObj, SLOT(setMonitorTemperature(bool)),Qt::QueuedConnection); //connect printer to temp widget connect(printerObj, SIGNAL(currentTemp(double,double,double)), this, SLOT(drawTemp(double,double,double))); connect(printerObj, SIGNAL(progress(int,int)), this, SLOT(updateProgress(int,int))); connect(printerObj, SIGNAL(connected(bool)), this, SLOT(printerConnected(bool))); //setting ui temp from gcode connect(printerObj, SIGNAL(settingTemp1(double)), this, SLOT(setTemp1FromGcode(double))); connect(printerObj, SIGNAL(settingTemp3(double)), this, SLOT(setTemp3FromGcode(double))); //updating head position in ui connect(printerObj, SIGNAL(currentPosition(QVector3D)), this, SLOT(updateHeadPosition(QVector3D))); //print finished signal connect(printerObj, SIGNAL(printFinished(bool)), this, SLOT(printFinished(bool))); //connect calibration dialog to printer connect(calibrateDialog, SIGNAL(writeToPrinter(QString)), printerObj, SLOT(send_now(QString)),Qt::QueuedConnection); //connect z slider connect(ui->zSlider, SIGNAL(valueChanged(int)), this, SLOT(moveZ(int))); connect(ui->zSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateZ(int))); //connect action load connect(ui->actionLoad, SIGNAL(triggered()), this, SLOT(loadFile())); printerObj->moveToThread(qthread); qthread->start(QThread::HighestPriority); this->portEnum = new QextSerialEnumerator(this); this->portEnum->setUpNotifications(); QList<QextPortInfo> ports = this->portEnum->getPorts(); //finding avalible ports foreach (QextPortInfo info, ports) { ui->portCombo->addItem(info.portName); }
//proper parsing of responce void Printer::processResponce(QString responce){ if(!this->connectionActive){ this->connectionActive=true; emit connected(true); } //add data to buffer inBuffer.append(responce); //send full responces to fifo if(inBuffer.contains("\n")){ responseBuffer.append(inBuffer.left(inBuffer.lastIndexOf("\n")+1).split("\n",QString::SkipEmptyParts)); } //clear buffer inBuffer.remove(0,inBuffer.lastIndexOf("\n")+1); //proccess all responces from fifo while(responseBuffer.size()>0){ QString lastResponse=responseBuffer.takeFirst(); emit write_to_console("Got responce: "+ lastResponse); //if its temp status if(lastResponse.contains("T:") || lastResponse.contains("B:")){ bool ok; double bed_temp = lastResponse.mid(lastResponse.indexOf("B:")+2,lastResponse.indexOf(" ",lastResponse.indexOf("B:"))-lastResponse.indexOf("B:")-2).toDouble(&ok); if(ok){ last_bed_temp=bed_temp; } double head_temp = lastResponse.mid(lastResponse.indexOf("T:")+2,lastResponse.indexOf(" ",lastResponse.indexOf("T:"))-lastResponse.indexOf("T:")-2).toDouble(&ok); if(ok){ last_head_temp=head_temp; } emit currentTemp(last_head_temp,0.0,last_bed_temp); } //if its something about sd card if(lastResponse.contains("Begin file list")){ this->sdFiles.clear(); this->updatingFileList=true; } if(this->updatingFileList){ this->sdFiles.append(lastResponse.toLower()); } if(lastResponse.contains("End file list")){ this->updatingFileList=false; this->sdFiles.removeFirst(); this->sdFiles.removeLast(); emit SDFileList(this->sdFiles); } if(lastResponse.contains("SD printing byte")){ QString prog=lastResponse.right(lastResponse.length()-lastResponse.lastIndexOf(" ")-1); QStringList values = prog.split("/"); emit progress(values.at(0).toInt(), values.at(1).toInt()); if(values.at(0).toInt()==values.at(1).toInt()){ emit printFinished(true); this->sdPrint=false; } } //if its response for position command //if we are printing then continue if(lastResponse.startsWith("ok")){ if(!this->isPaused){ emit clearToSend(); } } //if its error else if(lastResponse.startsWith("Error")){ } //resend line else if(lastResponse.toLower().startsWith("resend") || lastResponse.startsWith("rs")){ bool ok; int toResend; // toResend=QString::number(lastResponse.toLower().remove(QRegExp("[\\D]"))); toResend=lastResponse.toLower().remove(QRegExp("[\\D]")).toInt(&ok); if(ok){ this->resendFrom=toResend; if(!this->isPaused){ emit clearToSend(); } } else{ emit write_to_console(tr("something is horribly wrong")); } } } }
/** * @brief * @param * @see * @remarks * @code * @endcode * @return **/ void log_write_fmt( _In_ uint32_t log_level, _In_z_ const char* function, _In_z_ const char* fmt, _In_ ... ) { if (NULL == fmt) return; char log_buffer[2048]; size_t remain = sizeof(log_buffer); char* pos = log_buffer; va_list args; // log level switch (log_level) { case log_level_debug: StringCbPrintfExA(pos, remain, &pos, &remain, 0, "%s", "[DEBG] "); break; case log_level_info: StringCbPrintfExA(pos, remain, &pos, &remain, 0, "%s", "[INFO] "); break; case log_level_warn: StringCbPrintfExA(pos, remain, &pos, &remain, 0, "%s", "[WARN] "); break; case log_level_error: StringCbPrintfExA(pos, remain, &pos, &remain, 0, "%s", "[EROR] "); break; default: _ASSERTE(!"never reach here!"); return; } //> show process name if (true == _show_process_name) { StringCbPrintfExA( pos, remain, &pos, &remain, 0, "%ws", get_current_module_fileEx().c_str() ); } //> show pid, tid if (true == _show_pid_tid) { StringCbPrintfExA( pos, remain, &pos, &remain, 0, "(%+5u:%+5u) : ", GetCurrentProcessId(), GetCurrentThreadId() ); } //> show function name if (true == _show_function_name) { StringCbPrintfExA(pos, remain, &pos, &remain, 0, "%s : ", function); } va_start(args,fmt); HRESULT hRes = StringCbVPrintfExA( pos, remain, &pos, &remain, 0, fmt, args ); if (S_OK != hRes) { // invalid character 가 끼어있는 경우 발생 할 수 있음 StringCbPrintfExA( pos, remain, &pos, &remain, 0, "invalid function call parameters" ); } va_end(args); // line feed StringCbPrintfExA(pos, remain, &pos, &remain, 0, "\n"); // Let's write logs. switch (log_level) { case log_level_error: // same as log_level_critical write_to_console(wtc_red, log_buffer); break; case log_level_info: case log_level_warn: write_to_console(wtc_green, log_buffer); break; default: write_to_console(wtc_none, log_buffer); } OutputDebugStringA(log_buffer); }