Skip to content

Commit

Permalink
Merge pull request #27 from hedrosistemas/develop
Browse files Browse the repository at this point in the history
merge from hedrosistemas
  • Loading branch information
wjsan authored Oct 23, 2024
2 parents 8ae9d37 + 17593cd commit f942691
Show file tree
Hide file tree
Showing 10 changed files with 22,526 additions and 6,921 deletions.
3,582 changes: 3,582 additions & 0 deletions include/ciot_ca_crt_all.h

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/ciot_storage_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ ciot_storage_t ciot_storage_fs_new(void);
ciot_err_t ciot_storage_fs_delete(char *path);
ciot_err_t ciot_storage_fs_write_bytes(char *path, uint8_t *bytes, int size);
ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *size);
ciot_err_t ciot_storage_fs_read_text(char *path, char *buf);

#endif //!__CIOT_STORAGE_FS__H__
19 changes: 19 additions & 0 deletions src/common/ciot_storage_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,22 @@ ciot_err_t ciot_storage_fs_delete(char *path)
remove(path);
return CIOT__ERR__OK;
}

ciot_err_t ciot_storage_fs_read_text(char *path, char *buf)
{
FILE *file = fopen(path, "r");
if (file == NULL) {
return CIOT__ERR__NOT_FOUND;
}

fseek(file, 0, SEEK_END);
long filesize = ftell(file);
rewind(file);

fread(buf, sizeof(char), filesize, file);
buf[filesize] = '\0';

fclose(file);

return CIOT__ERR__OK;
}
10 changes: 5 additions & 5 deletions src/mg/ciot_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ciot_http_client
static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req);
static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg);
static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size);
static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void *ev_data);
static const char *ciot_http_client_get_method(ciot_http_client_method_t method);

ciot_http_client_t ciot_http_client_new(void *handle)
Expand Down Expand Up @@ -120,9 +120,9 @@ ciot_err_t ciot_http_client_stop(ciot_http_client_t self)
return CIOT__ERR__OK;
}

static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void *ev_data)
{
ciot_http_client_t self = fn_data;
ciot_http_client_t self = c->fn_data;
ciot_iface_event_t iface_event = {0};
mg_event_t mg_ev = ev;

Expand Down Expand Up @@ -168,7 +168,7 @@ static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void
"Content-Length: %d\r\n"
"\r\n",
ciot_http_client_get_method(self->base.cfg.method), mg_url_uri(self->base.cfg.url), (int)host.len,
host.ptr, self->base.send.len);
host.buf, self->base.send.len);
mg_send(c, self->base.send.data, self->base.send.len);
self->base.status.state = CIOT__HTTP_CLIENT_STATE__HTTP_CLIENT_STATE_CONNECTED;
iface_event.type = CIOT_IFACE_EVENT_INTERNAL;
Expand All @@ -183,7 +183,7 @@ static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void
c->is_draining = 1;
self->base.status.state = CIOT__HTTP_CLIENT_STATE__HTTP_CLIENT_STATE_IDLE;
iface_event.type = CIOT_IFACE_EVENT_REQUEST;
iface_event.data = (uint8_t*)hm->body.ptr;
iface_event.data = (uint8_t*)hm->body.buf;
iface_event.size = hm->body.len;
ciot_iface_send_event(&self->base.iface, &iface_event);
break;
Expand Down
16 changes: 8 additions & 8 deletions src/mg/ciot_http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ciot_http_server
};

static bool check_method(struct mg_http_message *hm, const char* method);
static void ciot_http_server_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
static void ciot_http_server_event_handler(struct mg_connection *c, int ev, void *ev_data);

ciot_http_server_t ciot_http_server_new(void *handle)
{
Expand Down Expand Up @@ -89,12 +89,12 @@ ciot_err_t ciot_http_server_send_bytes(ciot_http_server_t self, uint8_t *data, i

static bool check_method(struct mg_http_message *hm, const char* method)
{
return strncmp(hm->method.ptr, method, hm->method.len) == 0;
return strncmp(hm->method.buf, method, hm->method.len) == 0;
}

static void ciot_http_server_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
static void ciot_http_server_event_handler(struct mg_connection *c, int ev, void *ev_data)
{
ciot_http_server_t self = fn_data;
ciot_http_server_t self = c->fn_data;
ciot_http_server_base_t *base = &self->base;
ciot_iface_event_t iface_event = {0};
mg_event_t mg_ev = ev;
Expand Down Expand Up @@ -134,10 +134,10 @@ static void ciot_http_server_event_handler(struct mg_connection *c, int ev, void
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
mg_http_parse((char *)c->recv.buf, c->recv.len, hm);
self->conn_tx = c;
if (mg_http_match_uri(hm, base->cfg.route) && check_method(hm, "POST"))
if (mg_match(hm->uri, mg_str(base->cfg.route), NULL) && check_method(hm, "POST"))
{
iface_event.type = CIOT_IFACE_EVENT_REQUEST;
iface_event.data = (uint8_t*)hm->body.ptr;
iface_event.data = (uint8_t*)hm->body.buf;
iface_event.size = hm->body.len;
ciot_iface_send_event(&base->iface, &iface_event);
}
Expand All @@ -150,9 +150,9 @@ static void ciot_http_server_event_handler(struct mg_connection *c, int ev, void
else
{
ciot_http_server_event_data_t event_data = { 0 };
event_data.uri = (char*)hm->uri.ptr;
event_data.uri = (char*)hm->uri.buf;
event_data.uri[hm->uri.len] = '\0';
event_data.body = (uint8_t*)hm->body.ptr;
event_data.body = (uint8_t*)hm->body.buf;
iface_event.type = CIOT_IFACE_EVENT_DATA;
iface_event.data = (uint8_t*)&event_data;
iface_event.size = hm->body.len;
Expand Down
30 changes: 22 additions & 8 deletions src/mg/ciot_mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ciot_timer.h"
#include "ciot_str.h"
#include "ciot_msg.h"
#include "ciot_ca_crt_all.h"

static const char *TAG = "ciot_mqtt_client";

Expand All @@ -27,7 +28,7 @@ struct ciot_mqtt_client
time_t last_ping;
};

static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void *ev_data);

ciot_mqtt_client_t ciot_mqtt_client_new(void *handle)
{
Expand Down Expand Up @@ -109,7 +110,7 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d
CIOT_ERR_EMPTY_STRING_CHECK(topic);
struct mg_mqtt_opts opts = {0};
struct mg_str msg = {0};
msg.ptr = (const char*)data;
msg.buf = (char*)data;
msg.len = size;
opts.topic = mg_str(topic);
opts.message = msg;
Expand All @@ -120,9 +121,9 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d
return CIOT__ERR__OK;
}

static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void *ev_data)
{
ciot_mqtt_client_t self = fn_data;
ciot_mqtt_client_t self = c->fn_data;
ciot_mqtt_client_base_t *base = &self->base;
ciot_iface_event_t iface_event = {0};
mg_event_t mg_ev = ev;
Expand All @@ -143,6 +144,19 @@ static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void
iface_event.type = CIOT_IFACE_EVENT_INTERNAL;
ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_INTERNAL);
break;
case MG_EV_CONNECT:
{
if(mg_url_is_ssl(self->base.cfg.url))
{
struct mg_tls_opts opts = {
.ca = mg_str(ca_crt_all),
.name = mg_url_host(self->base.cfg.url),
.skip_verification = true
};
mg_tls_init(c, &opts);
}
break;
}
case MG_EV_POLL:
{
if(base->status.state == CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED &&
Expand Down Expand Up @@ -175,18 +189,18 @@ static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void
CIOT_LOGI(TAG, "MG_EV_MQTT_MSG");
struct mg_mqtt_message *mm = (struct mg_mqtt_message *)ev_data;
if(strlen(base->cfg.topics->sub) == mm->topic.len &&
strncmp(mm->topic.ptr, base->cfg.topics->sub, mm->topic.len) == 0)
strncmp(mm->topic.buf, base->cfg.topics->sub, mm->topic.len) == 0)
{
iface_event.type = CIOT_IFACE_EVENT_REQUEST;
iface_event.data = (uint8_t*)mm->data.ptr;
iface_event.data = (uint8_t*)mm->data.buf;
iface_event.size = mm->data.len;
ciot_iface_send_event(&base->iface, &iface_event);
}
else
{
ciot_mqtt_client_event_data_t event_data = {0};
event_data.topic = (char*)mm->topic.ptr;
event_data.data = (uint8_t*)mm->data.ptr;
event_data.topic = mm->topic.buf;
event_data.data = (uint8_t*)mm->data.buf;
iface_event.type = CIOT_IFACE_EVENT_DATA;
iface_event.data = (uint8_t*)&event_data;
iface_event.size = mm->data.len;
Expand Down
6 changes: 3 additions & 3 deletions src/mg/ciot_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ciot_socket

static const char *TAG = "ciot_socket";

static void ciot_socket_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
static void ciot_socket_event_handler(struct mg_connection *c, int ev, void *ev_data);
static ciot_err_t ciot_socket_server_start(ciot_socket_t self);
static ciot_err_t ciot_socket_client_start(ciot_socket_t self);

Expand Down Expand Up @@ -106,9 +106,9 @@ static ciot_err_t ciot_socket_client_start(ciot_socket_t self)
return CIOT__ERR__OK;
}

static void ciot_socket_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
static void ciot_socket_event_handler(struct mg_connection *c, int ev, void *ev_data)
{
ciot_socket_t self = fn_data;
ciot_socket_t self = c->fn_data;
ciot_socket_base_t *base = &self->base;
ciot_iface_event_t iface_event = {0};
mg_event_t mg_ev = ev;
Expand Down
Loading

0 comments on commit f942691

Please sign in to comment.