|
| 1 | +#ifdef linux |
| 2 | +#include "ruby.h" |
| 3 | +#include <stdio.h> |
| 4 | +#include <errno.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <sys/ioctl.h> |
| 7 | +#include <sys/socket.h> |
| 8 | +#include <bluetooth/bluetooth.h> |
| 9 | +#include <bluetooth/hci.h> |
| 10 | +#include <bluetooth/hci_lib.h> |
| 11 | +#pragma pack(1) |
| 12 | + |
| 13 | + |
| 14 | +#define FLAGS_NOT_CONNECTABLE 0x1A |
| 15 | +#define FLAGS_CONNECTABLE 0x18 |
| 16 | + |
| 17 | +typedef struct { |
| 18 | + uint8_t len; |
| 19 | + struct { |
| 20 | + uint8_t len; |
| 21 | + uint8_t type; |
| 22 | + uint8_t data; |
| 23 | + } flags; |
| 24 | + uint8_t ad_data[28]; |
| 25 | +} Advertisement; |
| 26 | + |
| 27 | +Advertisement advertisement; |
| 28 | +int advertising; |
| 29 | + |
| 30 | +VALUE method_start_advertising(); |
| 31 | + |
| 32 | +void init_advertisement() |
| 33 | +{ |
| 34 | + memset(&advertisement, 0, sizeof(advertisement)); |
| 35 | + advertisement.flags.len = 2; |
| 36 | + advertisement.flags.type = 1; |
| 37 | + advertisement.flags.data = FLAGS_NOT_CONNECTABLE; |
| 38 | + advertising = 0; |
| 39 | +} |
| 40 | + |
| 41 | +VALUE method_set_connectable(VALUE self, VALUE connectable) |
| 42 | +{ |
| 43 | + if ( RTEST(connectable) ) |
| 44 | + { |
| 45 | + advertisement.flags.data = FLAGS_CONNECTABLE; |
| 46 | + } else { |
| 47 | + advertisement.flags.data = FLAGS_NOT_CONNECTABLE; |
| 48 | + }; |
| 49 | + if (advertising) { |
| 50 | + method_start_advertising(); |
| 51 | + } |
| 52 | + return connectable; |
| 53 | +} |
| 54 | + |
| 55 | +VALUE method_set_advertisement_bytes(VALUE self, VALUE bytes) |
| 56 | +{ |
| 57 | + uint8_t len = RSTRING_LEN(bytes); |
| 58 | + if (len > 28) { |
| 59 | + len = 28; |
| 60 | + } |
| 61 | + advertisement.len = len + advertisement.flags.len + 1; // + 1 is to account for the flags length field |
| 62 | + memcpy(advertisement.ad_data, RSTRING_PTR(bytes), len); |
| 63 | + if (advertising) { |
| 64 | + method_start_advertising(); |
| 65 | + } |
| 66 | + return bytes; |
| 67 | +} |
| 68 | + |
| 69 | +VALUE method_start_advertising() |
| 70 | +{ |
| 71 | + struct hci_request rq; |
| 72 | + le_set_advertising_parameters_cp adv_params_cp; |
| 73 | + uint8_t status; |
| 74 | + |
| 75 | + // open connection to the device |
| 76 | + int device_id = hci_get_route(NULL); |
| 77 | + int device_handle = hci_open_dev(device_id); |
| 78 | + |
| 79 | + // set advertising data |
| 80 | + memset(&rq, 0, sizeof(rq)); |
| 81 | + rq.ogf = OGF_LE_CTL; |
| 82 | + rq.ocf = OCF_LE_SET_ADVERTISING_DATA; |
| 83 | + rq.cparam = &advertisement; |
| 84 | + rq.clen = sizeof(advertisement); |
| 85 | + rq.rparam = &status; |
| 86 | + rq.rlen = 1; |
| 87 | + hci_send_req(device_handle, &rq, 1000); |
| 88 | + |
| 89 | + // set advertising params |
| 90 | + memset(&adv_params_cp, 0, sizeof(adv_params_cp)); |
| 91 | + uint16_t interval_100ms = htobs(0x00A0); // 0xA0 * 0.625ms = 100ms |
| 92 | + adv_params_cp.min_interval = interval_100ms; |
| 93 | + adv_params_cp.max_interval = interval_100ms; |
| 94 | + adv_params_cp.advtype = 0x03; // non-connectable undirected advertising |
| 95 | + adv_params_cp.chan_map = 0x07;// all 3 channels |
| 96 | + memset(&rq, 0, sizeof(rq)); |
| 97 | + rq.ogf = OGF_LE_CTL; |
| 98 | + rq.ocf = OCF_LE_SET_ADVERTISING_PARAMETERS; |
| 99 | + rq.cparam = &adv_params_cp; |
| 100 | + rq.clen = LE_SET_ADVERTISING_PARAMETERS_CP_SIZE; |
| 101 | + rq.rparam = &status; |
| 102 | + rq.rlen = 1; |
| 103 | + hci_send_req(device_handle, &rq, 1000); |
| 104 | + |
| 105 | + // turn on advertising |
| 106 | + hci_le_set_advertise_enable(device_handle, 0x01, 1000); |
| 107 | + |
| 108 | + // and close the connection |
| 109 | + hci_close_dev(device_handle); |
| 110 | + advertising = 1; |
| 111 | + return Qnil; |
| 112 | +} |
| 113 | + |
| 114 | +VALUE method_stop_advertising() |
| 115 | +{ |
| 116 | + int device_id = hci_get_route(NULL); |
| 117 | + int device_handle = hci_open_dev(device_id); |
| 118 | + hci_le_set_advertise_enable(device_handle, 0x00, 1000); |
| 119 | + hci_close_dev(device_handle); |
| 120 | + advertising = 0; |
| 121 | + return Qnil; |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +#endif // linux |
0 commit comments