Beispiel #1
0
int is_valid_posret_msg(unsigned char *msg, int len)
{
	unsigned char crc = 0;
	int i = 0;
	//debug
	return 0;
	
	if(len < POS_MIN_RET_LEN || len > POS_MAX_RET_LEN)
		return -1;
	if(msg[0] != PRO_START)
		return -1;
	
	debug(LOG_DEBUG, "<-----POS result:\n");
	debug_buf(msg, len);
	
	crc = msg[1];
	for(i=2; i < len-1; i++) {
		crc ^= msg[i];
	}
	if(crc != msg[len-1]) {
		debug(LOG_ERR, "Pos result msg crc error. crc=0x%02x,get=0x%02x\n",crc,msg[len-1]);
		return -1;
	}
	return 0;
}
Beispiel #2
0
Datei: test.c Projekt: slgu/OS
int main(int argc, char *argv[])
{
	if (argc != 2) {
		printf("wrong input number\n");
		return 1;
	}
	int n = atoi(argv[1]);
	struct prinfo * buff = (struct prinfo *)malloc(n * sizeof(struct prinfo));
	int i, j;
	int level[10000];
	int ret = syscall(223, buff, &n);
	for (i = 0; i < 10000; ++i)
		level[i] = -1;
	if (ret > 0) {
	        printf("total number: %d\n", ret);
		if (ret < n) 
			n = ret;
	        for (i = 0; i < n; ++i){
			level[buff[i].pid] = level[buff[i].parent_pid] + 1;
			for (j = 0; j < level[buff[i].pid]; ++j)
				printf("\t");
			debug_buf(buff + i);
		}
	}
	else {
	        printf("get process info error\n");
	}
	return 0;
}
Beispiel #3
0
void onEvent (ev_t ev) {
    debug_event(ev);

    switch(ev) {
   
      // network joined, session established
      case EV_JOINED:
          // enable pinging mode, start scanning...
          // (set local ping interval configuration to 2^1 == 2 sec)
          LMIC_setPingable(1);
          debug_str("SCANNING...\r\n");
          break;

      // beacon found by scanning
      case EV_BEACON_FOUND:
          // send empty frame up to notify server of ping mode and interval!
          LMIC_sendAlive();
          break;

      // data frame received in ping slot
      case EV_RXCOMPLETE:
          // log frame data
          debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
          if(LMIC.dataLen == 1) {
              // set LED state if exactly one byte is received
              debug_led(LMIC.frame[LMIC.dataBeg] & 0x01);
          }
          break;    
    }
}
Beispiel #4
0
void onEvent (ev_t ev)
{
	debug_event(ev);

	switch(ev)
	{
	// network joined, session established
	case EV_JOINED:
		debug_val("netid = ", LMIC.netid);
		goto tx;

		// scheduled data sent (optionally data received)
	case EV_TXCOMPLETE:
		if(LMIC.dataLen) { // data received in rx slot after tx
			debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
		}
	tx:
		// immediately prepare next transmission
		LMIC.frame[0] = LMIC.snr;
		// schedule transmission (port 1, datalen 1, no ack requested)
		LMIC_setTxData2(1, LMIC.frame, 1, 0);
		// (will be sent as soon as duty cycle permits)
		break;
	default:
		break;
	}
}
Beispiel #5
0
void onEvent (ev_t ev)
{
    debug_event(ev);

    switch(ev)
    {
   
      // network joined, session established
      case EV_JOINED:
          debug_val("\r\nnetid = ", LMIC.netid);

          // immediately prepare next transmission
          debug_str("\r\nSending");
          LMIC.frame[0] = LMIC.snr;
          LMIC_setTxData2(1, LMIC.frame, 1, 0);	// schedule transmission (port 1, datalen 1, no ack requested)
    	  break;
        
      // scheduled data sent (optionally data received)
      case EV_TXCOMPLETE:
          if(LMIC.dataLen) { // data received in rx slot after tx
        	  debug_str("\r\nReceived:");
              debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
          }
          // immediately prepare next transmission
          LMIC.frame[0] = LMIC.snr;
          debug_val("\r\nSending:", LMIC.frame[0]);
			// schedule transmission (port 1, datalen 1, no ack requested)
          LMIC_setTxData2(1, LMIC.frame, 1, 0);
          break;
    }
}
Beispiel #6
0
int edma_linked_transfer_example(void) {
	printk("\n\n\n###### %s() ######\n", __func__);

	debug_buf("src", src, sizeof src);
	printk("\nbefore transfer");
	memset(dst, '\0', sizeof dst);
	debug_buf("dst", dst, sizeof dst);

	pa_conf[1]		= pa_conf[0];		// *** important ***
	pa_conf[1].index	= 127;
	pa_conf[1].src		= (uint32_t)src + 1;
	pa_conf[1].dst		= (uint32_t)dst + sizeof src;
	pa_conf[1].a_cnt	= 1;
	pa_conf[1].b_cnt	= sizeof src / 2;
	pa_conf[1].src_b_idx	= 2;
	pa_conf[1].dst_b_idx	= 1;
	pa_conf[1].tcc		= 1;
	pa_conf[1].link		= LINK_NULL;
	pa_conf[1].flags	= FLAG_SYNCTYPE_AB | FLAG_TRANS_INTR |
				FLAG_LAST_PAENTRY | FLAG_TCC_EARLY,

	pa_conf[0].index	= 0;
	pa_conf[0].src		= (uint32_t)src + 0;
	pa_conf[0].dst		= (uint32_t)dst + 0;
	pa_conf[0].a_cnt	= 1;
	pa_conf[0].b_cnt	= sizeof src / 2;
	pa_conf[0].src_b_idx	= 2;
	pa_conf[0].dst_b_idx	= 1;
	pa_conf[0].tcc		= 0;
	pa_conf[0].link		= LINK_NEXT(pa_conf[1].index);
	pa_conf[0].flags	= FLAG_SYNCTYPE_AB | FLAG_TRANS_INTR | FLAG_TCC_EARLY |
				FLAG_TRANS_EVT /* event to trigger pa_conf[1] */;

	edma_conf->pa_conf	= pa_conf;
	edma_conf->pa_cnt	= 2;
	edma_conf->channel	= pa_conf[0].index;

	edma_transfer_inner(edma_conf);

	printk("\nafter  transfer");
	debug_buf("dst", dst, sizeof dst);
	return 0;
}
Beispiel #7
0
int edma_block_move_example(void) {
	printk("\n\n\n###### %s() ######\n", __func__);

	debug_buf("src", src, sizeof src);
	printk("\nbefore transfer");
	memset(dst, '\0', sizeof dst);
	debug_buf("dst", dst, sizeof dst);

	pa_conf->src		= (uint32_t)src;
	pa_conf->dst		= (uint32_t)dst;
	edma_conf->pa_conf	= pa_conf;
	edma_conf->pa_cnt	= 1;
	edma_conf->channel	= pa_conf[0].index;

	edma_transfer_inner(edma_conf);

	printk("\nafter  transfer");
	debug_buf("dst", dst, sizeof dst);
	return 0;
}
Beispiel #8
0
int edma_transfer_channing_example(void) {
	printk("\n\n\n###### %s() ######\n", __func__);

	debug_buf("src", src, sizeof src);
	printk("\nbefore transfer");
	memset(dst, '\0', sizeof dst);
	debug_buf("dst", dst, sizeof dst);

	pa_conf[0].index	= 2;
	pa_conf[0].src		= (uint32_t)src + 0;
	pa_conf[0].dst		= (uint32_t)dst + 0;
	pa_conf[0].a_cnt	= 1;
	pa_conf[0].b_cnt	= 2;
	pa_conf[0].c_cnt	= sizeof src / pa_conf[0].b_cnt / 2;
	pa_conf[0].src_b_idx	= 1;
	pa_conf[0].src_c_idx	= 2;
	pa_conf[0].dst_b_idx	= 2;
	pa_conf[0].dst_c_idx	= 4;
	pa_conf[0].tcc		= 3;		/* event to trigger pa_conf[1] */
	pa_conf[0].link		= LINK_NULL;
	pa_conf[0].flags	= FLAG_SYNCTYPE_AB | FLAG_TRANS_INTR | FLAG_TCC_EARLY |
				FLAG_TRANS_EVT;

	pa_conf[1]		= pa_conf[0];
	pa_conf[1].index	= pa_conf[0].tcc;
	pa_conf[1].src		= (uint32_t)src + sizeof src / 2;
	pa_conf[1].dst		= (uint32_t)dst + sizeof src;
	pa_conf[1].dst_b_idx	= 1;
	pa_conf[1].dst_c_idx	= 2;
	pa_conf[1].tcc		= pa_conf[0].index;/* event to trigger pa_conf[0] */

	edma_conf->pa_conf	= pa_conf;
	edma_conf->pa_cnt	= 2;
	edma_conf->channel	= pa_conf[0].index;

	edma_transfer_inner(edma_conf);

	printk("\nafter  transfer");
	debug_buf("dst", dst, sizeof dst);
	return 0;
}
Beispiel #9
0
int _get_device_info(char **name, int *bsignal, int *flash) {

	unsigned char cm[2] = { 0x01, 0x9b };
	char *rbuf;
	int *rlen, cmret;
	char nxtname[14], nxtaddr[6];
	int bs, fl;

	if (dodebug) {
		printf("get_device_info:\n");
	}
	cmret = _do_cmd((char *)&cm, 2, (char **)&rbuf, (int *)&rlen, 2);
	if (cmret) {
		fprintf(stderr, "Failed to get device info\n");
		if (rbuf) free(rbuf);
		return(-1);
	}

	if (dodebug) {
		printf("got rlen %d\n", (int)rlen);
	}

	debug_buf(&rbuf[3], 4);

	memset(&nxtname, 0, 14);
	memset(&nxtaddr, 0, 6);
	memcpy(&nxtname, &rbuf[3], 14);
	memcpy(&nxtaddr, &rbuf[18], 6);
	memcpy(&bs, &rbuf[25], 4);
	memcpy(&fl, &rbuf[29], 4);

	if (dodebug) {
		printf("nxtname %s\n", nxtname);
		printf("nxtaddr %s\n", nxtaddr);
		printf("bs %d\n", bs);
		printf("flash %d\n", fl);
	}

	*name = malloc(strlen(nxtname)+1);
	memset(*name, 0, strlen(nxtname)+1);
	memcpy(*name, &nxtname, strlen(nxtname));
	*bsignal = bs;
	*flash = fl;

	free(rbuf);
	return(0);
}
Beispiel #10
0
static void transport_log_out(BYTE *params, TPM_DIGEST *transDigest)
{
  BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_OUT(x)];
  UINT32 len;
  tpm_sha1_ctx_t sha1;

  ptr = buf; len = sizeof(buf);
  tpm_marshal_TPM_TAG(&ptr, &len, TPM_TAG_TRANSPORT_LOG_OUT);
  tpm_marshal_TPM_CURRENT_TICKS(&ptr, &len, &tpmData.stany.data.currentTicks);
  tpm_marshal_BLOB(&ptr, &len, params, SHA1_DIGEST_LENGTH);
  tpm_marshal_TPM_MODIFIER_INDICATOR(&ptr, &len, tpmData.stany.flags.localityModifier);
  tpm_sha1_init(&sha1);
  tpm_sha1_update(&sha1, transDigest->digest, sizeof(transDigest->digest));
  tpm_sha1_update(&sha1, buf, sizeof(buf));
  tpm_sha1_final(&sha1, transDigest->digest);
  debug_buf("LogOut: transDigest: ", transDigest->digest, sizeof(transDigest->digest));
}
Beispiel #11
0
static void transport_log_in(BYTE *params, BYTE *pubKeyHash,
                             TPM_DIGEST *transDigest)
{
  BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_IN(x)];
  UINT32 len;
  tpm_sha1_ctx_t sha1;

  ptr = buf; len = sizeof(buf);
  tpm_marshal_TPM_TAG(&ptr, &len, TPM_TAG_TRANSPORT_LOG_IN);
  tpm_marshal_BLOB(&ptr, &len, params, SHA1_DIGEST_LENGTH);
  tpm_marshal_BLOB(&ptr, &len, pubKeyHash, SHA1_DIGEST_LENGTH);
  tpm_sha1_init(&sha1);
  tpm_sha1_update(&sha1, transDigest->digest, sizeof(transDigest->digest));
  tpm_sha1_update(&sha1, buf, sizeof(buf));
  tpm_sha1_final(&sha1, transDigest->digest);
  debug_buf("LogIn: transDigest: ", transDigest->digest, sizeof(transDigest->digest));
}
Beispiel #12
0
int send_deduct_money2pos(int money3B, int timeout)
{
	static unsigned short seqnum = 0;
	int fd = -1;
	int ret = -1;
	static unsigned char posmsg[POS_RET_MAX] = {0};
	int poslen = 0;
	
	fd = init_com_dev(POS_DEV, POS_BPS, POS_PAR, POS_BLOCK);
	if(fd <= 0) {
		debug(LOG_ERR, "Pos dev open error:%d\n",errno);
		return -1;
	}
	//clear history
	memset(posmsg, 0, sizeof(posmsg));
	poslen = 0;
	
	//generate new pos deduct msg
	package_deduct_msg(seqnum++, money3B, posmsg, &poslen);
	
	debug(LOG_DEBUG, "====debug, deduct msg to pos:\n");
	debug_buf(posmsg,poslen);
	
	//send to pos
	write(fd, posmsg, poslen);
	
	ret = pos_wait_result(fd, timeout);
	if(ret == 0) {
		close(fd);
		return 0;
	} else if(ret == DEAL_WAITING) {
		// need to waiting 30s for deal result??
		//it's too long for sale machine (5s)
	}
	return -1;
}
Beispiel #13
0
void assembler_print_result(struct asm_context_t *ctx)
{
    struct llist_t *loop;
    printf("================================ ASSEMBLED INFO ================================" NL);

    if (ctx->symbols.first)
    {
        struct symbol_t *s;

        printf(NL);
        printf("------------" NL);
        printf("- Symbols. -" NL);
        printf("------------" NL);

        symbols_mkloop(&ctx->symbols, &loop);
        while ((s = symbols_next(&loop)))
        {
            struct symbol_attr_t *a;
            struct llist_t *lla;

            switch (s->type)
            {
                case SYMBOL_TYPE_CONST:  printf("CONST"); break;
                case SYMBOL_TYPE_EXTERN: printf("EXTERN"); break;
                case SYMBOL_TYPE_LABEL:  printf("LABEL"); break;
                default:                 printf("-----");
            }
            printf(" \"%s\"", s->name);
            printf(", width %u", s->width);
            printf(", export %u", s->exp);
            printf(", value %06llX (%lld)", s->val64, s->val64);
            if (s->section)
                printf(", section \"%s\"", s->section);

            printf(NL);
            symbol_attr_mkloop(s, &lla);
            while ((a = symbol_attr_next(&lla)))
                printf("\tattr \"%s\" = \"%s\"" NL, a->name, a->value ? a->value : "NULL");
        }
    }

    if (ctx->relocations.first)
    {
        struct relocation_t *r;
        struct llist_t *ll;

        printf(NL);
        printf("----------------" NL);
        printf("- Relocations. -" NL);
        printf("----------------" NL);

        relocations_mkloop(&ctx->relocations, &ll);
        while ((r = relocations_next(&ll)))
        {
            printf("%s", r->type == RELOCATION_TYPE_ABOSULTE ? "ABS" : "REL");
            printf(", offset: 0x%06X", r->offset);
            printf(", length: 0x%02X", r->length);
            printf(", section: \"%s\"", r->section);
            printf(", symbol: \"%s\"", r->symbol);
            if (r->type == RELOCATION_TYPE_ABOSULTE)
                printf(", adjust: --");
            else
                printf(", adjust: %d", r->adjust);

            printf(NL);
        }
    }

    if (ctx->sections.first)
    {
        struct section_t *s;
        struct llist_t *ll;

        printf(NL);
        printf("-------------" NL);
        printf("- Sections. -" NL);
        printf("-------------" NL);
        sections_mkloop(&ctx->sections, &ll);
        while ((s = sections_next(&ll)))
        {
            printf(NL);
            printf("Section \"%s\" [%u bytes]", s->name, s->length);
            if (s->noload)
                printf(" NOLOAD" NL);
            else
                debug_buf((uint8_t*)s->data, s->length);
        }
    }

    printf(NL);
    printf("================================================================================" NL);
    printf(NL);
}
Beispiel #14
0
static int fj_handle_pro(int fd, unsigned char *buffer, int length)
{
	int outlen = 0;
	static char pd_end = 0;
	static char cp_ok = 0;
	static char SL_FLAG = 0;
	
	enum _fj_code cscode = END;
	int balance = 0x00;
	int lrc = 0;
	int ret = -1;
	unsigned char outresp[32] = {0x0};
	char tmp[3] = "";
	
	int i = 0;
	for(i=0; i < END; i++) {
		if(fj_cmds[i][0] == buffer[1] && fj_cmds[i][1] == buffer[2])
			break;
	}
	
	cscode = i;
	switch(cscode) {
		case PD:
			debug(LOG_DEBUG, "<------ PD, reset confirm.\n");
			pd_end = 1;
			memcpy(outresp, RSP_PD, 6);
			outlen = 6;
			break;
		case CR:
			debug(LOG_DEBUG, "<------ CR, check info.\n");
			if(0 == pd_end) {
			// send PW
			memcpy(outresp, RSP_PW, 6);
			outlen = 6;
			} else {
				if(cp_ok) {
					//  can recv card ;
					if(SL_FLAG)
					{
						//SL_FLAG = 0;
						
						//must have balance then canbe saled
						// send card balance 0x65 0x43 0x21
						memcpy(outresp, RSP_CR_CP_BAL, 13);
						outlen = 13;
					}
					else
					{
						memcpy(outresp, RSP_CR_CPOK, 10);
						outlen = 10;
					}
					
				} else {
					// can not recv card
					memcpy(outresp, RSP_CR_CPNOK, 10);
					outlen = 10;
				}
			}
			break;
		case CP:
			debug(LOG_DEBUG, "<------ CP, can recv card\n");
			if(0 == pd_end) {
				memcpy(outresp, RSP_PW, 6);
				outlen = 6;
			} else {
				// can recv card
				cp_ok = 1;
				memcpy(outresp, RSP_CP_OK, 6);
				outlen = 6;
			}
			break;
		case CW:
			debug(LOG_DEBUG, "<------ CW, money update.\n");
			if(0 == pd_end) {
				memcpy(outresp, RSP_PW, 6);
				outlen = 6;
			} else {
				//parse cw text
				if(length != 10) {
					debug(LOG_ERR, "CW content length error!\n");
					return -1;
				}
				snprintf(tmp,2,"%x",buffer[3]);
				balance = atoi(tmp);
				snprintf(tmp,2,"%x",buffer[4]);
				balance += atoi(tmp)*100;
				snprintf(tmp,2,"%x",buffer[5]);
				balance += atoi(tmp)*10000;
				
				debug(LOG_INFO, "deduct money: %d\n",balance);
				debug(LOG_INFO, "product number: %c%c\n",buffer[6],buffer[7]);
				
				//send to pos ,waiting for 3 seconds 
				ret = send_deduct_money2pos(balance,3000);
				if(ret == 0) {
					// update ok
					memcpy(outresp, RSP_CW_OK, 6);	
				} else {
					//update card fail
					memcpy(outresp, RSP_CW_NOK, 6);
				}
				outlen = 6;
			}
			//waiting next selected
			SL_FLAG = 0;
			break;
		case CE:
			debug("<-----VTS CE PD_STA=%d.\r\n",pd_end);
			if(0 == pd_end) {
					// send PW
					memcpy(outresp, RSP_PW, 6);
					outlen = 6;
			} else {
				//clear card
				memcpy(outresp, RSP_CE_OK, 6);
				outlen = 6;
			}
			break;
		case LR:
			debug("<-----VTS LR PD_STA=%d.\r\n",pd_end);
			//get card version,def 'A'
			if(0 == pd_end) {
					// send PW
					memcpy(outresp, RSP_PW, 6);
					outlen = 6;
			} else {
				memcpy(outresp, RSP_LR_OK, 6);
				outlen = 6;
			}
			break;
		case NS:
			debug("<-----VTS NS PD_STA=%d.\r\n",pd_end);
			SL_FLAG = 0;
			// not selected 
			if(0 == pd_end) {
					// send PW
					memcpy(outresp, RSP_PW, 6);
					outlen = 6;
			} else {
				memcpy(outresp, RSP_NS_OK, 6);
				outlen = 6;
			}
			break;
		case SL:
			debug("<-----VTS SL PD_STA=%d.\r\n",pd_end);
		
			// selected
			if(0 == pd_end) {
					// send PW
					memcpy(outresp, RSP_PW, 6);
					outlen = 6;
			} else {
				SL_FLAG = 1;
				//cp_ok = 1; //???
				memcpy(outresp, RSP_SL_OK, 6);
				outlen = 6;
			}
			break;
		case END:
			debug(LOG_WARNING, "=====Can not found CMD_SUB [0x%02x][0x%02x]!\n",buffer[1],buffer[2]);
			return -1;
		default:
			debug(LOG_WARNING, "==def===Can not found CMD_SUB [0x%02x][0x%02x]!\n",buffer[1],buffer[2]);
			return -1;
	}

	//calc LRC
	i = 2;
	lrc = outresp[1]; //start from CMD, end at ETX;
	for(i=2; i < outlen-1; i++) {
		lrc ^= outresp[i];
	}
	outresp[outlen-1] = lrc;
	
	debug(LOG_DEBUG, "---->resp to fj:\n");
	debug_buf(outresp,outlen);
	
	ret = write(fd, outresp, outlen);
	fsync(fd);
	return ret;
}
Beispiel #15
0
int fj_recv_loop(int fd, int timeout)
{
	int ret = 0, ch_num = 0, stflag = 0 , edflag = 0;
	unsigned char fjbuf[32] = {0};
	unsigned char bak = 0, ch = 0;
	fd_set readfds;
    struct timeval tv;
    if(timeout <= 0)
    	timeout = 100; //every char should be sent within 100 ms
	
	while(1) {
		FD_ZERO(&readfds);
		FD_SET(fd,&readfds);
		tv.tv_sec = timeout/1000;
		tv.tv_usec = (timeout%1000)*1000;
		ret = select(fd+1,&readfds,NULL,NULL,&tv);
		if(ret == 0) {
			//one package ending ???
			// restart
			ch = stflag = ch_num = edflag = 0;
			memset(fjbuf,0,sizeof(fjbuf));
			continue;
		} else if(ret < 0) {
			if(errno == EINTR) {
				debug(LOG_WARNING, "######cmd get_char interrupt by signal #####\n");
				continue;
			} else
				debug(LOG_ERR, "cmd get_char error,%d:%s\n",errno,strerror(errno));

 			return -1;  // error or closed
		}
		//run here! must be OK ?
		ret = read(fd,&ch,1);
		if(ret != 1) {
			debug(LOG_ERR, "cmd get_char error,%d:%s\n",errno,strerror(errno));
			return -1;
		}
		
		switch(ch) {
			case ENQ:
				if(0 == stflag) {
					debug(LOG_DEBUG, "-----< get ENQ\n");
					bak = ACK;
					write(fd, &bak, 1);
					fsync(fd);
					break;
				} else {
					//ENQ is data content. go down.
				}
				
			case STX:
				if(0 == stflag) {
					debug(LOG_DEBUG, "-----< get STX\n");
					//data start here.
					stflag = 1;
					edflag = 0;
					memset(fjbuf,0,sizeof(fjbuf));
					ch_num = 0;
					fjbuf[ch_num++] = STX;
					break;
				} else {
					//STX is data content, not start flag. go down.
				}
				
			default:
				if(stflag) {
					fjbuf[ch_num++] = ch;
					if(ch_num >= FJ_NORMAL_CMD_LEN) {
						if(fjbuf[1] == 'C' && fjbuf[2] == 'W') {
							if(ch_num >= FJ_CW_CMD_LEN) {
								// CW end.
								edflag = 1;
								break;
							} else {
								continue;
							}
						} else {
							//end. except CW need 10 bytes
							edflag = 1;
							break;
						}
					} else {
						continue;
					}
				} else {
					debug(LOG_DEBUG, "-----< get OTHERS:0x%02x\n",ch);
					break;
				}
		}
		if(edflag) {
			debug(LOG_DEBUG, "get buf length %d,dump content:\n",ch_num);
			debug_buf(fjbuf, ch_num);
			fj_handle_pro(fd, fjbuf, ch_num);
			
			ch = stflag = ch_num = edflag = 0;
			memset(fjbuf,0,sizeof(fjbuf));
		}
	}
	return 0;
}
int _do_cmd(char *sbuf, int slen, char **rbuf, int *rlen, int errpos) { 

	fd_set set;
	int maxfd = nxtsock;
	struct timeval timeout = {0,2};
	int i;
	unsigned char buf[1024], lbuf[1024];
	ssize_t x, xr;
	unsigned short int si  = (short int) slen;
	unsigned short int sr;
	char *eptr = NULL;

	FD_ZERO(&set);
	FD_SET(nxtsock, &set);

	memcpy(&buf[0], &si, 2);
	memcpy(&buf[2], sbuf, slen);

	*rbuf = NULL;

	x = write(nxtsock, &buf, slen + 2);
	if (x < 0) {
		perror("write: ");
		return(-1);
	}
	if (dodebug) { 
//		printf("write x %d: ", slen+2);
		debug_buf((unsigned char *)&buf, slen + 2);
	}

	if ((unsigned char)sbuf[0] == NXT_NORET) {
		 return(0); 
	}

	while  (select(maxfd, &set, 0, 0, &timeout) < 0) {
		printf("got nothing\n");
	}

	while ((xr = read(nxtsock, &lbuf, 2)) == -1) {
		// EMPTY nothing by default?
	}

	if (xr != 2) {
		perror("read: ");
		return(-1);
	}

	memcpy(&sr, &lbuf[0], 2);

	while ((xr = read(nxtsock, &lbuf, (int)sr)) == -1) {
		// EMPTY nothing by default
	}

//	printf("read x %d\n", xr);
	if (xr != (int)sr) { printf("got mismatch of %d and %d\n", xr, sr); }

	if ((unsigned char)lbuf[errpos] != 0) {
		int ret = (unsigned char) lbuf[errpos];
		if (dodebug) {
			eptr = _nxt_error((unsigned char) lbuf[errpos]);
			fprintf(stderr, "Error %02x: %s\n", (unsigned char)lbuf[errpos], eptr ? eptr : "Unknown");
		}
		return(ret);
	}

//	printf("return buf size %d\n", sr);
	*rbuf = (unsigned char *) malloc((int)sr);
	memcpy(*rbuf, &lbuf, (int)sr);
	*rlen = (int)sr;

	if (dodebug) {
		debug_buf(*rbuf, *rlen);
	}
	return(0);
}