Skip to content

Commit 88e8491

Browse files
author
Sean Lyons
committed
Merge pull request #88 in SA/ble_examples from feature/add_heap_buffer_limit_to_sss_example to simplelink_cc2640r2_sdk-2.20-develop
Squashed commit of the following: commit 93d0978a8d4cfdf71eadf5dc0f30fe118f102bc9 Author: Wennerfeldt <[email protected]> Date: Wed Oct 24 08:48:12 2018 +0200 Added an "alloacte only if enough headroom" helper function that is sued by the streaming service to reserve heap for the BLE appliation/stack.
1 parent 3060164 commit 88e8491

File tree

6 files changed

+158
-21
lines changed

6 files changed

+158
-21
lines changed

examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_client/src/app/simple_serial_socket_client.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Target Device: CC2640R2
1010
1111
******************************************************************************
12-
12+
1313
Copyright (c) 2018, Texas Instruments Incorporated
1414
All rights reserved.
1515
@@ -165,6 +165,9 @@
165165
// UART read buffer size
166166
#define UART_MAX_READ_SIZE (256)
167167

168+
// Minimum heap headroom for BLE application
169+
#define MIN_HEAP_HEADROOM (4000)
170+
168171
// Application states
169172
enum
170173
{
@@ -515,6 +518,10 @@ static void SimpleSerialSocketClient_init(void)
515518
// Setup an initial read
516519
UART_read(uartHandle, uartReadBuffer, UART_MAX_READ_SIZE);
517520

521+
// Make sure to leave at least MIN_HEAP_HEADROOM of heap
522+
// for the BLE stack application to operate.
523+
SimpleStreamClient_setHeadroomLimit(MIN_HEAP_HEADROOM);
524+
518525
// Setup the Central GAPRole Profile. For more information see the GAP section
519526
// in the User's Guide:
520527
// http://software-dl.ti.com/lprf/sdg-latest/html/
@@ -861,7 +868,7 @@ static void SimpleSerialSocketClient_processGATTMsg(gattMsgEvent_t *pMsg)
861868
if (pMsg->method == ATT_HANDLE_VALUE_NOTI)
862869
{
863870
uartMsg_t *newMsg;
864-
newMsg = ICall_malloc(sizeof(uartMsg_t) + pMsg->msg.handleValueNoti.len);
871+
newMsg = SimpleStreamClient_allocateWithHeadroom(sizeof(uartMsg_t) + pMsg->msg.handleValueNoti.len);
865872

866873
if (newMsg) {
867874

examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_client/src/app/simple_stream_profile_client.c

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ CONST uint8_t SimpleStreamServer_DataOutUUID[ATT_UUID_SIZE] =
9292
// Data queue to hold the outgoing data
9393
static List_List streamOutQueue;
9494

95+
static uint16_t heapHeadroom = 0;
96+
9597
/*********************************************************************
9698
* Service Discovery Table
9799
*/
@@ -301,6 +303,57 @@ void SimpleStreamClient_disconnectStream()
301303
SimpleStreamClient_clearQueue();
302304
}
303305

306+
/*********************************************************************
307+
* @fn SimpleStreamClient_setHeadroomLimit
308+
*
309+
* @brief Sets the limit on how much heap that needs to be available
310+
* following a memory allocation.
311+
*
312+
* @param minHeapHeadRoom - Smallest amount of free heap following
313+
* an memory allocation.
314+
*
315+
* @return none
316+
*/
317+
void SimpleStreamClient_setHeadroomLimit(uint16_t minHeapHeadroom)
318+
{
319+
// Store the minimal heap headroom limit
320+
heapHeadroom = minHeapHeadroom;
321+
}
322+
323+
/*********************************************************************
324+
* @fn SimpleStreamClient_allocateWithHeadroom
325+
*
326+
* @brief Checks if there will be enough free heap left following
327+
* a memory allocation. If there is enough heap, it will allocate
328+
* the memory.
329+
*
330+
* @param allocSize - number of bytes to be allocated
331+
*
332+
* @return none
333+
*/
334+
void* SimpleStreamClient_allocateWithHeadroom(uint16_t allocSize)
335+
{
336+
void *allocatedBuffer = NULL;
337+
ICall_heapStats_t stats;
338+
ICall_CSState key;
339+
340+
// Perform this inside a critical section
341+
key = ICall_enterCriticalSection();
342+
343+
// Get the current free heap
344+
ICall_getHeapStats(&stats);
345+
346+
if (((int16_t) allocSize) < ((int16_t)(stats.totalFreeSize - heapHeadroom)))
347+
{
348+
allocatedBuffer = ICall_malloc(allocSize);
349+
}
350+
351+
// Leave the critical section
352+
ICall_leaveCriticalSection(key);
353+
354+
return allocatedBuffer;
355+
}
356+
304357
/*********************************************************************
305358
* @fn SimpleStreamClient_sendData
306359
*
@@ -317,14 +370,14 @@ void SimpleStreamClient_disconnectStream()
317370
*/
318371
bStatus_t SimpleStreamClient_sendData(uint16_t connHandle, void *data, uint16_t len)
319372
{
320-
bStatus_t ret = SUCCESS;
373+
bStatus_t ret = bleMemAllocError;
321374
SimpleStreamNode_t* newNode;
322375

323376
// Reject if service is not yet discovered
324377
if (NULL != streamServiceHandle.chars[0].handle)
325378
{
326379
// Store the data into the queue
327-
newNode = (SimpleStreamNode_t*) ICall_malloc(sizeof(SimpleStreamNode_t) + len);
380+
newNode = (SimpleStreamNode_t*) SimpleStreamClient_allocateWithHeadroom(sizeof(SimpleStreamNode_t) + len);
328381
if (newNode != NULL)
329382
{
330383
newNode->connHandle = connHandle;
@@ -344,10 +397,6 @@ bStatus_t SimpleStreamClient_sendData(uint16_t connHandle, void *data, uint16_t
344397
ICall_free(newNode);
345398
}
346399
}
347-
else
348-
{
349-
ret = bleMemAllocError;
350-
}
351400
}
352401
else
353402
{

examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_client/src/app/simple_stream_profile_client.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ extern void SimpleStreamClient_disconnectStream();
118118
* and CCCD handle.
119119
*/
120120
extern bStatus_t SimpleStreamClient_enableNotifications(uint16_t connHandle);
121+
122+
/*
123+
* SimpleStreamClient_setHeadroomLimit - Sets the limit on how much heap that needs to be available
124+
* following a memory allocation.
125+
*/
126+
extern void SimpleStreamClient_setHeadroomLimit(uint16_t minHeapHeadroom);
127+
128+
/*
129+
* SimpleStreamClient_allocateWithHeadroom - Checks if there will be enough free heap left
130+
* following a memory allocation. If there is
131+
* enough heap, it will allocate the memory.
132+
*/
133+
extern void* SimpleStreamClient_allocateWithHeadroom(uint16_t allocSize);
121134
/*********************************************************************
122135
*********************************************************************/
123136

examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_server/src/app/simple_serial_socket_server.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Target Device: CC2640R2
1010
1111
******************************************************************************
12-
12+
1313
Copyright (c) 2018, Texas Instruments Incorporated
1414
All rights reserved.
1515
@@ -139,6 +139,9 @@
139139
// UART read buffer size
140140
#define UART_MAX_READ_SIZE (256)
141141

142+
// Minimum heap headroom for BLE application
143+
#define MIN_HEAP_HEADROOM (4000)
144+
142145
/*********************************************************************
143146
* TYPEDEFS
144147
*/
@@ -435,7 +438,7 @@ static void uartWriteCallback(UART_Handle handle, void *txBuf, size_t size)
435438
// Free the last printed buffer
436439
if (NULL != uartCurrentMsg)
437440
{
438-
free(uartCurrentMsg);
441+
ICall_free(uartCurrentMsg);
439442
uartCurrentMsg = NULL;
440443
}
441444

@@ -491,7 +494,7 @@ static void SimpleStreamServer_incomingDataCB(uint16_t connHandle,
491494
{
492495
// Try to allocate and store the data to our UART write queue
493496
uartMsg_t *newMsg;
494-
newMsg = malloc(sizeof(uartMsg_t) + len);
497+
newMsg = SimpleStreamServer_allocateWithHeadroom(sizeof(uartMsg_t) + len);
495498

496499
if (newMsg)
497500
{
@@ -667,6 +670,9 @@ static void SimpleSerialSocketServer_init(void)
667670
// Initialize Simple data stream service
668671
SimpleStreamServer_AddService(GATT_ALL_SERVICES);
669672
SimpleStreamServer_RegisterAppCBs(&SimpleStreamServer_SimpleStreamServerProfileCBs);
673+
// Make sure to leave at least MIN_HEAP_HEADROOM of heap
674+
// for the BLE stack application to operate.
675+
SimpleStreamServer_setHeadroomLimit(MIN_HEAP_HEADROOM);
670676

671677
// Register to connection events
672678
SimpleSerialSocketServer_RegisterToAllConnectionEvent(FOR_STREAM);
@@ -1027,13 +1033,13 @@ static void SimpleSerialSocketServer_processAppMsg(ssssEvt_t *pMsg)
10271033
}
10281034

10291035
// Connection event
1030-
case SSSS_CONN_EVT:
1036+
case SSSS_CONN_EVT:
10311037
{
10321038
SimpleSerialSocketServer_processConnEvt((Gap_ConnEventRpt_t *)(pMsg->pData));
10331039

10341040
ICall_free(pMsg->pData);
10351041
break;
1036-
}
1042+
}
10371043

10381044
default:
10391045
// Do nothing.
@@ -1201,5 +1207,6 @@ static uint8_t SimpleSerialSocketServer_enqueueMsg(uint8_t event, uint8_t state,
12011207

12021208
return FALSE;
12031209
}
1210+
12041211
/*********************************************************************
12051212
*********************************************************************/

examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_server/src/app/simple_stream_profile_server.c

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ static SimpleStreamServerCBs_t *pAppCBs = NULL;
9595
// Data queue to hold the outgoing data
9696
static List_List streamOutQueue;
9797

98+
static uint16_t heapHeadroom = 0;
99+
98100
/*********************************************************************
99101
* Profile Attributes - variables
100102
*/
@@ -526,6 +528,57 @@ void SimpleStreamServer_disconnectStream()
526528
SimpleStreamServer_clearQueue();
527529
}
528530

531+
/*********************************************************************
532+
* @fn SimpleStreamServer_setHeadroomLimit
533+
*
534+
* @brief Sets the limit on how much heap that needs to be available
535+
* following a memory allocation.
536+
*
537+
* @param minHeapHeadRoom - Smallest amount of free heap following
538+
* an memory allocation.
539+
*
540+
* @return none
541+
*/
542+
void SimpleStreamServer_setHeadroomLimit(uint16_t minHeapHeadroom)
543+
{
544+
// Store the minimal heap headroom limit
545+
heapHeadroom = minHeapHeadroom;
546+
}
547+
548+
/*********************************************************************
549+
* @fn SimpleStreamServer_allocateWithHeadroom
550+
*
551+
* @brief Checks if there will be enough free heap left following
552+
* a memory allocation. If there is enough heap, it will allocate
553+
* the memory.
554+
*
555+
* @param allocSize - number of bytes to be allocated
556+
*
557+
* @return none
558+
*/
559+
void* SimpleStreamServer_allocateWithHeadroom(uint16_t allocSize)
560+
{
561+
void *allocatedBuffer = NULL;
562+
ICall_heapStats_t stats;
563+
ICall_CSState key;
564+
565+
// Perform this inside a critical section
566+
key = ICall_enterCriticalSection();
567+
568+
// Get the current free heap
569+
ICall_getHeapStats(&stats);
570+
571+
if (((int16_t) allocSize) < ((int16_t)(stats.totalFreeSize - heapHeadroom)))
572+
{
573+
allocatedBuffer = ICall_malloc(allocSize);
574+
}
575+
576+
// Leave the critical section
577+
ICall_leaveCriticalSection(key);
578+
579+
return allocatedBuffer;
580+
}
581+
529582
/*********************************************************************
530583
* @fn SimpleStreamServer_sendData
531584
*
@@ -542,12 +595,11 @@ void SimpleStreamServer_disconnectStream()
542595
*/
543596
bStatus_t SimpleStreamServer_sendData(uint16_t connHandle, void *data, uint16_t len)
544597
{
545-
bStatus_t ret = SUCCESS;
598+
bStatus_t ret = bleMemAllocError;
546599
SimpleStreamNode_t* newNode;
547600

548-
549601
// Store the data into the queue
550-
newNode = (SimpleStreamNode_t*) ICall_malloc(sizeof(SimpleStreamNode_t) + len);
602+
newNode = (SimpleStreamNode_t*) SimpleStreamServer_allocateWithHeadroom(sizeof(SimpleStreamNode_t) + len);
551603
if (newNode != NULL)
552604
{
553605
newNode->connHandle = connHandle;
@@ -567,10 +619,6 @@ bStatus_t SimpleStreamServer_sendData(uint16_t connHandle, void *data, uint16_t
567619
ICall_free(newNode);
568620
}
569621
}
570-
else
571-
{
572-
ret = bleMemAllocError;
573-
}
574622

575623
return ret;
576624
}

examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_server/src/app/simple_stream_profile_server.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ typedef struct
117117
* GATT attributes with the GATT server.
118118
*
119119
*/
120-
extern bStatus_t SimpleStreamServer_AddService( uint32_t rspTaskId);
120+
extern bStatus_t SimpleStreamServer_AddService( uint32_t rspTaskId );
121121

122122
/*
123123
* SimpleStreamServer_RegisterAppCBs - Registers the application callback function.
@@ -147,6 +147,19 @@ extern bStatus_t SimpleStreamServer_processStream();
147147
* Clear and free up the existing outgoing stream queue.
148148
*/
149149
extern void SimpleStreamServer_disconnectStream();
150+
151+
/*
152+
* SimpleStreamServer_setHeadroomLimit - Sets the limit on how much heap that needs to be available
153+
* following a memory allocation.
154+
*/
155+
extern void SimpleStreamServer_setHeadroomLimit(uint16_t minHeapHeadroom);
156+
157+
/*
158+
* SimpleStreamServer_allocateWithHeadroom - Checks if there will be enough free heap left
159+
* following a memory allocation. If there is
160+
* enough heap, it will allocate the memory.
161+
*/
162+
extern void* SimpleStreamServer_allocateWithHeadroom(uint16_t allocSize);
150163
/*********************************************************************
151164
*********************************************************************/
152165

0 commit comments

Comments
 (0)