37 __typeof__(a) _a = (a); \
38 __typeof__(b) _b = (b); \
42 #define SUBFRAME_SIZE (16384)
43 #define MIN_FRAME_SIZE (512)
44 #define MAX_FRAME_SIZE (SUBFRAME_SIZE * 16)
45 #define SPECTRUM_SIZE (256)
47 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
48 static pthread_mutex_t condMutex = PTHREAD_MUTEX_INITIALIZER;
50 void RTLSDR::Callback()
58 unsigned char *buf = (
unsigned char *)malloc(len);
67 rtlsdr_read_sync(
rtl_dev, buf, len, &olen);
69 olen = read(
PortFD, buf, len);
83 std::deque<std::unique_ptr<RTLSDR>> receivers;
87 size_t numofConnectedReceivers = rtlsdr_get_device_count();
88 if (numofConnectedReceivers == 0)
91 IDLog(
"No USB RTLSDR receivers detected. Power on?");
92 IDMessage(
nullptr,
"No USB RTLSDR receivers detected. Power on?");
97 for (
size_t i = 0; i < numofConnectedReceivers; i++)
99 receivers.push_back(std::unique_ptr<RTLSDR>(
new RTLSDR(i)));
113 receiverIndex = index;
128 int r = rtlsdr_open(&
rtl_dev,
static_cast<uint32_t
>(receiverIndex));
131 LOGF_ERROR(
"Failed to open rtlsdr device index %d.", receiverIndex);
150 pthread_mutex_lock(&condMutex);
152 terminateThread =
true;
153 pthread_cond_signal(&cv);
154 pthread_mutex_unlock(&condMutex);
155 LOG_INFO(
"RTL-SDR Receiver disconnected successfully!");
164 return "RTL-SDR Receiver";
175 setMinMaxStep(
"SENSOR_INTEGRATION",
"SENSOR_INTEGRATION_VALUE", 0.001, 600, 0.001,
false);
176 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_FREQUENCY", 2.4e+7, 2.0e+9, 1,
false);
177 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_SAMPLERATE", 2.5e+5, 2.0e+6, 2.5e+5,
false);
178 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_GAIN", 0.0, 25.0, 0.1,
false);
179 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_BANDWIDTH", 2.5e+5, 2.0e+6, 2.5e+5,
false);
180 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_BITSPERSAMPLE", 16, 16, 0,
false);
181 setMinMaxStep(
"RECEIVER_SETTINGS",
"RECEIVER_ANTENNA", 1, 1, 0,
false);
221 r |= rtlsdr_set_tuner_gain_mode(
rtl_dev, 1);
222 r |= rtlsdr_set_tuner_gain(
rtl_dev,
static_cast<int>(gain * 10));
223 r |= rtlsdr_set_center_freq(
rtl_dev,
static_cast<uint32_t
>(freq));
224 r |= rtlsdr_set_sample_rate(
rtl_dev,
static_cast<uint32_t
>(sr));
225 r |= rtlsdr_set_tuner_bandwidth(
rtl_dev,
static_cast<uint32_t
>(sr));
228 LOG_INFO(
"Issue(s) setting parameters.");
232 setGain(
static_cast<double>(rtlsdr_get_tuner_gain(
rtl_dev)) / 10.0);
239 sendTcpCommand(CMD_SET_FREQ,
static_cast<int>(freq));
240 sendTcpCommand(CMD_SET_SAMPLE_RATE,
static_cast<int>(sr));
241 sendTcpCommand(CMD_SET_TUNER_GAIN_MODE, 0);
242 sendTcpCommand(CMD_SET_GAIN,
static_cast<int>(gain * 10));
243 sendTcpCommand(CMD_SET_FREQ_COR, 0);
244 sendTcpCommand(CMD_SET_AGC_MODE, 0);
245 sendTcpCommand(CMD_SET_TUNER_GAIN_INDEX, 0);
255 bool RTLSDR::sendTcpCommand(
int cmd,
int value)
257 unsigned char tosend[5];
258 tosend[0] =
static_cast<unsigned char>(
cmd);
259 tosend[1] = value & 0xff;
261 tosend[2] = value & 0xff;
263 tosend[3] = value & 0xff;
265 tosend[4] = value & 0xff;
267 tcflush(
PortFD, TCOFLUSH);
271 count = write(
PortFD, tosend, 5);
272 if (count < 0)
return false;
282 for(
int i = 0; i < n; i++)
284 if (!strcmp(names[i],
"RECEIVER_GAIN"))
288 else if (!strcmp(names[i],
"RECEIVER_FREQUENCY"))
292 else if (!strcmp(names[i],
"RECEIVER_SAMPLERATE"))
314 IntegrationRequest =
static_cast<float>(duration);
319 std::thread(&RTLSDR::Callback,
this).detach();
320 gettimeofday(&IntStart,
nullptr);
343 float RTLSDR::CalcTimeLeft()
348 gettimeofday(&now,
nullptr);
350 timesince = (double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) -
351 (double)(IntStart.tv_sec * 1000.0 + IntStart.tv_usec / 1000);
352 timesince = timesince / 1000;
354 timeleft = IntegrationRequest - timesince;
370 timeleft =
static_cast<long>(CalcTimeLeft());
374 LOG_INFO(
"Integration done, expecting data...");
423 pthread_mutex_lock(&condMutex);
426 pthread_mutex_unlock(&condMutex);
427 pthread_cond_signal(&cv);
434 pthread_mutex_lock(&condMutex);
436 pthread_mutex_unlock(&condMutex);
437 pthread_cond_signal(&cv);
448 LOG_ERROR(
"Failed to connect to rtl_tcp server.");
454 terminateThread =
false;
455 LOG_INFO(
"RTL-SDR Receiver connected successfully!");
const char * getDeviceName() const
void setDeviceName(const char *dev)
Set the device name.
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
void setGain(double gain)
setGain Set gain of Receiver device.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void setBandwidth(double bandwidth)
setBandwidth Set bandwidth of Receiver device.
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
virtual void setMinMaxStep(const char *property, const char *element, double min, double max, double step, bool sendToClient=true) override
setMinMaxStep for a number property element
void setFrequency(double freq)
setFrequency Set the frequency observed.
INumberVectorProperty ReceiverSettingsNP
double getSampleRate()
getSampleRate Get requested sample rate for the sensor in Hz.
double getFrequency()
getFrequency Get requested integration frequency for the sensor in Hz.
double getBandwidth()
getBandwidth Get requested integration bandwidth for the sensor in Hz.
void SetReceiverCapability(uint32_t cap)
SetReceiverCapability Set the Receiver capabilities. Al fields must be initialized.
void setSampleRate(double sr)
setSampleRate Set depth of Receiver device.
double getGain()
getGain Get requested integration gain for the sensor.
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
void setBPS(int bps)
setBPP Set depth of Sensor device.
void setBufferSize(int nbuf, bool allocMem=true)
setBufferSize Set desired buffer size. The function will allocate memory accordingly....
void setSensorConnection(const uint8_t &value)
setSensorConnection Set Sensor connection mode. Child class should call this in the constructor befor...
void setIntegrationLeft(double duration)
setIntegrationLeft Update Integration time left. Inform the client of the new Integration time left v...
uint8_t * getBuffer()
getBuffer Get raw buffer of the stream of the Sensor device.
std::unique_ptr< StreamManager > Streamer
void setIntegrationTime(double duration)
setIntegrationTime Set desired Sensor frame Integration duration for next Integration....
int PortFD
For Serial & TCP connections.
int getBufferSize() const
getContinuumBufferSize Get allocated continuum buffer size to hold the Sensor integrationd stream.
virtual bool IntegrationComplete()
Uploads target Device exposed buffer as FITS to the client. Dervied classes should class this functio...
uint8_t getSensorConnection()
int getBPS() const
getBPS Get Sensor depth (bits per sample).
void setIntegrationFileExtension(const char *ext)
setIntegrationExtension Set integration exntension
bool AbortIntegration() override
Abort ongoing Integration.
bool StopStreaming() override
StopStreaming Stop live video streaming.
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
bool Disconnect() override
Disconnect from device.
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
void setupParams(float sr, float freq, float gain)
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
const char * getDefaultName() override
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
bool Handshake() override
perform handshake with device to check communication
bool StartIntegration(double duration) override
Start integration from the Sensor device.
bool StartStreaming() override
StartStreaming Start live video streaming.
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void IDLog(const char *fmt,...)
Implementations for common driver routines.
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
void IDMessage(const char *dev, const char *fmt,...)
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)