Esempio n. 1
0
static void handle_outbound(struct aura_node *node, struct aura_object *o, struct aura_buffer *buf)
{
	int ret = -EIO;

	if (OPCODE("export")) {
		int gpio = aura_buffer_get_u32(buf);
		slog(4, SLOG_DEBUG, "gpio: export %d", gpio);
		ret = gpio_export(gpio);
	} else if (OPCODE("write")) {
		int gpio = aura_buffer_get_u32(buf);
		int value = aura_buffer_get_u32(buf);
		slog(4, SLOG_DEBUG, "gpio: write gpio %d value %d", gpio, value);
		ret = gpio_write(gpio, value);
	} else if (OPCODE("in")) {
		int gpio = aura_buffer_get_u32(buf);
		ret = gpio_in(gpio);
	} else if (OPCODE("out")) {
		int gpio = aura_buffer_get_u32(buf);
		ret = gpio_out(gpio);
	} else if (OPCODE("read")) {
		int gpio = aura_buffer_get_u32(buf);
		ret = gpio_read(gpio, &gpio);
		aura_buffer_rewind(buf);
		aura_buffer_put_u32(buf, gpio);
	}
	slog(0, SLOG_DEBUG, "gpio ret = %d", ret);
	if (ret) {
		aura_call_fail(node, o);
		return;
	}
	aura_queue_buffer(&node->inbound_buffers, buf);
}
Esempio n. 2
0
static struct aura_buffer *lua_to_buffer(lua_State *L, struct aura_node *node, int stackpos, struct aura_object *o)
{
	int i;
	struct aura_buffer *buf;
	const char *fmt;

	fmt = o->arg_fmt;
	if (lua_gettop(L) - stackpos + 1 != o->num_args) {
		slog(0, SLOG_ERROR, "Invalid argument count for %s: %d / %d",
		     o->name, lua_gettop(L) - stackpos, o->num_args);
		return NULL;
	}

	buf = aura_buffer_request(node, o->arglen);
	if (!buf) {
		slog(0, SLOG_ERROR, "Epic fail during buffer allocation");
		return NULL;
	}

	/* Let's serialize the data, arguments are on the stack,
	 * Starting from #3.
	 */

	for (i = stackpos; i <= lua_gettop(L); i++) {
		double tmp;

		switch (*fmt++) {
		case URPC_U8:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_u8(buf, tmp);
			break;
		case URPC_S8:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_s8(buf, tmp);
			break;
		case URPC_U16:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_u16(buf, (uint16_t)tmp);
			break;
		case URPC_S16:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_s16(buf, tmp);
			break;
		case URPC_U32:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_u32(buf, tmp);
			break;
		case URPC_S32:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_s32(buf, tmp);
			break;
		case URPC_S64:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_s64(buf, tmp);
			break;
		case URPC_U64:
			tmp = lua_tonumber(L, i);
			aura_buffer_put_u64(buf, tmp);
			break;

		/* Binary is the tricky part. String or usata? */
		case URPC_BIN:
		{
			const char *srcbuf = NULL;
			int len = 0;
			int blen;
			if (lua_isstring(L, i)) {
				srcbuf = lua_tostring(L, i);
				len = strlen(srcbuf);
			} else if (lua_isuserdata(L, i)) {
				srcbuf = lua_touserdata(L, i);
			}

			blen = atoi(fmt);

			if (blen == 0) {
				slog(0, SLOG_ERROR, "Internal serilizer bug processing: %s", fmt);
				goto err;
			}

			if (!srcbuf) {
				slog(0, SLOG_ERROR, "Internal bug fetching src pointer");
				goto err;
			}

			if (blen < len)
				len = blen;

			aura_buffer_put_bin(buf, srcbuf, len);

			while (*fmt && (*fmt++ != '.'));

			break;
		}
		default:
			BUG(node, "Unknown token: %c\n", *(--fmt));
			break;
		}
	}

	return buf;
err:
	aura_buffer_release(buf);
	return NULL;
}