Skip to content

Commit 0019885

Browse files
committed
add TCP server for NMEA
1 parent 3361dbc commit 0019885

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

app/src/main/java/com/jvdegithub/aiscatcher/AisCatcherJava.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public interface AisCallback {
5555

5656
static native int createUDP(String h, String p, boolean JSON);
5757

58+
static native int createTCPlistener(String p);
5859
static native int createWebViewer(String p);
5960

6061
static native int createSharing(boolean b, String key);

app/src/main/java/com/jvdegithub/aiscatcher/Settings.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ static void setDefault(Context context) {
107107
preferences.edit().putString("u4PORT", "10111").commit();
108108
preferences.edit().putBoolean("u4JSON", false).commit();
109109

110+
preferences.edit().putBoolean("s1SWITCH", false).commit();
111+
preferences.edit().putString("s1PORT", "5012").commit();
112+
110113
preferences.edit().putInt("mLINEARITY", 17).commit();
111114
preferences.edit().putString("mRATE", "2500K").commit();
112115
preferences.edit().putBoolean("mBIASTEE", false).commit();
@@ -144,6 +147,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
144147
((EditTextPreference) getPreferenceManager().findPreference("u2PORT")).setOnBindEditTextListener(validatePort);
145148
((EditTextPreference) getPreferenceManager().findPreference("u3PORT")).setOnBindEditTextListener(validatePort);
146149
((EditTextPreference) getPreferenceManager().findPreference("u4PORT")).setOnBindEditTextListener(validatePort);
150+
((EditTextPreference) getPreferenceManager().findPreference("s1PORT")).setOnBindEditTextListener(validatePort);
147151
((SeekBarPreference) getPreferenceManager().findPreference("mLINEARITY")).setUpdatesContinuously(true);
148152

149153
setSummaries();
@@ -156,7 +160,7 @@ static public int getModelType(Context context)
156160
}
157161

158162
private void setSummaries() {
159-
setSummaryText(new String[]{"w1PORT","tPORT","tHOST","sPORT","sHOST","u1HOST","u1PORT","u2HOST","u2PORT", "u3HOST","u3PORT", "u4HOST","u4PORT", "rFREQOFFSET", "sSHARINGKEY"});
163+
setSummaryText(new String[]{"w1PORT","tPORT","tHOST","sPORT","sHOST","u1HOST","u1PORT","u2HOST","u2PORT", "u3HOST","u3PORT", "u4HOST","u4PORT", "s1PORT", "rFREQOFFSET", "sSHARINGKEY"});
160164
setSummaryList(new String[]{"rTUNER","rRATE","sRATE","tRATE","tPROTOCOL","tTUNER","mRATE","hRATE","oMODEL_TYPE","oCGF_WIDE"});
161165
setSummarySeekbar(new String[]{"mLINEARITY", "sGAIN"});
162166
}
@@ -278,6 +282,8 @@ static public boolean Apply(Context context) {
278282
if (!SetUDPoutput("u3", context)) return false;
279283
if (!SetUDPoutput("u4", context)) return false;
280284

285+
if (!SetTCPListener(context)) return false;
286+
281287
if (!SetWebViewerOutput( context)) return false;
282288

283289
if(!SetSharing(context)) return false;
@@ -386,6 +392,19 @@ static private boolean SetUDPoutput(String s, Context context) {
386392
return true;
387393
}
388394

395+
static private boolean SetTCPListener(Context context) {
396+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
397+
398+
boolean b = preferences.getBoolean("s1SWITCH", true);
399+
if (b) {
400+
String port = preferences.getString("s1PORT", "");
401+
402+
return AisCatcherJava.createTCPlistener( port) == 0;
403+
404+
}
405+
return true;
406+
}
407+
389408
static private boolean SetWebViewerOutput(Context context) {
390409
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
391410

app/src/main/jni/JNI/AIScatcherNDK.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct Statistics {
6868

6969
WebViewer server;
7070
static std::unique_ptr<WebViewer> webviewer = nullptr;
71+
static std::unique_ptr<IO::TCPlistenerStreamer> TCP_listener = nullptr;
7172
int webviewer_port = -1;
7273

7374
std::string nmea_msg;
@@ -250,6 +251,7 @@ std::vector<IO::TCPClientStreamer > TCP_connections;
250251
std::vector<std::string> UDPhost;
251252
std::vector<std::string> UDPport;
252253
std::vector<bool> UDPJSON;
254+
std::string TCP_listener_port;
253255

254256
bool sharing = false;
255257
std::string sharingKey = "";
@@ -360,21 +362,35 @@ Java_com_jvdegithub_aiscatcher_AisCatcherJava_Run(JNIEnv *env, jclass) {
360362
model->Output() >> UDP_connections[i];
361363
}
362364

363-
if(sharing) {
364-
Info() << "Creating TCP output channels";
365-
TCP_connections.resize(1);
365+
if(!TCP_listener_port.empty()) {
366+
TCP_listener = std::make_unique<IO::TCPlistenerStreamer>();
367+
Info() << "Creating TCP listener at port " << TCP_listener_port;
368+
TCP_listener->Set("PORT", TCP_listener_port);
369+
TCP_listener->Set("TIMEOUT","0");
370+
TCP_listener->Set("JSON","false");
371+
model->Output() >> (*TCP_listener);
372+
}
366373

367-
TCP_connections[0].Set("HOST", "aiscatcher.org").Set("PORT", "4242").Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off");
368-
TCP_connections[0].Set("UUID", sharingKey);
369-
TCP_connections[0].Start();
370-
model->Output() >> TCP_connections[0];
374+
if(sharing) {
375+
Info() << "Creating Sharing output channel";
376+
int sharing_index = TCP_connections.size();
377+
TCP_connections.resize(sharing_index + 1);
378+
379+
TCP_connections[sharing_index].Set("HOST", "aiscatcher.org").Set("PORT", "4242").Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off");
380+
TCP_connections[sharing_index].Set("UUID", sharingKey);
381+
TCP_connections[sharing_index].Start();
382+
model->Output() >> TCP_connections[sharing_index];
371383
}
372384

373385
if(webviewer) {
374386
Info() << "Starting Web Viewer";
375387
webviewer->start();
376388
}
377389

390+
if(TCP_listener) {
391+
Info() << "Starting TCP listener";
392+
TCP_listener->Start();
393+
}
378394
Info() << "Start Device";
379395
device->setTag(tag);
380396
device->Play();
@@ -409,12 +425,16 @@ Java_com_jvdegithub_aiscatcher_AisCatcherJava_Run(JNIEnv *env, jclass) {
409425

410426
for (auto &u: UDP_connections) u.Stop();
411427
for (auto &t: TCP_connections) t.Stop();
428+
429+
if(TCP_listener) TCP_listener->Stop();
430+
412431
UDP_connections.clear();
413432
TCP_connections.clear();
414433

415434
UDPport.clear();
416435
UDPhost.clear();
417436
UDPJSON.clear();
437+
TCP_listener = nullptr;
418438

419439
if(webviewer) {
420440
webviewer->close();
@@ -708,3 +728,13 @@ Java_com_jvdegithub_aiscatcher_AisCatcherJava_getRateDescription(JNIEnv *env, jc
708728
return env->NewStringUTF(device->getRateDescription().c_str());
709729
}
710730

731+
732+
extern "C"
733+
JNIEXPORT jint JNICALL
734+
Java_com_jvdegithub_aiscatcher_AisCatcherJava_createTCPlistener(JNIEnv *env, jclass clazz,
735+
jstring p) {
736+
std::string port = toString(env, p);
737+
TCP_listener_port = port;
738+
Info() << "TCP Listener: " << port ;
739+
return 0;
740+
}

app/src/main/res/xml/preferences.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228

229229
<PreferenceCategory
230230
android:key="output_category"
231-
android:title="Output">
231+
android:title="Output UDP">
232232

233233
<SwitchPreferenceCompat
234234
android:key="u1SWITCH"
@@ -326,6 +326,23 @@
326326

327327
</PreferenceCategory>
328328

329+
<PreferenceCategory>
330+
android:key="output_tcp_server_category"
331+
android:title="Output TCP NMEA Server">
332+
333+
<SwitchPreferenceCompat
334+
android:key="s1SWITCH"
335+
android:defaultValue="False"
336+
android:title="TCP NMEA Server"/>
337+
338+
<EditTextPreference
339+
android:key="s1PORT"
340+
android:dependency="s1SWITCH"
341+
android:defaultValue="5012"
342+
android:title="Port"/>
343+
344+
</PreferenceCategory>
345+
329346
<PreferenceCategory
330347
android:key="model_category"
331348
android:title="Receiver">

0 commit comments

Comments
 (0)