You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

685 lines
20 KiB

  1. #include "bluefruit_le.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <alloca.h>
  5. #include "debug.h"
  6. #include "timer.h"
  7. #include "gpio.h"
  8. #include "ringbuffer.hpp"
  9. #include <string.h>
  10. #include "spi_master.h"
  11. #include "wait.h"
  12. #include "analog.h"
  13. #include "progmem.h"
  14. // These are the pin assignments for the 32u4 boards.
  15. // You may define them to something else in your config.h
  16. // if yours is wired up differently.
  17. #ifndef BLUEFRUIT_LE_RST_PIN
  18. # define BLUEFRUIT_LE_RST_PIN D4
  19. #endif
  20. #ifndef BLUEFRUIT_LE_CS_PIN
  21. # define BLUEFRUIT_LE_CS_PIN B4
  22. #endif
  23. #ifndef BLUEFRUIT_LE_IRQ_PIN
  24. # define BLUEFRUIT_LE_IRQ_PIN E6
  25. #endif
  26. #ifndef BLUEFRUIT_LE_SCK_DIVISOR
  27. # define BLUEFRUIT_LE_SCK_DIVISOR 2 // 4MHz SCK/8MHz CPU, calculated for Feather 32U4 BLE
  28. #endif
  29. #define SAMPLE_BATTERY
  30. #define ConnectionUpdateInterval 1000 /* milliseconds */
  31. #ifndef BATTERY_LEVEL_PIN
  32. # define BATTERY_LEVEL_PIN B5
  33. #endif
  34. static struct {
  35. bool is_connected;
  36. bool initialized;
  37. bool configured;
  38. #define ProbedEvents 1
  39. #define UsingEvents 2
  40. bool event_flags;
  41. #ifdef SAMPLE_BATTERY
  42. uint16_t last_battery_update;
  43. uint32_t vbat;
  44. #endif
  45. uint16_t last_connection_update;
  46. } state;
  47. // Commands are encoded using SDEP and sent via SPI
  48. // https://github.com/adafruit/Adafruit_BluefruitLE_nRF51/blob/master/SDEP.md
  49. #define SdepMaxPayload 16
  50. struct sdep_msg {
  51. uint8_t type;
  52. uint8_t cmd_low;
  53. uint8_t cmd_high;
  54. struct __attribute__((packed)) {
  55. uint8_t len : 7;
  56. uint8_t more : 1;
  57. };
  58. uint8_t payload[SdepMaxPayload];
  59. } __attribute__((packed));
  60. // The recv latency is relatively high, so when we're hammering keys quickly,
  61. // we want to avoid waiting for the responses in the matrix loop. We maintain
  62. // a short queue for that. Since there is quite a lot of space overhead for
  63. // the AT command representation wrapped up in SDEP, we queue the minimal
  64. // information here.
  65. enum queue_type {
  66. QTKeyReport, // 1-byte modifier + 6-byte key report
  67. QTConsumer, // 16-bit key code
  68. QTMouseMove, // 4-byte mouse report
  69. };
  70. struct queue_item {
  71. enum queue_type queue_type;
  72. uint16_t added;
  73. union __attribute__((packed)) {
  74. struct __attribute__((packed)) {
  75. uint8_t modifier;
  76. uint8_t keys[6];
  77. } key;
  78. uint16_t consumer;
  79. struct __attribute__((packed)) {
  80. int8_t x, y, scroll, pan;
  81. uint8_t buttons;
  82. } mousemove;
  83. };
  84. };
  85. // Items that we wish to send
  86. static RingBuffer<queue_item, 40> send_buf;
  87. // Pending response; while pending, we can't send any more requests.
  88. // This records the time at which we sent the command for which we
  89. // are expecting a response.
  90. static RingBuffer<uint16_t, 2> resp_buf;
  91. static bool process_queue_item(struct queue_item *item, uint16_t timeout);
  92. enum sdep_type {
  93. SdepCommand = 0x10,
  94. SdepResponse = 0x20,
  95. SdepAlert = 0x40,
  96. SdepError = 0x80,
  97. SdepSlaveNotReady = 0xFE, // Try again later
  98. SdepSlaveOverflow = 0xFF, // You read more data than is available
  99. };
  100. enum ble_cmd {
  101. BleInitialize = 0xBEEF,
  102. BleAtWrapper = 0x0A00,
  103. BleUartTx = 0x0A01,
  104. BleUartRx = 0x0A02,
  105. };
  106. enum ble_system_event_bits {
  107. BleSystemConnected = 0,
  108. BleSystemDisconnected = 1,
  109. BleSystemUartRx = 8,
  110. BleSystemMidiRx = 10,
  111. };
  112. #define SdepTimeout 150 /* milliseconds */
  113. #define SdepShortTimeout 10 /* milliseconds */
  114. #define SdepBackOff 25 /* microseconds */
  115. #define BatteryUpdateInterval 10000 /* milliseconds */
  116. static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout = SdepTimeout);
  117. static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose = false);
  118. // Send a single SDEP packet
  119. static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) {
  120. spi_start(BLUEFRUIT_LE_CS_PIN, false, 0, BLUEFRUIT_LE_SCK_DIVISOR);
  121. uint16_t timerStart = timer_read();
  122. bool success = false;
  123. bool ready = false;
  124. do {
  125. ready = spi_write(msg->type) != SdepSlaveNotReady;
  126. if (ready) {
  127. break;
  128. }
  129. // Release it and let it initialize
  130. spi_stop();
  131. wait_us(SdepBackOff);
  132. spi_start(BLUEFRUIT_LE_CS_PIN, false, 0, BLUEFRUIT_LE_SCK_DIVISOR);
  133. } while (timer_elapsed(timerStart) < timeout);
  134. if (ready) {
  135. // Slave is ready; send the rest of the packet
  136. spi_transmit(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len);
  137. success = true;
  138. }
  139. spi_stop();
  140. return success;
  141. }
  142. static inline void sdep_build_pkt(struct sdep_msg *msg, uint16_t command, const uint8_t *payload, uint8_t len, bool moredata) {
  143. msg->type = SdepCommand;
  144. msg->cmd_low = command & 0xFF;
  145. msg->cmd_high = command >> 8;
  146. msg->len = len;
  147. msg->more = (moredata && len == SdepMaxPayload) ? 1 : 0;
  148. static_assert(sizeof(*msg) == 20, "msg is correctly packed");
  149. memcpy(msg->payload, payload, len);
  150. }
  151. // Read a single SDEP packet
  152. static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) {
  153. bool success = false;
  154. uint16_t timerStart = timer_read();
  155. bool ready = false;
  156. do {
  157. ready = readPin(BLUEFRUIT_LE_IRQ_PIN);
  158. if (ready) {
  159. break;
  160. }
  161. wait_us(1);
  162. } while (timer_elapsed(timerStart) < timeout);
  163. if (ready) {
  164. spi_start(BLUEFRUIT_LE_CS_PIN, false, 0, BLUEFRUIT_LE_SCK_DIVISOR);
  165. do {
  166. // Read the command type, waiting for the data to be ready
  167. msg->type = spi_read();
  168. if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) {
  169. // Release it and let it initialize
  170. spi_stop();
  171. wait_us(SdepBackOff);
  172. spi_start(BLUEFRUIT_LE_CS_PIN, false, 0, BLUEFRUIT_LE_SCK_DIVISOR);
  173. continue;
  174. }
  175. // Read the rest of the header
  176. spi_receive(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)));
  177. // and get the payload if there is any
  178. if (msg->len <= SdepMaxPayload) {
  179. spi_receive(msg->payload, msg->len);
  180. }
  181. success = true;
  182. break;
  183. } while (timer_elapsed(timerStart) < timeout);
  184. spi_stop();
  185. }
  186. return success;
  187. }
  188. static void resp_buf_read_one(bool greedy) {
  189. uint16_t last_send;
  190. if (!resp_buf.peek(last_send)) {
  191. return;
  192. }
  193. if (readPin(BLUEFRUIT_LE_IRQ_PIN)) {
  194. struct sdep_msg msg;
  195. again:
  196. if (sdep_recv_pkt(&msg, SdepTimeout)) {
  197. if (!msg.more) {
  198. // We got it; consume this entry
  199. resp_buf.get(last_send);
  200. dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send));
  201. }
  202. if (greedy && resp_buf.peek(last_send) && readPin(BLUEFRUIT_LE_IRQ_PIN)) {
  203. goto again;
  204. }
  205. }
  206. } else if (timer_elapsed(last_send) > SdepTimeout * 2) {
  207. dprintf("waiting_for_result: timeout, resp_buf size %d\n", (int)resp_buf.size());
  208. // Timed out: consume this entry
  209. resp_buf.get(last_send);
  210. }
  211. }
  212. static void send_buf_send_one(uint16_t timeout = SdepTimeout) {
  213. struct queue_item item;
  214. // Don't send anything more until we get an ACK
  215. if (!resp_buf.empty()) {
  216. return;
  217. }
  218. if (!send_buf.peek(item)) {
  219. return;
  220. }
  221. if (process_queue_item(&item, timeout)) {
  222. // commit that peek
  223. send_buf.get(item);
  224. dprintf("send_buf_send_one: have %d remaining\n", (int)send_buf.size());
  225. } else {
  226. dprint("failed to send, will retry\n");
  227. wait_ms(SdepTimeout);
  228. resp_buf_read_one(true);
  229. }
  230. }
  231. static void resp_buf_wait(const char *cmd) {
  232. bool didPrint = false;
  233. while (!resp_buf.empty()) {
  234. if (!didPrint) {
  235. dprintf("wait on buf for %s\n", cmd);
  236. didPrint = true;
  237. }
  238. resp_buf_read_one(true);
  239. }
  240. }
  241. void bluefruit_le_init(void) {
  242. state.initialized = false;
  243. state.configured = false;
  244. state.is_connected = false;
  245. setPinInput(BLUEFRUIT_LE_IRQ_PIN);
  246. spi_init();
  247. // Perform a hardware reset
  248. setPinOutput(BLUEFRUIT_LE_RST_PIN);
  249. writePinHigh(BLUEFRUIT_LE_RST_PIN);
  250. writePinLow(BLUEFRUIT_LE_RST_PIN);
  251. wait_ms(10);
  252. writePinHigh(BLUEFRUIT_LE_RST_PIN);
  253. wait_ms(1000); // Give it a second to initialize
  254. state.initialized = true;
  255. }
  256. static inline uint8_t min(uint8_t a, uint8_t b) {
  257. return a < b ? a : b;
  258. }
  259. static bool read_response(char *resp, uint16_t resplen, bool verbose) {
  260. char *dest = resp;
  261. char *end = dest + resplen;
  262. while (true) {
  263. struct sdep_msg msg;
  264. if (!sdep_recv_pkt(&msg, 2 * SdepTimeout)) {
  265. dprint("sdep_recv_pkt failed\n");
  266. return false;
  267. }
  268. if (msg.type != SdepResponse) {
  269. *resp = 0;
  270. return false;
  271. }
  272. uint8_t len = min(msg.len, end - dest);
  273. if (len > 0) {
  274. memcpy(dest, msg.payload, len);
  275. dest += len;
  276. }
  277. if (!msg.more) {
  278. // No more data is expected!
  279. break;
  280. }
  281. }
  282. // Ensure the response is NUL terminated
  283. *dest = 0;
  284. // "Parse" the result text; we want to snip off the trailing OK or ERROR line
  285. // Rewind past the possible trailing CRLF so that we can strip it
  286. --dest;
  287. while (dest > resp && (dest[0] == '\n' || dest[0] == '\r')) {
  288. *dest = 0;
  289. --dest;
  290. }
  291. // Look back for start of preceeding line
  292. char *last_line = strrchr(resp, '\n');
  293. if (last_line) {
  294. ++last_line;
  295. } else {
  296. last_line = resp;
  297. }
  298. bool success = false;
  299. static const char kOK[] PROGMEM = "OK";
  300. success = !strcmp_P(last_line, kOK);
  301. if (verbose || !success) {
  302. dprintf("result: %s\n", resp);
  303. }
  304. return success;
  305. }
  306. static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout) {
  307. const char * end = cmd + strlen(cmd);
  308. struct sdep_msg msg;
  309. if (verbose) {
  310. dprintf("ble send: %s\n", cmd);
  311. }
  312. if (resp) {
  313. // They want to decode the response, so we need to flush and wait
  314. // for all pending I/O to finish before we start this one, so
  315. // that we don't confuse the results
  316. resp_buf_wait(cmd);
  317. *resp = 0;
  318. }
  319. // Fragment the command into a series of SDEP packets
  320. while (end - cmd > SdepMaxPayload) {
  321. sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, SdepMaxPayload, true);
  322. if (!sdep_send_pkt(&msg, timeout)) {
  323. return false;
  324. }
  325. cmd += SdepMaxPayload;
  326. }
  327. sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, end - cmd, false);
  328. if (!sdep_send_pkt(&msg, timeout)) {
  329. return false;
  330. }
  331. if (resp == NULL) {
  332. uint16_t now = timer_read();
  333. while (!resp_buf.enqueue(now)) {
  334. resp_buf_read_one(false);
  335. }
  336. uint16_t later = timer_read();
  337. if (TIMER_DIFF_16(later, now) > 0) {
  338. dprintf("waited %dms for resp_buf\n", TIMER_DIFF_16(later, now));
  339. }
  340. return true;
  341. }
  342. return read_response(resp, resplen, verbose);
  343. }
  344. bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose) {
  345. char *cmdbuf = (char *)alloca(strlen_P(cmd) + 1);
  346. strcpy_P(cmdbuf, cmd);
  347. return at_command(cmdbuf, resp, resplen, verbose);
  348. }
  349. bool bluefruit_le_is_connected(void) {
  350. return state.is_connected;
  351. }
  352. bool bluefruit_le_enable_keyboard(void) {
  353. char resbuf[128];
  354. if (!state.initialized) {
  355. return false;
  356. }
  357. state.configured = false;
  358. // Disable command echo
  359. static const char kEcho[] PROGMEM = "ATE=0";
  360. // Make the advertised name match the keyboard
  361. static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" PRODUCT;
  362. // Turn on keyboard support
  363. static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1";
  364. // Adjust intervals to improve latency. This causes the "central"
  365. // system (computer/tablet) to poll us every 10-30 ms. We can't
  366. // set a smaller value than 10ms, and 30ms seems to be the natural
  367. // processing time on my macbook. Keeping it constrained to that
  368. // feels reasonable to type to.
  369. static const char kGapIntervals[] PROGMEM = "AT+GAPINTERVALS=10,30,,";
  370. // Reset the device so that it picks up the above changes
  371. static const char kATZ[] PROGMEM = "ATZ";
  372. // Turn down the power level a bit
  373. static const char kPower[] PROGMEM = "AT+BLEPOWERLEVEL=-12";
  374. static PGM_P const configure_commands[] PROGMEM = {
  375. kEcho, kGapIntervals, kGapDevName, kHidEnOn, kPower, kATZ,
  376. };
  377. uint8_t i;
  378. for (i = 0; i < sizeof(configure_commands) / sizeof(configure_commands[0]); ++i) {
  379. PGM_P cmd;
  380. memcpy_P(&cmd, configure_commands + i, sizeof(cmd));
  381. if (!at_command_P(cmd, resbuf, sizeof(resbuf))) {
  382. dprintf("failed BLE command: %S: %s\n", cmd, resbuf);
  383. goto fail;
  384. }
  385. }
  386. state.configured = true;
  387. // Check connection status in a little while; allow the ATZ time
  388. // to kick in.
  389. state.last_connection_update = timer_read();
  390. fail:
  391. return state.configured;
  392. }
  393. static void set_connected(bool connected) {
  394. if (connected != state.is_connected) {
  395. if (connected) {
  396. dprint("BLE connected\n");
  397. } else {
  398. dprint("BLE disconnected\n");
  399. }
  400. state.is_connected = connected;
  401. // TODO: if modifiers are down on the USB interface and
  402. // we cut over to BLE or vice versa, they will remain stuck.
  403. // This feels like a good point to do something like clearing
  404. // the keyboard and/or generating a fake all keys up message.
  405. // However, I've noticed that it takes a couple of seconds
  406. // for macOS to to start recognizing key presses after BLE
  407. // is in the connected state, so I worry that doing that
  408. // here may not be good enough.
  409. }
  410. }
  411. void bluefruit_le_task(void) {
  412. char resbuf[48];
  413. if (!state.configured && !bluefruit_le_enable_keyboard()) {
  414. return;
  415. }
  416. resp_buf_read_one(true);
  417. send_buf_send_one(SdepShortTimeout);
  418. if (resp_buf.empty() && (state.event_flags & UsingEvents) && readPin(BLUEFRUIT_LE_IRQ_PIN)) {
  419. // Must be an event update
  420. if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) {
  421. uint32_t mask = strtoul(resbuf, NULL, 16);
  422. if (mask & BleSystemConnected) {
  423. set_connected(true);
  424. } else if (mask & BleSystemDisconnected) {
  425. set_connected(false);
  426. }
  427. }
  428. }
  429. if (timer_elapsed(state.last_connection_update) > ConnectionUpdateInterval) {
  430. bool shouldPoll = true;
  431. if (!(state.event_flags & ProbedEvents)) {
  432. // Request notifications about connection status changes.
  433. // This only works in SPIFRIEND firmware > 0.6.7, which is why
  434. // we check for this conditionally here.
  435. // Note that at the time of writing, HID reports only work correctly
  436. // with Apple products on firmware version 0.6.7!
  437. // https://forums.adafruit.com/viewtopic.php?f=8&t=104052
  438. if (at_command_P(PSTR("AT+EVENTENABLE=0x1"), resbuf, sizeof(resbuf))) {
  439. at_command_P(PSTR("AT+EVENTENABLE=0x2"), resbuf, sizeof(resbuf));
  440. state.event_flags |= UsingEvents;
  441. }
  442. state.event_flags |= ProbedEvents;
  443. // leave shouldPoll == true so that we check at least once
  444. // before relying solely on events
  445. } else {
  446. shouldPoll = false;
  447. }
  448. static const char kGetConn[] PROGMEM = "AT+GAPGETCONN";
  449. state.last_connection_update = timer_read();
  450. if (at_command_P(kGetConn, resbuf, sizeof(resbuf))) {
  451. set_connected(atoi(resbuf));
  452. }
  453. }
  454. #ifdef SAMPLE_BATTERY
  455. if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) {
  456. state.last_battery_update = timer_read();
  457. state.vbat = analogReadPin(BATTERY_LEVEL_PIN);
  458. }
  459. #endif
  460. }
  461. static bool process_queue_item(struct queue_item *item, uint16_t timeout) {
  462. char cmdbuf[48];
  463. char fmtbuf[64];
  464. // Arrange to re-check connection after keys have settled
  465. state.last_connection_update = timer_read();
  466. #if 1
  467. if (TIMER_DIFF_16(state.last_connection_update, item->added) > 0) {
  468. dprintf("send latency %dms\n", TIMER_DIFF_16(state.last_connection_update, item->added));
  469. }
  470. #endif
  471. switch (item->queue_type) {
  472. case QTKeyReport:
  473. strcpy_P(fmtbuf, PSTR("AT+BLEKEYBOARDCODE=%02x-00-%02x-%02x-%02x-%02x-%02x-%02x"));
  474. snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->key.modifier, item->key.keys[0], item->key.keys[1], item->key.keys[2], item->key.keys[3], item->key.keys[4], item->key.keys[5]);
  475. return at_command(cmdbuf, NULL, 0, true, timeout);
  476. #ifdef EXTRAKEY_ENABLE
  477. case QTConsumer:
  478. strcpy_P(fmtbuf, PSTR("AT+BLEHIDCONTROLKEY=0x%04x"));
  479. snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->consumer);
  480. return at_command(cmdbuf, NULL, 0, true, timeout);
  481. #endif
  482. #ifdef MOUSE_ENABLE
  483. case QTMouseMove:
  484. strcpy_P(fmtbuf, PSTR("AT+BLEHIDMOUSEMOVE=%d,%d,%d,%d"));
  485. snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->mousemove.x, item->mousemove.y, item->mousemove.scroll, item->mousemove.pan);
  486. if (!at_command(cmdbuf, NULL, 0, true, timeout)) {
  487. return false;
  488. }
  489. strcpy_P(cmdbuf, PSTR("AT+BLEHIDMOUSEBUTTON="));
  490. if (item->mousemove.buttons & MOUSE_BTN1) {
  491. strcat(cmdbuf, "L");
  492. }
  493. if (item->mousemove.buttons & MOUSE_BTN2) {
  494. strcat(cmdbuf, "R");
  495. }
  496. if (item->mousemove.buttons & MOUSE_BTN3) {
  497. strcat(cmdbuf, "M");
  498. }
  499. if (item->mousemove.buttons == 0) {
  500. strcat(cmdbuf, "0");
  501. }
  502. return at_command(cmdbuf, NULL, 0, true, timeout);
  503. #endif
  504. default:
  505. return true;
  506. }
  507. }
  508. void bluefruit_le_send_keyboard(report_keyboard_t *report) {
  509. struct queue_item item;
  510. item.queue_type = QTKeyReport;
  511. item.key.modifier = report->mods;
  512. item.key.keys[0] = report->keys[0];
  513. item.key.keys[1] = report->keys[1];
  514. item.key.keys[2] = report->keys[2];
  515. item.key.keys[3] = report->keys[3];
  516. item.key.keys[4] = report->keys[4];
  517. item.key.keys[5] = report->keys[5];
  518. while (!send_buf.enqueue(item)) {
  519. send_buf_send_one();
  520. }
  521. }
  522. void bluefruit_le_send_consumer(uint16_t usage) {
  523. struct queue_item item;
  524. item.queue_type = QTConsumer;
  525. item.consumer = usage;
  526. while (!send_buf.enqueue(item)) {
  527. send_buf_send_one();
  528. }
  529. }
  530. void bluefruit_le_send_mouse(report_mouse_t *report) {
  531. struct queue_item item;
  532. item.queue_type = QTMouseMove;
  533. item.mousemove.x = report->x;
  534. item.mousemove.y = report->y;
  535. item.mousemove.scroll = report->v;
  536. item.mousemove.pan = report->h;
  537. item.mousemove.buttons = report->buttons;
  538. while (!send_buf.enqueue(item)) {
  539. send_buf_send_one();
  540. }
  541. }
  542. uint32_t bluefruit_le_read_battery_voltage(void) {
  543. return state.vbat;
  544. }
  545. bool bluefruit_le_set_mode_leds(bool on) {
  546. if (!state.configured) {
  547. return false;
  548. }
  549. // The "mode" led is the red blinky one
  550. at_command_P(on ? PSTR("AT+HWMODELED=1") : PSTR("AT+HWMODELED=0"), NULL, 0);
  551. // Pin 19 is the blue "connected" LED; turn that off too.
  552. // When turning LEDs back on, don't turn that LED on if we're
  553. // not connected, as that would be confusing.
  554. at_command_P(on && state.is_connected ? PSTR("AT+HWGPIO=19,1") : PSTR("AT+HWGPIO=19,0"), NULL, 0);
  555. return true;
  556. }
  557. // https://learn.adafruit.com/adafruit-feather-32u4-bluefruit-le/ble-generic#at-plus-blepowerlevel
  558. bool bluefruit_le_set_power_level(int8_t level) {
  559. char cmd[46];
  560. if (!state.configured) {
  561. return false;
  562. }
  563. snprintf(cmd, sizeof(cmd), "AT+BLEPOWERLEVEL=%d", level);
  564. return at_command(cmd, NULL, 0, false);
  565. }