Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/ll-broadcaster/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <blessed/bdaddr.h>
#include <blessed/evtloop.h>
#include <blessed/events.h>

#include "ll.h"

Expand Down
7 changes: 7 additions & 0 deletions examples/ll-connection-master/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Makefile for ll-connection-master example

PROJECT_TARGET = ll-connection-master-example
PROJECT_SOURCE_FILES = main.c
PROJECT_CFLAGS = -g -O0

include ../Makefile.common
174 changes: 174 additions & 0 deletions examples/ll-connection-master/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/**
* The MIT License (MIT)
*
* 2014 - Tom Magnier <magnier.tom@gmail.com>
*
* Adapted from ll-initiator example
*
* This example demonstrates the connection state, in Central role.
* A GATT characteristic (handle 0x000E) is written regularly and notifications
* from the battery service (characteristic : handle 0x0015) are received.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include <blessed/bdaddr.h>
#include <blessed/log.h>
#include <blessed/events.h>
#include <blessed/evtloop.h>

#include "delay.h"

#include "ll.h"

#define SCAN_WINDOW 200000
#define SCAN_INTERVAL 500000

/* L2CAP specification, Section 3.1, Core 4.1 p.1683
* L2CAP specification, Section 1.1, Core 4.1 p.1677
* ATT specification, Section 3.4.5.3, Core 4.1 p. 2148 */
static uint8_t out_buffer[] = {
0x04, 0x00, //L2CAP payload length
0x04, 0x00, //L2CAP Channel ID : ATT
0x52, //ATT opcode : write cmd
0x0E, 0x00, //ATT handle (0x000E)
0x00}; //ATT value to write

static uint8_t in_buffer[LL_DATA_MTU_PAYLOAD];

/* PDU to send to enable notifications on battery service */
static uint8_t cccd_out_buffer[] = {
0x05, 0x00, //L2CAP payload length
0x04, 0x00, //L2CAP Channel ID : ATT
0x52, //ATT opcode : write cmd
0x16, 0x00, //ATT handle (0x0016)
0x01, 0x00}; //ATT value to write

/* Expected packet on handle value notification ; the 8th byte is the value */
static const uint8_t notification_buffer_value[] = {
0x04, 0x00, //L2CAP payload length
0x04, 0x00, //L2CAP CID : ATT
0x1B, //ATT opcode : handle value notification
0x15, 0x00}; //ATT handle (0x0015)

static const bdaddr_t addr = { { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF },
BDADDR_TYPE_RANDOM };

static bdaddr_t peer_addr;
uint8_t led_value = 0x00;


static __inline const char *format_address(const uint8_t *data)
{
static char address[18];
uint8_t i;

for (i = 0; i < 5; i++)
sprintf(address + 3*i, "%02x:", data[5-i]);
sprintf(address + 3*i, "%02x", data[5-i]);

return address;
}

static __inline const char *format_data(const uint8_t *data, uint8_t len)
{
static char output[3 * LL_ADV_MTU_DATA + 1];
uint8_t i;

for (i = 0; i < len; i++)
sprintf(output + 3*i, "%02x ", data[i]);

output[3*i] = '\0';

return output;
}

void conn_evt_cb(ble_evt_t type, const void *data)
{
const ble_evt_ll_connection_complete_t* conn_compl = data;
const ble_evt_ll_disconnect_complete_t* disconn_compl = data;
const ble_evt_ll_packets_received_t* packets_rx = data;

switch (type) {
case BLE_EVT_LL_CONNECTION_COMPLETE:
DBG("Connection complete, index %u, address %s",
conn_compl->index, format_address(conn_compl->peer_addr.addr));

/*Enable notifications on battery service (write 0x0001 on CCCD
* characteristic, handle 0x0016) */
ll_conn_send(cccd_out_buffer, 9);
break;

case BLE_EVT_LL_DISCONNECT_COMPLETE:
DBG("Disconnect complete, index %u, reason %02x",
disconn_compl->index, disconn_compl->reason);

evt_loop_run();
break;

case BLE_EVT_LL_PACKETS_SENT:
out_buffer[7] = led_value;
ll_conn_send(out_buffer, 8);
break;

case BLE_EVT_LL_PACKETS_RECEIVED:
if (!memcmp(in_buffer, notification_buffer_value, 7))
DBG("Battery value : %u %%", in_buffer[7]);
else
DBG("Received packet : %s", format_data(in_buffer,
packets_rx->length));
break;
}
}

void adv_report_cb(struct adv_report *report)
{
DBG("adv type %02x, addr type %02x", report->type, report->addr.type);
DBG("address %s, data %s", format_address(report->addr.addr),
format_data(report->data, report->len));

memcpy(peer_addr.addr, report->addr.addr, BDADDR_LEN);
peer_addr.type = report->addr.type;

ll_scan_stop();
ll_conn_create(SCAN_INTERVAL, SCAN_WINDOW, &peer_addr, 1, in_buffer,
conn_evt_cb);
}

int main(void)
{
log_init();
ll_init(&addr);

DBG("End init, connection + battery notification");

ll_scan_start(LL_SCAN_PASSIVE, SCAN_INTERVAL, SCAN_WINDOW,
adv_report_cb);
while (1)
{
led_value++;
delay(10000);
}

return 0;
}
22 changes: 21 additions & 1 deletion examples/ll-initiator/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <blessed/bdaddr.h>
#include <blessed/log.h>
#include <blessed/evtloop.h>
#include <blessed/events.h>

#include "nrf_delay.h"

Expand All @@ -44,6 +45,8 @@ static const bdaddr_t addr = { { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF },

static bdaddr_t peer_addr;

static uint8_t in_buffer[LL_DATA_MTU_PAYLOAD];

static __inline const char *format_address(const uint8_t *data)
{
static char address[18];
Expand All @@ -69,6 +72,22 @@ static __inline const char *format_data(const uint8_t *data, uint8_t len)
return output;
}


void conn_evt_cb(ble_evt_t type, const void *data)
{
const ble_evt_ll_connection_complete_t* conn_compl = data;

switch (type) {
case BLE_EVT_LL_CONNECTION_COMPLETE:
DBG("Connection complete, index %u, address %s",
conn_compl->index, format_address(conn_compl->peer_addr.addr));
break;
default:
break;
}
}


void adv_report_cb(struct adv_report *report)
{
DBG("adv type %02x, addr type %02x", report->type, report->addr.type);
Expand All @@ -78,7 +97,8 @@ void adv_report_cb(struct adv_report *report)
memcpy(&peer_addr, &report->addr, sizeof(bdaddr_t));

ll_scan_stop();
ll_conn_create(SCAN_INTERVAL, SCAN_WINDOW, &peer_addr, 1);
ll_conn_create(SCAN_INTERVAL, SCAN_WINDOW, &peer_addr, 1, in_buffer,
conn_evt_cb);

}

Expand Down
1 change: 1 addition & 0 deletions examples/ll-scanner/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <blessed/bdaddr.h>
#include <blessed/log.h>
#include <blessed/evtloop.h>
#include <blessed/events.h>

#include "ll.h"
#include "delay.h"
Expand Down
8 changes: 8 additions & 0 deletions include/blessed/errcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@
#define ENOREADY 0x04 /* Not ready */
#define EBUSY 0x05 /* Device or resource busy */
#define EINTERN 0x06 /* Internal error */

/* Reasons for disconnection
* Error codes, Section 1.3, Core 4.1 p. 666 */
#define BLE_HCI_CONNECTION_TIMEOUT 0x08
#define BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION 0x13
#define BLE_HCI_REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES 0x14
#define BLE_HCI_REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF 0x15
#define BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION 0x16
61 changes: 61 additions & 0 deletions include/blessed/events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2013 Paulo B. de Oliveira Filho <pauloborgesfilho@gmail.com>
* Copyright (c) 2013 Claudio Takahasi <claudio.takahasi@gmail.com>
* Copyright (c) 2013 João Paulo Rechi Vita <jprvita@gmail.com>
* Copyright (c) 2014 Tom Magnier <magnier.tom@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/* Maximum size of parameters structure (to allocate a static buffer) */
#define BLE_EVT_PARAMS_MAX_SIZE 8

/* HCI Specification, Section 7.7, Core 4.1 p.1132-1239 */
typedef enum ble_evt {
BLE_EVT_LL_CONNECTION_COMPLETE, /* Sec 7.7.3 p.1135 */
BLE_EVT_LL_DISCONNECT_COMPLETE, /* Sec 7.7.5 p.1138 */
BLE_EVT_LL_PACKETS_SENT,
BLE_EVT_LL_PACKETS_RECEIVED
}ble_evt_t;

/* Events parameters structure for BLE_EVT_LL_CONNECTION_COMPLETE event */
typedef struct ble_evt_ll_connection_complete {
uint8_t index; /* Connection index */
bdaddr_t peer_addr; /* Peer address */
} ble_evt_ll_connection_complete_t;

/* Events parameters structure for BLE_EVT_LL_DISCONNECT_COMPLETE event */
typedef struct ble_evt_ll_disconnect_complete {
uint8_t index; /* Connection index */
uint8_t reason; /* Reason for disconnection */
} ble_evt_ll_disconnect_complete_t;

/* Events parameters structure for BLE_EVT_LL_PACKETS_SENT event */
typedef struct ble_evt_ll_packets_sent {
uint8_t index; /* Connection index */
} ble_evt_ll_packets_sent_t;

/* Events parameters structure for BLE_EVT_LL_PACKETS_RECEIVED event */
typedef struct ble_evt_ll_packets_received {
uint8_t index; /* Connection index */
uint8_t length; /* Number of bytes received */
} ble_evt_ll_packets_received_t;

1 change: 1 addition & 0 deletions platform/nrf51822/ll-plat.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <blessed/errcodes.h>
#include <blessed/bdaddr.h>
#include <blessed/events.h>

#include "ll.h"
#include "nrf51822.h"
Expand Down
1 change: 1 addition & 0 deletions stack/bci.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <blessed/errcodes.h>
#include <blessed/bdaddr.h>
#include <blessed/events.h>
#include <blessed/bci.h>

#include "ll.h"
Expand Down
Loading