///////////////////////////////////////////////////////////////////////
//NOTE : This sample server is generated based on ocserverbasicops.cpp
///////////////////////////////////////////////////////////////////////
+#include "iotivity_config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
-#include <signal.h>
+#endif
+#ifdef HAVE_PTHREAD_H
#include <pthread.h>
+#endif
+#include <signal.h>
#include "ocstack.h"
-#include "logger.h"
#include "ocpayload.h"
+#include "pinoxmcommon.h"
+#include "srmutility.h"
+
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+/** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */
+#ifdef ERROR
+#undef ERROR
+#endif //ERROR
+#endif //HAVE_WINDOWS_H
+#include "platform_features.h"
+#include "logger.h"
+
#define TAG "SAMPLE_JUSTWORKS"
//Secure Virtual Resource database for Iotivity Server
//It contains Server's Identity and the PSK credentials
//of other devices which the server trusts
-static char CRED_FILE[] = "oic_svr_db_server_justworks.json";
+static char CRED_FILE[] = "oic_svr_db_server_justworks.dat";
/* Function that creates a new LED resource by calling the
* OCCreateResource() method.
OCRepPayload* payload = OCRepPayloadCreate();
if(!payload)
{
- OC_LOG(ERROR, TAG, "Failed to allocate Payload");
+ OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
return NULL;
}
{
if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
{
- OC_LOG(ERROR, TAG, "Incoming payload not a representation");
+ OIC_LOG(ERROR, TAG, "Incoming payload not a representation");
return NULL;
}
if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
{
- OC_LOG (INFO, TAG, "Created new LED instance");
+ OIC_LOG (INFO, TAG, "Created new LED instance");
gLedInstance[gCurrLedInstance].state = 0;
gLedInstance[gCurrLedInstance].power = 0;
gCurrLedInstance++;
- strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
+ strncpy ((char *)response->resourceUri, newLedUri, sizeof(response->resourceUri));
ehResult = OC_EH_RESOURCE_CREATED;
}
}
}
else
{
- OC_LOG_V (INFO, TAG, "Payload was NULL");
+ OIC_LOG_V (INFO, TAG, "Payload was NULL");
ehResult = OC_EH_ERROR;
}
OCEntityHandlerRequest *entityHandlerRequest,
void* callbackParam)
{
- OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
+ OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
(void)callbackParam;
OCEntityHandlerResult ehResult = OC_EH_ERROR;
// Validate pointer
if (!entityHandlerRequest)
{
- OC_LOG (ERROR, TAG, "Invalid request pointer");
+ OIC_LOG (ERROR, TAG, "Invalid request pointer");
return OC_EH_ERROR;
}
if (flag & OC_REQUEST_FLAG)
{
- OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
+ OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
if (entityHandlerRequest)
{
if (OC_REST_GET == entityHandlerRequest->method)
{
- OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
+ OIC_LOG (INFO, TAG, "Received OC_REST_GET from client");
ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
}
else if (OC_REST_PUT == entityHandlerRequest->method)
{
- OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
+ OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
}
else if (OC_REST_POST == entityHandlerRequest->method)
{
- OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
+ OIC_LOG (INFO, TAG, "Received OC_REST_POST from client");
ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
}
else
{
- OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+ OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
entityHandlerRequest->method);
ehResult = OC_EH_ERROR;
}
// Send the response
if (OCDoResponse(&response) != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "Error sending response");
+ OIC_LOG(ERROR, TAG, "Error sending response");
ehResult = OC_EH_ERROR;
}
}
return fopen(CRED_FILE, mode);
}
+static void OtmEventHandler(void *ctx, const char *addr, uint16_t port,
+ const char* ownerId, OCOtmEvent_t event)
+{
+ (void)ctx;
+ printf("--------------------------------------\n");
+ printf("Get OTM event.\n");
+ printf("Address : %s\n", addr);
+ printf("Port : %d\n", port);
+ printf("Owner ID : %s\n", ownerId);
+
+ switch (event)
+ {
+ case OIC_OTM_READY:
+ printf("State : OIC_OTM_READY\n");
+ break;
+ case OIC_OTM_STARTED:
+ printf("State : OIC_OTM_STARTED\n");
+ break;
+ case OIC_OTM_DONE:
+ printf("State : OIC_OTM_DONE\n");
+ break;
+ case OIC_OTM_ERROR:
+ printf("State : OIC_OTM_ERROR\n");
+ break;
+ default:
+ printf("State : Unknown state.\n");
+ break;
+ }
+ printf("--------------------------------------\n");
+}
+
int main()
{
struct timespec timeout;
- OC_LOG(DEBUG, TAG, "OCServer is starting...");
+ OIC_LOG(DEBUG, TAG, "OCServer is starting...");
+
+ //This function should be invoked before invoke OCInit
+ OCSetOtmEventHandler(NULL, OtmEventHandler);
// Initialize Persistent Storage for SVR database
- OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
+ OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink, NULL, NULL};
OCRegisterPersistentStorageHandler(&ps);
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack init error");
+ OIC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
timeout.tv_nsec = 100000000L;
// Break from loop with Ctrl-C
- OC_LOG(INFO, TAG, "Entering ocserver main loop...");
+ OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
signal(SIGINT, handleSigInt);
while (!gQuitFlag)
{
if (OCProcess() != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack process error");
+ OIC_LOG(ERROR, TAG, "OCStack process error");
return 0;
}
nanosleep(&timeout, NULL);
}
- OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
+ OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
if (OCStop() != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack process error");
+ OIC_LOG(ERROR, TAG, "OCStack process error");
}
return 0;
{
if (!uri)
{
- OC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
+ OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
return -1;
}
OCEntityHandlerCb,
NULL,
OC_DISCOVERABLE|OC_OBSERVABLE | OC_SECURE);
- OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
+ OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
return 0;
}