Example #1
0
static ssize_t scd_write(struct file *filp, const char __user * buf,
			 size_t count, loff_t * f_pos)
{
	struct scd_device *dev = filp->private_data;

	if (down_interruptible(&dev->sem))
		return -ERESTARTSYS;

	/* If there is no available space for writting release mutex and handle non-blocking case.
	 * For blocking case: Block (wait) for free space to become available and
	 * obtain semaphore before checking condition again.
	 * The buffer is full if Write is just behind of Read.
	 */
	while (!freespace(dev)) {
		printk(KERN_DEBUG
		       "scd_write. No freespace available for writting.\n");
		up(&dev->sem);
		if (filp->f_flags & O_NONBLOCK)
			return -EAGAIN;
		if (wait_event_interruptible(dev->out_queue, freespace(dev)))
			return -ERESTARTSYS;
		if (down_interruptible(&dev->sem))
			return -ERESTARTSYS;
	}

	/* There is space available for writting. */
	if (dev->write >= dev->read) {	/* Write is ahead of read. */
		if (dev->read == dev->begin)	/* If Read is at the beggining, do not wrap write pointer. */
			count =
			    min(count, (size_t) (dev->end - dev->write - 1));
		else		/* Write to the end of buffer (and wrap). */
			count = min(count, (size_t) (dev->end - dev->write));
	} else			/* Write has wrapped. Write up to the read pointer (-1). */
		count = min(count, (size_t) (dev->read - dev->write - 1));

	if (copy_from_user(dev->write, buf, count)) {	/* Returns number of bytes that could not be copied. On success, zero. */
		up(&dev->sem);
		return -EFAULT;
	}

	/* Adjust write pointer to reflect written data. */
	dev->write += count;
	if (dev->write == dev->end)
		dev->write = dev->begin;
	up(&dev->sem);

	/* Wake up any readers. */
	wake_up_interruptible(&dev->in_queue);
	return count;
}
Example #2
0
void k9MemoryFifo::addData(uchar *data,uint size) {
    while (1) {
        if (freespace()>=size) {
            enqueue(data,size);
            wDataReady.wakeAll();
            break;
        } else {
            QMutex m;
            m.lock();
            wDataRead.wait(&m);
            m.unlock();
        }
    }
}
Example #3
0
int
ufs_df(char *file, struct maxwidths *mwp)
{
    struct statfs statfsbuf;
    struct statvfs statvfsbuf;
    struct statfs *sfsp;
    struct statvfs *vsfsp;
    const char *mntpt;
    static int synced;

    if (synced++ == 0)
        sync();

    if ((rfd = open(file, O_RDONLY)) < 0) {
        warn("%s", file);
        return (1);
    }
    if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
        close(rfd);
        return (1);
    }
    sfsp = &statfsbuf;
    vsfsp = &statvfsbuf;
    sfsp->f_type = 1;
    strcpy(sfsp->f_fstypename, "ufs");
    sfsp->f_flags = 0;
    sfsp->f_bsize = vsfsp->f_bsize = sblock.fs_fsize;
    sfsp->f_iosize = vsfsp->f_frsize = sblock.fs_bsize;
    sfsp->f_blocks = vsfsp->f_blocks = sblock.fs_dsize;
    sfsp->f_bfree = vsfsp->f_bfree =
                        sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
                        sblock.fs_cstotal.cs_nffree;
    sfsp->f_bavail = vsfsp->f_bavail = freespace(&sblock, sblock.fs_minfree);
    sfsp->f_files = vsfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
    sfsp->f_ffree = vsfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
    sfsp->f_fsid.val[0] = 0;
    sfsp->f_fsid.val[1] = 0;
    if ((mntpt = getmntpt(file)) == NULL)
        mntpt = "";
    memmove(&sfsp->f_mntonname[0], mntpt, (size_t)MNAMELEN);
    memmove(&sfsp->f_mntfromname[0], file, (size_t)MNAMELEN);
    prtstat(sfsp, vsfsp, mwp);
    close(rfd);
    return (0);
}
/* ************************************************** */
double compute_logdistance_pathloss(struct entitydata *entitydata, nodeid_t src, nodeid_t dst, double rxdBm) {
    /*
     *  Pr_dBm(d) = Pr_dBm(d0) - 10 * beta * log10(d/d0)
     *
     *  Note: rxdBm = [Pt + Gt + Gr]_dBm, and L = 1
     *
     *  cf p102-104 ref "Wireless Communications: Principles and Practice", Theodore Rappaport, 1996.
     *
     */
    double dist;

    if (rxdBm != entitydata->last_rxdBm) {
        entitydata->Pr0  = freespace(entitydata, entitydata->dist0, dBm2mW(rxdBm));
        entitydata->last_rxdBm = rxdBm;
    }

    dist = distance(get_node_position(src), get_node_position(dst));

    return mW2dBm(entitydata->Pr0) - 10.0 * entitydata->pathloss * log10(dist/entitydata->dist0);
}
Example #5
0
/* ************************************************** */
double propagation(call_t *c, packet_t *packet, nodeid_t src, nodeid_t dst, double rxdBm) {
    struct entitydata *entitydata = get_entity_private_data(c);
    double dist, powerloss_dbm;
    /*
     *  Pr_dBm(d) = Pr_dBm(d0) - 10 * beta * log10(d/d0) + X
     *
     *  Note: rxdBm = [Pt + Gt + Gr]_dBm, L = 1, and X a normal distributed RV (in dBm)
     *
     *  cf p104-105 ref "Wireless Communications: Principles and Practice", Theodore Rappaport, 1996.
     *
     */
    if (rxdBm != entitydata->last_rxdBm) {
        entitydata->Pr0  = freespace(c, packet, entitydata->dist0, dBm2mW(rxdBm));
        entitydata->last_rxdBm = rxdBm;
    }

    dist = distance(get_node_position(src), get_node_position(dst));

    powerloss_dbm = -10.0 * entitydata->pathloss * log10(dist/entitydata->dist0) + normal(0.0, entitydata->deviation);
    
    return mW2dBm(entitydata->Pr0) + powerloss_dbm;
}
Example #6
0
/*
 * Put down the name, but we might need to break it
 * into chunks so that each chunk fits in 254-28-5 bytes.
 * What a crock.
 *
 * The new Plan 9 format uses strings of this form too, 
 * since they're already there.
 */
Cbuf*
Cputstring(Cdimg *cd, Cbuf *cp, Cbuf *cn, char *nm, char *p, int flags, int dowrite)
{
	char buf[256], *q;
	int free;

	for(; p[0] != '\0'; p = q) {
		cp = ensurespace(cd, 5+1, cp, cn, dowrite);
		cp->len -= 5+1;
		free = freespace(cp);
		assert(5+1 <= free && free < 256);

		strncpy(buf, p, free-5);
		buf[free-5] = '\0';
		q = p+strlen(buf);
		p = buf;

		ensurespace(cd, 5+strlen(p), cp, nil, dowrite);	/* nil: better not use this. */
		Cputrripname(cd, nm, flags | (q[0] ? NMcontinue : 0), p, dowrite);
	}
	return cp;
}
Example #7
0
/*
 * Allocate a block in the file system.
 *
 * A preference may be optionally specified. If a preference is given
 * the following hierarchy is used to allocate a block:
 *   1) allocate the requested block.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   3) allocate a block in the same cylinder group.
 *   4) quadradically rehash into other cylinder groups, until an
 *	  available block is located.
 * If no block preference is given the following hierarchy is used
 * to allocate a block:
 *   1) allocate a block in the cylinder group that contains the
 *	  inode for the file.
 *   2) quadradically rehash into other cylinder groups, until an
 *	  available block is located.
 */
int
ext2fs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref,
    kauth_cred_t cred, daddr_t *bnp)
{
	struct m_ext2fs *fs;
	daddr_t bno;
	int cg;

	*bnp = 0;
	fs = ip->i_e2fs;
#ifdef DIAGNOSTIC
	if (cred == NOCRED)
		panic("ext2fs_alloc: missing credential");
#endif /* DIAGNOSTIC */
	if (fs->e2fs.e2fs_fbcount == 0)
		goto nospace;
	if (kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
	    NULL, NULL) != 0 &&
	    freespace(fs) <= 0)
		goto nospace;
	if (bpref >= fs->e2fs.e2fs_bcount)
		bpref = 0;
	if (bpref == 0)
		cg = ino_to_cg(fs, ip->i_number);
	else
		cg = dtog(fs, bpref);
	bno = (daddr_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
	    ext2fs_alloccg);
	if (bno > 0) {
		ip->i_e2fs_nblock += btodb(fs->e2fs_bsize);
		ip->i_flag |= IN_CHANGE | IN_UPDATE;
		*bnp = bno;
		return (0);
	}
nospace:
	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
	return (ENOSPC);
}
Example #8
0
/* Poll and Ioctl. */
static unsigned int scd_poll(struct file *filp, poll_table * wait)
{
	struct scd_device *dev = filp->private_data;
	unsigned int mask = 0;

	if (down_interruptible(&dev->sem))
		return -ERESTARTSYS;

	/* Add wait queues to the poll_table. */
	poll_wait(filp, &dev->in_queue, wait);
	poll_wait(filp, &dev->out_queue, wait);

	/* Set mask to reflect inner state: 
	 * if there is anything to read or space available for writting. 
	 */
	if (dev->read != dev->write)
		mask |= POLLIN | POLLRDNORM;
	if (freespace(dev))
		mask |= POLLOUT | POLLWRNORM;

	up(&dev->sem);
	return mask;
}
Example #9
0
/*
 * Allocate a block in the file system.
 *
 * A preference may be optionally specified. If a preference is given
 * the following hierarchy is used to allocate a block:
 *   1) allocate the requested block.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   3) allocate a block in the same cylinder group.
 *   4) quadratically rehash into other cylinder groups, until an
 *	  available block is located.
 * If no block preference is given the following hierarchy is used
 * to allocate a block:
 *   1) allocate a block in the cylinder group that contains the
 *	  inode for the file.
 *   2) quadratically rehash into other cylinder groups, until an
 *	  available block is located.
 */
int
ext2fs_alloc(struct inode *ip, int32_t lbn, int32_t bpref,
    struct ucred *cred, int32_t *bnp)
{
	struct m_ext2fs *fs;
	int32_t bno;
	int cg;

	*bnp = 0;
	fs = ip->i_e2fs;
#ifdef DIAGNOSTIC
	if (cred == NOCRED)
		panic("ext2fs_alloc: missing credential");
#endif /* DIAGNOSTIC */
	if (fs->e2fs.e2fs_fbcount == 0)
		goto nospace;
	if (cred->cr_uid != 0 && freespace(fs) <= 0)
		goto nospace;
	if (bpref >= fs->e2fs.e2fs_bcount)
		bpref = 0;
	if (bpref == 0)
		cg = ino_to_cg(fs, ip->i_number);
	else
		cg = dtog(fs, bpref);
	bno = (int32_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
						 ext2fs_alloccg);
	if (bno > 0) {
		ip->i_e2fs_nblock += btodb(fs->e2fs_bsize);
		ip->i_flag |= IN_CHANGE | IN_UPDATE;
		*bnp = bno;
		return (0);
	}
nospace:
	ext2fs_fserr(fs, cred->cr_uid, "file system full");
	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
	return (ENOSPC);
}
Example #10
0
int download(const char *params)
{
	char parbuf[512];
	char bigbuf[10000];
	const char *srcstrh;
	int discon = 0;
	struct FFlag *myf;
	FILE *listh;
	char lastfile[100];
	int keepc = 1;

	bgrun = 0;
	wasbg = 0;

	setprotocol();
	changenodestatus("Downloading");
	TypeFile("download", TYPE_MAKE | TYPE_CONF | TYPE_WARN);

	if (!conference()->conf.CONF_FILEAREAS) {
		DDPut(sd[dlnoareasstr]);
		return 0;
	}
	if ((protocol->PROTOCOL_TYPE == 3 || protocol->PROTOCOL_TYPE == 2) && !conference()->conf.CONF_UPLOADAREA) {
		DDPut(sd[dlnouploadsstr]);
		return 0;
	}
	if (protocol->PROTOCOL_TYPE == 2 || protocol->PROTOCOL_TYPE == 3) {
		if (cleantemp() == -1) {
			DDPut(sd[tempcleanerrstr]);
			return 0;
		}
		if (!freespace())
			return 0;
		maketmplist();
	}
	srcstrh = params;

	for (;;) {
		if (strtoken(parbuf, &srcstrh, sizeof parbuf) > sizeof parbuf)
			continue;
		if (!*parbuf)
			break;
		flagfile(parbuf, 1);
	}
	for (;;) {
		typedlprompt();
		bigbuf[0] = 0;
		if (!(Prompt(bigbuf, 200, 0)))
			return 0;
		if (!bigbuf[0]) {
			break;
		} else if (!strcasecmp(bigbuf, "a")) {
			return 0;
		} else {
			srcstrh = bigbuf;
			for (;;) {
				if (strtoken(parbuf, &srcstrh, 
					     sizeof parbuf) > sizeof parbuf)
					continue;
				if (!*parbuf)
					break;
				flagfile(parbuf, 1);
			}
		}
	}
	if (!filestagged)
		return 0;
	listtags();
	if (estimsecs(bytestagged) > timeleft) {
		DDPut(sd[dlnotimestr]);
		return 0;
	}
	for (;;) {
		DDPut(sd[dlproceedstr]);
		bigbuf[0] = 0;
		if (!(Prompt(bigbuf, 3, 0)))
			return 0;
		if (!bigbuf[0] || bigbuf[0] == 'p' || bigbuf[0] == 'P')
			break;
		else if (bigbuf[0] == 'e' || bigbuf[0] == 'E') {
			taged(0);
		} else if (bigbuf[0] == 'd' || bigbuf[0] == 'D') {
			discon = 1;
			break;
		} else if (bigbuf[0] == 'a' || bigbuf[0] == 'A') {
			return 0;
		}
	}
	snprintf(parbuf, sizeof parbuf, "%s/dszlog.%d", DDTMP, node);

	sprintf(&parbuf[250], "%s/ddfilelist.%d", DDTMP, node);
	unlink(&parbuf[250]);

	if (!(listh = fopen(&parbuf[250], "w")))
		return 0;

	myf = (struct FFlag *) flaggedfiles->lh_Head;
	while (myf->fhead.ln_Succ) {
		char tbu[256];
		snprintf(tbu, sizeof tbu, "%s%s\n", 
			myf->f_path, myf->f_filename);
		fputs(tbu, listh);
		myf = (struct FFlag *) myf->fhead.ln_Succ;
	}
	fclose(listh);
	*lastfile = 0;

	if (protocol->PROTOCOL_TYPE == 2 || protocol->PROTOCOL_TYPE == 3) {
		if ((!(user.user_toggles & (1L << 15))) && (maincfg.CFG_FLAGS & (1L << 11))) {
			initbgchecker();

		}
	}
	sendfiles(&parbuf[250], lastfile, sizeof lastfile);


	if (protocol->PROTOCOL_TYPE == 2 || protocol->PROTOCOL_TYPE == 3) {
		upload(2);
	}
	if (*lastfile) {
		myf = (struct FFlag *) flaggedfiles->lh_Head;
		while (myf->fhead.ln_Succ && keepc) {
			struct FFlag *oldf;
			struct DD_DownloadLog ddl;
			char lbuf[100];
			int logfd;

			snprintf(lbuf, sizeof lbuf, 
				"%s/logfiles/downloadlog.dat", origdir);
			logfd = open(lbuf, O_WRONLY | O_CREAT, 0666);
			if (logfd != -1) {
				fsetperm(logfd, 0666);
				memset((char *) &ddl, 0, sizeof(struct DD_DownloadLog));
				ddl.DL_SLOT = user.user_account_id;
				strlcpy(ddl.DL_FILENAME, myf->f_filename, sizeof ddl.DL_FILENAME);
				ddl.DL_FILESIZE = myf->f_size;
				ddl.DL_TIME = time(0);
				ddl.DL_BPSRATE = bpsrate;
				ddl.DL_NODE = node;
				ddl.DL_CONF = (unsigned char) myf->f_conf;
				lseek(logfd, 0, SEEK_END);
				safe_write(logfd, &ddl, sizeof(struct DD_DownloadLog));
				close(logfd);
			}
			if (!(myf->f_flags & FLAG_FREE)) {
				user.user_dlbytes += myf->f_size;
				user.user_dlfiles++;
			}
			if (!strcasecmp(lastfile, myf->f_filename))
				keepc = 0;
			Remove((struct Node *) myf);
			oldf = myf;
			myf = (struct FFlag *) myf->fhead.ln_Succ;
			free(oldf);
		}
	}
	recountfiles();

	unlink(&parbuf[250]);

	if (discon) {
		if (autodisconnect())
			return 2;
	}
	return 1;
}
Example #11
0
/*
 * Write a Rock Ridge SUSP set of records for a directory entry.
 */
int
Cputsysuse(Cdimg *cd, Direc *d, int dot, int dowrite, int initlen)
{
	char buf[256], buf0[256], *nextpath, *p, *path, *q;
	int flags, free, m, what;
	uvlong o;
	Cbuf cn, co, *cp;

	assert(cd != nil);
	assert((initlen&1) == 0);

	if(dot == DTroot)
		return 0;

	co.len = initlen;

	o = Cwoffset(cd);

	assert(dowrite==0 || Cwoffset(cd) == o+co.len-initlen);
	cp = &co;

	if (dot == DTrootdot) {
		m = CputsuspSP(cd, 0);
		cp = ensurespace(cd, m, cp, &cn, dowrite);
		CputsuspSP(cd, dowrite);

		m = CputsuspER(cd, 0);
		cp = ensurespace(cd, m, cp, &cn, dowrite);
		CputsuspER(cd, dowrite);
	}

	/*
	 * In a perfect world, we'd be able to omit the NM
	 * entries when our name was all lowercase and conformant,
	 * but OpenBSD insists on uppercasing (really, not lowercasing)
	 * the ISO9660 names.
	 */
	what = RR_PX | RR_TF | RR_NM;
	if(d != nil && (d->mode & CHLINK))
		what |= RR_SL;

	m = CputsuspRR(cd, what, 0);
	cp = ensurespace(cd, m, cp, &cn, dowrite);	
	CputsuspRR(cd, what, dowrite);

	if(what & RR_PX) {
		m = CputrripPX(cd, d, dot, 0);
		cp = ensurespace(cd, m, cp, &cn, dowrite);
		CputrripPX(cd, d, dot, dowrite);
	}

	if(what & RR_NM) {
		if(dot == DTiden)
			p = d->name;
		else if(dot == DTdotdot)
			p = "..";
		else
			p = ".";

		flags = suspdirflags(d, dot);
		assert(dowrite==0 || cp != &co || Cwoffset(cd) == o+co.len-initlen);
		cp = Cputstring(cd, cp, &cn, "NM", p, flags, dowrite);
	}

	/*
	 * Put down the symbolic link.  This is even more of a crock.
	 * Not only are the individual elements potentially split, 
	 * but the whole path itself can be split across SL blocks.
	 * To keep the code simple as possible (really), we write
	 * only one element per SL block, wasting 6 bytes per element.
	 */
	if(what & RR_SL) {
		for(path=d->symlink; path[0] != '\0'; path=nextpath) {
			/* break off one component */
			if((nextpath = strchr(path, '/')) == nil)
				nextpath = path+strlen(path);
			strncpy(buf0, path, nextpath-path);
			buf0[nextpath-path] = '\0';
			if(nextpath[0] == '/')
				nextpath++;
			p = buf0;

			/* write the name, perhaps broken into pieces */
			if(strcmp(p, "") == 0)
				flags = NMroot;
			else if(strcmp(p, ".") == 0)
				flags = NMcurrent;
			else if(strcmp(p, "..") == 0)
				flags = NMparent;
			else
				flags = 0;

			/* the do-while handles the empty string properly */
			do {
				/* must have room for at least 1 byte of name */
				cp = ensurespace(cd, 7+1, cp, &cn, dowrite);
				cp->len -= 7+1;
				free = freespace(cp);
				assert(7+1 <= free && free < 256);

				strncpy(buf, p, free-7);
				buf[free-7] = '\0';
				q = p+strlen(buf);
				p = buf;

				/* nil: better not need to expand */
				assert(7+strlen(p) <= free);
				ensurespace(cd, 7+strlen(p), cp, nil, dowrite);
				CputrripSL(cd, nextpath[0], flags | (q[0] ? NMcontinue : 0), p, dowrite);
				p = q;
			} while(p[0] != '\0');
		}
	}

	assert(dowrite==0 || cp != &co || Cwoffset(cd) == o+co.len-initlen);

	if(what & RR_TF) {
		m = CputrripTF(cd, d, TFcreation|TFmodify|TFaccess|TFattributes, 0);
		cp = ensurespace(cd, m, cp, &cn, dowrite);
		CputrripTF(cd, d, TFcreation|TFmodify|TFaccess|TFattributes, dowrite);
	}
	assert(dowrite==0 || cp != &co || Cwoffset(cd) == o+co.len-initlen);

	if(cp == &cn && dowrite) {
		/* seek out of continuation, but mark our place */
		cd->rrcontin = Cwoffset(cd);
		setcelen(cd, cn.ceoffset, cn.len);
		Cwseek(cd, o+co.len-initlen);
	}

	if(co.len & 1) {
		co.len++;
		if(dowrite)
			Cputc(cd, 0);
	}

	if(dowrite) {
		if(Cwoffset(cd) != o+co.len-initlen)
			fprint(2, "offset %llud o+co.len-initlen %llud\n",
				Cwoffset(cd), o+co.len-initlen);
		assert(Cwoffset(cd) == o+co.len-initlen);
	} else
		assert(Cwoffset(cd) == o);

	assert(co.len <= 255);
	return co.len - initlen;
}
Example #12
0
// Not threadsafe.
lsn_t stasis_ringbuffer_nb_reserve_space(stasis_ringbuffer_t * ring, lsn_t sz) {
  if(freespace(ring) < sz) { return RING_FULL; }
  lsn_t ret = ring->wf;
  FETCH_AND_ADD(&ring->wf, sz); //ring->wf += sz;
  return ret;
}
Example #13
0
int main() {
    unsigned long long segmnt;
    LARGE_INTEGER viewbase;
    size_t vaddress,maplen;
    WCHAR ppath[PATH_MAX];
    NTSTATUS status;
    char tmp[64];
    int fd;

    if (OpenPhysMem()) {
        fprintf(stderr,"Unable to open physical memory device\n");
        return 1;
    }

    memset(&viewbase,0,sizeof(viewbase));
    viewbase.QuadPart = (ULONGLONG)ROM_offset;
    maplen = ROM_size-1;
    vaddress = 0;

    status = ZwMapViewOfSection(mem_fd,(HANDLE)-1,(PVOID*)(&vaddress),0UL,
                                ROM_size-1,&viewbase,(PDWORD)(&maplen),ViewShare,0,PAGE_READONLY);
    if (!NT_SUCCESS(status)) {
        fprintf(stderr,"Failed to map view of section for %08llx status=0x%08lx\n",viewbase.QuadPart,(unsigned long)status);
        return 1;
    }
    ROM = (unsigned char*)((size_t)vaddress);

    printf(HELLO);
    printf("Press ENTER to start\n");
    waitforenter();

    /* write it out */
    for (segmnt=ROM_offset; segmnt < (ROM_offset+(unsigned long long)ROM_size); segmnt += (unsigned long long)ROM_blocksize) {
        while (freespace() < (66UL << 10UL)) {
            if (GetCurrentDirectoryW(sizeof(ppath),ppath) == 0) {
                fprintf(stderr,"Unable to get current directory\n");
                return 1;
            }

            /* chdir() off the drive so that it is safe for the user to remove the
             * pen drive, floppy, etc. */
            if (SetCurrentDirectoryW(L"C:\\"/*FIXME*/) == 0) {
                fprintf(stderr,"Unable to bail out to root\n");
                return 1;
            }

            printf("Unmount and remove disk, move files off on another computer,\n");
            printf("re-mount and re-insert and hit ENTER\n");
            waitforenter();

            /* assuming the user has reloaded the disk/flash drive/whatever and remounted
             * at the same point, jump back into the directory and try to resume our work */
            if (SetCurrentDirectoryW(ppath) == 0) {
                fprintf(stderr,"Unable to reenter capture dir\n");
                return 1;
            }
        }

        sprintf(tmp,CAPTURE_SPRINTF,segmnt);
        printf("Writing ... %s\n",tmp);
        fd = open(tmp,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0644);
        if (fd < 0) {
            fprintf(stderr,"Unable to open file, %s\n",strerror(errno));
            return 1;
        }
        if ((size_t)write(fd,ROM+segmnt-ROM_offset,ROM_blocksize) != ROM_blocksize) {
            fprintf(stderr,"Unable to write ROM block\n");
            return 1;
        }
        close(fd);
    }

    ZwUnmapViewOfSection((HANDLE)-1,(PVOID)vaddress);
    ClosePhysMem();
    return 0;
}
//==================================================================================
//	main function					(main fucntion of the program)
//===================================================================================
void main(void)
{
	//assign variable
	unsigned char i=0,database;				
	unsigned char data[10];
	unsigned int m,j;

	init();
	d_graphic(0);					//display cytron logo
	delay(300000);					//delay for pic display

	// display claibrating messages
	send_config(0b00110000);			//set to function set configuration
	lcd_clr();					//clear the lcd
	send_string(cali);				//display string
	lcd_goto (8);					//cursor at 3rd character line
	send_string(mssg1);				//display string mssg1
	lcd_goto(24);					//cursor at 4th  character line
	send_string(mssg2);				//display string mssg2

	while(SW1);					//wait for button pushed
	while(!SW1);					//wait for button release
	calibrate();					//taking the current force as reference for flat plane
	lcd_clr();					//clear the lcd
	

	lcd_goto(1);					//cursor at 3rd character, 1st character line
	send_string(mssg3);				//display string mssg3
	lcd_goto(17);					//cursor at 3rd character, 2st character line
	send_string(mssg4);				//display string mssg4
	lcd_goto(0);					//cursor at 1st character, 1st character line
	send_char(0x10);				//display symbol arrow
	m =0 ;						//set the variable 0

	while(1)					//loop forever
	{

		if (!SW1)				//if sw1 is pressed, increase mode
		{
			while(!SW1);			//wait until sw1 is release
			m++;				//increament m
			if (m>1) m=0;			//if m more than 1 , clear m

			if (m==1) 			//if m is 1
			{
				lcd_goto(0);		//cursor at 1st line
				send_char('  ');	//space 1st and 2nd character , 1st line
				lcd_goto(16);		//cursor at 2nd line
				send_char(0x10);	//display arrow at 2nd charater, 2nd line
			}
			else 
			{
				lcd_goto(0);		//cursor at 1st line
				send_char(0x10);	//display arrow at 2nd charater, 1st line
				lcd_goto(16);		//cursor at 2nd line
				send_char('  ');	//space 1st and 2nd character , 2nd line
			}
		}

		else if (!SW2)					//if sw is pressed
		{
			while(!SW2);				//wait until sw2 is released
			lcd_clr();				//clear all character
			clr_graphic();				//clear all graphic

			// 16 bit timer initial status and start timer
			TMR0H = 194;						// value for timer reach overflow in 50 ms for clock 10MIPS		
			TMR0L = 246;
			T0CONbits.TMR0ON = 1;					//start timer

			while(1)						//loop forever
			{
				if (INTCONbits.TMR0IF==1)			//if timer over flow ( occur every 50 ms) // to update data every 50 ms
				{
					INTCONbits.TMR0IF==0;			//clear the flag bit
					TMR0H=194;				//value for timer reach overflow in 50 ms for clock 10MIPS		
					TMR0L=246;
					if (m==1) friction();			//if mode is 1 , the do calculation friction, else do calculation freespace.
					else 	freespace();			//update the data ( required for more accurate intergration ( faster, more accurate )) 

					i++;					//increment variable i
				}
					
				if (i==10) 					//if i reach ten ( around 500 ms) update the icon position
				{
					if((x_dis/sens >60)||(x_dis/sens<-60)||(y_dis/sens>28)||(y_dis/sens<-28))	//if the icon position not in range of glcd
					{																					//mean 'the ball drop out from glcd'
						d_graphic(1);			//display graphic 
						delay(300000);			//dealy for display the graphic
						d_graphic(2);			//display another graphic
						delay(300000);				
			 			while(1);
					}
					d_icon(60-(x_dis/sens),28+(y_dis/sens));		//display icon at updated x,y position
					i=0;							//clear the variable i
				}
			}
		}	
	}
}
Example #15
0
static int diskspacelow()
{
    static int counter=0;
    static int islow=0;

    if (counter == 0)
    {
        unsigned long checkblocks, nblock, checkinodes, ninodes;
        unsigned blocksize;

        // Time to recheck disk free space.  By default, remember
        // the ersults for 100 (or so) loops.
        //

        counter=100;
        if (freespace(MSGQDIR "/.", &checkblocks, &nblock, &checkinodes, &ninodes, &blocksize))
            return (islow);

        if (checkinodes && ninodes < (unsigned long)(100+msgq::queue.size()*2))
            islow=1;
        else
        {
            unsigned long n=100;
            size_t i;

            //
            // Add up how many recipients we have in memory.
            // The number of 512 byte disk blocks we require is 100 +
            // number of recipients.
            //

            for (i=0; i<msgq::queuehashfirst.size(); i++)
            {
                struct msgq *p;

                for (p=msgq::queuehashfirst[i]; p;
                        p=p->nexthash)
                {
                    size_t j;

                    for (j=0; j<p->rcptinfo_list.size();
                            j++)
                        n += p->rcptinfo_list[j].
                             addresses.size();
                }
            }

            n = n * 512 / blocksize;

            if (n  > nblock)
                islow=1;
            else
            {
                //
                // Figure out when to check again.
                // We'll wait a turn for every 512 byte block we're
                // above the minimum, subject to a 10 turn minimum
                // or 500 turn maximum.
                //
                n -= nblock;
                if (n > 500)	n=500;
                if (n < 10)	n=10;
                counter=n;
            }
        }
    }

    --counter;
    return (islow);
}
Example #16
0
int main() {
	unsigned long long segmnt;
	char ppath[PATH_MAX];
	struct stat st;
	char tmp[64];
	int fd;

	if ((mem_fd = open("/dev/mem",O_RDONLY)) < 0) {
		fprintf(stderr,"Unable to open /dev/mem, %s\n",strerror(errno));
		return 1;
	}
	if (fstat(mem_fd,&st)) {
		fprintf(stderr,"Unable to stat opened /dev/mem handle, %s\n",strerror(errno));
		return 1;
	}
	if (!S_ISCHR(st.st_mode)) {
		fprintf(stderr,"/dev/mem is NOT character device\n");
		return 1;
	}

	/* NTS: This code assumes you are compiling with _FILE_OFFSET_BITS == 64 so that
	 *      on 32-bit systems the limitations of the original mmap() call do not interfere
	 *      with our attempt here */
	ROM = mmap(NULL,ROM_size,PROT_READ,MAP_SHARED,mem_fd,ROM_offset);
	if (ROM == MAP_FAILED) {
		fprintf(stderr,"Unable to mmap ROM\n");
		return 1;
	}

	printf(HELLO);
	printf("Press ENTER to start\n");
	waitforenter();

	/* write it out */
	for (segmnt=ROM_offset;segmnt < (ROM_offset+(unsigned long long)ROM_size);segmnt += (unsigned long long)ROM_blocksize) {
		while (freespace() < (66UL << 10UL)) {
			if (getcwd(ppath,sizeof(ppath)) == NULL) {
				fprintf(stderr,"Unable to get current directory\n");
				return 1;
			}

			/* Linux counts the mountpoint as being in use if a file is open there or
			 * a program has it as it's current working directory. so to allow the user
			 * to remove the floppy (or whatever) we have to change our working directory */
			if (chdir("/")) {
				fprintf(stderr,"Unable to bail out to root\n");
				return 1;
			}

			printf("Unmount and remove disk, move files off on another computer,\n");
			printf("re-mount and re-insert and hit ENTER\n");
			waitforenter();

			/* assuming the user has reloaded the disk/flash drive/whatever and remounted
			 * at the same point, jump back into the directory and try to resume our work */
			if (chdir(ppath)) {
				fprintf(stderr,"Unable to reenter capture dir\n");
				return 1;
			}
		}

		sprintf(tmp,CAPTURE_SPRINTF,segmnt);
		printf("Writing ... %s\n",tmp);
		fd = open(tmp,O_WRONLY|O_CREAT|O_TRUNC,0644);
		if (fd < 0) {
			fprintf(stderr,"Unable to open file, %s\n",strerror(errno));
			return 1;
		}
		if ((size_t)write(fd,ROM+segmnt-ROM_offset,ROM_blocksize) != ROM_blocksize) {
			fprintf(stderr,"Unable to write ROM block\n");
			return 1;
		}
		close(fd);
	}

	munmap(ROM,ROM_size);
	close(mem_fd);
	return 0;
}