xref: /btstack/src/ble/gatt-service/cycling_power_service_server.h (revision 9ea49f801ca9d49a65636d9ce376c8a09ffa18c0)
1 /*
2  * Copyright (C) 2018 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * @title Cycling Power Service Server
40  *
41  */
42 
43 #ifndef CYCLING_POWER_SERVICE_SERVER_H
44 #define CYCLING_POWER_SERVICE_SERVER_H
45 
46 #include <stdint.h>
47 
48 #if defined __cplusplus
49 extern "C" {
50 #endif
51 
52 /**
53  * @text The Cycling Power Service allows to query device's power- and
54  * force-related data and optionally speed- and cadence-related data for
55  * use in sports and fitness applications.
56  *
57  * To use with your application, add `#import <cycling_power_service.gatt>`
58  * to your .gatt file.
59  */
60 
61 /* API_START */
62 #define CYCLING_POWER_MANUFACTURER_SPECIFIC_DATA_MAX_SIZE   16
63 
64 typedef enum {
65     CP_PEDAL_POWER_BALANCE_REFERENCE_UNKNOWN = 0,
66     CP_PEDAL_POWER_BALANCE_REFERENCE_LEFT,
67     CP_PEDAL_POWER_BALANCE_REFERENCE_NOT_SUPPORTED
68 } cycling_power_pedal_power_balance_reference_t;
69 
70 typedef enum {
71     CP_TORQUE_SOURCE_WHEEL = 0,
72     CP_TORQUE_SOURCE_CRANK,
73     CP_TORQUE_SOURCE_NOT_SUPPORTED
74 } cycling_power_torque_source_t;
75 
76 typedef enum {
77     CP_SENSOR_MEASUREMENT_CONTEXT_FORCE = 0,
78     CP_SENSOR_MEASUREMENT_CONTEXT_TORQUE
79 } cycling_power_sensor_measurement_context_t;
80 
81 typedef enum {
82     CP_DISTRIBUTED_SYSTEM_UNSPECIFIED = 0,
83     CP_DISTRIBUTED_SYSTEM_NOT_SUPPORTED,
84     CP_DISTRIBUTED_SYSTEM_SUPPORTED
85 } cycling_power_distributed_system_t;
86 
87 typedef enum {
88     CP_MEASUREMENT_FLAG_PEDAL_POWER_BALANCE_PRESENT = 0,
89     CP_MEASUREMENT_FLAG_PEDAL_POWER_BALANCE_REFERENCE, // 0 - unknown, 1 - left
90     CP_MEASUREMENT_FLAG_ACCUMULATED_TORQUE_PRESENT,
91     CP_MEASUREMENT_FLAG_ACCUMULATED_TORQUE_SOURCE, // 0 - wheel based, 1 - crank based
92     CP_MEASUREMENT_FLAG_WHEEL_REVOLUTION_DATA_PRESENT,
93     CP_MEASUREMENT_FLAG_CRANK_REVOLUTION_DATA_PRESENT,
94     CP_MEASUREMENT_FLAG_EXTREME_FORCE_MAGNITUDES_PRESENT,
95     CP_MEASUREMENT_FLAG_EXTREME_TORQUE_MAGNITUDES_PRESENT,
96     CP_MEASUREMENT_FLAG_EXTREME_ANGLES_PRESENT,
97     CP_MEASUREMENT_FLAG_TOP_DEAD_SPOT_ANGLE_PRESENT,
98     CP_MEASUREMENT_FLAG_BOTTOM_DEAD_SPOT_ANGLE_PRESENT,
99     CP_MEASUREMENT_FLAG_ACCUMULATED_ENERGY_PRESENT,
100     CP_MEASUREMENT_FLAG_OFFSET_COMPENSATION_INDICATOR,
101     CP_MEASUREMENT_FLAG_RESERVED
102 } cycling_power_measurement_flag_t;
103 
104 typedef enum {
105     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_UNKNOWN = 0,
106     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_TANGENTIAL_COMPONENT,
107     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_RADIAL_COMPONENT,
108     CP_INSTANTANEOUS_MEASUREMENT_DIRECTION_LATERAL_COMPONENT
109 } cycling_power_instantaneous_measurement_direction_t;
110 
111 typedef enum {
112     CP_VECTOR_FLAG_CRANK_REVOLUTION_DATA_PRESENT = 0,
113     CP_VECTOR_FLAG_FIRST_CRANK_MEASUREMENT_ANGLE_PRESENT,
114     CP_VECTOR_FLAG_INSTANTANEOUS_FORCE_MAGNITUDE_ARRAY_PRESENT,
115     CP_VECTOR_FLAG_INSTANTANEOUS_TORQUE_MAGNITUDE_ARRAY_PRESENT,
116     CP_VECTOR_FLAG_INSTANTANEOUS_MEASUREMENT_DIRECTION = 4, // 2 bit
117     CP_VECTOR_FLAG_RESERVED = 6
118 } cycling_power_vector_flag_t;
119 
120 typedef enum {
121     CP_SENSOR_LOCATION_OTHER,
122     CP_SENSOR_LOCATION_TOP_OF_SHOE,
123     CP_SENSOR_LOCATION_IN_SHOE,
124     CP_SENSOR_LOCATION_HIP,
125     CP_SENSOR_LOCATION_FRONT_WHEEL,
126     CP_SENSOR_LOCATION_LEFT_CRANK,
127     CP_SENSOR_LOCATION_RIGHT_CRANK,
128     CP_SENSOR_LOCATION_LEFT_PEDAL,
129     CP_SENSOR_LOCATION_RIGHT_PEDAL,
130     CP_SENSOR_LOCATION_FRONT_HUB,
131     CP_SENSOR_LOCATION_REAR_DROPOUT,
132     CP_SENSOR_LOCATION_CHAINSTAY,
133     CP_SENSOR_LOCATION_REAR_WHEEL,
134     CP_SENSOR_LOCATION_REAR_HUB,
135     CP_SENSOR_LOCATION_CHEST,
136     CP_SENSOR_LOCATION_SPIDER,
137     CP_SENSOR_LOCATION_CHAIN_RING,
138     CP_SENSOR_LOCATION_RESERVED
139 } cycling_power_sensor_location_t;
140 
141 typedef enum {
142     CP_FEATURE_FLAG_PEDAL_POWER_BALANCE_SUPPORTED = 0,
143     CP_FEATURE_FLAG_ACCUMULATED_TORQUE_SUPPORTED,
144     CP_FEATURE_FLAG_WHEEL_REVOLUTION_DATA_SUPPORTED,
145     CP_FEATURE_FLAG_CRANK_REVOLUTION_DATA_SUPPORTED,
146     CP_FEATURE_FLAG_EXTREME_MAGNITUDES_SUPPORTED,
147     CP_FEATURE_FLAG_EXTREME_ANGLES_SUPPORTED,
148     CP_FEATURE_FLAG_TOP_AND_BOTTOM_DEAD_SPOT_ANGLE_SUPPORTED,
149     CP_FEATURE_FLAG_ACCUMULATED_ENERGY_SUPPORTED,
150     CP_FEATURE_FLAG_OFFSET_COMPENSATION_INDICATOR_SUPPORTED,
151     CP_FEATURE_FLAG_OFFSET_COMPENSATION_SUPPORTED,
152     CP_FEATURE_FLAG_CYCLING_POWER_MEASUREMENT_CHARACTERISTIC_CONTENT_MASKING_SUPPORTED,
153     CP_FEATURE_FLAG_MULTIPLE_SENSOR_LOCATIONS_SUPPORTED,
154     CP_FEATURE_FLAG_CRANK_LENGTH_ADJUSTMENT_SUPPORTED,
155     CP_FEATURE_FLAG_CHAIN_LENGTH_ADJUSTMENT_SUPPORTED,
156     CP_FEATURE_FLAG_CHAIN_WEIGHT_ADJUSTMENT_SUPPORTED,
157     CP_FEATURE_FLAG_SPAN_LENGTH_ADJUSTMENT_SUPPORTED,
158     CP_FEATURE_FLAG_SENSOR_MEASUREMENT_CONTEXT, // 0-force based, 1-torque based
159     CP_FEATURE_FLAG_INSTANTANEOUS_MEASUREMENT_DIRECTION_SUPPORTED,
160     CP_FEATURE_FLAG_FACTORY_CALIBRATION_DATE_SUPPORTED,
161     CP_FEATURE_FLAG_ENHANCED_OFFSET_COMPENSATION_SUPPORTED,
162     CP_FEATURE_FLAG_DISTRIBUTED_SYSTEM_SUPPORT = 20,  // 0-unspecified, 1-not for use in distr. system, 2-used in distr. system, 3-reserved
163     CP_FEATURE_FLAG_RESERVED = 22
164 } cycling_power_feature_flag_t;
165 
166 typedef enum {
167     CP_CALIBRATION_STATUS_INCORRECT_CALIBRATION_POSITION = 0x01,
168     CP_CALIBRATION_STATUS_MANUFACTURER_SPECIFIC_ERROR_FOLLOWS = 0xFF
169 } cycling_power_calibration_status_t;
170 
171 
172 /**
173  * @brief Init Server with ATT DB
174  */
175 void cycling_power_service_server_init(uint32_t feature_flags,
176     cycling_power_pedal_power_balance_reference_t reference, cycling_power_torque_source_t torque_source,
177     cycling_power_sensor_location_t * supported_sensor_locations, uint16_t num_supported_sensor_locations,
178     cycling_power_sensor_location_t current_sensor_location);
179 /**
180  * @brief Push update
181  * @note triggers notifications if subscribed
182  */
183 void cycling_power_service_server_update_values(void);
184 
185 void cycling_power_server_enhanced_calibration_done(cycling_power_sensor_measurement_context_t measurement_type,
186                 uint16_t calibrated_value, uint16_t manufacturer_company_id,
187                 uint8_t num_manufacturer_specific_data, uint8_t * manufacturer_specific_data);
188 
189 int cycling_power_get_measurement_adv(uint16_t adv_interval, uint8_t * value, uint16_t max_value_size);
190 /**
191  * @brief Register callback for the calibration.
192  * @param callback
193  */
194 void cycling_power_service_server_packet_handler(btstack_packet_handler_t callback);
195 
196 void  cycling_power_server_calibration_done(cycling_power_sensor_measurement_context_t measurement_type, uint16_t calibrated_value);
197 
198 int  cycling_power_service_server_set_factory_calibration_date(gatt_date_time_t date);
199 void cycling_power_service_server_set_sampling_rate(uint8_t sampling_rate_hz);
200 
201 void cycling_power_service_server_add_torque(int16_t torque_m);
202 void cycling_power_service_server_add_wheel_revolution(int32_t wheel_revolution, uint16_t wheel_event_time_s);
203 void cycling_power_service_server_add_crank_revolution(uint16_t crank_revolution, uint16_t crank_event_time_s);
204 void cycling_power_service_add_energy(uint16_t energy_kJ);
205 
206 void cycling_power_service_server_set_instantaneous_power(int16_t instantaneous_power_watt);
207 void cycling_power_service_server_set_pedal_power_balance(uint8_t pedal_power_balance_percentage);
208 void cycling_power_service_server_set_force_magnitude(int16_t min_force_magnitude_newton, int16_t max_force_magnitude_newton);
209 void cycling_power_service_server_set_torque_magnitude(int16_t min_torque_magnitude_newton, int16_t max_torque_magnitude_newton);
210 void cycling_power_service_server_set_angle(uint16_t min_angle_deg, uint16_t max_angle_deg);
211 void cycling_power_service_server_set_top_dead_spot_angle(uint16_t top_dead_spot_angle_deg);
212 void cycling_power_service_server_set_bottom_dead_spot_angle(uint16_t bottom_dead_spot_angle_deg);
213 void cycling_power_service_server_set_force_magnitude_values(int force_magnitude_count, int16_t * force_magnitude_newton_array);
214 void cycling_power_service_server_set_torque_magnitude_values(int torque_magnitude_count, int16_t * torque_magnitude_newton_array);
215 void cycling_power_service_server_set_instantaneous_measurement_direction(cycling_power_instantaneous_measurement_direction_t direction);
216 // send only in first packet, ignore during continuation
217 void cycling_power_service_server_set_first_crank_measurement_angle(uint16_t first_crank_measurement_angle_deg);
218 
219 uint16_t cycling_power_service_measurement_flags(void);
220 uint8_t  cycling_power_service_vector_flags(void);
221 /* API_END */
222 
223 #if defined __cplusplus
224 }
225 #endif
226 
227 #endif
228 
229