mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-25 16:43:28 +00:00
add notifiable and indicatable characteristic that represents an 8 bit counter
This commit is contained in:
parent
157efbac7c
commit
abb2e83f6a
@ -285,3 +285,20 @@ void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_
|
|||||||
void att_server_register_packet_handler(btstack_packet_handler_t handler){
|
void att_server_register_packet_handler(btstack_packet_handler_t handler){
|
||||||
att_client_packet_handler = handler;
|
att_client_packet_handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int att_server_can_send(){
|
||||||
|
if (att_request_handle == 0) return 0;
|
||||||
|
return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
|
||||||
|
}
|
||||||
|
|
||||||
|
void att_server_notify(uint16_t handle, uint8_t *value, uint16_t value_len){
|
||||||
|
uint8_t packet_buffer[att_connection.mtu];
|
||||||
|
uint16_t size = att_prepare_handle_value_notification(&att_connection, handle, value, value_len, packet_buffer);
|
||||||
|
l2cap_send_connectionless(att_request_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, packet_buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len){
|
||||||
|
uint8_t packet_buffer[att_connection.mtu];
|
||||||
|
uint16_t size = att_prepare_handle_value_indication(&att_connection, handle, value, value_len, packet_buffer);
|
||||||
|
l2cap_send_connectionless(att_request_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, packet_buffer, size);
|
||||||
|
}
|
||||||
|
@ -38,6 +38,9 @@
|
|||||||
#include <btstack/btstack.h>
|
#include <btstack/btstack.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "att.h"
|
#include "att.h"
|
||||||
|
|
||||||
void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback);
|
void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback);
|
||||||
void att_server_register_packet_handler(btstack_packet_handler_t handler);
|
void att_server_register_packet_handler(btstack_packet_handler_t handler);
|
||||||
|
int att_server_can_send();
|
||||||
|
void att_server_notify(uint16_t handle, uint8_t *value, uint16_t value_len);
|
||||||
|
void att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len);
|
||||||
|
@ -33,15 +33,10 @@
|
|||||||
|
|
||||||
//*****************************************************************************
|
//*****************************************************************************
|
||||||
//
|
//
|
||||||
// att device demo
|
// BLE Peripheral Demo
|
||||||
//
|
//
|
||||||
//*****************************************************************************
|
//*****************************************************************************
|
||||||
|
|
||||||
// TODO: seperate BR/EDR from LE ACL buffers
|
|
||||||
// ..
|
|
||||||
|
|
||||||
// NOTE: Supports only a single connection
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -63,24 +58,52 @@
|
|||||||
#include "gap_le.h"
|
#include "gap_le.h"
|
||||||
#include "central_device_db.h"
|
#include "central_device_db.h"
|
||||||
|
|
||||||
///------
|
#define HEARTBEAT_PERIOD_MS 1000
|
||||||
static int advertisements_enabled = 0;
|
|
||||||
|
|
||||||
// test profile
|
// test profile
|
||||||
#include "profile.h"
|
#include "profile.h"
|
||||||
|
|
||||||
|
///------
|
||||||
|
static int advertisements_enabled = 0;
|
||||||
|
static timer_source_t heartbeat;
|
||||||
|
static uint8_t counter = 0;
|
||||||
|
static int update_client = 0;
|
||||||
|
static int client_configuration = 0;
|
||||||
|
|
||||||
|
static void app_run();
|
||||||
|
|
||||||
|
static void heartbeat_handler(struct timer *ts){
|
||||||
|
// restart timer
|
||||||
|
run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
|
||||||
|
run_loop_add_timer(ts);
|
||||||
|
|
||||||
|
counter++;
|
||||||
|
update_client = 1;
|
||||||
|
app_run();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void app_run(){
|
||||||
|
if (!client_configuration || !update_client) return;
|
||||||
|
if (!att_server_can_send()) return;
|
||||||
|
printf("Notify value %u\n", counter);
|
||||||
|
update_client = 0;
|
||||||
|
att_server_notify(0x0f, &counter, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// write requests
|
// write requests
|
||||||
static void att_write_callback(uint16_t handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size, signature_t * signature){
|
static int att_write_callback(uint16_t handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size, signature_t * signature){
|
||||||
printf("WRITE Callback, handle %04x\n", handle);
|
printf("WRITE Callback, handle %04x\n", handle);
|
||||||
switch(handle){
|
switch(handle){
|
||||||
case 0x000b:
|
case 0x0010:
|
||||||
buffer[buffer_size]=0;
|
client_configuration = buffer[0];
|
||||||
printf("New text: %s\n", buffer);
|
printf("Client Configuration set to %u\n", client_configuration);
|
||||||
break;
|
break;
|
||||||
case 0x000d:
|
default:
|
||||||
printf("New value: %u\n", buffer[0]);
|
printf("Value: ");
|
||||||
|
hexdump(buffer, buffer_size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||||
@ -180,9 +203,15 @@ void setup(void){
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
setup();
|
setup();
|
||||||
// gap_random_address_set_update_period(5000);
|
gap_random_address_set_update_period(60000);
|
||||||
gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE);
|
gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE);
|
||||||
|
|
||||||
|
// set one-shot timer
|
||||||
|
heartbeat.process = &heartbeat_handler;
|
||||||
|
run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
|
||||||
|
run_loop_add_timer(&heartbeat);
|
||||||
|
|
||||||
|
|
||||||
// turn on!
|
// turn on!
|
||||||
hci_power_control(HCI_POWER_ON);
|
hci_power_control(HCI_POWER_ON);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user