|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** @file |
| 8 | + * @brief Weight Scale Service(WSS) |
| 9 | + */ |
| 10 | + |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +#include "esp_ble_conn_mgr.h" |
| 14 | +#include "esp_wss.h" |
| 15 | + |
| 16 | +#define BLE_WSS_MAX_VAL_LEN 20 |
| 17 | + |
| 18 | +typedef struct { |
| 19 | + uint8_t len; |
| 20 | + uint8_t buf[BLE_WSS_MAX_VAL_LEN]; |
| 21 | +} indicate_buf_t; |
| 22 | + |
| 23 | +/* Weight Scale Feature value */ |
| 24 | +static esp_ble_wss_feature_t s_wss_feature = { |
| 25 | + .timestamp = 1, |
| 26 | + .user_id = 1, |
| 27 | + .bmi = 1, |
| 28 | + .weight = 1, |
| 29 | + .w_resolution = 1, |
| 30 | + .height = 1, |
| 31 | + .h_resolution = 1 |
| 32 | +}; |
| 33 | + |
| 34 | +static esp_ble_wss_measurement_t s_wss_measurement; |
| 35 | + |
| 36 | +static void build_wss_ind_buf(esp_ble_wss_measurement_t *in_val, indicate_buf_t *out_buf) |
| 37 | +{ |
| 38 | + uint32_t offset = 0; |
| 39 | + |
| 40 | + /* Add Flag */ |
| 41 | + memcpy(out_buf->buf + offset, &(in_val->flag), sizeof(uint32_t)); |
| 42 | + offset += sizeof(uint32_t); |
| 43 | + |
| 44 | + /* Add weight */ |
| 45 | + memcpy(out_buf->buf + offset, &(in_val->weight), sizeof(uint16_t)); |
| 46 | + offset += sizeof(uint32_t); |
| 47 | + |
| 48 | + if (in_val->flag.time_present == 1) { |
| 49 | + memcpy(out_buf->buf + offset, &(in_val->timestamp), sizeof(in_val->timestamp)); |
| 50 | + offset += sizeof(in_val->timestamp); // timestamp length |
| 51 | + } |
| 52 | + |
| 53 | + if (in_val->flag.user_present == 1) { |
| 54 | + memcpy(out_buf->buf + offset, &(in_val->user_id), sizeof(uint8_t)); |
| 55 | + offset += sizeof(uint8_t); // user id length |
| 56 | + } |
| 57 | + |
| 58 | + if (in_val->flag.bmi_height_present == 1) { |
| 59 | + memcpy(out_buf->buf + offset, &(in_val->bmi), sizeof(uint8_t)); |
| 60 | + offset += sizeof(uint8_t); // bmi length |
| 61 | + |
| 62 | + memcpy(out_buf->buf + offset, &(in_val->height), sizeof(uint16_t)); |
| 63 | + offset += sizeof(uint16_t); // height length |
| 64 | + } |
| 65 | + |
| 66 | + out_buf->len = offset; |
| 67 | + |
| 68 | + return; |
| 69 | +} |
| 70 | + |
| 71 | +esp_err_t esp_ble_wss_get_measurement(esp_ble_wss_measurement_t *out_val) |
| 72 | +{ |
| 73 | + if (!out_val) { |
| 74 | + return ESP_ERR_INVALID_ARG; |
| 75 | + } |
| 76 | + |
| 77 | + memcpy(out_val, &s_wss_measurement, sizeof(esp_ble_wss_measurement_t)); |
| 78 | + |
| 79 | + return ESP_OK; |
| 80 | +} |
| 81 | + |
| 82 | +esp_err_t esp_ble_wss_set_measurement(esp_ble_wss_measurement_t *in_val, bool need_send) |
| 83 | +{ |
| 84 | + if (in_val == NULL) { |
| 85 | + return ESP_ERR_INVALID_ARG; |
| 86 | + } |
| 87 | + |
| 88 | + memcpy(&s_wss_measurement, in_val, sizeof(esp_ble_wss_measurement_t)); |
| 89 | + |
| 90 | + /* Set Weight Scale Feature Bit Masks */ |
| 91 | + memset(&s_wss_feature, 0x0, sizeof(esp_ble_wss_feature_t)); |
| 92 | + s_wss_feature.weight = 1; |
| 93 | + |
| 94 | + if (in_val->flag.measurement_unit == 0x1) { |
| 95 | + s_wss_feature.w_resolution = in_val->weight_resolution; |
| 96 | + s_wss_feature.h_resolution = in_val->height_resolution; |
| 97 | + } |
| 98 | + |
| 99 | + if (in_val->flag.time_present == 0x1) { |
| 100 | + s_wss_feature.timestamp = 1; |
| 101 | + } |
| 102 | + |
| 103 | + if (in_val->flag.user_present == 0x1) { |
| 104 | + s_wss_feature.user_id = 1; |
| 105 | + } |
| 106 | + |
| 107 | + if (in_val->flag.bmi_height_present == 0x1) { |
| 108 | + s_wss_feature.bmi = 1; |
| 109 | + s_wss_feature.height = 1; |
| 110 | + } |
| 111 | + |
| 112 | + indicate_buf_t out_buf; |
| 113 | + |
| 114 | + /* Build Body Composition Measurement value */ |
| 115 | + build_wss_ind_buf(in_val, &out_buf); |
| 116 | + |
| 117 | + esp_ble_conn_data_t conn_data = { |
| 118 | + .type = BLE_CONN_UUID_TYPE_16, |
| 119 | + .uuid = { |
| 120 | + .uuid16 = BLE_WSS_CHR_UUID16_WEIGHT_MEASUREMENT, |
| 121 | + }, |
| 122 | + .data = out_buf.buf, |
| 123 | + .data_len = out_buf.len, |
| 124 | + }; |
| 125 | + |
| 126 | + return esp_ble_conn_write(&conn_data); |
| 127 | +} |
| 128 | + |
| 129 | +static esp_err_t wss_feature_cb(const uint8_t *inbuf, uint16_t inlen, |
| 130 | + uint8_t **outbuf, uint16_t *outlen, void *priv_data) |
| 131 | +{ |
| 132 | + uint8_t len = sizeof(s_wss_feature); |
| 133 | + |
| 134 | + if (inbuf || !outbuf || !outlen) { |
| 135 | + return ESP_ERR_INVALID_ARG; |
| 136 | + } |
| 137 | + |
| 138 | + *outbuf = calloc(1, len); |
| 139 | + if (!(*outbuf)) { |
| 140 | + return ESP_ERR_NO_MEM; |
| 141 | + } |
| 142 | + |
| 143 | + memcpy(*outbuf, &s_wss_feature, len); |
| 144 | + *outlen = len; |
| 145 | + |
| 146 | + return ESP_OK; |
| 147 | +} |
| 148 | + |
| 149 | +static const esp_ble_conn_character_t nu_lookup_table[] = { |
| 150 | + { |
| 151 | + "weight feature", BLE_CONN_UUID_TYPE_16, BLE_CONN_GATT_CHR_READ |
| 152 | + , { BLE_WSS_CHR_UUID16_WEIGHT_FEATURE }, wss_feature_cb |
| 153 | + }, |
| 154 | + { |
| 155 | + "weight measurement", BLE_CONN_UUID_TYPE_16, BLE_CONN_GATT_CHR_INDICATE |
| 156 | + , { BLE_WSS_CHR_UUID16_WEIGHT_MEASUREMENT }, NULL |
| 157 | + }, |
| 158 | +}; |
| 159 | + |
| 160 | +static const esp_ble_conn_svc_t svc = { |
| 161 | + .type = BLE_CONN_UUID_TYPE_16, |
| 162 | + .uuid = { |
| 163 | + .uuid16 = BLE_WSS_UUID16, |
| 164 | + }, |
| 165 | + .nu_lookup_count = sizeof(nu_lookup_table) / sizeof(nu_lookup_table[0]), |
| 166 | + .nu_lookup = (esp_ble_conn_character_t *)nu_lookup_table |
| 167 | +}; |
| 168 | + |
| 169 | +esp_err_t esp_ble_wss_init(void) |
| 170 | +{ |
| 171 | + return esp_ble_conn_add_svc(&svc); |
| 172 | +} |
0 commit comments