return g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id));
}
+static int get_resource_id_and_attr_data(struct request_client *client, char *args,
+ int *id, u_int64_t *attr_data)
+{
+ if (!client || !args || !id || !attr_data)
+ return -ENOENT;
+
+ /**
+ * Format of REQUEST_GET_VALUE_UINT args:
+ * Format of REQUEST_IS_RESOURCE_ATTR_SUPPORTED args:
+ * Format of REQUEST_GET_VALUE_INT args:
+ * Format of REQUEST_GET_VALUE_INT64 args:
+ * Format of REQUEST_GET_VALUE_UINT args:
+ * Format of REQUEST_GET_VALUE_UINT64 args:
+ * Format of REQUEST_GET_VALUE_DOUBLE args:
+ * Format of REQUEST_GET_VALUE_STRING args:
+ * Format of REQUEST_GET_VALUE_ARRAY args:
+ * - <RESOURCE_ID:RESOURCE_ATTR_ID>
+ * Format of REQUEST_SET_RESOURCE_ATTR and REQUEST_UNSET_RESOURCE_ATTR args:
+ * - <RESOURCE_ID:INTEREST_MASK>
+ */
+ if (sscanf(args, "%d$%"PRIu64, id, attr_data) < 2)
+ return -EINVAL;
+
+ return 0;
+}
+
static int handle_request_create_resource(struct request_client *client, char *args)
{
struct resource *res;
return -ENOENT;
}
- /**
- * Format of REQUEST_SET_RESOURCE_ATTR and REQUEST_UNSET_RESOURCE_ATTR args:
- * - <RESOURCE_ID:INTEREST_MASK>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &interest_masks) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &interest_masks);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
static int handle_request_is_resource_attr_supported(struct request_client *client, char *args, bool *supported)
{
struct resource *res;
- int resource_id;
+ int resource_id, ret;
u_int64_t attr_id;
if (!client || !args) {
return -ENOENT;
}
- /**
- * Format of REQUEST_IS_RESOURCE_ATTR_SUPPORTED args:
- * - <RESOURCE_ID:RESOURCE_ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64"", &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_INT args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
- client->socket_fd);
- return -EINVAL;
+ client->socket_fd);
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_INT64 args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_UINT args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_UINT64 args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_DOUBLE args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_INT args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);
return -ENOENT;
}
- /**
- * Format of REQUEST_GET_VALUE_ARRAY args:
- * - <RESOURCE_ID:ATTR_ID>
- */
- if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) {
+ ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id);
+ if (ret < 0) {
_E("failed to get resource and attribute id, client(%d)\n",
client->socket_fd);
- return -EINVAL;
+ return ret;
}
res = get_resource_by_id(client, resource_id);