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