Modify 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 "oic_malloc.h"
32 #include "cloud_connector.h"
33
34 #define CLOUD_CONTEXT_VALUE 0x99
35
36 char CLOUD_ADDRESS[50];
37 char CLOUD_AUTH_PROVIDER[50];
38 char CLOUD_AUTH_CODE[50];
39 char CLOUD_UID[50];
40 char CLOUD_ACCESS_TOKEN[50];
41 #endif
42
43
44 NSProvider * g_provider = NULL;
45
46 FILE* server_fopen(const char *path, const char *mode)
47 {
48     (void)path;
49     return fopen("oic_ns_provider_db.dat", mode);
50 }
51
52 void onDiscoverNotification(NSProvider * provider)
53 {
54     printf("notification resource discovered\n");
55     printf("subscribe result %d\n", NSSubscribe(provider));
56     printf("startSubscribing\n");
57 }
58
59 void printProviderTopicList(NSProvider *provider)
60 {
61     printf("printProviderTopicList\n");
62     if (provider->topicLL)
63     {
64         NSTopicLL * iter = provider->topicLL;
65         while (iter)
66         {
67             printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
68             iter = iter->next;
69         }
70     }
71 }
72
73 void onProviderChanged(NSProvider * provider, NSResponse response)
74 {
75     printf("Provider changed: %d\n", response);
76     printf("subscribed provider Id : %s\n", provider->providerId);
77
78     if (response == NS_TOPIC)
79     {
80         printf ("Provider Topic Updated\n");
81
82         printProviderTopicList(provider);
83         g_provider = provider;
84     }
85 }
86
87 void onNotificationPosted(NSMessage * notification)
88 {
89     printf("id : %lld\n", (long long int)notification->messageId);
90     printf("title : %s\n", notification->title);
91     printf("content : %s\n", notification->contentText);
92     printf("source : %s\n", notification->sourceName);
93     if (notification->topic && strlen(notification->topic) > 0)
94     {
95         printf("topic : %s\n", notification->topic);
96     }
97     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
98 }
99
100 void onNotificationSync(NSSyncInfo * sync)
101 {
102     printf("Sync ID : %lld\n", (long long int)sync->messageId);
103     printf("Sync STATE : %d\n", sync->state);
104 }
105
106 void* OCProcessThread(void * ptr)
107 {
108     (void) ptr;
109
110     while (true)
111     {
112         usleep(2000);
113         if(OCProcess() != OC_STACK_OK)
114         {
115             OCStop();
116             break;
117         }
118     }
119
120     return NULL;
121 }
122
123 int main(void)
124 {
125     bool isExit = false;
126     pthread_t OCThread = NULL;
127
128     printf("start Iotivity\n");
129
130     // open oic_db
131     static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
132     OCRegisterPersistentStorageHandler(&ps);
133
134     if (OCInit1(OC_CLIENT_SERVER, 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.changedCb = onProviderChanged;
143     cfg.messageCb = onNotificationPosted;
144     cfg.syncInfoCb = onNotificationSync;
145
146     pthread_create(&OCThread, NULL, OCProcessThread, NULL);
147
148     printf("start notification consumer service\n");
149     while (!isExit)
150     {
151         int num = 0;
152         char dummy = '\0';
153
154         printf("1. Start Consumer\n");
155         printf("2. Stop Consumer\n");
156         printf("3. Get Topics\n");
157         printf("4. Select Topics\n");
158         printf("5. Exit\n");
159 #ifdef WITH_CLOUD
160         printf("21. Enable Remote Service (after login)\n");
161         printf("31. Cloud Signup\n");
162         printf("32. Cloud Login\n");
163         printf("33. Cloud Logout\n");
164 #endif
165
166         printf("Input: ");
167
168         scanf("%d", &num);
169         fflush(stdin);
170         scanf("%c", &dummy);
171         fflush(stdin);
172
173         switch (num)
174         {
175             case 1:
176                 printf("1. Start Consumer\n");
177                 NSStartConsumer(cfg);
178                 break;
179             case 2:
180                 printf("2. Stop Consumer");
181                 NSStopConsumer();
182                 break;
183             case 3:
184                 printf("3. Get Topics\n");
185                 if(g_provider)
186                 {
187                     NSConsumerGetInterestTopics(g_provider);
188                 }
189                 break;
190             case 4:
191                 printf("4. Select Topics\n");
192
193                 if (g_provider && g_provider->topicLL)
194                 {
195                     NSTopicLL * iter = g_provider->topicLL;
196                     int i = 0;
197                     while (iter)
198                     {
199                         iter->state = (i++)%2;
200                         printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
201                         iter = iter->next;
202                     }
203                     NSConsumerSelectInterestTopics(g_provider);
204                 }
205                 break;
206             case 5:
207                 printf("5. Exit");
208                 isExit = true;
209                 break;
210 #ifdef WITH_CLOUD
211             case 21:
212                 printf("Enable Remote Service");
213                 if(!IsCloudLoggedin())
214                 {
215                     printf("Cloud Login required");
216                     break;
217                 }
218                 NSConsumerEnableRemoteService(CLOUD_ADDRESS);
219                 break;
220             case 31:
221                 printf("Remote Server Address: ");
222                 gets(CLOUD_ADDRESS);
223
224                 printf("Auth Provider(eg. github): ");
225                 gets(CLOUD_AUTH_PROVIDER);
226
227                 printf("Auth Code: ");
228                 gets(CLOUD_AUTH_CODE);
229
230                 OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
231                     CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
232                 printf("OCCloudSignup requested");
233                 break;
234             case 32:
235                 printf("Remote Server Address: ");
236                 gets(CLOUD_ADDRESS);
237
238                 printf("UID: ");
239                 gets(CLOUD_UID);
240
241                 printf("ACCESS_TOKEN: ");
242                 gets(CLOUD_ACCESS_TOKEN);
243
244                 OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
245                     CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
246                 printf("OCCloudLogin requested");
247                 break;
248             case 33:
249                 OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
250                 printf("OCCloudLogin requested");
251                 break;
252 #endif
253             default:
254                 break;
255         }
256     }
257     return 0;
258 }