BLE temperature profile using digital DS1820 or analog LM35 sensors

Dependencies:   DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers custom_helper.cpp Source File

custom_helper.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017  #include "custom_helper.h"
00018 
00019 /**************************************************************************/
00020 /*!
00021     @brief      Adds the base UUID to the custom service. All UUIDs used
00022                 by this service are based on this 128-bit UUID.
00023                 
00024     @note       This UUID needs to be added to the SoftDevice stack before
00025                 adding the service's primary service via
00026                 'sd_ble_gatts_service_add'
00027 
00028     @param[in]  p_uuid_base   A pointer to the 128-bit UUID array (8*16)
00029     
00030     @returns    The UUID type.
00031                 A return value of 0 should be considered an error.
00032                 
00033     @retval     0x00    BLE_UUID_TYPE_UNKNOWN
00034     @retval     0x01    BLE_UUID_TYPE_BLE
00035     @retval     0x02    BLE_UUID_TYPE_VENDOR_BEGIN
00036     
00037     @section EXAMPLE
00038     @code
00039 
00040     // Take note that bytes 2/3 are blank since these are used to identify
00041     // the primary service and individual characteristics
00042     #define CFG_CUSTOM_UUID_BASE  "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E"
00043     
00044     uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE);
00045     ASSERT(uuid_type > 0, ERROR_NOT_FOUND);
00046     
00047     // We can now safely add the primary service and any characteristics
00048     // for our custom service ...
00049     
00050     @endcode
00051 */
00052 /**************************************************************************/
00053 uint8_t custom_add_uuid_base(uint8_t const * const p_uuid_base)
00054 {
00055   ble_uuid128_t base_uuid;
00056   uint8_t uuid_type = 0;
00057 
00058   /* Reverse the bytes since ble_uuid128_t is LSB */
00059   for(uint8_t i=0; i<16; i++)
00060   {
00061     base_uuid.uuid128[i] = p_uuid_base[15-i];
00062   }
00063 
00064   ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0);
00065 
00066   return uuid_type;
00067 }
00068 
00069 /**************************************************************************/
00070 /*!
00071 
00072 */
00073 /**************************************************************************/
00074 error_t custom_decode_uuid_base(uint8_t const * const p_uuid_base, ble_uuid_t * p_uuid)
00075 {
00076   uint8_t uuid_base_le[16];
00077 
00078   /* Reverse the bytes since ble_uuid128_t is LSB */
00079   for(uint8_t i=0; i<16; i++)
00080   {
00081     uuid_base_le[i] = p_uuid_base[15-i];
00082   }
00083 
00084   ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid) );
00085 
00086   return ERROR_NONE;
00087 }
00088 
00089 /**************************************************************************/
00090 /*!
00091     @brief      Adds a new characteristic to the custom service, assigning
00092                 properties, a UUID add-on value, etc.
00093 
00094     @param[in]  service_handle
00095     @param[in]  p_uuid            The 16-bit value to add to the base UUID
00096                                   for this characteristic (normally >1
00097                                   since 1 is typically used by the primary
00098                                   service).
00099     @param[in]  char_props        The characteristic properties, as
00100                                   defined by ble_gatt_char_props_t
00101     @param[in]  max_length        The maximum length of this characeristic
00102     @param[in]  p_char_handle
00103     
00104     @returns
00105     @retval     ERROR_NONE        Everything executed normally
00106 */
00107 /**************************************************************************/
00108 error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t* p_uuid, uint8_t properties,
00109                                      uint8_t *p_data, uint16_t min_length, uint16_t max_length,
00110                                      ble_gatts_char_handles_t* p_char_handle)
00111 {
00112   /* Characteristic metadata */
00113   ble_gatts_attr_md_t cccd_md;
00114   ble_gatt_char_props_t char_props;
00115 
00116   memcpy(&char_props, &properties, 1);
00117 
00118   if ( char_props.notify || char_props.indicate )
00119   { 
00120     /* Notification requires cccd */
00121     memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t) );
00122     cccd_md.vloc = BLE_GATTS_VLOC_STACK;
00123     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
00124     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
00125   }
00126 
00127   ble_gatts_char_md_t char_md = { 0 };
00128 
00129   char_md.char_props = char_props;
00130   char_md.p_cccd_md  = (char_props.notify || char_props.indicate ) ? &cccd_md : NULL;
00131 
00132   /* Attribute declaration */
00133   ble_gatts_attr_md_t attr_md = { 0 };
00134 
00135   attr_md.vloc = BLE_GATTS_VLOC_STACK;
00136   attr_md.vlen = (min_length == max_length) ? 0 : 1;
00137 
00138   if ( char_props.read || char_props.notify || char_props.indicate )
00139   {
00140     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
00141   }
00142 
00143   if ( char_props.write )
00144   {
00145     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
00146   }
00147 
00148   ble_gatts_attr_t    attr_char_value = { 0 };
00149 
00150   attr_char_value.p_uuid    = p_uuid;
00151   attr_char_value.p_attr_md = &attr_md;
00152   attr_char_value.init_len  = min_length;
00153   attr_char_value.max_len   = max_length;
00154   attr_char_value.p_value   = p_data;
00155 
00156 
00157   ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle,
00158                                                   &char_md,
00159                                                   &attr_char_value,
00160                                                   p_char_handle) );
00161 
00162   return ERROR_NONE;
00163 }