How should I reference the CustomDataService object in my application?
This is what I currently have:
custom_data_service.h:
#ifndef CUSTOM_DATA_SERVICE_H_
#define CUSTOM_DATA_SERVICE_H_
#ifdef __cplusplus
#include <stdint.h>
#include <stdlib.h>
#include "data_service.h"
//#include "nrf_gpio.h"
class CustomDataService : public DataService
{
public:
static CustomDataService* instance();
//DataService functions
virtual int32_t getServiceID();
virtual int32_t DataCallback(uint8_t *data, int16_t length);
int32_t availableBytes;
private:
//this is a singleton class, so these all need to be private so they can't be called
CustomDataService(){
availableBytes = 0;
};
CustomDataService(CustomDataService const&){
availableBytes = 0;
};
CustomDataService& operator=(CustomDataService const&);
static CustomDataService* m_pInstance;
};
#endif
#endif /* CUSTOM_DATA_SERVICE_H_ */
custom_data_service.cpp:
#include <string.h>
#include "custom_data_service.h"
#include "data_management_layer.h"
#include "registered_data_services.h"
#include "deviceid_hal.h"
CustomDataService* CustomDataService::m_pInstance = NULL;
CustomDataService* CustomDataService::instance()
{
if (!m_pInstance) // Only allow one instance of class to be generated.
m_pInstance = new CustomDataService;
return m_pInstance;
}
//DataService functions
int32_t CustomDataService::getServiceID()
{
return CUSTOM_DATA_SERVICE;
}
int32_t CustomDataService::DataCallback(uint8_t *data, int16_t length)
{
switch (data[0]) {
case 0:
availableBytes +=length;
uint8_t id[12];
HAL_device_ID(id, 12);
uint8_t rsp[13];
rsp[0] = CUSTOM_DATA_SERVICE & 0xFF;
memcpy(rsp+1, id, 12);
DataManagementLayer::sendData(13, rsp);
break;
}
return 1;
}
application.cpp:
/* Includes ------------------------------------------------------------------*/
#include "application.h"
SYSTEM_MODE(AUTOMATIC);
int LED = D7;
CustomDataService *cData;
/* This function is called once at start up ----------------------------------*/
void setup()
{
//Setup the Tinker application here
//Register all the Tinker functions
// Particle.function("digitalread", tinkerDigitalRead);
// Particle.function("digitalwrite", tinkerDigitalWrite);
//
// Particle.function("analogread", tinkerAnalogRead);
// Particle.function("analogwrite", tinkerAnalogWrite);
cData = CustomDataService::instance();
pinMode(LED, OUTPUT);
Serial1.begin(115200);
}
/* This function loops forever --------------------------------------------*/
void loop()
{
// System.sleep(SLEEP_MODE_CPU);
//This will run in a loop
Serial1.print("availableBytes: ");
Serial1.println(cData->availableBytes);
delay(1000);
}
availableBytes always prints to the log as 0 even though I am absolutely certain that the DataCallback is firing and it is receiving 1 byte each time it fires. Am I referencing my object of the CustomDataService class incorrectly?