BTLE/BTLE.h

101 lines
2.8 KiB
C
Raw Normal View History

2018-07-20 20:21:55 +02:00
/*
* Copyright (C) 2013 Florian Echtler <floe@butterbrot.org>
2018-07-21 10:46:45 +02:00
* Eddystone part Copyright (c) 2018 Pavel Brychta <pablo@xpablo.cz>
2018-07-20 20:21:55 +02:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*/
#ifndef _BTLE_H_
#define _BTLE_H_
#include "Arduino.h"
#include <RF24.h>
// float as used on the nRF8001 and nRF51822 platforms
// and on the "nRF Master Control Panel" and "nRF Temp 2.0" apps.
// This float representation has the first 8 bits as a base-10 exponent
// and the last 24 bits as the mantissa.
typedef int32_t nRF_Float;
// Service UUIDs used on the nRF8001 and nRF51822 platforms
#define NRF_TEMPERATURE_SERVICE_UUID 0x1809
#define NRF_BATTERY_SERVICE_UUID 0x180F
#define NRF_DEVICE_INFORMATION_SERVICE_UUID 0x180A
// helper struct for sending temperature as BT service data
struct nrf_service_data {
int16_t service_uuid;
nRF_Float value;
};
// advertisement PDU
struct btle_adv_pdu {
// packet header
uint8_t pdu_type; // PDU type
uint8_t pl_size; // payload size
// MAC address
uint8_t mac[6];
// payload (including 3 bytes for CRC)
uint8_t payload[24];
};
// payload chunk in advertisement PDU payload
struct btle_pdu_chunk {
uint8_t size;
uint8_t type;
uint8_t data[];
};
// helper macro to access chunk at specific offset
#define chunk(x,y) ((btle_pdu_chunk*)(x.payload+y))
class BTLE {
public:
BTLE( RF24* _radio );
// convert an arduino float to a nRF_Float
static nRF_Float to_nRF_Float(float t);
void begin( const char* _name ); // set BTLE-compatible radio parameters & name
void setChannel( uint8_t num ); // set the current channel (from 36 to 38)
void hopChannel(); // hop to the next channel
// Broadcast an advertisement packet with a specific data type
2018-07-21 10:46:45 +02:00
// Standardized data types can be seen here:
2018-07-20 20:21:55 +02:00
// https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
2018-07-21 10:46:45 +02:00
bool advertise( uint8_t data_type, void* buf, uint8_t len );
2018-07-20 20:21:55 +02:00
// Broadcast an advertisement packet with optional payload
// Data type will be 0xFF (Manufacturer Specific Data)
2018-07-21 10:46:45 +02:00
bool advertise( void* buf, uint8_t len );
2018-07-20 20:21:55 +02:00
bool listen( int timeout = 100 ); // listen for advertisement packets (if true: result = buffer)
2018-07-21 10:46:45 +02:00
// Broadcast an Eddystone URL packet
bool eddystoneURL(const char *url);
2018-07-20 20:21:55 +02:00
btle_adv_pdu buffer; // buffer for received BTLE packet (also used for outgoing!)
private:
void whiten( uint8_t len );
void swapbuf( uint8_t len );
void crc( uint8_t len, uint8_t* dst );
2018-07-21 10:46:45 +02:00
uint8_t encodeURL(uint8_t* encodedUrl, const char *rawUrl);
2018-07-20 20:21:55 +02:00
RF24* radio; // pointer to the RF24 object managing the radio
uint8_t current; // current channel index
const char* name; // name of local device
};
#endif // _BTLE_H_