BeRTOS
dnotifier.c
Go to the documentation of this file.
00001 
00039 #include <cfg/debug.h>
00040 
00041 #include <dt/dtag.h>
00042 #include <dt/dnotifier.h>
00043 #include <struct/list.h>
00044 
00049 static void notifier_update(DNotifier *n, dtag_t tag, dval_t val)
00050 {
00051     dnotify_targets(n, tag, val);
00052 }
00053 
00057 void notifier_init(DNotifier *n)
00058 {
00059     // Init instance
00060     n->update = notifier_update;
00061     LIST_INIT(&n->targets);
00062 }
00063 
00070 void filter_update(DFilter *f, dtag_t tag, dval_t val)
00071 {
00072 
00073     const DFilterMap *map = f->map;
00074 
00075     if (map)
00076     {
00077         while (map->src.tag != TAG_END)
00078         {
00079             if ((map->src.tag == tag) && (map->src.val == val))
00080             {
00081                 tag = map->dst.tag;
00082                 val = map->dst.val;
00083                 break;
00084             }
00085             /* TAG_ANY matches anything */
00086             if (map->src.tag == TAG_ANY)
00087                 break;
00088             map++;
00089         }
00090 
00091         if (map->src.tag != TAG_END)
00092             dnotify(f->target, tag, val);
00093     }
00094     else
00095         dnotify(f->target, tag, val);
00096 }
00097 
00098 
00106 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val)
00107 {
00108 
00109     const DFilterMap *map = f->map;
00110     dfilter_mask_t mask;
00111 
00112     if (map)
00113     {
00114         while (map->src.tag != TAG_END)
00115         {
00116             mask = (dfilter_mask_t) map->src.val;
00117             if ((map->src.tag == tag) && ((mask & (dfilter_mask_t)val) == mask))
00118             {
00119                 tag = map->dst.tag;
00120                 val = map->dst.val;
00121                 break;
00122             }
00123             /* TAG_ANY matches anything */
00124             if (map->src.tag == TAG_ANY)
00125                 break;
00126             map++;
00127         }
00128 
00129 
00130         if (map->src.tag != TAG_END)
00131             dnotify(f->target, tag, val);
00132     }
00133     else
00134         dnotify(f->target, tag, val);
00135 }
00136 
00137 
00138 #define FILTER_MAGIC_ACTIVE 0xAA
00139 
00143 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target)
00144 {
00145     // Init instance
00146     if (masked)
00147         f->update = (update_filter_ptr)filter_mask_update;
00148     else
00149         f->update = (update_filter_ptr)filter_update;
00150 
00151     /* set filter map and target */
00152     f->map = map;
00153     f->target = target;
00154 
00155     /* these ensure that the filter is not inserted in more than one list */
00156     ASSERT(f->magic != FILTER_MAGIC_ACTIVE);
00157     DB(f->magic = FILTER_MAGIC_ACTIVE;)
00158 
00159     /* Add the filter to source filter list */
00160     ADDTAIL(&source->targets, &f->link);
00161 }