#include <cstdio>
#include <stdint.h>
#include <cstring>
#include <string.h>
#include <string>
void onDataReceived(const uint8_t* data, size_t len);
class DummyBleCharacteristic {
public:
int setValue(const uint8_t* buf, size_t len) {
if(len>BUFFER_SIZE) {
return -1;
}
memcpy(mBuffer,buf,len);
onDataReceived(mBuffer,len);
return len;
}
int setValue(const std::string& str) {
return setValue(reinterpret_cast<const uint8_t*>(str.c_str()), str.length());
}
int setValue(const char* str) {
return setValue(reinterpret_cast<const uint8_t*>(str), strlen(str));
}
private:
static constexpr size_t BUFFER_SIZE = 100;
uint8_t mBuffer[BUFFER_SIZE];
};
void onFrontDoorOpen() {
printf("onFrontDoorOpen\n");
}
void onFrontDoorClosed() {
printf("onFrontDoorClosed!\n");
}
void onDataReceived(const uint8_t* data, size_t len) {
const char* dataAsCharArray = (const char*)data;
if(strncmp("FrontDoor, Open!", dataAsCharArray, len) == 0) {
onFrontDoorOpen();
return;
}
if(strncmp("FrontDoor, Closed!", dataAsCharArray, len) == 0) {
onFrontDoorClosed();
return;
}
printf("received unknown message: %.*s\n",(int)len,dataAsCharArray);
}
int main()
{
const char* one = "FrontDoor, Open!";
const char* two = "FrontDoor, Closed!";
const char* three = "Just a Text";
uint8_t four[] = {'A','B','C'};
uint8_t five[] = "FrontDoor, Open!";
DummyBleCharacteristic characteristic;
printf("one:\n");
characteristic.setValue(one);
printf("two:\n");
characteristic.setValue(two);
printf("three:\n");
characteristic.setValue(three);
printf("four:\n");
characteristic.setValue(four,sizeof(four)/sizeof(four[0]));
printf("five:\n");
characteristic.setValue(five,sizeof(five)/sizeof(five[0]));
return 0;
}