#include <cstdio>
#include <stdint.h>
#include <string.h>
void onFrontDoorOpen() {
printf("onFrontDoorOpen\n");
}
void onFrontDoorClosed() {
printf("onFrontDoorClosed!\n");
}
void onDataReceived(const uint8_t* data, size_t len) {
const char* dataString = (const char*)data;
if(strnlen(dataString,len)==len) {
printf("received data is not a null terminated string!\n");
return; // data is not a null terminated string !
}
if(strcmp("FrontDoor, Open!", dataString) == 0) {
onFrontDoorOpen();
return;
}
if(strcmp("FrontDoor, Closed!", dataString) == 0) {
onFrontDoorClosed();
return;
}
printf("received unknown message: %s\n",dataString);
}
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!";
printf("one:\n");
onDataReceived((const uint8_t*)one,strlen(one)+1);
printf("two:\n");
onDataReceived((const uint8_t*)two,strlen(two)+1);
printf("three:\n");
onDataReceived((const uint8_t*)three,strlen(three)+1);
printf("four:\n");
onDataReceived(four,sizeof(four)/sizeof(four[0]));
printf("five:\n");
onDataReceived(five,sizeof(five)/sizeof(five[0]));
return 0;
}