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
00052 #include <packet_sim.h>
00053
00054 #define BIT_TIME 104
00055 #define BYTE_TIME (8 * BIT_TIME)
00056
00057 int byteTransmitTime = BYTE_TIME * 2;
00058 int initBackoffLow = 10 * BYTE_TIME;
00059 int initBackoffHigh = 20 * BYTE_TIME;
00060 int backoffLow = 10 * BYTE_TIME;
00061 int backoffHigh = 20 * BYTE_TIME;
00062 int txChangeLatency = 30 * BYTE_TIME;
00063 int preambleLength = 20;
00064 int headerLength = 7;
00065
00066
00067 struct IncomingMsg;
00068
00069 typedef struct IncomingMsg {
00070 TOS_Msg msg;
00071 struct IncomingMsg* next;
00072 } IncomingMsg;
00073
00074 TOS_MsgPtr packet_transmitting[TOSNODES];
00075 IncomingMsg* incoming[TOSNODES];
00076 link_t* cc1000_connectivity[TOSNODES];
00077
00078 void initialBackoff();
00079 void event_backoff_create(event_t* event, int node, long long eventTime);
00080 void event_backoff_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous));
00081
00082 void event_start_transmit_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous));
00083 void event_start_transmit_create(event_t* event, int node, long long eventTime);
00084
00085 void event_receive_packet_create(event_t* event, int node, long long eventTime, IncomingMsg* msg) __attribute__ ((C, spontaneous));
00086 void event_receive_packet_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous));
00087
00088 void event_send_packet_done_create(event_t* event, int node, long long eventTime) __attribute__ ((C, spontaneous));
00089 void event_send_packet_done_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous));
00090
00091 void packet_sim_init() {
00092 int i;
00093 for (i = 0; i < TOSNODES; i++) {
00094 packet_transmitting[i] = NULL;
00095 incoming[i] = NULL;
00096 cc1000_connectivity[i] = NULL;
00097 }
00098 cc1000_connectivity[0] = (link_t*)malloc(sizeof(link_t));
00099 cc1000_connectivity[0]->mote = 1;
00100 cc1000_connectivity[0]->next_link = NULL;
00101 cc1000_connectivity[2] = (link_t*)malloc(sizeof(link_t));
00102 cc1000_connectivity[2]->mote = 1;
00103 cc1000_connectivity[2]->next_link = NULL;
00104 }
00105
00106 result_t packet_sim_transmit(TOS_MsgPtr msg) {
00107 if (packet_transmitting[NODE_NUM] != NULL) {
00108 return FAIL;
00109 }
00110 packet_transmitting[NODE_NUM] = msg;
00111 initialBackoff();
00112 return SUCCESS;
00113 }
00114
00115 void initialBackoff() {
00116 event_t* event = (event_t*)malloc(sizeof(event_t));
00117 int backoffAmount = initBackoffLow;
00118 backoffAmount += rand() % (initBackoffHigh - initBackoffLow);
00119 event_backoff_create(event, NODE_NUM, tos_state.tos_time + backoffAmount);
00120 if (dbg_active(DBG_PACKET)) {
00121 char timeBuf[128];
00122 printTime(timeBuf, 128);
00123 dbg(DBG_PACKET, "SIM_PACKET: Initial backoff @%s is %i.\n", timeBuf, backoffAmount);
00124 }
00125 dbg(DBG_MEM, "SIM_PACKET: Allocated event 0x%x\n", (unsigned int)event);
00126 TOS_queue_insert_event(event);
00127 }
00128
00129
00130 void event_backoff_create(event_t* event, int node, long long eventTime) {
00131 event->mote = node;
00132 event->force = 0;
00133 event->pause = 0;
00134 event->data = NULL;
00135 event->time = eventTime;
00136 event->handle = event_backoff_handle;
00137 event->cleanup = event_total_cleanup;
00138 }
00139
00140
00141 void event_backoff_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous)) {
00142 if (incoming[NODE_NUM] != NULL) {
00143 int backoffAmount = backoffLow;
00144 backoffAmount += rand() % (backoffHigh - backoffLow);
00145 event_backoff_create(event, NODE_NUM, tos_state.tos_time + backoffAmount);
00146 dbg(DBG_PACKET, "SIM_PACKET: Backoff more: %i.\n", backoffAmount);
00147 TOS_queue_insert_event(event);
00148 }
00149 else {
00150 event_start_transmit_create(event, NODE_NUM, tos_state.tos_time + txChangeLatency);
00151 TOS_queue_insert_event(event);
00152 }
00153 }
00154
00155 void event_start_transmit_create(event_t* event, int node, long long eventTime) {
00156 event->mote = node;
00157 event->force = 0;
00158 event->pause = 0;
00159 event->data = NULL;
00160 event->time = eventTime;
00161 event->handle = event_start_transmit_handle;
00162 event->cleanup = event_total_cleanup;
00163 }
00164
00165 void corruptPacket(IncomingMsg* msg, int src, int dest) {
00166 int i;
00167 uint8_t* buf = (uint8_t*)&msg->msg;
00168 dbg(DBG_PACKET, "SIM_PACKET: Corrupting message from %i to %i\n", src, dest);
00169 for (i = 0; i < (TOSH_DATA_LENGTH + 7); i++) {
00170 buf[i] = (uint8_t)(rand() & 0xff);
00171 }
00172 }
00173
00174 void event_start_transmit_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous)) {
00175 link_t* connectLink;
00176 TOS_MsgPtr msg = packet_transmitting[NODE_NUM];
00177 int transmitTime = (preambleLength + msg->length + headerLength);
00178 transmitTime *= byteTransmitTime;
00179 connectLink = cc1000_connectivity[NODE_NUM];
00180
00181 dbg(DBG_PACKET, "SIM_PACKET: Transmitting, transmit time is %i.\n", transmitTime);
00182
00183 while (connectLink != NULL) {
00184 int mote = connectLink->mote;
00185 if (mote >= tos_state.num_nodes ||
00186 !tos_state.moteOn[mote]) {
00187 connectLink = connectLink->next_link;
00188 continue;
00189 }
00190
00191 if (packet_transmitting[mote] == NULL) {
00192 event_t* recvEvent = (event_t*)malloc(sizeof(event_t));
00193
00194 IncomingMsg* msgEvent = (IncomingMsg*)malloc(sizeof(IncomingMsg));
00195 memcpy(&msgEvent->msg, msg, sizeof(TOS_Msg));
00196 dbg(DBG_PACKET, "SIM_PACKET: Mote %i sending to mote %i\n", (int)TOS_LOCAL_ADDRESS, (int)mote);
00197
00198
00199 if (incoming[mote] != NULL) {
00200 IncomingMsg* incomingMsg = incoming[mote];
00201 corruptPacket(msgEvent, NODE_NUM, mote);
00202 while (incomingMsg != NULL) {
00203 corruptPacket(incomingMsg, NODE_NUM, mote);
00204 incomingMsg = incomingMsg->next;
00205 }
00206 }
00207
00208 msgEvent->next = incoming[mote];
00209 incoming[mote] = msgEvent;
00210
00211
00212 event_receive_packet_create(recvEvent, mote, tos_state.tos_time + transmitTime, msgEvent);
00213 TOS_queue_insert_event(recvEvent);
00214 }
00215 else {
00216 dbg(DBG_PACKET, "SIM_PACKET: Mote %i would send to mote %i, but it is transmitting.\n", (int)TOS_LOCAL_ADDRESS, (int)mote);
00217 }
00218 connectLink = connectLink->next_link;
00219 }
00220
00221 dbg(DBG_PACKET, "SIM_PACKET: Enqueueing send done.\n");
00222 event_send_packet_done_create(event, NODE_NUM, tos_state.tos_time + transmitTime);
00223 TOS_queue_insert_event(event);
00224
00225 }
00226
00227
00228 void event_receive_packet_create(event_t* event, int node, long long eventTime, IncomingMsg* msg) __attribute__ ((C, spontaneous)) {
00229 event->mote = node;
00230 event->force = 0;
00231 event->pause = 0;
00232 event->time = eventTime;
00233 event->handle = event_receive_packet_handle;
00234 event->cleanup = event_total_cleanup;
00235 event->data = msg;
00236 }
00237
00238
00239 void event_receive_packet_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous)) {
00240 IncomingMsg* receivedPacket;
00241 IncomingMsg* prevPacket = NULL;
00242 IncomingMsg* nextPacket = NULL;
00243
00244 receivedPacket = (IncomingMsg*)event->data;
00245
00246 prevPacket = incoming[NODE_NUM];
00247 if (prevPacket == receivedPacket) {
00248 incoming[NODE_NUM] = prevPacket->next;
00249 }
00250 else {
00251 while (prevPacket->next != NULL) {
00252 nextPacket = prevPacket->next;
00253 if (nextPacket == receivedPacket) {
00254 prevPacket->next = nextPacket->next;
00255 break;
00256 }
00257 prevPacket = nextPacket;
00258 }
00259 }
00260
00261 dbg(DBG_PACKET, "SIM_PACKET: Receiving\n");
00262 packet_sim_receive_msg(&receivedPacket->msg);
00263 event_cleanup(event);
00264 }
00265
00266 void event_send_packet_done_create(event_t* event, int node, long long eventTime) __attribute__ ((C, spontaneous)) {
00267 event->mote = node;
00268 event->force = 0;
00269 event->pause = 0;
00270 event->time = eventTime;
00271 event->handle = event_send_packet_done_handle;
00272 event->cleanup = event_total_cleanup;
00273 event->data = NULL;
00274 }
00275
00276
00277 void event_send_packet_done_handle(event_t* event, struct TOS_state* state) __attribute__ ((C, spontaneous)) {
00278 TOS_MsgPtr bufferPtr = packet_transmitting[NODE_NUM];
00279 if (dbg_active(DBG_PACKET)) {
00280 char timeBuf[128];
00281 printTime(timeBuf, 128);
00282 dbg(DBG_PACKET, "SIM_PACKET: Send done @%s\n", timeBuf);
00283 }
00284 packet_transmitting[NODE_NUM] = NULL;
00285 packet_sim_transmit_done(bufferPtr);
00286 event_cleanup(event);
00287 }