File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / mpd / src / event.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Nov 2 09:50:34 2016 UTC (7 years, 8 months ago) by misho
Branches: mpd, MAIN
CVS tags: v5_8p1_cross, v5_8p1, HEAD
mpd 5.8rc1

    1: /*
    2:  * See ``COPYRIGHT.mpd''
    3:  *
    4:  * $Id: event.c,v 1.1.1.2 2016/11/02 09:50:34 misho Exp $
    5:  *
    6:  */
    7: 
    8:   #include "ppp.h"
    9:   #include "event.h"
   10: 
   11: /*
   12:  * DEFINITIONS
   13:  */
   14: 
   15:   struct pevent_ctx	*gPeventCtx = NULL;
   16: 
   17: /*
   18:  * INTERNAL FUNCTIONS
   19:  */
   20: 
   21:   static void		EventHandler(void *arg);
   22: 
   23: /*
   24:  * EventInit()
   25:  *
   26:  */
   27: 
   28: int
   29: EventInit(void)
   30: {
   31: 
   32:   gPeventCtx = pevent_ctx_create(MB_EVENT, NULL);
   33:   if (!gPeventCtx) {
   34:     Log(LG_ERR, ("%s: error pevent_ctx_create: %d", __FUNCTION__, errno));
   35:     return(-1);
   36:   }
   37: 
   38:   return(0);
   39: }
   40: 
   41: /*
   42:  * EventStop()
   43:  *
   44:  * Stop servicing events
   45:  */
   46: 
   47: void
   48: EventStop(void)
   49: {
   50:   pevent_ctx_destroy(&gPeventCtx);
   51: }
   52: 
   53: /*
   54:  * EventDump()
   55:  */
   56: 
   57: void
   58: EventDump(Context ctx, const char *msg)
   59: {
   60:   u_int	n;
   61: 
   62:   n = pevent_ctx_count(gPeventCtx);
   63:   Printf("%d Events registered\r\n", n);
   64: }
   65: 
   66: /*
   67:  * EventRegister()
   68:  */
   69: 
   70: int
   71: EventRegister2(EventRef *refp, int type, int val, int flags,
   72: 	void (*action)(int type, void *cookie), void *cookie, const char *dbg,
   73: 	const char *file, int line)
   74: {
   75:     Log(LG_EVENTS, ("EVENT: Registering event %s at %s:%d", dbg, file, line));
   76:     if (!gPeventCtx)
   77: 	EventInit();
   78: 
   79:     refp->arg = cookie;
   80:     refp->handler = action;
   81:     refp->type = type;
   82:     refp->pe = NULL;
   83:     refp->dbg = dbg;
   84: 
   85:     if (pevent_register(gPeventCtx, &refp->pe, flags, &gGiantMutex, EventHandler,
   86: 	    refp, type, val) == -1) {
   87:         Perror("%s: error pevent_register", __FUNCTION__);
   88:         return(-1);
   89:     }
   90:   
   91:     Log(LG_EVENTS, ("EVENT: Registering event %s done at %s:%d", dbg, file, line));
   92:     return(0);
   93: }
   94: 
   95: /*
   96:  * EventUnRegister()
   97:  */
   98: 
   99: int
  100: EventUnRegister2(EventRef *refp, const char *file, int line)
  101: {
  102:     Log(LG_EVENTS, ("EVENT: Unregistering event %s at %s:%d", refp->dbg, file, line));
  103:     pevent_unregister(&refp->pe);
  104:     Log(LG_EVENTS, ("EVENT: Unregistering event %s done at %s:%d", refp->dbg, file, line));
  105:     return(0);
  106: }
  107: 
  108: /*
  109:  * EventIsRegistered()
  110:  */
  111: 
  112: int
  113: EventIsRegistered(EventRef *ref)
  114: {
  115:     if (ref->pe == NULL)
  116: 	return FALSE;
  117: 
  118:     return TRUE;
  119: }
  120: 
  121: /*
  122:  * EventTimerRemain()
  123:  *
  124:  * Returns the number of milliseconds remaining on a timer.
  125:  * Returns -1 if the timer is not registered or is not a timer event.
  126:  */
  127: 
  128: int
  129: EventTimerRemain(EventRef *refp)
  130: {
  131:     struct pevent_info	info;
  132: 
  133:     if (pevent_get_info(refp->pe, &info) == -1)
  134: 	return(-1);
  135: 
  136:     return(info.u.millis);
  137: }
  138: 
  139: static void
  140: EventHandler(void *arg)
  141: {
  142:     EventRef	*refp = (EventRef *) arg;
  143:     const char	*dbg = refp->dbg;
  144: 
  145:     Log(LG_EVENTS, ("EVENT: Processing event %s", dbg));
  146:     (refp->handler)(refp->type, refp->arg);
  147:     Log(LG_EVENTS, ("EVENT: Processing event %s done", dbg));
  148: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>