1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdbool.h>
6 #include <linux/prctl.h>
7 #include <sys/signalfd.h>
8 #include <sys/socket.h>
9 #include <sys/stat.h>
10 #include <sys/un.h>
11
12 #include <Rk_wifi.h>
13 #include <RkBtBase.h>
14 #include <RkBle.h>
15
16 #include "rk_ble_app.h"
17
18 /* Immediate wifi Service UUID */
19 #define BLE_UUID_SERVICE "0000180A-0000-1000-8000-00805F9B34FB"
20 #define BLE_UUID_WIFI_CHAR "00009999-0000-1000-8000-00805F9B34FB"
21 #define BLE_UUID_PROXIMITY "7B931104-1810-4CBC-94DA-875C8067F845"
22
23 #define UUID_MAX_LEN 36
24
25 typedef enum {
26 RK_BLE_WIFI_State_IDLE = 0,
27 RK_BLE_WIFI_State_CONNECTTING,
28 RK_BLE_WIFI_State_SUCCESS,
29 RK_BLE_WIFI_State_FAIL,
30 RK_BLE_WIFI_State_WRONGKEY_FAIL,
31 RK_BLE_WIFI_State_DISCONNECT
32 } RK_BLE_WIFI_State_e;
33
34 const char *MSG_BLE_WIFI_LIST_FORMAT = "{\"cmd\":\"wifilists\", \"ret\":%s}";
35
36 static unsigned char rk_wifi_list_buf[20 * 1024];
37 static int scanr_len = 0, scanr_len_use = 0;
38
39 static pthread_t wificonfig_tid = 0;
40 static pthread_t wificonfig_scan_tid = 0;
41
42 static char wifi_ssid[256];
43 static char wifi_password[256];
44
45 static unsigned int ble_mtu = 0;
46
47 struct wifi_config {
48 char ssid[512];
49 int ssid_len;
50 char psk[512];
51 int psk_len;
52 char key_mgmt[512];
53 int key_len;
54 bool hide;
55 void (*wifi_status_callback)(int status, int reason);
56 };
57
58 static struct wifi_config wifi_cfg;
59 typedef int (*RK_blewifi_state_callback)(RK_BLE_WIFI_State_e state);
60
61 #define RK_BLE_DATA_CMD_LEN 12
62 #define RK_BLE_DATA_SSID_LEN 32
63 #define RK_BLE_DATA_PSK_LEN 32
64
65 /* 消息开头必须为0x01,结尾必须为0x04 */
66 typedef struct {
67 unsigned char start; // 段, 01Byte, 固定为 0x01
68 char cmd[RK_BLE_DATA_CMD_LEN]; // 段, 12Byte, 值为:"wifisetup"、"wifilists"
69 char ssid[RK_BLE_DATA_SSID_LEN]; // 段, 32Byte, 需要连接的 WiFi ssid, 长度不足 32 的部分填充 0
70 char psk[RK_BLE_DATA_PSK_LEN]; // 段, 32Byte, 需要连接的 wifi password, 长度不足 32 的部分填充 0
71 //unsigned char end; // 段, 01Byte, 固定位 0x04
72 } RockChipBleData;
73
74 typedef struct {
75 uint8_t data[6];
76 } mac_t;
77
78 typedef struct {
79 uint8_t data[16];
80 } uuid128_t;
81
_rk_ble_status_cb(const char * bd_addr,const char * name,RK_BLE_STATE state)82 void _rk_ble_status_cb(const char *bd_addr, const char *name, RK_BLE_STATE state)
83 {
84 switch (state) {
85 case RK_BLE_STATE_IDLE:
86 printf("[RK] ble status: RK_BLE_STATE_IDLE\n");
87 break;
88 case RK_BLE_STATE_CONNECT:
89 printf("[RK] ble status: RK_BLE_STATE_CONNECT\n");
90 break;
91 case RK_BLE_STATE_DISCONNECT:
92 printf("[RK] ble status: RK_BLE_STATE_DISCONNECT\n");
93 ble_mtu = 0;
94 break;
95 }
96 }
97
_rk_ble_mtu_callback(const char * bd_addr,unsigned int mtu)98 static void _rk_ble_mtu_callback(const char *bd_addr, unsigned int mtu)
99 {
100 printf("=== %s: bd_addr: %s, mtu: %d ===\n", __func__, bd_addr, mtu);
101 ble_mtu = mtu;
102 }
103
rk_blewifi_state_callback(RK_WIFI_RUNNING_State_e state,RK_WIFI_INFO_Connection_s * info)104 static int rk_blewifi_state_callback(RK_WIFI_RUNNING_State_e state, RK_WIFI_INFO_Connection_s *info)
105 {
106 uint8_t ret = 0;
107
108 printf("[RK] %s state: %d\n", __func__, state);
109 switch(state) {
110 case RK_WIFI_State_CONNECTED:
111 ret = 0x1;
112 break;
113 case RK_WIFI_State_CONNECTFAILED:
114 case RK_WIFI_State_CONNECTFAILED_WRONG_KEY:
115 ret = 0x2;
116 break;
117 }
118
119 if(ret)
120 rk_ble_write(BLE_UUID_WIFI_CHAR, &ret, 0x1);
121
122 return 0;
123 }
124
rk_config_wifi_thread(void)125 static void *rk_config_wifi_thread(void)
126 {
127 printf("[RK] rk_config_wifi_thread\n");
128
129 prctl(PR_SET_NAME,"rk_config_wifi_thread");
130
131 RK_wifi_register_callback(rk_blewifi_state_callback);
132 RK_wifi_enable(0);
133 RK_wifi_enable(1);
134 RK_wifi_connect(wifi_cfg.ssid, wifi_cfg.psk);
135 return NULL;
136 }
137
rk_ble_send_data(void)138 static void rk_ble_send_data(void)
139 {
140 int len, send_max_len = BT_ATT_DEFAULT_LE_MTU;
141 uint8_t *data;
142
143 if (scanr_len == 0) {
144 scanr_len_use = 0;
145 printf("[RK] NO WIFI SCAN_R OR READ END!!!\n");
146 return;
147 }
148
149 prctl(PR_SET_NAME,"rk_ble_send_data");
150
151 if(ble_mtu > BT_ATT_HEADER_LEN)
152 send_max_len = ble_mtu;
153
154 send_max_len -= BT_ATT_HEADER_LEN;
155 if(send_max_len > BT_ATT_MAX_VALUE_LEN)
156 send_max_len = BT_ATT_MAX_VALUE_LEN;
157
158 while (scanr_len) {
159 printf("[RK] %s: wifi use: %d, remain len: %d\n", __func__, scanr_len_use, scanr_len);
160 len = (scanr_len > send_max_len) ? send_max_len : scanr_len;
161 data = rk_wifi_list_buf + scanr_len_use;
162 usleep(100000);
163 rk_ble_write(BLE_UUID_WIFI_CHAR, data, len);
164 scanr_len -= len;
165 scanr_len_use += len;
166 }
167 }
168
_rk_ble_recv_data_cb(const char * uuid,char * data,int len)169 void _rk_ble_recv_data_cb(const char *uuid, char *data, int len)
170 {
171 if (!strcmp(uuid, BLE_UUID_WIFI_CHAR)) {
172 uint8_t str[512];
173 RockChipBleData *ble_data;
174 char *wifilist;
175 unsigned char end_flag = 0;
176
177 memset(str, 0, 512);
178 memcpy(str, data, len);
179 str[len] = '\0';
180 end_flag = str[len - 1];
181
182 printf("[RK] chr_write_value: %p, %d, data: %s\n", data, len, data);
183
184 for (int i = 0; i < len; i++) {
185 if (!( i % 8))
186 printf("\n");
187 printf("0x%02x ", str[i]);
188 }
189 printf("\n");
190
191 ble_data = (RockChipBleData *)str;
192
193 /* Msg is valid? */
194 printf("[RK] ble_data.cmd: %s, ble_data.start: %d, ble_data.end: %d\n",
195 ble_data->cmd, ble_data->start, end_flag);
196 if ((ble_data->start != 0x1) || (end_flag != 0x4)) {
197 printf("[RK] BLE RECV DATA ERROR !!!\n");
198 return;
199 }
200
201 if (strncmp(ble_data->cmd, "wifilists", 9) == 0) {
202 scan_retry:
203 printf("[RK] RK_wifi_scan ...\n");
204 RK_wifi_scan();
205 usleep(800000);
206
207 printf("[RK] RK_wifi_scan_r_sec ...\n");
208 wifilist = RK_wifi_scan_r_sec(0x14);
209 printf("[RK] RK_wifi_scan_r_sec end wifilist: %p\n", wifilist);
210
211 if (wifilist == NULL)
212 goto scan_retry;
213 if (wifilist && (strlen(wifilist) < 3)) {
214 free(wifilist);
215 goto scan_retry;
216 }
217
218 memset(rk_wifi_list_buf, 0, sizeof(rk_wifi_list_buf));
219 snprintf(rk_wifi_list_buf, sizeof(rk_wifi_list_buf), MSG_BLE_WIFI_LIST_FORMAT, wifilist);
220 scanr_len = strlen(rk_wifi_list_buf);
221 scanr_len_use = 0;
222 printf("[RK] wifi scan_r: %s, len: %d\n", rk_wifi_list_buf, scanr_len);
223
224 free(wifilist);
225 pthread_create(&wificonfig_scan_tid, NULL, rk_ble_send_data, NULL);
226 } else if (strncmp(ble_data->cmd, "wifisetup", 9) == 0) {
227 strcpy(wifi_ssid, ble_data->ssid); // str + 20);
228 strcpy(wifi_password, ble_data->psk); // str + 52);
229
230 printf("[RK] wifi ssid is %s\n", wifi_ssid);
231 printf("[RK] wifi psk is %s\n", wifi_password);
232
233 strcpy(wifi_cfg.ssid, wifi_ssid);
234 strcpy(wifi_cfg.psk, wifi_password);
235 //wifi_cfg.wifi_status_callback = wifi_status_callback;
236 pthread_create(&wificonfig_tid, NULL, rk_config_wifi_thread, NULL);
237 }
238 }
239 }
240
_rk_ble_request_data_cb(const char * uuid,char * data,int len)241 void _rk_ble_request_data_cb(const char *uuid, char *data, int len)
242 {
243 printf("=== %s uuid: %s===\n", __func__, uuid);
244
245 //len <= mtu(g_mtu)
246 len = sizeof("hello rockchip");
247 memcpy(data, "hello rockchip", len);
248
249 return;
250 }
251
252 static unsigned char bt_is_on = 0;
rk_bt_state_cb(RK_BT_STATE state)253 static void rk_bt_state_cb(RK_BT_STATE state)
254 {
255 switch(state) {
256 case RK_BT_STATE_TURNING_ON:
257 printf("RK_BT_STATE_TURNING_ON\n");
258 break;
259 case RK_BT_STATE_ON:
260 bt_is_on = 1;
261 printf("RK_BT_STATE_ON\n");
262 break;
263 case RK_BT_STATE_TURNING_OFF:
264 printf("RK_BT_STATE_TURNING_OFF\n");
265 break;
266 case RK_BT_STATE_OFF:
267 bt_is_on = 0;
268 printf("RK_BT_STATE_OFF\n");
269 break;
270 }
271 }
272
rk_ble_wifi_init(void * data)273 void rk_ble_wifi_init(void *data)
274 {
275 RkBtContent bt_content;
276
277 printf("===== %s =====\n", __func__);
278
279 //MUST TO SET 0
280 memset(&bt_content, 0, sizeof(RkBtContent));
281 bt_content.bt_name = strdup("RockChip");
282 bt_content.ble_content.ble_name = strdup("RockChipBle");
283 bt_content.ble_content.server_uuid.uuid = BLE_UUID_SERVICE;
284 bt_content.ble_content.server_uuid.len = UUID_128;
285 bt_content.ble_content.chr_uuid[0].uuid = BLE_UUID_WIFI_CHAR;
286 bt_content.ble_content.chr_uuid[0].len = UUID_128;
287 bt_content.ble_content.chr_cnt = 1;
288 bt_content.ble_content.cb_ble_recv_fun = _rk_ble_recv_data_cb;
289 bt_content.ble_content.cb_ble_request_data = _rk_ble_request_data_cb;
290 bt_content.ble_content.advDataType = BLE_ADVDATA_TYPE_SYSTEM;
291 bt_is_on = 0;
292
293 rk_ble_register_status_callback(_rk_ble_status_cb);
294 rk_bt_register_state_callback(rk_bt_state_cb);
295 rk_bt_init(&bt_content);
296
297 while (!bt_is_on)
298 sleep(1);
299
300 printf(">>>>> Start ble ....\n");
301 rk_ble_register_mtu_callback(_rk_ble_mtu_callback);
302 rk_ble_start(&bt_content.ble_content);
303 }
304
rk_ble_wifi_deinit(void * data)305 void rk_ble_wifi_deinit(void *data)
306 {
307 ble_mtu = 0;
308 rk_ble_stop();
309 sleep(3);
310 rk_bt_deinit();
311 }
312