added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/utility/aci_setup.cpp@0:075ea2812998, 2014-10-17 (annotated)
- Committer:
- RedBearLab
- Date:
- Fri Oct 17 22:40:32 2014 +0800
- Revision:
- 0:075ea2812998
BLE_nRF8001 library first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:075ea2812998 | 1 | /* Copyright (c) 2014, Nordic Semiconductor ASA |
RedBearLab | 0:075ea2812998 | 2 | * |
RedBearLab | 0:075ea2812998 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
RedBearLab | 0:075ea2812998 | 4 | * of this software and associated documentation files (the "Software"), to deal |
RedBearLab | 0:075ea2812998 | 5 | * in the Software without restriction, including without limitation the rights |
RedBearLab | 0:075ea2812998 | 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
RedBearLab | 0:075ea2812998 | 7 | * copies of the Software, and to permit persons to whom the Software is |
RedBearLab | 0:075ea2812998 | 8 | * furnished to do so, subject to the following conditions: |
RedBearLab | 0:075ea2812998 | 9 | * |
RedBearLab | 0:075ea2812998 | 10 | * The above copyright notice and this permission notice shall be included in all |
RedBearLab | 0:075ea2812998 | 11 | * copies or substantial portions of the Software. |
RedBearLab | 0:075ea2812998 | 12 | * |
RedBearLab | 0:075ea2812998 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
RedBearLab | 0:075ea2812998 | 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
RedBearLab | 0:075ea2812998 | 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
RedBearLab | 0:075ea2812998 | 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
RedBearLab | 0:075ea2812998 | 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
RedBearLab | 0:075ea2812998 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
RedBearLab | 0:075ea2812998 | 19 | * SOFTWARE. |
RedBearLab | 0:075ea2812998 | 20 | */ |
RedBearLab | 0:075ea2812998 | 21 | |
RedBearLab | 0:075ea2812998 | 22 | #include <lib_aci.h> |
RedBearLab | 0:075ea2812998 | 23 | #include "aci_setup.h" |
RedBearLab | 0:075ea2812998 | 24 | |
RedBearLab | 0:075ea2812998 | 25 | |
RedBearLab | 0:075ea2812998 | 26 | // aci_struct that will contain |
RedBearLab | 0:075ea2812998 | 27 | // total initial credits |
RedBearLab | 0:075ea2812998 | 28 | // current credit |
RedBearLab | 0:075ea2812998 | 29 | // current state of the aci (setup/standby/active/sleep) |
RedBearLab | 0:075ea2812998 | 30 | // open remote pipe pending |
RedBearLab | 0:075ea2812998 | 31 | // close remote pipe pending |
RedBearLab | 0:075ea2812998 | 32 | // Current pipe available bitmap |
RedBearLab | 0:075ea2812998 | 33 | // Current pipe closed bitmap |
RedBearLab | 0:075ea2812998 | 34 | // Current connection interval, slave latency and link supervision timeout |
RedBearLab | 0:075ea2812998 | 35 | // Current State of the the GATT client (Service Discovery status) |
RedBearLab | 0:075ea2812998 | 36 | |
RedBearLab | 0:075ea2812998 | 37 | |
RedBearLab | 0:075ea2812998 | 38 | extern hal_aci_data_t msg_to_send; |
RedBearLab | 0:075ea2812998 | 39 | |
RedBearLab | 0:075ea2812998 | 40 | |
RedBearLab | 0:075ea2812998 | 41 | |
RedBearLab | 0:075ea2812998 | 42 | /************************************************************************** */ |
RedBearLab | 0:075ea2812998 | 43 | /* Utility function to fill the the ACI command queue */ |
RedBearLab | 0:075ea2812998 | 44 | /* aci_stat Pointer to the ACI state */ |
RedBearLab | 0:075ea2812998 | 45 | /* num_cmd_offset(in/out) Offset in the Setup message array to start from */ |
RedBearLab | 0:075ea2812998 | 46 | /* offset is updated to the new index after the queue is filled */ |
RedBearLab | 0:075ea2812998 | 47 | /* or the last message us placed in the queue */ |
RedBearLab | 0:075ea2812998 | 48 | /* Returns true if at least one message was transferred */ |
RedBearLab | 0:075ea2812998 | 49 | /***************************************************************************/ |
RedBearLab | 0:075ea2812998 | 50 | static bool aci_setup_fill(aci_state_t *aci_stat, uint8_t *num_cmd_offset) |
RedBearLab | 0:075ea2812998 | 51 | { |
RedBearLab | 0:075ea2812998 | 52 | bool ret_val = false; |
RedBearLab | 0:075ea2812998 | 53 | |
RedBearLab | 0:075ea2812998 | 54 | while (*num_cmd_offset < aci_stat->aci_setup_info.num_setup_msgs) |
RedBearLab | 0:075ea2812998 | 55 | { |
RedBearLab | 0:075ea2812998 | 56 | //Board dependent defines |
RedBearLab | 0:075ea2812998 | 57 | memcpy(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]), |
RedBearLab | 0:075ea2812998 | 58 | (aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]+2)); |
RedBearLab | 0:075ea2812998 | 59 | |
RedBearLab | 0:075ea2812998 | 60 | //Put the Setup ACI message in the command queue |
RedBearLab | 0:075ea2812998 | 61 | if (!hal_aci_tl_send(&msg_to_send)) |
RedBearLab | 0:075ea2812998 | 62 | { |
RedBearLab | 0:075ea2812998 | 63 | //ACI Command Queue is full |
RedBearLab | 0:075ea2812998 | 64 | // *num_cmd_offset is now pointing to the index of the Setup command that did not get sent |
RedBearLab | 0:075ea2812998 | 65 | return ret_val; |
RedBearLab | 0:075ea2812998 | 66 | } |
RedBearLab | 0:075ea2812998 | 67 | |
RedBearLab | 0:075ea2812998 | 68 | ret_val = true; |
RedBearLab | 0:075ea2812998 | 69 | |
RedBearLab | 0:075ea2812998 | 70 | (*num_cmd_offset)++; |
RedBearLab | 0:075ea2812998 | 71 | } |
RedBearLab | 0:075ea2812998 | 72 | |
RedBearLab | 0:075ea2812998 | 73 | return ret_val; |
RedBearLab | 0:075ea2812998 | 74 | } |
RedBearLab | 0:075ea2812998 | 75 | |
RedBearLab | 0:075ea2812998 | 76 | uint8_t do_aci_setup(aci_state_t *aci_stat) |
RedBearLab | 0:075ea2812998 | 77 | { |
RedBearLab | 0:075ea2812998 | 78 | uint8_t setup_offset = 0; |
RedBearLab | 0:075ea2812998 | 79 | uint32_t i = 0x0000; |
RedBearLab | 0:075ea2812998 | 80 | aci_evt_t * aci_evt = NULL; |
RedBearLab | 0:075ea2812998 | 81 | aci_status_code_t cmd_status = ACI_STATUS_ERROR_CRC_MISMATCH; |
RedBearLab | 0:075ea2812998 | 82 | |
RedBearLab | 0:075ea2812998 | 83 | /* |
RedBearLab | 0:075ea2812998 | 84 | We are using the same buffer since we are copying the contents of the buffer |
RedBearLab | 0:075ea2812998 | 85 | when queuing and immediately processing the buffer when receiving |
RedBearLab | 0:075ea2812998 | 86 | */ |
RedBearLab | 0:075ea2812998 | 87 | hal_aci_evt_t *aci_data = (hal_aci_evt_t *)&msg_to_send; |
RedBearLab | 0:075ea2812998 | 88 | |
RedBearLab | 0:075ea2812998 | 89 | /* Messages in the outgoing queue must be handled before the Setup routine can run. |
RedBearLab | 0:075ea2812998 | 90 | * If it is non-empty we return. The user should then process the messages before calling |
RedBearLab | 0:075ea2812998 | 91 | * do_aci_setup() again. |
RedBearLab | 0:075ea2812998 | 92 | */ |
RedBearLab | 0:075ea2812998 | 93 | if (!lib_aci_command_queue_empty()) |
RedBearLab | 0:075ea2812998 | 94 | { |
RedBearLab | 0:075ea2812998 | 95 | return SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY; |
RedBearLab | 0:075ea2812998 | 96 | } |
RedBearLab | 0:075ea2812998 | 97 | |
RedBearLab | 0:075ea2812998 | 98 | /* If there are events pending from the device that are not relevant to setup, we return false |
RedBearLab | 0:075ea2812998 | 99 | * so that the user can handle them. At this point we don't care what the event is, |
RedBearLab | 0:075ea2812998 | 100 | * as any event is an error. |
RedBearLab | 0:075ea2812998 | 101 | */ |
RedBearLab | 0:075ea2812998 | 102 | if (lib_aci_event_peek(aci_data)) |
RedBearLab | 0:075ea2812998 | 103 | { |
RedBearLab | 0:075ea2812998 | 104 | return SETUP_FAIL_EVENT_QUEUE_NOT_EMPTY; |
RedBearLab | 0:075ea2812998 | 105 | } |
RedBearLab | 0:075ea2812998 | 106 | |
RedBearLab | 0:075ea2812998 | 107 | /* Fill the ACI command queue with as many Setup messages as it will hold. */ |
RedBearLab | 0:075ea2812998 | 108 | aci_setup_fill(aci_stat, &setup_offset); |
RedBearLab | 0:075ea2812998 | 109 | |
RedBearLab | 0:075ea2812998 | 110 | while (cmd_status != ACI_STATUS_TRANSACTION_COMPLETE) |
RedBearLab | 0:075ea2812998 | 111 | { |
RedBearLab | 0:075ea2812998 | 112 | /* This counter is used to ensure that this function does not loop forever. When the device |
RedBearLab | 0:075ea2812998 | 113 | * returns a valid response, we reset the counter. |
RedBearLab | 0:075ea2812998 | 114 | */ |
RedBearLab | 0:075ea2812998 | 115 | if (i++ > 0xFFFFE) |
RedBearLab | 0:075ea2812998 | 116 | { |
RedBearLab | 0:075ea2812998 | 117 | return SETUP_FAIL_TIMEOUT; |
RedBearLab | 0:075ea2812998 | 118 | } |
RedBearLab | 0:075ea2812998 | 119 | |
RedBearLab | 0:075ea2812998 | 120 | if (lib_aci_event_peek(aci_data)) |
RedBearLab | 0:075ea2812998 | 121 | { |
RedBearLab | 0:075ea2812998 | 122 | aci_evt = &(aci_data->evt); |
RedBearLab | 0:075ea2812998 | 123 | |
RedBearLab | 0:075ea2812998 | 124 | if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode) |
RedBearLab | 0:075ea2812998 | 125 | { |
RedBearLab | 0:075ea2812998 | 126 | //Receiving something other than a Command Response Event is an error. |
RedBearLab | 0:075ea2812998 | 127 | return SETUP_FAIL_NOT_COMMAND_RESPONSE; |
RedBearLab | 0:075ea2812998 | 128 | } |
RedBearLab | 0:075ea2812998 | 129 | |
RedBearLab | 0:075ea2812998 | 130 | cmd_status = (aci_status_code_t) aci_evt->params.cmd_rsp.cmd_status; |
RedBearLab | 0:075ea2812998 | 131 | switch (cmd_status) |
RedBearLab | 0:075ea2812998 | 132 | { |
RedBearLab | 0:075ea2812998 | 133 | case ACI_STATUS_TRANSACTION_CONTINUE: |
RedBearLab | 0:075ea2812998 | 134 | //As the device is responding, reset guard counter |
RedBearLab | 0:075ea2812998 | 135 | i = 0; |
RedBearLab | 0:075ea2812998 | 136 | |
RedBearLab | 0:075ea2812998 | 137 | /* As the device has processed the Setup messages we put in the command queue earlier, |
RedBearLab | 0:075ea2812998 | 138 | * we can proceed to fill the queue with new messages |
RedBearLab | 0:075ea2812998 | 139 | */ |
RedBearLab | 0:075ea2812998 | 140 | aci_setup_fill(aci_stat, &setup_offset); |
RedBearLab | 0:075ea2812998 | 141 | break; |
RedBearLab | 0:075ea2812998 | 142 | |
RedBearLab | 0:075ea2812998 | 143 | case ACI_STATUS_TRANSACTION_COMPLETE: |
RedBearLab | 0:075ea2812998 | 144 | //Break out of the while loop when this status code appears |
RedBearLab | 0:075ea2812998 | 145 | break; |
RedBearLab | 0:075ea2812998 | 146 | |
RedBearLab | 0:075ea2812998 | 147 | default: |
RedBearLab | 0:075ea2812998 | 148 | //An event with any other status code should be handled by the application |
RedBearLab | 0:075ea2812998 | 149 | return SETUP_FAIL_NOT_SETUP_EVENT; |
RedBearLab | 0:075ea2812998 | 150 | } |
RedBearLab | 0:075ea2812998 | 151 | |
RedBearLab | 0:075ea2812998 | 152 | /* If we haven't returned at this point, the event was either ACI_STATUS_TRANSACTION_CONTINUE |
RedBearLab | 0:075ea2812998 | 153 | * or ACI_STATUS_TRANSACTION_COMPLETE. We don't need the event itself, so we simply |
RedBearLab | 0:075ea2812998 | 154 | * remove it from the queue. |
RedBearLab | 0:075ea2812998 | 155 | */ |
RedBearLab | 0:075ea2812998 | 156 | lib_aci_event_get (aci_stat, aci_data); |
RedBearLab | 0:075ea2812998 | 157 | } |
RedBearLab | 0:075ea2812998 | 158 | } |
RedBearLab | 0:075ea2812998 | 159 | |
RedBearLab | 0:075ea2812998 | 160 | return SETUP_SUCCESS; |
RedBearLab | 0:075ea2812998 | 161 | } |
RedBearLab | 0:075ea2812998 | 162 |