void CCLITerminal::RestartTerminalSession(CLISESSION *pSession) { if (pSession == NULL) return; // Callback Login if (m_pCLIService->m_pConstruct && m_pCLIService->m_pConstruct->pfnOnLogout) m_pCLIService->m_pConstruct->pfnOnLogout(pSession); if (m_pCLIService->GetRunLevel() == RUNLEVEL_NORMAL) { pSession->bNeedUser = TRUE; strcpy(pSession->szUser, ""); strcpy(pSession->szPassword, ""); ClearHistory(pSession); } pSession->nMode = CLIMODE_COMMAND; pSession->bLogined = m_pCLIService->GetRunLevel() == RUNLEVEL_NORMAL ? FALSE : TRUE; pSession->nLoginRetry = 0; pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; if (m_pCLIService->GetRunLevel() == RUNLEVEL_DEBUG) strcpy(pSession->szPrompt, m_pCLIService->m_szDebugPrompt); else strcpy(pSession->szPrompt, m_pCLIService->m_szUserPrompt); DisplaySplash(pSession); }
BOOL CLauncherService::Startup(int argc, char **argv) { struct sigaction handler; if (!CheckKernelParam(argc, argv)) return FALSE; // Set Interrrupt Signal Handler handler.sa_handler = InterruptSignalHandler; sigfillset(&handler.sa_mask); sigaction(SIGINT, &handler, 0); sigaction(SIGTERM, &handler, 0); sigaction(SIGCHLD, &handler, 0); FirmUpdate(); DisplaySplash(); LoadSetting(); Monitoring(); return TRUE; }
BOOL CMonitorService::Startup() { struct sigaction handler; VAROBJECT *pObject; // Set Interrrupt Signal Handler handler.sa_handler = InterruptSignalHandler; sigfillset(&handler.sa_mask); sigaction(SIGINT, &handler, 0); sigaction(SIGTERM, &handler, 0); sigaction(SIGCHLD, &handler, 0); DisplaySplash(); VARAPI_Initialize(VARCONF_FILENAME, (VAROBJECT *)m_Root_node, FALSE); // Local Port에 대한 설정 값을 얻어온다 pObject = VARAPI_GetObjectByName("sysLocalPort"); if (pObject != NULL) { m_nPort = pObject->var.stream.u32; } if(m_nPort == 0) { m_nPort = 8000; // Default Port } IF4API_Initialize(m_nPort, NULL); GetMemoryInfo(&m_nTotal, &m_nUse, &m_nFree, &m_nCache, &m_nBuffer); usleep(60*1000000); m_tmLastCheck = uptime(); for(;!m_bExitSignalPending;) { usleep(10*1000000); WatchSystem(); } return TRUE; }
// entry point int main() { int err; char key = '\0'; SetConsoleTitle( "Direct Connect P2P" ); SetupWinsock(); DisplaySplash(); do { if ( _kbhit() ) { key = (char) _getch(); switch ( key ) { case '1': WaitForFile(); break; case '2': SendFileTo(); break; } } Sleep( 1 ); } while ( key != 'q' ); err = closesocket( sTCP ); ErrCheck( err, 69 ); err = WSACleanup(); ErrCheck( err, 80 ); return 0; }
// Sending a file to waiting app void SendFileTo() { int err; std::string file; //std::ifstream inFile; FILE *inFile; unsigned nFileSize; char path[MAX_PATH]; std::cout << "file to send: "; std::cin.getline( path, MAX_PATH ); file = GetFileName( path ); inFile = fopen( path, "rb" ); if ( !inFile ) { perror( path ); return; } char addr[ADDR_STRING_LEN]; std::cout << "address to send to: "; std::cin.getline( addr, ADDR_STRING_LEN ); SOCKADDR_IN saDest; saDest.sin_family = AF_INET; saDest.sin_port = htons( PORT ); saDest.sin_addr.s_addr = inet_addr( addr ); err = connect( sTCP, (sockaddr *) &saDest, sizeof( saDest ) ); ErrCheck( err, 251 ); std::cout << "connecting..." << std::endl; bool done = false; fd_set fdWrite, fdExcept; do { FD_ZERO( &fdWrite ); FD_ZERO( &fdExcept ); TIMEVAL tv = { 0 }; FD_SET( sTCP, &fdWrite ); FD_SET( sTCP, &fdExcept ); err = select( 0, NULL, &fdWrite, &fdExcept, &tv ); ErrCheck( err, 269 ); // optionally... check if socket is accepted // ... // check to see if socket is writable if ( FD_ISSET( sTCP, &fdWrite ) ) done = true; } while ( !done ); Message msg; strcpy( (char *) msg.buffer, file.c_str() ); err = send( sTCP, (char *) &msg, (int) strlen( (char *) msg.buffer ) + HEADER_SIZE + 1, 0 ); ErrCheck( err, 284 ); // get file size fseek( inFile, 0, SEEK_END ); nFileSize = ftell( inFile ); rewind( inFile ); // set proper display of size std::stringstream sizeStream; if ( nFileSize < KIBIBYTE ) sizeStream << nFileSize << " b)"; else if ( nFileSize < KIBIBYTE * KIBIBYTE ) sizeStream << (float) nFileSize / (float) KIBIBYTE << " Kb)"; else sizeStream << (float) nFileSize / ( (float) KIBIBYTE * (float) KIBIBYTE ) << " Mb)"; std::cout << "sending file" << file << " (" << sizeStream.str() << std::endl; unsigned nSizeLeft = nFileSize; clock_t start = clock(); while ( !feof( inFile ) ) { msg.size = (unsigned short) fread( (char *) msg.buffer, 1, MAX_BUFFER_SIZE, inFile ); if ( msg.size < MAX_BUFFER_SIZE ) msg.id = MESSAGE_DONE; err = send( sTCP, (char *) &msg, (int) msg.size + HEADER_SIZE, 0 ); ErrCheck( err, 315 ); do { std::cout << "loop" << std::endl; fd_set fdWrite; FD_ZERO( &fdWrite ); FD_SET( sTCP, &fdWrite ); TIMEVAL tv = { 0 }; err = select( 0, NULL, &fdWrite, NULL, &tv ); ErrCheck( err, 317 ); } while ( !FD_ISSET( sTCP, &fdWrite ) ); Sleep( 0 ); } clock_t totalTime = clock() - start; float kbps = 1000.f * (float) nFileSize / ( (float) totalTime * (float) KIBIBYTE ); std::cout << "total time = " << totalTime << "ms (" << kbps << "Kb/s)" << std::endl; fclose( inFile ); std::cout << "sending of file \'" << file << "\' completed" << std::endl; std::cout << std::endl; DisplaySplash(); }
// Waiting for incoming files void WaitForFile() { bool first = true; int err; std::ofstream outFile; Message msg; std::string file; SOCKET sRecv; err = listen( sTCP, SOMAXCONN ); ErrCheck( err, 149 ); SOCKADDR_IN saConnect; int saSize; std::cout << "waiting for connection..." << std::endl; while ( 1 ) { sRecv = accept( sTCP, (sockaddr *) &saConnect, &saSize ); if ( sRecv == INVALID_SOCKET ) { err = WSAGetLastError(); if ( err != WSAEWOULDBLOCK ) { std::cout << "error accepting (" << err << ")" << std::endl; exit( -1 ); } } else { std::cout << "connection made" << std::endl; break; } } while ( msg.id != MESSAGE_DONE ) { err = recv( sRecv, (char *) &msg, SIZE_OF_MESSAGE, 0 ); if ( ErrCheck( err, 179 ) ) continue; if ( msg.id != MESSAGE_HEADER && msg.id != MESSAGE_DONE ) { std::cout << "rejected" << std::endl; std::cout << std::hex << msg.id << " vs " << std::hex << MESSAGE_HEADER << std::endl; continue; } if ( first ) { std::cout << "creating the file" << std::endl; file = (char *) msg.buffer; outFile.open( (char *) msg.buffer, std::ios::binary ); if ( !outFile ) { std::cout << "failed to create file \'" << file << "\'" << std::endl; exit( -1 ); } first = false; } else { std::cout << "writing to file" << std::endl; outFile.write( (char *) msg.buffer, msg.size ); } Sleep( 1 ); } outFile.close(); std::cout << "\'" << file << "\' successfully received" << std::endl; std::cout << std::endl; DisplaySplash(); }
BOOL CCLITerminal::CommandTask(CLISESSION *pSession, BOOL bComplete, BOOL bAddHistory) { CLIHANDLER *pHandler; char szCommand[256] = ""; int nResult, nDepts=0, nCommand; int i, nIndex, argc, nParam, nPos; // Make New Line WriteStream(pSession, "\n\r"); pSession->szCommand[pSession->nCmdLength] = '\0'; if (pSession->nMode == CLIMODE_USER) { nCommand = atoi(pSession->szCommand); pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; switch(nCommand) { case 1 : FirmwareDownload(pSession); WriteStream(pSession, "\n\r"); break; case 2 : pSession->nMode = CLIMODE_COMMAND; DisplayPrompt(pSession); return TRUE; } DisplaySplash(pSession); return TRUE; } // Check Login State if (!pSession->bLogined) { if (pSession->bNeedUser) { if (!CheckUser(pSession)) { // Display Login Error Message WriteStream(pSession, "Invalid User.\r\n"); } // Clear Command Buffer pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; DisplayPrompt(pSession); return TRUE; } else if (!CheckLogin(pSession)) { // Clear Command Buffer pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; pSession->bNeedUser = TRUE; // Display Login Error Message WriteStream(pSession, "Invalid account or password.\r\n\r\n"); strcpy(pSession->szPrompt, m_pCLIService->m_szUserPrompt); pSession->nLoginRetry++; if (pSession->nLoginRetry >= 3) { if (pSession->nType == CLITYPE_SERIAL) sleep(3); return FALSE; } DisplayPrompt(pSession); return TRUE; } // Clear Command Buffer pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; // Callback Login if (m_pCLIService->m_pConstruct && m_pCLIService->m_pConstruct->pfnOnLogin) m_pCLIService->m_pConstruct->pfnOnLogin(pSession); // Login Complete pSession->bLogined = TRUE; WriteStream(pSession, "\n\r"); strcpy(pSession->szPrompt, m_pCLIService->m_szDefaultPrompt); DisplayPrompt(pSession); return TRUE; } if (pSession->nCmdLength > 0) { // Find Command nResult = CLIERR_OK; pSession->szCommand[pSession->nCmdLength] = '\0'; if (pSession->szCommand[0] == '!') { if (strcmp(pSession->szCommand, "!!") == 0) { strcpy(pSession->szCommand, pSession->pszHistory[pSession->nHistoryCount-1]); pSession->nCmdLength = strlen(pSession->szCommand); } else { nIndex = atoi(&pSession->szCommand[1]); if ((nIndex > 0) && (nIndex <= pSession->nHistoryCount)) { strcpy(pSession->szCommand, pSession->pszHistory[nIndex-1]); pSession->nCmdLength = strlen(pSession->szCommand); } } } pSession->pszArgString = strdup(pSession->szCommand); if (bAddHistory) AddHistory(pSession, pSession->szCommand); SpliteParameter(pSession); pHandler = FindCommandHandler(pSession, nDepts, szCommand); if (pHandler != NULL) { // Execute Command nParam = GetParamCount(pHandler); argc = pSession->argc - (nDepts + 1); for(i=0; i<argc; i++) pSession->argv[i] = pSession->argv[nDepts+i+1]; // 파라메터의 갯수가 더 많은 경우를 막을때, add apn 명령 때문에 허용하도록 변경 2007/9/5 // if ((nParam == argc) || (pHandler->pszParam && (argc >= GetMinParamCount(pHandler)) && (argc <= nParam))) if ((nParam == argc) || (pHandler->pszParam && (argc >= GetMinParamCount(pHandler)))) { nPos = ValidateParameter(pSession, pHandler); if (nPos == -1) { pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; if (m_pCLIService->m_pConstruct && m_pCLIService->m_pConstruct->pfnOnCommand) m_pCLIService->m_pConstruct->pfnOnCommand(pSession, argc, pSession->argv, pHandler); if (pHandler->nGroup >= pSession->nGroup) { if (m_pCLIService->m_bEnableLog && pHandler->bLogFlag) m_pCLIService->AddLog(pSession->szUser, pSession->szCommand); nResult = pHandler->pfnCommand(pSession, argc, pSession->argv, (void *)pHandler); WriteStream(pSession, "\xd\xa"); } else { WriteStream(pSession, "Permission Denied."); WriteStream(pSession, "\xd\xa"); } } else { WriteStream(pSession, "Invalid parameter : '"); WriteStream(pSession, pSession->argv[nPos]); WriteStream(pSession, "'\xd\xa"); WriteStream(pSession, "\xd\xa"); } } else { WriteStream(pSession, "usage: "); WriteStream(pSession, szCommand); DisplayWideParameter(pSession, pHandler); DisplayAllParameter(pSession, pHandler); WriteStream(pSession, "\xd\xa"); } } if (pSession->pszArgString) FREE(pSession->pszArgString); pSession->pszArgString = NULL; if (nResult == CLIERR_ERROR) return FALSE; } // Clear Command Buffer pSession->nCmdLength = 0; pSession->szCommand[0] = '\0'; // Display Prompt DisplayPrompt(pSession); return TRUE; }
//***************************************************************************** // // The main loop for the user interface. // //***************************************************************************** int main(void) { unsigned long ulPanel; // // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V. This is // a workaround to allow the PLL to operate reliably. // if(REVISION_IS_A2) { SysCtlLDOSet(SYSCTL_LDO_2_75V); } // // Set the clocking to run at 50MHz from the PLL. // SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // // Set the priority of the interrupts. // IntPrioritySet(INT_CAN0, 0x00); IntPrioritySet(FAULT_SYSTICK, 0x20); // // Configure SysTick to generate an interrupt every millisecond. // SysTickPeriodSet(SysCtlClockGet() / 1000); SysTickIntEnable(); SysTickEnable(); // // Initialize the push button driver. // ButtonsInit(); // // Initialize the CAN communication channel. // CANCommInit(); // // Initialize the UART used to perform a "firmware update". // UpdateUARTInit(); // // Initialize the display. // RIT128x96x4Init(3500000); // // Add the screen-clearing widget to the widget tree. As the first widget // in the tree, this will always be drawn first, resulting in a blank // screen before anything else is drawn. // WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sBackground); // // Display the splash screen. // DisplaySplash(); // // Set the CAN device ID to one. // CANSetID(1); // // The "Voltage Control Mode" panel should be displayed first. // ulPanel = PANEL_VOLTAGE; // // Loop forever. // while(1) { // // Determine which panel to display. // switch(ulPanel) { // // The "Voltage Control Mode" panel should be displayed. // case PANEL_VOLTAGE: { ulPanel = DisplayVoltage(); break; } // // The "VComp Control Mode" panel should be displayed. // case PANEL_VCOMP: { ulPanel = DisplayVComp(); break; } // // The "Current Control Mode" panel should be displayed. // case PANEL_CURRENT: { ulPanel = DisplayCurrent(); break; } // // The "Speed Control Mode" panel should be displayed. // case PANEL_SPEED: { ulPanel = DisplaySpeed(); break; } // // The "Position Control Mode" panel should be displayed. // case PANEL_POSITION: { ulPanel = DisplayPosition(); break; } // // The "Configuration" panel should be displayed. // case PANEL_CONFIGURATION: { ulPanel = DisplayConfig(); break; } // // The "Device List" panel should be displayed. // case PANEL_DEV_LIST: { ulPanel = DisplayDevList(); break; } // // The "Firmware Update" panel should be displayed. // case PANEL_UPDATE: { ulPanel = DisplayUpdate(); break; } // // The "Help" panel should be displayed. // case PANEL_HELP: { ulPanel = DisplayHelp(); break; } // // The "About" panel should be displayed. // case PANEL_ABOUT: { ulPanel = DisplayAbout(); break; } } } }