Skip to content

Commit

Permalink
add mqtt and home assistant support
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Aug 4, 2023
1 parent f9ab06f commit 6be01f1
Show file tree
Hide file tree
Showing 14 changed files with 1,687 additions and 45 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ CYCLONE_SOURCES = \
cyclone/cyclone_tcp/http/http_common.c \
cyclone/cyclone_tcp/http/http_server.c \
cyclone/cyclone_tcp/http/http_server_misc.c \
cyclone/cyclone_tcp/mqtt/mqtt_client.c \
cyclone/cyclone_tcp/mqtt/mqtt_client_packet.c \
cyclone/cyclone_tcp/mqtt/mqtt_client_misc.c \
cyclone/cyclone_ssl/tls.c \
cyclone/cyclone_ssl/tls_cipher_suites.c \
cyclone/cyclone_ssl/tls_handshake.c \
Expand Down Expand Up @@ -245,13 +248,15 @@ CYCLONE_SOURCES := $(filter-out \
cyclone/cyclone_tcp/http/http_server.c \
cyclone/cyclone_tcp/http/http_server_misc.c \
cyclone/cyclone_ssl/tls_certificate.c \
cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c \
, $(CYCLONE_SOURCES))

# and add modified ones
CYCLONE_SOURCES += \
src/cyclone/common/debug.c \
src/cyclone/cyclone_tcp/http/http_server.c \
src/cyclone/cyclone_tcp/http/http_server_misc.c \
src/cyclone/cyclone_tcp/mqtt/mqtt_client_transport.c \
src/cyclone/cyclone_ssl/tls_certificate.c

CFLAGS += -D GPL_LICENSE_TERMS_ACCEPTED
Expand Down
108 changes: 108 additions & 0 deletions include/home_assistant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#pragma once

#include <stdint.h>
#include <stdbool.h>

#define MAX_LEN 32
#define MAX_ENTITIES (3 * 9 + 16 * 7 + 32)

typedef enum
{
ha_unused = 0,
/* https://www.home-assistant.io/integrations/text.mqtt/ */
ha_text,
/* https://www.home-assistant.io/integrations/sensor.mqtt/ */
ha_sensor,
/* https://www.home-assistant.io/integrations/number.mqtt/ */
ha_number,
/* https://www.home-assistant.io/integrations/button.mqtt/ */
ha_button,
/* https://www.home-assistant.io/integrations/select.mqtt/ */
ha_select,
/* https://www.home-assistant.io/integrations/binary_sensor.mqtt/ */
ha_binary_sensor,
/* https://www.home-assistant.io/integrations/ha_light.mqtt/ */
ha_light
} t_ha_device_type;

typedef struct s_ha_entity t_ha_entity;

struct s_ha_entity
{
t_ha_device_type type;

const char *name;
const char *id;

/* used by: sensor */
const char *unit_of_meas;
/* used by: sensor */
const char *val_tpl;
/* used by: sensor */
const char *dev_class;
/* used by: sensor */
const char *state_class;
/* used by: button, number, text */
const char *cmd_t;
/* used by: sensor, binary_sensor, number, text */
const char *stat_t;
/* used by: light */
const char *rgb_t;
const char *rgbw_t;
/* used by: switch, comma separated */
const char *options;
/* used by: number */
float min;
/* used by: number */
float max;
/* used by: number */
const char *mode;
/* icon */
const char *ic;
/* entity_category */
const char *ent_cat;

/* used by: light */
const char *fx_cmd_t;
/* used by: light */
const char *fx_stat_t;
/* used by: light, comma separated */
const char *fx_list;

/* alternative client name */
const char *alt_name;

void (*received)(const t_ha_entity *, void *, const char *);
void *received_ctx;
void (*rgb_received)(const t_ha_entity *, void *, const char *);
void *rgb_received_ctx;
void (*fx_received)(const t_ha_entity *, void *, const char *);
void *fx_received_ctx;
void (*transmit)(const t_ha_entity *, void *);
void *transmit_ctx;
};

typedef struct
{
char name[MAX_LEN];
char id[MAX_LEN];
char cu[MAX_LEN];
char mf[MAX_LEN];
char mdl[MAX_LEN];
char sw[MAX_LEN];
t_ha_entity entities[MAX_ENTITIES];
int entitiy_count;
} t_ha_info;

void ha_setup();
void ha_connected();
bool ha_loop();
void ha_transmit_all();
void ha_publish();
void ha_add(t_ha_entity *entity);
void ha_addmqtt(char *json_str, const char *name, const char *value, t_ha_entity *entity, bool last);
void ha_received(char *topic, const char *payload);
void ha_transmit(const t_ha_entity *entity, const char *value);
void ha_transmit_topic(const char *stat_t, const char *value);
int ha_parse_index(const char *options, const char *message);
void ha_get_index(const char *options, int index, char *text);
21 changes: 21 additions & 0 deletions include/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __MACROS_H__
#define __MACROS_H__

// #define min(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
// #define max(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
#define coerce(val, min, max) \
do \
{ \
if ((val) > (max)) \
{ \
val = max; \
} \
else if ((val) < (min)) \
{ \
val = min; \
} \
} while (0)
#define xstr(s) str(s)
#define str(s) #s

#endif
8 changes: 8 additions & 0 deletions include/mqtt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "error.h"

void mqtt_init();
error_t mqtt_sendEvent(const char *eventname, const char *content);
bool mqtt_publish(const char *item_topic, const char *content);
bool mqtt_subscribe(const char *item_topic);
11 changes: 8 additions & 3 deletions include/net_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ typedef struct
#define HTTP_SERVER_PRIVATE_CONTEXT http_connection_private_t private;
#define HTTP_SERVER_PERSISTENT_CONN_SUPPORT ENABLED

#define MQTT_CLIENT_SUPPORT ENABLED
#define MQTT_CLIENT_TLS_SUPPORT ENABLED
#define MQTT_CLIENT_MAX_PASSWORD_LEN 64
#define MQTT_CLIENT_MAX_USERNAME_LEN 64

// Trace level for TCP/IP stack debugging
#define MEM_TRACE_LEVEL TRACE_LEVEL_INFO
#define NIC_TRACE_LEVEL TRACE_LEVEL_INFO
Expand Down Expand Up @@ -73,7 +78,7 @@ typedef struct
#define COAP_TRACE_LEVEL TRACE_LEVEL_INFO
#define FTP_TRACE_LEVEL TRACE_LEVEL_INFO
#define HTTP_TRACE_LEVEL TRACE_LEVEL_INFO
#define MQTT_TRACE_LEVEL TRACE_LEVEL_INFO
#define MQTT_TRACE_LEVEL TRACE_LEVEL_WARNING
#define MQTT_SN_TRACE_LEVEL TRACE_LEVEL_INFO
#define SMTP_TRACE_LEVEL TRACE_LEVEL_INFO
#define SNMP_TRACE_LEVEL TRACE_LEVEL_INFO
Expand Down Expand Up @@ -179,7 +184,7 @@ typedef struct
#define HTTP_SERVER_MULTIPART_TYPE_SUPPORT ENABLED

/* match original cloud settings */
#define HTTP_SERVER_IDLE_TIMEOUT (5*60000)
#define HTTP_SERVER_TIMEOUT (1*60000)
#define HTTP_SERVER_IDLE_TIMEOUT (5 * 60000)
#define HTTP_SERVER_TIMEOUT (1 * 60000)

#endif
1 change: 1 addition & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct
{
bool enabled;
char *hostname;
uint32_t port;
char *username;
char *password;
char *identification;
Expand Down
3 changes: 3 additions & 0 deletions src/cloud_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "tls_adapter.h"
#include "handler_api.h"
#include "settings.h"
#include "mqtt.h"
#include "platform.h"

#include "handler_cloud.h"
Expand Down Expand Up @@ -115,6 +116,8 @@ int_t cloud_request(const char *server, int port, bool https, const char *uri, c
return ERROR_ADDRESS_NOT_FOUND;
}

mqtt_sendEvent("CloudRequest", uri);

HttpClientContext httpClientContext;

if (!server)
Expand Down
Loading

0 comments on commit 6be01f1

Please sign in to comment.