1 // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include <errno.h>
17 
18 #include "esp_system.h"
19 
20 #include "mesh_main.h"
21 #include "client_common.h"
22 #include "mesh_common.h"
23 
bt_mesh_malloc(size_t size)24 IRAM_ATTR void *bt_mesh_malloc(size_t size)
25 {
26 #ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
27     return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
28 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
29     return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
30 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
31     return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
32 #else
33     return malloc(size);
34 #endif
35 }
36 
bt_mesh_calloc(size_t size)37 IRAM_ATTR void *bt_mesh_calloc(size_t size)
38 {
39 #ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
40     return heap_caps_calloc(1, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
41 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
42     return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
43 #elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
44     return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
45 #else
46     return calloc(1, size);
47 #endif
48 }
49 
bt_mesh_free(void * ptr)50 IRAM_ATTR void bt_mesh_free(void *ptr)
51 {
52     heap_caps_free(ptr);
53 }
54 
bt_mesh_alloc_buf(uint16_t size)55 struct net_buf_simple *bt_mesh_alloc_buf(uint16_t size)
56 {
57     struct net_buf_simple *buf = NULL;
58     uint8_t *data = NULL;
59 
60     buf = (struct net_buf_simple *)bt_mesh_calloc(sizeof(struct net_buf_simple) + size);
61     if (!buf) {
62         BT_ERR("%s, Out of memory", __func__);
63         return NULL;
64     }
65 
66     data = (uint8_t *)buf + sizeof(struct net_buf_simple);
67 
68     buf->data = data;
69     buf->len = 0;
70     buf->size = size;
71     buf->__buf = data;
72 
73     return buf;
74 }
75 
bt_mesh_free_buf(struct net_buf_simple * buf)76 void bt_mesh_free_buf(struct net_buf_simple *buf)
77 {
78     if (buf) {
79         bt_mesh_free(buf);
80     }
81 }
82 
bt_mesh_get_device_role(struct bt_mesh_model * model,bool srv_send)83 uint8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send)
84 {
85     bt_mesh_client_user_data_t *client = NULL;
86 
87     if (srv_send) {
88         BT_DBG("Message is sent by a server model");
89         return NODE;
90     }
91 
92     if (!model || !model->user_data) {
93         BT_ERR("%s, Invalid parameter", __func__);
94         return ROLE_NVAL;
95     }
96 
97     client = (bt_mesh_client_user_data_t *)model->user_data;
98 
99     return client->msg_role;
100 }
101 
bt_mesh_rand(void * buf,size_t len)102 int bt_mesh_rand(void *buf, size_t len)
103 {
104     if (buf == NULL || len == 0) {
105         BT_ERR("%s, Invalid parameter", __func__);
106         return -EINVAL;
107     }
108 
109     esp_fill_random(buf, len);
110 
111     BT_DBG("Random %s", bt_hex(buf, len));
112 
113     return 0;
114 }
115