Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dts.c « App « STM32_WPAN « BLE_DataThroughput « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 165b34892043c7fc7cac1bc368a406c87ec7e0b2 (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
/**
 ******************************************************************************
 * @file    dts.c
 * @author  MCD Application Team
 * @brief   Data Transfer Service (Custom)
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/
#include "ble_common.h"
#include "ble.h"
#include "dbg_trace.h"
#include "dts.h"   

#define UUID_128_SUPPORTED 1

#if (UUID_128_SUPPORTED == 1)
#define DT_UUID_LENGTH  UUID_TYPE_128
#else
#define DT_UUID_LENGTH  UUID_TYPE_16
#endif

#if (UUID_128_SUPPORTED == 1)
const uint8_t DT_REQ_CHAR_UUID[16] =
{ 0x19, 0xed, 0x82, 0xae,
  0xed, 0x21, 0x4c, 0x9d,
  0x41, 0x45, 0x22, 0x8e,
  0x81, 0xFE, 0x00, 0x00};
#else
const uint8_t DT_REQ_CHAR_UUID[2] = { 0x81, 0xFE };
#endif

#if (UUID_128_SUPPORTED == 1)
const uint8_t DT_REQ_SERV_UUID[16] =
{ 0x19, 0xed, 0x82, 0xae,
  0xed, 0x21, 0x4c, 0x9d,
  0x41, 0x45, 0x22, 0x8e,
  0x80, 0xFE, 0x00, 0x00};
#else
const uint8_t DT_REQ_SERV_UUID[2] = { 0x80, 0xFE };
#endif

/* Private typedef -----------------------------------------------------------*/
typedef enum
{
  DTS_STM_NOTIFICATION_MASK = (1 << 0),
  DTS_STM_INDICATION_MASK = (1 << 1),
} ClientCharConfMask_t;

typedef struct
{
uint16_t DataTransferSvcHdle; /**< Service handle */
uint16_t DataTransferTxCharHdle; /**< Characteristic handle */
uint16_t DataTransferRxCharHdle; /**< Characteristic handle */
} DataTransferSvcContext_t;

/* Private defines -----------------------------------------------------------*/
#define DATA_TRANSFER_NOTIFICATION_LEN_MAX                                 (20)
#define DATA_TRANSFER_RX_LEN_MAX                                           (255)

/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static tBleStatus TX_Update_Char( DTS_STM_Payload_t *pDataValue );
static SVCCTL_EvtAckStatus_t DTS_Event_Handler( void *pckt );
static DataTransferSvcContext_t aDataTransferContext;

/* Functions Definition ------------------------------------------------------*/
/* Private functions ----------------------------------------------------------*/

/**
 * @brief  Event handler
 * @param  Event: Address of the buffer holding the Event
 * @retval Ack: Return whether the Event has been managed or not
 */
static SVCCTL_EvtAckStatus_t DTS_Event_Handler( void *Event )
{
  SVCCTL_EvtAckStatus_t return_value;
  hci_event_pckt * event_pckt;
  evt_blue_aci * blue_evt;
  aci_gatt_attribute_modified_event_rp0 * attribute_modified;

  DTS_STM_App_Notification_evt_t Notification;

  return_value = SVCCTL_EvtNotAck;
  event_pckt = (hci_event_pckt *) (((hci_uart_pckt*) Event)->data);

  switch (event_pckt->evt)
  {
    case EVT_VENDOR:
    {
      blue_evt = (evt_blue_aci*) event_pckt->data;

      switch (blue_evt->ecode)
      {
        case EVT_BLUE_ATT_EXCHANGE_MTU_RESP:
          break;
        /* server */
        case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED:
        {
          attribute_modified = (aci_gatt_attribute_modified_event_rp0*) blue_evt->data;
          if (attribute_modified->Attr_Handle == (aDataTransferContext.DataTransferTxCharHdle + 2))
          {
            /**
             * Notify to application to start measurement
             */
            if (attribute_modified->Attr_Data[0] & DTS_STM_NOTIFICATION_MASK)
            {
              APP_DBG_MSG("notification enabled\n");
              Notification.Evt_Opcode = DTS_STM__NOTIFICATION_ENABLED;
              DTS_Notification(&Notification);
            }
            else
            {
              APP_DBG_MSG("notification disabled\n");
              Notification.Evt_Opcode = DTS_STM_NOTIFICATION_DISABLED;
              DTS_Notification(&Notification);
            }
          }
        }
          break;
        case EVT_BLUE_GATT_TX_POOL_AVAILABLE:
          Resume_Notification();
          break;
          /* end server */
          /* end GATT event  */
        default:
          break;
      }
    }
      break; /* HCI_EVT_VENDOR_SPECIFIC */

    default:
      break;
  }

  return (return_value);
}/* end SVCCTL_EvtAckStatus_t */

/**
 * @brief  Feature Characteristic update
 * @param  Service_Instance: Instance of the service to which the characteristic belongs
 * @param  pFeatureValue: The address of the new value to be written
 * @retval None
 */
static tBleStatus TX_Update_Char( DTS_STM_Payload_t *pDataValue )
{
  tBleStatus ret;

  /**
   *  Notification Data Transfer Packet
   */
  ret = aci_gatt_update_char_value(
                                   aDataTransferContext.DataTransferSvcHdle,
                                   aDataTransferContext.DataTransferTxCharHdle,
                                   0, /* charValOffset */
                                   pDataValue->Length, /* charValueLen */
                                   (uint8_t *) pDataValue->pPayload);

  return ret;
}/* end TX_Update_Char() */

/* Public functions ----------------------------------------------------------*/

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void DTS_STM_Init( void )
{
  tBleStatus hciCmdResult = BLE_STATUS_FAILED;

  /**
   *	Register the event handler to the BLE controller
   */
  SVCCTL_RegisterSvcHandler(DTS_Event_Handler);

  /* DT service and characteristics */
  hciCmdResult = aci_gatt_add_service(DT_UUID_LENGTH, (Service_UUID_t *) DT_REQ_SERV_UUID,
  PRIMARY_SERVICE,
                                      10, &(aDataTransferContext.DataTransferSvcHdle));
  if (hciCmdResult != 0)
  {
    APP_DBG_MSG("error add service\n");
  }

  /**
   *  Add Data Transfer TX Characteristic
   */
  aci_gatt_add_char(aDataTransferContext.DataTransferSvcHdle,
  DT_UUID_LENGTH,
                    (Char_UUID_t *) DT_REQ_CHAR_UUID,
                    255, /* DATA_TRANSFER_NOTIFICATION_LEN_MAX, */
                    CHAR_PROP_NOTIFY,
                    ATTR_PERMISSION_NONE,
                    GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
                    10, /* encryKeySize */
                    1, /* isVariable */
                    &(aDataTransferContext.DataTransferTxCharHdle));
  if (hciCmdResult != 0)
  {
    APP_DBG_MSG("error add char Tx\n");
  }

  return;
}

/**
 * @brief  Characteristic update
 * @param  UUID: UUID of the characteristic
 * @param  Service_Instance: Instance of the service to which the characteristic belongs
 * 
 */
tBleStatus DTS_STM_UpdateChar( uint16_t UUID , uint8_t *pPayload )
{
  tBleStatus result = BLE_STATUS_INVALID_PARAMS;
  switch (UUID)
  {
    case DATA_TRANSFER_TX_CHAR_UUID:
      result = TX_Update_Char((DTS_STM_Payload_t*) pPayload);
      break;

    default:
      break;
  }
  return result;
}/* end DTS_STM_UpdateChar() */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/