Merge branch 'cloud-interface'
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationconsumer.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include "pthread.h"
24
25 #include "ocstack.h"
26 #include "NSCommon.h"
27 #include "NSConsumerInterface.h"
28
29 #ifdef WITH_CLOUD
30 #include "NSConstants.h"
31 #include "NSConsumerCommon.h"
32 #include "cloud_connector.h"
33 #include "oic_malloc.h"
34
35 #define CLOUD_CONTEXT_VALUE 0x99
36 #define CLOUD_PRESENCE_SUBSCRIBE_QUERY ""          // refer to IoTivity Cloud Module Sample
37
38 #define CLOUD_HOST_ADDRESS ""                      // refer to IoTivity Cloud Module Sample
39 #define CLOUD_IOTIVITYNS_SESSION ""                // refer to IoTivity Cloud Module Sample
40 #endif
41
42 void onDiscoverNotification(NSProvider * provider)
43 {
44     printf("notification resource discovered\n");
45     printf("subscribe result %d\n", NSSubscribe(provider));
46     printf("startSubscribing\n");
47 }
48
49 void onSubscriptionAccepted(NSProvider * provider)
50 {
51     printf("Subscription accepted\n");
52     printf("subscribed provider Id : %s\n", provider->providerId);
53 }
54
55 void onNotificationPosted(NSMessage * notification)
56 {
57     printf("id : %lld\n", (long long int)notification->messageId);
58     printf("title : %s\n", notification->title);
59     printf("content : %s\n", notification->contentText);
60     printf("source : %s\n", notification->sourceName);
61     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
62 }
63
64 void onNotificationSync(NSSyncInfo * sync)
65 {
66     printf("Sync ID : %lld\n", (long long int)sync->messageId);
67     printf("Sync STATE : %d\n", sync->state);
68 }
69
70
71 #ifdef WITH_CLOUD
72 OCStackApplicationResult handleLoginoutCB(void *ctx,
73         OCDoHandle handle,
74         OCClientResponse *clientResponse)
75 {
76     (void)handle;
77     if (ctx != (void *)CLOUD_CONTEXT_VALUE)
78     {
79         NS_LOG(DEBUG, "Invalid Login/out callback received");
80     }
81
82     NS_LOG(DEBUG, "Login/out response received");
83
84     if (clientResponse->payload != NULL &&
85         clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
86     {
87         NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
88
89         OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
90
91         while (val)
92         {
93             val = val->next;
94         }
95         NS_LOG(DEBUG, "Get payload values");
96
97         OCDevAddr * addr = NULL;
98         addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
99         memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
100
101         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
102
103         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
104         NSConsumerPushEvent(task);
105     }
106
107     return OC_STACK_KEEP_TRANSACTION;
108 }
109 #endif
110
111 void* OCProcessThread(void * ptr)
112 {
113     (void) ptr;
114
115     while (true)
116     {
117         usleep(2000);
118         if(OCProcess() != OC_STACK_OK)
119         {
120             OCStop();
121             break;
122         }
123     }
124
125     return NULL;
126 }
127
128 int main(void)
129 {
130     bool isExit = false;
131     pthread_t OCThread = NULL;
132
133     printf("start Iotivity\n");
134     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
135     {
136         printf("OCInit fail\n");
137         return 0;
138     }
139
140     NSConsumerConfig cfg;
141     cfg.discoverCb = onDiscoverNotification;
142     cfg.acceptedCb = onSubscriptionAccepted;
143     cfg.messageCb = onNotificationPosted;
144     cfg.syncInfoCb = onNotificationSync;
145
146 #ifdef WITH_CLOUD
147     NS_LOG(DEBUG, "process OCCloudLogin...");
148     OCCloudLogin(CLOUD_HOST_ADDRESS, CLOUD_IOTIVITYNS_SESSION, handleLoginoutCB);
149     NS_LOG(DEBUG, "OCCloudLogin return");
150 #endif
151
152     pthread_create(&OCThread, NULL, OCProcessThread, NULL);
153
154     printf("start notification consumer service\n");
155     while (!isExit)
156     {
157         int num = 0;
158         char dummy = '\0';
159
160         printf("1. Start Consumer\n");
161         printf("2. Stop Consumer\n");
162         printf("5. Exit\n");
163
164         printf("Input: ");
165
166         scanf("%d", &num);
167         fflush(stdin);
168         scanf("%c", &dummy);
169         fflush(stdin);
170
171         switch (num)
172         {
173             case 1:
174                 printf("1. Start Consumer\n");
175                 NSStartConsumer(cfg);
176                 break;
177             case 2:
178                 printf("2. Stop Consumer");
179                 NSStopConsumer();
180                 break;
181             case 5:
182                 printf("5. Exit");
183                 isExit = true;
184                 break;
185             default:
186                 break;
187         }
188     }
189
190     return 0;
191 }