Skip to content

Commit

Permalink
UDP datagram server running, some global objects changed to static /m…
Browse files Browse the repository at this point in the history
…essenger,logger/, server mode (UTP/TCP) menu switch added
  • Loading branch information
Paku- committed Jan 27, 2014
1 parent 277f01e commit 3332b61
Show file tree
Hide file tree
Showing 26 changed files with 312 additions and 254 deletions.
12 changes: 9 additions & 3 deletions MavLinkHUB/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_select_device_bluetooth"
android:icon="@drawable/ic_action_bluetooth_searching"
Expand All @@ -11,7 +10,14 @@
android:icon="@drawable/ic_action_usb"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/menu_title_usb"/>
android:title="@string/menu_title_usb"/>

<item
android:id="@+id/menu_switch_server_mode"
android:icon="@drawable/ic_action_import_export"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/menu_title_switch_server"/>

<item
android:id="@+id/menu_settings"
Expand Down
8 changes: 6 additions & 2 deletions MavLinkHUB/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<!-- Menu Section -->
<string name="menu_title_settings">Settings</string>
<string name="menu_title_bluetooth">Connect using Bluetooth</string>
<string name="menu_title_usb">Connect USB client</string>
<string name="menu_title_switch_server">Switch server mode (UDP/TCP)</string>
<!-- Fragments Titles Section -->
<string name="title_realtime_mavlink">RealTime MAVLink</string>
<string name="title_connectivity">Connection</string>
Expand Down Expand Up @@ -68,7 +70,9 @@
<string name="txt_device_connected">Device connected</string>
<string name="txt_device_disconnected">Device disconnected</string>
<string name="txt_device_connecting">Connecting device</string>
<string name="menu_title_usb">Connect USB client</string>
<string name="txt_select_device_to_connect_disconnect">Select device to connect/disconnect</string>
<string name="error_server_start_failed">Can not start server !</string>

<!-- not assigned strings needs moving up -->
<string name="error_server_start_failed">Can not start server !</string>

</resources>
16 changes: 11 additions & 5 deletions MavLinkHUB/src/com/paku/mavlinkhub/HUBActivityMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void onResume() {
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

// register for call interface;
hub.messenger.mainActivity = this;
HUBGlobals.messenger.mainActivity = this;

progressBarConnected.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
onDataUpdateStats();
Expand All @@ -83,7 +83,7 @@ public void onPause() {
super.onPause();

// unregister from call interface;
hub.messenger.mainActivity = null;
HUBGlobals.messenger.mainActivity = null;
}

@Override
Expand Down Expand Up @@ -144,6 +144,12 @@ public boolean onOptionsItemSelected(MenuItem item) {

return true;

case R.id.menu_switch_server_mode:

hub.switchServer();

return true;

default:
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -199,11 +205,11 @@ protected void onStop() {
}

private void closeHUB() {
hub.logger.sysLog(TAG, "MavLinkHUB closing ...");
HUBGlobals.logger.sysLog(TAG, "MavLinkHUB closing ...");
hub.droneClient.stopClient();
hub.gsServer.stopServer();
hub.queue.stopQueue();
hub.logger.stopAllLogs();
HUBGlobals.logger.stopAllLogs();
finish();
killApp(true);
}
Expand All @@ -222,7 +228,7 @@ public void enableProgressBar(boolean on) {
public void onDataUpdateStats() {

final TextView mTextViewLogStats = (TextView) findViewById(R.id.textView_system_status_bar);
mTextViewLogStats.setText(hub.logger.hubStats.toString_(MSG_SOURCE.FROM_ALL));
mTextViewLogStats.setText(HUBGlobals.logger.hubStats.toString_(MSG_SOURCE.FROM_ALL));

}

Expand Down
92 changes: 75 additions & 17 deletions MavLinkHUB/src/com/paku/mavlinkhub/HUBGlobals.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.paku.mavlinkhub;

import com.ftdi.j2xx.D2xxManager;
import com.paku.mavlinkhub.enums.APP_STATE;
import com.paku.mavlinkhub.enums.DEVICE_INTERFACE;
import com.paku.mavlinkhub.enums.UI_MODE;
import com.paku.mavlinkhub.fragments.FragmentsAdapter;
Expand All @@ -12,15 +13,18 @@
import com.paku.mavlinkhub.queue.endpoints.gs.GroundStationServerTCP;
import com.paku.mavlinkhub.queue.endpoints.gs.GroundStationServerUDP;
import com.paku.mavlinkhub.queue.hub.HUBQueue;
import com.paku.mavlinkhub.queue.items.ItemMavLinkMsg;
import com.paku.mavlinkhub.utils.HUBLogger;
import com.paku.mavlinkhub.viewadapters.devicelist.ItemPeerDevice;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;

public class HUBGlobals extends Application {

Expand All @@ -35,7 +39,7 @@ public class HUBGlobals extends Application {
public static final int serverUDP_port = 14550;

// messages handler
public HUBMessenger messenger;
public static HUBMessenger messenger;

// main Drone connector
public DroneClient droneClient;
Expand All @@ -47,7 +51,7 @@ public class HUBGlobals extends Application {
public HUBQueue queue;

// sys log stats holder object
public HUBLogger logger;
public static HUBLogger logger;

public static D2xxManager usbHub = null;

Expand All @@ -69,42 +73,79 @@ public void hubInit(Context mContext) {
// start application asynchronous messaging - has to be first !!!
messenger = new HUBMessenger(this);

//start system wide logger
logger = new HUBLogger(this);

/*
<usb-device vendor-id="1027" product-id="24577" /> <!-- FT232RL -->
<usb-device vendor-id="1027" product-id="24596" /> <!-- FT232H -->
<usb-device vendor-id="1027" product-id="24597" /> <!-- FT231X 0x0403 / 0x6015 -->
<usb-device vendor-id="1027" product-id="24592" /> <!-- FT2232C/D/HL -->
<usb-device vendor-id="1027" product-id="24593" /> <!-- FT4232HL -->
<usb-device vendor-id="1027" product-id="24597" /> <!-- FT230X -->
<usb-device vendor-id="1412" product-id="45088" /> <!-- REX-USB60F -->
<usb-device vendor-id="9025" product-id="16" /><!-- APM 2.5 device -->
<usb-device vendor-id="9900" product-id="16" /><!-- APM 2.5 device -->
<usb-device vendor-id="5824" product-id="1155" /><!-- Teensyduino -->
<usb-device vendor-id="4292" product-id="60000" /><!-- CP210x UART Bridge -->
<usb-device vendor-id="1118" product-id="688"/>
*/

//start USB driver
try {
usbHub = D2xxManager.getInstance(this);
// setup the additinal VIDPIDPAIRs
if (!usbHub.setVIDPID(0x2341, 0x0010)) Log.d(TAG, "APM 2.5 VIDPID1 setting error");
if (!usbHub.setVIDPID(0x26ac, 0x0010)) Log.d(TAG, "APM 2.5 VIDPID2 setting error");

}
catch (D2xxManager.D2xxException ex) {
ex.printStackTrace();
}

// setup the additinal VIDPIDPAIR
if (!usbHub.setVIDPID(0x0403, 0xada1)) Log.i("ftd2xx-java", "setVIDPID Error");

// Default is the BT client
droneClient = new DroneClientBluetooth(this);

//TCP server
// server started from the beginning
gsServer = new GroundStationServerTCP(this);
// start listening on configured port.
gsServer.startServer(HUBGlobals.serverTCP_port);

//UDP server
// server started from the beginning
gsServer = new GroundStationServerUDP(this);
// start listening on configured port.
gsServer.startServer(HUBGlobals.serverUDP_port);
// switch server, if it's null start UDP as default.
switchServer();

// finally start parsers and distributors
queue = new HUBQueue(this, 1000);
queue.startQueue();

}

public void switchServer() {

if (gsServer != null) {
gsServer.stopServer();

if (gsServer.getClass().equals(GroundStationServerTCP.class)) {
gsServer = new GroundStationServerUDP(this);
gsServer.startServer(HUBGlobals.serverUDP_port);
return;
}

if (gsServer.getClass().equals(GroundStationServerUDP.class)) {
gsServer = new GroundStationServerTCP(this);
gsServer.startServer(HUBGlobals.serverTCP_port);
return;
}
}
else {

gsServer = new GroundStationServerUDP(this);
gsServer.startServer(HUBGlobals.serverUDP_port);

}

Toast.makeText(this, "Server mode set to: " + gsServer.serverMode.toString(), Toast.LENGTH_LONG).show();

}

public void switchClient(ItemPeerDevice newDevice) {

if (null != droneClient) {
if (droneClient != null) {
droneClient.stopClient();
}

Expand All @@ -126,4 +167,21 @@ public void switchClient(ItemPeerDevice newDevice) {

}

//let's have the app wide STATIC messaging method for our children/application classes
public static final void sendAppMsg(APP_STATE msgId, String msgTxt) {
messenger.appMsgHandler.obtainMessage(msgId.ordinal(), msgTxt.length(), -1, msgTxt.getBytes()).sendToTarget();
}

public static final void sendAppMsg(APP_STATE msgId) {
messenger.appMsgHandler.obtainMessage(msgId.ordinal()).sendToTarget();
}

public static final void sendAppMsg(APP_STATE msgId, Message msg) {
messenger.appMsgHandler.obtainMessage(msgId.ordinal(), msg.arg1, msg.arg2, msg.obj).sendToTarget();
}

public static void sendAppMsg(APP_STATE msgId, ItemMavLinkMsg item) {
messenger.appMsgHandler.obtainMessage(msgId.ordinal(), -1, -1, item).sendToTarget();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.paku.mavlinkhub.fragments;

import com.paku.mavlinkhub.HUBActivityMain;
import com.paku.mavlinkhub.HUBGlobals;
import com.paku.mavlinkhub.R;
import com.paku.mavlinkhub.enums.APP_STATE;
import com.paku.mavlinkhub.interfaces.IDroneConnected;
Expand Down Expand Up @@ -33,10 +34,10 @@ public void onStart() {
@Override
public void onResume() {
super.onResume();
hub.messenger.register(this, APP_STATE.MSG_UI_MODE_CHANGED);
hub.messenger.register(this, APP_STATE.MSG_DRONE_CONNECTION_ATTEMPT_FAILED);
hub.messenger.register(this, APP_STATE.MSG_DRONE_CONNECTED);
hub.messenger.register(this, APP_STATE.MSG_DRONE_CONNECTION_LOST);
HUBGlobals.messenger.register(this, APP_STATE.MSG_UI_MODE_CHANGED);
HUBGlobals.messenger.register(this, APP_STATE.MSG_DRONE_CONNECTION_ATTEMPT_FAILED);
HUBGlobals.messenger.register(this, APP_STATE.MSG_DRONE_CONNECTED);
HUBGlobals.messenger.register(this, APP_STATE.MSG_DRONE_CONNECTION_LOST);
onUiModeChanged();

}
Expand All @@ -45,10 +46,10 @@ public void onResume() {
public void onPause() {
super.onPause();

hub.messenger.unregister(this, APP_STATE.MSG_UI_MODE_CHANGED);
hub.messenger.unregister(this, APP_STATE.MSG_DRONE_CONNECTION_ATTEMPT_FAILED);
hub.messenger.unregister(this, APP_STATE.MSG_DRONE_CONNECTED);
hub.messenger.unregister(this, APP_STATE.MSG_DRONE_CONNECTION_LOST);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_UI_MODE_CHANGED);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_DRONE_CONNECTION_ATTEMPT_FAILED);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_DRONE_CONNECTED);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_DRONE_CONNECTION_LOST);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
public void onResume() {
super.onResume();

hub.messenger.register(this, APP_STATE.MSG_DATA_UPDATE_BYTELOG);
hub.messenger.register(this, APP_STATE.MSG_QUEUE_MSGITEM_READY);
HUBGlobals.messenger.register(this, APP_STATE.MSG_DATA_UPDATE_BYTELOG);
HUBGlobals.messenger.register(this, APP_STATE.MSG_QUEUE_MSGITEM_READY);

// GUI update
onDataUpdateByteLog();
Expand Down Expand Up @@ -81,16 +81,16 @@ public void onItemClick(AdapterView<?> adapter, View view, int position, long ar
@Override
public void onPause() {
super.onPause();
hub.messenger.unregister(this, APP_STATE.MSG_DATA_UPDATE_BYTELOG);
hub.messenger.unregister(this, APP_STATE.MSG_QUEUE_MSGITEM_READY);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_DATA_UPDATE_BYTELOG);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_QUEUE_MSGITEM_READY);
}

@Override
public void onDataUpdateByteLog() {

final TextView mTextViewBytesLog = (TextView) (getView().findViewById(R.id.textView_logByte));

mTextViewBytesLog.setText(hub.logger.getByteLog());
mTextViewBytesLog.setText(HUBGlobals.logger.getByteLog());

if (hub.prefs.getBoolean("pref_byte_log_autoscroll", true)) {
// scroll down
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.paku.mavlinkhub.fragments;

import com.paku.mavlinkhub.HUBGlobals;
import com.paku.mavlinkhub.R;
import com.paku.mavlinkhub.enums.APP_STATE;
import com.paku.mavlinkhub.interfaces.IDataUpdateSysLog;
Expand Down Expand Up @@ -33,21 +34,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onResume() {
super.onResume();
hub.messenger.register(this, APP_STATE.MSG_DATA_UPDATE_SYSLOG);
HUBGlobals.messenger.register(this, APP_STATE.MSG_DATA_UPDATE_SYSLOG);
onDataUpdateSysLog();
}

@Override
public void onPause() {
super.onPause();
hub.messenger.unregister(this, APP_STATE.MSG_DATA_UPDATE_SYSLOG);
HUBGlobals.messenger.unregister(this, APP_STATE.MSG_DATA_UPDATE_SYSLOG);
}

@Override
public void onDataUpdateSysLog() {
final TextView mTextViewBytesLog = (TextView) (getView().findViewById(R.id.TextView_logSysLog));

mTextViewBytesLog.setText(hub.logger.inMemSysLogBuffer);
mTextViewBytesLog.setText(HUBGlobals.logger.inMemSysLogBuffer);

// scroll down
final ScrollView mScrollView = (ScrollView) (getView().findViewById(R.id.scrollView_logSys));
Expand Down
Loading

0 comments on commit 3332b61

Please sign in to comment.