1559 |
1559 |
1560 /* |
1560 /* |
1561 * Initialize mosquitto communication |
1561 * Initialize mosquitto communication |
1562 */ |
1562 */ |
1563 gethostname(my_hostname, 255); |
1563 gethostname(my_hostname, 255); |
|
1564 if (strchr(my_hostname, '.')) { |
|
1565 char *p = strchr(my_hostname, '.'); |
|
1566 *p = '\0'; |
|
1567 } |
1564 mosquitto_lib_init(); |
1568 mosquitto_lib_init(); |
1565 id = xstrcpy((char *)"thermferm/"); |
1569 id = xstrcpy((char *)"thermferm/"); |
1566 id = xstrcat(id, my_hostname); |
1570 id = xstrcat(id, my_hostname); |
1567 if (strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { |
1571 if (strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { |
1568 /* |
1572 /* |
1609 mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); |
1610 mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); |
1610 mosquitto_publish_callback_set(mosq, my_publish_callback); |
1611 mosquitto_publish_callback_set(mosq, my_publish_callback); |
1611 mosquitto_message_callback_set(mosq, my_message_callback); |
1612 mosquitto_message_callback_set(mosq, my_message_callback); |
1612 mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); |
1613 mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); |
1613 |
1614 |
1614 if (Config.mqtt_username && Config.mqtt_password) { |
1615 if (Config.mqtt_username) { |
1615 syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set(%s, %s)", Config.mqtt_username, Config.mqtt_password); |
1616 if (Config.mqtt_password) { |
1616 if ((rc = mosquitto_username_pw_set(mosq, Config.mqtt_username, Config.mqtt_password))) { |
1617 rc = mosquitto_username_pw_set(mosq, Config.mqtt_username, Config.mqtt_password); |
1617 switch(errno) { |
1618 } else { |
1618 case ENOMEM: |
1619 rc = mosquitto_username_pw_set(mosq, Config.mqtt_username, NULL); |
1619 syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Out of memory"); |
1620 } |
1620 break; |
1621 if (rc == MOSQ_ERR_INVAL) { |
1621 case EINVAL: |
1622 syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Invalid id"); |
1622 syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Invalid id"); |
1623 } else if (rc == MOSQ_ERR_NOMEM) { |
1623 break; |
1624 syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Out of memory"); |
1624 default: |
1625 } |
1625 syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Unknown error %d", errno); |
1626 if (rc != MOSQ_ERR_SUCCESS) { |
1626 break; |
|
1627 } |
|
1628 mosquitto_lib_cleanup(); |
1627 mosquitto_lib_cleanup(); |
1629 return; |
1628 return; |
1630 } |
1629 } |
1631 } |
1630 } |
1632 |
1631 |