User Tools

Site Tools


No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:nrf52zephyrbleperipheral

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:s2:aih:lab:nrf52zephyrbleperipheral [2023/12/12 22:30] – [Health thermometer service] ktokarzen:edu:zmitac:s2:aih:lab:nrf52zephyrbleperipheral [2024/01/13 15:23] (current) – [Adding the possibility of reading the characteristic] ktokarz
Line 75: Line 75:
 ); );
 </code> </code>
-BT_GATT_CHARACTERISTIC macro has the following parameters:+BT_GATT_CHARACTERISTIC macro has the following list of parameters 
 +<code> 
 +BT_GATT_CHARACTERISTIC(_uuid,_props,_perm,_read,_write,_user_data) 
 +</code> 
 +The meaning of parameters are as follows:
   * _uuid      – Characteristic attribute uuid.   * _uuid      – Characteristic attribute uuid.
-  * _props     – Characteristic attribute properties. +  * _props     – Characteristic attribute properties (what appears as available for the client: read, write, indicate etc.) 
-  * _perm      – Characteristic Attribute access permissions. +  * _perm      – Characteristic Attribute access permissions (if the client can really read or write the characteristic data.) 
-  * _read      – Characteristic Attribute read callback. +  * _read      – Characteristic Attribute read callback (address of the function called while the client wants to read the data). 
-  * _write     – Characteristic Attribute write callback. +  * _write     – Characteristic Attribute write callback (address of the write function). 
-  * _user_data – Characteristic Attribute user data. +  * _user_data – Characteristic Attribute user data (optional pointer to the characteristic data). 
-The indication mechanism allows the server to send data to the client only when needed (for example on value change). The client must enable indication by setting the descriptor with UUID 0x2902 to 0x0002. This can be done in the nRF Connect application by tapping the two arrows symbol {{arrow_indicate}} (Three arrows symbol visible in BAS service means notification {{arrow_notify}}, which can be enabled by setting the same descriptor to 0x0001).+The indication mechanism allows the server to send data to the client only when needed (for example periodically or on value change). The client must enable indication by setting the descriptor with UUID 0x2902 to 0x0002. This can be done in the nRF Connect application by tapping the two arrows symbol, as in figure {{ref>arrow_indicate}}. Three-arrows symbol visible in BAS service means notification, as in figure {{ref>arrow_notify}}, which can be enabled by setting the same descriptor to 0x0001.
 <figure arrow_indicate> <figure arrow_indicate>
 {{:en:edu:zmitac:s2:aih:lab:arrows_indicate.jpg?50|Indication symbol}} {{:en:edu:zmitac:s2:aih:lab:arrows_indicate.jpg?50|Indication symbol}}
Line 98: Line 102:
   * write the callback function that calls bt_gatt_attr_read() (example function in the following code)   * write the callback function that calls bt_gatt_attr_read() (example function in the following code)
   * register callback function within the BT_GATT_CHARACTERISTIC macro using the "_read" parameter.   * register callback function within the BT_GATT_CHARACTERISTIC macro using the "_read" parameter.
 +Notice that the temperature data sent in the original example by indications is encoded in the following format, which can be automatically decoded by nRF Connect software.
 +<code>
 +bool         temp_in_fahr_units
 +bool         time_stamp_present
 +bool         temp_type_present
 +ieee_float32_t temp_in_celcius
 +ieee_float32_t temp_in_fahr
 +ble_date_time_t time_stamp
 +uint8_t temp_type
 +</code>
 +All boolean flags are set to 0 (the first byte which is sent is 0x00), so the only data is the temperature expressed as a 32-bit floating point number encoded as an 8-bit exponent and 24-bit mantissa (ieee_float32_t). 
 +<note>
 +In the ieee_float32_t format, the exponent is encoded as a signed 8-bit binary 10-based number. The mantissa is encoded as a signed 24-bit binary number. This format is incompatible with single or double-precision formats used in microprocessors, so conversion is required.
 +</note>
 An example of the callback function which simulates the thermometer value can look like this: An example of the callback function which simulates the thermometer value can look like this:
 <code> <code>
Line 104: Line 122:
         uint16_t len, uint16_t offset)         uint16_t len, uint16_t offset)
 { {
- uint8_t lvl8 = 36; + uint8_t temp_buffer[5]; 
- return bt_gatt_attr_read(conn, attr, buf, len, offset, &lvl8, sizeof(lvl8));+ double temp_read; 
 + uint32_t mantissa; 
 + uint8_t exponent; 
 + 
 + temp_read = 36.6; //constant temperature for reading 
 + temp_buffer[0] = 0x00; //flags for temerature data formatting 
 + 
 +        //conversion from double to 4 bytes 
 + //mantissa is 24 bits signed binary 
 + //exponent is 8 bits signed 10-base binary 
 +  
 + mantissa = (uint32_t)(temp_read * 100);       
 + exponent = (uint8_t)-2; 
 + sys_put_le24(mantissa, (uint8_t *)&temp_buffer[1]); 
 + temp_buffer[4] = exponent; 
 + return bt_gatt_attr_read(conn, attr, buf, len, offset, &temp_buffer, sizeof(temp_buffer));
 } }
 </code> </code>
 +
 ===== Tasks to do during the laboratory ===== ===== Tasks to do during the laboratory =====
   - Import the peripheral_ht example.   - Import the peripheral_ht example.
en/edu/zmitac/s2/aih/lab/nrf52zephyrbleperipheral.1702416614.txt.gz · Last modified: by ktokarz