Previous version which works for my stm32f401 Nucleo board

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
mridup
Date:
Wed Aug 13 08:44:09 2014 +0000
Revision:
21:daebaf11a520
Parent:
20:718d56821672
Child:
22:36fd20c0fb9c
Support for Setting GATT Event Mask in case of WRITE ATTR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mridup 2:a2b623661316 1 /* mbed Microcontroller Library
mridup 2:a2b623661316 2 * Copyright (c) 2006-2013 ARM Limited
mridup 2:a2b623661316 3 *
mridup 2:a2b623661316 4 * Licensed under the Apache License, Version 2.0 (the "License");
mridup 2:a2b623661316 5 * you may not use this file except in compliance with the License.
mridup 2:a2b623661316 6 * You may obtain a copy of the License at
mridup 2:a2b623661316 7 *
mridup 2:a2b623661316 8 * http://www.apache.org/licenses/LICENSE-2.0
mridup 2:a2b623661316 9 *
mridup 2:a2b623661316 10 * Unless required by applicable law or agreed to in writing, software
mridup 2:a2b623661316 11 * distributed under the License is distributed on an "AS IS" BASIS,
mridup 2:a2b623661316 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mridup 2:a2b623661316 13 * See the License for the specific language governing permissions and
mridup 2:a2b623661316 14 * limitations under the License.
mridup 2:a2b623661316 15 */
mridup 2:a2b623661316 16
mridup 2:a2b623661316 17 #include "BlueNRGGattServer.h"
mridup 2:a2b623661316 18 #include "mbed.h"
mridup 2:a2b623661316 19 #include "BlueNRGGap.h"
mridup 17:63059fcb8db2 20 #include "Utils.h"
mridup 2:a2b623661316 21
mridup 5:31dedfa19a12 22 #define STORE_LE_16(buf, val) ( ((buf)[0] = (tHalUint8) (val) ) , \
mridup 5:31dedfa19a12 23 ((buf)[1] = (tHalUint8) (val>>8) ) )
mridup 5:31dedfa19a12 24
mridup 5:31dedfa19a12 25 #define STORE_LE_32(buf, val) ( ((buf)[0] = (tHalUint8) (val) ) , \
mridup 5:31dedfa19a12 26 ((buf)[1] = (tHalUint8) (val>>8) ) , \
mridup 5:31dedfa19a12 27 ((buf)[2] = (tHalUint8) (val>>16) ) , \
mridup 5:31dedfa19a12 28 ((buf)[3] = (tHalUint8) (val>>24) ) )
mridup 9:dbc2cfe5c24f 29
mridup 2:a2b623661316 30 /**************************************************************************/
mridup 2:a2b623661316 31 /*!
mridup 2:a2b623661316 32 @brief Adds a new service to the GATT table on the peripheral
mridup 2:a2b623661316 33
mridup 2:a2b623661316 34 @returns ble_error_t
mridup 2:a2b623661316 35
mridup 2:a2b623661316 36 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 37 Everything executed properly
mridup 2:a2b623661316 38
mridup 2:a2b623661316 39 @section EXAMPLE
mridup 2:a2b623661316 40
mridup 2:a2b623661316 41 @code
mridup 2:a2b623661316 42
mridup 2:a2b623661316 43 @endcode
mridup 2:a2b623661316 44 */
mridup 2:a2b623661316 45 /**************************************************************************/
mridup 2:a2b623661316 46 ble_error_t BlueNRGGattServer::addService(GattService &service)
mridup 2:a2b623661316 47 {
mridup 2:a2b623661316 48 /* ToDo: Make sure we don't overflow the array, etc. */
mridup 2:a2b623661316 49 /* ToDo: Make sure this service UUID doesn't already exist (?) */
mridup 2:a2b623661316 50 /* ToDo: Basic validation */
mridup 5:31dedfa19a12 51
mridup 5:31dedfa19a12 52 tBleStatus ret;
mridup 5:31dedfa19a12 53
mridup 20:718d56821672 54 DEBUG("AddService()\n\r");
mridup 5:31dedfa19a12 55 /* Add the service to the BlueNRG */
mridup 9:dbc2cfe5c24f 56 uint16_t short_uuid = (service.getUUID()).getShortUUID();
mridup 9:dbc2cfe5c24f 57
mridup 9:dbc2cfe5c24f 58 uint8_t primary_uuid[2];//= {0x0D,0x18};
mridup 9:dbc2cfe5c24f 59 STORE_LE_16(primary_uuid, short_uuid);
mridup 7:55ac052585db 60
mridup 7:55ac052585db 61 //TODO: Check UUID existence??
mridup 5:31dedfa19a12 62
mridup 8:486bcaa4eb3d 63 ret = aci_gatt_add_serv(UUID_TYPE_16, primary_uuid, PRIMARY_SERVICE, 7,
mridup 5:31dedfa19a12 64 &hrmServHandle);
mridup 5:31dedfa19a12 65 service.setHandle(hrmServHandle);
mridup 2:a2b623661316 66
mridup 7:55ac052585db 67 //TODO: iterate to include all characteristics
mridup 7:55ac052585db 68 for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
mridup 20:718d56821672 69 GattCharacteristic *p_char = service.getCharacteristic(i);
mridup 5:31dedfa19a12 70 uint16_t char_uuid = (p_char->getUUID()).getShortUUID();
mridup 7:55ac052585db 71
mridup 13:4c30346287e4 72 uint8_t int_8_uuid[2];
mridup 13:4c30346287e4 73 STORE_LE_16(int_8_uuid, char_uuid);
mridup 7:55ac052585db 74 //TODO: Check UUID existence??
mridup 20:718d56821672 75 DEBUG("Char Properties 0x%x\n\r", p_char->getProperties());
mridup 20:718d56821672 76 /*
mridup 20:718d56821672 77 * Gatt_Evt_Mask -> HardCoded (0)
mridup 20:718d56821672 78 * Encryption_Key_Size -> Hardcoded (16)
mridup 20:718d56821672 79 * isVariable (variable length value field) -> Hardcoded (1)
mridup 20:718d56821672 80 */
mridup 21:daebaf11a520 81 tGattServerEvent Gatt_Evt_Mask = 0x0;
mridup 21:daebaf11a520 82
mridup 21:daebaf11a520 83 if((p_char->getProperties() &
mridup 21:daebaf11a520 84 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
mridup 21:daebaf11a520 85 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
mridup 21:daebaf11a520 86 DEBUG("Setting up Gatt EVt Mask");
mridup 21:daebaf11a520 87 Gatt_Evt_Mask = GATT_SERVER_ATTR_WRITE;
mridup 21:daebaf11a520 88 }
mridup 21:daebaf11a520 89
mridup 20:718d56821672 90 ret = aci_gatt_add_char(service.getHandle(), UUID_TYPE_16, int_8_uuid, p_char->getMaxLength() /*2*/ /*Value Length*/,
mridup 21:daebaf11a520 91 p_char->getProperties(), ATTR_PERMISSION_NONE, Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
mridup 20:718d56821672 92 16 /*Encryption_Key_Size*/, 1 /*isVariable*/, &bleCharacteristicHandles[characteristicCount]);
mridup 7:55ac052585db 93
mridup 7:55ac052585db 94 /* Update the characteristic handle */
mridup 7:55ac052585db 95 uint16_t charHandle = characteristicCount;
mridup 5:31dedfa19a12 96
mridup 5:31dedfa19a12 97 p_characteristics[characteristicCount++] = p_char;
mridup 7:55ac052585db 98 p_char->setHandle(charHandle); //Set the characteristic count as the corresponding char handle
mridup 7:55ac052585db 99
mridup 7:55ac052585db 100 if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) {
mridup 7:55ac052585db 101 updateValue(charHandle, p_char->getValuePtr(), p_char->getInitialLength(), false /* localOnly */);
mridup 7:55ac052585db 102 }
mridup 7:55ac052585db 103 }
mridup 7:55ac052585db 104
mridup 5:31dedfa19a12 105 serviceCount++;
mridup 7:55ac052585db 106
mridup 7:55ac052585db 107 //FIXME: There is no GattService pointer array in GattServer.
mridup 7:55ac052585db 108 // There should be one? (Only the user is aware of GattServices!) Report to forum.
mridup 5:31dedfa19a12 109
mridup 2:a2b623661316 110 return BLE_ERROR_NONE;
mridup 2:a2b623661316 111 }
mridup 2:a2b623661316 112
mridup 7:55ac052585db 113
mridup 2:a2b623661316 114 /**************************************************************************/
mridup 2:a2b623661316 115 /*!
mridup 2:a2b623661316 116 @brief Reads the value of a characteristic, based on the service
mridup 2:a2b623661316 117 and characteristic index fields
mridup 2:a2b623661316 118
mridup 2:a2b623661316 119 @param[in] charHandle
mridup 2:a2b623661316 120 The handle of the GattCharacteristic to read from
mridup 2:a2b623661316 121 @param[in] buffer
mridup 2:a2b623661316 122 Buffer to hold the the characteristic's value
mridup 2:a2b623661316 123 (raw byte array in LSB format)
mridup 2:a2b623661316 124 @param[in] len
mridup 2:a2b623661316 125 The number of bytes read into the buffer
mridup 2:a2b623661316 126
mridup 2:a2b623661316 127 @returns ble_error_t
mridup 2:a2b623661316 128
mridup 2:a2b623661316 129 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 130 Everything executed properly
mridup 2:a2b623661316 131
mridup 2:a2b623661316 132 @section EXAMPLE
mridup 2:a2b623661316 133
mridup 2:a2b623661316 134 @code
mridup 2:a2b623661316 135
mridup 2:a2b623661316 136 @endcode
mridup 2:a2b623661316 137 */
mridup 2:a2b623661316 138 /**************************************************************************/
mridup 2:a2b623661316 139 ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP)
mridup 2:a2b623661316 140 {
mridup 20:718d56821672 141 DEBUG("ReadValue()\n\r");
mridup 2:a2b623661316 142 return BLE_ERROR_NONE;
mridup 2:a2b623661316 143 }
mridup 2:a2b623661316 144
mridup 2:a2b623661316 145 /**************************************************************************/
mridup 2:a2b623661316 146 /*!
mridup 2:a2b623661316 147 @brief Updates the value of a characteristic, based on the service
mridup 2:a2b623661316 148 and characteristic index fields
mridup 2:a2b623661316 149
mridup 2:a2b623661316 150 @param[in] charHandle
mridup 2:a2b623661316 151 The handle of the GattCharacteristic to write to
mridup 2:a2b623661316 152 @param[in] buffer
mridup 2:a2b623661316 153 Data to use when updating the characteristic's value
mridup 2:a2b623661316 154 (raw byte array in LSB format)
mridup 2:a2b623661316 155 @param[in] len
mridup 2:a2b623661316 156 The number of bytes in buffer
mridup 2:a2b623661316 157
mridup 2:a2b623661316 158 @returns ble_error_t
mridup 2:a2b623661316 159
mridup 2:a2b623661316 160 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 161 Everything executed properly
mridup 2:a2b623661316 162
mridup 2:a2b623661316 163 @section EXAMPLE
mridup 2:a2b623661316 164
mridup 2:a2b623661316 165 @code
mridup 2:a2b623661316 166
mridup 2:a2b623661316 167 @endcode
mridup 2:a2b623661316 168 */
mridup 2:a2b623661316 169 /**************************************************************************/
mridup 2:a2b623661316 170 ble_error_t BlueNRGGattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly)
mridup 2:a2b623661316 171 {
mridup 17:63059fcb8db2 172 tBleStatus ret;
mridup 17:63059fcb8db2 173 tHalUint8 buff[2];
mridup 2:a2b623661316 174
mridup 20:718d56821672 175 //STORE_LE_16(buff,125);
mridup 17:63059fcb8db2 176
mridup 20:718d56821672 177 //DEBUG("CharHandle: %d\n\r", charHandle);
mridup 20:718d56821672 178 //DEBUG("Actual Handle: 0x%x\n\r", bleCharacteristicHandles[charHandle]);
mridup 20:718d56821672 179 //DEBUG("Service Handle: 0x%x\n\r", hrmServHandle);
mridup 20:718d56821672 180 //DEBUG("buffer[0]: %d\n\r", buffer[0]);
mridup 20:718d56821672 181 //DEBUG("buffer[1]: %d\n\r", buffer[1]);
mridup 20:718d56821672 182 //DEBUG("len: %d\n\r", len);
mridup 5:31dedfa19a12 183
mridup 17:63059fcb8db2 184 ret = aci_gatt_update_char_value(hrmServHandle, bleCharacteristicHandles[charHandle], 0, len, buffer);
mridup 2:a2b623661316 185
mridup 17:63059fcb8db2 186 if (ret != BLE_STATUS_SUCCESS){
mridup 20:718d56821672 187 DEBUG("Error while updating characteristic.\n\r") ;
mridup 17:63059fcb8db2 188 return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value
mridup 17:63059fcb8db2 189 //FIXME: Define Error values equivalent to BlueNRG Error Codes.
mridup 17:63059fcb8db2 190 }
mridup 17:63059fcb8db2 191 return BLE_ERROR_NONE;
mridup 2:a2b623661316 192 }