/** * \brief Check for interrupt without wait * \param port a GPPort * \param data a pointer to an allocated buffer * \param size the number of bytes that should be read * * Reads a specified number of bytes from the inerrupt endpoint * into the supplied buffer. * Function waits 50 miliseconds for data on interrupt endpoint. * * \return a gphoto2 error code **/ int gp_port_check_int_fast (GPPort *port, char *data, int size) { int retval; gp_log (GP_LOG_DATA, __func__, "Reading %i = 0x%x bytes from interrupt endpoint...", size, size); C_PARAMS (port); CHECK_INIT (port); /* Check if we read as many bytes as expected */ CHECK_SUPP (port, "check_int", port->pc->ops->check_int); retval = port->pc->ops->check_int (port, data, size, FAST_TIMEOUT); CHECK_RESULT (retval); #ifdef IGNORE_EMPTY_INTR_READS /* For Canon cameras, we will make lots of reads that will return zero length. Don't bother to log them as errors. */ if (retval != 0 ) #endif LOG_DATA (data, retval, size, "Read ", "from interrupt endpoint (fast):"); return (retval); }
static void TEST_UTIL_Log(void) { /* create */ LOG x_log = LOG_Create(0, 0, 0, 0); LOG_ToFILE(x_log, stdout, 0/*false*/); /* simple logging */ LOG_WRITE(x_log, 0, 0, eLOG_Trace, 0); LOG_Write(x_log, 0, 0, eLOG_Trace, 0, 0, 0, 0, 0, 0); LOG_WRITE(x_log, 0, 0, eLOG_Warning, ""); /* LOG_WRITE(x_log, eLOG_Fatal, "Something fatal"); */ #undef THIS_MODULE #define THIS_MODULE "FooModuleName" LOG_WRITE(x_log, 0, 0, eLOG_Error, "With changed module name"); #undef THIS_FILE #define THIS_FILE "BarFileName" LOG_WRITE(x_log, 0, 0, eLOG_Critical, "With changed module and file name"); #undef THIS_FILE #define THIS_FILE __FILE__ #undef THIS_MODULE #define THIS_MODULE 0 /* data logging */ {{ unsigned char data[300]; size_t i; for (i = 0; i < sizeof(data); i++) { data[i] = (unsigned char) (i % 256); } LOG_DATA(x_log, 0, 0, eLOG_Note, data, sizeof(data), "Data logging test"); }} /* delete */ verify(LOG_Delete(x_log) == 0); }
/** * \brief Check for intterupt. * * \param port a GPPort * \param data a pointer to an allocated buffer * \param size the number of bytes that should be read * * Reads a specified number of bytes from the interrupt endpoint * into the supplied buffer. * Function waits port->timeout miliseconds for data on interrupt endpoint. * * \return a gphoto2 error code **/ int gp_port_check_int (GPPort *port, char *data, int size) { int retval; gp_log (GP_LOG_DATA, __func__, "Reading %i = 0x%x bytes from interrupt endpoint...", size, size); C_PARAMS (port); CHECK_INIT (port); /* Check if we read as many bytes as expected */ CHECK_SUPP (port, "check_int", port->pc->ops->check_int); retval = port->pc->ops->check_int (port, data, size, port->timeout); CHECK_RESULT (retval); LOG_DATA (data, retval, size, "Read ", "from interrupt endpoint:"); return (retval); }
/** * \brief Send a USB control message with input data * * \param port a GPPort * \param request control request code * \param value control value * \param index control index * \param bytes pointer to data * \param size size of the data * * Sends a specific USB interface control command and read associated data. * * \return a gphoto2 error code */ int gp_port_usb_msg_read (GPPort *port, int request, int value, int index, char *bytes, int size) { int retval; gp_log (GP_LOG_DATA, __func__, "Reading message (request=0x%x value=0x%x index=0x%x size=%i=0x%x)...", request, value, index, size, size); C_PARAMS (port); CHECK_INIT (port); CHECK_SUPP (port, "msg_read", port->pc->ops->msg_read); retval = port->pc->ops->msg_read (port, request, value, index, bytes, size); CHECK_RESULT (retval); LOG_DATA (bytes, retval, size, "Read", "USB message (request=0x%x value=0x%x index=0x%x size=%i=0x%x)", request, value, index, size, size); return (retval); }
/** * \brief Read data from port * * \param port a #GPPort * \param data a pointer to an allocated buffer * \param size the number of bytes that should be read * * Reads a specified number of bytes from the port into the supplied buffer. * It returns the number of bytes read or a negative error code. * * \return a gphoto2 error code or the amount of data read **/ int gp_port_read (GPPort *port, char *data, int size) { int retval; gp_log (GP_LOG_DATA, __func__, "Reading %i = 0x%x bytes from port...", size, size); C_PARAMS (port); CHECK_INIT (port); /* Check if we read as many bytes as expected */ CHECK_SUPP (port, "read", port->pc->ops->read); retval = port->pc->ops->read (port, data, size); if (retval < 0) { GP_LOG_E ("Reading %i = 0x%x bytes from port failed: %s (%d)", size, size, gp_port_result_as_string(retval), retval); return retval; } LOG_DATA (data, retval, size, "Read ", "from port:"); return (retval); }
/** * \brief Writes a specified amount of data to a port. * \param port a #GPPort * \param data the data to write to the port * \param size the number of bytes to write to the port * * Writes data to the port. On non-serial ports the amount of data * written is returned (and not just GP_OK). * * \return a negative gphoto2 error code or the amount of data written. **/ int gp_port_write (GPPort *port, const char *data, int size) { int retval; gp_log (GP_LOG_DATA, __func__, "Writing %i = 0x%x bytes to port...", size, size); C_PARAMS (port && data); CHECK_INIT (port); /* Check if we wrote all bytes */ CHECK_SUPP (port, "write", port->pc->ops->write); retval = port->pc->ops->write (port, data, size); if (retval < 0) { GP_LOG_E ("Writing %i = 0x%x bytes to port failed: %s (%d)", size, size, gp_port_result_as_string(retval), retval); return retval; } LOG_DATA (data, retval, size, "Wrote ", "to port:"); return (retval); }
void HttpProxy::handle_async( boost::system::error_code const & ec, Size const & bytes_transferred) { LOG_SECTION(); LOG_DEBUG("[handle_async] (id = %u, status = %s, ec = %s, bytes_transferred = %s)" %id_ % state_str[state_] % ec.message() % bytes_transferred.to_string()); if (watch_state_ == broken) { on_finish(); delete this; return; } if (ec) { if (state_ == receiving_request_head) { error_code ec1; response_.clear_data(); bool block = !get_non_block(ec1); if (block) set_non_block(true, ec1); boost::asio::read(*this, response_.data(), boost::asio::transfer_at_least(4096), ec1); if (block) set_non_block(false, ec1); LOG_DATA(framework::logger::Debug, ("receiving_request_head", response_.data().data())); } on_error(ec); switch (state_) { case receiving_request_head: case transferring_request_data: case sending_response_head: case transferring_response_data: on_finish(); state_ = exiting; if (watch_state_ == watching) { error_code ec1; HttpSocket::cancel(ec1); } else { delete this; } break; case local_processing: if (is_local() && !response_.head().content_length.is_initialized() && bytes_transferred.is_size_t()) { response_.head().content_length.reset(bytes_transferred.get_size_t()); } case preparing: case connectting: case sending_request_head: case receiving_response_head: state_ = sending_response_head; response_error(ec, boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; default: assert(0); break; } return; } switch (state_) { case stopped: state_ = receiving_request_head; response_.head() = HttpResponseHead(); async_read(request_.head(), boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case receiving_request_head: state_ = preparing; if (watch_state_ == watch_stopped && request_.head().content_length.get_value_or(0) == 0) { watch_state_ = watching; async_read_some(boost::asio::null_buffers(), boost::bind(&HttpProxy::handle_watch, this, _1)); } on_receive_request_head( request_.head(), boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case preparing: if (bytes_transferred.get_bool()) { if (!http_to_server_) http_to_server_ = new HttpSocket(get_io_service()); state_ = connectting; http_to_server_->async_connect(request_.head().host.get(), boost::bind(&HttpProxy::handle_async, this, _1, Size())); } else { state_ = transferring_request_data; transfer_request_data( boost::bind(&HttpProxy::handle_async, this, _1, _2)); } break; case connectting: state_ = sending_request_head; http_to_server_->async_write(request_.head(), boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case sending_request_head: state_ = transferring_request_data; transfer_request_data( boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case transferring_request_data: if (is_local()) { on_receive_request_data(transfer_buf_); transfer_buf_.consume(transfer_buf_.size()); } state_ = local_processing; if (watch_state_ == watch_stopped) { watch_state_ = watching; async_read_some(boost::asio::null_buffers(), boost::bind(&HttpProxy::handle_watch, this, _1)); } response_.head().connection = request_.head().connection; local_process( boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case local_processing: if (is_local()) { state_ = receiving_response_head; on_receive_response_head(response_.head()); if (!response_.head().content_length.is_initialized()) { if (bytes_transferred.is_size_t()) response_.head().content_length.reset(bytes_transferred.get_size_t()); else response_.head().connection.reset(http_field::Connection()); } if (!response_.head().connection.is_initialized()) { response_.head().connection.reset(http_field::Connection()); } handle_async(ec, Size()); } else { state_ = receiving_response_head; http_to_server_->async_read(response_.head(), boost::bind(&HttpProxy::handle_async, this, _1, _2)); } break; case receiving_response_head: if (!is_local()) { on_receive_response_head(response_.head()); if (!response_.head().connection.is_initialized()) { response_.head().connection.reset(http_field::Connection()); } if (response_.head().err_msg.empty()) response_.head().err_msg = "OK"; } state_ = sending_response_head; async_write(response_.head(), boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case sending_response_head: state_ = transferring_response_data; transfer_response_data( boost::bind(&HttpProxy::handle_async, this, _1, _2)); break; case transferring_response_data: on_finish(); if (!response_.head().connection || response_.head().connection.get() == http_field::Connection::close) { state_ = exiting; if (watch_state_ != watching) { delete this; } else { error_code ec; shutdown(boost::asio::socket_base::shutdown_send, ec); boost::asio::ip::tcp::socket::cancel(ec); } } else { state_ = stopped; if (watch_state_ != watching) { // restart watch_state_ = watch_stopped; handle_async(ec, Size()); } else { error_code ec; boost::asio::ip::tcp::socket::cancel(ec); } } break; default: assert(0); break; } }