dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2009 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This software is distributed under the terms of the GNU Public License. |
| 5 | * See the COPYING file in the main directory for details. |
| 6 | * |
| 7 | * This use of this software may be subject to additional restrictions. |
| 8 | * See the LEGAL file in the main directory for details. |
| 9 | |
| 10 | This program is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /* |
| 27 | Compilation Flags |
| 28 | |
| 29 | SWLOOPBACK compile for software loopback testing |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include <stdint.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include "Threads.h" |
| 37 | #include "DummyLoad.h" |
| 38 | |
| 39 | #include <Logger.h> |
| 40 | |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | |
| 45 | |
| 46 | int DummyLoad::loadBurst(short *wDummyBurst, int len) { |
| 47 | dummyBurst = wDummyBurst; |
| 48 | dummyBurstSz = len; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | DummyLoad::DummyLoad (double _desiredSampleRate) |
| 53 | { |
| 54 | LOG(INFO) << "creating USRP device..."; |
| 55 | sampleRate = _desiredSampleRate; |
| 56 | } |
| 57 | |
| 58 | void DummyLoad::updateTime(void) { |
| 59 | gettimeofday(&currTime,NULL); |
| 60 | double timeElapsed = (currTime.tv_sec - startTime.tv_sec)*1.0e6 + |
| 61 | (currTime.tv_usec - startTime.tv_usec); |
| 62 | currstamp = (TIMESTAMP) floor(timeElapsed/(1.0e6/sampleRate)); |
| 63 | } |
| 64 | |
| 65 | bool DummyLoad::make(bool wSkipRx) |
| 66 | { |
| 67 | |
| 68 | samplesRead = 0; |
| 69 | samplesWritten = 0; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool DummyLoad::start() |
| 74 | { |
| 75 | LOG(INFO) << "starting USRP..."; |
| 76 | underrun = false; |
| 77 | gettimeofday(&startTime,NULL); |
| 78 | dummyBurstCursor = 0; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool DummyLoad::stop() |
| 83 | { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | // NOTE: Assumes sequential reads |
| 89 | int DummyLoad::readSamples(short *buf, int len, bool *overrun, |
| 90 | TIMESTAMP timestamp, |
| 91 | bool *wUnderrun, |
| 92 | unsigned *RSSI) |
| 93 | { |
| 94 | updateTime(); |
| 95 | underrunLock.lock(); |
| 96 | *wUnderrun = underrun; |
| 97 | underrunLock.unlock(); |
| 98 | if (currstamp+len < timestamp) { |
| 99 | usleep(100); |
kurtis.heimerl | 1a2d244 | 2012-12-22 04:30:56 +0000 | [diff] [blame] | 100 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | } |
| 102 | else if (currstamp < timestamp) { |
| 103 | usleep(100); |
kurtis.heimerl | 1a2d244 | 2012-12-22 04:30:56 +0000 | [diff] [blame] | 104 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 105 | } |
| 106 | else if (timestamp+len < currstamp) { |
| 107 | memcpy(buf,dummyBurst+dummyBurstCursor*2,sizeof(short)*2*(dummyBurstSz-dummyBurstCursor)); |
| 108 | int retVal = dummyBurstSz-dummyBurstCursor; |
| 109 | dummyBurstCursor = 0; |
| 110 | return retVal; |
| 111 | } |
| 112 | else if (timestamp + len > currstamp) { |
| 113 | int amount = timestamp + len - currstamp; |
| 114 | if (amount < dummyBurstSz-dummyBurstCursor) { |
| 115 | memcpy(buf,dummyBurst+dummyBurstCursor*2,sizeof(short)*2*amount); |
| 116 | dummyBurstCursor += amount; |
| 117 | return amount; |
| 118 | } |
| 119 | else { |
| 120 | memcpy(buf,dummyBurst+dummyBurstCursor*2,sizeof(short)*2*(dummyBurstSz-dummyBurstCursor)); |
| 121 | int retVal = dummyBurstSz-dummyBurstCursor; |
| 122 | dummyBurstCursor = 0; |
| 123 | return retVal; |
| 124 | } |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int DummyLoad::writeSamples(short *buf, int len, bool *wUnderrun, |
| 130 | unsigned long long timestamp, |
| 131 | bool isControl) |
| 132 | { |
| 133 | updateTime(); |
| 134 | underrunLock.lock(); |
| 135 | underrun |= (currstamp+len < timestamp); |
| 136 | underrunLock.unlock(); |
| 137 | return len; |
| 138 | } |
| 139 | |
| 140 | bool DummyLoad::updateAlignment(TIMESTAMP timestamp) |
| 141 | { |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool DummyLoad::setTxFreq(double wFreq) { return true;}; |
| 146 | bool DummyLoad::setRxFreq(double wFreq) { return true;}; |