[TI] Update Number of LWIP Buffers, Fix ICD GPIO build error (#33053)

* Update Number of LWIP Buffers, Fix ICD GPIO build error
Update memory interface to support realloc, decrease num message buffers
Fix ICD LED Build failures

* Restyled by whitespace

* Restyled by clang-format

* Keep old implementation of realloc for CC3220

---------

Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp b/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp
index 3e33847..6fc9642 100644
--- a/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp
+++ b/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp
@@ -54,12 +54,6 @@
 #define APP_TASK_STACK_SIZE (5000)
 #define APP_TASK_PRIORITY 4
 #define APP_EVENT_QUEUE_SIZE 10
-
-#if (CHIP_CONFIG_ENABLE_ICD_SERVER == 1)
-#define LED_ENABLE 0
-#else
-#define LED_ENABLE 1
-#endif
 #define BUTTON_ENABLE 1
 
 using namespace ::chip;
@@ -68,7 +62,6 @@
 
 static TaskHandle_t sAppTaskHandle;
 static QueueHandle_t sAppEventQueue;
-
 static Button_Handle sAppLeftHandle;
 static Button_Handle sAppRightHandle;
 static DeviceInfoProviderImpl sExampleDeviceInfoProvider;
diff --git a/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp b/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp
index 01a945e..00fd64e 100644
--- a/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp
+++ b/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp
@@ -18,5 +18,7 @@
 
 #include "Globals.h"
 
+#if (LED_ENABLE == 1)
 LED_Handle sAppRedHandle;
 LED_Handle sAppGreenHandle;
+#endif
diff --git a/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h b/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h
index 4cdc0b6..8320b01 100644
--- a/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h
+++ b/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h
@@ -31,5 +31,12 @@
 #ifdef __cplusplus
 }
 #endif
+
+#if (CHIP_CONFIG_ENABLE_ICD_SERVER == 1)
+#define LED_ENABLE 0
+#else
+#define LED_ENABLE 1
+#endif
+
 extern LED_Handle sAppRedHandle;
 extern LED_Handle sAppGreenHandle;
diff --git a/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp b/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp
index bad302b..5b5bb7d 100644
--- a/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp
+++ b/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp
@@ -81,9 +81,10 @@
 
 static TaskHandle_t sAppTaskHandle;
 static QueueHandle_t sAppEventQueue;
-
+#if (LED_ENABLE == 1)
 static LED_Handle sAppRedHandle;
 static LED_Handle sAppGreenHandle;
+#endif
 static Button_Handle sAppLeftHandle;
 static Button_Handle sAppRightHandle;
 static DeviceInfoProviderImpl sExampleDeviceInfoProvider;
diff --git a/examples/lock-app/cc13x4_26x4/src/AppTask.cpp b/examples/lock-app/cc13x4_26x4/src/AppTask.cpp
index ec6a1d0..2a202ce 100644
--- a/examples/lock-app/cc13x4_26x4/src/AppTask.cpp
+++ b/examples/lock-app/cc13x4_26x4/src/AppTask.cpp
@@ -72,9 +72,10 @@
 
 static TaskHandle_t sAppTaskHandle;
 static QueueHandle_t sAppEventQueue;
-
+#if (LED_ENABLE == 1)
 static LED_Handle sAppRedHandle;
 static LED_Handle sAppGreenHandle;
+#endif
 static Button_Handle sAppLeftHandle;
 static Button_Handle sAppRightHandle;
 
diff --git a/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h b/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h
index 795d77b..c408757 100644
--- a/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h
+++ b/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h
@@ -23,6 +23,7 @@
 
 #pragma once
 
+#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 22
 #define OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE 0
 #define OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE 0
 #define OPENTHREAD_CONFIG_DIAG_ENABLE 0
diff --git a/examples/pump-app/cc13x4_26x4/main/AppTask.cpp b/examples/pump-app/cc13x4_26x4/main/AppTask.cpp
index 4450b09..0aaa1c6 100644
--- a/examples/pump-app/cc13x4_26x4/main/AppTask.cpp
+++ b/examples/pump-app/cc13x4_26x4/main/AppTask.cpp
@@ -79,9 +79,10 @@
 
 static TaskHandle_t sAppTaskHandle;
 static QueueHandle_t sAppEventQueue;
-
+#if (LED_ENABLE == 1)
 static LED_Handle sAppRedHandle;
 static LED_Handle sAppGreenHandle;
+#endif
 static Button_Handle sAppLeftHandle;
 static Button_Handle sAppRightHandle;
 
diff --git a/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp b/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp
index 6c84954..fd44a3d 100644
--- a/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp
+++ b/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp
@@ -68,9 +68,10 @@
 
 static TaskHandle_t sAppTaskHandle;
 static QueueHandle_t sAppEventQueue;
-
+#if (LED_ENABLE == 1)
 static LED_Handle sAppRedHandle;
 static LED_Handle sAppGreenHandle;
+#endif
 static Button_Handle sAppLeftHandle;
 static Button_Handle sAppRightHandle;
 
diff --git a/src/lwip/cc13xx_26xx/lwipopts.h b/src/lwip/cc13xx_26xx/lwipopts.h
index 925a06d..d89c33a 100644
--- a/src/lwip/cc13xx_26xx/lwipopts.h
+++ b/src/lwip/cc13xx_26xx/lwipopts.h
@@ -89,7 +89,7 @@
 #define MEMP_SEPARATE_POOLS (1)
 #define LWIP_PBUF_FROM_CUSTOM_POOLS (0)
 #define MEMP_USE_CUSTOM_POOLS (0)
-#define PBUF_POOL_SIZE (6)
+#define PBUF_POOL_SIZE (12)
 #define PBUF_POOL_BUFSIZE (1280)
 #define PBUF_CUSTOM_POOL_IDX_START (MEMP_PBUF_POOL_SMALL)
 #define PBUF_CUSTOM_POOL_IDX_END (MEMP_PBUF_POOL_LARGE)
diff --git a/src/platform/cc13xx_26xx/TI_heap_wrapper.c b/src/platform/cc13xx_26xx/TI_heap_wrapper.c
new file mode 100644
index 0000000..a749884
--- /dev/null
+++ b/src/platform/cc13xx_26xx/TI_heap_wrapper.c
@@ -0,0 +1,75 @@
+#include "bget.h"
+#include <ti/drivers/dpl/HwiP.h>
+#include <ti/drivers/dpl/SwiP.h>
+
+typedef unsigned int dpl_CSState;
+
+typedef union _dpl_cs_state_union_t
+{
+    /** critical section variable as declared in the interface */
+    dpl_CSState state;
+    /** @internal field used to access internal data */
+    struct _dpl_cs_state_aggr_t
+    {
+        /** field to store Swi_disable() return value */
+        uint_least16_t swikey;
+        /** field to store Hwi_disable() return value */
+        uint_least16_t hwikey;
+    } each;
+} dpl_CSStateUnion;
+
+/* This is enter critical section for DPL supported devices */
+dpl_CSState dpl_enterCSImpl(void)
+{
+
+    dpl_CSStateUnion cu;
+    cu.each.swikey = (uint_least16_t) SwiP_disable();
+    cu.each.hwikey = (uint_least16_t) HwiP_disable();
+    return cu.state;
+}
+
+/* This is exit critical section for DPL supported devices */
+void dpl_leaveCSImpl(dpl_CSState key)
+{
+    dpl_CSStateUnion * cu = (dpl_CSStateUnion *) &key;
+    HwiP_restore((uint32_t) cu->each.hwikey);
+    SwiP_restore((uint32_t) cu->each.swikey);
+}
+
+/* Protected allocation */
+void * pvPortMalloc(size_t xWantedSize)
+{
+    void * retVal = NULL;
+
+    dpl_CSState state;
+    state = dpl_enterCSImpl();
+
+    retVal = bget(xWantedSize);
+
+    dpl_leaveCSImpl(state);
+    return retVal;
+}
+
+/* Protected Deallocation */
+void vPortFree(void * pv)
+{
+    dpl_CSState state;
+    state = dpl_enterCSImpl();
+
+    brel(pv);
+
+    dpl_leaveCSImpl(state);
+}
+
+void * pvPortRealloc(void * pv, size_t size)
+{
+    void * retVal = NULL;
+
+    dpl_CSState state;
+    state = dpl_enterCSImpl();
+
+    retVal = bgetr(pv, size);
+
+    dpl_leaveCSImpl(state);
+    return retVal;
+}
diff --git a/src/platform/cc13xx_26xx/TI_heap_wrapper.h b/src/platform/cc13xx_26xx/TI_heap_wrapper.h
new file mode 100644
index 0000000..11df56f
--- /dev/null
+++ b/src/platform/cc13xx_26xx/TI_heap_wrapper.h
@@ -0,0 +1,10 @@
+/* Protected allocation
+ malloc/ICall_heapMalloc  --> ti_heap_wrapper --> bget protected by critical section
+*/
+void * pvPortMalloc(size_t xWantedSize);
+
+/* Protected Deallocation
+ Free/ICall_heapFree --> ti_heap_wrapper --> brel protected by critical section
+ */
+void vPortFree(void * pv);
+void * pvPortRealloc(void * pv, size_t size);
diff --git a/src/platform/cc13xx_26xx/memory.c b/src/platform/cc13xx_26xx/memory.c
new file mode 100644
index 0000000..2bd8536
--- /dev/null
+++ b/src/platform/cc13xx_26xx/memory.c
@@ -0,0 +1,398 @@
+/*
+ * Copyright (c) 2016-2022 Texas Instruments Incorporated - http://www.ti.com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * *  Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * *  Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * *  Neither the name of Texas Instruments Incorporated nor the names of
+ *    its contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+/*
+ *  ======== memory.c ========
+ */
+
+#if defined(__ti__) && !defined(__clang__)
+
+#pragma FUNC_EXT_CALLED(malloc);
+#pragma FUNC_EXT_CALLED(memalign);
+#pragma FUNC_EXT_CALLED(free);
+#pragma FUNC_EXT_CALLED(calloc);
+#pragma FUNC_EXT_CALLED(realloc);
+#pragma FUNC_EXT_CALLED(aligned_alloc);
+
+#define ATTRIBUTE
+
+#elif defined(__IAR_SYSTEMS_ICC__)
+
+#define ATTRIBUTE
+
+#else
+
+#define ATTRIBUTE __attribute__((used))
+
+#endif
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <FreeRTOS.h>
+#include <portmacro.h>
+
+#if defined(__GNUC__) && !defined(__ti__)
+
+#include <reent.h>
+
+#endif
+
+extern void * pvPortRealloc(void * pv, size_t size);
+
+/*
+ * This Header is only needed to support advanced memory services - namely
+ * realloc() and memalign().  If the user doesn't require those features, they
+ * can remove the overhead and save both code and data.
+ */
+#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV)
+/*
+ * Header is a union to make sure that the size is a power of 2.
+ *
+ * On the MSP430 small model (MSP430X), size_t is 2 bytes, which makes
+ * the size of this struct 6 bytes.
+ */
+typedef union Header
+{
+    struct
+    {
+        void * actualBuf;
+        size_t size;
+    } header;
+    int pad[2]; /* 4 words on 28L, 8 bytes on most others */
+} Header;
+#endif
+
+/*
+ *  ======== malloc ========
+ */
+void ATTRIBUTE * malloc(size_t size)
+{
+#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV)
+    Header * packet;
+    size_t allocSize;
+
+    allocSize = size + sizeof(Header);
+
+    /*
+     * If size is very large and allocSize overflows, the result will be
+     * smaller than size. In this case, don't try to allocate.
+     */
+    if ((size == 0) || (allocSize < size))
+    {
+        errno = EINVAL;
+        return (NULL);
+    }
+
+    packet = (Header *) pvPortMalloc(allocSize);
+
+    if (packet == NULL)
+    {
+        errno = ENOMEM;
+        return (NULL);
+    }
+
+    packet->header.actualBuf = (void *) packet;
+    packet->header.size      = allocSize;
+
+    return (packet + 1);
+#else
+    void * packet;
+
+    if (size == 0)
+    {
+        errno = EINVAL;
+        return (NULL);
+    }
+
+    packet = pvPortMalloc(size);
+
+    if (packet == NULL)
+    {
+        errno = ENOMEM;
+        return (NULL);
+    }
+
+    return (packet);
+#endif
+}
+
+/*
+ *  ======== calloc ========
+ */
+void ATTRIBUTE * calloc(size_t nmemb, size_t size)
+{
+    size_t nbytes;
+    void * retval;
+
+    /* guard against divide by zero exception below */
+    if (nmemb == 0)
+    {
+        errno = EINVAL;
+        return (NULL);
+    }
+
+    nbytes = nmemb * size;
+
+    /* return NULL if there's an overflow */
+    if (nmemb && size != (nbytes / nmemb))
+    {
+        errno = EOVERFLOW;
+        return (NULL);
+    }
+
+    retval = malloc(nbytes);
+    if (retval != NULL)
+    {
+        (void) memset(retval, (int) '\0', nbytes);
+    }
+
+    return (retval);
+}
+#ifndef DeviceFamily_CC3220
+/*
+ *  ======== realloc ========
+ */
+void ATTRIBUTE * realloc(void * ptr, size_t size)
+{
+#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV)
+    void * retval;
+    Header * packet;
+    size_t oldSize;
+
+    if (ptr == NULL)
+    {
+        retval = malloc(size);
+    }
+    else if (size == 0)
+    {
+        errno  = EINVAL;
+        retval = NULL;
+    }
+    else
+    {
+        packet = (Header *) ptr - 1;
+        retval = malloc(size);
+        if (retval != NULL)
+        {
+            oldSize = packet->header.size - sizeof(Header);
+            (void) memcpy(retval, ptr, (size < oldSize) ? size : oldSize);
+            free(ptr);
+        }
+    }
+
+    return (retval);
+#else
+    void * packet;
+
+    if (size == 0)
+    {
+        errno = EINVAL;
+        return (NULL);
+    }
+
+    packet = pvPortRealloc(ptr, size);
+
+    if (packet == NULL)
+    {
+        errno = ENOMEM;
+        return (NULL);
+    }
+
+    return (packet);
+#endif
+}
+
+#else
+/*
+ *  ======== realloc ========
+ */
+void ATTRIBUTE * realloc(void * ptr, size_t size)
+{
+#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV)
+    void * retval;
+    Header * packet;
+    size_t oldSize;
+
+    if (ptr == NULL)
+    {
+        retval = malloc(size);
+    }
+    else if (size == 0)
+    {
+        errno  = EINVAL;
+        retval = NULL;
+    }
+    else
+    {
+        packet = (Header *) ptr - 1;
+        retval = malloc(size);
+        if (retval != NULL)
+        {
+            oldSize = packet->header.size - sizeof(Header);
+            (void) memcpy(retval, ptr, (size < oldSize) ? size : oldSize);
+            free(ptr);
+        }
+    }
+
+    return (retval);
+#else
+    /* Unsupported implementation */
+    return (NULL);
+#endif
+}
+#endif
+
+/*
+ *  ======== free ========
+ */
+void ATTRIBUTE free(void * ptr)
+{
+#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV)
+    Header * packet;
+
+    if (ptr != NULL)
+    {
+        packet = ((Header *) ptr) - 1;
+        vPortFree(packet->header.actualBuf);
+    }
+#else
+    vPortFree(ptr);
+#endif
+}
+
+/*
+ *  ======== memalign ========
+ */
+void ATTRIBUTE * memalign(size_t boundary, size_t size)
+{
+#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV)
+    Header * packet;
+    void * tmp;
+
+    /* return NULL if size is 0, or alignment is not a power-of-2 */
+    if (size == 0 || (boundary & (boundary - 1)))
+    {
+        errno = EINVAL;
+        return (NULL);
+    }
+
+    if (boundary < sizeof(Header))
+    {
+        boundary = sizeof(Header);
+    }
+
+    /*
+     * Allocate 'align + size + sizeof(Header)' so that we have room for
+     * the 'packet' and can return an aligned buffer.
+     */
+    tmp = pvPortMalloc(boundary + size + sizeof(Header));
+
+    if (tmp == NULL)
+    {
+        errno = ENOMEM;
+        return (NULL);
+    }
+
+    if ((unsigned int) tmp & (boundary - 1))
+    {
+        /* tmp is not already aligned */
+        packet = (Header *) (((unsigned int) tmp + boundary) & ~(boundary - 1)) - 1;
+        if (packet < (Header *) tmp)
+        {
+            /* don't have room for Header before aligned address */
+            packet = (Header *) ((unsigned int) packet + boundary);
+        }
+    }
+    else
+    {
+        /* tmp is already aligned to boundary (by chance) */
+        packet = ((Header *) (((unsigned int) tmp + boundary))) - 1;
+    }
+
+    packet->header.actualBuf = tmp;
+    packet->header.size      = size + sizeof(Header);
+
+    return (packet + 1);
+#else
+    /* user called an unsupported function */
+    return (NULL);
+#endif
+}
+
+/*
+ *  ======== aligned_alloc ========
+ */
+void ATTRIBUTE * aligned_alloc(size_t alignment, size_t size)
+{
+    /* use existing implementation to allocate buffer */
+    return (memalign(alignment, size));
+}
+
+#if defined(__GNUC__) && !defined(__ti__)
+
+/*
+ *  ======== _malloc_r ========
+ */
+void ATTRIBUTE * _malloc_r(struct _reent * rptr, size_t size)
+{
+    return malloc(size);
+}
+
+/*
+ *  ======== _calloc_r ========
+ */
+void ATTRIBUTE * _calloc_r(struct _reent * rptr, size_t nmemb, size_t size)
+{
+    return calloc(nmemb, size);
+}
+
+/*
+ *  ======== _free_r ========
+ */
+void ATTRIBUTE _free_r(struct _reent * rptr, void * ptr)
+{
+    free(ptr);
+}
+
+/*
+ *  ======== _realloc_r ========
+ */
+void ATTRIBUTE * _realloc_r(struct _reent * rptr, void * ptr, size_t size)
+{
+    return realloc(ptr, size);
+}
+
+#endif
diff --git a/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni b/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni
index 0698af1..af3e810 100644
--- a/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni
+++ b/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni
@@ -305,8 +305,8 @@
     configs -= [ "${build_root}/config/compiler:std_default" ]
     configs += [ ":${sdk_target_name}_posix_config" ]
     sources = [
+      "${chip_root}/src/platform/cc13xx_26xx/memory.c",
       "${ti_simplelink_sdk_root}/source/ti/posix/freertos/clock.c",
-      "${ti_simplelink_sdk_root}/source/ti/posix/freertos/memory.c",
       "${ti_simplelink_sdk_root}/source/ti/posix/freertos/mqueue.c",
       "${ti_simplelink_sdk_root}/source/ti/posix/freertos/pthread.c",
       "${ti_simplelink_sdk_root}/source/ti/posix/freertos/pthread_barrier.c",
@@ -521,8 +521,8 @@
       ]
 
       sources = [
+        "${chip_root}/src/platform/cc13xx_26xx/TI_heap_wrapper.c",
         "${chip_root}/src/platform/cc13xx_26xx/chipOBleProfile.c",
-        "${ti_simplelink_sdk_root}/source/ti/ble5stack/common/cc26xx/freertos/TI_heap_wrapper.c",
         "${ti_simplelink_sdk_root}/source/ti/ble5stack/common/cc26xx/freertos/bget.c",
         "${ti_simplelink_sdk_root}/source/ti/ble5stack/common/cc26xx/rcosc/rcosc_calibration.c",
         "${ti_simplelink_sdk_root}/source/ti/dmm/apps/common/ble_remote_display/stack/osal_icall_ble.c",