added debugging

Fork of BLE_nRF8001 by RedBearLab

Revision:
0:075ea2812998
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_nRF8001/utility/aci_setup.cpp	Fri Oct 17 22:40:32 2014 +0800
@@ -0,0 +1,162 @@
+/* Copyright (c) 2014, Nordic Semiconductor ASA
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <lib_aci.h>
+#include "aci_setup.h"
+
+
+// aci_struct that will contain 
+// total initial credits
+// current credit
+// current state of the aci (setup/standby/active/sleep)
+// open remote pipe pending
+// close remote pipe pending
+// Current pipe available bitmap
+// Current pipe closed bitmap
+// Current connection interval, slave latency and link supervision timeout
+// Current State of the the GATT client (Service Discovery status)
+
+
+extern hal_aci_data_t msg_to_send;
+
+
+
+/**************************************************************************                */
+/* Utility function to fill the the ACI command queue                                      */
+/* aci_stat               Pointer to the ACI state                                         */
+/* num_cmd_offset(in/out) Offset in the Setup message array to start from                  */
+/*                        offset is updated to the new index after the queue is filled     */
+/*                        or the last message us placed in the queue                       */
+/* Returns                true if at least one message was transferred                     */
+/***************************************************************************/
+static bool aci_setup_fill(aci_state_t *aci_stat, uint8_t *num_cmd_offset)
+{
+  bool ret_val = false;
+  
+  while (*num_cmd_offset < aci_stat->aci_setup_info.num_setup_msgs)
+  {
+    //Board dependent defines
+    memcpy(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]), 
+          (aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]+2));
+
+    //Put the Setup ACI message in the command queue
+    if (!hal_aci_tl_send(&msg_to_send))
+    {
+      //ACI Command Queue is full
+      // *num_cmd_offset is now pointing to the index of the Setup command that did not get sent
+      return ret_val;
+    }
+   
+    ret_val = true;
+    
+    (*num_cmd_offset)++;
+  }
+  
+  return ret_val;
+}
+
+uint8_t do_aci_setup(aci_state_t *aci_stat)
+{
+  uint8_t setup_offset         = 0;
+  uint32_t i                   = 0x0000;
+  aci_evt_t * aci_evt          = NULL;
+  aci_status_code_t cmd_status = ACI_STATUS_ERROR_CRC_MISMATCH;
+  
+  /*
+  We are using the same buffer since we are copying the contents of the buffer 
+  when queuing and immediately processing the buffer when receiving
+  */
+  hal_aci_evt_t  *aci_data = (hal_aci_evt_t *)&msg_to_send;
+  
+  /* Messages in the outgoing queue must be handled before the Setup routine can run.
+   * If it is non-empty we return. The user should then process the messages before calling
+   * do_aci_setup() again.
+   */
+  if (!lib_aci_command_queue_empty())
+  {
+    return SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY;
+  }
+  
+  /* If there are events pending from the device that are not relevant to setup, we return false
+   * so that the user can handle them. At this point we don't care what the event is,
+   * as any event is an error.
+   */
+  if (lib_aci_event_peek(aci_data))
+  {
+    return SETUP_FAIL_EVENT_QUEUE_NOT_EMPTY;
+  }
+  
+  /* Fill the ACI command queue with as many Setup messages as it will hold. */
+  aci_setup_fill(aci_stat, &setup_offset);
+  
+  while (cmd_status != ACI_STATUS_TRANSACTION_COMPLETE)
+  {
+    /* This counter is used to ensure that this function does not loop forever. When the device
+     * returns a valid response, we reset the counter.
+     */
+    if (i++ > 0xFFFFE)
+    {
+      return SETUP_FAIL_TIMEOUT;    
+    }
+    
+    if (lib_aci_event_peek(aci_data))
+    {
+      aci_evt = &(aci_data->evt);
+      
+      if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode)
+      {
+        //Receiving something other than a Command Response Event is an error.
+        return SETUP_FAIL_NOT_COMMAND_RESPONSE;
+      }
+      
+      cmd_status = (aci_status_code_t) aci_evt->params.cmd_rsp.cmd_status;
+      switch (cmd_status)
+      {
+        case ACI_STATUS_TRANSACTION_CONTINUE:
+          //As the device is responding, reset guard counter
+          i = 0;
+          
+          /* As the device has processed the Setup messages we put in the command queue earlier,
+           * we can proceed to fill the queue with new messages
+           */
+          aci_setup_fill(aci_stat, &setup_offset);
+          break;
+        
+        case ACI_STATUS_TRANSACTION_COMPLETE:
+          //Break out of the while loop when this status code appears
+          break;
+        
+        default:
+          //An event with any other status code should be handled by the application
+          return SETUP_FAIL_NOT_SETUP_EVENT;
+      }
+      
+      /* If we haven't returned at this point, the event was either ACI_STATUS_TRANSACTION_CONTINUE
+       * or ACI_STATUS_TRANSACTION_COMPLETE. We don't need the event itself, so we simply
+       * remove it from the queue.
+       */
+       lib_aci_event_get (aci_stat, aci_data);
+    }
+  }
+  
+  return SETUP_SUCCESS;
+}
+