1 #include <stdint.h>
2 #include <hal/hal.h>
3
4 #include <bluetooth/bluetooth.h>
5 #include <bluetooth/hci.h>
6 //#include <aos/aos.h>
7 #include <aos/kv.h>
8 #include "adv.h"
9 #include "common/log.h"
10 #include "genie_service.h"
11 #include "genie_sal_ble.h"
12
13 static aos_sem_t scan_sem;
14 static genie_sal_ble_get_rssi_cb get_rssi_cb = NULL;
15 static uint8_t test_dev_mac[6] = {0};
16
17 extern int hci_driver_init(void);
18
mesh_scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)19 void mesh_scan_cb(const bt_addr_le_t *addr, int8_t rssi,
20 uint8_t adv_type, struct net_buf_simple *buf)
21 {
22 uint8_t mac_addr[6] = {0};
23
24 if (buf == NULL || addr == NULL)
25 {
26 return;
27 }
28
29 //printf("addr->a.val: %s, rssi: %d\n", bt_hex(addr->a.val, sizeof(addr->a.val)), rssi);
30 sys_memcpy_swap(mac_addr, addr->a.val, 6);
31 if (memcmp(test_dev_mac, mac_addr, sizeof(test_dev_mac)) == 0)
32 {
33 bt_mesh_scan_disable();
34 bt_mesh_scan_enable();
35
36 memset(test_dev_mac, 0, sizeof(test_dev_mac));
37 if (get_rssi_cb)
38 {
39 get_rssi_cb(mac_addr, rssi);
40 }
41 aos_sem_signal_all(&scan_sem);
42 }
43 }
44
scan_timeout(void)45 static void scan_timeout(void)
46 {
47 printf("scan timeout\r\n");
48 bt_mesh_scan_disable();
49 bt_mesh_scan_enable();
50
51 memset(test_dev_mac, 0, sizeof(test_dev_mac));
52 if (get_rssi_cb)
53 {
54 get_rssi_cb(test_dev_mac, 0);
55 }
56 }
57
genie_sal_ble_get_rssi(uint8_t mac[6],genie_sal_ble_get_rssi_cb get_rssi_callback,uint32_t timeout)58 int genie_sal_ble_get_rssi(uint8_t mac[6], genie_sal_ble_get_rssi_cb get_rssi_callback, uint32_t timeout)
59 {
60 struct bt_le_scan_param scan_param = {
61 .type = BT_HCI_LE_SCAN_PASSIVE,
62 .filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_DISABLE,
63 .interval = 0x10,
64 .window = 0x10};
65
66 if (get_rssi_callback == NULL)
67 {
68 return -1;
69 }
70
71 if (0 != aos_sem_new(&scan_sem, 0))
72 {
73 printf("new sem fail\r\n");
74 return -1;
75 }
76
77 memcpy(test_dev_mac, mac, sizeof(test_dev_mac));
78 printf("scan mac:%s\n", bt_hex(test_dev_mac, sizeof(test_dev_mac)));
79 get_rssi_cb = get_rssi_callback;
80
81 bt_mesh_scan_disable();
82 int ret = bt_le_scan_start(&scan_param, mesh_scan_cb);
83 if (ret)
84 {
85 printf("scan err:%d\n", ret);
86 memset(test_dev_mac, 0, sizeof(test_dev_mac));
87 if (get_rssi_cb)
88 {
89 get_rssi_cb(test_dev_mac, 0);
90 }
91 aos_sem_free(&scan_sem);
92 return -1;
93 }
94
95 if (-ETIMEDOUT == aos_sem_wait(&scan_sem, timeout))
96 {
97 scan_timeout();
98 }
99 aos_sem_free(&scan_sem);
100
101 return 0;
102 }
103
genie_sal_ble_send_msg(uint8_t element_id,uint8_t * p_data,uint8_t len)104 int genie_sal_ble_send_msg(uint8_t element_id, uint8_t *p_data, uint8_t len)
105 {
106 int ret = -1;
107 uint8_t i = 0;
108 uint8_t seg_count = 0;
109 struct bt_mesh_model *p_model = NULL;
110 struct net_buf_simple *msg = NULL;
111 struct bt_mesh_msg_ctx ctx;
112 struct bt_mesh_elem *p_element = NULL;
113 genie_transport_model_param_t request_msg;
114
115 if (len > GENIE_HAL_BLE_SEND_MAX_DATA_LEN)
116 {
117 return -1;
118 }
119
120 p_element = bt_mesh_elem_find_by_id(element_id);
121 if (p_element == NULL)
122 {
123 printf("not find element by id:%d\r\n", element_id);
124 return -1;
125 }
126
127 if ((len >= 4) && (p_data[0] == VENDOR_OP_ATTR_INDICATE || p_data[0] == VENDOR_OP_ATTR_INDICATE_TG))
128 {
129 seg_count = genie_transport_get_seg_count(len - 4 + 4);
130 memset(&request_msg, 0, sizeof(genie_transport_model_param_t));
131 request_msg.opid = p_data[0];
132 request_msg.tid = p_data[3];
133 request_msg.data = (u8_t *)(p_data + 4);
134 request_msg.len = len - 4;
135 request_msg.p_elem = p_element;
136 request_msg.retry_period = GENIE_TRANSPORT_EACH_PDU_TIMEOUT * seg_count;
137 request_msg.retry = VENDOR_MODEL_MSG_MAX_RETRY_TIMES;
138
139 ret = genie_transport_send_model(&request_msg);
140 if (ret != 0)
141 {
142 printf("vendor model msg send fail\n");
143 return -1;
144 }
145 return 0;
146 }
147 else
148 {
149 ctx.app_idx = 0;
150 ctx.net_idx = 0;
151 ctx.addr = genie_transport_src_addr_get();
152 ctx.send_ttl = GENIE_TRANSPORT_DEFAULT_TTL;
153 ctx.send_rel = 0;
154
155 msg = NET_BUF_SIMPLE(GENIE_HAL_BLE_SEND_MAX_DATA_LEN + 4);
156 if (msg == NULL)
157 {
158 printf("no mem\n");
159 return -1;
160 }
161
162 net_buf_simple_init(msg, 0);
163 while (i < len)
164 {
165 net_buf_simple_add_u8(msg, p_data[i]);
166 i++;
167 }
168
169 if ((p_data[0] & 0xC0) == 0xC0)
170 {
171 p_model = p_element->vnd_models;
172 }
173 else
174 {
175 p_model = p_element->models;
176 }
177
178 if (p_model == NULL)
179 {
180 printf("no model\n");
181 return -1;
182 }
183
184 if (bt_mesh_model_send(p_model, &ctx, msg, NULL, NULL))
185 {
186 printf("mesh send fail\n");
187 return -1;
188 }
189
190 return 0;
191 }
192 }
193
genie_sal_ble_set_factory_flag(void)194 int genie_sal_ble_set_factory_flag(void)
195 {
196 int ret = -1;
197 uint16_t flag = 0xAA55;
198
199 ret = aos_kv_set("FTST", &flag, sizeof(uint16_t), 0);
200 if (ret != 0)
201 {
202 printf("write FTST flag error\n");
203 return ret;
204 }
205
206 return 0;
207 }
208
genie_sal_ble_init(void)209 int genie_sal_ble_init(void)
210 {
211 return hci_driver_init();
212 }
213