1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <errno.h>
9 
10 #include "esp_system.h"
11 
12 #include "mesh_main.h"
13 #include "client_common.h"
14 #include "mesh_common.h"
15 
bt_mesh_malloc(size_t size)16 IRAM_ATTR void *bt_mesh_malloc(size_t size)
17 {
18 #ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
19     return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
20 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
21     return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
22 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
23     return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
24 #else
25     return malloc(size);
26 #endif
27 }
28 
bt_mesh_calloc(size_t size)29 IRAM_ATTR void *bt_mesh_calloc(size_t size)
30 {
31 #ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
32     return heap_caps_calloc(1, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
33 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
34     return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
35 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
36     return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
37 #else
38     return calloc(1, size);
39 #endif
40 }
41 
bt_mesh_free(void * ptr)42 IRAM_ATTR void bt_mesh_free(void *ptr)
43 {
44     heap_caps_free(ptr);
45 }
46 
bt_mesh_alloc_buf(uint16_t size)47 struct net_buf_simple *bt_mesh_alloc_buf(uint16_t size)
48 {
49     struct net_buf_simple *buf = NULL;
50     uint8_t *data = NULL;
51 
52     buf = (struct net_buf_simple *)bt_mesh_calloc(sizeof(struct net_buf_simple) + size);
53     if (!buf) {
54         BT_ERR("%s, Out of memory", __func__);
55         return NULL;
56     }
57 
58     data = (uint8_t *)buf + sizeof(struct net_buf_simple);
59 
60     buf->data = data;
61     buf->len = 0;
62     buf->size = size;
63     buf->__buf = data;
64 
65     return buf;
66 }
67 
bt_mesh_free_buf(struct net_buf_simple * buf)68 void bt_mesh_free_buf(struct net_buf_simple *buf)
69 {
70     if (buf) {
71         bt_mesh_free(buf);
72     }
73 }
74 
bt_mesh_get_device_role(struct bt_mesh_model * model,bool srv_send)75 uint8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send)
76 {
77     bt_mesh_client_user_data_t *client = NULL;
78 
79     if (srv_send) {
80         BT_DBG("Message is sent by a server model");
81         return NODE;
82     }
83 
84     if (!model || !model->user_data) {
85         BT_ERR("%s, Invalid parameter", __func__);
86         return ROLE_NVAL;
87     }
88 
89     client = (bt_mesh_client_user_data_t *)model->user_data;
90 
91     return client->msg_role;
92 }
93 
bt_mesh_rand(void * buf,size_t len)94 int bt_mesh_rand(void *buf, size_t len)
95 {
96     if (buf == NULL || len == 0) {
97         BT_ERR("%s, Invalid parameter", __func__);
98         return -EINVAL;
99     }
100 
101     esp_fill_random(buf, len);
102 
103     BT_DBG("Random %s", bt_hex(buf, len));
104 
105     return 0;
106 }
107