#include #include #include #include #include #include "msg.h" int unmarshal_button_msg(const char *str, struct button_msg *msg) { int status; cJSON *json, *device_id; if (!(json = cJSON_ParseWithLength(str, strlen(str)))) { warnx("error parsing json at char number %d from %s", (int)(cJSON_GetErrorPtr() - str), str); status = -1; goto end; } device_id = cJSON_GetObjectItemCaseSensitive(json, "device_id"); if (!cJSON_IsString(device_id) || !device_id->valuestring) goto invalid_data; if (strlen(device_id->valuestring) != 64) goto invalid_data; memcpy(msg->id, device_id->valuestring, 65); status = 0; goto end; invalid_data: warnx("error: invalid data"); status = -1; end: cJSON_Delete(json); return status; } int unmarshal_brightness_msg(const char *str, struct brightness_msg *msg) { int status; cJSON *json, *device_id, *val; if (!(json = cJSON_ParseWithLength(str, strlen(str)))) { warnx("error parsing json at char number %d from %s", (int)(cJSON_GetErrorPtr() - str), str); status = -1; goto end; } device_id = cJSON_GetObjectItemCaseSensitive(json, "device_id"); if (!cJSON_IsString(device_id) || !device_id->valuestring) goto invalid_data; val = cJSON_GetObjectItemCaseSensitive(json, "val"); if (!cJSON_IsNumber(val)) goto invalid_data; if (strlen(device_id->valuestring) != 64) goto invalid_data; memcpy(msg->id, device_id->valuestring, 65); msg->val = val->valueint; status = 0; goto end; invalid_data: warnx("error: invalid data"); status = -1; end: cJSON_Delete(json); return status; }