blob: cc36b31f8ba665df5d042f77994df42c0501abf0 [file] [log] [blame]
Benjamin Prosnitz37078552014-07-10 16:28:22 -07001// +build linux,!android
Jiri Simsa5293dcb2014-05-10 09:56:38 -07002
3#include <stdlib.h>
4
5// Maximum allowed LE payload size. See the Bluetooth 4.0 spec for more info on
6// Bluetooth LE payload structure:
7// https://www.bluetooth.org/en-us/specification/adopted-specifications
8const int kMaxLEPayloadSize;
Asim Shankar13752172014-07-09 11:29:07 -07009// The highest bluetooth channel that can be used for establishing RFCOMM
Jiri Simsa5293dcb2014-05-10 09:56:38 -070010// connections.
Asim Shankar13752172014-07-09 11:29:07 -070011const int kMaxChannel;
Jiri Simsa5293dcb2014-05-10 09:56:38 -070012
13// Maximum number of local devices to scan over when a particular device isn't
14// explicitly specified.
15const int kMaxDevices;
16
17// Opens the bluetooth device with the provided id, storing its device
18// descriptor into '*dd', its device name into '*name', and its MAC address
19// into '*local_address'.
20// Returns an error string if any error is encoutered. If a non-NULL error
21// string is returned, the caller must free it.
22// The caller must free '*name' and '*local_address' strings whenever a NULL
23// error string is returned.
24// REQUIRES: dev_id >= 0
25char* bt_open_device(int dev_id, int* dd, char** name, char** local_address);
26
27// Closes the (previously opened) device with the given device descriptor.
28// Returns error string on failure, or NULL otherwise. If a non-NULL error
29// string is returned, the caller must free it.
30char* bt_close_device(int dd);
31
Asim Shankar13752172014-07-09 11:29:07 -070032// Binds the given socket to the provided MAC address/channel. If '*local_address'
Jiri Simsa5293dcb2014-05-10 09:56:38 -070033// is NULL, it will bind to the first available bluetooth device and overwrite
Asim Shankar13752172014-07-09 11:29:07 -070034// '*local_address' to contain that device's MAC address. If '*channel' is zero,
35// it will bind to the first available channel on a given device and overwrite
36// '*channel' to contain that channel value. (If both of the above are true, we will
37// find the first device/channel pair that works and overwrite both values.)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070038// Returns an error string if any error is encoutered. If a non-NULL error
39// string is returned, the caller must free it.
40// The caller must free '*local_address' string whenever a NULL value was passed
41// in and a NULL error string is returned.
Asim Shankar13752172014-07-09 11:29:07 -070042char* bt_bind(int sock, char** local_address, int* channel);
Jiri Simsa5293dcb2014-05-10 09:56:38 -070043
44// Accepts the next connection on the provided socket. Stores the file
45// descriptor for the newly established connection into 'fd', and the MAC
46// address of the remote party into 'remote_address". Returns an error string
47// if any error is encountered. If a non-NULL error string is returned, the
48// caller must free it.
49// The caller must free '*remote_address' string whenever a NULL error string
50// is returned.
51char* bt_accept(int sock, int* fd, char** remote_address);
52
Asim Shankar13752172014-07-09 11:29:07 -070053// Connects to the remote address/channel pair, using the provided local socket.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070054// Returns an error string if any error is encountered. If a non-NULL error
55// string is returned, the caller must free it.
Asim Shankar13752172014-07-09 11:29:07 -070056char* bt_connect(int sock, const char* remote_address, int remote_channel);
Jiri Simsa5293dcb2014-05-10 09:56:38 -070057
58// Starts bluetooth LE advertising on the provided device descriptor, sending
59// one advertising packet every 'adv_interval_ms' milliseconds.
60// Returns error string on failure, or NULL otherwise. If a non-NULL error
61// string is returned, the caller must free it.
62char* bt_start_le_advertising(int dd, int adv_interval_ms);
63
64// Sets the advertising payload that is sent with each advertising packet.
65// This function may be called at any time to adjust the payload that is
66// currently being advertised.
67// Returns error string on failure, or NULL otherwise. If a non-NULL error
68// string is returned, the caller must free it.
69char* bt_set_le_advertising_payload(int dd, char* adv_payload);
70
71// Stops bluetooth LE advertising on the provided device descriptor.
72// Returns error string on failure, or NULL otherwise. If a non-NULL error
73// string is returned, the caller must free it.
74char* bt_stop_le_advertising(int dd);
75
76// Parses the LE meta event, extracting remote address, name, and RSSI. It also
77// checks whether the event is "LE Connection Complete Event", which indicates
78// that the scan has stopped, and writes the result of that check into 'done'.
79// Returns error if data cannot be parsed. If a non-NULL error string
80// is returned, the caller must free it.
81char* bt_parse_le_meta_event(
82 void* data, char** remote_addr, char** remote_name, int* rssi, int* done);