public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Bob Smith <bsmith@linuxtoys.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 001/001] CHAR DRIVERS: a simple device to give daemons a /sys-like interface
Date: Wed, 07 Aug 2013 12:02:03 -0700	[thread overview]
Message-ID: <520299AB.1020607@linuxtoys.org> (raw)
In-Reply-To: <20130806094604.GE27889@kroah.com>

Greg
    This sample program shows what I'm trying to accomplish.

I still owe you a reply for your previous posting

thanks
Bob Smith


/*
  * pxtest.c : This program demonstrates the use of a proxy device.
  *
  * The program generates some data once a second and tries to send
  * it to /dev/proxyout.  The original data is modified by adding
  * an offset to each input character.  The offset can be set or
  * viewed at the proxy device node /dev/proxyctrl.
  *
  * Typical usage might be
  *    sudo modprobe proxy
  *    PROXYDEV=`grep proxy /proc/devices | cut -d\  -f 1`
  *    sudo mknod /dev/proxyout c $PROXYDEV 0
  *    sudo mknod /dev/proxyctrl c $PROXYDEV 1
  *    sudo chmod 666 /dev/proxyout /dev/proxyctrl
  *    gcc -o pxtest pxtest.c
  *    ./pxtest &
  *    cat /dev/proxyout    # view the output
  *    (switch to another terminal window)
  *    cat /dev/proxyctrl   # what is the offset?
  *    echo 2 > /dev/proxyctrl  # set offset to 2
  */

#include <stdio.h>
#include <stdlib.h>
#include <termio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/time.h>


extern int errno;
#define PXBUFZS 100


int main (int argc, char *argv[])
{
     fd_set rfds;                /* for select() */
     fd_set wfds;                /* for select() */
     int    nfds;                /* for select() */
     struct timeval tv;          /* for select() */
     time_t now;                 /* source of input data */
     int    i;
     int    offset = 0;          /* configured via /dev/proxyctrl */
     int    outfd;               /* fd to /dev/proxyout */
     char   obuff[PXBUFZS];
     int    listener;            /* ==1 if someone is listening at /dev/proxyout */
     int    ctrlfd = -1;         /* fd to /dev/proxyctrl */
     char   cbuff[PXBUFZS];      /* i/o buffer for /dev/proxyctrl */
     int    slret,wrret,rdret;   /* select() write() read() return value */


     outfd =  open("/dev/proxyout", (O_WRONLY | O_NDELAY) , 0);
     if (outfd < 0 ) {
         printf("Unable to open proxy output port\n");
         exit(1);
     }
     listener = 0;

     tv.tv_sec = 1;
     tv.tv_usec = 0;

     while(1) {
         FD_ZERO(&rfds);
         FD_ZERO(&wfds);

         /* open, or reopen, the offset control port at /dev/proxyctrl */
         if (ctrlfd == -1) {
             ctrlfd =  open("/dev/proxyctrl", O_RDWR | O_NDELAY,0);
             if (ctrlfd < 0 ) {
                 printf("Unable to open proxy control port\n");
                 exit(1);
             }
         }
         FD_SET(ctrlfd, &rfds);
         FD_SET(ctrlfd, &wfds);

         /* If no one is listening, watch for a connection */
         if (listener == 0) {
             FD_SET(outfd, &wfds);
         }

         nfds = (outfd > ctrlfd) ? outfd : ctrlfd;

         slret = select((nfds + 1), &rfds, &wfds, (fd_set *)NULL, &tv);

         if (slret == 0) {
             /* Generate bogus data.  Real data might be coming from
              * an I2C read or from an accelerometer at the end of a
              * USB serial link.  For now, just use the current time */
             time(&now);
             ctime_r(&now, obuff);

             /* "process" the data by adding offset to each character */
             i = 0;
             while (obuff[i] != '\n') {
                 obuff[i] = (char) (obuff[i] + offset);
                 obuff[i] = (isprint(obuff[i])) ? obuff[i] : '_';
                 i++;
             }

             /* Try to send the processed output to /dev/proxyout if possible */
             if (listener == 1) {
                 wrret = write(outfd, obuff, strlen(obuff));
                 if ((wrret == 0) || ((wrret < 0) && (errno == EAGAIN))) {
                     /* listener dropped off */
                     listener = 0;
                 }
             }
             tv.tv_sec = 1;
             tv.tv_usec = 0;
         }

         /* Did a listener attach at /dev/proxyout? */
         if (FD_ISSET(outfd, &wfds)) {
             listener = 1;
         }

         /* Data on /dev/proxyctrl is marshalled as newline terminated ASCII
          * This is not a requirement.  Use binary, XML or whatever you need.
          * This demo forces a close but that too is not required. */

         /* Did anyone ask to read the offset value? */
         if (FD_ISSET(ctrlfd, &wfds)) {
             snprintf(cbuff, PXBUFZS, "%d\n", offset);
             write(ctrlfd, cbuff, strlen(cbuff));  /* send offset value */
             write(ctrlfd, cbuff, 0);              /* send EOF */
         }

         /* Is anyone trying to set the offset value? */
         if (FD_ISSET(ctrlfd, &rfds)) {
             rdret = read(ctrlfd, cbuff, PXBUFZS);
             if (rdret == 0) {
                 close(ctrlfd);
                 ctrlfd = -1;
             } else if ((rdret > 0) && (cbuff[rdret - 1] == '\n')) {
                 /* buffer mgmt should be more than checking for ending \n */
                 sscanf(cbuff, "%d", &offset);
             }
         }
     }
}



  reply	other threads:[~2013-08-07 19:02 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <51FC5478.40500@linuxtoys.org>
2013-08-03  1:19 ` [PATCH 001/001] CHAR DRIVERS: a simple device to give daemons a /sys-like interface Bob Smith
2013-08-03  1:56   ` Joe Perches
2013-08-03  2:35   ` Greg Kroah-Hartman
2013-08-03 18:12     ` Bob Smith
2013-08-03 22:38   ` Greg Kroah-Hartman
2013-08-04 21:54     ` Bob Smith
2013-08-04 23:19       ` Greg Kroah-Hartman
2013-08-05 23:46         ` Bob Smith
2013-08-06  9:46           ` Greg Kroah-Hartman
2013-08-07 19:02             ` Bob Smith [this message]
2013-08-07 19:27               ` Greg Kroah-Hartman
2013-08-07 19:39                 ` Bob Smith
2013-08-07 19:51                   ` Greg Kroah-Hartman
2013-08-07 19:54                   ` Greg Kroah-Hartman
2013-08-07 21:04                     ` Bob Smith
2013-08-07 21:33                       ` Greg Kroah-Hartman
2013-08-08 21:23                         ` Bob Smith
2013-08-09 21:52                           ` Greg Kroah-Hartman
2013-08-09 22:20                             ` Bob Smith
2013-08-09 22:14                         ` Bob Smith
2013-08-09 23:01                           ` Greg Kroah-Hartman
2013-08-09 23:35                             ` Bob Smith
2013-08-09 23:46                               ` Greg Kroah-Hartman
2013-08-10 20:08                             ` Bob Smith
2013-08-10 20:29                               ` richard -rw- weinberger
2013-08-10 20:49                                 ` Bob Smith
2013-08-10 21:43                               ` Arnd Bergmann
2013-08-10 22:07                                 ` Bob Smith
2013-08-13 20:15                                   ` Arnd Bergmann
2013-08-07 21:28                     ` Bob Smith
2013-08-07 21:40                       ` Greg Kroah-Hartman
2013-08-07 21:53                         ` Bob Smith
2013-08-09 21:54                           ` Greg Kroah-Hartman
2013-08-09 22:51                             ` Bob Smith
2013-08-09 23:04                               ` Greg Kroah-Hartman
2013-08-07 21:38             ` Bob Smith

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=520299AB.1020607@linuxtoys.org \
    --to=bsmith@linuxtoys.org \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox