/* status = write_usb_msg(msg,[timeout]) writes a message to the CHDK ptp interface msg may be nil, boolean, number, string or table (table has some restrictions, will be converted to string) returns true if the message was queued successfully, otherwise false if timeout is set and not zero, wait until message is written or timeout expires NOTE strings will not include a terminating NULL, must be handled by recipient */ static int luaCB_write_usb_msg( lua_State* L ) { ptp_script_msg *msg; int timeout = luaL_optnumber( L, 2, 0 ); // TODO would it be better to either ignore this or return nil ? // a write_usb_msg(function_which_returns_no_value()) is an error in this case // replacing with nil might be more luaish if(lua_gettop(L) < 1) { return luaL_error(L,"missing argument"); } msg=lua_create_usb_msg(L,1,PTP_CHDK_S_MSGTYPE_USER); // for user messages, trying to create a message from an incompatible type throws an error if(msg->subtype == PTP_CHDK_TYPE_UNSUPPORTED) { free(msg); return luaL_error(L,"unsupported type"); } if(!msg) { return luaL_error(L,"failed to create message"); } if(timeout) { action_push(timeout); action_push((int)msg); action_push(AS_SCRIPT_WRITE_USB_MSG); return lua_yield( L, 0 ); } lua_pushboolean(L,ptp_script_write_msg(msg)); return 1; }
// convenience function write an error message int ptp_script_write_error_msg(unsigned errtype, const char *err) { if(script_msg_q_full(&msg_q_out)) { return 0; } ptp_script_msg *msg = ptp_script_create_msg(PTP_CHDK_S_MSGTYPE_ERR,errtype,strlen(err),err); if(!msg) { return 0; } return ptp_script_write_msg(msg); }
// TODO more stuff from script.c should be moved here void lua_script_finish(lua_State *L) { #ifdef CAM_CHDK_PTP if(lua_script_is_ptp) { // send all return values as RET messages int i,end = lua_gettop(L); for(i=1; i<=end; i++) { // if the queue is full return values will be silently discarded // incompatible types will be returned as TYPE_UNSUPPORTED to preserve expected number and order of return values ptp_script_write_msg(lua_create_usb_msg(L,i,PTP_CHDK_S_MSGTYPE_RET)); } } #endif }