fix: uuid saving as blob

This commit is contained in:
Tobias Reisinger 2020-04-18 21:29:52 +02:00
parent 2b9abff7f1
commit beea18f70b
10 changed files with 80 additions and 20 deletions

View file

@ -5,6 +5,9 @@
#include <unistd.h>
#include <uuid/uuid.h>
#include <helpers.h>
#include <binn.h>
#include <enums.h>
#include "controller_dbo.h"
#include "globals.h"
@ -228,23 +231,37 @@ controller_dbo::get_by(helpers::sql_filter_builder **filters)
}
bool
controller_dbo::command(int command_code, const char *payload)
controller_dbo::command(binn *payload)
{
LOG_DEBUG << "Commanding " << binn_map_uint8(payload, COMMAND_MAPPING_CODE);
int bytes_transferred;
char port_str[6];
sprintf(port_str, "%d", this->port);
int controller_socket = helpers::open_tcp_connection(this->ip, port_str);
void *payload_ptr = binn_ptr(payload);
uint32_t payload_size = binn_size(payload);
if(!controller_socket)
int fd_controller = helpers::open_tcp_connection(this->ip, port_str);
if(fd_controller == -1)
{
LOG_ERROR << "Can't open command socket " << this->ip << ":" << port_str;
return false;
}
LOG_DEBUG << "Commanding (" << command_code << ") " << payload;
send(controller_socket, &command_code, 1, 0);
send(controller_socket, payload, strlen(payload), 0);
close(controller_socket);
if((bytes_transferred = send(fd_controller, &payload_size, sizeof(payload_size), 0)) <= 0)
{
LOG_ERROR << "error during sending size";
return false;
}
if((bytes_transferred = send(fd_controller, payload_ptr, payload_size, 0)) <= 0)
{
LOG_ERROR << "error during sending";
return false;
}
close(fd_controller);
return true;
}

View file

@ -6,6 +6,7 @@
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
#include <binn.h>
#include "relay_dbo.h"
class controller_dbo
@ -45,7 +46,7 @@ public:
get_all();
bool
command(int command_code, const char *payload);
command(binn *payload);
static void
free_list(controller_dbo **controllers_list);

View file

@ -55,7 +55,7 @@ period_list::to_json()
}
uint16_t*
period_list::to_db_blob()
period_list::to_blob()
{
auto result = (uint16_t*)malloc(sizeof(uint16_t) * ((this->length * 2) + 1));

View file

@ -22,7 +22,7 @@ public:
to_json();
uint16_t*
to_db_blob();
to_blob();
};
#endif //EMGAUWA_CORE_PERIOD_LIST_H

View file

@ -8,7 +8,7 @@
static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt)
{
int rc;
uint16_t *periods_blob = schedule->periods->to_db_blob();
uint16_t *periods_blob = schedule->periods->to_blob();
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
sqlite3_bind_blob(stmt, 1, schedule->id, sizeof(uuid_t), SQLITE_STATIC);