Home | History | Annotate | Download | only in nrf8001
      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 
     23 #include "aci.h"
     24 #include "hal_aci_tl.h"
     25 #include <lib_aci.h>
     26 #include "aci_setup.h"
     27 
     28 
     29 // aci_struct that will contain
     30 // total initial credits
     31 // current credit
     32 // current state of the aci (setup/standby/active/sleep)
     33 // open remote pipe pending
     34 // close remote pipe pending
     35 // Current pipe available bitmap
     36 // Current pipe closed bitmap
     37 // Current connection interval, slave latency and link supervision timeout
     38 // Current State of the the GATT client (Service Discovery status)
     39 
     40 
     41 // hal_aci_data_t msg_to_send;
     42 extern hal_aci_data_t msg_to_send;
     43 
     44 
     45 /**************************************************************************                */
     46 /* Utility function to fill the the ACI command queue                                      */
     47 /* aci_stat               Pointer to the ACI state                                         */
     48 /* num_cmd_offset(in/out) Offset in the Setup message array to start from                  */
     49 /*                        offset is updated to the new index after the queue is filled     */
     50 /*                        or the last message us placed in the queue                       */
     51 /* Returns                true if at least one message was transferred                     */
     52 /***************************************************************************/
     53 bool aci_setup_fill(aci_state_t *aci_stat, uint8_t *num_cmd_offset)
     54 {
     55   bool ret_val = false;
     56 
     57   while (*num_cmd_offset < aci_stat->aci_setup_info.num_setup_msgs)
     58   {
     59     //Board dependent defines
     60     /*#if defined (__AVR__)
     61         //For Arduino copy the setup ACI message from Flash to RAM.
     62         memcpy_P(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]),
     63                   pgm_read_byte_near(&(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]))+2);
     64     #elif defined(__PIC32MX__)
     65         //In ChipKit we store the setup messages in RAM
     66         //Add 2 bytes to the length byte for status byte, length for the total number of bytes
     67         memcpy(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]),
     68                   (aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]+2));
     69     #endif*/
     70 
     71     memcpy(&msg_to_send, &(aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset]),
     72                   (aci_stat->aci_setup_info.setup_msgs[*num_cmd_offset].buffer[0]+2));
     73 
     74     //Put the Setup ACI message in the command queue
     75     if (!hal_aci_tl_send(&msg_to_send))
     76     {
     77       //ACI Command Queue is full
     78       // *num_cmd_offset is now pointing to the index of the Setup command that did not get sent
     79       return ret_val;
     80    }
     81 
     82     ret_val = true;
     83 
     84     (*num_cmd_offset)++;
     85   }
     86 
     87   return ret_val;
     88 }
     89 
     90 uint8_t do_aci_setup(aci_state_t *aci_stat)
     91 {
     92   uint8_t setup_offset         = 0;
     93   uint32_t i                   = 0x0000;
     94   aci_evt_t * aci_evt          = NULL;
     95   aci_status_code_t cmd_status = ACI_STATUS_ERROR_CRC_MISMATCH;
     96 
     97   /*
     98   We are using the same buffer since we are copying the contents of the buffer
     99   when queuing and immediately processing the buffer when receiving
    100   */
    101   hal_aci_evt_t  *aci_data = (hal_aci_evt_t *)&msg_to_send;
    102 
    103   /* Messages in the outgoing queue must be handled before the Setup routine can run.
    104    * If it is non-empty we return. The user should then process the messages before calling
    105    * do_aci_setup() again.
    106    */
    107   if (!lib_aci_command_queue_empty())
    108   {
    109     return SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY;
    110   }
    111 
    112   /* If there are events pending from the device that are not relevant to setup, we return false
    113    * so that the user can handle them. At this point we don't care what the event is,
    114    * as any event is an error.
    115    */
    116   if (lib_aci_event_peek(aci_data))
    117   {
    118     return SETUP_FAIL_EVENT_QUEUE_NOT_EMPTY;
    119   }
    120 
    121   /* Fill the ACI command queue with as many Setup messages as it will hold. */
    122   aci_setup_fill(aci_stat, &setup_offset);
    123 
    124   while (cmd_status != ACI_STATUS_TRANSACTION_COMPLETE)
    125   {
    126     /* This counter is used to ensure that this function does not loop forever. When the device
    127      * returns a valid response, we reset the counter.
    128      */
    129     if (i++ > 0xFFFFE)
    130     {
    131       return SETUP_FAIL_TIMEOUT;
    132     }
    133 
    134     if (lib_aci_event_peek(aci_data))
    135     {
    136       aci_evt = &(aci_data->evt);
    137 
    138       if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode)
    139       {
    140         //Receiving something other than a Command Response Event is an error.
    141         return SETUP_FAIL_NOT_COMMAND_RESPONSE;
    142       }
    143 
    144       cmd_status = (aci_status_code_t) aci_evt->params.cmd_rsp.cmd_status;
    145       switch (cmd_status)
    146       {
    147         case ACI_STATUS_TRANSACTION_CONTINUE:
    148           //As the device is responding, reset guard counter
    149           i = 0;
    150 
    151           /* As the device has processed the Setup messages we put in the command queue earlier,
    152            * we can proceed to fill the queue with new messages
    153            */
    154           aci_setup_fill(aci_stat, &setup_offset);
    155           break;
    156 
    157         case ACI_STATUS_TRANSACTION_COMPLETE:
    158           //Break out of the while loop when this status code appears
    159           break;
    160 
    161         default:
    162           //An event with any other status code should be handled by the application
    163           return SETUP_FAIL_NOT_SETUP_EVENT;
    164       }
    165 
    166       /* If we haven't returned at this point, the event was either ACI_STATUS_TRANSACTION_CONTINUE
    167        * or ACI_STATUS_TRANSACTION_COMPLETE. We don't need the event itself, so we simply
    168        * remove it from the queue.
    169        */
    170        lib_aci_event_get (aci_stat, aci_data);
    171     }
    172   }
    173 
    174   return SETUP_SUCCESS;
    175 }
    176 
    177 
    178