Beispiel #1
0
int
crc_stwr(void)
{
	docrc = 1;
	return(dev_start());
}
Beispiel #2
0
int loop_socket_handle(int socket, int tt_ms)
{
	unsigned char outhexbuf[ONE_TCP_MAX_LEN] = {0};
	PRO *pro = (PRO*)malloc(sizeof(PRO));
	memset(pro, 0, sizeof(PRO));
	pro->payload = (unsigned char*)malloc(ONLINE_MAX_LEN);
	memset(pro->payload, 0, (ONLINE_MAX_LEN));
	
	int ret = 0;
	int login_ok = 0;
	unsigned int sendseq = 0;
	int outinx = 0;
	int hexlen = 0;
	MACH_STAT stat = IDLE;
	
	while(1) {
		debug(LOG_NOTICE, "stat=%d,login_flag=%d,outinx=%d,last_cmd=%04x,last_recv_ret=%d,dev_work_status=%d\n",
							stat,login_ok,outinx,pro->hd.cmd,ret,dev_work_status);
		switch(stat) {
			case IDLE:
				//sleep_intp_s(5); //will block tcp recving
				break;
				
			case TO_LOGIN:
				memset(&pro->hd, 0, sizeof(PRO_HD));
				memset(pro->payload, 0, (ONLINE_MAX_LEN));
				send_login_info(socket, pro, sendseq++);
				break;
				
			case TO_DEVSTATUS:
				//recv_uart();
				//send_dev_data();
				if(glb_cfg.glb_sock != socket)
					glb_cfg.glb_sock = socket; // uart thread will write glb_sock directly.
				break;
				
			case TO_WORKSTATUS:
				memset(pro->payload, 0, (ONLINE_MAX_LEN));
				if(dev_work_status == E_DEV_TAKEUP) {
					send_busy_info(socket, pro, dev_work_status, sendseq++);
				} else {
					send_status_info(socket, pro, dev_work_status, sendseq++);
				}
				stat = IDLE;
				break;

			case TO_RESPSTATUS:
				memset(pro->payload, 0, (ONLINE_MAX_LEN));
				response_status_info(socket, pro, dev_work_status, sendseq++);
				stat = IDLE;
				break;
			
			case TO_HEART:
				memset(&pro->hd, 0, sizeof(PRO_HD));
				send_heart_info(socket, &pro->hd, sendseq++);
				if(outinx++ > HEART_EXIT_COUNT)
					goto EXIT_ERR;
				else
					stat = IDLE;
				break;
				
			case TO_RESP:
				//for testing;all back
				pro->hd.stat = PRO_RSP;
				memset(outhexbuf, 0, sizeof(outhexbuf));
				hexlen = 0;
				pro_pro2hexbuf(pro,outhexbuf,&hexlen);
				send(socket, outhexbuf, hexlen, sendseq++);
				debug(LOG_DEBUG, "Send Resp Len:%d\n",hexlen);
				stat = IDLE;
				break;
				
			case NET_DISCONN:
				goto EXIT_ERR;
				break;
			
			default:
				break;
		}
		
		//memset(pro, 0, sizeof(PRO)); //NOTE***: will init payload as NULL; this will cause crash!!!
		memset(&pro->hd, 0, sizeof(PRO_HD));
		memset(pro->payload, 0, (ONLINE_MAX_LEN));
		ret = socket_recv_tt(socket, pro, tt_ms);
		if(ret == 0) {
			// timeout
			if(!login_ok) {
				stat = TO_LOGIN;
			} else {
				stat = TO_HEART;
			}
		} else if(ret < 0) {
			// socket error
			goto EXIT_ERR;
			
		} else if( (ret != sizeof(PRO_HD)) && 
				   (ret != sizeof(PRO_HD)+pro->hd.len) ) {
			debug(LOG_NOTICE, "Recv Length %d Error!\n",ret);
			continue;
		} else {
			// handle data...
			switch(pro->hd.cmd) {
				case IDM_DEV_LOGIN:
					debug(LOG_NOTICE, "---< server back: login\n");
					login_ok = 1;
					if(dev_work_status != E_DEV_TAKEUP)
						dev_work_status = E_DEV_ONLINE;
					stat = TO_DEVSTATUS;
					break;
					
				case IDM_DEV_HEART:
					debug(LOG_NOTICE, "---< server back: heart\n");
					outinx = 0;
					break;
				
				case IDM_GETDEV:
					debug(LOG_NOTICE, "---< server cmd: get dev\n");
					stat = TO_RESPSTATUS;
					break;

				/* for response testing */
				case 0xfffe:
					debug(LOG_NOTICE, "---< server cmd: test response\n");
					stat = TO_RESP;
					break;
					
				case IDM_TAKEUPDEV:
				case IDM_RELEASEDEV:
				case IDM_RESET:
				case IDM_DOSTART:
				case IDM_DOSTOP:
				case IDM_SETPARS:
				case IDM_DELPARS:
					if(0 == memcmp(taskid, pro->payload+32, 32)) {
						break;
					} else {
						if(dev_work_status == E_DEV_TAKEUP) {
							debug(LOG_WARNING, "---< server taskid is not correct!\n");
							stat = TO_WORKSTATUS;
							continue;
						} else {
							/* Firstly takeup device */
							break;
						}
					}
				default:
					debug(LOG_ERR, "==== Handle: Unknow Server CMD 0x%02x\n", pro->hd.cmd);
					continue;
			}
			
			switch(pro->hd.cmd) {
				/* handle at local */
				case IDM_TAKEUPDEV:
					debug(LOG_NOTICE, "---< server cmd: takeup dev\n");
					dev_work_status = E_DEV_TAKEUP;
					stat = TO_WORKSTATUS;
					memcpy(taskid, pro->payload+32, sizeof(taskid)); /* skip devid 32*/
					break;

				case IDM_RELEASEDEV:
					debug(LOG_NOTICE, "---< server cmd: release dev\n");
					dev_work_status = E_DEV_ONLINE;
					stat = TO_WORKSTATUS;
					memset(taskid, 0, sizeof(taskid));
					break;
				
				/* send to uart */
				case IDM_RESET:
					debug(LOG_NOTICE, "---< server cmd: reset dev\n");
					dev_work_status = E_DEV_ONLINE;
					stat = TO_WORKSTATUS;
					/* stop and clear */
					dev_reset(pro->payload, pro->hd.len);
					break;

				case IDM_DOSTART:
					debug(LOG_NOTICE, "---< server cmd: start dev\n");
					glb_cfg.rsp_cmd_type = SVR_NEED_TASK_RET;
					glb_cfg.rsp_cmd_type |= SVR_NEED_TASK_PERCT;
					/* only send payload to uart */
					ret = dev_start(pro->payload, pro->hd.len);
					debug(LOG_DEBUG, "---Send To Com ret %d\n",ret);
					break;

				case IDM_DOSTOP:
					debug(LOG_NOTICE, "---< server cmd: stop dev\n");
					glb_cfg.rsp_cmd_type = SVR_NEED_TASK_RET;
					/* only send payload to uart */
					ret = dev_stop(pro->payload, pro->hd.len);
					debug(LOG_DEBUG, "---Send To Com ret %d\n",ret);
					break;

				case IDM_SETPARS:
					debug(LOG_NOTICE, "---< server cmd: set params dev\n");
					/* only send params to uart */
					ret = dev_setpar(pro->payload+64+2, (pro->hd.len-66)>0?(pro->hd.len-66):0);
					debug(LOG_DEBUG, "---Send To Com ret %d\n",ret);
					break;

				case IDM_DELPARS:
					debug(LOG_NOTICE, "---< server cmd: del params\n");
					/* only send payload to uart */
					ret = dev_clrpar(pro->payload, pro->hd.len);
					debug(LOG_DEBUG, "---Send To Com ret %d\n",ret);
					break;
			}
		}
		
		continue;
	} /* end while(1) */
	
EXIT_ERR:
	debug(LOG_NOTICE, "<--- Socket recv out!\n");
	glb_cfg.glb_sock = -1;
	glb_cfg.rsp_cmd_type = 0;
	if(socket > 0) close(socket);
	if(pro && pro->payload) {
		free(pro->payload);
		free(pro);
	}
	return -1;
}
Beispiel #3
0
int
cpio_stwr(void)
{
	return(dev_start());
}
Beispiel #4
0
int
append(void)
{
	ARCHD *arcn;
	int res;
	FSUB *orgfrmt;
	int udev;
	off_t tlen;

	arcn = &archd;
	orgfrmt = frmt;

	/*
	 * Do not allow an append operation if the actual archive is of a
	 * different format than the user specified format.
	 */
	if (get_arc() < 0)
		return 1;
	if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
		tty_warn(1, "Cannot mix current archive format %s with %s",
		    frmt->name, orgfrmt->name);
		return 1;
	}

	/*
	 * pass the format any options and start up format
	 */
	if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
		return 1;

	/*
	 * if we only are adding members that are newer, we need to save the
	 * mod times for all files we see.
	 */
	if (uflag && (ftime_start() < 0))
		return 1;

	/*
	 * some archive formats encode hard links by recording the device and
	 * file serial number (inode) but copy the file anyway (multiple times)
	 * to the archive. When we append, we run the risk that newly added
	 * files may have the same device and inode numbers as those recorded
	 * on the archive but during a previous run. If this happens, when the
	 * archive is extracted we get INCORRECT hard links. We avoid this by
	 * remapping the device numbers so that newly added files will never
	 * use the same device number as one found on the archive. remapping
	 * allows new members to safely have links among themselves. remapping
	 * also avoids problems with file inode (serial number) truncations
	 * when the inode number is larger than storage space in the archive
	 * header. See the remap routines for more details.
	 */
	if ((udev = frmt->udev) && (dev_start() < 0))
		return 1;

	/*
	 * reading the archive may take a long time. If verbose tell the user
	 */
	if (vflag || Vflag) {
		(void)fprintf(listf,
			"%s: Reading archive to position at the end...", argv0);
		vfpart = 1;
	}

	/*
	 * step through the archive until the format says it is done
	 */
	while (next_head(arcn) == 0) {
		/*
		 * check if this file meets user specified options.
		 */
		if (sel_chk(arcn) != 0) {
			if (rd_skip(arcn->skip + arcn->pad) == 1)
				break;
			continue;
		}

		if (uflag) {
			/*
			 * see if this is the newest version of this file has
			 * already been seen, if so skip.
			 */
			if ((res = chk_ftime(arcn)) < 0)
				break;
			if (res > 0) {
				if (rd_skip(arcn->skip + arcn->pad) == 1)
					break;
				continue;
			}
		}

		/*
		 * Store this device number. Device numbers seen during the
		 * read phase of append will cause newly appended files with a
		 * device number seen in the old part of the archive to be
		 * remapped to an unused device number.
		 */
		if ((udev && (add_dev(arcn) < 0)) ||
		    (rd_skip(arcn->skip + arcn->pad) == 1))
			break;
	}

	/*
	 * done, finish up read and get the number of bytes to back up so we
	 * can add new members. The format might have used the hard link table,
	 * purge it.
	 */
	tlen = (*frmt->end_rd)();
	lnk_end();

	/*
	 * try to position for write, if this fails quit. if any error occurs,
	 * we will refuse to write
	 */
	if (appnd_start(tlen) < 0)
		return 1;

	/*
	 * tell the user we are done reading.
	 */
	if ((vflag || Vflag) && vfpart) {
		(void)safe_print("done.\n", listf);
		vfpart = 0;
	}

	/*
	 * go to the writing phase to add the new members
	 */
	res = wr_archive(arcn, 1);
	if (res == 1) {
		/*
		 * wr_archive failed in some way, but before any files were
		 * added. These are the only steps needed to cleanup (and
		 * not truncate the archive).
		 */
		wr_fin();
		(void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
		ar_close();
	}
	return res;
}