void 
SockDgram_AsyncOpeartion::handleUserDefinedSystemEvent(OSAL_SOCKET sd)
{      
    os_sockaddr_in fromAddr;
    DEBUGTRACE("[UDP Server] SockDgram_AsyncOpeartion: run!");
    
    Msg_t ReadBuf;
    ReadBuf.pData_ = incomingMsg_.pData_;
    
    MW_DEBUG((LM_DEBUG,"[UDP Server] SockDgram_AsyncOpeartion: Ready to receive data!\n\n"));	
#ifdef OSAL_WIN32		
	int nRead = pOwnDevice_->read(&incomingMsg_, NULL, (os_sockaddr*)&fromAddr);
#else
	int nRead = pOwnDevice_->read(&incomingMsg_, NULL, &fromAddr);
#endif
    
    MW_DEBUG((LM_DEBUG,"[UDP Server] From IP address=%s, Port number=%d\n", 
    inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port)));

    if(nRead <= 0)
    {
        MW_DEBUG((LM_DEBUG,"[UDP Server] SockDgram_AsyncOpeartion: Error!\n"));
        perror("SockDgram Read Error:");
    }
    else
    {
        MW_DEBUG((LM_DEBUG,"[UDP Server] SockDgram_AsyncOpeartion: dispatch!\n"));
        ReadBuf.len_= nRead;
#ifdef OSAL_WIN32		
		pOwnDevice_->dispatch(&fromAddr, nRead, ACT_READEVENT);
#else
		pOwnDevice_->dispatch(&fromAddr, &ReadBuf, ACT_READEVENT);	
#endif
    }   
}
void 
SockStream_SyncOpeartion::run()
{
	DEBUGTRACE("SockStream_SyncOpeartion::run");
	
	while(pOwnDevice_->getState() == IODevice::IODEV_ACTIVE)
	{
		MW_DEBUG((LM_DEBUG,"SockStream_SyncOpeartion: Ready to receive data!\n"));	
	}
	
	pOwnDevice_->close();
}
Beispiel #3
0
void
MessageBroker::recvNAL(void* arg, byte srcAddr, Msg_t* InputMsg)
{
	DEBUGTRACE("NAL::recvNAL");
	byte cmdType = InputMsg->pData_[0];

	//Incoming Command Message
	switch(cmdType)
	{
	case NAL_CMD_ROUTING_INFO_REQ:
		DEBUGTRACE("This message is Routing Information Request!\n");
		lnkNetworkManager_->processRoutingInformationRequest(arg, srcAddr);
		break;
	case NAL_CMD_ROUTING_INFO_REP:
		DEBUGTRACE("This message is Routing Information Response!\n");
		lnkNetworkManager_->processRoutingInformationResponse(arg, srcAddr, InputMsg);
		break;
	default:
		DEBUGTRACE("Invalid NAL type Message\n");
		exit(1);
	}
}
	int VirtualMachine::CallInterpreted(int address)
	{
		word w;
		int i, argCount = 0;

		/* Set up call. */
		OpPUSH(w);
		DEBUGTRACE("Starting with PC=%d, DP=%d, RP=%d to %d\n", PC, DP, RP, address);
		w.int4 = (argCount + 2) * sizeof(word);
		OpENTER(w);
		i = 8;
		/**w.int4 = arg0; Marshal(i, w); i += 4;
		w.int4 = arg1; Marshal(i, w); i += 4;
		w.int4 = arg2; Marshal(i, w); i += 4;
		w.int4 = arg3; Marshal(i, w); i += 4;
		w.int4 = arg4; Marshal(i, w); i += 4;
		w.int4 = arg5; Marshal(i, w); i += 4;
		w.int4 = arg6; Marshal(i, w); i += 4;
		w.int4 = arg7; Marshal(i, w); i += 4;
		w.int4 = arg8; Marshal(i, w); i += 4;
		w.int4 = arg9; Marshal(i, w); i += 4;
		w.int4 = arg10; Marshal(i, w); i += 4;
		w.int4 = arg11; Marshal(i, w); i += 4;
		w.int4 = arg12; Marshal(i, w); i += 4;*/
		w.int4 = address;
		Push(w);
		OpCALL(w);
		DEBUGTRACE("Upon running PC=%d, DP=%d, RP=%d\n", PC, DP, RP);
		Run();
		DEBUGTRACE("At finish PC=%d, DP=%d, RP=%d\n", PC, DP, RP);
		w.int4 = (argCount + 2) * sizeof(word);
		OpLEAVE(w);
		OpPOP(w);
		PC = romSize + 1;
		return 0;
	}
Beispiel #5
0
void RS_SendDataPack(serial_driver_t *rs, unsigned char *buf, int len)
{
	U32 etime=GetTickCount();
	DBPRINTF("485 send:%d\n",len);
//// 485收发控制	低电平收,高电平发
	RS485_setmode(TRUE);
	DelayUS(1000);//
	DEBUGTRACE(buf,len);
//	rs->write(0x88);//多发送一个字节,修正485网络干扰
	rs->write_buf(buf,len);
	rs->tcdrain();
	while(rs->get_lsr()!=TIOCSER_TEMT);	//wait for the transmit shift register empty
	DBPRINTF("get_lsr() time:%d\n",GetTickCount()-etime);
//	mmsleep(3*115200/gOptions.RS232BaudRate);
	RS485_setmode(FALSE);
}
void 
SockDgram_IOChannel_Thread::run()
{	
    sockaddr_in fromAddr;
    Msg_t incomingBuf, ReadBuf;

	memset(&fromAddr,0, sizeof(sockaddr_in));
    
	incomingBuf.len_ = pSockDgram_IOChannel_->getSizeofBuffer();
	incomingBuf.pData_ = ReadBuf.pData_ = pSockDgram_IOChannel_->getBuffer();
	
	int nRead = 0;
	DEBUGTRACE("SockDgram_SyncOpeartion: run!");
	
	while((pSockDgram_IOChannel_ != NULL) ) 
	{
		MW_DEBUG((LM_DEBUG,"SockDgram_SyncOpeartion: Ready to receive data!\n\n"));	
//		int nRead = pSockDgram_IOChannel_->read(&incomingBuf, &fromAddr);
		  		
		MW_DEBUG((LM_DEBUG,"[UDP] From IP address=%s, Port number=%d\n", 
		inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port)));

        if(nRead > 0)
		{
			ReadBuf.len_= nRead;
			
			pSockDgram_IOChannel_->dataIndication(NULL, &ReadBuf);
			MW_DEBUG((LM_DEBUG,"SockDgram_IOChannel_Thread::Read data is succeeded nRead %d!\n",nRead));
		}
		else
		{
			MW_DEBUG((LM_DEBUG,"SockDgram_IOChannel_Thread:: Error on reading data"));
			pSockDgram_IOChannel_->close();
			break;
		}
	}  
}
Beispiel #7
0
int  search_file(char *szInputFile)
{
      int         iStatusFlag       = 0;                    /* Return value */
      int         iTokenLength      = 0;
      char        szStr[MAXLINE+1];
      long        lStartOffset      = 0L;

      FILE        *fpInFile;

      /*-------------------------------------*/

      iTokenLength = strlen(szToken);

      if (0 >= iTokenLength)
      {
            pszErrMsg   = "Token is too short (ie. < 1)";
            fprintf(stderr, pszErrMsg);
            DEBUGTRACE(TRACEFILE, pszErrMsg);
            return(1);
      }

      if (0 == (fpInFile = fopen(szInputFile, "rb")))
      {
            fprintf(stderr, "Cannot open input file: %s", szInputFile);
            return(1);
      }

      lStartOffset = (long) find_str_in_file(fpInFile, lStartOffset, szToken);

      if (0 <= lStartOffset)
      {
            fprintf(stdout, "\n - %s\n", szInputFile);
            /* First found -> write filename */
      }

      while (0 <= lStartOffset)
      {
            ON_DEBUG { TRACE(TRACEFILE, "\n\t%s found in %s offset %lu",
                        szToken, szInputFile, lStartOffset); }

            /* Write to file if debug flag in active */

            lStartOffset += (long)strlen(szToken);

            /* Go beyond token */

            fseek(fpInFile, lStartOffset, SEEK_SET);

            /* Go to start offset */

            fgets(szStr, MAXLINE, fpInFile);

            /* Get the string after token */

            strtrimr(&szStr);                   /* Remove trailing line feed */

            fprintf(stdout, "%s\n", szStr);

            /* Write string */

            lStartOffset += (long)strlen(szStr);

            /* Next search starts from end of string */

            lStartOffset = (long)find_str_in_file(fpInFile,
                  (long)lStartOffset, szToken);

            /* Find next token */
      }
      fclose(fpInFile);

      return(iStatusFlag);                      /* Return default value */
}
Beispiel #8
0
int 
SockDgram::doOpen(void *fileDescriptor)
{
	DEBUGTRACE("SockDgram::doOpen");

	//read configuration file
	OSAL_UNUSED_ARG(fileDescriptor);

	//set operation mode
	this->setOpMode(pSysEventProcessor_->getOpMode());

	if(this->getOpMode() == OSAL::OPMODE_ASYNC)
	{
		//pfRead_ = SockDgram::asyncRead;
		//pfWrite_ = SockDgram::asyncWrite;
		pfRead_ = SockDgram::read;
		pfWrite_ = SockDgram::write;

#ifdef OSAL_WIN32

		if(this->openAsyncSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED, _ACCEPT_USE_) == INVALID_SOCKET)
		{
			return INVALID_SOCKET;
		}
#else
		pOlReadACT_ = NULL;
		pOlWriteACT_= NULL; 

		if(this->socket(AF_INET, SOCK_DGRAM, 0) == -1)
		{
			perror("Failed to open socket");
			return -1;
		}

		int m_sockfd = this->getSD();
		int flag = fcntl(m_sockfd, F_GETFL, 0);            // socket status flags
		fcntl(m_sockfd, F_SETFL, flag | O_NONBLOCK);
#endif
	}
	else	/*	Synchronous mode*/
	{
		pfRead_ = SockDgram::read;
		pfWrite_ = SockDgram::write;

		//create socket
		if(this->openSyncSocket(AF_INET, SOCK_DGRAM, 0) == -1)
		{
			return -1;
		}

	}

	//set socket options
#ifdef OSAL_WIN32
	BOOL bOptVal = TRUE;
	int bOptLen = sizeof(BOOL);

	SocketControlParam_t scp[] = {
		{this->getSD(_ACCEPT_USE_), SOL_SOCKET, MW_SO_BROADCAST, (char*)&bOptVal, &bOptLen},				
	};
#else
	int one = 1;
	SocketControlParam_t scp[] = {
		{this->getSD(), SOL_SOCKET, SO_BROADCAST, &one},				
	};    
#endif

	if(control(SETPARAMS, scp) == -1 )
	{
		MW_ERRMSG((LM_DEBUG,"SockDgram::Broadcast is not enabled\n"));
		return -1;
	}

	//bind socket
	if(this->bind(ConfigParam_.ipAddr_, ConfigParam_.port_) != MW_SUCCESS)
		return MW_ERROR;

	MW_DEBUG((LM_DEBUG,"[Dev:Socket Dgram] UDP Service is ready to start!\n"));
	return MW_SUCCESS;
}
Beispiel #9
0
MY_DLLEXPORT OWPL_RESULT
owplInit(	const int asyncCallbackMode,
			const int udpPort, 
			const int tcpPort, 
			const int tlsPort, 
			const char* szBindToAddr,
			const int bUserSequentialPorts)
{
	int i;

	phcb = (phCallbacks_t * ) malloc(sizeof(phCallbacks_t));
	memset(phcb, 0, sizeof(phCallbacks_t));

	phcfg.asyncmode = asyncCallbackMode;

	ph_avcodec_init();
	ph_calls_init();

	if (udpPort > 0) {
		snprintf(phcfg.sipport, sizeof(phcfg.sipport), "%d", udpPort);
	}

	if (phcfg.use_tunnel)
	{
		i = ph_tunnel_init();
		if (i)
		{
			return i;
		}
	}

#ifdef FORCE_VAD
/* HACK for test */
#ifdef EMBED
	phcfg.vad = VAD_VALID_MASK | (500 & VAD_THRESHOLD_MASK);
#else
	phcfg.vad = VAD_VALID_MASK | (1000 & VAD_THRESHOLD_MASK);
#endif
#endif

#ifdef FORCE_CNG
  /* HACK for test */
  phcfg.cng = 1;
#endif

	ph_media_init(phcfg.use_tunnel, phcfg.plugin_path);

	i = eXosip_init(0, 0, atoi(phcfg.sipport), phTunnel);

	if (i)
	{
		return OWPL_RESULT_FAILURE;
	}

	{
		/* <UOLFONE> */
		/*
		const char * ua  = WENGOPHONE_UA;
		eXosip_set_user_agent(ua);
		*/
		if (phcfg.user_agent[0])
		{
			eXosip_set_user_agent(phcfg.user_agent);
		}
		else
		{
			const char * ua  = UOLFONECLIENT_UA;
			eXosip_set_user_agent(ua);
		}
		/* <UOLFONE> */
	}

	ph_nat_init();

/* This should be done in the setter of phcfg.proxy
	if (phcfg.force_proxy)
	{
		eXosip_force_proxy(phcfg.proxy);
	}
*/

	{
		char contact[512];

		eXosip_guess_contact_uri(phcfg.identity, contact, sizeof(contact), 1);
		eXosip_set_answer_contact(contact);
	}

	ph_vlines_init();

	ph_payloads_init();

	if (!phcfg.audio_dev || phcfg.audio_dev[0] == '\0')
	{
		// Set default audio device if no one has been set before
		owplAudioSetConfigString(0);
	}

	/* register callbacks? */
	eXosip_set_mode(EVENT_MODE);

	if (!phcfg.asyncmode)
	{
		phWaitTimeout = 1;
	}
	else 
	{
		phWaitTimeout = 500;
	}

	if (phcfg.asyncmode)
	{
		osip_thread_create(20000, ph_api_thread, 0);
	}

	ph_media_stop_mutex = g_mutex_new();

	phIsInitialized = 1;

	DEBUGTRACE("PhInit finished\n");

	return OWPL_RESULT_SUCCESS;
}
Beispiel #10
0
int RS232Check(serial_driver_t *rs)
{
	int waitingChars = 0;
	char buf[MaxLen];
	int len = 0;
	int i = 0;
	/*sleep(1);
	char Sender[SENDER_LEN];
	//获取发送方ip and port
	memset(Sender, 0, SENDER_LEN);
	memcpy(Sender, (void*)&rs, sizeof(rs));
	SendRSData(buf,4, Sender);*/


	if((waitingChars = rs->poll()) == 0) return 0;

	//DBPRINTF("---Begin poll size:%d   %d\n",waitingChars,COMMBUFLEN_MAX_RS485_232);

	memset(buf,0x00,MaxLen);
	//接收完所有数据,考虑中间中断等,须分多次接收
/*	do
	{
		if(len >= COMMBUFLEN_MAX_RS485_232)
		{
			printf("------RS232Check:Get data len  = %d\n",len);
			break;
		}

		waitingChars = rs->read_buf(buf+len,waitingChars);
		len += waitingChars;

		// 如果是9600, 传输1 byte,约要1ms,
		mmsleep(10);
		//DBPRINTF("Get data  packet len  = %d\n",len);
		waitingChars=rs->poll();
		if(waitingChars > 0)
		{
			i = 5;
		}
		else
		{
			mmsleep(10);
			i--;
		}
	}
	while(i);

	printf("RS232Check:Get data len  = %d\n",len);
*/
	TC3Cmdstruct c3buf;
	int result;
	char sendbuf[MaxLen];
	int sendlen = 0;

	memset(sendbuf, 0 , sizeof(sendbuf));
	i = 5;
	while(i--)
	{
		len = RS232WaitRespose(rs,gOptions.DeviceID,buf,200);
		printf("len %d\n",len);
		//len = 8;
		//buf[0]=0xaa;buf[1]=0x01;buf[2]=0x01;buf[3]=0x00;buf[4]=0x00;buf[5]=0x50;buf[6]=0x3c;buf[7]=0x55;
		if(0 > len)
		{
			return 0;
		}
		memset(&c3buf, 0, sizeof(TC3Cmdstruct));
		DEBUGTRACE(buf,len);
		result = Process_ReceiveData(buf,len,&c3buf.framehead,RS485_232_MODE);

		if(result==  EC_FINISH)
		{
			printf("c3 cmd = %d,data's len = %d \n ",c3buf.command,c3buf.datalen);
			BYTE currcmd = c3buf.command;
	//		if(process_comm_cmd(&c3buf.command,c3buf.buf,c3buf.datalen,sendbuf,&sendlen,RS485_232_MODE) == CMD_SUCCESS)
			process_comm_cmd(&c3buf.command,c3buf.buf,c3buf.datalen,sendbuf,&sendlen,RS485_232_MODE) ;
			{
	//			if(currcmd == c3buf.command)
	//				c3buf.command = 200;//命令执行成功
				c3buf.datalen = sendlen;//
				if(sendlen)
					memcpy(c3buf.buf,sendbuf,sendlen);
				Make_SendData(c3buf.buf,c3buf.command,c3buf.datalen,sendbuf,&sendlen);

				char Sender[SENDER_LEN];
				//获取发送方ip and port
				memset(Sender, 0, SENDER_LEN);
				memcpy(Sender, (void*)&rs, sizeof(rs));
				SendRSData(sendbuf,sendlen, Sender);
				printf("Rs232 runcommcmd OK ! sendlen = %d \n ",sendlen);
				return 0;
			}
	//		else
	//			printf("RS232 runcommcmd error ! \n ");
		}
		else
		{
			printf("-RS232Check error = 0x%x,\n", 	result);
		}
	}
	return -1;
}
Beispiel #11
0
OP_STATUS P2PConnection::OnWrite(OpByteBuffer *sendbuffer)
{
	ENTER_METHOD;

	AddRef();

	if(m_closing)
	{
		// we are closing, do nothing
		Release();
//		m_observer = &m_nullobserver;
		if(m_socket.Get() != NULL)
		{
//			m_socket.Get()->SetObserver(&m_nullobserver);
		}
		return OpStatus::OK;
	}
	if(sendbuffer == NULL)
	{
//		OP_ASSERT(FALSE);
//		sendbuffer = m_output;
		return OpStatus::ERR_NULL_POINTER;
	}
	if(sendbuffer->DataSize() == 0)
	{
		Release();

		return OpStatus::OK;
	}
	OP_STATUS status;
	unsigned char* buffer;
	UINT32 buffersize;

	buffer = sendbuffer->Buffer();
	buffersize = sendbuffer->DataSize();

	if(m_upload)
	{
		DEBUGTRACE_UP(UNI_L("UL: WRITING %d bytes to host: "), buffersize);
		DEBUGTRACE_UP(UNI_L("%s\n"), (uni_char *)m_address);
	}
	else
	{
		DEBUGTRACE(UNI_L("DL: WRITING %d bytes to host: "), buffersize);
		DEBUGTRACE(UNI_L("%s\n"), (uni_char *)m_address);
	}
	m_last_buffer_sent = buffer;
	status = m_socket.Get()->Send((void *)buffer, buffersize);
	if(status != OpStatus::OK)
	{
	}
	else
	{
	}

	LEAVE_METHOD;

	Release();

	return status;
}
	int VirtualMachine::readProgram(std::istream & input)
	{
		qvm_header_t qvminfo;
		int i, n;
		uint1_t x[4];
		word w;

		DEBUGTRACE("Loading file...\n");
		qvminfo.magic = readInt(input); /* magic. */
		if (qvminfo.magic != QVM_MAGIC)
		{
			DEBUGTRACE("Invalid magic");
			throw InvalidProgramException();
			//q3vm_error("Does not appear to be a QVM file.");
			/* XXX: option to force continue. */
			return 0;
		}
		DEBUGTRACE("Magic OK\n");
		/* variable-length instructions mean instruction count != code length */
		qvminfo.inscount = readInt(input);
		qvminfo.codeoff = readInt(input);
		qvminfo.codelen = readInt(input);
		qvminfo.dataoff = readInt(input);
		qvminfo.datalen = readInt(input);
		qvminfo.litlen = readInt(input);
		qvminfo.bsslen = readInt(input);

		/* Code segment should follow... */
		/* XXX: use fseek with SEEK_CUR? */
		DEBUGTRACE("Searching for .code @ %d from %d\n", qvminfo.codeoff, input.tellg());

		//  rom = (q3vm_rom_t*)(hunk);  /* ROM-in-hunk */
		rom = (Instruction*)calloc(qvminfo.inscount, sizeof(rom[0]));
		while (input.tellg() < qvminfo.codeoff)
			readByte(input);
		while (romSize < qvminfo.inscount)
		{
			n = readByte(input);
			w.int4 = 0;
			if ((i = opcodeParameterSize(n)))
			{
				x[0] = x[1] = x[2] = x[3] = 0;
				input.readsome((char*)x, i);
				w.uint4 = (x[0]) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24);
			}
			rom[romSize].Operation = n;
			rom[romSize].Parameter = w;
			romSize++;
		}
		DEBUGTRACE("After loading code: at %d, should be %d\n", input.tellg(), qvminfo.codeoff + qvminfo.codelen);

		/* Then data segment. */
		//  ram = hunk + ((romlen + 3) & ~3);  /* RAM-in-hunk */
		ram = hunk;
		DEBUGTRACE("Searching for .data @ %d from %d\n", qvminfo.dataoff, input.tellg());
		while (input.tellg() < qvminfo.dataoff)
			readByte(input);
		for (n = 0; n < (qvminfo.datalen / sizeof(uint1_t)); n++)
		{
			i = input.readsome((char*)x, sizeof(x));
			w.uint4 = (x[0]) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24);
			*((word*)(ram + ramSize)) = w;
			ramSize += sizeof(word);
		}
	
		/* lit segment follows data segment. */
		/* Assembler should have already padded properly. */
		DEBUGTRACE("Loading .lit\n");
		for (n = 0; n < (qvminfo.litlen / sizeof(uint1_t)); n++)
		{
			i = input.readsome((char*)x, sizeof(x));
			memcpy(&(w.uint1), &x, sizeof(x));  /* no byte-swapping. */
			*((word*)(ram + ramSize)) = w;
			ramSize += sizeof(word);
		}
		/* bss segment. */
		DEBUGTRACE("Allocating .bss %d (%X) bytes\n", qvminfo.bsslen, qvminfo.bsslen);
		/* huge empty chunk. */
		ramSize += qvminfo.bsslen;

		hunkFree = hunkSize - ((ramSize * sizeof(uint1_t)) + 4);

		DEBUGTRACE("VM hunk has %d of %d bytes free (RAM = %d B).\n", hunkFree, hunkSize, ramSize);
		if (ramSize > hunkSize)
		{
			throw OutOfMemoryException();
			return 0;
		}

		/* set up stack. */
		{
			int stacksize = 0x10000;
			dataStack = ramSize - (stacksize / 2);
			returnStack = ramSize;
			//returnStack = dataStack+4;
			RP = returnStack;
			DP = dataStack;
		}

		/* set up PC for return-to-termination. */
		PC = romSize + 1;

		ramMask = ramSize;

		return 1;
	}