Index: branches/wireless/code/projects/libwireless/wireless_send.c
===================================================================
--- branches/wireless/code/projects/libwireless/wireless_send.c	(revision 1790)
+++ branches/wireless/code/projects/libwireless/wireless_send.c	(revision 1791)
@@ -99,6 +99,7 @@
   packet[3] = XBEE_OPTIONS_NONE;
   packet[4] = nextframe;
   packet[5] = group;
+  nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
   
   // set scope
   if (scope == GLOBAL)
@@ -182,7 +183,6 @@
     WL_DEBUG_PRINT_P("send_buf_num_packets:");
     WL_DEBUG_PRINT_INT(send_buf_num_packets);
     
-    nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
     return (ret == WL_SUCCESS)?packet[0]:ret; // return frame number for ack tracking
   }
   return WL_ERROR_SEND; // shouldn't get here, but if it does there's a problem
@@ -345,7 +345,11 @@
   uint8_t num_packet = 0;
   WL_DEBUG_PRINT_P("entering ackhandle function: num:");
   WL_DEBUG_PRINT_HEX(num);
+  WL_DEBUG_PRINT_P("|num_send_packets:");
+  WL_DEBUG_PRINT_INT(send_buf_num_packets);
   WL_DEBUG_PRINT_P("\r\n");
+  if (send_buf_num_packets == 0)
+    return;
   switch(val) {
   case 0:
     WL_DEBUG_PRINT_P("ack success\r\n");
@@ -353,10 +357,6 @@
     setack(num,ACK_OK); // set status code
     // remove from buffer
     val = send_buf_first;
-    if (send_buf_num_packets == 0)
-      return;
-    WL_DEBUG_PRINT_P("|num_send_packets:");
-    WL_DEBUG_PRINT_INT(send_buf_num_packets);
     while (1) {
       len = send_buf_get(&val);
       WL_DEBUG_PRINT_P("|send_packet_buf_pos:");
@@ -416,7 +416,9 @@
         return;
       }
     }
-    break;
+    break;  
+  case 2:
+    WL_DEBUG_PRINT_P("CCA failure\r\n");
   case 1:
     WL_DEBUG_PRINT_P("no ack\r\n");
     // no ack
@@ -426,7 +428,7 @@
       return;
     while (1) {
       len = send_buf_get(&val);
-      if (send_buf_get(&val) != num) {
+      if (send_buf[val] != num) {
         // not the correct packet, so continue
         val += len;
         if (val >= PACKET_BUFFER_SIZE)
@@ -438,18 +440,20 @@
         }
       } else {
         // check attempts on packet
-        if (val+len-1 >= PACKET_BUFFER_SIZE)
-          num = val+len-1-PACKET_BUFFER_SIZE;
+        if (val+len+1 >= PACKET_BUFFER_SIZE)
+          num = val+len-PACKET_BUFFER_SIZE;
         else
-          num = val+len-1; // set to end of packet
+          num = val+len; // set to end of packet
         if (send_buf[num] < NUM_RETRIES) {
-          val = (val<2)?PACKET_BUFFER_SIZE+val-2:val-2; // set to start of this packet
-          // retry sending
+          // retry sending          
           send_buf[num]++; // increment retries
-          send_buf[val+1] = nextframe; // reset frame number
+          
+          setack(send_buf[val],ACK_OK);
+          send_buf[val] = nextframe; // reset frame number
+          setack(nextframe,SENDING);
           nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
           
-          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // go back one byte
+          val = (val>0)?PACKET_BUFFER_SIZE+val-1:val-1; // set to start of this packet
           if (xbee_send_header(len+1) != WL_SUCCESS) {
             WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
             return;
@@ -458,13 +462,13 @@
           while(val!=num) {
             // compute checksum
             len += send_buf[val];
-            if (xbee_putc(send_buf_get(&val)) != WL_SUCCESS) { // send  byte
+            if (xbee_sendc(send_buf_get(&val)) != WL_SUCCESS) { // send  byte
               WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
               return;
             }
           }
           len = 0xFF - len;
-          if (xbee_putc(len) != WL_SUCCESS) { // send
+          if (xbee_sendc(len) != WL_SUCCESS) { // send
             WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
             return;
           }
@@ -471,9 +475,9 @@
           return;
         } else {
           // done sending, mark as failed send
-          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to frame num
-          setack(send_buf[val],ACK_FAILURE);
+          setack(send_buf[num],ACK_FAILURE);
           val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to start of this packet
+          num = (num+1==PACKET_BUFFER_SIZE)?0:num+1; // set to end of this packet
           // remove packet
           while(num != send_buf_last) {
             if (send_buf_add(&val,send_buf_get(&num)) != WL_SUCCESS)
@@ -484,69 +488,6 @@
       }
     }    
     break; // shouldn't get here, but just in case
-  case 2:
-    WL_DEBUG_PRINT_P("CCA failure\r\n");
-    // CCA failure
-    // check resend attempts
-    val = send_buf_first;
-    if (send_buf_num_packets == 0)
-      return;
-    while (1) {
-      len = send_buf_get(&val);
-      if (send_buf_get(&val) != num) {
-        // not the correct packet, so continue
-        val += len;
-        if (val >= PACKET_BUFFER_SIZE)
-          val -= PACKET_BUFFER_SIZE;
-        if (val == send_buf_last) {
-          // reached end of bufer, so assume not present
-          setack(num,CCA_FAILURE); // mark as failed send
-          return;
-        }
-      } else {
-        // check attempts on packet
-        num = val+len-1; // set to end of packet
-        if (num >= PACKET_BUFFER_SIZE)
-          num -= PACKET_BUFFER_SIZE;
-        if (send_buf[num] < NUM_RETRIES) {
-          val = (val<2)?PACKET_BUFFER_SIZE+val-2:val-2; // set to start of this packet
-          // retry sending
-          send_buf[num]++; // increment retries
-          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // go back one byte
-          if (xbee_send_header(len+1) != WL_SUCCESS) {
-            WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
-            return;
-          }
-          len = XBEE_FRAME_TX_REQUEST_16;
-          while(val!=num) {
-            // compute checksum
-            len += send_buf[val];
-            if (xbee_putc(send_buf_get(&val)) != WL_SUCCESS) { // send  byte
-              WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
-              return;
-            }
-          }
-          len = 0xFF - len;
-          if (xbee_putc(len) != WL_SUCCESS) { // send
-            WL_DEBUG_PRINT_P("Error sending packet from ackhandle function\r\n");
-            return;
-          }
-          return;
-        } else {
-          // done sending, mark as failed send
-          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to frame num
-          setack(send_buf[val],CCA_FAILURE);
-          val = (val==0)?PACKET_BUFFER_SIZE-1:val-1; // set to start of this packet
-          // remove packet
-          while(num != send_buf_last) {
-            if (send_buf_add(&val,send_buf_get(&num)) != WL_SUCCESS)
-              return; // error
-          }
-          return;
-        }
-      }
-    }    
-    break; // shouldn't get here, but just in case
   }
 }
 
