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