int io_write (resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb)
{
    int     status;    

    if ((status = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK)
        return (status);

    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
        return(ENOSYS);

    /* set up the number of bytes (returned by client's write()) */

    _IO_SET_WRITE_NBYTES (ctp, msg->i.nbytes);   

    /*
     *  Reread the data from the sender's message buffer.
     *  We're not assuming that all of the data fit into the
     *  resource manager library's receive buffer.
     */
    
    resmgr_msgread(ctp, buffer, min(msg->i.nbytes, 4095), sizeof(msg->i));    
    buffer[min(msg->i.nbytes, 4095)] = '\0'; /* just in case the text is not NULL terminated */
    ocb->offset = 0;
    attr.nbytes = strlen (buffer)+1;
    printf("%s\n", buffer);  

    if (msg->i.nbytes > 0)
        ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_CTIME;

    return (_RESMGR_NPARTS (0));
}
Ejemplo n.º 2
0
int io_write(resmgr_context_t *ctp, io_write_t *msg, iofunc_ocb_t *ocb)
{
	LOCK(resource_mutex);
	printf("IO write activated\n");

	// set data to return
	_IO_SET_WRITE_NBYTES(ctp, msg->i.nbytes);

	int nBytes = msg->i.nbytes + 1;

	char *buf = (char *)malloc(nBytes * sizeof(char));
	if (!buf)
		RETURN_UNLOCK(ENOMEM, resource_mutex);

	resmgr_msgread(ctp, buf, nBytes, sizeof(msg->i));
	buf[msg->i.nbytes] = '\0';
	printf("Received %d bytes = \"%s\"\n", msg->i.nbytes, buf);

	int rcvid = fifo_rem_blocked_id(fifoHandler);
	if(rcvid != -1)
	{
		printf("Found blocked id, sending string '%s' with %i bytes\n", buf, nBytes);
		MsgReply(rcvid, nBytes, buf, nBytes);
		free(buf);
		RETURN_UNLOCK(_RESMGR_NPARTS(0), resource_mutex);
	}

	fifo_add_string(fifoHandler, buf);
	free(buf);

	RETURN_UNLOCK(_RESMGR_NPARTS(0), resource_mutex);
}
Ejemplo n.º 3
0
int
_spi_iomsg_write(resmgr_context_t *ctp, io_msg_t *msg, spi_ocb_t *ocb)
{
	spi_msg_t	*spimsg = (spi_msg_t *)msg;
    uint8_t		*buf;
    int			nbytes, msglen, status;
	SPIDEV		*drvhdl = (SPIDEV *)ocb->hdr.attr;
	spi_dev_t	*dev = drvhdl->hdl;
	uint16_t	chip = spimsg->device & SPI_DEV_ID_MASK;

	/* check if message buffer is too short */
	nbytes = spimsg->xlen;

    if (nbytes <= 0) {
        _IO_SET_WRITE_NBYTES(ctp, 0);
        return _RESMGR_NPARTS(0);
    }

	msglen = nbytes + sizeof(spi_msg_t);

	if (msglen > ctp->msg_max_size) {
		if (dev->buflen < msglen) {
			dev->buflen = msglen;
			if (dev->buf)
				free(dev->buf);
			if ((dev->buf = malloc(dev->buflen)) == NULL) {
				dev->buflen = 0;
				return ENOMEM;
			}
		}

		status = resmgr_msgread(ctp, dev->buf, msglen, 0);
		if (status < 0)
			return errno;
		if (status < msglen)
			return EFAULT;

		buf = dev->buf;
	}
	else
		buf = (uint8_t *)msg;

	if (chip == SPI_DEV_ID_NONE)
		chip = ocb->chip;

	buf = dev->funcs->xfer(drvhdl, _SPI_DEV_WRITE(chip), buf + sizeof(spi_msg_t), &nbytes);

	if (nbytes == 0)
		return EAGAIN;

	if (nbytes > 0) {
		_IO_SET_WRITE_NBYTES(ctp, nbytes);
		return _RESMGR_NPARTS(0);
	}

	return EIO;
}
Ejemplo n.º 4
0
int io_write (resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb){
    char    *buf;

    /* set up the number of bytes (returned by client's write()) */
    _IO_SET_WRITE_NBYTES (ctp, msg->i.nbytes);

    buf = (char *) malloc(msg->i.nbytes + 1);
    if (buf == NULL)
        return(ENOMEM);

    /*
     *  Reread the data from the sender's message buffer.
     *  We're not assuming that all of the data fit into the
     *  resource manager library's receive buffer.
     */

    resmgr_msgread(ctp, buf, msg->i.nbytes, sizeof(msg->i));
    buf [msg->i.nbytes] = '\0'; /* just in case the text is not NULL terminated */
    printf ("Received %d bytes = '%s'\n", msg -> i.nbytes, buf);

    /* Set the dir var for counter thread according to the command */
    if(!strncmp(buf, "up", 2)){
    	/* Count upwards */
    	pthread_mutex_lock(&lock);
    	counter_dir = DIR_UP;
    	pthread_mutex_unlock(&lock);

    }else if(!strncmp(buf,"down", 4)){
    	/* Count downwards */
    	pthread_mutex_lock(&lock);
    	counter_dir = DIR_DOWN;
    	pthread_mutex_unlock(&lock);

    }else if(!strncmp(buf, "stop", 4)){
    	/* Stop counting */
    	pthread_mutex_lock(&lock);
    	counter_dir = DIR_NONE;
    	pthread_mutex_unlock(&lock);

    }else{
    	// do nothing
    }

    free(buf);

    return (_RESMGR_NPARTS (0));
}
Ejemplo n.º 5
0
int
io_write( resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb)
{
  int status;
  char *buf;

  if((status = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK )
    return(status);

  if((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
    return(ENOSYS);

  _IO_SET_WRITE_NBYTES(ctp, msg->i.nbytes);

  buf = (char *)malloc(msg->i.nbytes + 1);
  if( buf == NULL)
    return(ENOMEM);

  /*
   * reread the data from the sender's message buffer.
   * We're not assuming that all of the data fit into the 
   * resource manager library's receive buffer.
   */
  if( (status = resmgr_msgread(ctp, buf, msg->i.nbytes, sizeof(msg->i))) == -1){ 
    return(ENOSYS);
  }
  fprintf(stderr,"_ics660-drvr:  bytes attemted: %d  bytes received %d\n",msg->i.nbytes,status);
  buf[msg->i.nbytes] = '\0'; //just in case text is not NULL terminated 
  memcpy((int *)ics660->mem1,buf,(size_t)msg->i.nbytes);
  free(buf);

  if(msg->i.nbytes > 0)
    ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_CTIME;

  return(_RESMGR_NPARTS(0));
}
Ejemplo n.º 6
0
int
io_write (resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb)
{
    int status;
    int nb;
    
    if (optv) {
        printf ("%s:  in io_write, of %d bytes\n", progname, msg->i.nbytes);
    }

    if ((status = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK)
        return (status);
        
    // No special xtypes
    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
        return(ENOSYS);
    }
	
	if( msg->i.nbytes == ctp->info.msglen - (ctp->offset + sizeof(*msg) ))
	{
		/* have all the data */
		char *buf;
		buf = (char *)(msg+1);
		
		nb = write( STDOUT_FILENO , buf, msg->i.nbytes ); // fd 1 is stdout
		if( -1 == nb )
		   return errno;
	} else 
	{
#if 0
		char *buf;
		buf = malloc( msg->i.nbytes );
		if (NULL == buf )
		   return ENOMEM;
		nb = resmgr_msgread(ctp, buf, msg->i.nbytes, sizeof(*msg));
		nb = write(1, buf, nb ); // fd 1 is stdout
		free(buf);
		if( -1 == nb )
		   return errno;
#else		   
		char buf[1000]; // my hardware buffer
        int count, bytes;
        count = 0;
        
        while ( count < msg->i.nbytes )
        {
        	bytes = resmgr_msgread( ctp, buf, 1000, count + sizeof(*msg ));
        	if( bytes == -1 )
        	   return errno;
        	bytes = write( 1, buf, bytes ); // fd 1 is standard out 
        	if( bytes == -1 )
        	{  
        	   if (!count )
        	     return errno;
        	   else 
        	      break;
        	}
        	count += bytes;
        }
        nb = count;
#endif		
	}
    _IO_SET_WRITE_NBYTES (ctp, nb);

    if (msg->i.nbytes > 0)
        ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_CTIME;

    return (_RESMGR_NPARTS (0));
}