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

lls.c « Src « svc « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ea8b3c61dbeaa4332efbc6f05dc621e9eaea8bc (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
/**
 ******************************************************************************
 * @file    lls.c
 * @author  MCD Application Team
 * @brief   Link Loss Service
 ******************************************************************************
 * @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 "common_blesvc.h"


/* Private typedef -----------------------------------------------------------*/
typedef struct
{
  uint16_t SvcHdle;                  /**< Service handle */
  uint16_t AlertLevelCharHdle;       /**< Characteristic handle */
}LLS_Context_t;


/* Private defines -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/**
 * START of Section BLE_DRIVER_CONTEXT
 */

PLACE_IN_SECTION("BLE_DRIVER_CONTEXT") static LLS_Context_t LLS_Context;

/**
 * END of Section BLE_DRIVER_CONTEXT
 */


/* Private function prototypes -----------------------------------------------*/
static SVCCTL_EvtAckStatus_t LLS_Event_Handler(void *pckt);


/* 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 LLS_Event_Handler(void *Event)
{
  SVCCTL_EvtAckStatus_t return_value;
  hci_event_pckt *event_pckt;
  evt_blecore_aci *blecore_evt;
  aci_gatt_attribute_modified_event_rp0    * attribute_modified;
  LLS_App_Notification_evt_t Notification;

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

  switch(event_pckt->evt)
  {
    case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
    {
      blecore_evt = (evt_blecore_aci*)event_pckt->data;
      switch(blecore_evt->ecode)
      {
        case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE:
        {
          attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
          if(attribute_modified->Attr_Handle == (LLS_Context.AlertLevelCharHdle + 1))
          {
            return_value = SVCCTL_EvtAckFlowEnable;
            if(attribute_modified->Attr_Data[0] == 0)
            {
              Notification.LLS_Evt_Opcode = LLS_NO_ALERT_EVT;
              LLS_App_Notification(&Notification);
            }
            else if(attribute_modified->Attr_Data[0] == 1)
            {
              Notification.LLS_Evt_Opcode = LLS_MID_ALERT_EVT;
              LLS_App_Notification(&Notification);
            }
            else if(attribute_modified->Attr_Data[0] == 2)
            {
              Notification.LLS_Evt_Opcode = LLS_HIGH_ALERT_EVT;
              LLS_App_Notification(&Notification);
            }
          }
        }
        break;

        default:
          break;
      }
    }
    break; /* HCI_HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE_SPECIFIC */

    default:
      break;
  }

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


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

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void LLS_Init(void)
{
  uint16_t uuid;
  tBleStatus hciCmdResult;

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

  /**
   *  Add Link Loss Service
   *
   * Max_Attribute_Records = 2*no_of_char + 1
   * service_max_attribute_record = 1 for Link Loss service +
   *                                2 for alert level characteristic +
   */
  uuid = LINK_LOSS_SERVICE_UUID;
  hciCmdResult = aci_gatt_add_service(UUID_TYPE_16,
                                   (Service_UUID_t *) &uuid,
                                   PRIMARY_SERVICE,
                                   3,
                                   &(LLS_Context.SvcHdle));

  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_LLS_MSG ("Link Loss Service (LLS) is added Successfully %04X\n", 
                 LLS_Context.SvcHdle);
  }
  else
  {
    BLE_DBG_LLS_MSG ("FAILED to add Link Loss Service (LLS), Error: %02X !!\n", 
                 hciCmdResult);
  }

  /**
   *  Add Alert Level Characteristic
   */
  uuid = LINK_LOSS_ALERT_LEVEL_CHARACTERISTIC_UUID;
  hciCmdResult = aci_gatt_add_char(LLS_Context.SvcHdle,
                                   UUID_TYPE_16,
                                   (Char_UUID_t *) &uuid ,
                                   1,
                                   CHAR_PROP_READ | CHAR_PROP_WRITE,
                                   ATTR_PERMISSION_NONE,
                                   GATT_NOTIFY_ATTRIBUTE_WRITE,
                                   MIN_ENCRY_KEY_SIZE,
                                   CHAR_VALUE_LEN_CONSTANT,
                                   &(LLS_Context.AlertLevelCharHdle));

  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_LLS_MSG ("Alert Level Characteristic Added Successfully  %04X \n", 
                 LLS_Context.AlertLevelCharHdle);
  }
  else
  {
    BLE_DBG_LLS_MSG ("FAILED to add Alert Level Characteristic, Error: %02X !!\n", 
                 hciCmdResult);
  }

  return;
}


/**
 * @brief  Characteristic update
 * @param  UUID: UUID of the characteristic
 * @retval AlertLevelValue: The new value to be written
 */
tBleStatus LLS_Update_Char(uint16_t UUID, uint8_t *pPayload)
{
  tBleStatus return_value = BLE_STATUS_FAILED;

  if(UUID == LINK_LOSS_ALERT_LEVEL_CHARACTERISTIC_UUID)
  {
    return_value = aci_gatt_update_char_value(LLS_Context.SvcHdle,
                                              LLS_Context.AlertLevelCharHdle,
                                              0, /* charValOffset */
                                              1, /* charValueLen */
                                              (uint8_t *) pPayload);
  }
  
  return return_value;
}/* end LLS_Update_Char() */


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