Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions HughTheLightbulbV2/Device/cheriot.demo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Trust anchor for self-signed cert on cheriot.demo (non-standard TLD) */

static const unsigned char TA0_DN[] = {
0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
0x0C, 0x63, 0x68, 0x65, 0x72, 0x69, 0x6F, 0x74, 0x2E, 0x64, 0x65, 0x6D,
0x6F
};

static const unsigned char TA0_EC_Q[] = {
0x04, 0xAD, 0x02, 0x25, 0x3D, 0x08, 0x4F, 0xE0, 0x61, 0x99, 0x67, 0xFF,
0x97, 0x2E, 0xE8, 0x7E, 0x2C, 0x91, 0x17, 0xCB, 0x4F, 0xE7, 0xD4, 0x1D,
0x7F, 0x6B, 0x38, 0x87, 0x3C, 0x2B, 0x95, 0xC1, 0xFC, 0xD9, 0x0C, 0x90,
0x8A, 0x8D, 0x44, 0x28, 0x7D, 0x2F, 0x0F, 0x18, 0x6B, 0xE9, 0xDA, 0x20,
0xDB, 0x9A, 0xDF, 0xA9, 0x33, 0x71, 0x12, 0xC4, 0x0C, 0x37, 0x11, 0xB6,
0x12, 0x27, 0x52, 0xAE, 0x10
};

static const br_x509_trust_anchor TAs[1] = {
{
{ (unsigned char *)TA0_DN, sizeof TA0_DN },
BR_X509_TA_CA,
{
BR_KEYTYPE_EC,
{ .ec = {
BR_EC_secp256r1,
(unsigned char *)TA0_EC_Q, sizeof TA0_EC_Q,
} }
}
}
};

#define TAs_NUM 1
38 changes: 26 additions & 12 deletions HughTheLightbulbV2/Device/mqtt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,32 @@
#include <sntp.h>
#include <tick_macros.h>

// Uncomment to use the demo.cheriot.org server instead of mosquitto.
// This is faster (not rate limited) but might not be running.
// #define CHERIOT_DEMO_SERVER 1

#ifdef CHERIOT_DEMO_SERVER
// Permit statically selecting between three MQTT servers:
// 1) test.mosquitto.org (public mosquitto.org server intended only for testing, rate limited and sometimes unreliable)
// 2) demo.cheriot.org (cheriot.org server that is faster but may be turned off)
// 3) cheriot.demo (a non-standard TLD used only with local server setup)

#define MOSQUITTO_ORG 1
#define DEMO_CHERIOT_ORG 2
#define CHERIOT_DEMO 3

// Uncomment one of these lines to select the server used
#define MQTT_SERVER MOSQUITTO_ORG
// #define MQTT_SERVER DEMO_CHERIOT_ORG
// #define MQTT_SERVER CHERIOT_DEMO

// Include the relevant trust anchor for the selected server
#if MQTT_SERVER == MOSQUITTO_ORG
# include "mosquitto.org.h"
#define MQTT_SERVER_NAME "test.mosquitto.org"
#elif MQTT_SERVER == DEMO_CHERIOT_ORG
# include "letsencrypt.h"
#define MQTT_SERVER_NAME "demo.cheriot.org"
#elif MQTT_SERVER == CHERIOT_DEMO
#include "cheriot.demo.h"
#define MQTT_SERVER_NAME "cheriot.demo"
#else
# include "mosquitto.org.h"
# error "unknown MQTT_SERVER"
#endif

using CHERI::Capability;
Expand All @@ -47,11 +65,7 @@ constexpr const size_t outgoingPublishCount = 20;
// MQTT test broker: https://test.mosquitto.org/
// Note: port 8883 is encrypted and unauthenticated
DECLARE_AND_DEFINE_CONNECTION_CAPABILITY(MQTTServerCapability,
#ifdef CHERIOT_DEMO_SERVER
"demo.cheriot.org",
#else
"test.mosquitto.org",
#endif
MQTT_SERVER_NAME,
8883,
ConnectionTypeTCP);

Expand Down Expand Up @@ -185,7 +199,7 @@ void __cheri_compartment("hugh_network") run()
mqtt_generate_client_id(clientID.data() + clientIDPrefix.size(),
clientID.size() - clientIDPrefix.size());

Debug::log("Connecting to MQTT broker...");
Debug::log("Connecting to MQTT broker {}...", MQTT_SERVER_NAME);

t = UnlimitedTimeout;
auto handle = mqtt_connect(&t,
Expand Down
2 changes: 1 addition & 1 deletion HughTheLightbulbV2/HughController/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions HughTheLightbulbV2/HughController/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion HughTheLightbulbV2/HughController/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions HughTheLightbulbV2/HughController/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.cheriot.hughcontroller;

import android.os.Bundle;
import android.text.Editable;
import android.view.*;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.textfield.TextInputEditText;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.skydoves.colorpickerview.ColorPickerView;
import com.hivemq.client.mqtt.mqtt3.*;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener;
import com.skydoves.colorpickerview.sliders.BrightnessSlideBar;

import com.google.zxing.integration.android.IntentIntegrator;
Expand All @@ -25,17 +27,25 @@
import com.sun.jna.NativeLong;


public class MainActivity extends AppCompatActivity implements android.view.View.OnClickListener, com.skydoves.colorpickerview.listeners.ColorEnvelopeListener {
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ColorEnvelopeListener, AdapterView.OnItemSelectedListener {

String MQTTName;
Mqtt3BlockingClient MQTTClient;

// Set to true to use the demo.cheriot.org MQTT server (may not be running!).
final boolean UseCHERIoTDemoServer = false;

byte key[];
final byte[] context = new byte[8];

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
android.util.Log.i("selected", "" + l);
// initialiseMQTT();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

public interface Hydrogen extends Library {
Hydrogen INSTANCE = (Hydrogen)
Native.loadLibrary("hydrogen", Hydrogen.class);
Expand Down Expand Up @@ -88,7 +98,8 @@ protected void onRestoreInstanceState (Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
key = savedInstanceState.getByteArray("key");
MQTTName = savedInstanceState.getString("topic");
initialiseMQTT();
if (MQTTName != null)
initialiseMQTT();
}

private void initialiseMQTT()
Expand All @@ -102,15 +113,27 @@ private void initialiseMQTT()
clientID.append(ClientIdCharacters.charAt(rand.nextInt(ClientIdCharacters.length())));
}
android.util.Log.i("MQTT Client ID", clientID.toString());
String server = UseCHERIoTDemoServer ? "demo.cheriot.org" : "test.mosquitto.org";
int port = UseCHERIoTDemoServer ? 8883 : 8886;
MQTTClient = Mqtt3Client.builder()
Spinner serverSpinner = (Spinner) findViewById(R.id.serverSpinner);
String selectedServer = serverSpinner.getSelectedItem().toString();
String[] serverParts = selectedServer.split(":");
String server = serverParts[0];
int port = Integer.valueOf(serverParts[1]);
boolean use_ssl = port != 1883;

if (MQTTClient != null && MQTTClient.getState().isConnected()) {
android.util.Log.i("MQTT", "disconnecting");
MQTTClient.disconnect();
}

android.util.Log.i("MQTT", "Connecting to " + server + ":" + port);
Mqtt3ClientBuilder builder = Mqtt3Client.builder()
.serverHost(server)
.serverPort(port)
.sslWithDefaultConfig()
.automaticReconnectWithDefaultConfig()
.identifier(clientID.toString())
.buildBlocking();
.serverPort(port);
if (use_ssl)
builder = builder.sslWithDefaultConfig();
builder = builder.automaticReconnectWithDefaultConfig();
builder.identifier(clientID.toString());
MQTTClient = builder.buildBlocking();
android.util.Log.i("MQTT Client connection", "Started");
MQTTClient.connect();
android.util.Log.i("MQTT Client connection", "Completed");
Expand All @@ -131,18 +154,31 @@ protected void onCreate(Bundle savedInstanceState) {
return insets;
});

initialiseMQTT();
// initialiseMQTT();

android.widget.Button btnQRScan = findViewById(R.id.btnQRScan);
btnQRScan.setOnClickListener(this);

android.widget.Button btnForgetKey = findViewById(R.id.btnForgetKey);
btnForgetKey.setOnClickListener(this);

android.widget.Button btnConnect = findViewById(R.id.btnConnect);
btnConnect.setOnClickListener(this);

ColorPickerView colourPicker = findViewById(R.id.colorPickerView);
colourPicker.setColorListener(this);
BrightnessSlideBar brightnessSlideBar = findViewById(R.id.brightnessSlide);
colourPicker.attachBrightnessSlider(brightnessSlideBar);

Spinner serverSpinner = (Spinner) findViewById(R.id.serverSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.servers,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
serverSpinner.setAdapter(adapter);
serverSpinner.setOnItemSelectedListener(this);
}


Expand Down Expand Up @@ -183,6 +219,8 @@ public void onClick(View view) {
intentIntegrator.initiateScan();
} else if (view.getId() == R.id.btnForgetKey) {
key = new byte[32];
} else if (view.getId() == R.id.btnConnect) {
initialiseMQTT();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@
android:layout_height="wrap_content"
android:text="Forget Encryption Key" />

<Spinner
android:id="@+id/serverSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btnConnect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connect" />

</LinearLayout>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">Hugh Controller</string>
</resources>
<string-array name="servers">
<item>test.mosquitto.org:8886</item>
<item>demo.cheriot.org:8883</item>
<item>10.0.0.10:1883</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.5.2"
agp = "8.8.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Aug 29 10:10:38 BST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists