napperley
07/02/2019, 11:56 PM# ...
---
static inline MQTTClient_connectOptions createMqttConnectionOptions() {
MQTTClient_connectOptions options = MQTTClient_connectOptions_initializer;
return options;
}
napperley
07/02/2019, 11:59 PMMQTTClient_connect
function is called):
// ...
private fun setupMqttSubscription(settings: Map<String, String>): Unit = memScoped {
val address = settings["address"] ?: ""
val clientId = settings["clientId"] ?: ""
val topic = settings["topic"] ?: ""
val client = allocArray<MQTTClientVar>(1)
val connOptions = createMqttConnectionOptions()
val qos = 1
MQTTClient_create(client, address, clientId, MQTTCLIENT_PERSISTENCE_NONE, null)
connOptions.ptr.pointed.keepAliveInterval = 20
connOptions.ptr.pointed.cleansession = 1
MQTTClient_setCallbacks(client, null, null, null, staticCFunction(::onMessageReceived))
println("Connecting to MQTT Broker...")
val rc = MQTTClient_connect(client, connOptions)
if (rc != MQTTCLIENT_SUCCESS) {
println("Failed to connect (error code $rc)")
exit(-1)
}
println("Subscribing to topic $topic for client $clientId using QoS $qos...")
MQTTClient_subscribe(client, topic, qos)
}
// ...
msink
07/03/2019, 12:54 AMstatic
before MQTTClient_connectOption
napperley
07/03/2019, 1:04 AMclient
property is the one I'am not sure about. Here are the relevant definitions in the knm file:
public typealias MQTTClient = kotlinx.cinterop.COpaquePointer
public typealias MQTTClientVar = kotlinx.cinterop.CPointerVarOf<paho_mqtt.MQTTClient>
napperley
07/03/2019, 1:11 AMMQTTClient_create
function is properly initialising client
.napperley
07/03/2019, 1:36 AMproperty
isn't being properly initialised by the MQTTClient_create
function.napperley
07/03/2019, 1:40 AM==16462== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==16462== Command: ./mqtt_client.kexe
==16462==
Starting MQTT Client...
Settings:
* address: <tcp://xxx.xx.xx:xxxx>
* clientId: ExampleClientSub
* topic: MQTT Examples
* timeout: 10000
* qos: 1
Connecting to MQTT Broker...
==16462== Invalid read of size 4
==16462== at 0x64731AA: MQTTClient_connect (MQTTClient.c:1503)
napperley
07/03/2019, 2:03 AMclient
property's first element contains a pointer after a call to the MQTTClient_create
function, eg: CPointer(raw=0x545d14)napperley
07/03/2019, 2:43 AMMQTTClient_connect(client, connOptions)
) instead of passing through a pointer to the first element in the array (MQTTClient_connect(client[0], connOptions)
) 🤦♂️ .