/** Read data from debug device and save the datas in buffer. Reads NumberOfBytes data bytes from a debug device into the buffer specified by Buffer. The number of bytes actually read is returned. If the return value is less than NumberOfBytes, then the rest operation failed. If NumberOfBytes is zero, then return 0. @param Handle Debug port handle. @param Buffer Pointer to the data buffer to store the data read from the debug device. @param NumberOfBytes Number of bytes which will be read. @param Timeout Timeout value for reading from debug device. It unit is Microsecond. @retval 0 Read data failed, no data is to be read. @retval >0 Actual number of bytes read from debug device. **/ UINTN EFIAPI DebugPortReadBuffer ( IN DEBUG_PORT_HANDLE Handle, IN UINT8 *Buffer, IN UINTN NumberOfBytes, IN UINTN Timeout ) { USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle; USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister; RETURN_STATUS Status; UINT8 Received; UINTN Total; UINTN Remaining; UINT8 Index; UINT8 Length; UINT64 Begin; UINT64 TimeoutTicker; UINT64 TimerRound; if (NumberOfBytes == 0 || Buffer == NULL) { return 0; } Received = 0; Total = 0; Remaining = 0; // // If Handle is NULL, it means memory is ready for use. // Use global variable to store handle value. // if (Handle == NULL) { UsbDebugPortHandle = &mUsbDebugPortHandle; } else { UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle; } if (NeedReinitializeHardware(UsbDebugPortHandle)) { Status = InitializeUsbDebugHardware (UsbDebugPortHandle); if (RETURN_ERROR(Status)) { return 0; } } UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset); // // First read data from buffer, then read debug port hw to get received data. // if (UsbDebugPortHandle->DataCount > 0) { if (NumberOfBytes <= UsbDebugPortHandle->DataCount) { Total = NumberOfBytes; } else { Total = UsbDebugPortHandle->DataCount; } for (Index = 0; Index < Total; Index++) { Buffer[Index] = UsbDebugPortHandle->Data[Index]; } for (Index = 0; Index < UsbDebugPortHandle->DataCount - Total; Index++) { if (Total + Index >= 8) { return 0; } UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Total + Index]; } UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - (UINT8)Total); } // // If Timeout is equal to 0, then it means it should always wait until all datum required are received. // Begin = 0; TimeoutTicker = 0; TimerRound = 0; if (Timeout != 0) { Begin = GetPerformanceCounter (); TimeoutTicker = DivU64x32 ( MultU64x64 ( UsbDebugPortHandle->TimerFrequency, Timeout ), 1000000u ); TimerRound = DivU64x64Remainder ( TimeoutTicker, DivU64x32 (UsbDebugPortHandle->TimerCycle, 2), &TimeoutTicker ); } // // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw. // while (Total < NumberOfBytes) { if (Timeout != 0) { if (TimerRound == 0) { if (IsTimerTimeout (UsbDebugPortHandle, Begin, TimeoutTicker)) { // // If time out occurs. // return 0; } } else { if (IsTimerTimeout (UsbDebugPortHandle, Begin, DivU64x32 (UsbDebugPortHandle->TimerCycle, 2))) { TimerRound --; } } } Remaining = NumberOfBytes - Total; if (Remaining >= USB_DEBUG_PORT_MAX_PACKET_SIZE) { Status = UsbDebugPortIn(UsbDebugPortRegister, Buffer + Total, &Received, INPUT_PID, 0x7f, 0x82, UsbDebugPortHandle->BulkInToggle); if (RETURN_ERROR(Status)) { return Total; } } else { Status = UsbDebugPortIn(UsbDebugPortRegister, &UsbDebugPortHandle->Data[0], &Received, INPUT_PID, 0x7f, 0x82, UsbDebugPortHandle->BulkInToggle); if (RETURN_ERROR(Status)) { return Total; } UsbDebugPortHandle->DataCount = Received; if (Remaining <= Received) { Length = (UINT8)Remaining; } else { Length = (UINT8)Received; } // // Copy required data from the data buffer to user buffer. // for (Index = 0; Index < Length; Index++) { (Buffer + Total)[Index] = UsbDebugPortHandle->Data[Index]; UsbDebugPortHandle->DataCount--; } // // reorder the data buffer to make available data arranged from the beginning of the data buffer. // for (Index = 0; Index < Received - Length; Index++) { if (Length + Index >= 8) { return 0; } UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Length + Index]; } // // fixup the real received length in Buffer. // Received = Length; } UsbDebugPortHandle->BulkInToggle ^= 1; Total += Received; } return Total; }
void svrTimer(int sig) { if (0 == pthread_mutex_trylock(&mutex_lock_timer)) { SerialCom* comm = static_chp.m_comm; int nQid = static_chp.m_nQid; bool dbg = static_debug_gws; if (!svrIncreaseTicks(sig)) { pthread_mutex_unlock(&mutex_lock_timer); return; } if (CheckPeriod(nTimer_ticks,2 * SECOND_TICKS)) { svrIamAlive(-1); } const int nNoiseScanPeriod = PERIOD_TOTAL * MSEC_TICKS; switch (svrIsScanning()) { case SCANNING_FIXED_INIT: //fixed channel scanning... svrQueryNoise(NULL,static_chp.m_pKpi,static_chp.m_nIdentifier); //reset noise floor PRE_SCAN_COMMAND(comm,"setrxatten 0 0"); PRE_SCAN_COMMAND(comm,"setrxatten 1 36"); EnableTimer(timer_chan_switch,2000 * MSEC_TICKS); //500 ms to ensure the pll done static_chp.m_uChanScan = SCANNING_FIXED_READ; break; case SCANNING_FIXED_READ: //channel scanning... if (IsTimerTimeout(timer_chan_switch)) svrNoiseFloorRead(dbg,static_chp.m_ifName, static_chp.m_pKpi, static_chp.m_nIdentifier); break; case SCANNING_FIXED_DONE: do { //channel scan done int region = 0; int ch = -1; RW_KPI_VAR(static_chp.m_nIdentifier,region,static_chp.m_pKpi->m_radio.m_nRegion); ch = static_chp.m_nCurrentChannel - MIN_CHANNEL(region); RW_KPI_VAR(static_chp.m_nIdentifier,static_chp.m_pKpi->m_noise_floor[ch].m_noise_cur, static_chp.m_pKpi->m_noise_floor[ch].m_noise_avg); CHANNEL_SCAN_ON_OFF(static_chp.m_uChanScan,SCANNING_DONE); APP_SCAN_COMMAND(comm,static_chp.m_nOriginChannel); } while (0); break; case SCANNING_ALL_INIT: //channel scanning... if (TimeDiff(nTimer_ticks, &nRecentChanSW, nNoiseScanPeriod)) { //handler for noise scan svrNoiseFloorSwitchChan(dbg,comm,&timer_chan_switch); } else if (IsTimerTimeout(timer_chan_switch)) { //switch done, notify that noise available svrNoiseFloorSetAvailable(dbg,&timer_chan_switch,&timer_noise_floor); static_chp.m_uChanScan = SCANNING_ALL_READ; } break; case SCANNING_ALL_READ: //channel scanning... if (IsTimerDisabled(timer_chan_switch) && IsTimerRunning(timer_noise_floor)) { svrNoiseFloorRead(dbg,static_chp.m_ifName, static_chp.m_pKpi, static_chp.m_nIdentifier); } else if (IsTimerTimeout(timer_noise_floor)) { svrNoiseFloorDone(dbg,comm,&timer_noise_floor,&static_chp.m_uChanScan); } break; default: //routine... if (CheckPeriod(nTimer_ticks,3 * SECOND_TICKS)) { //radio polling..... svrGwsRequest(dbg,comm,&timer_radio_query); } else if (IsTimerRunning(timer_radio_query)) { //within radio query timeout svrGwsPolling(dbg,comm,&timer_radio_query); } else if (IsTimerTimeout(timer_radio_query)) { //radio query done or timeout svrGwsParse(dbg,&timer_radio_query); } if (IsTimerDisabled(timer_noise_floor)) { // ensure noise is not reading currently svrQueryWifi(dbg,static_chp.m_ifName, static_chp.m_pKpi, static_chp.m_nIdentifier,nTimer_ticks); } } if (TASK_COMMAND == latest_msg.m_nType) { if (0 == svrCmdHandler(comm,&latest_msg,&static_chp)) { SVR_DEBUG(dbg,VT100_RESET VT100_STYLE_HOT "\nDone : idel = %d\n",nSvrTimerBusy); pthread_mutex_unlock(&mutex_lock_timer); svrExitCommandHandler(); return; } } if (COM_TEST) { //latest message has already done if (msgrcv(nQid, &latest_msg, MSG_SZ, latest_msg.m_nType, IPC_NOWAIT) > 0) { //no task currently, try to detect new task } else { //no new message, we can handle the channel scan things } } //else, consider there is still a task not handled yet pthread_mutex_unlock(&mutex_lock_timer); } return; }