Re: Help me wrap PahoMQTT
- Posted by petelomax Apr 05, 2019
- 3166 views
xfox26 said...
Can someone help me get this struct on EU code?
First try using a persistent_type of MQTTCLIENT_PERSISTENCE_NONE(=1) and a persistent_context of NULL on the create. The hndl use is also wrong:
--atom xMQTTClient_create = define_c_func(paho_c_dll, "+MQTTClient_create", {C_HANDLE, C_POINTER ,C_POINTER, C_INT, C_POINTER}, C_INT) --(^changing the 5th arg to ptr is technically more accurate, but won't make much difference, ditto persistent_type of int below) constant MQTTCLIENT_PERSISTENCE_NONE = 1, MQTTCLIENT_SUCCESS = 0 function MQTTClient_create(sequence server_uri, sequence client_id) --, integer persistence_type, atom persistence_context) atom hndl = allocate(4) atom ptr_server_uri = allocate_string(server_uri) atom ptr_client_id = allocate_string(client_id) atom ret = c_func(xMQTTClient_create, {hndl, ptr_server_uri, ptr_client_id, MQTTCLIENT_PERSISTENCE_NONE, NULL}) if ret = MQTTCLIENT_SUCCESS then ret = peek4u(hndl) else ret = NULL end if free(ptr_server_uri) free(ptr_client_id) free(hndl) return ret end function
Now, for connect, in https://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/_m_q_t_t_client_8h_source.html I find
typedef struct { char struct_id[4]; int struct_version; int keepAliveInterval; int cleansession; int reliable; MQTTClient_willOptions* will; const char* username; const char* password; int connectTimeout; int retryInterval; MQTTClient_SSLOptions* ssl; int serverURIcount; char* const* serverURIs; int MQTTVersion; struct { const char* serverURI; int MQTTVersion; int sessionPresent; } returned; struct { int len; const void* data; } binarypwd; int maxInflightMessages; /* * MQTT V5 clean start flag. Only clears state at the beginning of the session. */ int cleanstart; } MQTTClient_connectOptions; #define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 6, 60, 1, 1, NULL, NULL, NULL, 30, 0, NULL, 0, NULL, MQTTVERSION_DEFAULT, {NULL, 0, 0}, {0, NULL}, -1, 0} #define MQTTClient_connectOptions_initializer5 { {'M', 'Q', 'T', 'C'}, 6, 60, 0, 1, NULL, NULL, NULL, 30, 0, NULL, 0, NULL, MQTTVERSION_5, {NULL, 0, 0}, {0, NULL}, -1, 1}which is what you should really be looking at. It'll be different if you're runing 64 bit, but I'm going to assume you are using 32 bit.
I count 21 4-byte fields, which you need to set as per one of the last two lines, so it goes something like this (untested!):
atom pConnectOptions = allocate(4*21) poke(pConnectOptions+0,"MQTC") poke4(pConnectOptions+4,6) poke4(pConnectOptions+8,60) ... poke4(pConnectOptions+4*19,-1) poke4(pConnectOptions+4*20,1)
Good luck!
PS: using Phix's cffi might make things somewhat easier, but it would not surprise me if it baulked at the nested "returned" and "binarypwd" structs, if so I'd just delete the containers leaving just the inner fields.