void Worker::run() { qDebug()<<"run"; while (!bThread) { msleep(300); if (myCom->isOpen()) { char data[1024] = ""; myCom->read(data,1024); int length = strlen(data); if (length == 0) { continue; } if (bAccept) { // emit ReceiveText(data, length); Decoding(data, length); } } else { emit SendInfo(QStringLiteral("串口没有打开")); bThread = true; } } }
int main() { char choice; while(1) { welcome(); scanf("%c",&choice); switch(choice) { case 'i': case 'I':Init();break; case 'e': case 'E':Encoding();break; case 'd': case 'D':Decoding();break; case 'p': case 'P':Print();break; case 't': case 'T':Tree_printing();break; case 'q': case 'Q':Free();exit(1); default :printf("Input error!\n"); } getchar(); } return 0; }
/****************************************** Fun: 完整帧数据处理 Input: void Output:void Return:void ******************************************/ void FrameDataHandle() { DataInterfaceStu tempbuf = {0}; u8 tempbuffer[250]={0}; u8 templen = 0; u8 i=0, j=0; templen = framerevfifo.Datalen-framerevpara.offset; FifoCopy(tempbuffer, &framerevfifo, framerevpara.offset, templen); FifoDeletHead(&framerevfifo, templen); #ifdef OLD u16 tempcrc = 0; u8 errortype = 0; tempcrc = tempbuffer[templen-2]<<8 | tempbuffer[templen-1];//转换为小端模式 if(tempcrc != crc_cal(tempbuffer, (templen-2))) //从帧头开始校验 { errortype = 1; } else { memcpy(laststate, &tempbuffer[8+4], 4); OldToNew((u8*)&tempbuf, tempbuffer); } #else tempbuf.framehead[i] = tempbuffer[i++]; //帧头 tempbuf.framehead[i] = tempbuffer[i++]; //帧头 tempbuf.framectrl.byte = tempbuffer[i++]; //控制域 tempbuf.framedata.datalen = tempbuffer[i++]; //长度域 for(j=0; j<tempbuf.framedata.datalen-2; j++,i++) { tempbuf.framedata.data[j] = tempbuffer[i]; if(FRAMEHEADER == tempbuffer[i]) { i++; } } tempbuf.framedata.data[j] = tempbuffer[i]; if(FRAMEHEADER == tempbuffer[i]) { tempbuf.framedata.data[j+1] = tempbuffer[i+2]; } else { tempbuf.framedata.data[j+1] = tempbuffer[i+1]; } u16 tempcrc = 0; u8 errortype = 0; if(0 != tempbuf.framectrl.bit.encrypt) { if(OK != Decoding((u8*)&tempbuf.framedata, (templen-3)))//解密长度减去头和控制域 { errortype = 2; goto endhandle; } } switch(tempbuf.framectrl.bit.crctype) { case CRCCHECK: tempcrc = tempbuf.framedata.data[j]<<8 |tempbuf.framedata.data[j+1];//转换为小端模式 if(tempcrc != crc_cal(tempbuffer, i)) //从帧头开始校验 { errortype = 1; } break; case SUMCHECK: if(tempbuf.framedata.data[j] != SumCheck(&tempbuffer[3], (i-3))) //从长度域开始 { errortype = 1; } break; case BCCCHECK: if(tempbuf.framedata.data[j] != XorCheck(&tempbuffer[3], (i-3)))//从长度域开始 { errortype = 1; } break; default: break; } #endif endhandle: if(0 == errortype) { framerevpara.errortype = 4; if(FifoInput(&framerevfifo, (u8*)&tempbuf, (tempbuf.framedata.datalen+4))) { framerevpara.offset += tempbuf.framedata.datalen+4; } } else { framerevpara.errortype = errortype; } FrameRevInit(); }
int main(int argc, char *argv[]) { // tweaked by SungboKang /////////////////////////////////////////////////// int frameCnt = 0; int tempSeparateH264FileNumber = 0; int thread_id; pthread_t controlThread; //////////////////////////////////////////////////////////////////////////// char * pImgBuff; SOCKET StreamSock; char strQuery[MAX_QUERY]; int AppKey; unsigned long DaemonId = 0; int ScanMode = SCAN_RAW_MODE; int idx = 0; long nRead; int ImageSize; int nRetCode; char tmp_img_file[_MAX_PATH]; SytJesVideoCodecTypeEnum current_codec; #ifdef DECODER_ON Cffm4l ffm4l; #endif int First_IFrame_Flag = 0; pImgBuff = (char*)malloc(MAX_PACK_SIZE); if(pImgBuff == NULL) return -1; #ifdef DECODER_ON if (ffm4l.CreateFfm4lDecoder(CODEC_ID_H264)!=FFM4L_COMMON_SUCCESS) { printf("ffmpeg Create failed\n"); free(pImgBuff); return -1; } #endif current_codec = JES_VIDEO_CODEC_H264; setuped_codec = JES_VIDEO_CODEC_H264; SOCK_STARTUP(); //============================================================================= //Get FlexWATCH System Information // printf("FwSysGetCgiWp\n"); // memset(pImgBuff, 0, MAX_PACK_SIZE); // nRead = FwSysGetCgiWp(VSMID, TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); // if(nRead > 0) // printf("FwSysGetCgiWp=[%s]\n",pImgBuff); //============================================================================= //============================================================================= //Open Cgi Stream AppKey = rand(); #ifdef __HEADER_STREAM_MODE_SUPPORT sprintf( strQuery, STREAM_CGI_FMT_V30, VSMID, AppKey, "0", // Port Id List (0,1,2,3) 0x00, // 0x00 = Normal Mode, 0x0f = Header Only Mode PAUSE_TIME, FWCGI_VERSION); #else // Normal Mode Only sprintf( strQuery, STREAM_CGI_FMT_V30, VSMID, AppKey, "0,1", // Port Id List (0,1,2,3) PAUSE_TIME, FWCGI_VERSION); #endif StreamSock = FwOpenGetCgiWp(TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, strQuery, OPEN_TIMEOUT, wp_domain, wp_port); if(StreamSock == INVALID_SOCKET) { SOCK_CLEANUP(); free(pImgBuff); return 0; } //============================================================================= //============================================================================= // tweaked by SungboKang //// thread_id = pthread_create(&controlThread, NULL, Control_thread_function, NULL); // make the control thread if(thread_id < 0) // when thread creating doesnt work properly { perror("thread control creation error\n"); exit(-1); } // if playlist file exists already, wipe it out and make a new one sprintf(tmp_img_file, PLAYLIST_FILENAME); if(!FileExist(tmp_img_file)) { WriteM3U8("#EXTM3U"); sprintf(tmp_img_file, "#EXT-X-TARGETDURATION:%d", FRAMECOUNT/30); WriteM3U8(tmp_img_file); WriteM3U8("#EXT-X-MEDIA-SEQUENCE:0"); } #ifdef USE_FFMPEG_LIBRARY // Initialize ffmpeg library av_register_all(); #endif ///////////////////////////// // tweaked by GJ Yang // printf("%c[2J%c[0;0H",27,27); ///////////////////////////// // Get Cgi Stream //for(idx=0 ; idx < RECV_IMAGE_CNT ; idx++) while(1) { // tweaked by GJ Yang // printf("Getting frame...\n"); // printf("%c[s",27);fflush(stdout); //save the cursor location // printf("%c[%d;%dH",27, 31, 2);fflush(stdout); // printf("\033[%dmGetting frame...%2d\033[0m for",42, frameCnt); // printf(" \033[%dmVIDEO%d.h264\033[0m\n", 41, tempSeparateH264FileNumber); // printf("%c[u",27); fflush(stdout);//restore the cursor locarion /////////////////////////////////// nRetCode = FwRcvCgiStream(StreamSock, pImgBuff, MAX_PACK_SIZE, &ImageSize, &ScanMode, &DaemonId); /******************************** CODE ADDED BY SEYEON TECH START **********************************/ p_jpeg_usr_t pJesHeader; // oject of the class that contains the header information unsigned short JesHeaderSize; // char* pH264Image; int H264FrameSize; pJesHeader = (p_jpeg_usr_t)pImgBuff; // magic code // pJesHeader->start_of_jpg[0] : It mean JPEG or H.264 // pJesHeader->start_of_jpg[1] : It mean Sequence Number 0,1,2,3... JesHeaderSize = (unsigned short)(ntohs(pJesHeader->usr_length) + 2) + 1; // previously 2 // added 1 // IT WORKS!!! // tweaked by GJ Yang // printf("################################ Header Size: %d\n", JesHeaderSize); //////////////////////////////////// // modify here remove the first 0x00 always // look at research log, june 20, 2012 H264FrameSize = ImageSize - JesHeaderSize; pH264Image = pImgBuff + JesHeaderSize + 1; /******************************** CODE ADDED BY SEYEON TECH END ***********************************/ if( nRetCode < 0 ) { // tweaked by GJ Yang /////////////////// // printf("%c[%d;%dH",27, 32, 2);fflush(stdout); // printf("\033[%dmGetCgiStream Error=%d\033[0m\n", 41, nRetCode); //////////////////////////////////////// } else #ifdef DECODER_ON { sprintf(tmp_img_file, "img%d_3_%02d.raw",__LINE__, idx); if(IsJesPacketVideo((pjpeg_usr_t)pImgBuff) ) { current_codec = GetVideoCodecTypeOfJesPacket((pjpeg_usr_t)pImgBuff); if(current_codec == setuped_codec) { if(current_codec == JES_VIDEO_CODEC_JPEG) Decoding(&ffm4l, (pjpeg_usr_t)pImgBuff, tmp_img_file); else if(current_codec == JES_VIDEO_CODEC_H264) { if(IsJesPacketVideoH264IFrame((pjpeg_usr_t)pImgBuff) ) { if(First_IFrame_Flag == 0) First_IFrame_Flag = 1; } else if(First_IFrame_Flag == 0) continue; Decoding(&ffm4l, (pjpeg_usr_t)pImgBuff, tmp_img_file); } } else if(current_codec != setuped_codec) { CodecChange(&ffm4l, current_codec); } } } #else { if(IsJesPacketVideo((pjpeg_usr_t)pImgBuff) ) { if (GetVideoCodecTypeOfJesPacket((pjpeg_usr_t)pImgBuff) == JES_VIDEO_CODEC_JPEG) sprintf(tmp_img_file, "img%d_3_%02d.jpg",__LINE__, idx); else if(GetVideoCodecTypeOfJesPacket((pjpeg_usr_t)pImgBuff) == JES_VIDEO_CODEC_H264) { if(IsJesPacketVideoH264IFrame((pjpeg_usr_t)pImgBuff) ) { if(First_IFrame_Flag == 0) First_IFrame_Flag = 1; } else if(First_IFrame_Flag == 0) continue; // sprintf(tmp_img_file, "img%d_3_%02d.264",__LINE__, idx); // original function call. saves one frame into one file // sprintf(tmp_img_file, "VIDEO.h264"); // new function call. saves many frames into one file // tweaked by SungboKang // sprintf(tmp_img_file, "VIDEO%d.h264", tempSeparateH264FileNumber); // newer function call. saves many frames into numbered files /////////////////////////// } // SavePacket(pImgBuff, tmp_img_file, ImageSize); // original function call. saves one frame into one file WITH HEADER DATA SavePacket(pH264Image, tmp_img_file, H264FrameSize); // new function call. saves a frame without the JES headers frameCnt++; } } // tweaked by SungboKang /////////////////////////////////////////////////////////////////////// // assume that a IP Camera sends 30 frames at any circumstances. // In here, it runs every 10 second(300 frames) to make a separate H264 file if(frameCnt == FRAMECOUNT) { while(QueueIsFull()); // waits till the queue is not full Enqueue(tempSeparateH264FileNumber); // put data into the queue tempSeparateH264FileNumber = (tempSeparateH264FileNumber + 1) % MAX_QUEUE_N; // tempSeparateH264FileNumber = tempSeparateH264FileNumber + 1; // If the next file numbered with 'tempSeparateH264FileNumber' exists, wipe it out // It is probable to have a flaw related to authority. // In addition, FileExist() works for files up to 2GB only sprintf(tmp_img_file, "VIDEO%d.h264", tempSeparateH264FileNumber); if(FileExist(tmp_img_file)) { if(remove(tmp_img_file) < 0) { printf("%s file removing error\n", tmp_img_file); exit(-1); } } frameCnt = 0; First_IFrame_Flag = 0; // initialize Iframe checker // makes 30 h264 files then quit looping // It should be less than MAX_QUEUE_N, otherwise working infinitely // if(tempSeparateH264FileNumber == (MAX_QUEUE_N-1)) // break; } //////////////////////////////////////////////////////////////////////////////////////////////// #endif } //============================================================================= //============================================================================= // Control Cgi Stream memset(pImgBuff, 0, MAX_PACK_SIZE); //============================================================================= // Set Stream as Normal mode ( Header + Image ) // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Normal", "0,1,2,3", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Set Stream as Header mode (Header Only) // nRead = FwStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Header", "0,1,2,3", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Add Camera 3,4 --> PortId (2,3) // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Add", "2,3", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Remove Camera 2 --> PortId (1) // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Remove", "1", PAUSE_TIME,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Set pause time 1000 msec // nRead = TestStCtrCgiV30Wp(VSMID, AppKey, DaemonId, "Set", NULL, 100,TARGET_IPADDR, TARGET_PORT, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); //============================================================================= // Get Cgi Stream #if 0 for(idx=0 ; idx < RECV_IMAGE_CNT ; idx++) { sprintf(tmp_img_file, "img%d_3_%02d.jpg",__LINE__, idx); #ifdef WIN32 tmp_img_filep=fopen(tmp_img_file, "wb"); #else tmp_img_filep=fopen(tmp_img_file, "wb"); #endif nRetCode = FwRcvCgiStream(StreamSock, pImgBuff, MAX_PACK_SIZE, &ImageSize, &ScanMode, &DaemonId); if( nRetCode < 0 ) { printf("GetCgiStream Error=%d\n", nRetCode); } else { // printf("-------- Jpeg Header --------\n"); // PrintJpegHeader(pImgBuff); printf("DaemondId : 0x%08lx\n", DaemonId); printf("FwModId(0~): 0x%04x\n", GET_FW_STREAM_MOD_ID( nRetCode )); printf("PortId(0~3): 0x%04x\n", GET_FW_STREAM_PORT_ID( nRetCode )); fwrite(pImgBuff, sizeof(char), ImageSize, tmp_img_filep); } fclose(tmp_img_filep); } //============================================================================= #endif //============================================================================= // Close Cgi Stream FwCloseCgiWp(StreamSock); //============================================================================= // nRead = TestCamGetCgiV30Wp(VSMID, TARGET_IPADDR, TARGET_PORT, 0, FW_USER_ID, FW_PASS_WD, pImgBuff, MAX_PACK_SIZE, wp_domain, wp_port); // if(nRead > 0) // printf("FwSysGetCgiWp=[%s]\n",pImgBuff); // tweaked by SungboKang ////////////////////////////////////////////////// setControlThreadEndFlag(TRUE); pthread_join(controlThread, NULL); // wait for the control thread // WriteM3U8("#EXT-X-ENDLIST"); /////////////////////////////////////////////////////////////////////////// SOCK_CLEANUP(); free(pImgBuff); return 0; }
void MasterThread::run() { // qDebug()<<"run()"; bool currentPortNameChanged = false; bool currentBaudRateChanged = false; bool currentStopBitsChanged = false; bool currentDataBitsChanged = false; bool currentParityChanged = false; mutex.lock(); //! [4] //! [5] QString currentPortName; if (currentPortName != portName) { currentPortName = portName; currentPortNameChanged = true; } int currentBaudRate = this->baudRate; int currentStopBits = this->stopBits; int currentDataBits = this->dataBits; int currentParity = this->parity; int currentWaitTimeout = this->waitTimeout; mutex.unlock(); //! [5] //! [6] QSerialPort serial; while (!quit) { //![6] //! [7] if (currentPortNameChanged) { serial.close(); serial.setPortName(currentPortName); if (!serial.open(QIODevice::ReadWrite)) { emit error(tr("Can't open %1, error code %2") .arg(portName).arg(serial.error())); return; } } if (currentBaudRateChanged) { serial.setBaudRate(currentBaudRate); } if (currentDataBitsChanged) { serial.setDataBits(QSerialPort::DataBits(currentDataBits)); } if (currentStopBitsChanged) { serial.setStopBits(QSerialPort::StopBits(currentStopBits)); } if (currentParityChanged) { serial.setParity(QSerialPort::Parity(currentParity)); } if (serial.waitForReadyRead(currentWaitTimeout)) { QByteArray responseData = serial.readAll(); while (serial.waitForReadyRead(10)) responseData += serial.readAll(); QString response(responseData); //! [12] Decoding(response); // emit this->response(response); //! [10] //! [11] //! [12] } else { emit timeout(tr("Wait read response timeout %1") .arg(QTime::currentTime().toString())); } mutex.lock(); // cond.wait(&mutex); if (currentPortName != portName) { currentPortName = portName; currentPortNameChanged = true; } else { currentPortNameChanged = false; } if (currentBaudRate != baudRate) { currentBaudRate = baudRate; currentBaudRateChanged = true; } else { currentBaudRateChanged = false; } if (currentDataBits != dataBits) { currentDataBits = dataBits; currentDataBitsChanged = true; } else { currentDataBitsChanged = false; } if (currentStopBits != stopBits) { currentStopBits = stopBits; currentStopBitsChanged = true; } else { currentStopBitsChanged = false; } if (currentParity != parity) { currentParity = parity; currentParityChanged = true; } else { currentParityChanged = false; } currentWaitTimeout = waitTimeout; mutex.unlock(); } //! [13] }