| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #include "warpmac.h" |
|---|
| 31 | #include "warpphy.h" |
|---|
| 32 | #include "hopMacServer.h" |
|---|
| 33 | #include "xparameters.h" |
|---|
| 34 | #include <string.h> |
|---|
| 35 | #include <errno.h> |
|---|
| 36 | #include <stdlib.h> |
|---|
| 37 | #include <stdio.h> |
|---|
| 38 | #include "ofdm_txrx_mimo_regMacros.h" |
|---|
| 39 | #include "xtime_l.h" |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | typedef struct { |
|---|
| 46 | unsigned char addr[6]; |
|---|
| 47 | } route; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | route routeTable[16]; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | unsigned char myAddr[6]; |
|---|
| 54 | |
|---|
| 55 | unsigned char myID; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | Macframe txBuffer; |
|---|
| 59 | |
|---|
| 60 | Macframe rxBuffer; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | unsigned char currentAnt = 0; |
|---|
| 64 | static volatile unsigned char uartByte = 0; |
|---|
| 65 | unsigned char chan; |
|---|
| 66 | |
|---|
| 67 | unsigned char controlMode = 1; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | #define DATAPACKET 1 |
|---|
| 72 | |
|---|
| 73 | #define ACKPACKET 0 |
|---|
| 74 | |
|---|
| 75 | #define HOPPACKET 2 |
|---|
| 76 | |
|---|
| 77 | #define ARPREQUEST 0x806 |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | void up(){ |
|---|
| 84 | controlMode=1; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | void middle(){ |
|---|
| 91 | controlMode=0; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void switchChannels(){ |
|---|
| 95 | Macframe followMe; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | unsigned char chanVec[14] = {1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 14}; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | chan = rand()%14; |
|---|
| 102 | while(chanVec[chan] != 1){ |
|---|
| 103 | chan = rand()%14; |
|---|
| 104 | } |
|---|
| 105 | chan = chan+1; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | unsigned char broadcast[6] = {255,255,255,255,255,255}; |
|---|
| 109 | followMe.header.length = 0; |
|---|
| 110 | followMe.header.pktType = HOPPACKET; |
|---|
| 111 | memcpy(followMe.header.destAddr,broadcast,6); |
|---|
| 112 | followMe.header.reserved1 = chan; |
|---|
| 113 | char i=0; |
|---|
| 114 | #define NUMCONTROL 32 |
|---|
| 115 | if(controlMode){ |
|---|
| 116 | while(i<NUMCONTROL){ |
|---|
| 117 | if(warpmac_carrierSense()){ |
|---|
| 118 | |
|---|
| 119 | warpmac_prepPhyForXmit(&followMe,2); |
|---|
| 120 | |
|---|
| 121 | warpmac_startPhyXmit(2); |
|---|
| 122 | |
|---|
| 123 | warpmac_finishPhyXmit(); |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | i++; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | warpphy_setChannel(GHZ_2,chan); |
|---|
| 136 | warpmac_leftHex(chan); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | void timer_callback(unsigned char timerType){ |
|---|
| 154 | int status; |
|---|
| 155 | switch(timerType){ |
|---|
| 156 | case TIMEOUT: |
|---|
| 157 | |
|---|
| 158 | status = warpmac_incrementResend(&txBuffer); |
|---|
| 159 | if(status == 0){ |
|---|
| 160 | txBuffer.isNew = 0; |
|---|
| 161 | |
|---|
| 162 | warpmac_enableEthernet(); |
|---|
| 163 | return 0; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | warpmac_setTimer(BACKOFF); |
|---|
| 167 | return 0; |
|---|
| 168 | |
|---|
| 169 | break; |
|---|
| 170 | case BACKOFF: |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | warpmac_prepPhyForXmit(&txBuffer,1); |
|---|
| 174 | |
|---|
| 175 | warpmac_startPhyXmit(1); |
|---|
| 176 | |
|---|
| 177 | warpmac_finishPhyXmit(); |
|---|
| 178 | |
|---|
| 179 | warpmac_setTimer(TIMEOUT); |
|---|
| 180 | break; |
|---|
| 181 | return 0; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | void emacRx_callback(Xuint32 length){ |
|---|
| 194 | txBuffer.isNew = 1; |
|---|
| 195 | u8* payload = warpphy_getBuffAddr(1)+NUM_HEADER_BYTES; |
|---|
| 196 | |
|---|
| 197 | unsigned short int* pkttype = &payload[12]; |
|---|
| 198 | unsigned char* destNode; |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | if(*pkttype==ARPREQUEST){ |
|---|
| 202 | destNode=&payload[41]; |
|---|
| 203 | } |
|---|
| 204 | else{ |
|---|
| 205 | destNode=&payload[33]; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | warpmac_disableEthernet(); |
|---|
| 216 | txBuffer.header.currReSend = 0; |
|---|
| 217 | txBuffer.header.length = length; |
|---|
| 218 | txBuffer.header.pktType = DATAPACKET; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | txBuffer.header.fullRate = QPSK; |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | memcpy(txBuffer.header.destAddr,routeTable[*destNode].addr,6); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | if(warpmac_carrierSense()){ |
|---|
| 229 | |
|---|
| 230 | warpmac_prepPhyForXmit(&txBuffer,1); |
|---|
| 231 | |
|---|
| 232 | warpmac_startPhyXmit(1); |
|---|
| 233 | |
|---|
| 234 | warpmac_finishPhyXmit(); |
|---|
| 235 | |
|---|
| 236 | warpmac_setTimer(TIMEOUT); |
|---|
| 237 | } |
|---|
| 238 | else{ |
|---|
| 239 | |
|---|
| 240 | warpmac_setTimer(BACKOFF); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | return; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | void phyRx_badHeader_callback() { |
|---|
| 251 | warpmac_incrementLEDLow(); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | void phyRx_goodHeader_callback(Macframe* packet){ |
|---|
| 261 | warpmac_incrementLEDHigh(); |
|---|
| 262 | Macframe ackPacket; |
|---|
| 263 | unsigned char state=INCOMPLETE; |
|---|
| 264 | |
|---|
| 265 | if(warpmac_addressedToMe(packet)){ |
|---|
| 266 | |
|---|
| 267 | switch(packet->header.pktType){ |
|---|
| 268 | case DATAPACKET: |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | while(state==INCOMPLETE){ |
|---|
| 273 | |
|---|
| 274 | state = warpphy_pollRxStatus(); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if(state==GOODPACKET){ |
|---|
| 278 | warpmac_prepEmacForXmit(packet); |
|---|
| 279 | |
|---|
| 280 | warpmac_startEmacXmit(packet); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if(state==BADPACKET){ |
|---|
| 284 | |
|---|
| 285 | warpmac_incrementLEDLow(); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | break; |
|---|
| 289 | |
|---|
| 290 | case ACKPACKET: |
|---|
| 291 | |
|---|
| 292 | if(warpmac_inTimeout()){ |
|---|
| 293 | txBuffer.isNew = 0; |
|---|
| 294 | warpmac_incrementLEDHigh(); |
|---|
| 295 | warpmac_clearTimer(TIMEOUT); |
|---|
| 296 | warpmac_enableEthernet(); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | break; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | int main(){ |
|---|
| 312 | xil_printf("Hopmac Server\r\n"); |
|---|
| 313 | |
|---|
| 314 | warpmac_init(); |
|---|
| 315 | |
|---|
| 316 | chan = 1; |
|---|
| 317 | |
|---|
| 318 | warpmac_setMaxResend(2); |
|---|
| 319 | warpmac_setMaxCW(2); |
|---|
| 320 | warpmac_setTimeout(160); |
|---|
| 321 | warpmac_setSlotTime(9); |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | myID = warpmac_getMyId(); |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | unsigned char tmpAddr[6] = {0x16,0x24,0x63,0x53,0xe2,0xc2+myID}; |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | memcpy((unsigned char *)myAddr,(unsigned char *)tmpAddr,6); |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | unsigned char i; |
|---|
| 337 | for(i=0;i<16;i++){ |
|---|
| 338 | routeTable[i].addr[0] = myAddr[0]; |
|---|
| 339 | routeTable[i].addr[1] = myAddr[1]; |
|---|
| 340 | routeTable[i].addr[2] = myAddr[2]; |
|---|
| 341 | routeTable[i].addr[3] = myAddr[3]; |
|---|
| 342 | routeTable[i].addr[4] = myAddr[4]; |
|---|
| 343 | routeTable[i].addr[5] = myAddr[5]+i-myID; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | warpmac_setMacAddr((unsigned char *)(&myAddr)); |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | warpmac_setRxBuffer(&rxBuffer,0); |
|---|
| 350 | |
|---|
| 351 | warpmac_setTxBuffer(1); |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | memcpy(txBuffer.header.srcAddr,(unsigned char *)myAddr,6); |
|---|
| 355 | |
|---|
| 356 | warpmac_setBadHeaderCallback((void *)phyRx_badHeader_callback); |
|---|
| 357 | warpmac_setGoodHeaderCallback((void *)phyRx_goodHeader_callback); |
|---|
| 358 | warpmac_setTimerCallback((void *)timer_callback); |
|---|
| 359 | warpmac_setEmacCallback((void *)emacRx_callback); |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | warpmac_setUpButtonCallback((void *)up); |
|---|
| 363 | warpmac_setMiddleButtonCallback((void *)middle); |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | warpphy_setChannel(GHZ_2, chan); |
|---|
| 368 | |
|---|
| 369 | warpmac_enableCSMA(); |
|---|
| 370 | warpmac_enableEthernet(); |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | warpmac_setBaseRate(QPSK); |
|---|
| 374 | |
|---|
| 375 | XTime tEnd, tCur; |
|---|
| 376 | unsigned int seconds = 5; |
|---|
| 377 | unsigned char startCount = 1; |
|---|
| 378 | |
|---|
| 379 | xil_printf("Mask version 1\r\n"); |
|---|
| 380 | while(1){ |
|---|
| 381 | warpmac_pollEthernet(); |
|---|
| 382 | |
|---|
| 383 | XTime_GetTime(&tCur); |
|---|
| 384 | |
|---|
| 385 | if (startCount){ |
|---|
| 386 | tEnd = tCur + ((XTime) seconds) * 160000000; |
|---|
| 387 | startCount=0; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | if(tCur>tEnd){ |
|---|
| 397 | if(txBuffer.isNew==0){ |
|---|
| 398 | warpmac_disableEthernet(); |
|---|
| 399 | usleep(1000); |
|---|
| 400 | switchChannels(); |
|---|
| 401 | startCount=1; |
|---|
| 402 | seconds = 5; |
|---|
| 403 | warpmac_enableEthernet(); |
|---|
| 404 | } |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | } |
|---|
| 408 | return; |
|---|
| 409 | } |
|---|