SG RFID nRF51822 fork

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ble_srv_common.h Source File

ble_srv_common.h

Go to the documentation of this file.
00001 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  */
00012 
00013 /** @file
00014  *
00015  * @defgroup ble_sdk_srv_common 'Common service definitions'
00016  * @{
00017  * @ingroup ble_sdk_srv
00018  * @brief Constants, type definitions and functions that are common to all services.
00019  */
00020 
00021 #ifndef BLE_SRV_COMMON_H__
00022 #define BLE_SRV_COMMON_H__
00023 
00024 #include <stdint.h>
00025 #include <stdbool.h>
00026 #include "ble_types.h"
00027 #include "app_util.h "
00028 #include "ble_gap.h"
00029 #include "ble_gatt.h"
00030 
00031 /** @defgroup UUID_SERVICES Service UUID definitions
00032  * @{ */
00033 #define BLE_UUID_ALERT_NOTIFICATION_SERVICE                      0x1811     /**< Alert Notification service UUID. */
00034 #define BLE_UUID_BATTERY_SERVICE                                 0x180F     /**< Battery service UUID. */
00035 #define BLE_UUID_BLOOD_PRESSURE_SERVICE                          0x1810     /**< Blood Pressure service UUID. */
00036 #define BLE_UUID_CURRENT_TIME_SERVICE                            0x1805     /**< Current Time service UUID. */
00037 #define BLE_UUID_CYCLING_SPEED_AND_CADENCE                       0x1816     /**< Cycling Speed and Cadence service UUID. */
00038 #define BLE_UUID_DEVICE_INFORMATION_SERVICE                      0x180A     /**< Device Information service UUID. */
00039 #define BLE_UUID_GLUCOSE_SERVICE                                 0x1808     /**< Glucose service UUID. */
00040 #define BLE_UUID_HEALTH_THERMOMETER_SERVICE                      0x1809     /**< Health Thermometer service UUID. */
00041 #define BLE_UUID_HEART_RATE_SERVICE                              0x180D     /**< Heart Rate service UUID. */
00042 #define BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE                  0x1812     /**< Human Interface Device service UUID. */
00043 #define BLE_UUID_IMMEDIATE_ALERT_SERVICE                         0x1802     /**< Immediate Alert service UUID. */
00044 #define BLE_UUID_LINK_LOSS_SERVICE                               0x1803     /**< Link Loss service UUID. */
00045 #define BLE_UUID_NEXT_DST_CHANGE_SERVICE                         0x1807     /**< Next Dst Change service UUID. */
00046 #define BLE_UUID_PHONE_ALERT_STATUS_SERVICE                      0x180E     /**< Phone Alert Status service UUID. */
00047 #define BLE_UUID_REFERENCE_TIME_UPDATE_SERVICE                   0x1806     /**< Reference Time Update service UUID. */
00048 #define BLE_UUID_RUNNING_SPEED_AND_CADENCE                       0x1814     /**< Running Speed and Cadence service UUID. */
00049 #define BLE_UUID_SCAN_PARAMETERS_SERVICE                         0x1813     /**< Scan Parameters service UUID. */
00050 #define BLE_UUID_TX_POWER_SERVICE                                0x1804     /**< TX Power service UUID. */
00051 /** @} */
00052 
00053 /** @defgroup UUID_CHARACTERISTICS Characteristic UUID definitions
00054  * @{ */
00055 #define BLE_UUID_BATTERY_LEVEL_STATE_CHAR                        0x2A1B     /**< Battery Level State characteristic UUID. */
00056 #define BLE_UUID_BATTERY_POWER_STATE_CHAR                        0x2A1A     /**< Battery Power State characteristic UUID. */
00057 #define BLE_UUID_REMOVABLE_CHAR                                  0x2A3A     /**< Removable characteristic UUID. */
00058 #define BLE_UUID_SERVICE_REQUIRED_CHAR                           0x2A3B     /**< Service Required characteristic UUID. */
00059 #define BLE_UUID_ALERT_CATEGORY_ID_CHAR                          0x2A43     /**< Alert Category Id characteristic UUID. */
00060 #define BLE_UUID_ALERT_CATEGORY_ID_BIT_MASK_CHAR                 0x2A42     /**< Alert Category Id Bit Mask characteristic UUID. */
00061 #define BLE_UUID_ALERT_LEVEL_CHAR                                0x2A06     /**< Alert Level characteristic UUID. */
00062 #define BLE_UUID_ALERT_NOTIFICATION_CONTROL_POINT_CHAR           0x2A44     /**< Alert Notification Control Point characteristic UUID. */
00063 #define BLE_UUID_ALERT_STATUS_CHAR                               0x2A3F     /**< Alert Status characteristic UUID. */
00064 #define BLE_UUID_BATTERY_LEVEL_CHAR                              0x2A19     /**< Battery Level characteristic UUID. */
00065 #define BLE_UUID_BLOOD_PRESSURE_FEATURE_CHAR                     0x2A49     /**< Blood Pressure Feature characteristic UUID. */
00066 #define BLE_UUID_BLOOD_PRESSURE_MEASUREMENT_CHAR                 0x2A35     /**< Blood Pressure Measurement characteristic UUID. */
00067 #define BLE_UUID_BODY_SENSOR_LOCATION_CHAR                       0x2A38     /**< Body Sensor Location characteristic UUID. */
00068 #define BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR                 0x2A22     /**< Boot Keyboard Input Report characteristic UUID. */
00069 #define BLE_UUID_BOOT_KEYBOARD_OUTPUT_REPORT_CHAR                0x2A32     /**< Boot Keyboard Output Report characteristic UUID. */
00070 #define BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR                    0x2A33     /**< Boot Mouse Input Report characteristic UUID. */
00071 #define BLE_UUID_CURRENT_TIME_CHAR                               0x2A2B     /**< Current Time characteristic UUID. */
00072 #define BLE_UUID_DATE_TIME_CHAR                                  0x2A08     /**< Date Time characteristic UUID. */
00073 #define BLE_UUID_DAY_DATE_TIME_CHAR                              0x2A0A     /**< Day Date Time characteristic UUID. */
00074 #define BLE_UUID_DAY_OF_WEEK_CHAR                                0x2A09     /**< Day Of Week characteristic UUID. */
00075 #define BLE_UUID_DST_OFFSET_CHAR                                 0x2A0D     /**< Dst Offset characteristic UUID. */
00076 #define BLE_UUID_EXACT_TIME_256_CHAR                             0x2A0C     /**< Exact Time 256 characteristic UUID. */
00077 #define BLE_UUID_FIRMWARE_REVISION_STRING_CHAR                   0x2A26     /**< Firmware Revision String characteristic UUID. */
00078 #define BLE_UUID_GLUCOSE_FEATURE_CHAR                            0x2A51     /**< Glucose Feature characteristic UUID. */
00079 #define BLE_UUID_GLUCOSE_MEASUREMENT_CHAR                        0x2A18     /**< Glucose Measurement characteristic UUID. */
00080 #define BLE_UUID_GLUCOSE_MEASUREMENT_CONTEXT_CHAR                0x2A34     /**< Glucose Measurement Context characteristic UUID. */
00081 #define BLE_UUID_HARDWARE_REVISION_STRING_CHAR                   0x2A27     /**< Hardware Revision String characteristic UUID. */
00082 #define BLE_UUID_HEART_RATE_CONTROL_POINT_CHAR                   0x2A39     /**< Heart Rate Control Point characteristic UUID. */
00083 #define BLE_UUID_HEART_RATE_MEASUREMENT_CHAR                     0x2A37     /**< Heart Rate Measurement characteristic UUID. */
00084 #define BLE_UUID_HID_CONTROL_POINT_CHAR                          0x2A4C     /**< Hid Control Point characteristic UUID. */
00085 #define BLE_UUID_HID_INFORMATION_CHAR                            0x2A4A     /**< Hid Information characteristic UUID. */
00086 #define BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR    0x2A2A     /**< IEEE Regulatory Certification Data List characteristic UUID. */
00087 #define BLE_UUID_INTERMEDIATE_CUFF_PRESSURE_CHAR                 0x2A36     /**< Intermediate Cuff Pressure characteristic UUID. */
00088 #define BLE_UUID_INTERMEDIATE_TEMPERATURE_CHAR                   0x2A1E     /**< Intermediate Temperature characteristic UUID. */
00089 #define BLE_UUID_LOCAL_TIME_INFORMATION_CHAR                     0x2A0F     /**< Local Time Information characteristic UUID. */
00090 #define BLE_UUID_MANUFACTURER_NAME_STRING_CHAR                   0x2A29     /**< Manufacturer Name String characteristic UUID. */
00091 #define BLE_UUID_MEASUREMENT_INTERVAL_CHAR                       0x2A21     /**< Measurement Interval characteristic UUID. */
00092 #define BLE_UUID_MODEL_NUMBER_STRING_CHAR                        0x2A24     /**< Model Number String characteristic UUID. */
00093 #define BLE_UUID_UNREAD_ALERT_CHAR                               0x2A45     /**< Unread Alert characteristic UUID. */
00094 #define BLE_UUID_NEW_ALERT_CHAR                                  0x2A46     /**< New Alert characteristic UUID. */
00095 #define BLE_UUID_PNP_ID_CHAR                                     0x2A50     /**< PNP Id characteristic UUID. */
00096 #define BLE_UUID_PROTOCOL_MODE_CHAR                              0x2A4E     /**< Protocol Mode characteristic UUID. */
00097 #define BLE_UUID_RECORD_ACCESS_CONTROL_POINT_CHAR                0x2A52     /**< Record Access Control Point characteristic UUID. */
00098 #define BLE_UUID_REFERENCE_TIME_INFORMATION_CHAR                 0x2A14     /**< Reference Time Information characteristic UUID. */
00099 #define BLE_UUID_REPORT_CHAR                                     0x2A4D     /**< Report characteristic UUID. */
00100 #define BLE_UUID_REPORT_MAP_CHAR                                 0x2A4B     /**< Report Map characteristic UUID. */
00101 #define BLE_UUID_RINGER_CONTROL_POINT_CHAR                       0x2A40     /**< Ringer Control Point characteristic UUID. */
00102 #define BLE_UUID_RINGER_SETTING_CHAR                             0x2A41     /**< Ringer Setting characteristic UUID. */
00103 #define BLE_UUID_SCAN_INTERVAL_WINDOW_CHAR                       0x2A4F     /**< Scan Interval Window characteristic UUID. */
00104 #define BLE_UUID_SCAN_REFRESH_CHAR                               0x2A31     /**< Scan Refresh characteristic UUID. */
00105 #define BLE_UUID_SERIAL_NUMBER_STRING_CHAR                       0x2A25     /**< Serial Number String characteristic UUID. */
00106 #define BLE_UUID_SOFTWARE_REVISION_STRING_CHAR                   0x2A28     /**< Software Revision String characteristic UUID. */
00107 #define BLE_UUID_SUPPORTED_NEW_ALERT_CATEGORY_CHAR               0x2A47     /**< Supported New Alert Category characteristic UUID. */
00108 #define BLE_UUID_SUPPORTED_UNREAD_ALERT_CATEGORY_CHAR            0x2A48     /**< Supported Unread Alert Category characteristic UUID. */
00109 #define BLE_UUID_SYSTEM_ID_CHAR                                  0x2A23     /**< System Id characteristic UUID. */
00110 #define BLE_UUID_TEMPERATURE_MEASUREMENT_CHAR                    0x2A1C     /**< Temperature Measurement characteristic UUID. */
00111 #define BLE_UUID_TEMPERATURE_TYPE_CHAR                           0x2A1D     /**< Temperature Type characteristic UUID. */
00112 #define BLE_UUID_TIME_ACCURACY_CHAR                              0x2A12     /**< Time Accuracy characteristic UUID. */
00113 #define BLE_UUID_TIME_SOURCE_CHAR                                0x2A13     /**< Time Source characteristic UUID. */
00114 #define BLE_UUID_TIME_UPDATE_CONTROL_POINT_CHAR                  0x2A16     /**< Time Update Control Point characteristic UUID. */
00115 #define BLE_UUID_TIME_UPDATE_STATE_CHAR                          0x2A17     /**< Time Update State characteristic UUID. */
00116 #define BLE_UUID_TIME_WITH_DST_CHAR                              0x2A11     /**< Time With Dst characteristic UUID. */
00117 #define BLE_UUID_TIME_ZONE_CHAR                                  0x2A0E     /**< Time Zone characteristic UUID. */
00118 #define BLE_UUID_TX_POWER_LEVEL_CHAR                             0x2A07     /**< TX Power Level characteristic UUID. */
00119 #define BLE_UUID_CSC_FEATURE_CHAR                                0x2A5C     /**< Cycling Speed and Cadence Feature characteristic UUID. */
00120 #define BLE_UUID_CSC_MEASUREMENT_CHAR                            0x2A5B     /**< Cycling Speed and Cadence Measurement characteristic UUID. */
00121 #define BLE_UUID_RSC_FEATURE_CHAR                                0x2A54     /**< Running Speed and Cadence Feature characteristic UUID. */
00122 #define BLE_UUID_SC_CTRLPT_CHAR                                  0x2A55     /**< Speed and Cadence Control Point UUID. */
00123 #define BLE_UUID_RSC_MEASUREMENT_CHAR                            0x2A53     /**< Running Speed and Cadence Measurement characteristic UUID. */
00124 #define BLE_UUID_SENSOR_LOCATION_CHAR                            0x2A5D     /**< Sensor Location characteristic UUID. */
00125 /** @} */
00126 
00127 /** @defgroup UUID_CHARACTERISTICS descriptors UUID definitions
00128  * @{ */
00129 #define BLE_UUID_EXTERNAL_REPORT_REF_DESCR                       0x2907     /**< External Report Reference descriptor UUID. */
00130 #define BLE_UUID_REPORT_REF_DESCR                                0x2908     /**< Report Reference descriptor UUID. */
00131 /** @} */
00132 
00133 /** @defgroup ALERT_LEVEL_VALUES Definitions for the Alert Level characteristic values
00134  * @{ */
00135 #define BLE_CHAR_ALERT_LEVEL_NO_ALERT                            0x00       /**< No Alert. */
00136 #define BLE_CHAR_ALERT_LEVEL_MILD_ALERT                          0x01       /**< Mild Alert. */
00137 #define BLE_CHAR_ALERT_LEVEL_HIGH_ALERT                          0x02       /**< High Alert. */
00138 /** @} */
00139 
00140 #define BLE_SRV_ENCODED_REPORT_REF_LEN                           2          /**< The length of an encoded Report Reference Descriptor. */
00141 #define BLE_CCCD_VALUE_LEN                                       2          /**< The length of a CCCD value. */
00142 
00143 /**@brief Type definition for error handler function which will be called in case of an error in
00144  *        a service or a service library module. */
00145 typedef void (*ble_srv_error_handler_t) (uint32_t nrf_error);
00146 
00147 /**@brief Value of a Report Reference descriptor. 
00148  *
00149  * @details This is mapping information which maps the parent characteristic to the Report ID(s) and
00150  *          Report Type(s) defined within a Report Map characteristic.
00151  */
00152 typedef struct
00153 {
00154     uint8_t report_id;                                  /**< Non-zero value if these is more than one instance of the same Report Type */
00155     uint8_t report_type;                                /**< Type of Report characteristic (see @ref BLE_SRV_HIDS_REPORT_TYPE) */
00156 } ble_srv_report_ref_t;
00157 
00158 /**@brief UTF-8 string data type.
00159  *
00160  * @note The type can only hold a pointer to the string data (i.e. not the actual data).
00161  */
00162 typedef struct
00163 {
00164     uint16_t  length;                                   /**< String length. */
00165     uint8_t * p_str;                                    /**< String data. */
00166 } ble_srv_utf8_str_t;
00167 
00168 /**@brief Security settings structure.
00169  * @details This structure contains the security options needed during initialization of the
00170  *          service.
00171  */
00172 typedef struct
00173 {
00174     ble_gap_conn_sec_mode_t read_perm;                  /**< Read permissions. */
00175     ble_gap_conn_sec_mode_t write_perm;                 /**< Write permissions. */
00176 } ble_srv_security_mode_t;
00177 
00178 /**@brief Security settings structure.
00179  * @details This structure contains the security options needed during initialization of the
00180  *          service. It can be used when the charecteristics contains cccd.
00181  */
00182 typedef struct
00183 {
00184     ble_gap_conn_sec_mode_t cccd_write_perm;
00185     ble_gap_conn_sec_mode_t read_perm;                  /**< Read permissions. */
00186     ble_gap_conn_sec_mode_t write_perm;                 /**< Write permissions. */
00187 } ble_srv_cccd_security_mode_t;
00188 
00189 /**@brief Function for decoding a CCCD value, and then testing if notification is
00190  *        enabled.
00191  *
00192  * @param[in]   p_encoded_data   Buffer where the encoded CCCD is stored.
00193  *
00194  * @return      TRUE if notification is enabled, FALSE otherwise.
00195  */
00196 static __INLINE bool ble_srv_is_notification_enabled(uint8_t * p_encoded_data)
00197 {
00198     uint16_t cccd_value = uint16_decode(p_encoded_data);
00199     return ((cccd_value & BLE_GATT_HVX_NOTIFICATION) != 0);
00200 }
00201     
00202 /**@brief Function for decoding a CCCD value, and then testing if indication is
00203  *        enabled.
00204  *
00205  * @param[in]   p_encoded_data   Buffer where the encoded CCCD is stored.
00206  *
00207  * @return      TRUE if indication is enabled, FALSE otherwise.
00208  */
00209 static __INLINE bool ble_srv_is_indication_enabled(uint8_t * p_encoded_data)
00210 {
00211     uint16_t cccd_value = uint16_decode(p_encoded_data);
00212     return ((cccd_value & BLE_GATT_HVX_INDICATION) != 0);
00213 }
00214 
00215 /**@brief Function for encoding a Report Reference Descriptor.
00216  *
00217  * @param[in]   p_encoded_buffer  The buffer of the encoded data.
00218  * @param[in]   p_report_ref      Report Reference value to be encoded.
00219  *
00220  * @return      Length of the encoded data.
00221  */
00222 uint8_t ble_srv_report_ref_encode(uint8_t *                    p_encoded_buffer,
00223                                   const ble_srv_report_ref_t * p_report_ref);
00224 
00225 /**@brief Function for making UTF-8 structure refer to an ASCII string.
00226  *
00227  * @param[out]  p_utf8   UTF-8 structure to be set.
00228  * @param[in]   p_ascii  ASCII string to be referred to.
00229  */
00230 void ble_srv_ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, char * p_ascii);
00231 
00232 #endif // BLE_SRV_COMMON_H__
00233 
00234 /** @} */