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/stunicast.h"
00047 #include "net/rime.h"
00048 #include <string.h>
00049
00050 #define DEBUG 0
00051 #if DEBUG
00052 #include <stdio.h>
00053 #define PRINTF(...) printf(__VA_ARGS__)
00054 #else
00055 #define PRINTF(...)
00056 #endif
00057
00058
00059 static void
00060 recv_from_uc(struct unicast_conn *uc, const rimeaddr_t *from)
00061 {
00062 register struct stunicast_conn *c = (struct stunicast_conn *)uc;
00063 PRINTF("%d.%d: stunicast: recv_from_uc from %d.%d\n",
00064 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00065 from->u8[0], from->u8[1]);
00066 if(c->u->recv != NULL) {
00067 c->u->recv(c, from);
00068 }
00069 }
00070
00071 static void
00072 sent_by_uc(struct unicast_conn *uc, int status, int num_tx)
00073 {
00074 register struct stunicast_conn *c = (struct stunicast_conn *)uc;
00075 PRINTF("%d.%d: stunicast: recv_from_uc from %d.%d\n",
00076 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00077 packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
00078 packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1]);
00079 if(c->u->sent != NULL) {
00080 c->u->sent(c, status, num_tx);
00081 }
00082 }
00083
00084 static const struct unicast_callbacks stunicast = {recv_from_uc,
00085 sent_by_uc};
00086
00087 void
00088 stunicast_open(struct stunicast_conn *c, uint16_t channel,
00089 const struct stunicast_callbacks *u)
00090 {
00091 unicast_open(&c->c, channel, &stunicast);
00092 c->u = u;
00093 }
00094
00095 void
00096 stunicast_close(struct stunicast_conn *c)
00097 {
00098 unicast_close(&c->c);
00099 stunicast_cancel(c);
00100 }
00101
00102 rimeaddr_t *
00103 stunicast_receiver(struct stunicast_conn *c)
00104 {
00105 return &c->receiver;
00106 }
00107
00108 static void
00109 send(void *ptr)
00110 {
00111 struct stunicast_conn *c = ptr;
00112
00113 PRINTF("%d.%d: stunicast: resend to %d.%d\n",
00114 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00115 c->receiver.u8[0], c->receiver.u8[1]);
00116 queuebuf_to_packetbuf(c->buf);
00117 unicast_send(&c->c, &c->receiver);
00118 stunicast_set_timer(c, CLOCK_SECOND);
00119
00120
00121
00122 }
00123
00124 void
00125 stunicast_set_timer(struct stunicast_conn *c, clock_time_t t)
00126 {
00127 ctimer_set(&c->t, t, send, c);
00128 }
00129
00130 int
00131 stunicast_send_stubborn(struct stunicast_conn *c, const rimeaddr_t *receiver,
00132 clock_time_t rxmittime)
00133 {
00134 if(c->buf != NULL) {
00135 queuebuf_free(c->buf);
00136 }
00137 c->buf = queuebuf_new_from_packetbuf();
00138 if(c->buf == NULL) {
00139 return 0;
00140 }
00141 rimeaddr_copy(&c->receiver, receiver);
00142 ctimer_set(&c->t, rxmittime, send, c);
00143
00144 PRINTF("%d.%d: stunicast_send_stubborn to %d.%d\n",
00145 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00146 c->receiver.u8[0],c->receiver.u8[1]);
00147 unicast_send(&c->c, &c->receiver);
00148
00149
00150
00151
00152 return 1;
00153
00154 }
00155
00156 int
00157 stunicast_send(struct stunicast_conn *c, const rimeaddr_t *receiver)
00158 {
00159 PRINTF("%d.%d: stunicast_send to %d.%d\n",
00160 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00161 receiver->u8[0], receiver->u8[1]);
00162 return unicast_send(&c->c, receiver);
00163 }
00164
00165 void
00166 stunicast_cancel(struct stunicast_conn *c)
00167 {
00168 ctimer_stop(&c->t);
00169 if(c->buf != NULL) {
00170 queuebuf_free(c->buf);
00171 c->buf = NULL;
00172 }
00173 }
00174
00175