All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>
To: Carlos Eduardo de Brito Novaes <carlosnov@domain.hid>
Cc: xenomai@xenomai.org
Subject: Re: [Xenomai-help] Trouble with rtdm timers and events
Date: Thu, 11 Aug 2011 21:34:08 +0200	[thread overview]
Message-ID: <4E442EB0.7030909@domain.hid> (raw)
In-Reply-To: <201108111512.40209.carlosnov@domain.hid>

On 08/11/2011 08:12 PM, Carlos Eduardo de Brito Novaes wrote:
> I really did not understand why nor even what exactly it means to overflow the 
> source area, but I forgot to mention that I commented it out, so the 
> copy_to_user is not being called at all.

It means that sizeof(sample_number) is 40, and neither the zone you are
copying from, neither the zone you are copying to is 40 bytes large
(except for the first call to read).

Also, there is no problem starting a timer in open_nrt and close_nrt
handlers.

So, here is a simple self-contained testcase which does what you are
trying to do. And which works. So, I insist, you should really inspect
your code carefully, the bugs you have do not seem to come from xenomai.

Module code:
#include <rtdm/rtdm_driver.h>

struct device_ctx {
	rtdm_event_t event;
	rtdm_timer_t timer;
};

MODULE_LICENSE("GPL");
MODULE_AUTHOR("gch");

static void test_timer(rtdm_timer_t *timer)
{
	struct device_ctx *ctx = container_of(timer, struct device_ctx, timer);

	rtdm_event_pulse(&ctx->event);
	rtdm_timer_start_in_handler(&ctx->timer,
			      rtdm_clock_read_monotonic() + 100000000,
			      0, RTDM_TIMERMODE_ABSOLUTE);
}

static int test_open(struct rtdm_dev_context *context,
		     rtdm_user_info_t *user_info,
		     int oflags)
{
	struct device_ctx *ctx = (struct device_ctx *)context->dev_private;
	int rc;

	rc = rtdm_timer_init(&ctx->timer, test_timer, "timer");
	if (rc < 0)
		return rc;

	rtdm_event_init(&ctx->event, 0);

	rc = rtdm_timer_start(&ctx->timer,
			      rtdm_clock_read_monotonic() + 100000000,
			      0, RTDM_TIMERMODE_ABSOLUTE);
	if (rc < 0) {
		rtdm_timer_destroy(&ctx->timer);
		rtdm_event_destroy(&ctx->event);
		return rc;
	}

	return 0;
}

static int test_close(struct rtdm_dev_context *context,
		      rtdm_user_info_t *user_info)
{
	struct device_ctx *ctx = (struct device_ctx *)context->dev_private;

	rtdm_timer_destroy(&ctx->timer);
	rtdm_event_destroy(&ctx->event);

	return 0;
}

static ssize_t test_read(struct rtdm_dev_context *context,
			 rtdm_user_info_t *user_info, void *buf, size_t size)
{
	struct device_ctx *ctx = (struct device_ctx *)context->dev_private;
	rtdm_toseq_t to;
	ssize_t rc;

	rtdm_toseq_init(&to, 1000000000);

	rc = rtdm_event_timedwait(&ctx->event, 1000000000, &to);
	printk("rtdm_event_timedwait: %d\n", rc);

	return rc;
}

static struct rtdm_device device = {
	struct_version: RTDM_DEVICE_STRUCT_VER,

	device_flags: RTDM_NAMED_DEVICE,
	context_size: sizeof(struct device_ctx),
	device_name:  "test",

	open_rt: NULL,
	open_nrt: test_open,

	ops: {
		close_rt: NULL,
		close_nrt: test_close,

		ioctl_rt: NULL,
		ioctl_nrt: NULL,

		read_rt: test_read,
		read_nrt: NULL,

		write_rt: NULL,
		write_nrt: NULL,

		recvmsg_rt: NULL,
		recvmsg_nrt: NULL,

		sendmsg_rt: NULL,
		sendmsg_nrt: NULL,
	},

	device_class: RTDM_CLASS_EXPERIMENTAL,
	device_sub_class: 0,
	profile_version: 1,
	driver_name: "xeno_switchtest",
	driver_version: RTDM_DRIVER_VER(0, 1, 1),
	peripheral_name: "Context Switch Test",
	provider_name: "Gilles Chanteperdrix",
	proc_name: device.device_name,
};

int __init rtdm_test_init(void)
{
	return rtdm_dev_register(&device);
}

void rtdm_test_exit(void)
{
	rtdm_dev_unregister(&device, 1000);
}

module_init(rtdm_test_init);
module_exit(rtdm_test_exit);

Application code:
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/mman.h>

int main(void)
{
	int i, rc, fd;

	mlockall(MCL_CURRENT | MCL_FUTURE);

	fd = open("/dev/test", O_RDONLY);
	if (fd < 0) {
		perror("open(rtdm_test)");
		return EXIT_FAILURE;
	}

	for (i = 0; i < 10; i++) {
		char buf[40];

		rc = read(fd, buf, sizeof(buf));
		if (rc < 0) {
			perror("read");
			pause();
			return EXIT_FAILURE;
		}
	}

	return EXIT_SUCCESS;
}

-- 
                                                                Gilles.


  reply	other threads:[~2011-08-11 19:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-10 17:49 [Xenomai-help] Trouble with rtdm timers and events Carlos Eduardo de Brito Novaes
2011-08-10 19:57 ` Carlos Eduardo de Brito Novaes
2011-08-10 21:10   ` Carlos Eduardo de Brito Novaes
2011-08-11  7:39     ` Gilles Chanteperdrix
2011-08-11 14:31       ` Carlos Eduardo de Brito Novaes
2011-08-11 15:00         ` Gilles Chanteperdrix
2011-08-11 17:56           ` Carlos Eduardo de Brito Novaes
2011-08-11 18:01             ` Gilles Chanteperdrix
2011-08-11 18:12               ` Carlos Eduardo de Brito Novaes
2011-08-11 19:34                 ` Gilles Chanteperdrix [this message]
2011-08-11 22:28                   ` Carlos Eduardo de Brito Novaes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E442EB0.7030909@domain.hid \
    --to=gilles.chanteperdrix@xenomai.org \
    --cc=carlosnov@domain.hid \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.