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