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 "oic_malloc.h"
33
34 #define CLOUD_CONTEXT_VALUE 0x99
35 #define CLOUD_PRESENCE_SUBSCRIBE_QUERY ""          // refer to IoTivity Cloud Module Sample
36
37 #define CLOUD_HOST_ADDRESS ""                      // refer to IoTivity Cloud Module Sample
38 #define CLOUD_IOTIVITYNS_SESSION ""                // refer to IoTivity Cloud Module Sample
39 #endif
40
41 void onDiscoverNotification(NSProvider * provider)
42 {
43     printf("notification resource discovered\n");
44     printf("subscribe result %d\n", NSSubscribe(provider));
45     printf("startSubscribing\n");
46 }
47
48 void onProviderChanged(NSProvider * provider, NSResponse response)
49 {
50     printf("Provider changed: %d\n", response);
51     printf("subscribed provider Id : %s\n", provider->providerId);
52
53     if (response == NS_TOPIC)
54     {
55         printf ("Provider Topic Updated\n");
56         if (provider->topicLL)
57         {
58             NSTopicLL * iter = provider->topicLL;
59             while (iter)
60             {
61                 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
62                 iter = iter->next;
63             }
64         }
65
66         printf("3. Get Topics\n");
67         printf("4. Select Topics\n");
68         printf("input: ");
69
70         int num = 0;
71         char dummy = '\0';
72         scanf("%d", &num);
73         fflush(stdin);
74         scanf("%c", &dummy);
75         fflush(stdin);
76
77         switch (num)
78         {
79             case 3:
80                 printf("3. Get Topics\n");
81                 NSConsumerGetInterestTopics(provider);
82                 break;
83             case 4:
84                 printf("4. Select Topics\n");
85                 if (provider->topicLL)
86                 {
87                     NSTopicLL * iter = provider->topicLL;
88                     int i = 0;
89                     while (iter)
90                     {
91                         iter->state = (i++)%2;
92                         printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
93                         iter = iter->next;
94                     }
95                 }
96                 NSConsumerSelectInterestTopics(provider);
97                 break;
98         }
99     }
100 }
101
102 void onNotificationPosted(NSMessage * notification)
103 {
104     printf("id : %lld\n", (long long int)notification->messageId);
105     printf("title : %s\n", notification->title);
106     printf("content : %s\n", notification->contentText);
107     printf("source : %s\n", notification->sourceName);
108     if (notification->topic && strlen(notification->topic) > 0)
109     {
110         printf("topic : %s\n", notification->topic);
111     }
112     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
113 }
114
115 void onNotificationSync(NSSyncInfo * sync)
116 {
117     printf("Sync ID : %lld\n", (long long int)sync->messageId);
118     printf("Sync STATE : %d\n", sync->state);
119 }
120
121
122 #ifdef WITH_CLOUD
123 OCStackApplicationResult handleLoginoutCB(void *ctx,
124         OCDoHandle handle,
125         OCClientResponse *clientResponse)
126 {
127     (void)handle;
128     if (ctx != (void *)CLOUD_CONTEXT_VALUE)
129     {
130         NS_LOG(DEBUG, "Invalid Login/out callback received");
131     }
132
133     NS_LOG(DEBUG, "Login/out response received");
134
135     if (clientResponse->payload != NULL &&
136         clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
137     {
138         NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
139
140         OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
141
142         while (val)
143         {
144             val = val->next;
145         }
146         NS_LOG(DEBUG, "Get payload values");
147
148         OCDevAddr * addr = NULL;
149         addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
150         memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
151
152         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
153
154         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
155         NSConsumerPushEvent(task);
156     }
157
158     return OC_STACK_KEEP_TRANSACTION;
159 }
160 #endif
161
162 void* OCProcessThread(void * ptr)
163 {
164     (void) ptr;
165
166     while (true)
167     {
168         usleep(2000);
169         if(OCProcess() != OC_STACK_OK)
170         {
171             OCStop();
172             break;
173         }
174     }
175
176     return NULL;
177 }
178
179 int main(void)
180 {
181     bool isExit = false;
182     pthread_t OCThread = NULL;
183
184     printf("start Iotivity\n");
185     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
186     {
187         printf("OCInit fail\n");
188         return 0;
189     }
190
191     NSConsumerConfig cfg;
192     cfg.discoverCb = onDiscoverNotification;
193     cfg.changedCb = onProviderChanged;
194     cfg.messageCb = onNotificationPosted;
195     cfg.syncInfoCb = onNotificationSync;
196
197 #ifdef WITH_CLOUD
198     NS_LOG(DEBUG, "process OCCloudLogin...");
199     NS_LOG(DEBUG, "OCCloudLogin return");
200 #endif
201
202     pthread_create(&OCThread, NULL, OCProcessThread, NULL);
203
204     printf("start notification consumer service\n");
205     while (!isExit)
206     {
207         int num = 0;
208         char dummy = '\0';
209
210         printf("1. Start Consumer\n");
211         printf("2. Stop Consumer\n");
212         printf("5. Exit\n");
213
214         printf("Input: ");
215
216         scanf("%d", &num);
217         fflush(stdin);
218         scanf("%c", &dummy);
219         fflush(stdin);
220
221         switch (num)
222         {
223             case 1:
224                 printf("1. Start Consumer\n");
225                 NSStartConsumer(cfg);
226                 break;
227             case 2:
228                 printf("2. Stop Consumer");
229                 NSStopConsumer();
230                 break;
231             case 5:
232                 printf("5. Exit");
233                 isExit = true;
234                 break;
235             default:
236                 break;
237         }
238     }
239
240     return 0;
241 }