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

app_lpm_transmit_mac_802_15_4.c « app « STM32_WPAN « Mac_802_15_4_LPM_Periodic_Transmit « Mac_802_15_4 « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f5cd8c846caabc7b32f0bac6af9af41202f2a6ce (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
 ******************************************************************************
 * @file    app_lpm_transmit_mac_802_15_4.c
 * @author  MCD Application Connectivity Team
 * @brief   Application implementing periodic data transmission using the
 *          Proxy MAC interfacing 802.15.4 RF core.
 ******************************************************************************
 * @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 "app_common.h"
#include "stm_queue.h"
#include "app_conf.h"
#include "utilities_common.h"
#include "app_entry.h"
#include "main.h"

#include "app_lpm_transmit_mac_802_15_4.h"
#include "802_15_4_mac_sap.h"
#include "app_lpm_transmit_mac_802_15_4_process.h"


#include "shci.h"
#include "stm32_lpm.h"

#define DEMO_CHANNEL 16

#define DATA "DATA From low powered STM32WBx5xx device \0"

/* Private function prototypes -----------------------------------------------*/


uint8_t xorSign( const char * pmessage, uint8_t message_len);


static void APP_LPM_TRANSMIT_MAC_802_15_4_TraceError(char * pMess, uint32_t ErrCode);
static void APP_LPM_TRANSMIT_MAC_802_15_4_Config (void);

static uint8_t rfBuffer[256];


static uint16_t     g_panId             = 0x1AAA;
static uint8_t      g_dataHandle        = 0x02;
static uint16_t     g_shortAddr         = 0xABCD;

static uint16_t     g_broadcastAddr     = 0xFFFF; /* Broadcast Addr */
static uint8_t      g_channel           = DEMO_CHANNEL;

MAC_callbacks_t macCbConfig ;
MAC_associateCnf_t g_MAC_associateCnf;



__IO ITStatus CertifOutputPeripheralReady = SET;


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

void APP_LPM_TRANSMIT_MAC_802_15_4_Init( APP_MAC_802_15_4_InitMode_t InitMode, TL_CmdPacket_t* pCmdBuffer)
{
  /* Register cmdbuffer */
  APP_ENTRY_RegisterCmdBuffer(pCmdBuffer);

  /* Init config buffer and call TL_THREAD_Init */
  APP_ENTRY_TL_MAC_802_15_4_Init();

  /* Send MAC_802_15_4 start system cmd to RF Core */
  SHCI_C2_MAC_802_15_4_Init();

  /* Register task */
  /* Create the different tasks */
  UTIL_SEQ_RegTask( 1<<CFG_TASK_MSG_FROM_RF_CORE, UTIL_SEQ_RFU, APP_ENTRY_ProcessMsgFromRFCoreTask);

  UTIL_SEQ_RegTask( 1<<CFG_TASK_RFD, UTIL_SEQ_RFU,APP_LPM_TRANSMIT_MAC_802_15_4_SetupTask);

  /* Configuration MAC 802_15_4 */
  APP_LPM_TRANSMIT_MAC_802_15_4_Config();

  /*Start Main Node - RFD Task*/
  UTIL_SEQ_SetTask( 1<< CFG_TASK_RFD, CFG_SCH_PRIO_0 );

}


void APP_LPM_TRANSMIT_MAC_802_15_4_NodeSrvTask(void)
{
}

void APP_LPM_TRANSMIT_MAC_802_15_4_NodeDataTask(void)
{
  APP_LPM_TRANSMIT_MAC_802_15_4_SendData(DATA);
}


void APP_LPM_TRANSMIT_MAC_802_15_4_SetupTask(void)
{

  MAC_Status_t MacStatus = MAC_ERROR;

  MAC_resetReq_t    ResetReq;
  MAC_setReq_t      SetReq;

  /* Reset RFD Device */
  
  /* Reset MAC */
  memset(&ResetReq,0x00,sizeof(MAC_resetReq_t));
  ResetReq.set_default_PIB = TRUE;

  MacStatus = MAC_MLMEResetReq( &ResetReq );
  if ( MAC_SUCCESS != MacStatus ) {
    return;
  }
  /* Wait for Reset Confirmation */
  UTIL_SEQ_WaitEvt( 1U<< CFG_EVT_DEVICE_RESET_CNF );

  /* Setup device for Silent Start */

  /* Set Device Short Address */
  memset(&SetReq,0x00,sizeof(MAC_setReq_t));
  SetReq.PIB_attribute = g_MAC_SHORT_ADDRESS_c;
  SetReq.PIB_attribute_valuePtr = (uint8_t*)&g_shortAddr;

  MacStatus = MAC_MLMESetReq( &SetReq );
  if ( MAC_SUCCESS != MacStatus ) {
    return;
  }
  UTIL_SEQ_WaitEvt( 1U<< CFG_EVT_SET_CNF );
  
  /* Set PanID Address */
  memset(&SetReq,0x00,sizeof(MAC_setReq_t));
  SetReq.PIB_attribute = g_MAC_PAN_ID_c;
  SetReq.PIB_attribute_valuePtr = (uint8_t*)&g_panId;

  MacStatus = MAC_MLMESetReq( &SetReq );
  if ( MAC_SUCCESS != MacStatus ) {
    return;
  }
  UTIL_SEQ_WaitEvt( 1U<< CFG_EVT_SET_CNF );
  
  /* Setup TX Channel */
  memset(&SetReq,0x00,sizeof(MAC_setReq_t));
  SetReq.PIB_attribute = g_PHY_CURRENT_CHANNEL_c;
  SetReq.PIB_attribute_valuePtr = (uint8_t*)&g_channel;

  MacStatus = MAC_MLMESetReq( &SetReq );
  if ( MAC_SUCCESS != MacStatus ) {
    return;
  }

  UTIL_SEQ_WaitEvt( 1U<< CFG_EVT_SET_CNF );

  /* Register Data emiter node task */
  UTIL_SEQ_RegTask( 1<<CFG_TASK_DATA_NODE, UTIL_SEQ_RFU, APP_LPM_TRANSMIT_MAC_802_15_4_NodeDataTask);

  /* Start LPTimer to trigg the data emiter node task */
  if (HAL_LPTIM_SetOnce_Start_IT(&hlptim1, PERIOD, TIMEOUT) != HAL_OK)
  {
    Error_Handler();
  }
  
  UTIL_LPM_SetStopMode(CFG_INIT_TASK, UTIL_LPM_ENABLE);
}

void APP_LPM_TRANSMIT_MAC_802_15_4_SendData(const char * data)
{
  MAC_Status_t MacStatus = MAC_ERROR;

  MAC_dataReq_t DataReq;
  DataReq.src_addr_mode = g_SHORT_ADDR_MODE_c;
  DataReq.dst_addr_mode = g_SHORT_ADDR_MODE_c;

  memcpy(DataReq.a_dst_PAN_id,&g_panId,0x02);
  memcpy(DataReq.dst_address.a_short_addr,&g_broadcastAddr,0x02);

  DataReq.msdu_handle = g_dataHandle++;
  /* No ACK is required */
  DataReq.ack_Tx =0x00;
  /* GTS not enabled */
  DataReq.GTS_Tx = FALSE;

  memcpy(&rfBuffer,data,strlen(data));

  /* Xor Data to potentialy verified data paylaod on receiver side */
  rfBuffer[strlen(data)] = xorSign(data,strlen(data));


  DataReq.msduPtr = (uint8_t*) rfBuffer;
  DataReq.msdu_length = strlen(data)+1;

  /* No Security @ MAC level */
  DataReq.security_level = 0x00;

  /* Request Data transmission */

  MacStatus = MAC_MCPSDataReq( &DataReq );

  /* Treat Error in case of failure */
  if ( MAC_SUCCESS != MacStatus ) {
    /* To be implemented according to the application */
    return;
  }

  /* Wait for MCPS data Cnf - Cf. Data Cnf Callback */
  UTIL_SEQ_WaitEvt( 1U << CFG_EVT_DATA_DATA_CNF );
  
}
/**
  * @brief  Trace the error or the warning reported.
  * @param  ErrId :
  * @param  ErrCode
  * @retval None
  */

void APP_LPM_TRANSMIT_MAC_802_15_4_Error(uint32_t ErrId, uint32_t ErrCode)
{

  switch(ErrId)
  {
  case ERR_APPLI_REC_MULTI_MSG_FROM_RFCore :
    APP_LPM_TRANSMIT_MAC_802_15_4_TraceError("ERROR : ERR_REC_MULTI_MSG_FROM_RFCore ",ErrCode);
    break;
  case ERR_INTERFACE_IPCC_SEND_ACK_TO_RFCore :
    APP_LPM_TRANSMIT_MAC_802_15_4_TraceError("ERROR : ERR_IPCC_SEND_ACK_TO_RFCore ",ErrCode);
    break;
  default :
    APP_LPM_TRANSMIT_MAC_802_15_4_TraceError("ERROR Unknown ",0);
    break;
  }
}

/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/
static void APP_LPM_TRANSMIT_MAC_802_15_4_Config()
{
    /* Register MAC 802.15.4 callback functions */

  memset(&macCbConfig,0x00,sizeof(MAC_callbacks_t));

  /* Mac Call Back Initialization */
  macCbConfig.mlmeResetCnfCb = APP_MAC_mlmeResetCnfCb;
  macCbConfig.mlmeScanCnfCb = APP_MAC_mlmeScanCnfCb;
  macCbConfig.mlmeAssociateCnfCb = APP_MAC_mlmeAssociateCnfCb;
  macCbConfig.mlmeAssociateIndCb = APP_MAC_mlmeAssociateIndCb;
  macCbConfig.mlmeBeaconNotifyIndCb = APP_MAC_mlmeBeaconNotifyIndCb;
  macCbConfig.mlmeCalibrateCnfCb = APP_MAC_mlmeCalibrateCnfCb;
  macCbConfig.mlmeCommStatusIndCb = APP_MAC_mlmeCommStatusIndCb;
  macCbConfig.mlmeDisassociateCnfCb = APP_MAC_mlmeDisassociateCnfCb;
  macCbConfig.mlmeDisassociateIndCb = APP_MAC_mlmeDisassociateIndCb;
  macCbConfig.mlmeDpsCnfCb = APP_MAC_mlmeDpsCnfCb;
  macCbConfig.mlmeDpsIndCb = APP_MAC_mlmeDpsIndCb;
  macCbConfig.mlmeGetCnfCb = APP_MAC_mlmeGetCnfCb;
  macCbConfig.mlmeGtsCnfCb = APP_MAC_mlmeGtsCnfCb;
  macCbConfig.mlmeGtsIndCb = APP_MAC_mlmeGtsIndCb;
  macCbConfig.mlmeOrphanIndCb = APP_MAC_mlmeOrphanIndCb;
  macCbConfig.mlmePollCnfCb = APP_MAC_mlmePollCnfCb;
  macCbConfig.mlmeRxEnableCnfCb = APP_MAC_mlmeRxEnableCnfCb;
  macCbConfig.mlmeSetCnfCb = APP_MAC_mlmeSetCnfCb;
  macCbConfig.mlmeSoundingCnfCb = APP_MAC_mlmeSoundingCnfCb;
  macCbConfig.mlmeStartCnfCb = APP_MAC_mlmeStartCnfCb;
  macCbConfig.mlmeSyncLossIndCb = APP_MAC_mlmeSyncLossIndCb;
  macCbConfig.mcpsDataIndCb = APP_MAC_mcpsDataIndCb;
  macCbConfig.mcpsDataCnfCb = APP_MAC_mcpsDataCnfCb;
  macCbConfig.mcpsPurgeCnfCb = APP_MAC_mcpsPurgeCnfCb;
  macCbConfig.mlmePollIndCb =  APP_MAC_mlmePollIndCb;
}

/**
  * @brief  Warn the user that an error has occurred.In this case,
  *         the LEDs on the Board will start blinking.
  *
  * @param  pMess  : Message associated to the error.
  * @param  ErrCode: Error code associated to the module (OpenThread or other module if any)
  * @retval None
  */
static void APP_LPM_TRANSMIT_MAC_802_15_4_TraceError(char * pMess, uint32_t ErrCode)
{
  while(1 == 1)
  {
#if (CFG_LED_SUPPORTED == 1U)
    BSP_LED_Toggle(LED1);
    HAL_Delay(500);
    BSP_LED_Toggle(LED2);
    HAL_Delay(500);
    BSP_LED_Toggle(LED3);
#endif
    HAL_Delay(500);
  }
}


/************** APP RFD ********************/

/* Private variables ---------------------------------------------------------*/
/** @defgroup APP RFD private variables
 * @{
 */

/**
 * @}
 */

/* Global variables ----------------------------------------------------------*/
/** @defgroup APP RFD Global variable
 * @{
 */
/**
 * @}
 */

/* Private function prototypes -----------------------------------------------*/
/** @defgroup APP RFD private function prototypes
 * @{
 */

/* USER CODE BEGIN 4 */
/**
  * @brief  Compare match callback in non blocking mode 
  * @param  hlptim : LPTIM handle
  * @retval None
  */
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
  /* Timeout was reached, toggle LED2 */
  #if ( CFG_LED_SUPPORTED == 0x01 ) && ( CFG_LED_TRACE_LPTIMER == 0x01 )
  BSP_LED_Toggle(LED2);
  #endif
  HAL_LPTIM_SetOnce_Stop_IT(&hlptim1);
#if 1
#if 0
  if (HAL_LPTIM_SetOnce_Start_IT(&hlptim1, PERIOD, TIMEOUT) != HAL_OK)
  {
    BSP_LED_Toggle(LED1);
    Error_Handler();
  }
#endif
  // Request Data Transmit on Compare Match callback
  UTIL_SEQ_SetTask(1 << CFG_TASK_DATA_NODE, CFG_SCH_PRIO_0 );
#endif
}
/* USER CODE END 4 */


/**
  * @brief  comptue simple xor signature of the data to transmit
  *
  * @param  pmessage   : Message to sign.
  * @param  message_len: Message Len
  * @retval Message Signature
  */

uint8_t xorSign( const char * pmessage, uint8_t message_len)
{
  uint8_t seed = 0x00;
  for (uint8_t i=0x00;i<message_len;i++)
    seed = (uint8_t)pmessage[i]^seed;
  return seed; 
}

/**
 * @}
 */


/* Private Functions Definition ------------------------------------------------------*/
/** @defgroup APP RFD Private function
 * @{
 */
/**
 * @}
 */

/* Functions Definition ------------------------------------------------------*/
/** @defgroup APP RFD function prototypes
 * @{
 */


/**
 * @}
 */



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