Example #1
0
error handle::write(uint32_t addr, const void *buf, size_t& sz, bool atomic)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = m_dev->get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* ensure valid status */
    error err = status();
    if(err != error::SUCCESS)
        return err;
    /* split transfer as needed */
    size_t cnt = 0;
    const uint8_t *bufp = (uint8_t *)buf;
    while(sz > 0)
    {
        size_t xfer = std::min(sz, get_buffer_size());
        err = write_dev(addr, buf, xfer, atomic);
        if(err != error::SUCCESS)
            return err;
        sz -= xfer;
        bufp += xfer;
        addr += xfer;
        cnt += xfer;
    }
    sz = cnt;
    return error::SUCCESS;
}
Example #2
0
static VALUE stream_write(VALUE self, VALUE buffer)
{
    PaError err = Pa_WriteStream(stream, get_samples(buffer), get_buffer_size(buffer));
    raise_if_error(err, ERROR_WRITING_STREAM);

    return Qtrue;
}
JNIEXPORT jint JNICALL Java_ism_monagent_EMListener_get_1buffer_1size
  (JNIEnv *, jobject)
{
    int size = get_buffer_size();
    TRACE("get_buffer_size()=" << size);
    return size;
}
void SoundOutput_DirectSound::create_sound_buffer()
{
	WAVEFORMATEXTENSIBLE wave_format;
	wave_format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
	wave_format.Format.nChannels = 2;
	wave_format.Format.nSamplesPerSec = mixing_frequency;
	wave_format.Format.nBlockAlign = get_bytes_per_sample();
	wave_format.Format.nAvgBytesPerSec = wave_format.Format.nSamplesPerSec * wave_format.Format.nBlockAlign;
	wave_format.Format.wBitsPerSample = sizeof(float)*8;
	wave_format.Format.cbSize = 22;
	wave_format.Samples.wValidBitsPerSample = wave_format.Format.wBitsPerSample;
	wave_format.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
	wave_format.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;

	DSBUFFERDESC desc;
	desc.dwSize = sizeof(DSBUFFERDESC); 
	desc.dwFlags = DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2;
	desc.dwBufferBytes = get_buffer_size();
	desc.dwReserved = 0;
	desc.lpwfxFormat = &wave_format.Format; 
	desc.guid3DAlgorithm = GUID_NULL;

	HRESULT err = directsound->CreateSoundBuffer(&desc, &soundbuffer, NULL);
	if (FAILED(err))
		throw Exception("IDirectSound8::CreateSoundBuffer failed");
}
Example #5
0
static void
selection_buffer_read (SelectionBuffer *buffer)
{
  selection_buffer_ref (buffer);
  g_input_stream_read_bytes_async (buffer->stream, get_buffer_size(),
                                   G_PRIORITY_DEFAULT,
                                   buffer->cancellable, selection_buffer_read_cb,
                                   buffer);
}
Example #6
0
static unsigned int get_timeout(struct dev_context *devc)
{
	size_t total_size;
	unsigned int timeout;

	total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
	timeout = total_size / bytes_per_ms(devc);
	return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
}
Example #7
0
void initbuffer(int buffer_id)
{
  get_buffer(buffer_id)        = malloc_good( DEFAULT_BUFFER_SIZE + 1 );
  get_buffer_size(buffer_id)   = DEFAULT_BUFFER_SIZE;
  get_buffer_filter(buffer_id) = BUFFER_FILTER_NONE;

  clearbuffer(buffer_id);
  setbuffernull(buffer_id);
}
Example #8
0
void resizebuffer(int buffer_id, int newsize)
{
  get_buffer(buffer_id) = realloc_good( get_buffer(buffer_id), newsize + 1 );
  get_buffer_size(buffer_id) = newsize;
  get_buffer_filter(buffer_id) = BUFFER_FILTER_NONE;

  clearbuffer(buffer_id);
  setbuffernull(buffer_id);
}
Example #9
0
/**
 * We only implement this function as a mechanism to check if the
 * framebuffer size has changed (and update corresponding state).
 */
static void
viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
{
   GLuint newWidth, newHeight;
   GLframebuffer *buffer;

   buffer = ctx->WinSysDrawBuffer;
   get_buffer_size( buffer, &newWidth, &newHeight );
   if (buffer->Width != newWidth || buffer->Height != newHeight) {
      _mesa_resize_framebuffer(ctx, buffer, newWidth, newHeight );
   }

   buffer = ctx->WinSysReadBuffer;
   get_buffer_size( buffer, &newWidth, &newHeight );
   if (buffer->Width != newWidth || buffer->Height != newHeight) {
      _mesa_resize_framebuffer(ctx, buffer, newWidth, newHeight );
   }
}
Example #10
0
/*
 * start recording
 */
static int_fast32_t pulse_start_recording(struct pulse_data *data)
{
	if (pulse_get_server_info(pulse_server_info, (void *) data) < 0) {
		blog(LOG_ERROR, "pulse-input: Unable to get server info !");
		return -1;
	}

	pa_sample_spec spec;
	spec.format   = data->format;
	spec.rate     = data->samples_per_sec;
	spec.channels = data->channels;

	if (!pa_sample_spec_valid(&spec)) {
		blog(LOG_ERROR, "pulse-input: Sample spec is not valid");
		return -1;
	}

	data->bytes_per_frame = pa_frame_size(&spec);
	blog(LOG_DEBUG, "pulse-input: %u bytes per frame",
	     (unsigned int) data->bytes_per_frame);

	data->stream = pulse_stream_new(obs_source_getname(data->source),
		&spec, NULL);
	if (!data->stream) {
		blog(LOG_ERROR, "pulse-input: Unable to create stream");
		return -1;
	}

	pulse_lock();
	pa_stream_set_read_callback(data->stream, pulse_stream_read,
		(void *) data);
	pulse_unlock();

	pa_buffer_attr attr;
	attr.fragsize  = get_buffer_size(data, 250);
	attr.maxlength = (uint32_t) -1;
	attr.minreq    = (uint32_t) -1;
	attr.prebuf    = (uint32_t) -1;
	attr.tlength   = (uint32_t) -1;

	pa_stream_flags_t flags =
		PA_STREAM_INTERPOLATE_TIMING
		| PA_STREAM_AUTO_TIMING_UPDATE
		| PA_STREAM_ADJUST_LATENCY;

	pulse_lock();
	int_fast32_t ret = pa_stream_connect_record(data->stream, data->device,
		&attr, flags);
	pulse_unlock();
	if (ret < 0) {
		blog(LOG_ERROR, "pulse-input: Unable to connect to stream");
		return -1;
	}

	blog(LOG_DEBUG, "pulse-input: Recording started");
	return 0;
}
Example #11
0
int main (int argc, char *argv[])
{
	int write_character = -1;

 g_prog_mode  = PRG_SENDER_MODE;
	
    /*check parameters */
	if ((buffer_size = get_buffer_size(argc, argv)) == RETURN_ERROR)
	{
		return EXIT_FAILURE;
	}
    /*create semaphore and ringbuffer*/
	if (create_environment() == RETURN_ERROR)
	{
		return EXIT_FAILURE;
	}
    /*write ringbuffer*/
	do
	{
	    /*read stdin input*/
		write_character = fgetc(stdin);

        /*decrement semaphore*/
		if (my_sem_wait() == RETURN_ERROR)
		{
			return EXIT_FAILURE;
		}
        /*write ringbuffer*/
		write_char_to_buffer(write_character);

        /*increment semaphore*/
		if (my_sem_post() == RETURN_ERROR)
		{
			return EXIT_FAILURE;
		}
	} while (write_character != EOF);

    /*check for errors in input of stdin*/
	if (ferror(stdin))
	{
		fprintf(stderr, "%s: %s%s\n", g_program_name, "Error while reading input from \"stdin\".", strerror(errno));

		destroy_environment(PRG_ERROR);
		return EXIT_FAILURE;
	}
    /*destroy semaphore and ringbuffer*/
	if (destroy_environment(PRG_SUCCESS) == RETURN_ERROR)
	{
		return EXIT_FAILURE;
	}
	else
	{
		return EXIT_SUCCESS;
	}
}
Example #12
0
t_size reader_membuffer_base::read(void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
	p_abort.check_e();
	t_size max = get_buffer_size();
	if (max < m_offset) uBugCheck();
	max -= m_offset;
	t_size delta = p_bytes;
	if (delta > max) delta = max;
	memcpy(p_buffer,(char*)get_buffer() + m_offset,delta);
	m_offset += delta;
	return delta;
}
Example #13
0
static unsigned int get_number_of_transfers(struct dev_context *devc)
{
	unsigned int n;

	/* Total buffer size should be able to hold about 500ms of data. */
	n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);

	if (n > NUM_SIMUL_TRANSFERS)
		return NUM_SIMUL_TRANSFERS;

	return n;
}
Example #14
0
/*
 * Create a new pulse audio stream and connect to it
 *
 * Return a negative value on error
 */
static int pulse_connect_stream(struct pulse_data *data)
{
	pa_sample_spec spec;
	spec.format = data->format;
	spec.rate = data->samples_per_sec;
	spec.channels = get_audio_channels(data->speakers);

	if (!pa_sample_spec_valid(&spec)) {
		blog(LOG_ERROR, "pulse-input: Sample spec is not valid");
		return -1;
	}

	data->bytes_per_frame = pa_frame_size(&spec);
	blog(LOG_DEBUG, "pulse-input: %u bytes per frame",
	     (unsigned int) data->bytes_per_frame);

	pa_buffer_attr attr;
	attr.fragsize = get_buffer_size(data, 250);
	attr.maxlength = (uint32_t) -1;
	attr.minreq = (uint32_t) -1;
	attr.prebuf = (uint32_t) -1;
	attr.tlength = (uint32_t) -1;

	data->stream = pa_stream_new_with_proplist(data->context,
		obs_source_getname(data->source), &spec, NULL, data->props);
	if (!data->stream) {
		blog(LOG_ERROR, "pulse-input: Unable to create stream");
		return -1;
	}
	pa_stream_flags_t flags =
		PA_STREAM_INTERPOLATE_TIMING
		| PA_STREAM_AUTO_TIMING_UPDATE
		| PA_STREAM_ADJUST_LATENCY;
	if (pa_stream_connect_record(data->stream, NULL, &attr, flags) < 0) {
		blog(LOG_ERROR, "pulse-input: Unable to connect to stream");
		return -1;
	}

	for (;;) {
		pulse_iterate(data);
		pa_stream_state_t state = pa_stream_get_state(data->stream);
		if (state == PA_STREAM_READY) {
			blog(LOG_DEBUG, "pulse-input: Stream ready");
			break;
		}
		if (!PA_STREAM_IS_GOOD(state)) {
			blog(LOG_ERROR, "pulse-input: Stream connect failed");
			return -1;
		}
	}

	return 0;
}
Example #15
0
static int init_buffer(output_buffer_t *buffer, int string_length)
{
    /* PRE:  The given pointer is valid and legal. The given string_length
             is >= 0. */
    /* POST: Initialises the output buffer to contain sufficient memory on the
             heap for a string of the given length. */

    int buffer_size = get_buffer_size(string_length);
    buffer->num_lines = 0;
    buffer->morse_chars = malloc(sizeof(morse_type_t) * buffer_size);

    return EXIT_SUCCESS;
}
void SoundOutput_DirectSound::verify_sound_buffer_capabilities()
{
	DSBCAPS caps;
	memset(&caps, 0, sizeof(DSBCAPS));
	caps.dwSize = sizeof(DSBCAPS);
	
	HRESULT err = soundbuffer->GetCaps(&caps);
	if (FAILED(err))
		throw Exception("Could not retrieve direct sound buffer capabilities.");

	if (caps.dwBufferBytes != get_buffer_size())
		throw Exception("Direct sound buffer size does not match our request.");
}
Example #17
0
// Reads another chunk of a file to a buffer
// @param file a pointer to a file variable
static void manage_file_buffer(file_info *file) {
    
    // Read another chunk of the file to the file buffer
    if (file->file_count % TRANSFER_THRESHOLD_PACKETS == 0) {
        
        free(file->file_buffer);
        file->file_buffer = NULL;
        
        file->file_buffer = read_from_file(file->path, file->bytes_sent, get_buffer_size(file), &file->buffer_length);
        file->pos = file->file_buffer;
        
    } //end if
    
} //end managa_file_buffer
Example #18
0
size_t SCE_RGetBufferPoolSize (const SCE_RBufferPool *pool)
{
    size_t i, j, n, size = 0;
    SCEuint *ptr = NULL;

    for (i = 0; i < 32; i++) {
        n = SCE_Array_GetSize (&pool->buffers[i]) / sizeof (SCEuint);
        ptr = SCE_Array_Get (&pool->buffers[i]);
        for (j = 0; j < n; j++)
            size += get_buffer_size (pool->target, ptr[j]);
    }

    return size;
}
bool nas_qos_buffer_profile::push_leaf_attr_to_npu (nas_attr_id_t attr_id,
                                           npu_id_t npu_id)
{
    t_std_error rc = STD_ERR_OK;

    EV_LOG_TRACE(ev_log_t_QOS, 3, "QOS", "Modifying npu: %d, attr_id %d",
                    npu_id, attr_id);

    ndi_qos_buffer_profile_struct_t cfg= {0};

    switch (attr_id) {
    case BASE_QOS_BUFFER_PROFILE_POOL_ID:
        cfg.pool_id = nas2ndi_pool_id(get_buffer_pool_id(), npu_id);
        break;

    case BASE_QOS_BUFFER_PROFILE_BUFFER_SIZE:
        cfg.buffer_size = get_buffer_size();
        break;

    case BASE_QOS_BUFFER_PROFILE_SHARED_DYNAMIC_THRESHOLD:
        cfg.shared_dynamic_th = get_shared_dynamic_th();
        break;

    case BASE_QOS_BUFFER_PROFILE_SHARED_STATIC_THRESHOLD:
        cfg.shared_static_th = get_shared_static_th();
        break;

    case BASE_QOS_BUFFER_PROFILE_XOFF_THRESHOLD:
        cfg.xoff_th = get_xoff_th();
        break;

    case BASE_QOS_BUFFER_PROFILE_XON_THRESHOLD:
        cfg.xon_th = get_xon_th();
        break;

    default:
        STD_ASSERT (0); //non-modifiable object
    }

    rc = ndi_qos_set_buffer_profile_attr(npu_id,
                                   ndi_obj_id(npu_id),
                                   (BASE_QOS_BUFFER_PROFILE_t)attr_id,
                                   &cfg);
    if (rc != STD_ERR_OK) {
            throw nas::base_exception {rc, __PRETTY_FUNCTION__,
                "NDI attribute Set Failed"};
    }

    return true;
}
Example #20
0
static char *
make_screen_pos (Window wp)
{
  bool tv = window_top_visible (wp);
  bool bv = window_bottom_visible (wp);

  if (tv && bv)
    return xasprintf ("All");
  else if (tv)
    return xasprintf ("Top");
  else if (bv)
    return xasprintf ("Bot");
  else
    return xasprintf ("%2d%%",
                      (int) ((float) 100.0 * window_o (wp) / get_buffer_size (get_window_bp (wp))));
}
void SoundOutput_DirectSound::clear_sound_buffer()
{
	DWORD size1 = 0, size2 = 0;
	void *ptr1 = 0;
	void *ptr2 = 0;
	HRESULT err = soundbuffer->Lock(0, get_buffer_size(), &ptr1, &size1, &ptr2, &size2, 0);
	if (FAILED(err))
		throw Exception("Unable to lock DirectSound buffer.");

	memset(ptr1, 0, size1);
	if (ptr2)
		memset(ptr2, 0, size2);

	err = soundbuffer->Unlock(ptr1, size1, ptr2, size2);
	if (FAILED(err))
		throw Exception("Could not unlock DirectSound buffer.");
}
Example #22
0
void copybuffer(int buffer_id_1, int buffer_id_2)
{
  printf("copybuffer: copying %i to %i...\n", buffer_id_1, buffer_id_2);

  if (get_buffer_size(buffer_id_2) < get_buffer_real_size(buffer_id_1))
  {
    printf("copybuffer: must resize buffer %i to %i bytes\n", 
                            buffer_id_2, get_buffer_real_size(buffer_id_1));
    resizebuffer(buffer_id_2, get_buffer_real_size(buffer_id_1));
  }

  clearbuffer(buffer_id_2);
  memcpy(get_buffer(buffer_id_2), get_buffer(buffer_id_1),
                            get_buffer_real_size(buffer_id_1));

  *(get_buffer(buffer_id_2) + get_buffer_real_size(buffer_id_1)) = 0;
  setbuffernull(buffer_id_2);
}
Example #23
0
static void
draw_window (size_t topline, Window wp)
{
  size_t i, o;
  Region r;
  int highlight = calculate_highlight_region (wp, &r);

  /* Find the first line to display on the first screen line. */
  for (o = buffer_start_of_line (get_window_bp (wp), window_o (wp)), i = get_window_topdelta (wp);
       i > 0 && o > 0;
       assert ((o = buffer_prev_line (get_window_bp (wp), o)) != SIZE_MAX), --i)
    ;

  /* Draw the window lines. */
  size_t cur_tab_width = tab_width (get_window_bp (wp));
  for (i = topline; i < get_window_eheight (wp) + topline; ++i)
    {
      /* Clear the line. */
      term_move (i, 0);
      term_clrtoeol ();

      /* If at the end of the buffer, don't write any text. */
      if (o == SIZE_MAX)
        continue;

      draw_line (i, get_window_start_column (wp), wp, o, r, highlight, cur_tab_width);

      if (get_window_start_column (wp) > 0)
        {
          term_move (i, 0);
          term_addstr("$");
        }

      o = buffer_next_line (get_window_bp (wp), o);
    }

  set_window_all_displayed (wp, o >= get_buffer_size (get_window_bp (wp)));

  /* Draw the status line only if there is available space after the
     buffer text space. */
  if (get_window_fheight (wp) - get_window_eheight (wp) > 0)
    draw_status_line (topline + get_window_eheight (wp), wp);
}
Example #24
0
int serve_title(int fd,off64_t range_start, off64_t range_end){
	
	
	/* get buffer size*/
	int intBufSizeBytes;
	int intRetVal=get_buffer_size(dd_disc,&intBufSizeBytes);
	if(ERR_OK!=intRetVal) return intRetVal;

        
	
	if(range_end+1>dd_disc->current_title_size || 0==range_end)
		range_end=dd_disc->current_title_size-1;

	if(range_start<0)
		range_start=dd_disc->current_title_size+range_start-1>=0 ? dd_disc->current_title_size+range_start-1 : 0;

	if(range_start>dd_disc->current_title_size-1)
		return 0;

	off64_t offset=range_start;
	off64_t bytes_total=range_end-range_start+1;

        unsigned char* tmpbuf=malloc(intBufSizeBytes*sizeof(unsigned char));
	off64_t bytes_read=0;
	off64_t bytes_to_read=0;
	while(bytes_total>0 && 0==abort_requested){
		bytes_to_read=(bytes_total>intBufSizeBytes)? intBufSizeBytes: bytes_total;	
		bytes_read=read_bytes( dd_disc, offset, bytes_to_read, tmpbuf );
		if(bytes_read<0){
		        free(tmpbuf);
			return ERR_READ;
		}
		
		if(write(fd,tmpbuf,bytes_read)!=(ssize_t)bytes_read){
		        free(tmpbuf);
			return ERR_FILEWRITE;
		}
		bytes_total-=bytes_read;
		offset+=bytes_read;
	}
	free(tmpbuf);
	return ERR_OK;
}
Example #25
0
SR_PRIV int fx2lafw_start_acquisition(const struct sr_dev_inst *sdi)
{
	struct sr_dev_driver *di;
	struct drv_context *drvc;
	struct dev_context *devc;
	int timeout, ret;
	size_t size;

	di = sdi->driver;
	drvc = di->context;
	devc = sdi->priv;

	devc->ctx = drvc->sr_ctx;
	devc->sent_samples = 0;
	devc->empty_transfer_count = 0;
	devc->acq_aborted = FALSE;

	if (configure_channels(sdi) != SR_OK) {
		sr_err("Failed to configure channels.");
		return SR_ERR;
	}

	timeout = get_timeout(devc);
	usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);

	size = get_buffer_size(devc);
	/* Prepare for analog sampling. */
	if (g_slist_length(devc->enabled_analog_channels) > 0) {
		/* We need a buffer half the size of a transfer. */
		devc->logic_buffer = g_try_malloc(size / 2);
		devc->analog_buffer = g_try_malloc(
			sizeof(float) * size / 2);
	}
	start_transfers(sdi);
	if ((ret = command_start_acquisition(sdi)) != SR_OK) {
		fx2lafw_abort_acquisition(devc);
		return ret;
	}

	return SR_OK;
}
Example #26
0
void file2buffer(char *name, int buffer_id)
{
  int i, original_size;
  char *buf;
  FILE *file;
  struct stat filestats;

  clearbuffer(buffer_id);

  /* Grab file stats */
  if (stat(name, &filestats) == -1)
  {
    printf("file2buffer: stat failed: %s\n", strerror(errno));
    return;
  }

  /* Check if it's a regular file */
  if (!S_ISREG(filestats.st_mode))
  {
    printf("file2buffer: not a regular file; failed\n");
    return;
  }

  file = fopen(name, "r");
  if (file == NULL)
  {
    printf("file2buffer: fopen failed: %s\n", strerror(errno));
    return;
  }
  
  flock(fileno(file), LOCK_SH);

  /* Save original size incase we need to bail out */
  original_size = get_buffer_size(buffer_id);

  if (filestats.st_size > get_buffer_size(buffer_id))
  {
    printf("file2buffer: expanding buffer %i to accommodate file's %li bytes\n",
                    buffer_id, filestats.st_size);
    resizebuffer(buffer_id, filestats.st_size + 1);
  }

  i = 0;
  buf = get_buffer(buffer_id);

  while (feof(file) == 0)
  {
    *(buf + i) = fgetc(file);

    /* Detect error, convert to a null, and finish */
    if (*(buf + i) == -1)
    {
      *(buf + i) = 0;
      break;
    }
    else if (!ASCII_CH(*(buf + i)) && !XSPACE_CH(*(buf + i)))
    {
      /* trap non ascii stuff, including a null */
      printf("file2buffer: error - file is not ASCII, "
                "unknown character %2x at byte %i\n", 
                (unsigned char) *(buf + i), i);

      /* bail out, I guess. */
      printf("file2buffer: bailing out: emptying buffer\n");
      clearbuffer(buffer_id);

      /* return buffer to original size, if needed */
      if (original_size != get_buffer_size(buffer_id))
      {
        printf("file2buffer: returning buffer %i to original size %i\n",
                     buffer_id, original_size);
        resizebuffer(buffer_id, original_size);
      }

      flock(fileno(file), LOCK_UN);
      fclose(file);
      return;
    }

    i++;
  }

  *(buf + i) = 0;
  setbuffernull(buffer_id);

  printf("file2buffer: loaded %i bytes into buffer %i\n", i - 1, buffer_id);

  flock(fileno(file), LOCK_UN);
  fclose(file);
}
Example #27
0
void clearbuffer(int buffer_id)
{
  memset(get_buffer(buffer_id), 0, get_buffer_size(buffer_id));
  setbuffernull(buffer_id);
}
Example #28
0
void destroybuffer(int buffer_id)
{
  free(get_buffer(buffer_id));
  get_buffer_size(buffer_id)   = 0;
  get_buffer_filter(buffer_id) = BUFFER_FILTER_NONE;
}
Example #29
0
/**
 * @brief Receives data from USB0
 *
 * This function receives data from USB0
 *
 * @param data pointer to the buffer where the received data will be stored
 * @param max_length maximum length of data to be received
 *
 * @return actual number of bytes received
 */
uint8_t sio_usb_0_rx(uint8_t *data, uint8_t max_length)
{
    uint8_t data_received = 0;
    uint8_t size = 0;

    if (usb_0_buffer.rx_count == 0)
    {
        /* usb receive buffer is empty. */
        return 0;
    }

    /* The receive interrupt is disabled. */
    ENTER_CRITICAL_REGION();

    if (USB_RX_BUF_MAX_SIZE <= usb_0_buffer.rx_count)
    {
        /*
         * Bytes between head and tail are overwritten by new data.
         * The oldest data in buffer is the one to which the tail is
         * pointing. So reading operation should start from the tail.
         */
        usb_0_buffer.rx_buf_head = usb_0_buffer.rx_buf_tail;

        /*
         * This is a buffer overflow case. But still only bytes equivalent to
         * full buffer size are useful.
         */
        usb_0_buffer.rx_count = USB_RX_BUF_MAX_SIZE;

        /* Bytes received is more than or equal to buffer. */
        if (USB_RX_BUF_MAX_SIZE <= max_length)
        {
            /*
             * Requested receive length (max_length) is more than the
             * max size of receive buffer, but at max the full
             * buffer can be read.
             */
            max_length = USB_RX_BUF_MAX_SIZE;
        }
    }
    else
    {
        /* Bytes received is less than receive buffer maximum length. */
        if (max_length > usb_0_buffer.rx_count)
        {
            /*
             * Requested receive length (max_length) is more than the data
             * present in receive buffer. Hence only the number of bytes
             * present in receive buffer are read.
             */
            max_length = usb_0_buffer.rx_count;
        }
    }

    data_received = max_length;

    while (max_length > 0)
    {
        /* Start to copy from head. */
        *data = usb_0_buffer.rx_buf[usb_0_buffer.rx_buf_head];
        usb_0_buffer.rx_buf_head++;
        usb_0_buffer.rx_count--;
        data++;
        max_length--;
        if ((USB_RX_BUF_MAX_SIZE) == usb_0_buffer.rx_buf_head)
        {
            usb_0_buffer.rx_buf_head = 0;
        }
    }

    size = get_buffer_size( usb_0_buffer.rx_buf_head,
                            usb_0_buffer.rx_buf_tail,
                            USB_RX_BUF_MAX_SIZE);

    /* The receive interrupt is enabled back. */
    LEAVE_CRITICAL_REGION();

    if (size > 60)
    {
        CDCDSerialDriver_Read(  usb_0_rx_temp_buf,
                                sizeof(usb_0_rx_temp_buf),
                                (TransferCallback) usb0_rx_complete_handler,
                                0);

        usb_0_rx_disable_flag = 0;
    }

    return data_received;
}
Example #30
0
void reader_membuffer_base::seek(t_filesize position,abort_callback & p_abort) {
	p_abort.check_e();
	t_filesize max = get_buffer_size();
	if (position == filesize_invalid || position > max) throw exception_io_seek_out_of_range();
	m_offset = (t_size)position;
}