1616//
1717
1818#include "atomvm_mqtt_client.h"
19+ #include "smp.h"
1920
2021#include <atom.h>
2122#include <bif.h>
@@ -61,6 +62,7 @@ static const char *const unsubscribe_failed_atom = ATOM_STR("\x12", "unsu
6162static const char * const unsubscribed_atom = ATOM_STR ("\xC" , "unsubscribed" );
6263static const char * const url_atom = ATOM_STR ("\x3" , "url" );
6364static const char * const username_atom = ATOM_STR ("\x8" , "username" );
65+ static const char * const client_id_atom = ATOM_STR ("\x9" , "client_id" );
6466
6567// error codes
6668static const char * const bad_username_atom = ATOM_STR ("\x0C" , "bad_username" );
@@ -700,12 +702,11 @@ void atomvm_mqtt_client_init(GlobalContext *global)
700702 esp_log_level_set ("MQTT_CLIENT" , ESP_LOG_VERBOSE );
701703}
702704
703- // NB. Caller assumes ownership of returned string
704- static char * maybe_get_string (term kv , AtomString key , GlobalContext * global )
705+ static char * maybe_get_string_or_default (term kv , AtomString key , char * default_value , GlobalContext * global )
705706{
706707 term value_term = interop_kv_get_value (kv , key , global );
707708 if (!term_is_string (value_term ) && !term_is_binary (value_term )) {
708- return NULL ;
709+ return default_value ;
709710 }
710711
711712 int ok ;
@@ -717,6 +718,12 @@ static char *maybe_get_string(term kv, AtomString key, GlobalContext *global)
717718 return value_str ;
718719}
719720
721+ // NB. Caller assumes ownership of returned string
722+ static char * maybe_get_string (term kv , AtomString key , GlobalContext * global )
723+ {
724+ return maybe_get_string_or_default (kv , key , NULL , global );
725+ }
726+
720727// NB. Caller assumes ownership of returned string
721728// static char *get_string_default(term kv, AtomString key, AtomString default_value, GlobalContext *global)
722729// {
@@ -785,16 +792,23 @@ Context *atomvm_mqtt_client_create_port(GlobalContext *global, term opts)
785792 UNUSED (port );
786793 char * username_str = maybe_get_string (opts , username_atom , global );
787794 char * password_str = maybe_get_string (opts , password_atom , global );
788- // char *cert_str = maybe_get_string (opts, cert_atom , global);
795+ char * client_id = maybe_get_string_or_default (opts , client_id_atom , get_default_client_id () , global );
789796
790797 // Note that char * values passed into this struct are copied into the MQTT state
791- const char * client_id = get_default_client_id ();
792798 esp_mqtt_client_config_t mqtt_cfg = {
793799#if ESP_IDF_VERSION_MAJOR >= 5
794800 .broker .address .uri = url_str ,
795- .credentials .client_id = client_id
801+ .credentials = {
802+ .username = username_str ,
803+ .client_id = client_id ,
804+ .authentication = {
805+ .password = password_str
806+ }
807+ },
796808#else
797809 .uri = url_str ,
810+ .username = username_str ,
811+ .password = password_str
798812 .client_id = client_id ,
799813 .user_context = (void * ) ctx
800814#endif
@@ -805,6 +819,7 @@ Context *atomvm_mqtt_client_create_port(GlobalContext *global, term opts)
805819 free (host_str );
806820 free (username_str );
807821 free (password_str );
822+ free (client_id );
808823
809824 if (UNLIKELY (IS_NULL_PTR (client ))) {
810825 ESP_LOGE (TAG , "Error: Unable to initialize MQTT client.\n" );
0 commit comments