xref: /btstack/src/ble/gatt-service/cycling_power_service_server.h (revision 80e33422a96c028b3a9c308fc4b9b874712dafb4)
19fe70df8SMilanka Ringwald /*
29fe70df8SMilanka Ringwald  * Copyright (C) 2018 BlueKitchen GmbH
39fe70df8SMilanka Ringwald  *
49fe70df8SMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
59fe70df8SMilanka Ringwald  * modification, are permitted provided that the following conditions
69fe70df8SMilanka Ringwald  * are met:
79fe70df8SMilanka Ringwald  *
89fe70df8SMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
99fe70df8SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
109fe70df8SMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
119fe70df8SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
129fe70df8SMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
139fe70df8SMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
149fe70df8SMilanka Ringwald  *    contributors may be used to endorse or promote products derived
159fe70df8SMilanka Ringwald  *    from this software without specific prior written permission.
169fe70df8SMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
179fe70df8SMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
189fe70df8SMilanka Ringwald  *    monetary gain.
199fe70df8SMilanka Ringwald  *
209fe70df8SMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
219fe70df8SMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
229fe70df8SMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
239fe70df8SMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
249fe70df8SMilanka Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
259fe70df8SMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
269fe70df8SMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
279fe70df8SMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
289fe70df8SMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
299fe70df8SMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
309fe70df8SMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319fe70df8SMilanka Ringwald  * SUCH DAMAGE.
329fe70df8SMilanka Ringwald  *
339fe70df8SMilanka Ringwald  * Please inquire about commercial licensing options at
349fe70df8SMilanka Ringwald  * [email protected]
359fe70df8SMilanka Ringwald  *
369fe70df8SMilanka Ringwald  */
37*80e33422SMatthias Ringwald #ifndef CYCLING_POWER_SERVICE_SERVER_H
38*80e33422SMatthias Ringwald #define CYCLING_POWER_SERVICE_SERVER_H
399fe70df8SMilanka Ringwald 
409fe70df8SMilanka Ringwald #include <stdint.h>
419fe70df8SMilanka Ringwald 
429fe70df8SMilanka Ringwald #if defined __cplusplus
439fe70df8SMilanka Ringwald extern "C" {
449fe70df8SMilanka Ringwald #endif
459fe70df8SMilanka Ringwald 
469fe70df8SMilanka Ringwald /**
479fe70df8SMilanka Ringwald  * Implementation of the GATT Cycling Power Service Server
489fe70df8SMilanka Ringwald  */
499fe70df8SMilanka Ringwald 
509fe70df8SMilanka Ringwald // *****************************************************************************
519fe70df8SMilanka Ringwald /* GATT_SERVICE_SERVER_START(cycling_power_service_server){Cycling PowerService}
529fe70df8SMilanka Ringwald  *
539fe70df8SMilanka Ringwald  */
549fe70df8SMilanka Ringwald // *****************************************************************************
559fe70df8SMilanka Ringwald /* GATT_SERVICE_SERVER_END */
569fe70df8SMilanka Ringwald 
579fe70df8SMilanka Ringwald /* API_START */
589fe70df8SMilanka Ringwald #define CYCLING_POWER_MANUFACTURER_SPECIFIC_DATA_MAX_SIZE   16
599fe70df8SMilanka Ringwald 
609fe70df8SMilanka Ringwald typedef enum {
619fe70df8SMilanka Ringwald     CP_PEDAL_POWER_BALANCE_REFERENCE_UNKNOWN = 0,
629fe70df8SMilanka Ringwald     CP_PEDAL_POWER_BALANCE_REFERENCE_LEFT,
639fe70df8SMilanka Ringwald     CP_PEDAL_POWER_BALANCE_REFERENCE_NOT_SUPPORTED
649fe70df8SMilanka Ringwald } cycling_power_pedal_power_balance_reference_t;
659fe70df8SMilanka Ringwald 
669fe70df8SMilanka Ringwald typedef enum {
679fe70df8SMilanka Ringwald     CP_TORQUE_SOURCE_WHEEL = 0,
689fe70df8SMilanka Ringwald     CP_TORQUE_SOURCE_CRANK,
699fe70df8SMilanka Ringwald     CP_TORQUE_SOURCE_NOT_SUPPORTED
709fe70df8SMilanka Ringwald } cycling_power_torque_source_t;
719fe70df8SMilanka Ringwald 
729fe70df8SMilanka Ringwald typedef enum {
739fe70df8SMilanka Ringwald     CP_SENSOR_MEASUREMENT_CONTEXT_FORCE = 0,
749fe70df8SMilanka Ringwald     CP_SENSOR_MEASUREMENT_CONTEXT_TORQUE
759fe70df8SMilanka Ringwald } cycling_power_sensor_measurement_context_t;
769fe70df8SMilanka Ringwald 
779fe70df8SMilanka Ringwald typedef enum {
789fe70df8SMilanka Ringwald     CP_DISTRIBUTED_SYSTEM_UNSPECIFIED = 0,
799fe70df8SMilanka Ringwald     CP_DISTRIBUTED_SYSTEM_NOT_SUPPORTED,
809fe70df8SMilanka Ringwald     CP_DISTRIBUTED_SYSTEM_SUPPORTED
819fe70df8SMilanka Ringwald } cycling_power_distributed_system_t;
829fe70df8SMilanka Ringwald 
839fe70df8SMilanka Ringwald typedef enum {
849fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_PEDAL_POWER_BALANCE_PRESENT = 0,
859fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_PEDAL_POWER_BALANCE_REFERENCE, // 0 - unknown, 1 - left
869fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_ACCUMULATED_TORQUE_PRESENT,
879fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_ACCUMULATED_TORQUE_SOURCE, // 0 - wheel based, 1 - crank based
889fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_WHEEL_REVOLUTION_DATA_PRESENT,
899fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_CRANK_REVOLUTION_DATA_PRESENT,
909fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_EXTREME_FORCE_MAGNITUDES_PRESENT,
919fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_EXTREME_TORQUE_MAGNITUDES_PRESENT,
929fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_EXTREME_ANGLES_PRESENT,
939fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_TOP_DEAD_SPOT_ANGLE_PRESENT,
949fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_BOTTOM_DEAD_SPOT_ANGLE_PRESENT,
959fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_ACCUMULATED_ENERGY_PRESENT,
969fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_OFFSET_COMPENSATION_INDICATOR,
979fe70df8SMilanka Ringwald     CP_MEASUREMENT_FLAG_RESERVED
989fe70df8SMilanka Ringwald } cycling_power_measurement_flag_t;
999fe70df8SMilanka Ringwald 
1009fe70df8SMilanka Ringwald typedef enum {
1019fe70df8SMilanka Ringwald     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_UNKNOWN = 0,
1029fe70df8SMilanka Ringwald     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_TANGENTIAL_COMPONENT,
1039fe70df8SMilanka Ringwald     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_RADIAL_COMPONENT,
1049fe70df8SMilanka Ringwald     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_LATERAL_COMPONENT
1059fe70df8SMilanka Ringwald } cycling_power_instantaneous_measurement_direction_t;
1069fe70df8SMilanka Ringwald 
1079fe70df8SMilanka Ringwald typedef enum {
1089fe70df8SMilanka Ringwald     CP_VECTOR_FLAG_CRANK_REVOLUTION_DATA_PRESENT = 0,
1099fe70df8SMilanka Ringwald     CP_VECTOR_FLAG_FIRST_CRANK_MEASUREMENT_ANGLE_PRESENT,
1109fe70df8SMilanka Ringwald     CP_VECTOR_FLAG_INSTANTANEOUS_FORCE_MAGNITUDE_ARRAY_PRESENT,
1119fe70df8SMilanka Ringwald     CP_VECTOR_FLAG_INSTANTANEOUS_TORQUE_MAGNITUDE_ARRAY_PRESENT,
1129fe70df8SMilanka Ringwald     CP_VECTOR_FLAG_INSTANTANEOUS_MEASUREMENT_DIRECTION = 4, // 2 bit
1139fe70df8SMilanka Ringwald     CP_VECTOR_FLAG_RESERVED = 6
1149fe70df8SMilanka Ringwald } cycling_power_vector_flag_t;
1159fe70df8SMilanka Ringwald 
1169fe70df8SMilanka Ringwald typedef enum {
1179fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_OTHER,
1189fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_TOP_OF_SHOE,
1199fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_IN_SHOE,
1209fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_HIP,
1219fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_FRONT_WHEEL,
1229fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_LEFT_CRANK,
1239fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_RIGHT_CRANK,
1249fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_LEFT_PEDAL,
1259fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_RIGHT_PEDAL,
1269fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_FRONT_HUB,
1279fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_REAR_DROPOUT,
1289fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_CHAINSTAY,
1299fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_REAR_WHEEL,
1309fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_REAR_HUB,
1319fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_CHEST,
1329fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_SPIDER,
1339fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_CHAIN_RING,
1349fe70df8SMilanka Ringwald     CP_SENSOR_LOCATION_RESERVED
1359fe70df8SMilanka Ringwald } cycling_power_sensor_location_t;
1369fe70df8SMilanka Ringwald 
1379fe70df8SMilanka Ringwald typedef enum {
1389fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_PEDAL_POWER_BALANCE_SUPPORTED = 0,
1399fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_ACCUMULATED_TORQUE_SUPPORTED,
1409fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_WHEEL_REVOLUTION_DATA_SUPPORTED,
1419fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_CRANK_REVOLUTION_DATA_SUPPORTED,
1429fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_EXTREME_MAGNITUDES_SUPPORTED,
1439fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_EXTREME_ANGLES_SUPPORTED,
1449fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_TOP_AND_BOTTOM_DEAD_SPOT_ANGLE_SUPPORTED,
1459fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_ACCUMULATED_ENERGY_SUPPORTED,
1469fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_OFFSET_COMPENSATION_INDICATOR_SUPPORTED,
1479fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_OFFSET_COMPENSATION_SUPPORTED,
1489fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_CYCLING_POWER_MEASUREMENT_CHARACTERISTIC_CONTENT_MASKING_SUPPORTED,
1499fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_MULTIPLE_SENSOR_LOCATIONS_SUPPORTED,
1509fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_CRANK_LENGTH_ADJUSTMENT_SUPPORTED,
1519fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_CHAIN_LENGTH_ADJUSTMENT_SUPPORTED,
1529fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_CHAIN_WEIGHT_ADJUSTMENT_SUPPORTED,
1539fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_SPAN_LENGTH_ADJUSTMENT_SUPPORTED,
1549fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_SENSOR_MEASUREMENT_CONTEXT, // 0-force based, 1-torque based
1559fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_INSTANTANEOUS_MEASUREMENT_DIRECTION_SUPPORTED,
1569fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_FACTORY_CALIBRATION_DATE_SUPPORTED,
1579fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_ENHANCED_OFFSET_COMPENSATION_SUPPORTED,
1589fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_DISTRIBUTED_SYSTEM_SUPPORT = 20,  // 0-unspecified, 1-not for use in distr. system, 2-used in distr. system, 3-reserved
1599fe70df8SMilanka Ringwald     CP_FEATURE_FLAG_RESERVED = 22
1609fe70df8SMilanka Ringwald } cycling_power_feature_flag_t;
1619fe70df8SMilanka Ringwald 
1629fe70df8SMilanka Ringwald typedef enum {
1639fe70df8SMilanka Ringwald     CP_CALIBRATION_STATUS_INCORRECT_CALIBRATION_POSITION = 0x01,
1649fe70df8SMilanka Ringwald     CP_CALIBRATION_STATUS_MANUFACTURER_SPECIFIC_ERROR_FOLLOWS = 0xFF
1659fe70df8SMilanka Ringwald } cycling_power_calibration_status_t;
1669fe70df8SMilanka Ringwald 
1679fe70df8SMilanka Ringwald 
1689fe70df8SMilanka Ringwald /**
1699fe70df8SMilanka Ringwald  * @brief Init Server with ATT DB
1709fe70df8SMilanka Ringwald  */
1719fe70df8SMilanka Ringwald void cycling_power_service_server_init(uint32_t feature_flags,
1729fe70df8SMilanka Ringwald     cycling_power_pedal_power_balance_reference_t reference, cycling_power_torque_source_t torque_source,
1739fe70df8SMilanka Ringwald     cycling_power_sensor_location_t * supported_sensor_locations, uint16_t num_supported_sensor_locations,
1749fe70df8SMilanka Ringwald     cycling_power_sensor_location_t current_sensor_location);
1759fe70df8SMilanka Ringwald /**
1769fe70df8SMilanka Ringwald  * @brief Push update
1779fe70df8SMilanka Ringwald  * @note triggers notifications if subscribed
1789fe70df8SMilanka Ringwald  */
1799fe70df8SMilanka Ringwald void cycling_power_service_server_update_values(void);
1809fe70df8SMilanka Ringwald 
1819fe70df8SMilanka Ringwald void cycling_power_server_enhanced_calibration_done(cycling_power_sensor_measurement_context_t measurement_type,
1829fe70df8SMilanka Ringwald                 uint16_t calibrated_value, uint16_t manufacturer_company_id,
1839fe70df8SMilanka Ringwald                 uint8_t num_manufacturer_specific_data, uint8_t * manufacturer_specific_data);
1849fe70df8SMilanka Ringwald 
1859fe70df8SMilanka Ringwald int cycling_power_get_measurement_adv(uint16_t adv_interval, uint8_t * value, uint16_t max_value_size);
1869fe70df8SMilanka Ringwald /**
1879fe70df8SMilanka Ringwald  * @brief Register callback for the calibration.
1889fe70df8SMilanka Ringwald  * @param callback
1899fe70df8SMilanka Ringwald  */
1909fe70df8SMilanka Ringwald void cycling_power_service_server_packet_handler(btstack_packet_handler_t callback);
1919fe70df8SMilanka Ringwald 
1929fe70df8SMilanka Ringwald void  cycling_power_server_calibration_done(cycling_power_sensor_measurement_context_t measurement_type, uint16_t calibrated_value);
1939fe70df8SMilanka Ringwald 
1949fe70df8SMilanka Ringwald int  cycling_power_service_server_set_factory_calibration_date(gatt_date_time_t date);
1959fe70df8SMilanka Ringwald void cycling_power_service_server_set_sampling_rate(uint8_t sampling_rate_hz);
1969fe70df8SMilanka Ringwald 
1979fe70df8SMilanka Ringwald void cycling_power_service_server_add_torque(int16_t torque_m);
1989fe70df8SMilanka Ringwald void cycling_power_service_server_add_wheel_revolution(int32_t wheel_revolution, uint16_t wheel_event_time_s);
1999fe70df8SMilanka Ringwald void cycling_power_service_server_add_crank_revolution(uint16_t crank_revolution, uint16_t crank_event_time_s);
2009fe70df8SMilanka Ringwald void cycling_power_service_add_energy(uint16_t energy_kJ);
2019fe70df8SMilanka Ringwald 
2029fe70df8SMilanka Ringwald void cycling_power_service_server_set_instantaneous_power(int16_t instantaneous_power_watt);
2039fe70df8SMilanka Ringwald void cycling_power_service_server_set_pedal_power_balance(uint8_t pedal_power_balance_percentage);
2049fe70df8SMilanka Ringwald void cycling_power_service_server_set_force_magnitude(int16_t min_force_magnitude_newton, int16_t max_force_magnitude_newton);
2059fe70df8SMilanka Ringwald void cycling_power_service_server_set_torque_magnitude(int16_t min_torque_magnitude_newton, int16_t max_torque_magnitude_newton);
2069fe70df8SMilanka Ringwald void cycling_power_service_server_set_angle(uint16_t min_angle_deg, uint16_t max_angle_deg);
2079fe70df8SMilanka Ringwald void cycling_power_service_server_set_top_dead_spot_angle(uint16_t top_dead_spot_angle_deg);
2089fe70df8SMilanka Ringwald void cycling_power_service_server_set_bottom_dead_spot_angle(uint16_t bottom_dead_spot_angle_deg);
2099fe70df8SMilanka Ringwald void cycling_power_service_server_set_force_magnitude_values(int force_magnitude_count, int16_t * force_magnitude_newton_array);
2109fe70df8SMilanka Ringwald void cycling_power_service_server_set_torque_magnitude_values(int torque_magnitude_count, int16_t * torque_magnitude_newton_array);
2119fe70df8SMilanka Ringwald void cycling_power_service_server_set_instantaneous_measurement_direction(cycling_power_instantaneous_measurement_direction_t direction);
2129fe70df8SMilanka Ringwald // send only in first packet, ignore during continuation
2139fe70df8SMilanka Ringwald void cycling_power_service_server_set_first_crank_measurement_angle(uint16_t first_crank_measurement_angle_deg);
2149fe70df8SMilanka Ringwald 
2159fe70df8SMilanka Ringwald uint16_t cycling_power_service_measurement_flags(void);
2169fe70df8SMilanka Ringwald uint8_t  cycling_power_service_vector_flags(void);
2179fe70df8SMilanka Ringwald /* API_END */
2189fe70df8SMilanka Ringwald 
2199fe70df8SMilanka Ringwald #if defined __cplusplus
2209fe70df8SMilanka Ringwald }
2219fe70df8SMilanka Ringwald #endif
2229fe70df8SMilanka Ringwald 
2239fe70df8SMilanka Ringwald #endif
2249fe70df8SMilanka Ringwald 
225