|
| 1 | + |
| 2 | +#include <stdio.h> |
| 3 | +#include "Adafruit_BluefruitLE_GATT.h" |
| 4 | + |
| 5 | +#define RED_LED_PIN 13 |
| 6 | + |
| 7 | +const char fmt_gapdevname[] PROGMEM = "AT+GAPDEVNAME=%s"; |
| 8 | +const char fmt_gattaddservice[] PROGMEM = "AT+GATTADDSERVICE=UUID128=%s"; |
| 9 | +const char fmt_gattaddchar[] PROGMEM = "AT+GATTADDCHAR=UUID=%#4X,PROPERTIES=%#2X,MIN_LEN=%i,MAX_LEN=%i,VALUE=%s"; |
| 10 | +const char fmt_gattsetchar[] PROGMEM = "AT+GATTCHAR=%i,%s"; |
| 11 | +const char fmt_gattgetchar[] PROGMEM = "AT+GATTCHAR=%i"; |
| 12 | +const char fmt_bintohex[] PROGMEM = "%02X-"; |
| 13 | + |
| 14 | +void Adafruit_BluefruitLE_GATT::assertOK(boolean condition, const __FlashStringHelper *err) { |
| 15 | + if (condition) return; |
| 16 | + |
| 17 | + Serial.print(F("### S.O.S. ### ")); |
| 18 | + Serial.println(err); |
| 19 | + Serial.flush(); |
| 20 | + int pulse = 300; // [ms] |
| 21 | + while(1) { |
| 22 | + // S.O.S. |
| 23 | + pulse = (pulse + 200) % 400; // toggles between 100 and 300 ms |
| 24 | + delay(pulse); |
| 25 | + for(byte i=0; i<3; i++) { |
| 26 | + digitalWrite(RED_LED_PIN, HIGH); |
| 27 | + delay(pulse); |
| 28 | + digitalWrite(RED_LED_PIN, LOW); |
| 29 | + delay(pulse); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void Adafruit_BluefruitLE_GATT::setGattDeviceName(const char *name) { |
| 35 | + char cmd[strlen_P(fmt_gapdevname) + strlen(name) + 1]; |
| 36 | + sprintf_P(cmd, fmt_gapdevname, name); |
| 37 | + assertOK(sendCommandCheckOK(cmd), F("Could not set device name?")); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +int8_t Adafruit_BluefruitLE_GATT::addGattService(const char *uuid128) { |
| 42 | + char cmd[strlen_P(fmt_gattaddservice) + strlen(uuid128) + 1]; |
| 43 | + sprintf_P(cmd, fmt_gattaddservice, uuid128); |
| 44 | + int32_t pos; |
| 45 | + assertOK(sendCommandWithIntReply(cmd, &pos), F("Could not add service")); |
| 46 | + return (int8_t) pos; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +int8_t Adafruit_BluefruitLE_GATT::addGattCharacteristic(uint16_t uuid16, CharacteristicProperties props, uint8_t minLen, uint8_t maxLen) { |
| 51 | + char zeros[maxLen*3]; |
| 52 | + for(uint8_t i=0; i<maxLen; i++) { |
| 53 | + zeros[i*3] = '0'; |
| 54 | + zeros[i*3+1] = '0'; |
| 55 | + zeros[i*3+2] = '-'; |
| 56 | + } |
| 57 | + zeros[maxLen*3-1] = '\0'; // replace the last '-' by '\0' |
| 58 | + |
| 59 | + char cmd[strlen_P(fmt_gattaddchar) + 2 + 1 + 1 + maxLen*3]; |
| 60 | + sprintf_P(cmd, fmt_gattaddchar, uuid16, props, minLen, maxLen, zeros); |
| 61 | + int32_t pos; |
| 62 | + assertOK(sendCommandWithIntReply(cmd, &pos), F("Could not add characteristic")); |
| 63 | + return (int8_t) pos; |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +void Adafruit_BluefruitLE_GATT::setGattCharacteristicValue(int8_t id, byte *value, uint16_t len) { |
| 68 | + if (len == 0) return; |
| 69 | + |
| 70 | + // AT+GATTCHAR takes each byte in hex separated by a dash, e.g. 4 bytes: xx-xx-xx-xx (= 11 characters) |
| 71 | + char str[len*3 + 1]; // +1 caters for terminating '\0' after each 'xx-' group |
| 72 | + for (uint16_t i=0; i<len; i++) { |
| 73 | + sprintf_P(&str[i*3], fmt_bintohex, value[i]); |
| 74 | + } |
| 75 | + str[len*3 - 1] = '\0'; // replace the last '-' by '\0' |
| 76 | + |
| 77 | + char cmd[strlen_P(fmt_gattsetchar) + len*3]; |
| 78 | + sprintf_P(cmd, fmt_gattsetchar, id, str); |
| 79 | + assertOK(sendCommandCheckOK(cmd), F("Could not set characteristic value")); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +void Adafruit_BluefruitLE_GATT::setGattCharacteristicValue(int8_t id, int16_t value) { |
| 84 | + byte bytes[sizeof(int16_t)]; |
| 85 | + memcpy(bytes, &value, sizeof(int16_t)); |
| 86 | + reverseBytes(bytes, sizeof(int16_t)); |
| 87 | + setGattCharacteristicValue(id, bytes, sizeof(int16_t)); |
| 88 | +} |
| 89 | +void Adafruit_BluefruitLE_GATT::setGattCharacteristicValue(int8_t id, uint16_t value) { |
| 90 | + byte bytes[sizeof(uint16_t)]; |
| 91 | + memcpy(bytes, &value, sizeof(uint16_t)); |
| 92 | + reverseBytes(bytes, sizeof(uint16_t)); |
| 93 | + setGattCharacteristicValue(id, bytes, sizeof(uint16_t)); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +void Adafruit_BluefruitLE_GATT::setGattCharacteristicValue(int8_t id, int32_t value) { |
| 98 | + byte bytes[sizeof(int32_t)]; |
| 99 | + memcpy(bytes, &value, sizeof(int32_t)); |
| 100 | + reverseBytes(bytes, sizeof(int32_t)); |
| 101 | + setGattCharacteristicValue(id, bytes, sizeof(int32_t)); |
| 102 | +} |
| 103 | + |
| 104 | +void Adafruit_BluefruitLE_GATT::setGattCharacteristicValue(int8_t id, uint32_t value) { |
| 105 | + byte bytes[sizeof(uint32_t)]; |
| 106 | + memcpy(bytes, &value, sizeof(uint32_t)); |
| 107 | + reverseBytes(bytes, sizeof(uint32_t)); |
| 108 | + setGattCharacteristicValue(id, bytes, sizeof(uint32_t)); |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +void Adafruit_BluefruitLE_GATT::setGattCharacteristicValue(int8_t id, float value) { |
| 113 | + byte bytes[sizeof(float)]; |
| 114 | + memcpy(bytes, &value, sizeof(float)); |
| 115 | + setGattCharacteristicValue(id, bytes, sizeof(float)); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +uint16_t Adafruit_BluefruitLE_GATT::getGattCharacteristicValue(int8_t id, byte *reply, uint16_t maxLen) { |
| 120 | + char cmd[strlen_P(fmt_gattgetchar) + 2 + 1]; |
| 121 | + sprintf_P(cmd, fmt_gattgetchar, id); |
| 122 | + |
| 123 | + // AT+GATTCHAR returns each byte in hex separated by a dash, e.g. 4 bytes: xx-xx-xx-xx (= 11 characters) |
| 124 | + char replyStr[maxLen*3]; // includes terminating '\0' |
| 125 | + uint16_t strLen; |
| 126 | + assertOK(sendCommandWithStringReply(cmd, replyStr, &strLen), F("Could not get characteristic value")); |
| 127 | + |
| 128 | + if (strLen == 0 || (strLen + 1) % 3 != 0) { |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + // Parse dash-separated hex string into bytes: |
| 133 | + uint16_t numBytes = min((strLen + 1) / 3, maxLen); |
| 134 | + for(uint16_t i=0; i<numBytes; i++) { |
| 135 | + reply[i] = (byte) strtol(&replyStr[i*3], NULL, 16); |
| 136 | + } |
| 137 | + return numBytes; |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +void Adafruit_BluefruitLE_GATT::getGattCharacteristicValue(int8_t id, int16_t *reply) { |
| 142 | + byte bytes[sizeof(int16_t)]; |
| 143 | + getGattCharacteristicValue(id, bytes, sizeof(int16_t)); |
| 144 | + reverseBytes(bytes, sizeof(int16_t)); |
| 145 | + memcpy(reply, bytes, sizeof(int16_t)); |
| 146 | +} |
| 147 | + |
| 148 | +void Adafruit_BluefruitLE_GATT::getGattCharacteristicValue(int8_t id, uint16_t *reply) { |
| 149 | + byte bytes[sizeof(uint16_t)]; |
| 150 | + getGattCharacteristicValue(id, bytes, sizeof(uint16_t)); |
| 151 | + reverseBytes(bytes, sizeof(uint16_t)); |
| 152 | + memcpy(reply, bytes, sizeof(uint16_t)); |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +void Adafruit_BluefruitLE_GATT::getGattCharacteristicValue(int8_t id, int32_t *reply) { |
| 157 | + byte bytes[sizeof(int32_t)]; |
| 158 | + getGattCharacteristicValue(id, bytes, sizeof(int32_t)); |
| 159 | + reverseBytes(bytes, sizeof(int32_t)); |
| 160 | + memcpy(reply, bytes, sizeof(int32_t)); |
| 161 | +} |
| 162 | + |
| 163 | +void Adafruit_BluefruitLE_GATT::getGattCharacteristicValue(int8_t id, uint32_t *reply) { |
| 164 | + byte bytes[sizeof(uint32_t)]; |
| 165 | + getGattCharacteristicValue(id, bytes, sizeof(uint32_t)); |
| 166 | + reverseBytes(bytes, sizeof(uint32_t)); |
| 167 | + memcpy(reply, bytes, sizeof(uint32_t)); |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +void Adafruit_BluefruitLE_GATT::getGattCharacteristicValue(int8_t id, float *reply) { |
| 172 | + byte bytes[sizeof(float)]; |
| 173 | + getGattCharacteristicValue(id, bytes, sizeof(float)); |
| 174 | + // don't reverse bytes! |
| 175 | + memcpy(reply, bytes, sizeof(float)); |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +bool Adafruit_BluefruitLE_GATT::sendCommandWithStringReply(const char cmd[], char *reply, uint16_t *len) { |
| 180 | + bool success; |
| 181 | + uint8_t current_mode = _mode; |
| 182 | + |
| 183 | + // switch mode if necessary to execute command |
| 184 | + if (current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_COMMAND); |
| 185 | + |
| 186 | + println(cmd); // send command |
| 187 | + if (_verbose) { |
| 188 | + SerialDebug.print( F("\n<- ") ); |
| 189 | + } |
| 190 | + (*len) = readline(); |
| 191 | + memcpy(reply, buffer, *len); |
| 192 | + success = waitForOK(); |
| 193 | + |
| 194 | + // switch back if necessary |
| 195 | + if (current_mode == BLUEFRUIT_MODE_DATA ) setMode(BLUEFRUIT_MODE_DATA); |
| 196 | + |
| 197 | + return success; |
| 198 | +} |
| 199 | + |
| 200 | + |
| 201 | +void reverseBytes(byte *buf, uint16_t len) { |
| 202 | + for(uint16_t i=0; i<len/2; i++) { |
| 203 | + byte b = buf[i]; |
| 204 | + buf[i] = buf[len-1-i]; |
| 205 | + buf[len-1-i] = b; |
| 206 | + } |
| 207 | +} |
0 commit comments