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

chat_app.c « App « STM32_WPAN « BLE_LLD_Chat « BLE_LLD « Applications « STM32WB5MM-DK « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bba80c6362cc7a5ba27e0d9835fa7b9ee0e6a39b (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* USER CODE BEGIN Header */
/**
 ******************************************************************************
  * File Name          : chat_app.c
  * Description        : CHAT LLD BLE Application.
  ******************************************************************************
  * @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
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_common.h"
#include "utilities_common.h"
#include "app_entry.h"
#include "dbg_trace.h"
#include "ble_lld.h"
#include "ble_hal.h"
#include "app_ble_lld.h"
#include "tl.h"
#include "shci.h"
#include "stm_logging.h"
#include "stm32_lpm.h"
#include "stm32_seq.h"
#include "gpio_lld.h"
#include "chat_app.h"

/* Private includes -----------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

typedef PACKED_STRUCT
{
  char message[200];
} userPayloadMsg;

// Defines user data format in payload
typedef PACKED_STRUCT
{
  uint8_t type;
  union{
    userPayloadMsg msg;
  } __attribute__((packed)) content;
} userPayload;


/* Private defines -----------------------------------------------------------*/
#define CHANNEL           12            // Radio channel
#define POWER             TX_POW_PLUS_6_DB // Transmit power
#define NET_ID            0xB55A54AA    // Network ID (address) must be the same for the two boards
#define RX_TIMEOUT_US     (5*1000*1000) // max delay radio will listen for a packet
#define RX_ACK_TIMEOUT_US 1000          // Timeout for acknowledge reception

#define PROMPT "BLE LLD > "

enum{
  PAYLOAD_MSG,
  PAYLOAD_ACK,
};

/* Private macros ------------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
static void CHAT_Led(void);
static void CHAT_ToggleEncrypt(void);
static void sendPacketStart(char *text);
static void sendPacketEnd(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size);
static void receivePacketStart(void);
static void receivePacketEnd(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size);

static void uartRxBufferProcess(void);
static void uartRxCallback(char received);

static void radioInit(void);

static uint8_t payloadMsgPrepare(userPayload *payload, char *text);
static bool payloadMsgCheck(userPayload *payload);
static void payloadMsgExtract(userPayload *payload, char *text);
static uint8_t payloadAckPrepare(userPayload *payload);
static bool payloadAckCheck(userPayload *payload);

/* Private variables -----------------------------------------------*/
/* LED */
static bool chatCryptoLed  = false;
static bool chatTxLed      = false;
static bool chatRxLed     = false;


/* Encryption */
static const uint8_t chatcountTx[5]     = {0x00,0x00,0xAF,0x00,0x08};
static const uint8_t chatcountRx[5]    = {0x00,0x00,0xAF,0x00,0x08};
// Key must be kept secret and unique for a given set of devices communicating with each other
static const uint8_t chatenc_key[16]     = {0xBF,0x01,0xFB,0x9D,0x4E,0xF3,0xBC,0x36,0xD8,0x74,0xF5,0x39,0x41,0x38,0x68,0x56};
// IV must be chosen randomly at the beginning of each secure session
static const uint8_t chatencIv[8]       = {0x00,0x00,0xAF,0x00,0x08,0x00,0x00,0x00};

/* Parameters */
static bool chatEncrypt = true;

/* Variables used for UART reception */
static char uartRxChar;
static char uartRxBuf[IPBLE_BLE_LLDANT_MAX_PAYLOAD_SIZE];

/* Functions Definition ------------------------------------------------------*/

void CHAT_APP_Init(void)
{
  /* Check the compatibility with the Coprocessor Wireless Firmware loaded */
  CheckWirelessFirmwareInfo();

  /* Do not allow standby in the application */
  UTIL_LPM_SetOffMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE);
  /* Disable low power mode for now, may be enable later depending on configuration */
  UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE );

  /* Register tasks for event processing */
  UTIL_SEQ_RegTask( 1<<CFG_TASK_HAL_BLE_ENCRYPT , UTIL_SEQ_RFU, CHAT_ToggleEncrypt);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_PROCESS_UART_RX_BUFFER, UTIL_SEQ_RFU, uartRxBufferProcess);

  APP_BLE_LLD_Init();


  radioInit();
  uartWrite("");
  uartWrite("************ ID: 0x%X / channel: %u **************", NET_ID, CHANNEL);
  uartWriteRaw(PROMPT);

  APP_BLE_LLD_uartRxStart(uartRxCallback);
  receivePacketStart();
}

static void radioInit(void)
{
  HAL_BLE_LLD_Init(CFG_HS_STARTUP_TIME, true);
  HAL_BLE_LLD_Configure(POWER, CHANNEL, true, CFG_BACK2BACK_TIME, NET_ID);
  
/* Encryption Parameters */
  if (chatEncrypt) {
    BLE_LLD_SetEncryptFlags(STATE_MACHINE_0, ENABLE);
    BLE_LLD_SetEncryptionAttributes(STATE_MACHINE_0, &chatencIv, &chatenc_key);
    BLE_LLD_SetEncryptionCount(STATE_MACHINE_0, &chatcountTx, &chatcountRx);
    chatCryptoLed = true;
    CHAT_Led();
  }else{
    BLE_LLD_SetEncryptFlags(STATE_MACHINE_0, DISABLE);
    chatCryptoLed = false;
    CHAT_Led();
  }
}

// Stores the received character and schedules its processing
static void uartRxCallback(char received)
{
  uartRxChar = received;
  UTIL_SEQ_SetTask(1U << CFG_TASK_PROCESS_UART_RX_BUFFER, CFG_SCH_PRIO_0);
}

// Processes received character (send packet when line complete)
static void uartRxBufferProcess(void)
{
  static uint8_t uartRxBufIdx = 0;
  if (uartRxChar == '\n' || uartRxChar == '\r'){
    uartRxBuf[uartRxBufIdx] = '\0';
    uartRxBufIdx = 0;
    sendPacketStart(uartRxBuf);
  } else if (uartRxBufIdx < (sizeof(uartRxBuf) - 1)){
    char echo[2];
    uartRxBuf[uartRxBufIdx] = uartRxChar;
    uartRxBufIdx++;
    // Send echo
    echo[0] = uartRxChar;
    echo[1] = '\0';
    uartWriteRaw(echo);
  }else{
  }
}

/* Appli common functions */


/* Appli custom functions */
static void CHAT_Led(void) {
  aPwmLedGsData_TypeDef led_Chat;

  led_Chat[PWM_LED_RED]   = chatTxLed ? PWM_LED_GSDATA_7_0:PWM_LED_GSDATA_OFF;
  led_Chat[PWM_LED_GREEN] = chatRxLed ? PWM_LED_GSDATA_7_0:PWM_LED_GSDATA_OFF;
  led_Chat[PWM_LED_BLUE]  = chatCryptoLed ? PWM_LED_GSDATA_7_0:PWM_LED_GSDATA_OFF;
  LED_On(led_Chat);
}

static void CHAT_ToggleEncrypt(void)
{
  BLE_LLD_StopActivity();
  chatEncrypt = !chatEncrypt;
  // Radio was destroyed by StopActivity
  radioInit();
  uartWrite("");
  uartWrite("************ %s **************", chatEncrypt ? "Encrypted" : "UnEncrypted");
  uartWriteRaw(PROMPT);
  receivePacketStart();
}

static void sendPacketStart(char *text)
{
  userPayload payload;
  uint8_t payloadSize;
  BLE_LLD_StopActivity();
  radioInit();

  payloadSize = payloadMsgPrepare(&payload, uartRxBuf);
  HAL_BLE_LLD_SendPacketWithAck(&payload,
                                payloadSize,
                                RX_ACK_TIMEOUT_US,
                                sendPacketEnd);
}

static void sendPacketEnd(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size)
{
  userPayload *payload = data;

  if (cmd == RX_OK_READY){
    if (! payloadAckCheck(payload)){
    }else{
      uartWrite("");
      uartWriteRaw(PROMPT);
    }
  }else if (cmd != TX_OK_BUSY){
    uartWrite("");
    uartWrite("ERROR: message delivery failed");
    uartWriteRaw(PROMPT);
  }
  if (RADIO_IS_READY(cmd)){
    receivePacketStart();
  }
}

static void receivePacketStart(void)
{
  uint8_t status;
  userPayload payload;
  uint8_t payloadSize;
  payloadSize = payloadAckPrepare(&payload);
  status = HAL_BLE_LLD_ReceivePacketWithAck(&payload,
                                            payloadSize,
                                            RX_TIMEOUT_US,
                                            receivePacketEnd);
  if (SUCCESS_0 != status){
  }
}

static void receivePacketEnd(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size)
{
  char display[sizeof(userPayloadMsg)];
  userPayload *payload = data;
  // Display data as soon as it is received (ACK is still being sent)
  if (cmd == RX_OK_BUSY){
    if (!payloadMsgCheck(payload)){
    }else{
      payloadMsgExtract(payload, display);
      uartWriteRaw(display);
      uartWrite("");
      uartWriteRaw(PROMPT);
    }
  }
  if (RADIO_IS_READY(cmd)){
    receivePacketStart();
  }
}

static uint8_t payloadMsgPrepare(userPayload *payload, char *text)
{
  payload->type = PAYLOAD_MSG;
  strncpy(payload->content.msg.message, uartRxBuf, (sizeof(userPayloadMsg) - 1));
  payload->content.msg.message[sizeof(userPayloadMsg) - 1] = '\0';
  // Be careful to update computed size if structure changes
  return (sizeof(userPayload) - sizeof(userPayloadMsg) +
          strlen(payload->content.msg.message) + 1);
}

static bool payloadMsgCheck(userPayload *payload)
{
  return (payload->type == PAYLOAD_MSG);
}

static void payloadMsgExtract(userPayload *payload, char *text)
{
  strncpy(text, payload->content.msg.message, (sizeof(userPayloadMsg) - 1));
  text[sizeof(userPayloadMsg) - 1] = '\0';
}

static uint8_t payloadAckPrepare(userPayload *payload)
{
  payload->type = PAYLOAD_ACK;
  // Be careful to update computed size if structure changes
  return (sizeof(userPayload) - sizeof(userPayloadMsg));
}

static bool payloadAckCheck(userPayload *payload)
{
  return (payload->type == PAYLOAD_ACK);
}


/* USER CODE END FD */

/* USER CODE BEGIN FD_WRAP_FUNCTIONS */
void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )
{
  switch (GPIO_Pin)
  {
    case BUTTON_USER1_PIN:
      UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_ENCRYPT, CFG_SCH_PRIO_0);
      break;

    case BUTTON_USER2_PIN:
      break;

    default:
      break;
  }
  return;
}

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