From 9aebdfa77d57426fdaaab123d7c91c32a20e87be Mon Sep 17 00:00:00 2001 From: Scott Murray Date: Mon, 22 May 2017 18:05:21 -0400 Subject: Rework to add and use a binding for radio control A radio binding has been added in the new binding directory, and the application has been reworked to use it. The binding uses a modified version of the rtl_fm code used in the qtmultimedia radio plugin that was previously used, and some new code has been added to output to PulseAudio using the asynchronous API to ensure compatibility with stream corking. The rtl_fm code has been enhanced to add seeking support, and the application has been tweaked to use it. Bug-AGL: SPEC-581 Change-Id: I011e98374accc2cad2b36c93ac800948ee51f2aa Signed-off-by: Scott Murray --- .gitignore | 1 + COPYING | 339 ++++++++++ app/Radio.qml | 65 +- app/api/Binding.qml | 196 ++++++ app/main.cpp | 28 +- app/radio.qrc | 1 + binding/binding.pri | 6 + binding/binding.pro | 11 + binding/convenience/convenience.c | 304 +++++++++ binding/convenience/convenience.h | 142 +++++ binding/export.map | 1 + binding/radio-binding.c | 493 +++++++++++++++ binding/radio_impl.h | 76 +++ binding/radio_impl_rtlsdr.c | 254 ++++++++ binding/radio_output.c | 294 +++++++++ binding/radio_output.h | 31 + binding/rtl_fm.c | 1267 +++++++++++++++++++++++++++++++++++++ binding/rtl_fm.h | 70 ++ package/config.xml | 8 +- radio.pro | 4 +- 20 files changed, 3568 insertions(+), 23 deletions(-) create mode 100644 .gitignore create mode 100644 COPYING create mode 100644 app/api/Binding.qml create mode 100644 binding/binding.pri create mode 100644 binding/binding.pro create mode 100644 binding/convenience/convenience.c create mode 100644 binding/convenience/convenience.h create mode 100644 binding/export.map create mode 100644 binding/radio-binding.c create mode 100644 binding/radio_impl.h create mode 100644 binding/radio_impl_rtlsdr.c create mode 100644 binding/radio_output.c create mode 100644 binding/radio_output.h create mode 100644 binding/rtl_fm.c create mode 100644 binding/rtl_fm.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..d511905 --- /dev/null +++ b/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/app/Radio.qml b/app/Radio.qml index 8bcd56e..f812af1 100644 --- a/app/Radio.qml +++ b/app/Radio.qml @@ -1,5 +1,6 @@ /* * Copyright (C) 2016 The Qt Company Ltd. + * Copyright (C) 2017 Konsulko Group * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,29 +18,23 @@ import QtQuick 2.6 import QtQuick.Layouts 1.1 import QtQuick.Controls 2.0 -import QtMultimedia 5.5 import AGL.Demo.Controls 1.0 +import 'api' as API ApplicationWindow { id: root - Radio { + API.Binding { id: radio + property string title + onBandChanged: frequency = minimumFrequency onStationFound: title = stationId onFrequencyChanged: { title = '' slider.value = frequency } - - function freq2str(freq) { - if (freq > 5000000) { - return '%1 MHz'.arg((freq / 1000000).toFixed(1)) - } else { - return '%1 kHz'.arg((freq / 1000).toFixed(0)) - } - } } ColumnLayout { @@ -82,7 +77,7 @@ ApplicationWindow { // offImage: './images/FM_Icons_FM.svg' // onImage: './images/FM_Icons_AM.svg' // onCheckedChanged: { -// radio.band = checked ? Radio.AM : Radio.FM +// radio.band = checked ? radio.amBand : radio.fmBand // radio.frequency = radio.minimumFrequency // } // } @@ -129,7 +124,11 @@ ApplicationWindow { } RowLayout { Layout.fillHeight: true - Item { Layout.fillWidth: true } + + Label { + text: 'TUNE' + } + ImageButton { offImage: './images/AGL_MediaPlayer_BackArrow.svg' Timer { @@ -140,13 +139,27 @@ ApplicationWindow { onTriggered: radio.tuneDown() } } + + ImageButton { + offImage: './images/AGL_MediaPlayer_ForwardArrow.svg' + Timer { + running: parent.pressed + triggeredOnStart: true + interval: 100 + repeat: true + onTriggered: radio.tuneUp() + } + } + + Item { Layout.fillWidth: true } + ImageButton { id: play offImage: './images/AGL_MediaPlayer_Player_Play.svg' onClicked: radio.start() states: [ State { - when: radio.state === Radio.ActiveState + when: radio.state === radio.activeState PropertyChanges { target: play offImage: './images/AGL_MediaPlayer_Player_Pause.svg' @@ -155,6 +168,25 @@ ApplicationWindow { } ] } + + Item { Layout.fillWidth: true } + + Label { + //Layout.fillWidth: true + text: 'SCAN' + } + + ImageButton { + offImage: './images/AGL_MediaPlayer_BackArrow.svg' + Timer { + running: parent.pressed + triggeredOnStart: true + interval: 100 + repeat: true + onTriggered: radio.scanDown() + } + } + ImageButton { offImage: './images/AGL_MediaPlayer_ForwardArrow.svg' Timer { @@ -162,11 +194,10 @@ ApplicationWindow { triggeredOnStart: true interval: 100 repeat: true - onTriggered: radio.tuneUp() + onTriggered: radio.scanUp() } } - Item { Layout.fillWidth: true } } } } @@ -214,9 +245,9 @@ ApplicationWindow { Image { source: { switch (model.modelData.band) { - case Radio.FM: + case radio.fmBand: return './images/FM_Icons_FM.svg' - case Radio.AM: + case radio.amBand: return './images/FM_Icons_AM.svg' } return null diff --git a/app/api/Binding.qml b/app/api/Binding.qml new file mode 100644 index 0000000..3b43510 --- /dev/null +++ b/app/api/Binding.qml @@ -0,0 +1,196 @@ +/* + * Copyright (C) 2016 The Qt Company Ltd. + * Copyright (C) 2017 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import QtQuick 2.6 +import QtWebSockets 1.0 + +WebSocket { + id: root + active: true + url: bindingAddress + + property string apiString: "radio" + property var verbs: [] + property string payloadLength: "9999" + + readonly property var msgid: { + "call": 2, + "retok": 3, + "reterr": 4, + "event": 5 + } + + readonly property int amBand: 0 + readonly property int fmBand: 1 + + readonly property int stoppedState: 0 + readonly property int activeState: 1 + + property int band: fmBand + property int frequency + property int frequencyStep + property int minimumFrequency + property int maximumFrequency + property int state: stoppedState + property int scanningState: stoppedState + property bool scanningFreqUpdate: false + property string stationId: "" + + signal stationFound + + property Connections c : Connections { + target: root + + onFrequencyChanged: { + if(scanningState != activeState) { + // Not scanning, push update + sendSocketMessage("frequency", { value: frequency }) + } else if(!scanningFreqUpdate) { + // External change, stop scanning + sendSocketMessage("scan_stop", 'None') + scanningState = stoppedState + sendSocketMessage("frequency", { value: frequency }) + } else { + // This update was from scanning, clear state + scanningFreqUpdate = false + } + } + + onBandChanged: { + sendSocketMessage("band", { value: band }) + updateFrequencyRange(band) + updateFrequencyStep(band) + frequency = minimumFrequency + } + } + + onTextMessageReceived: { + var json = JSON.parse(message) + //console.debug("Raw response: " + message) + var request = json[2].request + var response = json[2].response + + switch (json[0]) { + case msgid.call: + break + case msgid.retok: + var verb = verbs.shift() + if (verb == "frequency_range") { + minimumFrequency = response.min + maximumFrequency = response.max + } else if (verb == "frequency_step") { + frequencyStep = response.step + } + break + case msgid.event: + var event = JSON.parse(JSON.stringify(json[2])) + if (event.event === "radio/frequency") { + if(scanningState == activeState) { + scanningFreqUpdate = true + frequency = event.data.value + } + } else if (event.event === "radio/station_found") { + if(scanningState == activeState) { + scanningState = stoppedState + stationId = freq2str(event.data.value) + root.stationFound() + } + } + break + case msg.reterr: + console.debug("Bad return value, binding probably not installed") + break + case MessageId.event: + break + } + } + + onStatusChanged: { + switch (status) { + case WebSocket.Open: + // Initialize band values now that we're connected to the + // binding + updateFrequencyRange(band) + updateFrequencyStep(band) + frequency = minimumFrequency + sendSocketMessage("subscribe", { value: "frequency" }) + sendSocketMessage("subscribe", { value: "station_found" }) + break + case WebSocket.Error: + console.debug("WebSocket error: " + root.errorString) + break + } + } + + function freq2str(freq) { + if (freq > 5000000) { + return '%1 MHz'.arg((freq / 1000000).toFixed(1)) + } else { + return '%1 kHz'.arg((freq / 1000).toFixed(0)) + } + } + + function sendSocketMessage(verb, parameter) { + var requestJson = [ msgid.call, payloadLength, apiString + '/' + + verb, parameter ] + //console.debug("sendSocketMessage: " + JSON.stringify(requestJson)) + verbs.push(verb) + sendTextMessage(JSON.stringify(requestJson)) + } + + function start() { + sendSocketMessage("start", 'None') + state = activeState + } + + function stop() { + sendSocketMessage("stop", 'None') + state = stoppedState + } + + function tuneUp() { + frequency += frequencyStep + if(frequency > maximumFrequency) { + frequency = minimumFrequency + } + } + + function tuneDown() { + frequency -= frequencyStep + if(frequency < minimumFrequency) { + frequency = maximumFrequency + } + } + + function scanUp() { + scanningState = activeState + sendSocketMessage("scan_start", { direction: "forward" }) + } + + function scanDown() { + scanningState = activeState + sendSocketMessage("scan_start", { direction: "backward" }) + } + + function updateFrequencyRange(band) { + sendSocketMessage("frequency_range", { band: band }) + } + + function updateFrequencyStep(band) { + sendSocketMessage("frequency_step", { band: band }) + } +} diff --git a/app/main.cpp b/app/main.cpp index 9d2785e..e430099 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 2016 The Qt Company Ltd. - * Copyright (C) 2016 Scott Murray + * Copyright (C) 2016, 2017 Konsulko Group * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,13 @@ */ #include +#include +#include #include #include #include #include #include -#include #include #include "PresetDataObject.h" @@ -45,6 +46,14 @@ int main(int argc, char *argv[]) QQuickStyle::setStyle("AGL"); + QCommandLineParser parser; + parser.addPositionalArgument("port", app.translate("main", "port for binding")); + parser.addPositionalArgument("secret", app.translate("main", "secret for binding")); + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + QStringList positionalArguments = parser.positionalArguments(); + // Read presets from configuration file // // If HOME is set, use $HOME/app-data/radio/presets.conf, else fall back @@ -70,13 +79,26 @@ int main(int argc, char *argv[]) pSettings->setArrayIndex(i); presetDataList.append(new PresetDataObject(pSettings->value("title").toString(), pSettings->value("frequency").toInt(), - QRadioTuner::FM)); + 1)); } pSettings->endArray(); QQmlApplicationEngine engine; QQmlContext *context = engine.rootContext(); context->setContextProperty("presetModel", QVariant::fromValue(presetDataList)); + if (positionalArguments.length() == 2) { + int port = positionalArguments.takeFirst().toInt(); + QString secret = positionalArguments.takeFirst(); + QUrl bindingAddress; + bindingAddress.setScheme(QStringLiteral("ws")); + bindingAddress.setHost(QStringLiteral("localhost")); + bindingAddress.setPort(port); + bindingAddress.setPath(QStringLiteral("/api")); + QUrlQuery query; + query.addQueryItem(QStringLiteral("token"), secret); + bindingAddress.setQuery(query); + context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress); + } engine.load(QUrl(QStringLiteral("qrc:/Radio.qml"))); return app.exec(); diff --git a/app/radio.qrc b/app/radio.qrc index 347894c..38ce4f8 100644 --- a/app/radio.qrc +++ b/app/radio.qrc @@ -1,5 +1,6 @@ Radio.qml + api/Binding.qml diff --git a/binding/binding.pri b/binding/binding.pri new file mode 100644 index 0000000..3448a56 --- /dev/null +++ b/binding/binding.pri @@ -0,0 +1,6 @@ +TEMPLATE = lib +CONFIG += plugin use_c_linker +CONFIG -= qt +QMAKE_CFLAGS += -Wextra -Wconversion -Wno-unused-parameter -Werror=maybe-uninitialized -Werror=implicit-function-declaration -ffunction-sections -fdata-sections -Wl,--as-needed -Wl,--gc-sections + +DESTDIR = $${OUT_PWD}/../package/root/lib diff --git a/binding/binding.pro b/binding/binding.pro new file mode 100644 index 0000000..d8c5a93 --- /dev/null +++ b/binding/binding.pro @@ -0,0 +1,11 @@ +TARGET = radio-binding + +HEADERS = radio_impl.h radio_output.h rtl_fm.h convenience/convenience.h +SOURCES = radio-binding.c radio_output.c radio_impl_rtlsdr.c rtl_fm.c convenience/convenience.c + +LIBS += -Wl,--version-script=$$PWD/export.map + +CONFIG += link_pkgconfig +PKGCONFIG += json-c afb-daemon librtlsdr glib-2.0 libpulse-simple + +include(binding.pri) diff --git a/binding/convenience/convenience.c b/binding/convenience/convenience.c new file mode 100644 index 0000000..517dc4e --- /dev/null +++ b/binding/convenience/convenience.c @@ -0,0 +1,304 @@ +/* + * Copyright (C) 2014 by Kyle Keen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* a collection of user friendly tools + * todo: use strtol for more flexible int parsing + * */ + +#include +#include +#include + +#ifndef _WIN32 +#include +#else +#include +#include +#include +#define _USE_MATH_DEFINES +#endif + +#include + +#include "rtl-sdr.h" + +double atofs(char *s) +/* standard suffixes */ +{ + char last; + int len; + double suff = 1.0; + len = strlen(s); + last = s[len-1]; + s[len-1] = '\0'; + switch (last) { + case 'g': + case 'G': + suff *= 1e3; + case 'm': + case 'M': + suff *= 1e3; + case 'k': + case 'K': + suff *= 1e3; + suff *= atof(s); + s[len-1] = last; + return suff; + } + s[len-1] = last; + return atof(s); +} + +double atoft(char *s) +/* time suffixes, returns seconds */ +{ + char last; + int len; + double suff = 1.0; + len = strlen(s); + last = s[len-1]; + s[len-1] = '\0'; + switch (last) { + case 'h': + case 'H': + suff *= 60; + case 'm': + case 'M': + suff *= 60; + case 's': + case 'S': + suff *= atof(s); + s[len-1] = last; + return suff; + } + s[len-1] = last; + return atof(s); +} + +double atofp(char *s) +/* percent suffixes */ +{ + char last; + int len; + double suff = 1.0; + len = strlen(s); + last = s[len-1]; + s[len-1] = '\0'; + switch (last) { + case '%': + suff *= 0.01; + suff *= atof(s); + s[len-1] = last; + return suff; + } + s[len-1] = last; + return atof(s); +} + +int nearest_gain(rtlsdr_dev_t *dev, int target_gain) +{ + int i, r, err1, err2, count, nearest; + int* gains; + r = rtlsdr_set_tuner_gain_mode(dev, 1); + if (r < 0) { + fprintf(stderr, "WARNING: Failed to enable manual gain.\n"); + return r; + } + count = rtlsdr_get_tuner_gains(dev, NULL); + if (count <= 0) { + return 0; + } + gains = malloc(sizeof(int) * count); + count = rtlsdr_get_tuner_gains(dev, gains); + nearest = gains[0]; + for (i=0; i= 0 && device < device_count) { + fprintf(stderr, "Using device %d: %s\n", + device, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + /* does string exact match a serial */ + for (i = 0; i < device_count; i++) { + rtlsdr_get_device_usb_strings(i, vendor, product, serial); + if (strcmp(s, serial) != 0) { + continue;} + device = i; + fprintf(stderr, "Using device %d: %s\n", + device, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + /* does string prefix match a serial */ + for (i = 0; i < device_count; i++) { + rtlsdr_get_device_usb_strings(i, vendor, product, serial); + if (strncmp(s, serial, strlen(s)) != 0) { + continue;} + device = i; + fprintf(stderr, "Using device %d: %s\n", + device, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + /* does string suffix match a serial */ + for (i = 0; i < device_count; i++) { + rtlsdr_get_device_usb_strings(i, vendor, product, serial); + offset = strlen(serial) - strlen(s); + if (offset < 0) { + continue;} + if (strncmp(s, serial+offset, strlen(s)) != 0) { + continue;} + device = i; + fprintf(stderr, "Using device %d: %s\n", + device, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + fprintf(stderr, "No matching devices found.\n"); + return -1; +} + +// vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab diff --git a/binding/convenience/convenience.h b/binding/convenience/convenience.h new file mode 100644 index 0000000..1faa2af --- /dev/null +++ b/binding/convenience/convenience.h @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2014 by Kyle Keen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* a collection of user friendly tools */ + +/*! + * Convert standard suffixes (k, M, G) to double + * + * \param s a string to be parsed + * \return double + */ + +double atofs(char *s); + +/*! + * Convert time suffixes (s, m, h) to double + * + * \param s a string to be parsed + * \return seconds as double + */ + +double atoft(char *s); + +/*! + * Convert percent suffixe (%) to double + * + * \param s a string to be parsed + * \return double + */ + +double atofp(char *s); + +/*! + * Find nearest supported gain + * + * \param dev the device handle given by rtlsdr_open() + * \param target_gain in tenths of a dB + * \return 0 on success + */ + +int nearest_gain(rtlsdr_dev_t *dev, int target_gain); + +/*! + * Set device frequency and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param frequency in Hz + * \return 0 on success + */ + +int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency); + +/*! + * Set device sample rate and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param samp_rate in samples/second + * \return 0 on success + */ + +int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate); + +/*! + * Enable or disable the direct sampling mode and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled + * \return 0 on success + */ + +int verbose_direct_sampling(rtlsdr_dev_t *dev, int on); + +/*! + * Enable offset tuning and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \return 0 on success + */ + +int verbose_offset_tuning(rtlsdr_dev_t *dev); + +/*! + * Enable auto gain and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \return 0 on success + */ + +int verbose_auto_gain(rtlsdr_dev_t *dev); + +/*! + * Set tuner gain and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param gain in tenths of a dB + * \return 0 on success + */ + +int verbose_gain_set(rtlsdr_dev_t *dev, int gain); + +/*! + * Set the frequency correction value for the device and report status on stderr. + * + * \param dev the device handle given by rtlsdr_open() + * \param ppm_error correction value in parts per million (ppm) + * \return 0 on success + */ + +int verbose_ppm_set(rtlsdr_dev_t *dev, int ppm_error); + +/*! + * Reset buffer + * + * \param dev the device handle given by rtlsdr_open() + * \return 0 on success + */ + +int verbose_reset_buffer(rtlsdr_dev_t *dev); + +/*! + * Find the closest matching device. + * + * \param s a string to be parsed + * \return dev_index int, -1 on error + */ + +int verbose_device_search(char *s); + diff --git a/binding/export.map b/binding/export.map new file mode 100644 index 0000000..52c1b4a --- /dev/null +++ b/binding/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1*; local: *; }; diff --git a/binding/radio-binding.c b/binding/radio-binding.c new file mode 100644 index 0000000..12ed966 --- /dev/null +++ b/binding/radio-binding.c @@ -0,0 +1,493 @@ +/* + * Copyright (C) 2017 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "radio_impl.h" + +static const struct afb_binding_interface *interface; + +static struct afb_event freq_event; +static struct afb_event scan_event; + +static void freq_callback(uint32_t frequency, void *data) +{ + json_object *jresp = json_object_new_object(); + json_object *value = json_object_new_int((int) frequency); + + json_object_object_add(jresp, "value", value); + afb_event_push(freq_event, json_object_get(jresp)); +} + +static void scan_callback(uint32_t frequency, void *data) +{ + json_object *jresp = json_object_new_object(); + json_object *value = json_object_new_int((int) frequency); + + json_object_object_add(jresp, "value", value); + afb_event_push(scan_event, json_object_get(jresp)); +} + +/* + * Binding verb handlers + */ + +/* + * @brief Get (and optionally set) frequency + * + * @param struct afb_req : an afb request structure + * + */ +static void frequency(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "value"); + uint32_t frequency; + + if(value) { + char *p; + frequency = strtoul(value, &p, 10); + if(frequency && *p == '\0') { + radio_impl_set_frequency(frequency); + } else { + afb_req_fail(request, "failed", "Invalid scan direction"); + return; + } + } + ret_json = json_object_new_object(); + frequency = radio_impl_get_frequency(); + json_object_object_add(ret_json, "frequency", json_object_new_int((int32_t) frequency)); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Get (and optionally set) frequency band + * + * @param struct afb_req : an afb request structure + * + */ +static void band(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "value"); + int valid = 0; + radio_band_t band; + char band_name[4]; + + if(value) { + if(!strcasecmp(value, "AM")) { + band = BAND_AM; + valid = 1; + } else if(!strcasecmp(value, "FM")) { + band = BAND_FM; + valid = 1; + } else { + char *p; + band = strtoul(value, &p, 10); + if(p != value && *p == '\0') { + switch(band) { + case BAND_AM: + case BAND_FM: + valid = 1; + break; + default: + break; + } + } + } + if(valid) { + radio_impl_set_band(band); + } else { + afb_req_fail(request, "failed", "Invalid band"); + return; + } + } + ret_json = json_object_new_object(); + band = radio_impl_get_band(); + sprintf(band_name, "%s", band == BAND_AM ? "AM" : "FM"); + json_object_object_add(ret_json, "band", json_object_new_string(band_name)); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Check if band is supported + * + * @param struct afb_req : an afb request structure + * + */ +static void band_supported(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "band"); + int valid = 0; + radio_band_t band; + + if(value) { + if(!strcasecmp(value, "AM")) { + band = BAND_AM; + valid = 1; + } else if(!strcasecmp(value, "FM")) { + band = BAND_FM; + valid = 1; + } else { + char *p; + band = strtoul(value, &p, 10); + if(p != value && *p == '\0') { + switch(band) { + case BAND_AM: + case BAND_FM: + valid = 1; + break; + default: + break; + } + } + } + } + if(!valid) { + afb_req_fail(request, "failed", "Invalid band"); + return; + } + ret_json = json_object_new_object(); + json_object_object_add(ret_json, + "supported", + json_object_new_int(radio_impl_band_supported(band))); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Get frequency range for a band + * + * @param struct afb_req : an afb request structure + * + */ +static void frequency_range(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "band"); + int valid = 0; + radio_band_t band; + uint32_t min_frequency; + uint32_t max_frequency; + + if(value) { + if(!strcasecmp(value, "AM")) { + band = BAND_AM; + valid = 1; + } else if(!strcasecmp(value, "FM")) { + band = BAND_FM; + valid = 1; + } else { + char *p; + band = strtoul(value, &p, 10); + if(p != value && *p == '\0') { + switch(band) { + case BAND_AM: + case BAND_FM: + valid = 1; + break; + default: + break; + } + } + } + } + if(!valid) { + afb_req_fail(request, "failed", "Invalid band"); + return; + } + ret_json = json_object_new_object(); + min_frequency = radio_impl_get_min_frequency(band); + max_frequency = radio_impl_get_max_frequency(band); + json_object_object_add(ret_json, "min", json_object_new_int((int32_t) min_frequency)); + json_object_object_add(ret_json, "max", json_object_new_int((int32_t) max_frequency)); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Get frequency step size (Hz) for a band + * + * @param struct afb_req : an afb request structure + * + */ +static void frequency_step(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "band"); + int valid = 0; + radio_band_t band; + uint32_t step; + + if(value) { + if(!strcasecmp(value, "AM")) { + band = BAND_AM; + valid = 1; + } else if(!strcasecmp(value, "FM")) { + band = BAND_FM; + valid = 1; + } else { + char *p; + band = strtoul(value, &p, 10); + if(p != value && *p == '\0') { + switch(band) { + case BAND_AM: + case BAND_FM: + valid = 1; + break; + default: + break; + } + } + } + } + if(!valid) { + afb_req_fail(request, "failed", "Invalid band"); + return; + } + ret_json = json_object_new_object(); + step = radio_impl_get_frequency_step(band); + json_object_object_add(ret_json, "step", json_object_new_int((int32_t) step)); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Start radio playback + * + * @param struct afb_req : an afb request structure + * + */ +static void start(struct afb_req request) +{ + radio_impl_start(); + afb_req_success(request, NULL, NULL); +} + +/* + * @brief Stop radio playback + * + * @param struct afb_req : an afb request structure + * + */ +static void stop(struct afb_req request) +{ + radio_impl_stop(); + afb_req_success(request, NULL, NULL); +} + +/* + * @brief Scan for a station in the specified direction + * + * @param struct afb_req : an afb request structure + * + */ +static void scan_start(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "direction"); + int valid = 0; + radio_scan_direction_t direction; + + if(value) { + if(!strcasecmp(value, "forward")) { + direction = SCAN_FORWARD; + valid = 1; + } else if(!strcasecmp(value, "backward")) { + direction = SCAN_BACKWARD; + valid = 1; + } else { + char *p; + direction = strtoul(value, &p, 10); + if(p != value && *p == '\0') { + switch(direction) { + case SCAN_FORWARD: + case SCAN_BACKWARD: + valid = 1; + break; + default: + break; + } + } + } + } + if(!valid) { + afb_req_fail(request, "failed", "Invalid direction"); + return; + } + radio_impl_scan_start(direction, scan_callback, NULL); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Stop station scan + * + * @param struct afb_req : an afb request structure + * + */ +static void scan_stop(struct afb_req request) +{ + radio_impl_scan_stop(); + afb_req_success(request, NULL, NULL); +} + +/* + * @brief Get (and optionally set) stereo mode + * + * @param struct afb_req : an afb request structure + * + */ +static void stereo_mode(struct afb_req request) +{ + json_object *ret_json; + const char *value = afb_req_value(request, "value"); + int valid = 0; + radio_stereo_mode_t mode; + char mode_name[4]; + + if(value) { + if(!strcasecmp(value, "mono")) { + mode = MONO; + valid = 1; + } else if(!strcasecmp(value, "stereo")) { + mode = STEREO; + valid = 1; + } else { + char *p; + mode = strtoul(value, &p, 10); + if(p != value && *p == '\0') { + switch(mode) { + case MONO: + case STEREO: + valid = 1; + break; + default: + break; + } + } + } + if(valid) { + radio_impl_set_stereo_mode(mode); + } else { + afb_req_fail(request, "failed", "Invalid mode"); + return; + } + } + ret_json = json_object_new_object(); + mode = radio_impl_get_stereo_mode(); + sprintf(mode_name, "%s", mode == MONO ? "mono" : "stereo"); + json_object_object_add(ret_json, "mode", json_object_new_string(mode_name)); + afb_req_success(request, ret_json, NULL); +} + +/* + * @brief Subscribe for an event + * + * @param struct afb_req : an afb request structure + * + */ +static void subscribe(struct afb_req request) +{ + const char *value = afb_req_value(request, "value"); + if(value) { + if(!strcasecmp(value, "frequency")) { + afb_req_subscribe(request, freq_event); + } else if(!strcasecmp(value, "station_found")) { + afb_req_subscribe(request, scan_event); + } else { + afb_req_fail(request, "failed", "Invalid event"); + return; + } + } + afb_req_success(request, NULL, NULL); +} + +/* + * @brief Unsubscribe for an event + * + * @param struct afb_req : an afb request structure + * + */ +static void unsubscribe(struct afb_req request) +{ + const char *value = afb_req_value(request, "value"); + if(value) { + if(!strcasecmp(value, "frequency")) { + afb_req_unsubscribe(request, freq_event); + } else if(!strcasecmp(value, "station_found")) { + afb_req_unsubscribe(request, scan_event); + } else { + afb_req_fail(request, "failed", "Invalid event"); + return; + } + } + afb_req_success(request, NULL, NULL); +} + +static const struct afb_verb_desc_v1 verbs[]= { + { "frequency", AFB_SESSION_CHECK, frequency, "Get/Set frequency" }, + { "band", AFB_SESSION_CHECK, band, "Get/Set band" }, + { "band_supported", AFB_SESSION_CHECK, band_supported, "Check band support" }, + { "frequency_range", AFB_SESSION_CHECK, frequency_range, "Get frequency range" }, + { "frequency_step", AFB_SESSION_CHECK, frequency_step, "Get frequency step" }, + { "start", AFB_SESSION_CHECK, start, "Start radio playback" }, + { "stop", AFB_SESSION_CHECK, stop, "Stop radio playback" }, + { "scan_start", AFB_SESSION_CHECK, scan_start, "Start station scan" }, + { "scan_stop", AFB_SESSION_CHECK, scan_stop, "Stop station scan" }, + { "stereo_mode", AFB_SESSION_CHECK, stereo_mode, "Get/Set stereo_mode" }, + { "subscribe", AFB_SESSION_CHECK, subscribe, "Subscribe for an event" }, + { "unsubscribe", AFB_SESSION_CHECK, unsubscribe, "Unsubscribe for an event" }, + { NULL } +}; + +static const struct afb_binding binding_desc = { + .type = AFB_BINDING_VERSION_1, + .v1 = { + .info = "radio service", + .prefix = "radio", + .verbs = verbs + } +}; + +const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf) +{ + interface = itf; + + return &binding_desc; +} + +int afbBindingV1ServiceInit(struct afb_service service) +{ + int rc; + + freq_event = afb_daemon_make_event(interface->daemon, "frequency"); + scan_event = afb_daemon_make_event(interface->daemon, "station_found"); + + rc = radio_impl_init(); + if(rc == 0) { + radio_impl_set_frequency_callback(freq_callback, NULL); + } + + return rc; +} diff --git a/binding/radio_impl.h b/binding/radio_impl.h new file mode 100644 index 0000000..79e91a4 --- /dev/null +++ b/binding/radio_impl.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2017 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _RADIO_IMPL_H +#define _RADIO_IMPL_H + +#include + +typedef enum { + BAND_AM = 0, + BAND_FM +} radio_band_t; + +typedef enum { + SCAN_FORWARD = 0, + SCAN_BACKWARD +} radio_scan_direction_t; + +typedef void (*radio_scan_callback_t)(uint32_t frequency, void *data); + +typedef void (*radio_freq_callback_t)(uint32_t frequency, void *data); + +typedef enum { + MONO = 0, + STEREO +} radio_stereo_mode_t; + +int radio_impl_init(void); + +uint32_t radio_impl_get_frequency(void); + +void radio_impl_set_frequency(uint32_t frequency); + +void radio_impl_set_frequency_callback(radio_freq_callback_t callback, + void *data); + +radio_band_t radio_impl_get_band(void); + +void radio_impl_set_band(radio_band_t band); + +int radio_impl_band_supported(radio_band_t band); + +uint32_t radio_impl_get_min_frequency(radio_band_t band); + +uint32_t radio_impl_get_max_frequency(radio_band_t band); + +uint32_t radio_impl_get_frequency_step(radio_band_t band); + +void radio_impl_start(void); + +void radio_impl_stop(void); + +void radio_impl_scan_start(radio_scan_direction_t direction, + radio_scan_callback_t callback, + void *data); + +void radio_impl_scan_stop(void); + +radio_stereo_mode_t radio_impl_get_stereo_mode(void); + +void radio_impl_set_stereo_mode(radio_stereo_mode_t mode); + +#endif /* _RADIO_IMPL_H */ diff --git a/binding/radio_impl_rtlsdr.c b/binding/radio_impl_rtlsdr.c new file mode 100644 index 0000000..4364fd5 --- /dev/null +++ b/binding/radio_impl_rtlsdr.c @@ -0,0 +1,254 @@ +/* + * Copyright (C) 2017 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include "radio_impl.h" +#include "radio_output.h" +#include "rtl_fm.h" + +// Structure to describe FM band plans, all values in Hz. +typedef struct { + char *name; + uint32_t min; + uint32_t max; + uint32_t step; +} fm_band_plan_t; + +static fm_band_plan_t known_fm_band_plans[5] = { + { .name = "US", .min = 87900000, .max = 107900000, .step = 200000 }, + { .name = "JP", .min = 76100000, .max = 89900000, .step = 100000 }, + { .name = "EU", .min = 87500000, .max = 108000000, .step = 50000 }, + { .name = "ITU-1", .min = 87500000, .max = 108000000, .step = 50000 }, + { .name = "ITU-2", .min = 87900000, .max = 107900000, .step = 50000 } +}; + +static unsigned int bandplan; +static bool present; +static bool active; +static uint32_t current_frequency; + +static void rtl_output_callback(int16_t *result, int result_len, void *ctx) +{ + if(active) + radio_output_write((char*) result, result_len * 2); +} + +int radio_impl_init(void) +{ + GKeyFile* conf_file; + int conf_file_present = 0; + char *value_str; + + if(present) + return -1; + + // Load settings from configuration file if it exists + conf_file = g_key_file_new(); + if(conf_file && + g_key_file_load_from_dirs(conf_file, + "AGL.conf", + (const gchar**) g_get_system_config_dirs(), + NULL, + G_KEY_FILE_KEEP_COMMENTS, + NULL) == TRUE) { + conf_file_present = 1; + + // Set band plan if it is specified + value_str = g_key_file_get_string(conf_file, + "radio", + "fmbandplan", + NULL); + if(value_str) { + unsigned int i; + for(i = 0; + i < sizeof(known_fm_band_plans) / sizeof(fm_band_plan_t); + i++) { + if(!strcasecmp(value_str, known_fm_band_plans[i].name)) { + bandplan = i; + break; + } + } + } + } + fprintf(stderr, "Using FM Bandplan: %s\n", known_fm_band_plans[bandplan].name); + + current_frequency = radio_impl_get_min_frequency(BAND_FM); + if(rtl_fm_init(current_frequency, 200000, 48000, rtl_output_callback, NULL) < 0) { + return -1; + } + + if(conf_file_present) { + GError *error = NULL; + int n; + + // Allow over-riding scanning parameters just in case a demo + // setup needs to do so to work reliably. + n = g_key_file_get_integer(conf_file, + "radio", + "scan_squelch_level", + &error); + //error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND && + //error->code != G_KEY_FILE_ERROR_INVALID_VALUE) { + if(!error) { + fprintf(stderr, "Scanning squelch level set to %d\n", n); + rtl_fm_scan_set_squelch_level(n); + } + + error = NULL; + n = g_key_file_get_integer(conf_file, + "radio", + "scan_squelch_limit", + &error); + if(!error) { + fprintf(stderr, "Scanning squelch limit set to %d\n", n); + rtl_fm_scan_set_squelch_limit(n); + } + + g_key_file_free(conf_file); + } + + present = true; + return 0; +} + +uint32_t radio_impl_get_frequency(void) +{ + return current_frequency; +} + +void radio_impl_set_frequency(uint32_t frequency) +{ + if(!present) + return; + + if(frequency < known_fm_band_plans[bandplan].min || + frequency > known_fm_band_plans[bandplan].max) + return; + + radio_impl_scan_stop(); + current_frequency = frequency; + rtl_fm_set_freq(frequency); +} + +void radio_impl_set_frequency_callback(radio_freq_callback_t callback, + void *data) +{ + rtl_fm_set_freq_callback(callback, data); +} + +radio_band_t radio_impl_get_band(void) +{ + return BAND_FM; +} + +void radio_impl_set_band(radio_band_t band) +{ + // We only support FM, so do nothing +} + +int radio_impl_band_supported(radio_band_t band) +{ + if(band == BAND_FM) + return 1; + return 0; +} + +uint32_t radio_impl_get_min_frequency(radio_band_t band) +{ + return known_fm_band_plans[bandplan].min; +} + +uint32_t radio_impl_get_max_frequency(radio_band_t band) +{ + return known_fm_band_plans[bandplan].max; +} + +uint32_t radio_impl_get_frequency_step(radio_band_t band) +{ + uint32_t ret = 0; + + switch (band) { + case BAND_AM: + ret = 1000; // 1 kHz + break; + case BAND_FM: + ret = known_fm_band_plans[bandplan].step; + break; + default: + break; + } + return ret; +} + +void radio_impl_start(void) +{ + if(!present) + return; + + if(!active) { + if(radio_output_start() != 0) + return; + + rtl_fm_start(); + active = true; + } +} + +void radio_impl_stop(void) +{ + if(!present) + return; + + if(active) { + active = false; + radio_output_stop(); + rtl_fm_stop(); + + } +} + +void radio_impl_scan_start(radio_scan_direction_t direction, + radio_scan_callback_t callback, + void *data) +{ + rtl_fm_scan_start(direction == SCAN_FORWARD ? 0 : 1, + callback, + data, + radio_impl_get_frequency_step(BAND_FM), + radio_impl_get_min_frequency(BAND_FM), + radio_impl_get_max_frequency(BAND_FM)); +} + +void radio_impl_scan_stop(void) +{ + rtl_fm_scan_stop(); +} + +radio_stereo_mode_t radio_impl_get_stereo_mode(void) +{ + return STEREO; +} + +void radio_impl_set_stereo_mode(radio_stereo_mode_t mode) +{ + // We only support stereo, so do nothing +} diff --git a/binding/radio_output.c b/binding/radio_output.c new file mode 100644 index 0000000..a49687b --- /dev/null +++ b/binding/radio_output.c @@ -0,0 +1,294 @@ +/* + * Copyright (C) 2017 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "radio_output.h" +#include "rtl_fm.h" + +static pa_threaded_mainloop *mainloop; +static pa_context *context; +static pa_stream *stream; + +static unsigned int extra; +static int16_t extra_buf[1]; +static unsigned char *output_buf; + +static void pa_context_state_cb(pa_context *c, void *data) { + pa_operation *o; + + assert(c); + switch (pa_context_get_state(c)) { + case PA_CONTEXT_CONNECTING: + case PA_CONTEXT_AUTHORIZING: + case PA_CONTEXT_SETTING_NAME: + case PA_CONTEXT_READY: + break; + case PA_CONTEXT_TERMINATED: + pa_threaded_mainloop_stop(mainloop); + break; + case PA_CONTEXT_FAILED: + default: + fprintf(stderr, "PA connection failed: %s\n", + pa_strerror(pa_context_errno(c))); + pa_threaded_mainloop_stop(mainloop); + break; + } + pa_threaded_mainloop_signal(mainloop, 0); +} + +int radio_output_open(void) +{ + pa_context *c; + pa_mainloop_api *mapi; + char *client; + + if(context) + return 0; + + if (!(mainloop = pa_threaded_mainloop_new())) { + fprintf(stderr, "pa_mainloop_new() failed.\n"); + return -1; + } + + pa_threaded_mainloop_set_name(mainloop, "pa_mainloop"); + mapi = pa_threaded_mainloop_get_api(mainloop); + + client = pa_xstrdup("radio"); + if (!(c = pa_context_new(mapi, client))) { + fprintf(stderr, "pa_context_new() failed.\n"); + goto exit; + } + + pa_context_set_state_callback(c, pa_context_state_cb, NULL); + if (pa_context_connect(c, NULL, 0, NULL) < 0) { + fprintf(stderr, "pa_context_connect(): %s", pa_strerror(pa_context_errno(c))); + goto exit; + } + + if (pa_threaded_mainloop_start(mainloop) < 0) { + fprintf(stderr, "pa_mainloop_run() failed.\n"); + goto exit; + } + + context = c; + + extra = 0; + output_buf = malloc(sizeof(unsigned char) * RTL_FM_MAXIMUM_BUF_LENGTH); + + return 0; + +exit: + if (c) + pa_context_unref(c); + + if (mainloop) + pa_threaded_mainloop_free(mainloop); + + pa_xfree(client); + return -1; +} + +int radio_output_start(void) +{ + int error = 0; + pa_sample_spec *spec; + + if(stream) + return 0; + + if(!context) { + error = radio_output_open(); + if(error != 0) + return error; + } + + while(pa_context_get_state(context) != PA_CONTEXT_READY) + pa_threaded_mainloop_wait(mainloop); + + spec = (pa_sample_spec*) calloc(1, sizeof(pa_sample_spec)); + spec->format = PA_SAMPLE_S16LE; + spec->rate = 24000; + spec->channels = 2; + if (!pa_sample_spec_valid(spec)) { + fprintf(stderr, "%s\n", + pa_strerror(pa_context_errno(context))); + return -1; + } + + pa_threaded_mainloop_lock(mainloop); + pa_proplist *props = pa_proplist_new(); + pa_proplist_sets(props, PA_PROP_MEDIA_ROLE, "radio"); + stream = pa_stream_new_with_proplist(context, "radio-output", spec, 0, props); + if(!stream) { + fprintf(stderr, "Error creating stream %s\n", + pa_strerror(pa_context_errno(context))); + pa_proplist_free(props); + free(spec); + pa_threaded_mainloop_unlock(mainloop); + return -1; + } + pa_proplist_free(props); + free(spec); + + if(pa_stream_connect_playback(stream, + NULL, + NULL, + (pa_stream_flags_t) 0, + NULL, + NULL) < 0) { + fprintf(stderr, "Error connecting to PulseAudio : %s\n", + pa_strerror(pa_context_errno(context))); + pa_stream_unref(stream); + stream = NULL; + pa_threaded_mainloop_unlock(mainloop); + return -1; + } + + pa_threaded_mainloop_unlock(mainloop); + + while(pa_stream_get_state(stream) != PA_STREAM_READY) + pa_threaded_mainloop_wait(mainloop); + + return error; +} + +void radio_output_stop(void) +{ + if(stream) { + pa_threaded_mainloop_lock(mainloop); + + pa_stream_set_state_callback(stream, 0, 0); + pa_stream_set_write_callback(stream, 0, 0); + pa_stream_set_underflow_callback(stream, 0, 0); + pa_stream_set_overflow_callback(stream, 0, 0); + pa_stream_set_latency_update_callback(stream, 0, 0); + + pa_operation *o = pa_stream_flush(stream, NULL, NULL); + if(o) + pa_operation_unref(o); + + pa_stream_disconnect(stream); + pa_stream_unref(stream); + stream = NULL; + + pa_threaded_mainloop_unlock(mainloop); + } +} + +void radio_output_suspend(int state) +{ + if(stream) { + pa_stream_cork(stream, state, NULL, NULL); + } +} + +void radio_output_close(void) +{ + radio_output_stop(); + + if(context) { + pa_context_disconnect(context); + pa_context_unref(context); + context = NULL; + } + + if(mainloop) { + pa_threaded_mainloop_stop(mainloop); + pa_threaded_mainloop_free(mainloop); + mainloop = NULL; + } + + free(output_buf); + output_buf = NULL; +} + +int radio_output_write(void *buf, int len) +{ + int rc = -EINVAL; + int error; + size_t n = len; + size_t avail; + int samples = len / 2; + void *p; + + if(!stream) { + return -1; + } + + if(!buf) { + fprintf(stderr, "Error: buf == null!\n"); + return rc; + } + + pa_threaded_mainloop_lock(mainloop); + + avail = pa_stream_writable_size(stream); + if(avail < n) { + /* + * NOTE: Definitely room for improvement here,but for now just + * check for the no space case that happens when the + * stream is corked. + */ + if(!avail) { + rc = 0; + goto exit; + } + } + + /* + * Handle the rtl_fm code giving us an odd number of samples, which + * PA does not like. This extra buffer copying approach is not + * particularly efficient, but works for now. It looks feasible to + * hack in something in the demod and output thread routines in + * rtl_fm.c to handle it there if more performance is required. + */ + p = output_buf; + if(extra) { + memcpy(output_buf, extra_buf, sizeof(int16_t)); + if((extra + samples) % 2) { + // We still have an extra sample, n remains the same, store the extra + memcpy(output_buf + sizeof(int16_t), buf, n - 2); + memcpy(extra_buf, ((unsigned char*) buf) + n - 2, sizeof(int16_t)); + } else { + // We have an even number of samples, no extra + memcpy(output_buf + sizeof(int16_t), buf, n); + n += 2; + extra = 0; + } + } else if(samples % 2) { + // We have an extra sample, store it, and decrease n + n -= 2; + memcpy(output_buf + sizeof(int16_t), buf, n); + memcpy(extra_buf, ((unsigned char*) buf) + n, sizeof(int16_t)); + extra = 1; + } else { + p = buf; + } + + if ((rc = pa_stream_write(stream, p, n, NULL, 0, PA_SEEK_RELATIVE)) < 0) { + fprintf(stderr, "Error writing %d bytes to PulseAudio : %s\n", + n, pa_strerror(pa_context_errno(context))); + } +exit: + pa_threaded_mainloop_unlock(mainloop); + + return rc; +} diff --git a/binding/radio_output.h b/binding/radio_output.h new file mode 100644 index 0000000..2192811 --- /dev/null +++ b/binding/radio_output.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _RADIO_OUTPUT_H +#define _RADIO_OUTPUT_H + +int radio_output_open(void); + +int radio_output_start(void); + +void radio_output_stop(void); + +void radio_output_close(void); + +int radio_output_write(void *buf, int len); + +#endif /* _RADIO_OUTPUT_H */ + diff --git a/binding/rtl_fm.c b/binding/rtl_fm.c new file mode 100644 index 0000000..1c6a6b2 --- /dev/null +++ b/binding/rtl_fm.c @@ -0,0 +1,1267 @@ +/* + * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver + * Copyright (C) 2012 by Steve Markgraf + * Copyright (C) 2012 by Hoernchen + * Copyright (C) 2012 by Kyle Keen + * Copyright (C) 2013 by Elias Oenal + * Copyright (C) 2016, 2017 Konsulko Group + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* + * Note that this version replaces the standalone main() with separate + * init/start/stop API calls to allow building into another application. + * Other than removing the separate controller thread and adding an output + * function callback, other changes have been kept to a minimum to + * potentially allow using other rtl_fm features by modifying rtl_fm_init. + * + * December 2016, Scott Murray + */ + +/* + * written because people could not do real time + * FM demod on Atom hardware with GNU radio + * based on rtl_sdr.c and rtl_tcp.c + * + * lots of locks, but that is okay + * (no many-to-many locks) + * + * todo: + * sanity checks + * scale squelch to other input parameters + * test all the demodulations + * pad output on hop + * frequency ranges could be stored better + * scaled AM demod amplification + * auto-hop after time limit + * peak detector to tune onto stronger signals + * fifo for active hop frequency + * clips + * noise squelch + * merge stereo patch + * merge soft agc patch + * merge udp patch + * testmode to detect overruns + * watchdog to reset bad dongle + * fix oversampling + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rtl-sdr.h" +#include "rtl_fm.h" +#include "convenience/convenience.h" + +#define DEFAULT_SAMPLE_RATE 24000 +#define DEFAULT_BUF_LENGTH RTL_FM_DEFAULT_BUF_LENGTH +#define MAXIMUM_OVERSAMPLE RTL_FM_MAXIMUM_OVERSAMPLE +#define MAXIMUM_BUF_LENGTH RTL_FM_MAXIMUM_BUF_LENGTH +#define AUTO_GAIN -100 +#define BUFFER_DUMP 4096 + +#define FREQUENCIES_LIMIT 1000 + +#define DEFAULT_SQUELCH_LEVEL 140 +#define DEFAULT_CONSEQ_SQUELCH 10 + +static volatile int do_exit = 0; +static int lcm_post[17] = {1,1,1,3,1,5,3,7,1,9,5,11,3,13,7,15,1}; +static int ACTUAL_BUF_LENGTH; + +static int *atan_lut = NULL; +static int atan_lut_size = 131072; /* 512 KB */ +static int atan_lut_coef = 8; + +struct dongle_state +{ + int exit_flag; + pthread_t thread; + rtlsdr_dev_t *dev; + int dev_index; + uint32_t freq; + uint32_t rate; + int gain; + uint16_t buf16[MAXIMUM_BUF_LENGTH]; + uint32_t buf_len; + int ppm_error; + int offset_tuning; + int direct_sampling; + int mute; + struct demod_state *demod_target; +}; + +struct demod_state +{ + int exit_flag; + pthread_t thread; + int16_t lowpassed[MAXIMUM_BUF_LENGTH]; + int lp_len; + int16_t lp_i_hist[10][6]; + int16_t lp_q_hist[10][6]; + int16_t result[MAXIMUM_BUF_LENGTH]; + int16_t droop_i_hist[9]; + int16_t droop_q_hist[9]; + int result_len; + int rate_in; + int rate_out; + int rate_out2; + int now_r, now_j; + int pre_r, pre_j; + int prev_index; + int downsample; /* min 1, max 256 */ + int post_downsample; + int output_scale; + int squelch_level, conseq_squelch, squelch_hits, terminate_on_squelch; + int downsample_passes; + int comp_fir_size; + int custom_atan; + int deemph, deemph_a; + int now_lpr; + int prev_lpr_index; + int dc_block, dc_avg; + void (*mode_demod)(struct demod_state*); + pthread_rwlock_t rw; + pthread_cond_t ready; + pthread_mutex_t ready_m; + struct output_state *output_target; +}; + +struct output_state +{ + int exit_flag; + pthread_t thread; + rtl_fm_output_fn_t output_fn; + void *output_fn_data; + int16_t result[MAXIMUM_BUF_LENGTH]; + int result_len; + int rate; + pthread_rwlock_t rw; + pthread_cond_t ready; + pthread_mutex_t ready_m; +}; + +struct controller_state +{ + int exit_flag; + pthread_t thread; + uint32_t freqs[FREQUENCIES_LIMIT]; + int freq_len; + int freq_now; + int edge; + int wb_mode; + pthread_cond_t hop; + pthread_mutex_t hop_m; + + void (*freq_callback)(uint32_t, void*); + void *freq_callback_data; + + int scanning; + int scan_direction; + void (*scan_callback)(uint32_t, void*); + void *scan_callback_data; + uint32_t scan_step; + uint32_t scan_min; + uint32_t scan_max; + int scan_squelch_level; + int scan_squelch_count; +}; + +// multiple of these, eventually +struct dongle_state dongle; +struct demod_state demod; +struct output_state output; +struct controller_state controller; + +#if 0 +static void sighandler(int signum) +{ + fprintf(stderr, "Signal caught, exiting!\n"); + do_exit = 1; + rtlsdr_cancel_async(dongle.dev); +} +#endif + +/* more cond dumbness */ +#define safe_cond_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m) +#define safe_cond_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m) + +/* {length, coef, coef, coef} and scaled by 2^15 + for now, only length 9, optimal way to get +85% bandwidth */ +#define CIC_TABLE_MAX 10 +int cic_9_tables[][10] = { + {0,}, + {9, -156, -97, 2798, -15489, 61019, -15489, 2798, -97, -156}, + {9, -128, -568, 5593, -24125, 74126, -24125, 5593, -568, -128}, + {9, -129, -639, 6187, -26281, 77511, -26281, 6187, -639, -129}, + {9, -122, -612, 6082, -26353, 77818, -26353, 6082, -612, -122}, + {9, -120, -602, 6015, -26269, 77757, -26269, 6015, -602, -120}, + {9, -120, -582, 5951, -26128, 77542, -26128, 5951, -582, -120}, + {9, -119, -580, 5931, -26094, 77505, -26094, 5931, -580, -119}, + {9, -119, -578, 5921, -26077, 77484, -26077, 5921, -578, -119}, + {9, -119, -577, 5917, -26067, 77473, -26067, 5917, -577, -119}, + {9, -199, -362, 5303, -25505, 77489, -25505, 5303, -362, -199}, +}; + +void rotate_90(unsigned char *buf, uint32_t len) +/* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j + or [0, 1, -3, 2, -4, -5, 7, -6] */ +{ + uint32_t i; + unsigned char tmp; + for (i=0; ilp_len) { + d->now_r += d->lowpassed[i]; + d->now_j += d->lowpassed[i+1]; + i += 2; + d->prev_index++; + if (d->prev_index < d->downsample) { + continue; + } + d->lowpassed[i2] = d->now_r; // * d->output_scale; + d->lowpassed[i2+1] = d->now_j; // * d->output_scale; + d->prev_index = 0; + d->now_r = 0; + d->now_j = 0; + i2 += 2; + } + d->lp_len = i2; +} + +int low_pass_simple(int16_t *signal2, int len, int step) +// no wrap around, length must be multiple of step +{ + int i, i2, sum; + for(i=0; i < len; i+=step) { + sum = 0; + for(i2=0; i2rate_out; + int slow = s->rate_out2; + while (i < s->result_len) { + s->now_lpr += s->result[i]; + i++; + s->prev_lpr_index += slow; + if (s->prev_lpr_index < fast) { + continue; + } + s->result[i2] = (int16_t)(s->now_lpr / (fast/slow)); + s->prev_lpr_index -= fast; + s->now_lpr = 0; + i2 += 1; + } + s->result_len = i2; +} + +void fifth_order(int16_t *data, int length, int16_t *hist) +/* for half of interleaved data */ +{ + int i; + int16_t a, b, c, d, e, f; + a = hist[1]; + b = hist[2]; + c = hist[3]; + d = hist[4]; + e = hist[5]; + f = data[0]; + /* a downsample should improve resolution, so don't fully shift */ + data[0] = (a + (b+e)*5 + (c+d)*10 + f) >> 4; + for (i=4; i> 4; + } + /* archive */ + hist[0] = a; + hist[1] = b; + hist[2] = c; + hist[3] = d; + hist[4] = e; + hist[5] = f; +} + +void generic_fir(int16_t *data, int length, int *fir, int16_t *hist) +/* Okay, not at all generic. Assumes length 9, fix that eventually. */ +{ + int d, temp, sum; + for (d=0; d> 15 ; + hist[0] = hist[1]; + hist[1] = hist[2]; + hist[2] = hist[3]; + hist[3] = hist[4]; + hist[4] = hist[5]; + hist[5] = hist[6]; + hist[6] = hist[7]; + hist[7] = hist[8]; + hist[8] = temp; + } +} + +/* define our own complex math ops + because ARMv5 has no hardware float */ + +void multiply(int ar, int aj, int br, int bj, int *cr, int *cj) +{ + *cr = ar*br - aj*bj; + *cj = aj*br + ar*bj; +} + +int polar_discriminant(int ar, int aj, int br, int bj) +{ + int cr, cj; + double angle; + multiply(ar, aj, br, -bj, &cr, &cj); + angle = atan2((double)cj, (double)cr); + return (int)(angle / 3.14159 * (1<<14)); +} + +int fast_atan2(int y, int x) +/* pre scaled for int16 */ +{ + int yabs, angle; + int pi4=(1<<12), pi34=3*(1<<12); // note pi = 1<<14 + if (x==0 && y==0) { + return 0; + } + yabs = y; + if (yabs < 0) { + yabs = -yabs; + } + if (x >= 0) { + angle = pi4 - pi4 * (x-yabs) / (x+yabs); + } else { + angle = pi34 - pi4 * (x+yabs) / (yabs-x); + } + if (y < 0) { + return -angle; + } + return angle; +} + +int polar_disc_fast(int ar, int aj, int br, int bj) +{ + int cr, cj; + multiply(ar, aj, br, -bj, &cr, &cj); + return fast_atan2(cj, cr); +} + +int atan_lut_init(void) +{ + int i = 0; + + atan_lut = malloc(atan_lut_size * sizeof(int)); + + for (i = 0; i < atan_lut_size; i++) { + atan_lut[i] = (int) (atan((double) i / (1< 0) + {return 1 << 13;} + if (cr == 0 && cj < 0) + {return -(1 << 13);} + if (cj == 0 && cr > 0) + {return 0;} + if (cj == 0 && cr < 0) + {return 1 << 14;} + } + + /* real range -32768 - 32768 use 64x range -> absolute maximum: 2097152 */ + x = (cj << atan_lut_coef) / cr; + x_abs = abs(x); + + if (x_abs >= atan_lut_size) { + /* we can use linear range, but it is not necessary */ + return (cj > 0) ? 1<<13 : -1<<13; + } + + if (x > 0) { + return (cj > 0) ? atan_lut[x] : atan_lut[x] - (1<<14); + } else { + return (cj > 0) ? (1<<14) - atan_lut[-x] : -atan_lut[-x]; + } + + return 0; +} + +void fm_demod(struct demod_state *fm) +{ + int i, pcm; + int16_t *lp = fm->lowpassed; + pcm = polar_discriminant(lp[0], lp[1], + fm->pre_r, fm->pre_j); + fm->result[0] = (int16_t)pcm; + for (i = 2; i < (fm->lp_len-1); i += 2) { + switch (fm->custom_atan) { + case 0: + pcm = polar_discriminant(lp[i], lp[i+1], + lp[i-2], lp[i-1]); + break; + case 1: + pcm = polar_disc_fast(lp[i], lp[i+1], + lp[i-2], lp[i-1]); + break; + case 2: + pcm = polar_disc_lut(lp[i], lp[i+1], + lp[i-2], lp[i-1]); + break; + } + fm->result[i/2] = (int16_t)pcm; + } + fm->pre_r = lp[fm->lp_len - 2]; + fm->pre_j = lp[fm->lp_len - 1]; + fm->result_len = fm->lp_len/2; +} + +void am_demod(struct demod_state *fm) +// todo, fix this extreme laziness +{ + int i, pcm; + int16_t *lp = fm->lowpassed; + int16_t *r = fm->result; + for (i = 0; i < fm->lp_len; i += 2) { + // hypot uses floats but won't overflow + //r[i/2] = (int16_t)hypot(lp[i], lp[i+1]); + pcm = lp[i] * lp[i]; + pcm += lp[i+1] * lp[i+1]; + r[i/2] = (int16_t)sqrt(pcm) * fm->output_scale; + } + fm->result_len = fm->lp_len/2; + // lowpass? (3khz) highpass? (dc) +} + +void usb_demod(struct demod_state *fm) +{ + int i, pcm; + int16_t *lp = fm->lowpassed; + int16_t *r = fm->result; + for (i = 0; i < fm->lp_len; i += 2) { + pcm = lp[i] + lp[i+1]; + r[i/2] = (int16_t)pcm * fm->output_scale; + } + fm->result_len = fm->lp_len/2; +} + +void lsb_demod(struct demod_state *fm) +{ + int i, pcm; + int16_t *lp = fm->lowpassed; + int16_t *r = fm->result; + for (i = 0; i < fm->lp_len; i += 2) { + pcm = lp[i] - lp[i+1]; + r[i/2] = (int16_t)pcm * fm->output_scale; + } + fm->result_len = fm->lp_len/2; +} + +void raw_demod(struct demod_state *fm) +{ + int i; + for (i = 0; i < fm->lp_len; i++) { + fm->result[i] = (int16_t)fm->lowpassed[i]; + } + fm->result_len = fm->lp_len; +} + +void deemph_filter(struct demod_state *fm) +{ + static int avg; // cheating... + int i, d; + // de-emph IIR + // avg = avg * (1 - alpha) + sample * alpha; + for (i = 0; i < fm->result_len; i++) { + d = fm->result[i] - avg; + if (d > 0) { + avg += (d + fm->deemph_a/2) / fm->deemph_a; + } else { + avg += (d - fm->deemph_a/2) / fm->deemph_a; + } + fm->result[i] = (int16_t)avg; + } +} + +void dc_block_filter(struct demod_state *fm) +{ + int i, avg; + int64_t sum = 0; + for (i=0; i < fm->result_len; i++) { + sum += fm->result[i]; + } + avg = sum / fm->result_len; + avg = (avg + fm->dc_avg * 9) / 10; + for (i=0; i < fm->result_len; i++) { + fm->result[i] -= avg; + } + fm->dc_avg = avg; +} + +int mad(int16_t *samples, int len, int step) +/* mean average deviation */ +{ + int i=0, sum=0, ave=0; + if (len == 0) + {return 0;} + for (i=0; i len2) { + tick -= len2; + i++; + } + if (i >= len1) { + i = len1 - 1; + tick = len2; + } + } +} + +void arbitrary_downsample(int16_t *buf1, int16_t *buf2, int len1, int len2) +/* fractional boxcar lowpass, len1 > len2 */ +{ + int i = 1; + int j = 0; + int tick = 0; + double remainder = 0; + double frac; // use integers... + buf2[0] = 0; + while (j < len2) { + frac = 1.0; + if ((tick + len2) > len1) { + frac = (double)(len1 - tick) / (double)len2;} + buf2[j] += (int16_t)((double)buf1[i] * frac + remainder); + remainder = (double)buf1[i] * (1.0-frac); + tick += len2; + i++; + if (tick > len1) { + j++; + buf2[j] = 0; + tick -= len1; + } + if (i >= len1) { + i = len1 - 1; + tick = len1; + } + } + for (j=0; jdownsample_passes; + if (ds_p) { + for (i=0; i < ds_p; i++) { + fifth_order(d->lowpassed, (d->lp_len >> i), d->lp_i_hist[i]); + fifth_order(d->lowpassed+1, (d->lp_len >> i) - 1, d->lp_q_hist[i]); + } + d->lp_len = d->lp_len >> ds_p; + /* droop compensation */ + if (d->comp_fir_size == 9 && ds_p <= CIC_TABLE_MAX) { + generic_fir(d->lowpassed, d->lp_len, + cic_9_tables[ds_p], d->droop_i_hist); + generic_fir(d->lowpassed+1, d->lp_len-1, + cic_9_tables[ds_p], d->droop_q_hist); + } + } else { + low_pass(d); + } + /* power squelch */ + if (d->squelch_level) { + sr = rms(d->lowpassed, d->lp_len, 1); + if (sr < d->squelch_level) { + d->squelch_hits++; + for (i=0; i< d->lp_len; i++) { + d->lowpassed[i] = 0; + } + } else { + d->squelch_hits = 0; + } + } + d->mode_demod(d); /* lowpassed -> result */ + if (d->mode_demod == &raw_demod) { + return; + } + /* todo, fm noise squelch */ + // use nicer filter here too? + if (d->post_downsample > 1) { + d->result_len = low_pass_simple(d->result, d->result_len, d->post_downsample);} + if (d->deemph) { + deemph_filter(d);} + if (d->dc_block) { + dc_block_filter(d);} + if (d->rate_out2 > 0) { + low_pass_real(d); + //arbitrary_resample(d->result, d->result, d->result_len, d->result_len * d->rate_out2 / d->rate_out); + } +} + +static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) +{ + int i; + struct dongle_state *s = ctx; + struct demod_state *d = s->demod_target; + + if (do_exit) { + return;} + if (!ctx) { + return;} + if (s->mute) { + for (i=0; imute; i++) { + buf[i] = 127;} + s->mute = 0; + } + if (!s->offset_tuning) { + rotate_90(buf, len);} + for (i=0; i<(int)len; i++) { + s->buf16[i] = (int16_t)buf[i] - 127;} + pthread_rwlock_wrlock(&d->rw); + memcpy(d->lowpassed, s->buf16, 2*len); + d->lp_len = len; + pthread_rwlock_unlock(&d->rw); + safe_cond_signal(&d->ready, &d->ready_m); +} + +static void *dongle_thread_fn(void *arg) +{ + struct dongle_state *s = arg; + fprintf(stderr, "dongle_thread_fn running\n"); + rtlsdr_read_async(s->dev, rtlsdr_callback, s, 0, s->buf_len); + fprintf(stderr, "dongle_thread_fn exited!\n"); + return 0; +} + +static void rtl_fm_scan_callback(void) +{ + struct controller_state *s = &controller; + uint32_t frequency = rtl_fm_get_freq(); + + if(!s->scanning) + return; + + if(!s->scan_direction) { + frequency += s->scan_step; + if(frequency > s->scan_max) + frequency = s->scan_min; + } else { + frequency -= s->scan_step; + if(frequency < s->scan_min) + frequency = s->scan_max; + } + + rtl_fm_set_freq(frequency); +} + +static void rtl_fm_scan_end_callback(void) +{ + struct controller_state *s = &controller; + + if(!s->scanning) + return; + + rtl_fm_scan_stop(); + + if(s->scan_callback) + s->scan_callback(rtl_fm_get_freq(), s->scan_callback_data); +} + +static void *demod_thread_fn(void *arg) +{ + struct demod_state *d = arg; + struct output_state *o = d->output_target; + fprintf(stderr, "demod_thread_fn running\n"); + while (!do_exit) { + safe_cond_wait(&d->ready, &d->ready_m); + pthread_rwlock_wrlock(&d->rw); + full_demod(d); + pthread_rwlock_unlock(&d->rw); + if (d->exit_flag) { + do_exit = 1; + } + if (d->squelch_level) { + if(d->squelch_hits > d->conseq_squelch) { + d->squelch_hits = d->conseq_squelch + 1; /* hair trigger */ + //safe_cond_signal(&controller.hop, &controller.hop_m); + rtl_fm_scan_callback(); + continue; + } else if(!d->squelch_hits) { + rtl_fm_scan_end_callback(); + } + } + pthread_rwlock_wrlock(&o->rw); + memcpy(o->result, d->result, 2*d->result_len); + o->result_len = d->result_len; + pthread_rwlock_unlock(&o->rw); + safe_cond_signal(&o->ready, &o->ready_m); + } + fprintf(stderr, "demod_thread_fn exited!\n"); + return 0; +} + +static void *output_thread_fn(void *arg) +{ + struct output_state *s = arg; + fprintf(stderr, "output_thread_fn running\n"); + while (!do_exit) { + // use timedwait and pad out under runs + safe_cond_wait(&s->ready, &s->ready_m); + pthread_rwlock_rdlock(&s->rw); + if(s->output_fn) { + s->output_fn(s->result, s->result_len, s->output_fn_data); + } + pthread_rwlock_unlock(&s->rw); + } + fprintf(stderr, "output_thread_fn exited!\n"); + return 0; +} + +static void optimal_settings(int freq, int rate) +{ + // giant ball of hacks + // seems unable to do a single pass, 2:1 + int capture_freq, capture_rate; + struct dongle_state *d = &dongle; + struct demod_state *dm = &demod; + struct controller_state *cs = &controller; + dm->downsample = (1000000 / dm->rate_in) + 1; + if (dm->downsample_passes) { + dm->downsample_passes = (int)log2(dm->downsample) + 1; + dm->downsample = 1 << dm->downsample_passes; + } + capture_freq = freq; + capture_rate = dm->downsample * dm->rate_in; + if (!d->offset_tuning) { + capture_freq = freq + capture_rate/4;} + capture_freq += cs->edge * dm->rate_in / 2; + dm->output_scale = (1<<15) / (128 * dm->downsample); + if (dm->output_scale < 1) { + dm->output_scale = 1;} + if (dm->mode_demod == &fm_demod) { + dm->output_scale = 1;} + d->freq = (uint32_t)capture_freq; + d->rate = (uint32_t)capture_rate; +} + + +void frequency_range(struct controller_state *s, char *arg) +{ + char *start, *stop, *step; + int i; + start = arg; + stop = strchr(start, ':') + 1; + stop[-1] = '\0'; + step = strchr(stop, ':') + 1; + step[-1] = '\0'; + for(i=(int)atofs(start); i<=(int)atofs(stop); i+=(int)atofs(step)) + { + s->freqs[s->freq_len] = (uint32_t)i; + s->freq_len++; + if (s->freq_len >= FREQUENCIES_LIMIT) { + break;} + } + stop[-1] = ':'; + step[-1] = ':'; +} + +void dongle_init(struct dongle_state *s) +{ + s->rate = DEFAULT_SAMPLE_RATE; + s->gain = AUTO_GAIN; // tenths of a dB + s->mute = 0; + s->direct_sampling = 0; + s->offset_tuning = 0; + s->demod_target = &demod; +} + +void demod_init(struct demod_state *s) +{ + s->rate_in = DEFAULT_SAMPLE_RATE; + s->rate_out = DEFAULT_SAMPLE_RATE; + s->squelch_level = 0; + s->conseq_squelch = DEFAULT_CONSEQ_SQUELCH; + s->terminate_on_squelch = 0; + s->squelch_hits = DEFAULT_CONSEQ_SQUELCH + 1; + s->downsample_passes = 0; + s->comp_fir_size = 0; + s->prev_index = 0; + s->post_downsample = 1; // once this works, default = 4 + s->custom_atan = 0; + s->deemph = 0; + s->rate_out2 = -1; // flag for disabled + s->mode_demod = &fm_demod; + s->pre_j = s->pre_r = s->now_r = s->now_j = 0; + s->prev_lpr_index = 0; + s->deemph_a = 0; + s->now_lpr = 0; + s->dc_block = 0; + s->dc_avg = 0; + pthread_rwlock_init(&s->rw, NULL); + pthread_cond_init(&s->ready, NULL); + pthread_mutex_init(&s->ready_m, NULL); + s->output_target = &output; +} + +void demod_cleanup(struct demod_state *s) +{ + pthread_rwlock_destroy(&s->rw); + pthread_cond_destroy(&s->ready); + pthread_mutex_destroy(&s->ready_m); +} + +void output_init(struct output_state *s) +{ + s->rate = DEFAULT_SAMPLE_RATE; + s->output_fn = NULL; + s->output_fn_data = NULL; + pthread_rwlock_init(&s->rw, NULL); + pthread_cond_init(&s->ready, NULL); + pthread_mutex_init(&s->ready_m, NULL); +} + +void output_cleanup(struct output_state *s) +{ + pthread_rwlock_destroy(&s->rw); + pthread_cond_destroy(&s->ready); + pthread_mutex_destroy(&s->ready_m); +} + +void controller_init(struct controller_state *s) +{ + s->freqs[0] = 100000000; + s->freq_len = 0; + s->edge = 0; + s->wb_mode = 0; + pthread_cond_init(&s->hop, NULL); + pthread_mutex_init(&s->hop_m, NULL); +} + +void controller_cleanup(struct controller_state *s) +{ + pthread_cond_destroy(&s->hop); + pthread_mutex_destroy(&s->hop_m); +} + +void sanity_checks(void) +{ + if (controller.freq_len == 0) { + fprintf(stderr, "Please specify a frequency.\n"); + exit(1); + } + + if (controller.freq_len >= FREQUENCIES_LIMIT) { + fprintf(stderr, "Too many channels, maximum %i.\n", FREQUENCIES_LIMIT); + exit(1); + } + + if (controller.freq_len > 1 && demod.squelch_level == 0) { + fprintf(stderr, "Please specify a squelch level. Required for scanning multiple frequencies.\n"); + exit(1); + } + +} + +int rtl_fm_init(uint32_t freq, + uint32_t sample_rate, + uint32_t resample_rate, + rtl_fm_output_fn_t output_fn, + void *output_fn_data) +{ + int r = 0; + + dongle_init(&dongle); + demod_init(&demod); + output_init(&output); + controller_init(&controller); + + /* + * Simulate the effects of command line arguments: + * + * -W wbfm -s -r + */ + + /* Set initial frequency */ + controller.freqs[0] = freq; + controller.freq_len++; + + /* Set mode to wbfm */ + controller.wb_mode = 1; + demod.mode_demod = &fm_demod; + demod.rate_in = 170000; + demod.rate_out = 170000; + demod.rate_out2 = 32000; + demod.custom_atan = 1; + //demod.post_downsample = 4; + demod.deemph = 1; + controller.scan_squelch_count = DEFAULT_CONSEQ_SQUELCH; + controller.scan_squelch_level = DEFAULT_SQUELCH_LEVEL; + demod.squelch_level = 0; + + /* Adjust frequency for wb mode */ + controller.freqs[0] += 16000; + + /* Set sample rate */ + demod.rate_in = sample_rate; + demod.rate_out = sample_rate; + + /* Set resample rate */ + output.rate = (int) resample_rate; + demod.rate_out2 = (int) resample_rate; + + /* Set output function pointer */ + if(output_fn) { + output.output_fn = output_fn; + output.output_fn_data = output_fn_data; + } + + /* quadruple sample_rate to limit to Δθ to ±π/2 */ + demod.rate_in *= demod.post_downsample; + + if (!output.rate) { + output.rate = demod.rate_out; + } + + sanity_checks(); + + if (controller.freq_len > 1) { + demod.terminate_on_squelch = 0; + } + + ACTUAL_BUF_LENGTH = lcm_post[demod.post_downsample] * DEFAULT_BUF_LENGTH; + + dongle.dev_index = verbose_device_search("0"); + if (dongle.dev_index < 0) { + return -1; + } + + r = rtlsdr_open(&dongle.dev, (uint32_t)dongle.dev_index); + if (r < 0) { + fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dongle.dev_index); + return r; + } + + if (demod.deemph) { + demod.deemph_a = (int)round(1.0/((1.0-exp(-1.0/(demod.rate_out * 75e-6))))); + } + + /* Set the tuner gain */ + if (dongle.gain == AUTO_GAIN) { + verbose_auto_gain(dongle.dev); + } else { + dongle.gain = nearest_gain(dongle.dev, dongle.gain); + verbose_gain_set(dongle.dev, dongle.gain); + } + + verbose_ppm_set(dongle.dev, dongle.ppm_error); + + //r = rtlsdr_set_testmode(dongle.dev, 1); + + return r; +} + +void rtl_fm_start(void) +{ + struct controller_state *s = &controller; + + /* + * A bunch of the following is pulled from the controller_thread_fn, + * which has been removed. + */ + + /* Reset endpoint before we start reading from it (mandatory) */ + verbose_reset_buffer(dongle.dev); + + /* set up primary channel */ + optimal_settings(s->freqs[0], demod.rate_in); + if (dongle.direct_sampling) { + verbose_direct_sampling(dongle.dev, 1);} + if (dongle.offset_tuning) { + verbose_offset_tuning(dongle.dev);} + + /* Set the frequency */ + verbose_set_frequency(dongle.dev, dongle.freq); + fprintf(stderr, "Oversampling input by: %ix.\n", demod.downsample); + fprintf(stderr, "Oversampling output by: %ix.\n", demod.post_downsample); + fprintf(stderr, "Buffer size: %0.2fms\n", + 1000 * 0.5 * (float)ACTUAL_BUF_LENGTH / (float)dongle.rate); + + /* Set the sample rate */ + verbose_set_sample_rate(dongle.dev, dongle.rate); + fprintf(stderr, "Output at %u Hz.\n", demod.rate_in/demod.post_downsample); + usleep(100000); + + rtl_fm_scan_stop(); + + do_exit = 0; + pthread_create(&output.thread, NULL, output_thread_fn, (void *)(&output)); + pthread_create(&demod.thread, NULL, demod_thread_fn, (void *)(&demod)); + pthread_create(&dongle.thread, NULL, dongle_thread_fn, (void *)(&dongle)); +} + +void rtl_fm_set_freq(uint32_t freq) +{ + struct controller_state *s = &controller; + + if(s->freqs[0] == freq) + return; + + s->freqs[0] = freq; + s->freq_len = 1; + + if (s->wb_mode) { + s->freqs[0] += 16000; + } + + optimal_settings(s->freqs[0], demod.rate_in); + if (dongle.offset_tuning) { + verbose_offset_tuning(dongle.dev); + } + rtlsdr_set_center_freq(dongle.dev, dongle.freq); + + // It does not look like refreshing the sample rate is desirable + // (e.g. the scanning code in the removed controller thread function + // did not do it), and behavior seemed a bit less robust with it + // present. However, I am leaving this here as a reminder to revisit + // via some more testing. + //rtlsdr_set_sample_rate(dongle.dev, dongle.rate); + + // This triggers a mute during the frequency change + dongle.mute = BUFFER_DUMP; + + if(s->freq_callback) + s->freq_callback(freq, s->freq_callback_data); +} + +void rtl_fm_set_freq_callback(void (*callback)(uint32_t, void *), + void *data) +{ + struct controller_state *s = &controller; + + s->freq_callback = callback; + s->freq_callback_data = data; +} + +uint32_t rtl_fm_get_freq(void) +{ + struct controller_state *s = &controller; + uint32_t frequency = s->freqs[0]; + + if (s->wb_mode) + frequency -= 16000; + + return frequency; +} + +void rtl_fm_stop(void) +{ + rtl_fm_scan_stop(); + + rtlsdr_cancel_async(dongle.dev); + do_exit = 1; + pthread_join(dongle.thread, NULL); + safe_cond_signal(&demod.ready, &demod.ready_m); + pthread_join(demod.thread, NULL); + safe_cond_signal(&output.ready, &output.ready_m); + pthread_join(output.thread, NULL); +} + +void rtl_fm_scan_start(int direction, + void (*callback)(uint32_t, void *), + void *data, + uint32_t step, + uint32_t min, + uint32_t max) +{ + struct controller_state *s = &controller; + struct demod_state *dm = &demod; + uint32_t frequency = rtl_fm_get_freq(); + + if(s->scanning && s->scan_direction == direction) + return; + + s->scanning = 1; + s->scan_direction = direction; + s->scan_callback = callback; + s->scan_callback_data = data; + s->scan_step = step; + s->scan_min = min; + s->scan_max = max; + + /* Start scan by stepping in the desired direction */ + if(!direction) { + frequency += s->scan_step; + if(frequency > s->scan_max) + frequency = s->scan_min; + } else { + frequency -= s->scan_step; + if(frequency < s->scan_min) + frequency = s->scan_max; + } + + rtl_fm_set_freq(frequency); + + dm->conseq_squelch = s->scan_squelch_count; + dm->squelch_hits = s->scan_squelch_count + 1; + dm->squelch_level = s->scan_squelch_level; +} + +void rtl_fm_scan_stop(void) +{ + struct controller_state *s = &controller; + struct demod_state *dm = &demod; + + s->scanning = 0; + + dm->squelch_hits = s->scan_squelch_count + 1; + dm->squelch_level = 0; +} + +void rtl_fm_scan_set_squelch_level(int level) +{ + struct controller_state *s = &controller; + + s->scan_squelch_level = level; +} + +void rtl_fm_scan_set_squelch_limit(int count) +{ + struct controller_state *s = &controller; + + s->scan_squelch_count = count; +} + +void rtl_fm_cleanup(void) +{ + //dongle_cleanup(&dongle); + demod_cleanup(&demod); + output_cleanup(&output); + controller_cleanup(&controller); + + rtlsdr_close(dongle.dev); +} + +// vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab diff --git a/binding/rtl_fm.h b/binding/rtl_fm.h new file mode 100644 index 0000000..f5b2a86 --- /dev/null +++ b/binding/rtl_fm.h @@ -0,0 +1,70 @@ +/* + * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver + * Copyright (C) 2016, 2017 Konsulko Group + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef RTL_FM_H +#define RTL_FM_H + +#include + +#define RTL_FM_DEFAULT_BUF_LENGTH (1 * 16384) +#define RTL_FM_MAXIMUM_OVERSAMPLE 16 +#define RTL_FM_MAXIMUM_BUF_LENGTH (RTL_FM_MAXIMUM_OVERSAMPLE * RTL_FM_DEFAULT_BUF_LENGTH) + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (*rtl_fm_output_fn_t)(int16_t *result, int result_len, void *data); + +int rtl_fm_init(uint32_t freq, + uint32_t sample_rate, + uint32_t resample_rate, + rtl_fm_output_fn_t output_fn, + void *output_fn_data); + +void rtl_fm_start(void); + +void rtl_fm_set_freq(uint32_t freq); + +void rtl_fm_set_freq_callback(void (*callback)(uint32_t, void *), + void *data); + +uint32_t rtl_fm_get_freq(void); + +void rtl_fm_stop(void); + +void rtl_fm_scan_start(int direction, + void (*callback)(uint32_t, void *), + void *data, + uint32_t step, + uint32_t min, + uint32_t max); + +void rtl_fm_scan_stop(void); + +void rtl_fm_scan_set_squelch_level(int level); + +void rtl_fm_scan_set_squelch_limit(int count); + +void rtl_fm_cleanup(void); + +#ifdef __cplusplus +} +#endif + +#endif /* RTL_FM_H */ diff --git a/package/config.xml b/package/config.xml index a96d0b7..158c5c7 100644 --- a/package/config.xml +++ b/package/config.xml @@ -2,10 +2,16 @@ Radio - + This is a demo Radio application Qt APL 2.0 + + + + + + diff --git a/radio.pro b/radio.pro index 579a952..80f6d6c 100644 --- a/radio.pro +++ b/radio.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs -SUBDIRS = app package -package.depends += app +SUBDIRS = app binding package +package.depends += app binding -- cgit 1.2.3-korg