123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include <stdio.h>
- #include <string.h>
- #include "Includes.h"
- #include "profile.h"
- #include "ble.h"
- #include "platform.h"
- hci_con_handle_t connection_handle;
- void sm_peripheral_setup (void){
- }
- void _HWradio_Go_To_Idle_State_Patch (void){
- }
- static uint32_t min(uint32_t a, uint32_t b){
- return a < b ? a : b;
- }
- static int g_conn_stat = 0;
- int app_get_connect_state(void)
- {
- return g_conn_stat;
- }
- // att_read_callback helpers
- static uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
- if (buffer){
- uint16_t bytes_to_copy = min(blob_size - offset, buffer_size);
- memcpy(buffer, &blob[offset], bytes_to_copy);
- return bytes_to_copy;
- } else {
- return blob_size;
- }
- }
- // read requests
- static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
- printf("%s, con_handle=%x, att_handle=%x, offset=%x, buffer=%x, size=%x\n",__func__, con_handle, att_handle, offset,(uint32_t)buffer, buffer_size);
-
- if((att_handle != ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE) &&
- (att_handle != ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE)) return 0;
-
- static uint8_t read_test[10] = "012345678";
- return att_read_callback_handle_blob((const uint8_t *)read_test, sizeof(read_test), offset, buffer, buffer_size);
- }
- // write requests
- static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
-
- uint32_t le_notification_enabled;
- printf("%s, con_handle=%x, att_handle=%x, offset=%x, buffer=%x, size=%x\n", __func__, con_handle, att_handle, offset, (uint32_t)buffer, buffer_size);
- if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0;
- switch(att_handle)
- {
- case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
- le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
- printf("Notifications enabled %u\n",le_notification_enabled);
- break;
- case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
- case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
- printf("att_handle=0x%x, offset=0x%x, length=0x%x\n", att_handle, offset, buffer_size);
- printf("att data: ");
- for(uint32_t i=0; i<buffer_size; i++) printf("0x%x ", buffer[i]);
- printf("\n");
- break;
- default:
- break;
- }
- return 0;
- }
- static int con_count;
- static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
- if (packet_type != HCI_EVENT_PACKET) return;
- switch(hci_event_packet_get_type(packet))
- {
- case BTSTACK_EVENT_STATE:
- if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
- printf("BTstack up and running.\n");
- break;
-
- case HCI_EVENT_LE_META:
- switch (hci_event_le_meta_get_subevent_code(packet))
- {
- case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: {
- connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
- printf("\n CONNECT RIGHT ! (HANDLE = 0x%x)\n", connection_handle);
- g_conn_stat = 1;
-
- printf("Connected, requesting conn param update for handle 0x%04x\n", connection_handle);
- con_count = 1;
- }
- break;
- default:
- break;
- }
- break;
-
- case HCI_EVENT_DISCONNECTION_COMPLETE:
- printf("\n DISCONNECT ! (HANDLE = 0x%x) ", hci_event_disconnection_complete_get_connection_handle(packet));
- printf("REASON = 0x%x\n", hci_event_disconnection_complete_get_reason(packet));
- g_conn_stat = 0;
- break;
-
- case L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE:
- printf("L2CAP Connection Parameter Update Complete, response: %x\n", l2cap_event_connection_parameter_update_response_get_result(packet));
- break;
- case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
- printf("ATT MTU = %u\n", att_event_mtu_exchange_complete_get_MTU(packet));
- break;
-
- case ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE:
- (att_event_handle_value_indication_complete_get_status(packet) == 0) ? printf("INDICATION OK\n") : printf("INDICATION FAIL\n");
- break;
- default:
- break;
- }
- }
- static const blestack_init_t blestack_init = {
-
- /* uint16_t (*att_read_callback)(...);*/ att_read_callback,
- /* int (*att_write_callback)(...); */ att_write_callback,
- /* void (*packet_handler)(...) */ packet_handler,
- /* const uint8_t *ble_device_db */ profile_data
- };
- const uint8_t adv_data[] = {
- // Flags general discoverable, BR/EDR not supported
- 0x02, 0x01, 0x06,
- // Name
- 0x0A, 0x09, 'X', 'i', 'c', '_', 'B', 'L', 'E', 'v', '1',
- };
- uint8_t adv_data_len = sizeof(adv_data);
- const uint8_t scanresp_data[] = {
- //- Manufacture.
- 0x0c, 0xff, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1',
- //0x03,0x03,0x0,0xFF,0x09,0x09,0x42,0x52,0x54,0x20,0x44,0x41,0x54,0x41,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
- };
- uint8_t scanresp_data_len = sizeof(scanresp_data);
- void ble_listen(void){
-
- xc_wdog_init(0x9);
-
- }
- //setup mac
- void set_bd_addr(void)
- {
- extern uint8_t bd_addr[6];
- bd_addr[0]= 0x45;
- bd_addr[1]= 0x1B;
- bd_addr[2]= 0x26;
- bd_addr[3]= 0xA3;
- bd_addr[4]= 0x84;
- bd_addr[5]= 0x1D;
- }
- enum adv_type{
- ADV_IND,
- ADV_DIRECT_IND,
- ADV_SCAN_IND,
- ADV_NONCONN_IND,
- };
- int i =0;
- void stack_reset(void)
- {
- hci_power_control(1);
- GPIO_Sleep_Config();
- memset(null_addr, 0, 6);
- gap_advertisements_set_params(adv_int_min, adv_int_max, ADV_IND, 0, null_addr, 0x07, 0x00);
- gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
- gap_scan_response_set_data(scanresp_data_len , (uint8_t*) scanresp_data);
- gap_advertisements_enable(1);
- }
- int main(void)
- {
- set_bd_addr();
- // setup advertisements
- uint16_t adv_int_min = 0x00a0;
- uint16_t adv_int_max = 0x00a0;
- bd_addr_t null_addr;
-
- ble_init((void *)&blestack_init);
- GPIO_Sleep_Config();
- memset(null_addr, 0, 6);
- gap_advertisements_set_params(adv_int_min, adv_int_max, ADV_IND, 0, null_addr, 0x07, 0x00);
- gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
- gap_scan_response_set_data(scanresp_data_len , (uint8_t*) scanresp_data);
- gap_advertisements_enable(1);
- ble_system_idle_init();
- while(1) {
- ble_mainloop();
- ble_system_idle();
- if(con_count == 1){
- i++;
- if(i >= 5000){
- i=0;
- // gap_request_connection_parameter_update(connection_handle, 400-20, 400, 0, 600);
- gap_request_connection_parameter_update(connection_handle, 800-20, 800, 0, 600);
- con_count = 0;
- }
- }
- }
- }
|