Пример #1
0
/*!
********************************************************************************
@brief	Timeout stoppen

stopt einen Timer.

@usage
\code
static	TIMEOUT_HDL	_to_hdl;

extern	void	my_fkt (void)
{
	TIMEOUT_RET	ret;
	
	ret = timeout_stop( &_to_hdl);
}

\endcode

@warning 	-
@bug 		-
@see		-

@return	-
********************************************************************************
*/
extern	TIMEOUT_RET	timeout_stop (
	TIMEOUT_HDL	*hdl)		//!< \c m: Referenz auf das Timerhandle
{
	TIMEOUT_HDL	*my;
	TIMEOUT_HDL	*old;

	if (!(_mstate & _MSTATE_INIT))
	{
		_RETURN(TIMEOUT_RET_E_NOT_INIT);
	}
	
	//Dieses Handle in der Liste finden und aushaengen
	if (_entry.next == NULL)
	{
		_RETURN(TIMEOUT_RET_NOT_RUNING);
	}

	for (my = _entry.next, old = NULL; my != NULL; old = my, my = my->next)
	{
		if (my == hdl)
		{
			/* Gefunden ! */
			// Aus Liste austragen
			if (old == NULL)
			{
				_entry.next = my->next;
			}
			else
			{
				old->next = my->next;
			}

			/* Zeit korrigierne, falls es nicht das letzte Handle war*/
			if (my->next != NULL)
			{
				my->next->time_ms += hdl->time_ms;
			}
// 			return;
			break;
		}
	}

	_timer_dump();
	
	_RETURN(TIMEOUT_RET_OK);
}
Пример #2
0
/*!
********************************************************************************
@brief	Timeout anlegen

Initialisiert ein Timeout handler.

@usage
\code
static	TIMEOUT_HDL	_to_hdl;

static	void _timeout_handler (void *arg)
{
	// Do something
}


extern	void	my_fkt (void)
{
	TIMEOUT_RET	ret;
	
	ret = timeout_init();
	if (ret != TIMEOUT_RET_OK)
	{
		_DBGF(VOID, ERROR, "Dings Fehler ret: %u", ret);
		return;
	}
	
	ret = timeout_create( &_to_hdl, _to_hdl, NULL, "MY MODUL Timer");
	if (ret != TIMEOUT_RET_OK)
	{
		_DBGF(VOID, ERROR, "Dings Fehler ret: %u", ret);
		return;
	}
}
\endcode

@warning 	-
@bug 		-
@see		-

@return	-
********************************************************************************
*/
extern	TIMEOUT_RET	timeout_create (
	TIMEOUT_HDL	*hdl,		//!< \c o: Referenz auf das zu initialisierende Handle
	TIMEOUT_CB	callback,	//!< \c i: Handle, das bei einem Timeout aufgerufen wird
	void		*arg,		//!< \c i: Argument, mit dem die Callbackfunktion aufgerufen wird.
	char		*name)		//!< \c i: Name fuer das Timeout handle -> maximal TIMEOUT_NAMELEN lan
{
	if (!(_mstate & _MSTATE_INIT))
	{
		_RETURN(TIMEOUT_RET_E_NOT_INIT);
	}
	
	memset(hdl, 0, sizeof(TIMEOUT_HDL));
	
	hdl->cb = callback;	//!< Callback bei Timeout
	hdl->cb_arg = arg;	//!< Argument des Callbacks
// 	hdl->next = NULL;	//!< naechstes Handle in der aus
	
	strncpy(hdl->name, name, TIMEOUT_NAMELEN);

	_RETURN(TIMEOUT_RET_OK);
}
Пример #3
0
/*!
********************************************************************************
@brief	Timeout Modul initialisieren

Initialisiert das Timeout Modul.

@usage
\code

\endcode

@warning 	-
@bug 		-
@see		-

@return	-
********************************************************************************
*/
extern	TIMEOUT_RET	timeout_init (void)
{

	if (_mstate & _MSTATE_INIT)
	{
		_RETURN(TIMEOUT_RET_E_ALREADY_INIT);
	}

	_mstate |= _MSTATE_INIT;
	gettimeofday(&_tv_old, NULL);
	
	return TIMEOUT_RET_OK;
}
Пример #4
0
int GetSourceLine( SOURCEFILE *ps, char *Dst, int MaxLen )
{
    char  c,word[TOKEN_MAX_LEN];
    char* Src = (char*)malloc(RAW_SOURCE_MAX);
    int   i,idx;
    int   len,eof;

NEXT_LINE:
    do
    {
        if( !GetTextLine(ps, Src, RAW_SOURCE_MAX, &len, &eof) )
            _RETURN(-1);
    } while( !len && !eof );

    if( !len && eof )
    {
        if( ps->ccDepthIn != ccDepth )
            { Report(ps,REP_ERROR,"#endif mismatch in file"); _RETURN(0); }
        _RETURN(0);
    }

    /*
    // Process any '#' directives
    */
    if( Src[0]=='#' )
    {
        idx = 1;
        c = Src[idx++];
        if( (c<'A'||c>'Z') && (c<'a'||c>'z') )
            { Report(ps,REP_ERROR,"Expected {a-z} after #"); _RETURN(-1); }
        i=0;
        word[i++]=c;
        while( i<(TOKEN_MAX_LEN-1) )
        {
            c = Src[idx++];
            if( c==' ' || c==0x9 || !c )
                break;
            word[i++]=c;
        }
        word[i]=0;

        /* Make sure the process functions see the final NULL */
        if( !c )
            idx--;

        if( !stricmp( word, "ifdef" ) )
        {
            if( !IfDefProcess( ps, Src+idx, 1 ) )
                _RETURN(-1);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "ifndef" ) )
        {
            if( !IfDefProcess( ps, Src+idx, 0 ) )
                _RETURN(-1);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "else" ) )
        {
            if( !ElseProcess( ps, Src+idx ) )
                _RETURN(-1);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "endif" ) )
        {
            if( !EndifProcess( ps, Src+idx ) )
                _RETURN(-1);
            goto NEXT_LINE;
        }

        if( ccDepth && !(ccStateFlags[ccDepth-1]&CCSTATEFLG_TRUE) )
            goto NEXT_LINE;

        if( !stricmp( word, "error" ) )
        {
            Report(ps,REP_ERROR,"%s",Src+idx);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "warn" ) )
        {
            Report(ps,REP_WARN1,"%s",Src+idx);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "note" ) )
        {
            Report(ps,REP_INFO,"%s",Src+idx);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "include" ) )
        {
            if( !LoadInclude( ps, Src+idx ) )
                _RETURN(-1);
            goto NEXT_LINE;
        }
        if( !stricmp( word, "define" ) )
        {
            EquateProcess( ps, Src+idx );
            goto NEXT_LINE;
        }
        if( !stricmp( word, "undef" ) )
        {
            UndefProcess( ps, Src+idx );
            goto NEXT_LINE;
        }
        Report(ps,REP_ERROR,"Unknown # directive");
        _RETURN(-1);
    }

    /*
    // Not '#' directive, process as string
    */

    if( ccDepth && !(ccStateFlags[ccDepth-1]&CCSTATEFLG_TRUE) )
        goto NEXT_LINE;

    idx = 0;
    if( !ParseSource( ps, Src, Dst, &idx, MaxLen ) )
        _RETURN(0);

    Dst[idx] = 0;
    _RETURN(idx);
}
Пример #5
0
RES ResourceFormatPBM::load(const String &p_path,const String& p_original_path,Error *r_error) {

#define _RETURN(m_err)\
{\
	if (r_error)\
		*r_error=m_err;\
	ERR_FAIL_V(RES());\
}


	FileAccessRef f=FileAccess::open(p_path,FileAccess::READ);
	uint8_t saved=0;
	if (!f)
		_RETURN(ERR_CANT_OPEN);

	PoolVector<uint8_t> token;

	if (!_get_token(f,saved,token)) {
		_RETURN(ERR_PARSE_ERROR);
	}

	if (token.size()!=2) {
		_RETURN(ERR_FILE_CORRUPT);
	}
	if (token[0]!='P') {
		_RETURN(ERR_FILE_CORRUPT);
	}
	if (token[1]!='1' && token[1]!='4') {
		_RETURN(ERR_FILE_CORRUPT);
	}

	bool bits = token[1]=='4';

	if (!_get_token(f,saved,token)) {
		_RETURN(ERR_PARSE_ERROR);
	}

	int width = _get_number_from_token(token);
	if (width<=0) {
		_RETURN(ERR_FILE_CORRUPT);
	}


	if (!_get_token(f,saved,token)) {
		_RETURN(ERR_PARSE_ERROR);
	}

	int height = _get_number_from_token(token);
	if (height<=0) {
		_RETURN(ERR_FILE_CORRUPT);
	}


	Ref<BitMap> bm;
	bm.instance();
	bm->create(Size2i(width,height));

	if (!bits) {

		int required_bytes = width*height;
		if (!_get_token(f,saved,token,false,true)) {
			_RETURN(ERR_PARSE_ERROR);
		}

		if (token.size()<required_bytes) {
			_RETURN(ERR_FILE_CORRUPT);
		}

		PoolVector<uint8_t>::Read r=token.read();
			
		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {


				char num = r[i*width+j];
				bm->set_bit(Point2i(j,i),num=='0');
			}

		}



	} else {
		//a single, entire token of bits!
		if (!_get_token(f,saved,token,true)) {
			_RETURN(ERR_PARSE_ERROR);
		}
		int required_bytes = Math::ceil((width*height)/8.0);
		if (token.size()<required_bytes) {
			_RETURN(ERR_FILE_CORRUPT);
		}

		PoolVector<uint8_t>::Read r=token.read();
		int bitwidth = width;
		if (bitwidth % 8)
			bitwidth+=8-(bitwidth%8);

		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {

				int ofs = bitwidth*i+j;

				uint8_t byte = r[ofs/8];
				bool bit = (byte>>(7-(ofs%8)))&1;

				bm->set_bit(Point2i(j,i),!bit);

			}

		}

	}

	return bm;


}
Пример #6
0
/*!
********************************************************************************
@brief	Timeout neu starten

startet einen Timer mit einem neuen Timeout. Der Alte Wert wird geloescht.

@usage
\code
static	TIMEOUT_HDL	_to_hdl;

static	void _timeout_handler (void *arg)
{
	// Do something
}


extern	void	my_fkt (void)
{
	TIMEOUT_RET	ret;
	
	ret = timeout_restart( &_to_hdl,	500);
}
\endcode

@warning 	-
@bug 		-
@see		-

@return	-
********************************************************************************
*/
extern	TIMEOUT_RET	timeout_start (
	TIMEOUT_HDL	*hdl,		//!< \c m: Referenz auf das Timerhandle
	int		timeout_ms)	//!< \c i: Zeit in [ms] nach der Timeout kommen soll
{
	if (!(_mstate & _MSTATE_INIT))
	{
		_RETURN(TIMEOUT_RET_E_NOT_INIT);
	}
	
	timeout_stop(hdl);

	
	hdl->next = NULL;
	hdl->time_ms = timeout_ms;
	hdl->time_ms_startval = timeout_ms;

	_DBGF(VOID, NOTICE, "set timer %s to %d", hdl->name, timeout_ms);

	if (_entry.next == NULL)
	{
		// Erstes Element, Timer neu starten
		_get_timediff_last_ms(NULL); 
		_entry.next = hdl;
	}
	else
	{
		if (_entry.next->time_ms > hdl->time_ms)
		{
			// Hdl am Anfang einfuegen, vorher zeit korrigieren
			_entry.next->time_ms -= hdl->time_ms;
			hdl->next = _entry.next;
			_entry.next = hdl;
			_DBGF(VOID, NOTICE, "set to first timer");
		}
		else
		{
			// Hdl in der Kette einfuegen
			TIMEOUT_HDL	*my;
			
			for (my = _entry.next; my != NULL; my = my->next)
			{
				hdl->time_ms -= my->time_ms;
				if ((my->next == NULL) ||
				    (my->next->time_ms > hdl->time_ms))
				{
					// Davor einfuegen
					if (my->next != NULL)	
					{	// Kommt noch einer hinten drann -> Vorher abziehen
						my->next->time_ms -= hdl->time_ms;
					}

					//Einfuegen
					hdl->next = my->next; // auch NULL
					my->next = hdl;
					
					break; // Ende
				}
			}
		}
	}
	_timer_dump();
	
	_RETURN(TIMEOUT_RET_OK);
}
Пример #7
0
/*!
********************************************************************************
@brief	Timouts ueberpruefen

Diese Funktion ueberprueft die angemeldeten Timeout handler. Im Kontext dieser 
Funktion werden ggf. die Callback Funktion ausgefuehrt.

@usage
\code

\endcode

@warning 	-
@bug 		-
@see		-

@return	-
********************************************************************************
*/
extern	TIMEOUT_RET	timeout_check (
	struct timespec	**timeout)	//!< \c o: naechste Timeout Variable oder NULL (Pointer, die Struktur wird intern gehalten)
{
	int		timediff_ms;
	TIMEOUT_HDL	*my;
	TIMEOUT_HDL	*my_old;

	if (!(_mstate & _MSTATE_INIT))
	{
		_RETURN(TIMEOUT_RET_E_NOT_INIT);
	}
	
	// --- Neuen Timeout bestimmen ---
	if (_entry.next == NULL)
	{
		// Es stehen keine Timeouts an
		*timeout = NULL;	
	}
	else
	{
		//--->>> bereits abgelaufene Zeit bestimmen und ggf. Timeouts ausfuehren
		_get_timediff_last_ms(&timediff_ms);
#if 0
		if (time == SYS_ARCH_TIMEOUT) {
		/* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
		could be fetched. We should now call the timeout handler and
		deallocate the memory allocated for the timeout. */
		tmptimeout = timeouts->next;
		timeouts->next = tmptimeout->next;
		h = tmptimeout->h;
		arg = tmptimeout->arg;
		memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
		if (h != NULL) {
			LWIP_DEBUGF(SYS_DEBUG, ("ssw h=%p(%p)\n", (void *)h, (void *)arg));
			h(arg);
		}

		/* We try again to fetch a message from the mbox. */
		goto again;
		} else {
		/* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
		occured. The time variable is set to the number of
		milliseconds we waited for the message. */
		if (time <= timeouts->next->time) {
		timeouts->next->time -= time;
		} else {
		timeouts->next->time = 0;
		}
		}
#endif
		
		// Handle erst aus der Liste herrausloesen + Timer korrigieren, dann Callbacks ausfuehren
		
		// Solange Callbacks ausfuehren + CB loeschen, bis die Zeit erreicht wurde, dann verbleibende Timeoutzeit abspeichern
		for ( my = _entry.next, my_old = NULL; my != NULL && (timediff_ms > 0); my_old = my, my = my->next)
		{
			// Zeit treffer / ueberschreitung?
			if (my->time_ms <= timediff_ms)
			{
				// Zeit ist abgelaufen 

				// Timer aus Liste entfernen 
				if (my_old == NULL)
				{
					 _entry.next = my->next; // Handler aus der Liste entfernen
				}
				else
				{
					my_old->next = my->next; // Handler aus der Liste entfernen
				}
				
				//Callback ausfuehren
				_DBGF(VOID, NOTICE, "exec Timer %s my->time_ms %u <= timediff_ms %u", my->name, my->time_ms, timediff_ms);
				my->cb(my->cb_arg);	// Handler ausfuehren

				break; // ICh treffe die Annahme, dass es kaum verzoegerungen bei der Ausfuehrung der Timer gab.
				
/*				while ((my->next != NULL) && (my->next->time_ms == 0))
				{
					my = my->next;	// gleich den naechsten ausfuehren
					my_old->next = my->next; // Handler aus der Liste entfernen
					
					_DBGF(VOID, NOTICE, "exec Timer %s", my->name);
					my->cb(my->cb_arg);	// Handler ausfuehren
				}
				timediff_ms -= my->time_ms;*/
			}
			else
			{
				// War nicht groesser, abziehen und fertig.
				my->time_ms -= timediff_ms;
				break;
			}
		}

		if (_entry.next == NULL)
		{
			// Es stehen keine Timeouts an
			_DBGF(VOID, NOTICE," No timer started");
			*timeout = NULL;	
			
		}
		else
		{
			_DBGF(VOID, INFO, "next timer: %s in %u ms, timediff_ms %d", _entry.next->name, _entry.next->time_ms, timediff_ms);
			_to.tv_sec = (_entry.next->time_ms/*- timediff_ms*/) / 1000;
			_to.tv_nsec = ((_entry.next->time_ms/*- timediff_ms*/) % 1000) * 1000/*[us/ms]*/ * 1000 /*[ns/us]*/ ;
			*timeout = &_to;
		}

	}

	_timer_dump();
	
	_RETURN(TIMEOUT_RET_OK);
}