announcement.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "net/rime/announcement.h"
00047 #include "lib/list.h"
00048 #include "sys/cc.h"
00049
00050 LIST(announcements);
00051
00052 static void (* listen_callback)(int time);
00053 static announcement_observer observer_callback;
00054
00055
00056 void
00057 announcement_init(void)
00058 {
00059 list_init(announcements);
00060 }
00061
00062 void
00063 announcement_register(struct announcement *a, uint16_t id,
00064 announcement_callback_t callback)
00065 {
00066 a->id = id;
00067 a->has_value = 0;
00068 a->callback = callback;
00069 list_add(announcements, a);
00070 if(observer_callback) {
00071 observer_callback(a->id, a->has_value,
00072 a->value, 0, ANNOUNCEMENT_BUMP);
00073 }
00074 }
00075
00076 void
00077 announcement_remove(struct announcement *a)
00078 {
00079 list_remove(announcements, a);
00080 }
00081
00082 void
00083 announcement_remove_value(struct announcement *a)
00084 {
00085 a->has_value = 0;
00086 if(observer_callback) {
00087 observer_callback(a->id, 0, 0, 0, ANNOUNCEMENT_NOBUMP);
00088 }
00089
00090 }
00091
00092 void
00093 announcement_set_value(struct announcement *a, uint16_t value)
00094 {
00095 uint16_t oldvalue = a->value;
00096
00097 a->has_value = 1;
00098 a->value = value;
00099 if(observer_callback) {
00100 observer_callback(a->id, a->has_value,
00101 value, oldvalue, ANNOUNCEMENT_NOBUMP);
00102 }
00103 }
00104
00105 void
00106 announcement_bump(struct announcement *a)
00107 {
00108 if(observer_callback) {
00109 observer_callback(a->id, a->has_value,
00110 a->value, a->value, ANNOUNCEMENT_BUMP);
00111 }
00112 }
00113
00114 void
00115 announcement_listen(int time)
00116 {
00117 if(listen_callback) {
00118 listen_callback(time);
00119 }
00120 }
00121
00122 void
00123 announcement_register_listen_callback(void (*callback)(int time))
00124 {
00125 listen_callback = callback;
00126 }
00127
00128 void
00129 announcement_register_observer_callback(announcement_observer callback)
00130 {
00131 observer_callback = callback;
00132 }
00133
00134 struct announcement *
00135 announcement_list(void)
00136 {
00137 return list_head(announcements);
00138 }
00139
00140 void
00141 announcement_heard(const rimeaddr_t *from, uint16_t id, uint16_t value)
00142 {
00143 struct announcement *a;
00144 for(a = list_head(announcements); a != NULL; a = list_item_next(a)) {
00145 if(a->id == id) {
00146 if(a->callback != NULL) {
00147 a->callback(a, from, id, value);
00148 }
00149 return;
00150 }
00151 }
00152 }
00153
00154