int io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb){
    int nbytes;
    int buffer_read;

    /* Check if the buffer has been reads */
    if(ocb->offset){
    	nbytes = 0;
    	_IO_SET_READ_NBYTES (ctp, 0);
    	buffer_read = 0;
    }else{
    	nbytes = BUFFER_SIZE;

    	pthread_mutex_lock(&lock);
		sprintf(buffer, "%i\n", counter_var);
		pthread_mutex_unlock(&lock);

		printf("buffer is: %s \n", buffer);

		/* Set up the return data IOV */
		SETIOV (ctp->iov, buffer, nbytes);

		/* Set up the number of bytes (returned by client's read()) */
		_IO_SET_READ_NBYTES (ctp, nbytes);

		/* Set the offset to one, indicating the buffer has been read */
		ocb->offset = 1;
		buffer_read = 1;
    }
    return (_RESMGR_NPARTS (buffer_read));
}
int io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{
	int         nleft;
    int         nbytes;
    int         nparts;
    int         status;

    if ((status = iofunc_read_verify (ctp, msg, ocb, NULL)) != EOK)
        return (status);
        
    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
        return (ENOSYS);

    /*
     *  On all reads (first and subsequent), calculate
     *  how many bytes we can return to the client,
     *  based upon the number of bytes available (nleft)
     *  and the client's buffer size
     */

    nleft = ocb->attr->nbytes - ocb->offset;
    nbytes = min(msg->i.nbytes, nleft);    

    if (nbytes > 0) {
        /* set up the return data IOV */
        SETIOV (ctp->iov, buffer + ocb->offset, nbytes);

        /* set up the number of bytes (returned by client's read()) */
        _IO_SET_READ_NBYTES (ctp, nbytes);

        /*
         * advance the offset by the number of bytes
         * returned to the client.
         */

        ocb->offset += nbytes;
        
        nparts = 1;
    } else {
        /*
         * they've asked for zero bytes or they've already previously
         * read everything
         */
        
        _IO_SET_READ_NBYTES (ctp, 0);
        
        nparts = 0;
    }

    /* mark the access time as invalid (we just accessed it) */

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

    return (_RESMGR_NPARTS (nparts));
}
Example #3
0
int
_spi_read(resmgr_context_t *ctp, io_read_t *msg, spi_ocb_t *ocb)
{
	uint8_t		*buf;
	int			nbytes;
	int			nonblock = 0;
	int			status;
	SPIDEV		*drvhdl = (SPIDEV *)ocb->hdr.attr;
	spi_dev_t	*dev = drvhdl->hdl;

    if ((status = iofunc_read_verify(ctp, msg, &ocb->hdr, &nonblock)) != EOK)
        return status;

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

	/*
	 * Check to see if the device is locked
	 */
	if ((status =_spi_lock_check(ctp, ocb->chip, ocb)) != EOK)
		return status;

    if (msg->i.nbytes <= 0) {
        _IO_SET_READ_NBYTES(ctp, 0);
        return _RESMGR_NPARTS(0);
    }

    /* check if message buffer is too short */
    nbytes = msg->i.nbytes;
    if (nbytes > ctp->msg_max_size) {
        if (dev->buflen < nbytes) {
            dev->buflen = nbytes;
			if (dev->buf)
            	free(dev->buf);
            if (NULL == (dev->buf = malloc(dev->buflen))) {
                dev->buflen = 0;
                return ENOMEM;
            }
        }
        buf = dev->buf;
    }
	else
        buf = (uint8_t *)msg;

	buf = dev->funcs->xfer(drvhdl, _SPI_DEV_READ(ocb->chip), buf, &nbytes);

	if (nbytes > 0) {
		_IO_SET_READ_NBYTES(ctp, nbytes);
		return _RESMGR_PTR(ctp, buf, nbytes);
	}

	return EIO;
}
Example #4
0
int
io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{
    int status;
	static char data[] = "hello";
	int nb;
	
	
    if (optv) {
        printf ("%s:  in io_read\n", progname);
    }
    
 	if ((status = iofunc_read_verify(ctp, msg, ocb, NULL)) != EOK) {
    	if (optv) printf("read failed because of error %d\n", status );
        return (status);
    }
        
    // No special xtypes
    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
        return(ENOSYS);
    }
    
    nb = strlen(data );
    
	nb = min( nb, msg->i.nbytes );
	
    _IO_SET_READ_NBYTES (ctp, nb);
    SETIOV( ctp->iov, data, nb );

    if (nb > 0)
        ocb->attr->flags |= IOFUNC_ATTR_ATIME;

    return (_RESMGR_NPARTS (1));
    
}
Example #5
0
int io_read(resmgr_context_t *ctp, io_read_t *msg, iofunc_ocb_t *ocb)
{
	LOCK(resource_mutex);
	printf("IO read activated\n");

	int nonblock, status;

	if ((status = iofunc_read_verify (ctp, msg, ocb, &nonblock)) != EOK)
	{
		RETURN_UNLOCK(status, resource_mutex);
	}

	// set data to return
	if (ocb->offset)
	{
		SETIOV(ctp->iov, NULL, 0);
		_IO_SET_READ_NBYTES(ctp, 0);
		RETURN_UNLOCK(_RESMGR_NPARTS(0), resource_mutex);
	}
	ocb->offset = 1;

	if (nonblock && !fifo_status(fifoHandler))
	{
		SETIOV(ctp->iov, NULL, 0);
		_IO_SET_READ_NBYTES(ctp, 0);
		RETURN_UNLOCK(_RESMGR_NPARTS(0), resource_mutex);
	}
	else if(!fifo_status(fifoHandler))
	{
		fifo_add_blocked_id(fifoHandler, ctp->rcvid);
		RETURN_UNLOCK(_RESMGR_NOREPLY, resource_mutex);
	}

	char buffer[WIDTH];
	fifo_rem_string(fifoHandler, buffer);
	SETIOV(ctp->iov, buffer, strlen(buffer));
	_IO_SET_READ_NBYTES(ctp, strlen(buffer));
	RETURN_UNLOCK(_RESMGR_NPARTS(1), resource_mutex);
}
Example #6
0
int
io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{
    int status;
    
    if (optv) {
        printf ("%s:  in io_read\n", progname);
    }

    if ((status = iofunc_read_verify(ctp, msg, ocb, NULL)) != EOK)
        return (status);
        
    // No special xtypes
    if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
        return(ENOSYS);
    }

    _IO_SET_READ_NBYTES (ctp, 0);

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

    return (_RESMGR_NPARTS (0));
}
Example #7
0
static int 
mem_read_write(resmgr_context_t *ctp, resmgr_iomsgs_t *msg, struct mem_ocb *ocb) {
	void					*addr;
	unsigned				nbytes;
	unsigned				size;
	PROCESS					*proc;
	union object			*obp;
	unsigned				*pos;
	unsigned				offset;
	size_t					skip;

	// IO_READ and IO_WRITE messages exactly overlay, as does XTYPE_OFFSET!

	if(!ocb) {
		nbytes = 0;
		goto ok;
	}
	if(!(ocb->ioflag & ((msg->type == _IO_READ) ? _IO_FLAG_RD : _IO_FLAG_WR))) {
		return EBADF;
	}
	nbytes = msg->read.i.nbytes;

	obp = ocb->object;
	if((msg->type == _IO_WRITE) && (ocb->ioflag & O_APPEND) && obp) {
		ocb->pos = OBJECT_SIZE(obp);
	}

	switch (msg->read.i.xtype & _IO_XTYPE_MASK)
	{
    	case _IO_XTYPE_NONE:
		    pos = &ocb->pos;
			skip = sizeof(msg->read.i);
			break;
	    case _IO_XTYPE_OFFSET:
			offset = ((io_pread_t*)msg)->offset.offset;
			pos = &offset;
			skip = sizeof(io_pread_t);
			break;
		default: /* this really shouldn't happen, but just in case */
		    return ENOSYS;
	}

	proc = sysmgr_prp;
	size = obp ? OBJECT_SIZE(obp) : UINT_MAX;
	if( *pos > size - nbytes || *pos + nbytes > size) {
		if(obp && msg->type == _IO_WRITE) {
			int					status;

			status = memmgr.resize(obp, *pos + nbytes);
			if(status != EOK) {
				return status;
			}
			size = OBJECT_SIZE(obp);
		}
		if(*pos >= size) {
			nbytes = 0;
		} else {
			nbytes = size - *pos;
		}
	}

	if(nbytes) {
		int						status, err;
		int						flags;
		part_id_t			mpid;

		if(proc_thread_pool_reserve() != 0) {
			return EAGAIN;
		}
		ProcessBind(SYSMGR_PID);

		proc_wlock_adp(proc);
		flags = MAP_SHARED;
		if(obp != NULL) {
			proc_mux_lock(&obp->mem.mm.mux);
			mpid = obp->hdr.mpid;
		} else {
			flags |= MAP_PHYS;
			mpid = part_id_t_INVALID;
		}
		status = memmgr.mmap(proc, 0, nbytes, PROT_READ|PROT_WRITE, flags,
						obp, *pos, 0, nbytes, NOFD, &addr, &size, mpid);
		if(obp != NULL) proc_mux_unlock(&obp->mem.mm.mux);
		proc_unlock_adp(proc);
		if(status != EOK) {
			ProcessBind(0);
			proc_thread_pool_reserve_done();
			return status;
		}

		SETIOV(ctp->iov + 0, addr, nbytes);

		if(msg->type == _IO_READ) {
			status = resmgr_msgwritev(ctp, ctp->iov + 0, 1, 0);
		} else {
			status = resmgr_msgreadv(ctp, ctp->iov + 0, 1, skip);
		}
		err = errno;

		proc_wlock_adp(proc);
		(void) memmgr.munmap(proc, (uintptr_t)addr, size, 0, mempart_getid(proc, sys_memclass_id));
		proc_unlock_adp(proc);

		if(status == -1) {
			ProcessBind(0);
			proc_thread_pool_reserve_done();
			return err;
		}

		*pos += nbytes;
		if(obp && msg->type == _IO_WRITE) {
			obp->mem.pm.mtime = time(0);
		}
		ProcessBind(0);
		proc_thread_pool_reserve_done();
	}

ok:
	_IO_SET_READ_NBYTES(ctp, nbytes);
	return EOK;
}
Example #8
0
int
io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{
  int nleft;
  int nbytes;
  int nparts;
  int status;
  uint32_t chip;
  char lbuff[64];
  uint32_t cval;

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

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

  
  /*
   * On all reads, calculate how many
   * bytes we can return to the client
   * based upon the number of bytes available (nleft)
   * and the client's buffer size
   */

  nleft = ocb->attr->nbytes - ocb->offset;
  nbytes = min(msg->i.nbytes, nleft);

  cval = *(uint32_t *)ics660->regs.control;
  fprintf(stderr,"io_read - cval %d\n",cval);
  sprintf(lbuff,"%x",cval);
  for( chip=1; chip<=4; chip++){
    select_chip(ics660->mod_control_reg,chip);
    printf("MAIN PRINT_REGISTER\n");
    print_registers(ics660->dc60m_regs);
  }      
  select_chip(ics660->mod_control_reg,(uint32_t)0x0);

  if (nbytes > 0){
    /* set up the return data IOV */
    //SETIOV( ctp->iov, buffer + ocb->offset, nbytes );
    SETIOV( ctp->iov, lbuff + ocb->offset, nbytes );

    /* set up the number of bytes (returned by client's read()) */
    _IO_SET_READ_NBYTES(ctp, nbytes);

    /*
     * advance the offset by the number of bytes 
     * returned to the client.
     */

    ocb->offset += nbytes;

    nparts = 1;
  } else {
    /*
     * they've asked for zero bytres or they've already 
     * read everything
     */
    _IO_SET_READ_NBYTES(ctp,0);

    nparts = 0;
  }

  /* mark the access time as invalid (we just accessed it) */
  if (msg->i.nbytes > 0)
    ocb->attr->flags |= IOFUNC_ATTR_ATIME;
 
  return (_RESMGR_NPARTS(nparts));
}
Example #9
0
int
io_read(resmgr_context_t *ctp, io_read_t *msg, struct ocb *ocb) {
	MQDEV					*dev = ocb->ocb.attr;
	MQMSG					*mp;
	static MQMSG			dummy;
	MQWAIT					*wp;
	int						nonblock, status, n, rcvid;

	// Is queue open for read?
	if((status = iofunc_read_verify(ctp, msg, &ocb->ocb, &nonblock)) != EOK) {
		return status;
	}

	// If an xtype is specified make sure it is a mqueue.
	if((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE && (msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_MQUEUE) {
		return ENOSYS;
	}

	// Is the msg buffer too small for the queue?
	if(msg->i.nbytes < dev->mq_attr.mq_msgsize) {
		return EMSGSIZE;
	}

	// Are there waiting msgs.
	if(dev->mq_attr.mq_curmsgs == 0) {
		if(nonblock & O_NONBLOCK) {
			return EAGAIN;
		}

		if((wp = MemchunkMalloc(memchunk, sizeof(*wp))) == NULL) {
			return ENOMEM;
		}

		wp->rcvid = ctp->rcvid;
		wp->scoid = ctp->info.scoid;
		wp->coid = ctp->info.coid;
		wp->priority = 0;	// Must get real priority from ctp->info
		wp->xtype = msg->i.xtype;
		LINK_PRI_CLIENT(&dev->waiting_read, wp);
		++dev->mq_attr.mq_recvwait;

		return _RESMGR_NOREPLY;
	}

	// Reply with the data
	mp = (dev->mq_attr.mq_flags & MQ_SEMAPHORE) ? &dummy : dev->waiting_msg[0];
	if(mp->nbytes) {
		dev->attr.flags |= (IOFUNC_ATTR_ATIME | IOFUNC_ATTR_DIRTY_TIME);
	}
	resmgr_endian_context(ctp, _IO_READ, S_IFNAM, msg->i.xtype);
	_IO_SET_READ_NBYTES(ctp, mp->nbytes);
	if((msg->i.xtype & _IO_XTYPE_MASK) == _IO_XTYPE_MQUEUE) {
		uint32_t		prio;

		prio = mp->priority;
		SETIOV(&ctp->iov[0], &prio, sizeof(prio));
		SETIOV(&ctp->iov[1], mp->data, mp->nbytes);
		if(resmgr_msgreplyv(ctp, ctp->iov, 2) == -1) {
			return errno;
		}
	} else {
		SETIOV(&ctp->iov[0], mp->data, mp->nbytes);
		if(resmgr_msgreplyv(ctp, ctp->iov, 1) == -1) {
			return errno;
		}
	}

	// Remove the msg
	if(mp != &dummy) {
		if ((dev->waiting_msg[0] = mp->next) == NULL)
			dev->waiting_msg[1] = NULL;
		MemchunkFree(memchunk, mp);
	}
	--dev->mq_attr.mq_curmsgs;

	// Keep stat info up-to-date. We overload st_size to be messages waiting.
	dev->attr.nbytes = dev->mq_attr.mq_curmsgs;

	// Since we removed a msg we may need to wake someone waiting for a msg.
	if(wp = dev->waiting_write) {

		// Unlink and free wait entry
		rcvid = wp->rcvid;
		dev->waiting_write = wp->next;
		MemchunkFree(memchunk, wp);
		--dev->mq_attr.mq_sendwait;

		// Process the message
		resmgr_msg_again(ctp, rcvid);
	}

	if((n = dev->mq_attr.mq_maxmsg - dev->mq_attr.mq_curmsgs) != 0
			&& IOFUNC_NOTIFY_OUTPUT_CHECK(dev->notify, n)) {
		iofunc_notify_trigger(dev->notify, n, IOFUNC_NOTIFY_OUTPUT);
	}

	return _RESMGR_NOREPLY;
}