Skip to content

Commit

Permalink
Removes dependencies from json-c from knot_cloud.
Browse files Browse the repository at this point in the history
- Changes knot_cloud.c file to not use directly json-c API;
- Modifies parser.c and parser.h methods to wrapper json-c;
- Modifies parser.h callback function to not use json-c;
- Modifies knot_cloud.c device callback function to only create device;
  • Loading branch information
agms-cesar authored and vitbaq committed Oct 13, 2020
1 parent 7643c75 commit 41c6a15
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 263 deletions.
235 changes: 68 additions & 167 deletions src/knot_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,17 @@ static void knot_cloud_msg_destroy(struct knot_cloud_msg *msg)
l_queue_destroy(msg->list, l_free);
else if (msg->type == LIST_MSG)
l_queue_destroy(msg->list, knot_cloud_device_free);

if (msg->token) {
l_free(msg->token);
msg->token = NULL;
}
l_free(msg);
}

static void *knot_cloud_device_array_foreach(json_object *array_item)
static void *create_device_item(const char *id, const char *name,
struct l_queue *schema)
{
json_object *jobjkey;
struct knot_cloud_device *device;
struct l_queue *schema;
const char *id, *name;

/* Getting 'Id': Mandatory field for registered device */
id = parser_get_key_str_from_json_obj(array_item,
KNOT_JSON_FIELD_DEVICE_ID);
if (!id)
return NULL;

/* Getting 'schema': Mandatory field for registered device */
/* FIXME: Call a parser function instead of json-c function*/
if (!json_object_object_get_ex(array_item,
KNOT_JSON_FIELD_SCHEMA, &jobjkey))
return NULL;

schema = parser_schema_to_list(json_object_to_json_string(jobjkey));
if (!schema)
return NULL;

/* Getting 'Name' */
name = parser_get_key_str_from_json_obj(array_item,
KNOT_JSON_FIELD_DEVICE_NAME);
if (!name)
return NULL;

device = l_new(struct knot_cloud_device, 1);
device->id = l_strdup(id);
Expand All @@ -121,127 +100,73 @@ static int map_routing_key_to_msg_type(const char *routing_key)
}

static struct knot_cloud_msg *create_msg(const char *routing_key,
json_object *jso)
const char *json_str)
{
bool has_err;

struct knot_cloud_msg *msg = l_new(struct knot_cloud_msg, 1);

msg->type = map_routing_key_to_msg_type(routing_key);

switch (msg->type) {
case UPDATE_MSG:
msg->error = NULL;
msg->device_id = parser_get_key_str_from_json_obj(jso,
has_err = false;
if (msg->type == LIST_MSG) {
msg->device_id = NULL;
} else {
msg->device_id = parser_get_key_str_from_json_str(json_str,
KNOT_JSON_FIELD_DEVICE_ID);
if (!msg->device_id) {
l_error("Malformed JSON message");
goto err;
}
has_err = msg->device_id ? false : true;
}

msg->list = parser_update_to_list(jso);
if (!msg->list) {
l_error("Malformed JSON message");
goto err;
}
msg->error = parser_get_key_str_from_json_str(json_str,
KNOT_JSON_FIELD_ERROR);

break;
case REQUEST_MSG:
msg->error = NULL;
msg->device_id = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_DEVICE_ID);
if (!msg->device_id) {
l_error("Malformed JSON message");
goto err;
}
if (msg->error)
has_err = parser_is_key_str_or_null(json_str,
KNOT_JSON_FIELD_ERROR) ? false : true;

msg->list = parser_request_to_list(jso);
if (!msg->list) {
l_error("Malformed JSON message");
goto err;
}
if (has_err) {
l_error("Ill-formed JSON message");
knot_cloud_msg_destroy(msg);
return NULL;
}

switch (msg->type) {
case UPDATE_MSG:
msg->list = parser_update_to_list(json_str);
has_err = msg->list ? false : true;
break;
case REQUEST_MSG:
msg->list = parser_request_to_list(json_str);
has_err = msg->list ? false : true;
break;
case REGISTER_MSG:
msg->device_id = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_DEVICE_ID);
if (!msg->device_id ||
!parser_is_key_str_or_null(jso,
KNOT_JSON_FIELD_ERROR)) {
l_error("Malformed JSON message");
goto err;
}

msg->token = parser_get_key_str_from_json_obj(jso,
msg->token = parser_get_key_str_from_json_str(json_str,
KNOT_JSON_FIELD_DEVICE_TOKEN);
if (!msg->token) {
l_error("Malformed JSON message");
goto err;
}

msg->error = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_ERROR);
has_err = msg->token ? false : true;
break;
case UNREGISTER_MSG:
msg->device_id = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_DEVICE_ID);
if (!msg->device_id ||
!parser_is_key_str_or_null(jso,
KNOT_JSON_FIELD_ERROR)) {
l_error("Malformed JSON message");
goto err;
}

msg->error = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_ERROR);
break;
case AUTH_MSG:
msg->device_id = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_DEVICE_ID);
if (!msg->device_id ||
!parser_is_key_str_or_null(jso,
KNOT_JSON_FIELD_ERROR)) {
l_error("Malformed JSON message");
goto err;
}

msg->error = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_ERROR);
break;
case SCHEMA_MSG:
msg->device_id = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_DEVICE_ID);
if (!msg->device_id ||
!parser_is_key_str_or_null(jso,
KNOT_JSON_FIELD_ERROR)) {
l_error("Malformed JSON message");
goto err;
}

msg->error = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_ERROR);
break;
case LIST_MSG:
msg->device_id = NULL;
msg->list = parser_queue_from_json_array(jso,
knot_cloud_device_array_foreach);
if (!msg->list || !parser_is_key_str_or_null(jso,
KNOT_JSON_FIELD_ERROR)) {
l_error("Malformed JSON message");
goto err;
}

msg->error = parser_get_key_str_from_json_obj(jso,
KNOT_JSON_FIELD_ERROR);
msg->list = parser_queue_from_json_array(json_str,
create_device_item);
has_err = msg->list ? false : true;
break;
case MSG_TYPES_LENGTH:
default:
l_error("Unknown event %s", routing_key);
goto err;
has_err = true;
break;
}

if (has_err) {
l_error("Ill-formed JSON message");
knot_cloud_msg_destroy(msg);
return NULL;
}

return msg;
err:
knot_cloud_msg_destroy(msg);
return NULL;
}

/**
Expand All @@ -257,22 +182,13 @@ static bool on_amqp_receive_message(const char *exchange,
{
struct knot_cloud_msg *msg;
bool consumed = true;
json_object *jso;

jso = json_tokener_parse(body);
if (!jso) {
l_error("Error on parse JSON object");
return false;
}

msg = create_msg(routing_key, jso);
msg = create_msg(routing_key, body);
if (msg) {
consumed = knot_cloud_cb(msg, user_data);
knot_cloud_msg_destroy(msg);
}

json_object_put(jso);

return consumed;
}

Expand Down Expand Up @@ -404,16 +320,13 @@ int knot_cloud_set_log_priority(int priority)
*/
int knot_cloud_register_device(const char *id, const char *name)
{
json_object *jobj_device;
const char *json_str;
char *json_str;
int result;

jobj_device = parser_device_json_create(id, name);
if (!jobj_device)
json_str = parser_device_json_create(id, name);
if (!json_str)
return KNOT_ERR_CLOUD_FAILURE;

json_str = json_object_to_json_string(jobj_device);

/**
* Exchange
* Type: Direct
Expand All @@ -435,7 +348,7 @@ int knot_cloud_register_device(const char *id, const char *name)
if (result < 0)
result = KNOT_ERR_CLOUD_FAILURE;

json_object_put(jobj_device);
l_free(json_str);

return result;
}
Expand All @@ -452,16 +365,13 @@ int knot_cloud_register_device(const char *id, const char *name)
*/
int knot_cloud_unregister_device(const char *id)
{
json_object *jobj_unreg;
const char *json_str;
char *json_str;
int result;

jobj_unreg = parser_unregister_json_create(id);
if (!jobj_unreg)
json_str = parser_unregister_json_create(id);
if (!json_str)
return KNOT_ERR_CLOUD_FAILURE;

json_str = json_object_to_json_string(jobj_unreg);

/**
* Exchange
* Type: Direct
Expand All @@ -483,7 +393,7 @@ int knot_cloud_unregister_device(const char *id)
if (result < 0)
return KNOT_ERR_CLOUD_FAILURE;

json_object_put(jobj_unreg);
l_free(json_str);

return 0;
}
Expand All @@ -501,16 +411,13 @@ int knot_cloud_unregister_device(const char *id)
*/
int knot_cloud_auth_device(const char *id, const char *token)
{
json_object *jobj_auth;
const char *json_str;
char *json_str;
int result;

jobj_auth = parser_auth_json_create(id, token);
if (!jobj_auth)
json_str = parser_auth_json_create(id, token);
if (!json_str)
return KNOT_ERR_CLOUD_FAILURE;

json_str = json_object_to_json_string(jobj_auth);

/**
* Exchange
* Type: Direct
Expand All @@ -531,7 +438,7 @@ int knot_cloud_auth_device(const char *id, const char *token)
if (result < 0)
result = KNOT_ERR_CLOUD_FAILURE;

json_object_put(jobj_auth);
l_free(json_str);

return result;
}
Expand All @@ -547,16 +454,13 @@ int knot_cloud_auth_device(const char *id, const char *token)
*/
int knot_cloud_update_schema(const char *id, struct l_queue *schema_list)
{
json_object *jobj_schema;
const char *json_str;
char *json_str;
int result;

jobj_schema = parser_schema_create_object(id, schema_list);
if (!jobj_schema)
json_str = parser_schema_create_object(id, schema_list);
if (!json_str)
return KNOT_ERR_CLOUD_FAILURE;

json_str = json_object_to_json_string(jobj_schema);

/**
* Exchange
* Type: Direct
Expand All @@ -579,7 +483,7 @@ int knot_cloud_update_schema(const char *id, struct l_queue *schema_list)
if (result < 0)
result = KNOT_ERR_CLOUD_FAILURE;

json_object_put(jobj_schema);
l_free(json_str);

return result;
}
Expand Down Expand Up @@ -642,17 +546,14 @@ int knot_cloud_publish_data(const char *id, uint8_t sensor_id,
uint8_t value_type, const knot_value_type *value,
uint8_t kval_len)
{
json_object *jobj_data;
const char *json_str;
char *json_str;
int result;

jobj_data = parser_data_create_object(id, sensor_id, value_type, value,
json_str = parser_data_create_object(id, sensor_id, value_type, value,
kval_len);
if (!jobj_data)
if (!json_str)
return KNOT_ERR_CLOUD_FAILURE;

json_str = json_object_to_json_string(jobj_data);

/**
* Exchange
* Type: Fanout
Expand All @@ -670,7 +571,7 @@ int knot_cloud_publish_data(const char *id, uint8_t sensor_id,
if (result < 0)
result = KNOT_ERR_CLOUD_FAILURE;

json_object_put(jobj_data);
l_free(json_str);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/knot_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct knot_cloud_msg {
MSG_TYPES_LENGTH
} type;
union {
const char *token; // used when type is REGISTER
char *token; // used when type is REGISTER
struct l_queue *list; // used when type is UPDATE/REQUEST/LIST
};
};
Expand Down
Loading

0 comments on commit 41c6a15

Please sign in to comment.