aboutsummaryrefslogtreecommitdiff
path: root/src/app/google_rcu/profile_module/fms.c
blob: b2ac182367a1d0b706864e57b9a1d2b87ada7cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
*********************************************************************************************************
*               Copyright(c) 2017, Realtek Semiconductor Corporation. All rights reserved.
**********************************************************************************************************
* @file     fms.c
* @brief    Find Me service source file.
* @details  Interfaces to access Find Me service.
* @author   Chenjie jin
* @date     2022-05-31
* @version  v1.0
*********************************************************************************************************
*/

#include "board.h"
#include "string.h"
#include "trace.h"
#include "gatt.h"
#include "profile_server.h"
#include "fms.h"
#include "fms_service_handle.h"
#include "gap.h"

/********************************************************************************************************
* local static variables defined here, only used in this source file.
********************************************************************************************************/
static uint16_t Fms_Ctl_mode;
static P_FUN_SERVER_GENERAL_CB pfn_fms_cb = NULL;
static const uint8_t GATT_UUID_FMS_SERVICE[16] = {GATT_UUID128_FMS_SERVICE};

/********************************************************************************************************
* global variables
********************************************************************************************************/
/**< @brief  profile/service definition.
*   here is an example of fms service table
*   including Write
*/
const T_ATTRIB_APPL fms_svc_attr_tbl[] =
{
    /*--------------------------FMS Service ---------------------------*/
    /* <<Primary Service>>, .. 0*/
    {
        (ATTRIB_FLAG_VOID | ATTRIB_FLAG_LE),        /* wFlags */
        {                                           /* type_value */
            LO_WORD(GATT_UUID_PRIMARY_SERVICE),
            HI_WORD(GATT_UUID_PRIMARY_SERVICE),
        },
        UUID_128BIT_SIZE,                           /* bValueLen */
        (void *)GATT_UUID_FMS_SERVICE,               /* pValueContext */
        GATT_PERM_READ                              /* wPermissions */
    },

    /* <<Characteristic>> 1 */
    {
        ATTRIB_FLAG_VALUE_INCL,                     /* wFlags */
        {                                           /* type_value */
            LO_WORD(GATT_UUID_CHARACTERISTIC),
            HI_WORD(GATT_UUID_CHARACTERISTIC),
            GATT_CHAR_PROP_READ | GATT_CHAR_PROP_WRITE_NO_RSP,            /* characteristic properties */
            /* characteristic UUID not needed here, is UUID of next attrib. */
        },
        1,                                          /* bValueLen */
        NULL,
        GATT_PERM_READ                              /* wPermissions */
    },

    /* FMS Control 2 */
    {
        ATTRIB_FLAG_VALUE_APPL | ATTRIB_FLAG_UUID_128BIT, /* wFlags */
        {                                           /* type_value */
            GATT_UUID128_FMS_CONTROL
        },
        0,                                          /* variable size */
        NULL,
        (GATT_PERM_READ | GATT_PERM_WRITE)          /* wPermissions */
    },

    /* <<Characteristic>> 3 */
    {
        ATTRIB_FLAG_VALUE_INCL,                     /* wFlags */
        {                                           /* type_value */
            LO_WORD(GATT_UUID_CHARACTERISTIC),
            HI_WORD(GATT_UUID_CHARACTERISTIC),
            GATT_CHAR_PROP_WRITE_NO_RSP | GATT_CHAR_PROP_NOTIFY,    /* characteristic properties */
            /* characteristic UUID not needed here, is UUID of next attrib. */
        },
        1,                                          /* bValueLen */
        NULL,
        GATT_PERM_READ                              /* wPermissions */
    },

    /* FMS Data 4 */
    {
        ATTRIB_FLAG_VALUE_APPL | ATTRIB_FLAG_UUID_128BIT, /* wFlags */
        {                                           /* type_value */
            GATT_UUID128_FMS_DATA
        },
        0,                                          /* variable size */
        NULL,
        (GATT_PERM_READ | GATT_PERM_WRITE)          /* wPermissions */
    },

    /* client characteristic configuration  5 */
    {
        (ATTRIB_FLAG_VALUE_INCL |                   /* flags */
         ATTRIB_FLAG_CCCD_APPL),
        {                                           /* type_value */
            LO_WORD(GATT_UUID_CHAR_CLIENT_CONFIG),
            HI_WORD(GATT_UUID_CHAR_CLIENT_CONFIG),
            /* NOTE: this value has an instantiation for each client, a write to */
            /* this attribute does not modify this default value:                */
            LO_WORD(GATT_CLIENT_CHAR_CONFIG_DEFAULT), /* client char. config. bit field */
            HI_WORD(GATT_CLIENT_CHAR_CONFIG_DEFAULT)
        },
        2,                                          /* bValueLen */
        NULL,
        (GATT_PERM_READ | GATT_PERM_WRITE)          /* permissions */
    }
};

/**
 * @brief read characteristic data from service.
 *
 * @param conn_id           Connection ID.
 * @param service_id        ServiceID to be read.
 * @param attrib_index      Attribute index of getting characteristic data.
 * @param offset            Offset of characteritic to be read.
 * @param p_length          Length of getting characteristic data.
 * @param pp_value          Pointer to pointer of characteristic value to be read.
 * @return T_APP_RESULT
*/
T_APP_RESULT fms_attr_read_cb(uint8_t conn_id, T_SERVER_ID service_id, uint16_t attrib_index,
                              uint16_t offset, uint16_t *p_length, uint8_t **pp_value)
{
    T_APP_RESULT  cause  = APP_RESULT_SUCCESS;
    *p_length = 0;

    PROFILE_PRINT_INFO2("fms_attr_read_cb attrib_index = %d offset %x", attrib_index, offset);

    switch (attrib_index)
    {
    case GATT_SVC_FMS_CONTROL_INDEX:
        {
            /* update Fms_Ctl_mode */
            Fms_Ctl_mode = fms_handle_get_current_mode();

            *pp_value = (uint8_t *)&Fms_Ctl_mode;
            *p_length = 2;
        }
        break;

    default:
        cause = APP_RESULT_ATTR_NOT_FOUND;
        break;
    }

    return cause;
}

/**
 * @brief write characteristic data from service.
 *
 * @param ServiceID          ServiceID to be written.
 * @param iAttribIndex       Attribute index of characteristic.
 * @param wLength            length of value to be written.
 * @param pValue             value to be written.
 * @return Profile procedure result
*/
T_APP_RESULT  fms_attr_write_cb(uint8_t conn_id, uint8_t service_id, uint16_t attrib_index,
                                T_WRITE_TYPE write_type,
                                uint16_t length, uint8_t *p_value, P_FUN_WRITE_IND_POST_PROC *p_write_ind_post_proc)
{
    bool handle = true;
    T_FMS_CALLBACK_DATA callback_data;
    T_APP_RESULT  cause = APP_RESULT_SUCCESS;

    APP_PRINT_INFO1("[fms_attr_write_cb] attrib_index is %d", attrib_index);

    if (GATT_SVC_FMS_CONTROL_INDEX == attrib_index)
    {
        callback_data.msg_type = SERVICE_CALLBACK_TYPE_WRITE_CHAR_VALUE;
        callback_data.msg_data.write_msg.write_type = FMS_WRITE_CHAR_CONTROL;
        callback_data.msg_data.write_msg.write_parameter.report_data.len = length;
        callback_data.msg_data.write_msg.write_parameter.report_data.report = p_value;
    }
    else if (GATT_SVC_FMS_DATA_INDEX == attrib_index)
    {
        callback_data.msg_type = SERVICE_CALLBACK_TYPE_WRITE_CHAR_VALUE;
        callback_data.msg_data.write_msg.write_type = FMS_WRITE_CHAR_DATA;
        callback_data.msg_data.write_msg.write_parameter.report_data.len = length;
        callback_data.msg_data.write_msg.write_parameter.report_data.report = p_value;
    }

    if ((pfn_fms_cb) && (handle == true))
    {
        pfn_fms_cb(service_id, (void *)&callback_data);
    }

    return cause;

}

/**
 * @brief update CCCD bits from stack.
 *
 * @param ServiceId          Service ID.
 * @param Index          Attribute index of characteristic data.
 * @param wCCCBits         CCCD bits from stack.
 * @return None
*/
void fms_cccd_update_cb(uint8_t conn_id, T_SERVER_ID service_id, uint16_t index,
                        uint16_t ccc_bits)
{
    bool handle = true;
    T_FMS_CALLBACK_DATA callback_data;
    callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;

    APP_PRINT_INFO2("[fms_cccd_update_cb] index = %d, ccc_bits %x", index, ccc_bits);
    switch (index)
    {
    case GATT_SVC_FMS_CCCD_INDEX:
        {
            if (ccc_bits & GATT_CLIENT_CHAR_CONFIG_NOTIFY)
            {
                callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;
                callback_data.msg_data.notification_indification_index = FMS_DATA_NOTIFY_ENABLE;
            }
            else
            {
                callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;
                callback_data.msg_data.notification_indification_index = FMS_DATA_NOTIFY_DISABLE;
            }
            break;
        }
    default:
        {
            handle = false;
            break;
        }
    }

    if (pfn_fms_cb && (handle == true))
    {
        /* Notify Application */
        pfn_fms_cb(service_id, (void *)&callback_data);
    }

    return;
}

/**
 * @brief FMS Service Callbacks.
*/
const T_FUN_GATT_SERVICE_CBS fms_cbs =
{
    fms_attr_read_cb,    // Read callback function pointer
    fms_attr_write_cb,   // Write callback function pointer
    fms_cccd_update_cb   // CCCD update callback function pointer
};

/**
  * @brief       Add FMS service to the BLE stack database.
  *
  *
  * @param[in]   p_func  Callback when service attribute was read, write or cccd update.
  * @return Service id generated by the BLE stack: @ref T_SERVER_ID.
  * @retval 0xFF Operation failure.
  * @retval Others Service id assigned by stack.
  *
  * <b>Example usage</b>
  * \code{.c}
     void profile_init()
     {
         server_init(1);
         bas_id = fms_add_service(app_handle_profile_message);
     }
  * \endcode
  */
uint8_t fms_add_service(void *p_func)
{
    uint8_t service_id;
    if (false == server_add_service(&service_id,
                                    (uint8_t *)fms_svc_attr_tbl,
                                    sizeof(fms_svc_attr_tbl),
                                    fms_cbs))
    {
        APP_PRINT_INFO1("fms_add_service: service_id %d", service_id);
        service_id = 0xff;
        return service_id;
    }
    pfn_fms_cb = (P_FUN_SERVER_GENERAL_CB)p_func;
    return service_id;
}