Project

General

Profile

Revision 1791

wireless: fixing no ack state

View differences:

branches/wireless/code/projects/libwireless/wireless_send.c
99 99
  packet[3] = XBEE_OPTIONS_NONE;
100 100
  packet[4] = nextframe;
101 101
  packet[5] = group;
102
  nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
102 103
  
103 104
  // set scope
104 105
  if (scope == GLOBAL)
......
182 183
    WL_DEBUG_PRINT_P("send_buf_num_packets:");
183 184
    WL_DEBUG_PRINT_INT(send_buf_num_packets);
184 185
    
185
    nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
186 186
    return (ret == WL_SUCCESS)?packet[0]:ret; // return frame number for ack tracking
187 187
  }
188 188
  return WL_ERROR_SEND; // shouldn't get here, but if it does there's a problem
......
345 345
  uint8_t num_packet = 0;
346 346
  WL_DEBUG_PRINT_P("entering ackhandle function: num:");
347 347
  WL_DEBUG_PRINT_HEX(num);
348
  WL_DEBUG_PRINT_P("|num_send_packets:");
349
  WL_DEBUG_PRINT_INT(send_buf_num_packets);
348 350
  WL_DEBUG_PRINT_P("\r\n");
351
  if (send_buf_num_packets == 0)
352
    return;
349 353
  switch(val) {
350 354
  case 0:
351 355
    WL_DEBUG_PRINT_P("ack success\r\n");
......
353 357
    setack(num,ACK_OK); // set status code
354 358
    // remove from buffer
355 359
    val = send_buf_first;
356
    if (send_buf_num_packets == 0)
357
      return;
358
    WL_DEBUG_PRINT_P("|num_send_packets:");
359
    WL_DEBUG_PRINT_INT(send_buf_num_packets);
360 360
    while (1) {
361 361
      len = send_buf_get(&val);
362 362
      WL_DEBUG_PRINT_P("|send_packet_buf_pos:");
......
416 416
        return;
417 417
      }
418 418
    }
419
    break;
419
    break;  
420
  case 2:
421
    WL_DEBUG_PRINT_P("CCA failure\r\n");
420 422
  case 1:
421 423
    WL_DEBUG_PRINT_P("no ack\r\n");
422 424
    // no ack
......
426 428
      return;
427 429
    while (1) {
428 430
      len = send_buf_get(&val);
429
      if (send_buf_get(&val) != num) {
431
      if (send_buf[val] != num) {
430 432
        // not the correct packet, so continue
431 433
        val += len;
432 434
        if (val >= PACKET_BUFFER_SIZE)
......
438 440
        }
439 441
      } else {
440 442
        // check attempts on packet
441
        if (val+len-1 >= PACKET_BUFFER_SIZE)
442
          num = val+len-1-PACKET_BUFFER_SIZE;
443
        if (val+len+1 >= PACKET_BUFFER_SIZE)
444
          num = val+len-PACKET_BUFFER_SIZE;
443 445
        else
444
          num = val+len-1; // set to end of packet
446
          num = val+len; // set to end of packet
445 447
        if (send_buf[num] < NUM_RETRIES) {
446
          val = (val<2)?PACKET_BUFFER_SIZE+val-2:val-2; // set to start of this packet
447
          // retry sending
448
          // retry sending          
448 449
          send_buf[num]++; // increment retries
449
          send_buf[val+1] = nextframe; // reset frame number
450
          
451
          setack(send_buf[val],ACK_OK);
452
          send_buf[val] = nextframe; // reset frame number
453
          setack(nextframe,SENDING);
450 454
          nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
451 455
          
452
          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // go back one byte
456
          val = (val>0)?PACKET_BUFFER_SIZE+val-1:val-1; // set to start of this packet
453 457
          if (xbee_send_header(len+1) != WL_SUCCESS) {
454 458
            WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
455 459
            return;
......
458 462
          while(val!=num) {
459 463
            // compute checksum
460 464
            len += send_buf[val];
461
            if (xbee_putc(send_buf_get(&val)) != WL_SUCCESS) { // send  byte
465
            if (xbee_sendc(send_buf_get(&val)) != WL_SUCCESS) { // send  byte
462 466
              WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
463 467
              return;
464 468
            }
465 469
          }
466 470
          len = 0xFF - len;
467
          if (xbee_putc(len) != WL_SUCCESS) { // send
471
          if (xbee_sendc(len) != WL_SUCCESS) { // send
468 472
            WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
469 473
            return;
470 474
          }
471 475
          return;
472 476
        } else {
473 477
          // done sending, mark as failed send
474
          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to frame num
475
          setack(send_buf[val],ACK_FAILURE);
478
          setack(send_buf[num],ACK_FAILURE);
476 479
          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to start of this packet
480
          num = (num+1==PACKET_BUFFER_SIZE)?0:num+1; // set to end of this packet
477 481
          // remove packet
478 482
          while(num != send_buf_last) {
479 483
            if (send_buf_add(&val,send_buf_get(&num)) != WL_SUCCESS)
......
484 488
      }
485 489
    }    
486 490
    break; // shouldn't get here, but just in case
487
  case 2:
488
    WL_DEBUG_PRINT_P("CCA failure\r\n");
489
    // CCA failure
490
    // check resend attempts
491
    val = send_buf_first;
492
    if (send_buf_num_packets == 0)
493
      return;
494
    while (1) {
495
      len = send_buf_get(&val);
496
      if (send_buf_get(&val) != num) {
497
        // not the correct packet, so continue
498
        val += len;
499
        if (val >= PACKET_BUFFER_SIZE)
500
          val -= PACKET_BUFFER_SIZE;
501
        if (val == send_buf_last) {
502
          // reached end of bufer, so assume not present
503
          setack(num,CCA_FAILURE); // mark as failed send
504
          return;
505
        }
506
      } else {
507
        // check attempts on packet
508
        num = val+len-1; // set to end of packet
509
        if (num >= PACKET_BUFFER_SIZE)
510
          num -= PACKET_BUFFER_SIZE;
511
        if (send_buf[num] < NUM_RETRIES) {
512
          val = (val<2)?PACKET_BUFFER_SIZE+val-2:val-2; // set to start of this packet
513
          // retry sending
514
          send_buf[num]++; // increment retries
515
          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // go back one byte
516
          if (xbee_send_header(len+1) != WL_SUCCESS) {
517
            WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
518
            return;
519
          }
520
          len = XBEE_FRAME_TX_REQUEST_16;
521
          while(val!=num) {
522
            // compute checksum
523
            len += send_buf[val];
524
            if (xbee_putc(send_buf_get(&val)) != WL_SUCCESS) { // send  byte
525
              WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
526
              return;
527
            }
528
          }
529
          len = 0xFF - len;
530
          if (xbee_putc(len) != WL_SUCCESS) { // send
531
            WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
532
            return;
533
          }
534
          return;
535
        } else {
536
          // done sending, mark as failed send
537
          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to frame num
538
          setack(send_buf[val],CCA_FAILURE);
539
          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to start of this packet
540
          // remove packet
541
          while(num != send_buf_last) {
542
            if (send_buf_add(&val,send_buf_get(&num)) != WL_SUCCESS)
543
              return; // error
544
          }
545
          return;
546
        }
547
      }
548
    }    
549
    break; // shouldn't get here, but just in case
550 491
  }
551 492
}
552 493

  

Also available in: Unified diff