Originally from Donal's blog article. http://mbed.org/users/donalm/code/BLE_Health_Thermometer_Blog/ Changed low freq. clock source from XTAL to IRC.

Dependents:   BLE_Health_Thermometer_IRC BLE_RCBController_micono_test BLE_konashi_PIO_test BLE_ADT7410_TMP102_Sample ... more

Fork of BLE_API_Native_blog by Donal Morrissey

Files at this revision

API Documentation at this revision

Comitter:
ktownsend
Date:
Mon Feb 17 11:09:42 2014 +0000
Parent:
10:eb5f77205fb5
Parent:
9:bbcea9b97272
Child:
12:d42122e07eb4
Commit message:
Merged changes between branches

Changed in this revision

--- a/GattCharacteristic.h	Mon Feb 17 11:06:48 2014 +0000
+++ b/GattCharacteristic.h	Mon Feb 17 11:09:42 2014 +0000
@@ -309,7 +309,7 @@
         uint16_t uuid;              /* Characteristic UUID */
         uint16_t lenMin;            /* Minimum length of the value */
         uint16_t lenMax;            /* Maximum length of the value */
-        uint16_t  handle;
+        uint16_t handle;
         uint8_t  properties;
         
     private:    
--- a/hw/nRF51822n/btle/btle.cpp	Mon Feb 17 11:06:48 2014 +0000
+++ b/hw/nRF51822n/btle/btle.cpp	Mon Feb 17 11:09:42 2014 +0000
@@ -91,16 +91,16 @@
   ble_conn_params_on_ble_evt(p_ble_evt);
 
   /* Custom event handler */
-  static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
   switch (p_ble_evt->header.evt_id)
   {
     case BLE_GAP_EVT_CONNECTED:
-      m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
+      nRF51GattServer::getInstance().m_connectionHandle = p_ble_evt->evt.gap_evt.conn_handle;
       nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED);
       break;
 
     case BLE_GAP_EVT_DISCONNECTED:
       // Since we are not in a connection and have not started advertising, store bonds
+      nRF51GattServer::getInstance().m_connectionHandle = p_ble_evt->evt.gap_evt.conn_handle;
       ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store() );
       nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED);
       break;
@@ -117,7 +117,7 @@
         sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE    ;
         sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE    ;
 
-        ASSERT_STATUS_RET_VOID ( sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_params) );
+        ASSERT_STATUS_RET_VOID ( sd_ble_gap_sec_params_reply(nRF51GattServer::getInstance().m_connectionHandle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_params) );
       }
       break;
 
--- a/hw/nRF51822n/nRF51822n.h	Mon Feb 17 11:06:48 2014 +0000
+++ b/hw/nRF51822n/nRF51822n.h	Mon Feb 17 11:09:42 2014 +0000
@@ -40,7 +40,7 @@
         nRF51822n(void);
         virtual ~nRF51822n(void);
 
-    		virtual Gap&        getGap()        { return nRF51Gap::getInstance(); };
+    	virtual Gap&        getGap()        { return nRF51Gap::getInstance(); };
         virtual GattServer& getGattServer() { return nRF51GattServer::getInstance(); };
         virtual ble_error_t init(void);
         virtual ble_error_t reset(void);
--- a/hw/nRF51822n/nRF51GattServer.cpp	Mon Feb 17 11:06:48 2014 +0000
+++ b/hw/nRF51822n/nRF51GattServer.cpp	Mon Feb 17 11:09:42 2014 +0000
@@ -71,7 +71,7 @@
         
       /* Update the characteristic handle */
       p_char->handle = characteristicCount;
-      characteristicCount++;
+      p_characteristics[characteristicCount++] = p_char;
     }
 
     serviceCount++;
@@ -138,8 +138,31 @@
 /**************************************************************************/
 ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len)
 {
-  /* ToDo: Handle Notify/Indicate */
-  ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE );
+  if ((p_characteristics[charHandle]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
+      (m_connectionHandle != BLE_CONN_HANDLE_INVALID) )
+  {
+    /* HVX update for the characteristic value */
+    ble_gatts_hvx_params_t hvx_params;
+
+    hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle;
+    hvx_params.type   = (p_characteristics[charHandle]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)  ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
+    hvx_params.offset = 0;
+    hvx_params.p_data = buffer;
+    hvx_params.p_len  = &len;
+
+    error_t error = (error_t) sd_ble_gatts_hvx(m_connectionHandle, &hvx_params);
+
+    /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */
+    if ( (error != ERROR_NONE                      ) && (error != ERROR_INVALID_STATE) &&
+         (error != ERROR_BLE_NO_TX_BUFFERS         ) && (error != ERROR_BUSY         ) &&
+         (error != ERROR_BLEGATTS_SYS_ATTR_MISSING ) )
+    {
+      ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE );
+    }
+  } else
+  {
+    ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE );
+  }
     
   return BLE_ERROR_NONE;
 }
--- a/hw/nRF51822n/nRF51GattServer.h	Mon Feb 17 11:06:48 2014 +0000
+++ b/hw/nRF51822n/nRF51GattServer.h	Mon Feb 17 11:09:42 2014 +0000
@@ -33,7 +33,7 @@
 class nRF51GattServer : public GattServer
 {        
     public:
-				static nRF51GattServer& getInstance()
+        static nRF51GattServer& getInstance()
         {
             static nRF51GattServer m_instance;
             return m_instance;
@@ -46,13 +46,17 @@
         
         /* nRF51 Functions */
         void eventCallback(void);
+        
+        uint16_t m_connectionHandle; // TODO move to private
 
     private:
-        nRF51GattServer() { serviceCount = 0; characteristicCount = 0; };
+        GattCharacteristic* p_characteristics[BLE_TOTAL_CHARACTERISTICS];
+        ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS];
+
+        nRF51GattServer() { serviceCount = 0; characteristicCount = 0; m_connectionHandle = BLE_CONN_HANDLE_INVALID; };
+
         nRF51GattServer(nRF51GattServer const&);
         void operator=(nRF51GattServer const&);
-					
-        ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS];
 };
 
 #endif