Benjamin Prosnitz | 3707855 | 2014-07-10 16:28:22 -0700 | [diff] [blame] | 1 | // +build linux,!android |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 2 | |
| 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 |
| 8 | const int kMaxLEPayloadSize; |
Asim Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 9 | // The highest bluetooth channel that can be used for establishing RFCOMM |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 10 | // connections. |
Asim Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 11 | const int kMaxChannel; |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | |
| 13 | // Maximum number of local devices to scan over when a particular device isn't |
| 14 | // explicitly specified. |
| 15 | const 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 |
| 25 | char* 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. |
| 30 | char* bt_close_device(int dd); |
| 31 | |
Asim Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 32 | // Binds the given socket to the provided MAC address/channel. If '*local_address' |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 33 | // is NULL, it will bind to the first available bluetooth device and overwrite |
Asim Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 34 | // '*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 Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 38 | // 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 Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 42 | char* bt_bind(int sock, char** local_address, int* channel); |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 43 | |
| 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. |
| 51 | char* bt_accept(int sock, int* fd, char** remote_address); |
| 52 | |
Asim Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 53 | // Connects to the remote address/channel pair, using the provided local socket. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 54 | // Returns an error string if any error is encountered. If a non-NULL error |
| 55 | // string is returned, the caller must free it. |
Asim Shankar | 1375217 | 2014-07-09 11:29:07 -0700 | [diff] [blame] | 56 | char* bt_connect(int sock, const char* remote_address, int remote_channel); |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 57 | |
| 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. |
| 62 | char* 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. |
| 69 | char* 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. |
| 74 | char* 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. |
| 81 | char* bt_parse_le_meta_event( |
| 82 | void* data, char** remote_addr, char** remote_name, int* rssi, int* done); |