#include "logger.h"
#include "ocstack.h"
+#include "ocpayload.h"
#include <string.h>
#ifdef ARDUINOWIFI
static LightResource Light;
-static char responsePayloadGet[] = "{\"href\":\"/a/light\",\"rep\":{\"state\":\"on\",\"power\":10}}";
-static char responsePayloadPut[] = "{\"href\":\"/a/light\",\"rep\":{\"state\":\"off\",\"power\":0}}";
-
-/// This is the port which Arduino Server will use for all unicast communication with it's peers
-static uint16_t OC_WELL_KNOWN_PORT = 5683;
-
#ifdef ARDUINOWIFI
// Arduino WiFi Shield
// Note : Arduino WiFi Shield currently does NOT support multicast and therefore
// this server will NOT be listening on 224.0.1.187 multicast address.
+static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
+
/// WiFi Shield firmware with Intel patches
static const char INTEL_WIFI_SHIELD_FW_VER[] = "1.2.0";
// Verify that WiFi Shield is running the firmware with all UDP fixes
fwVersion = WiFi.firmwareVersion();
OC_LOG_V(INFO, TAG, "WiFi Shield Firmware version %s", fwVersion);
- if ( strncmp(fwVersion, INTEL_WIFI_SHIELD_FW_VER, sizeof(INTEL_WIFI_SHIELD_FW_VER)) !=0 )
+ if ( strncmp(fwVersion, ARDUINO_WIFI_SHIELD_UDP_FW_VER, sizeof(ARDUINO_WIFI_SHIELD_UDP_FW_VER)) !=0 )
{
OC_LOG(DEBUG, TAG, PCF("!!!!! Upgrade WiFi Shield Firmware version !!!!!!"));
return -1;
// This is the entity handler for the registered resource.
// This is invoked by OCStack whenever it recevies a request for this resource.
-OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest )
+OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest,
+ void *callbackParam)
{
OCEntityHandlerResult ehRet = OC_EH_OK;
OCEntityHandlerResponse response = {0};
- char payload[MAX_RESPONSE_LENGTH] = {0};
+ OCRepPayload* payload = OCRepPayloadCreate();
+ if(!payload)
+ {
+ OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
+ return OC_EH_ERROR;
+ }
if(entityHandlerRequest && (flag & OC_REQUEST_FLAG))
{
if(OC_REST_GET == entityHandlerRequest->method)
{
- size_t responsePayloadGetLength = strlen(responsePayloadGet);
- if (responsePayloadGetLength < (sizeof(payload) - 1))
- {
- strncpy(payload, responsePayloadGet, responsePayloadGetLength);
- }
- else
- {
- ehRet = OC_EH_ERROR;
- }
+ OCRepPayloadSetUri(payload, "/a/light");
+ OCRepPayloadSetPropBool(payload, "state", true);
+ OCRepPayloadSetPropInt(payload, "power", 10);
}
else if(OC_REST_PUT == entityHandlerRequest->method)
{
//Do something with the 'put' payload
- size_t responsePayloadPutLength = strlen(responsePayloadPut);
- if (responsePayloadPutLength < (sizeof(payload) - 1))
- {
- strncpy((char *)payload, responsePayloadPut, responsePayloadPutLength);
- }
- else
- {
- ehRet = OC_EH_ERROR;
- }
+ OCRepPayloadSetUri(payload, "/a/light");
+ OCRepPayloadSetPropBool(payload, "state", false);
+ OCRepPayloadSetPropInt(payload, "power", 0);
}
if (ehRet == OC_EH_OK)
response.requestHandle = entityHandlerRequest->requestHandle;
response.resourceHandle = entityHandlerRequest->resource;
response.ehResult = ehRet;
- response.payload = (unsigned char *)payload;
- response.payloadSize = strlen(payload);
+ response.payload = (OCPayload*) payload;
response.numSendVendorSpecificHeaderOptions = 0;
- memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
+ memset(response.sendVendorSpecificHeaderOptions, 0,
+ sizeof response.sendVendorSpecificHeaderOptions);
memset(response.resourceUri, 0, sizeof response.resourceUri);
// Indicate that response is NOT in a persistent buffer
response.persistentBufferFlag = 0;
else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
{
OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client"));
+ gLightUnderObservation = 0;
}
}
-
+ OCRepPayloadDestroy(payload);
return ehRet;
}
(void)param;
OCStackResult result = OC_STACK_ERROR;
modCounter += 1;
- if(modCounter % 10 == 0) // Matching the timing that the Linux Sample Server App uses for the same functionality.
+ // Matching the timing that the Linux Sample Server App uses for the same functionality.
+ if(modCounter % 10 == 0)
{
Light.power += 5;
if (gLightUnderObservation)
// Note : This will initialize Serial port on Arduino at 115200 bauds
OC_LOG_INIT();
OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
- uint16_t port = OC_WELL_KNOWN_PORT;
// Connect to Ethernet or WiFi network
if (ConnectToNetwork() != 0)
}
// Initialize the OC Stack in Server mode
- if (OCInit(NULL, port, OC_SERVER) != OC_STACK_OK)
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, PCF("OCStack init error"));
return;
void loop()
{
// This artificial delay is kept here to avoid endless spinning
- // of Arduino microcontroller. Modify it as per specfic application needs.
+ // of Arduino microcontroller. Modify it as per specific application needs.
delay(2000);
// This call displays the amount of free SRAM available on Arduino
Light.state = false;
OCStackResult res = OCCreateResource(&Light.handle,
"core.light",
- "oc.mi.def",
+ OC_RSRVD_INTERFACE_DEFAULT,
"/a/light",
OCEntityHandlerCb,
+ NULL,
OC_DISCOVERABLE|OC_OBSERVABLE);
OC_LOG_V(INFO, TAG, "Created Light resource with result: %s", getResult(res));
}
return "UNKNOWN";
}
}
+