Skip to content

Commit

Permalink
Smoke Detector profile for COAP.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Jul 28, 2020
1 parent 141bc43 commit e384ca5
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 18 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Expand Up @@ -41,12 +41,15 @@ dependencies {
//hive-Mq
implementation group: 'com.hivemq', name: 'hivemq-mqtt-client', version: '1.2.0'
//COAP
//implementation 'com.mbed.java-coap:coap-core:5.0.0'
//implementation 'com.mbed.java-coap:coap-core:5.0.0' //original package
implementation files('libs/coap-core-5.1.0-SNAPSHOT.jar')

//moquette
implementation 'io.moquette:moquette-broker:0.12.1'

//SU Library for root
implementation 'eu.chainfire:libsuperuser:1.0.0.201704021214'

//androidX legacy support
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.preference:preference:1.1.1'
Expand All @@ -60,8 +63,7 @@ dependencies {
implementation 'org.snmp4j:snmp4j:1.10.1'
implementation files('libs/snmp4j-agent-2.3.3.jar')
implementation files('libs/VirusTotalAPI.jar')
//MQTT Broker Cassandana
//implementation files('libs/cassandana-jar-with-dependencies.jar')

//materialDesign
implementation 'com.google.android.material:material:1.3.0-alpha01'

Expand Down
Expand Up @@ -525,6 +525,7 @@ public void fillWithDefaultData() throws Exception {
addParanoidProfile();
addMQTTBrokerProfile();
addMQTTSensorProfile();
addSmokeSensorProfile();

persistData();
}
Expand Down Expand Up @@ -796,4 +797,17 @@ private void addMQTTSensorProfile(){
this.addProfile(mqttSensor,false);
}

private void addSmokeSensorProfile(){
Profile smokeSensor = new Profile(
15,
"ESP8266 Smoke Sensor",
"This profile simulates an ESP8266 smoke detector",
R.drawable.ic_profile_smoke_detector,
false
);

smokeSensor.mActiveProtocols.put("COAP",true);
this.addProfile(smokeSensor,false);
}

}
43 changes: 33 additions & 10 deletions src/main/java/de/tudarmstadt/informatik/hostage/protocol/COAP.java
Expand Up @@ -10,11 +10,16 @@
import java.net.UnknownHostException;
import java.util.List;

import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
import de.tudarmstadt.informatik.hostage.protocol.coapUtils.COAPHandler;
import de.tudarmstadt.informatik.hostage.protocol.coapUtils.smokeSensor.SmokeSensorProfile;
import de.tudarmstadt.informatik.hostage.wrapper.Packet;

public class COAP implements Protocol {
private final static String defaultAddress="192.168.1.5";
private final static String defaultAddress="192.168.1.5";//change accordingly.
private final static int defaultPort = 5683;
private int port=5683;
private static boolean serverStarted = false; //prevents the server from starting multiple times from the threads
private static InetAddress address=null;
static {
try {
Expand All @@ -23,15 +28,22 @@ public class COAP implements Protocol {
e.printStackTrace();
}
}

private final static int defaultPort = 5683;
private int port=5683;
private static boolean serverStarted = false; //prevents the server from starting multiple times from the threads
private final static CoapServer server = CoapServer.builder().transport(address,defaultPort).build();


public COAP() throws IOException {
public COAP() throws Exception {
if(!serverStarted)
startServerMode();
}

private boolean enabledProfile() throws Exception {
return ProfileManager.getInstance().getCurrentActivatedProfile().mId == 15;
}

private void startServerMode() throws Exception {
SmokeSensorProfile profile = new SmokeSensorProfile();
if(enabledProfile())
startServerProfile(profile.getTemperature(),profile.getAbnormality());
else
startServer();
}

Expand Down Expand Up @@ -75,7 +87,6 @@ public boolean isClosed() {
public boolean isSecure() {
return false;
}

/**
* Determines the next response.
*
Expand Down Expand Up @@ -120,6 +131,19 @@ public CoapServer startServer() throws IOException {
return server;
}

/**
* Starts the CoAP server for a Profile.
* @throws IOException
*/
public CoapServer startServerProfile(String temperature,String abnormality) throws IOException {
server.addRequestHandler("/temp",new COAPHandler(temperature));
server.addRequestHandler("/abnormality",new COAPHandler(abnormality));
server.start();
serverStarted=true;

return server;
}

/**
* Returns a simple CoAP client.
* @param serverAddress the server address with the port.
Expand All @@ -132,9 +156,8 @@ public CoapClient getSimpleClient(InetSocketAddress serverAddress, CoapServer se
}

/**
* Stops the broker and closes the port
* Stops the server and closes the port
*/

public static void serverStop(){
server.stop();
}
Expand Down
@@ -1,6 +1,7 @@
package de.tudarmstadt.informatik.hostage.protocol;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.DhcpInfo;
Expand Down Expand Up @@ -66,6 +67,7 @@ public Listener getListener(){
return mListener;
}

@SuppressLint("ResourceType")
public void initialize(Listener mListener) {
this.mListener = mListener;
FileInject fileInject = new FileInject();
Expand All @@ -87,7 +89,7 @@ public void initialize(Listener mListener) {
XMLServerConfiguration smbConfig = new XMLServerConfiguration();

try {
smbConfig.loadConfiguration(new InputStreamReader(MainActivity.getContext().getResources().openRawResource(R.raw.jlan_config)));
smbConfig.loadConfiguration(new InputStreamReader(MainActivity.getContext().getResources().openRawResource(R.xml.jlan_config)));
mCifsServer = new CifsServer(smbConfig, this, fileInject);
mCifsServer.run();
} catch (IOException e) {
Expand Down
Expand Up @@ -24,14 +24,24 @@
public class COAPHandler extends CoapResource {
private static ArrayList<CoapPacket> requests = new ArrayList<>();
private static final ArrayList<CoapPacket> fullRequests = new ArrayList<>();
private String value="Response";


public COAPHandler(){

}

public COAPHandler(String value){
this.value=value;
}


@Override
public void get(CoapExchange exchange) {
requests.add(exchange.getRequest());
fullRequests.add(exchange.getRequest());
exchange.setResponseCode(Code.C205_CONTENT);
exchange.setResponseBody("Response");
exchange.setResponseBody(value);
exchange.sendResponse();
}

Expand Down
@@ -0,0 +1,21 @@
package de.tudarmstadt.informatik.hostage.protocol.coapUtils.smokeSensor;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class SmokeSensorProfile {
private final String temperature = ThreadLocalRandom.current().nextDouble(25, 35 + 1)+"°C";
private final String[] values = {"High", "Low", "Medium", "Critical"};
private final Random random = new Random();
// randomly selects an index from the arr
private final int select = random.nextInt(values.length);
private final String abnormality = values[select];

public String getTemperature() {
return temperature;
}

public String getAbnormality() {
return abnormality;
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions src/main/res/values/strings.xml
Expand Up @@ -78,8 +78,11 @@
<string name="close">Close</string>
<string name="show_records">Show records</string>
<string name="dialog_clear_database">Clear all data?</string>
<string name="export_dialog_title">Choose Export Format</string>
<string name="delete_dialog_title">Delete data sets by:</string>
<string name="export_dialog_title">Choose Export Format</string>
<string name="error_su" translatable="false">Could not acquire root access.\nYour device must be rooted to use default prots.</string>
<string name="applying_rules" translatable="false">Applying iptables rules</string>
<string name="toast_bin_installed" translatable="false">Binary Iptables files installed!</string>
<string name="delete_dialog_title">Delete data sets by:</string>
<string name="dialog_clear_database_date">Delete all data before:</string>

<string name="deleteFILTEREDAttacksTitle">Delete the filtered attack data?</string>
Expand Down
File renamed without changes.
Expand Up @@ -279,7 +279,6 @@ public void put(CoapExchange exchange) {
exchange.sendResponse();
}
});

cnn.start();

CoapPacket request = new CoapPacket(InMemoryCoapTransport.createAddress(5683));
Expand All @@ -296,6 +295,45 @@ public void put(CoapExchange exchange) {
cnn.stop();
}

@Test
public void testRequestHandlers() throws Exception {
CoapServer cnn = CoapServerBuilder.newBuilder()
.transport(new InMemoryCoapTransport(5683)).build();
cnn.addRequestHandler("/*", new CoapResource() {
@Override
public void get(CoapExchange exchange) {
requests.add(exchange.getRequest());
exchange.setResponseCode(Code.C405_METHOD_NOT_ALLOWED);
exchange.setResponseBody("Dziala");
exchange.sendResponse();
}

@Override
public void put(CoapExchange exchange) {
exchange.setResponseCode(Code.C204_CHANGED);
exchange.setResponseBody(Integer.valueOf(exchange.getRequestBody().length).toString());
exchange.sendResponse();
}
});
cnn.addRequestHandler("/temp", new ReadOnlyCoapResource("50"));


cnn.start();

CoapPacket request = new CoapPacket(InMemoryCoapTransport.createAddress(5683));
request.setMethod(Method.GET);
request.headers().setUriPath("/temp");
request.setPayload("testing");
request.setMessageId(1647);

FutureCallbackAdapter<CoapPacket> callback = new FutureCallbackAdapter<>();
cnn.makeRequest(request, callback);
assertEquals("50", callback.get().getPayloadString());
//assertEquals(1, requests.size());
//assertEquals("testing",requests.get(0).getPayloadString());
cnn.stop();
}

@Test
public void testRequestWithPacketDelay() throws Exception {
CoapServer serverNode = CoapServerBuilder.newBuilder().transport(InMemoryCoapTransport.create(5683)).build();
Expand Down

0 comments on commit e384ca5

Please sign in to comment.