separated the scheduler interface and added callbackresponse.
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationprovider.c
index 7a9bbd2..ff7343c 100644 (file)
@@ -1,35 +1,41 @@
-/******************************************************************\r
- *\r
- * Copyright 2015 Samsung Electronics All Rights Reserved.\r
- *\r
- *\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *\r
- ******************************************************************/\r
+//******************************************************************\r
+//\r
+// Copyright 2016 Samsung Electronics All Rights Reserved.\r
+//\r
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+//      http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+//\r
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
 \r
 #include <stdio.h>\r
 #include <stdbool.h>\r
 #include <stdlib.h>\r
+#include <unistd.h>\r
+\r
+#include "NSProvider.h"\r
 #include "NSCommon.h"\r
-#include "NSProviderInterface.h"\r
 #include "logger.h"\r
 #include "octypes.h"\r
 #include "pthread.h"\r
 #include "oic_string.h"\r
 #include "oic_malloc.h"\r
 \r
-#define TAG "notiProviderExample"\r
+#define TAG "NSProviderExample"\r
+\r
+// Input the following values to publish resource to cloud\r
+char REMOTE_SERVER_ADDRESS[50];\r
+char REMOTE_SERVER_SESSION[50];\r
 \r
 extern char *strdup(const char *s);\r
 \r
@@ -44,7 +50,7 @@ void* OCProcessThread(void * ptr)
     {\r
         if (OCProcess() != OC_STACK_OK)\r
         {\r
-            OIC_LOG(ERROR, TAG, "OCStack process error");\r
+            printf("OCStack process error");\r
             return NULL;\r
         }\r
     }\r
@@ -54,19 +60,55 @@ void* OCProcessThread(void * ptr)
 \r
 void subscribeRequestCallback(NSConsumer *consumer)\r
 {\r
-    OIC_LOG(INFO, TAG, "consumer requested to subscribe");\r
+    printf("consumer requested to subscribe");\r
 \r
-    printf("NS_APP Consumer Address ID: %s\n", consumer->mAddress);\r
-    printf("NS_APP Consumer Device ID: %s\n", consumer->mDeviceId);\r
+    printf("NS_APP Consumer Device ID: %s\n", consumer->consumerId);\r
 \r
     NSAccept(consumer, true);\r
 }\r
 \r
-void syncCallback(NSSync *sync)\r
+void syncCallback(NSSyncInfo *sync)\r
+{\r
+    printf("sync requested");\r
+\r
+    printf("NS_APP Sync State: %d\n", sync->state);\r
+}\r
+\r
+OCStackApplicationResult CloudLoginoutCallback(void *ctx,\r
+        OCDoHandle handle, OCClientResponse *clientResponse)\r
 {\r
-    OIC_LOG(INFO, TAG, "sync requested");\r
+    int CtxValue = 0x99;\r
+    if (ctx != (void *)CtxValue)\r
+    {\r
+        printf("Invalid Cloud Login/out callback received");\r
+    }\r
+\r
+    printf("Login/out response received");\r
+\r
+    if (clientResponse->payload != NULL &&\r
+            clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)\r
+    {\r
+        printf("PAYLOAD_TYPE_REPRESENTATION received");\r
+\r
+        OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;\r
+\r
+        printf("Get payload values");\r
+        while (val)\r
+        {\r
+            printf("key: %s / Value: %s", val->name, val->str);\r
+            val = val->next;\r
+        }\r
 \r
-    printf("NS_APP Sync State: %d\n", sync->mState);\r
+        NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS);\r
+    }\r
+\r
+    return OC_STACK_KEEP_TRANSACTION;\r
+}\r
+\r
+FILE* server_fopen(const char *path, const char *mode)\r
+{\r
+    (void)path;\r
+    return fopen("oic_ns_provider_db.dat", mode);\r
 }\r
 \r
 int main()\r
@@ -74,15 +116,19 @@ int main()
     int num;\r
     pthread_t processThread;\r
 \r
-    OIC_LOG(INFO, TAG, "NSStartProvider()");\r
+    printf("NSStartProvider()");\r
+\r
+    // open oic_db\r
+    static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};\r
+    OCRegisterPersistentStorageHandler(&ps);\r
 \r
     if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)\r
     {\r
-        OIC_LOG(INFO, TAG, "OCStack init error");\r
+        printf("OCStack init error");\r
         return 0;\r
     }\r
 \r
-    pthread_create(&processThread, NULL, OCProcessThread, NULL);\r
+    pthread_create(&processThread, NULL, OCProcessThread, unlink);\r
 \r
     while (!isExit)\r
     {\r
@@ -93,9 +139,9 @@ int main()
         printf("3. NSSendNotification() \n");\r
         printf("4. NSRead \n");\r
         printf("5. NSStopProvider() \n");\r
-        printf("6. NSGetConsumerList \n");\r
-        //printf("7. startPresence \n");\r
-        //printf("8. stopPresence \n");\r
+\r
+        printf("11. NSCloudLogin \n");\r
+        printf("12. NSCloudLogout \n");\r
         printf("0. Exit() \n");\r
 \r
         printf("input : ");\r
@@ -108,19 +154,18 @@ int main()
         switch (num)\r
         {\r
             case 1:\r
-                OIC_LOG(INFO, TAG, "NSStartProvider(Accepter: Provider)");\r
-                NSStartProvider(NS_ACCEPTER_PROVIDER, subscribeRequestCallback, syncCallback);\r
+                printf("NSStartProvider(Accepter: Provider)");\r
+                NSStartProvider(NS_ACCESS_ALLOW, subscribeRequestCallback, syncCallback);\r
                 break;\r
             case 2:\r
-                OIC_LOG(INFO, TAG, "NSStartProvider(Accepter: Consumer)");\r
-                NSStartProvider(NS_ACCEPTER_CONSUMER, subscribeRequestCallback, syncCallback);\r
+                printf("NSStartProvider(Accepter: Consumer)");\r
+                NSStartProvider(NS_ACCESS_DENY, subscribeRequestCallback, syncCallback);\r
                 break;\r
             case 3:\r
-                OIC_LOG(INFO, TAG, "NSSendNotification()");\r
+                printf("NSSendNotification()");\r
 \r
                 char title[100];\r
                 char body[100];\r
-                char charID[100];\r
 \r
                 printf("id : %d\n", ++id);\r
                 printf("title : ");\r
@@ -130,52 +175,57 @@ int main()
                 printf("body : ");\r
                 gets(body);\r
 \r
-                printf("app - mId : %s \n", charID);\r
                 printf("app - mTitle : %s \n", title);\r
                 printf("app - mContentText : %s \n", body);\r
 \r
-                NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));\r
-\r
-                sprintf(charID, "%d", id);\r
+                NSMessage * msg = NSCreateMessage();\r
 \r
-                msg->mId = strdup(charID);\r
-                msg->mTitle = strdup(title);\r
-                msg->mContentText = OICStrdup(body);\r
-                msg->mSource = OICStrdup("OCF");\r
+                msg->title = OICStrdup(title);\r
+                msg->contentText = OICStrdup(body);\r
+                msg->sourceName = OICStrdup("OCF");\r
 \r
                 NSSendMessage(msg);\r
 \r
                 break;\r
 \r
             case 4:\r
-                OIC_LOG(INFO, TAG, "NSRead");\r
-                NSSync * sync = (NSSync*) OICMalloc(sizeof(NSSync));\r
+                printf("NSRead");\r
+                NSSyncInfo * sync = (NSSyncInfo*) OICMalloc(sizeof(NSSyncInfo));\r
 \r
-                sync->mMessageId = OICStrdup("dev_001");\r
-                sync->mState = 1;\r
+                sync->messageId = OICStrdup("dev_001");\r
+                sync->state = 1;\r
 \r
                 break;\r
 \r
             case 5:\r
                 NSStopProvider();\r
                 break;\r
-            case 6:\r
-                OIC_LOG(INFO, TAG, "NSGetConsumerList");\r
-                break;\r
-            case 7:\r
-                OIC_LOG(INFO, TAG, "NSStartPresence - not working");\r
-                //NSTestStartPresence();\r
+\r
+            case 11:\r
+                printf("NSCloudLogin");\r
+\r
+                printf("Cloud Address: ");\r
+                gets(REMOTE_SERVER_ADDRESS);\r
+\r
+                printf("Session Code: ");\r
+                gets(REMOTE_SERVER_SESSION);\r
+\r
+\r
+                NSCloudLogin(REMOTE_SERVER_ADDRESS, REMOTE_SERVER_SESSION, CloudLoginoutCallback);\r
+                printf("OCCloudLogin requested");\r
                 break;\r
-            case 8:\r
-                OIC_LOG(INFO, TAG, "NSStopPresence- not working");\r
-                //NSTestStopPresence();\r
+            case 12:\r
+                printf("NSCloudLogout");\r
+\r
+                NSCloudLogout(REMOTE_SERVER_ADDRESS, REMOTE_SERVER_SESSION, CloudLoginoutCallback);\r
+                printf("OCCloudLogout requested");\r
                 break;\r
             case 0:\r
                 NSStopProvider();\r
                 isExit = true;\r
                 break;\r
             default:\r
-                OIC_LOG(INFO, TAG, "Under Construction");\r
+                printf("Under Construction");\r
                 break;\r
         }\r
 \r