Implementation of connectivity abstraction feature Release v0.61
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / casample.c
1 /******************************************************************
2  *
3  * Copyright 2014 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 <ctype.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <glib.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29 #include "cacommon.h"
30 #include "cainterface.h"
31
32 static GMainLoop *mainloop;
33 static GIOChannel *channel;
34 static guint g_test_io_watch_id;
35 static GError *g_err_Sample;
36
37 pthread_t thread;
38
39 #define MAX_BUF_LEN 1024
40 #define MAX_OPT_LEN 16
41
42 char get_menu();
43 void process();
44
45 void initialize();
46 void start_listening_server();
47 void start_discovery_server();
48 void find_resource();
49 void send_request();
50 void send_response();
51 void advertise_resource();
52 void send_notification();
53 void select_network();
54 void unselect_network();
55 void handle_request_response();
56
57 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
58 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
59 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
60 void terminate();
61
62 void pthread_func()
63 {
64     g_main_loop_run(mainloop);
65 }
66
67 int main()
68 {
69
70     printf("=============================================\n");
71     printf("\t\tsample main\n");
72     printf("=============================================\n");
73
74     process();
75
76     return 0;
77 }
78
79 void process()
80 {
81     while (1)
82     {
83         char menu = toupper(get_menu());
84
85         switch (menu)
86         {
87             case 'Q': // quits the sample program
88                 printf("quit..!!\n");
89                 g_main_loop_quit(mainloop);
90                 return;
91
92             case 'I': // Initialize interface
93                 initialize();
94                 break;
95
96             case 'S': // start server
97                 start_listening_server();
98                 break;
99
100             case 'C': // start client
101                 start_discovery_server();
102                 break;
103
104             case 'F': // find resource
105                 find_resource();
106                 break;
107
108             case 'R': // send request
109                 send_request();
110                 break;
111
112             case 'A': // advertise resource
113                 advertise_resource();
114                 break;
115
116             case 'N': // select network
117                 select_network();
118                 break;
119
120             case 'X': // unselect network
121                 unselect_network();
122                 break;
123
124             case 'H': // handle request response
125                 handle_request_response();
126                 break;
127
128             case 'T': // Terminate interface
129                 terminate();
130                 break;
131
132             default:
133                 printf("not supported menu!!\n");
134                 break;
135         }
136     }
137
138 }
139
140 void initialize()
141 {
142     mainloop = g_main_loop_new(NULL, FALSE);
143     pthread_create (&thread, NULL, (void *) &pthread_func, NULL);
144
145     CAInitialize();
146
147     // network enable
148     // default
149     printf("select default network(WIFI)\n");
150     CASelectNetwork(CA_WIFI);
151
152     // set handler.
153     CARegisterHandler(request_handler, response_handler);
154 }
155
156 void start_listening_server()
157 {
158     printf("start listening server!!\n");
159
160     CAStartListeningServer();
161 }
162
163 void start_discovery_server()
164 {
165     printf("start discovery server at client!!\n");
166
167     CAStartDiscoveryServer();
168 }
169
170 void find_resource()
171 {
172     char buf[MAX_BUF_LEN];
173
174     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
175
176     printf("\n=============================================\n");
177     printf("ex) a/light\n");
178     printf("reference uri : ");
179
180     gets(buf);
181
182     // create token
183     CAToken_t token = NULL;
184     CAResult_t res = CAGenerateToken(&token);
185     if (res != CA_STATUS_OK)
186     {
187         printf("token generate error!!\n");
188         token = NULL;
189     }
190
191     printf("generated token %s\n", (token != NULL) ? token : "");
192
193     res = CAFindResource(buf, token);
194
195     if (res != CA_STATUS_OK)
196     {
197         printf("find resource error:%d !!\n", res);
198     }
199     else
200     {
201         printf("find resource for %s URI\n", buf);
202     }
203
204     printf("=============================================\n");
205 }
206
207 void send_request()
208 {
209     char buf[MAX_BUF_LEN];
210
211     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
212
213     printf("\n=============================================\n");
214     printf("10.11.12.13:4545/resource_uri ( for IP )\n");
215     printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
216     printf("uri : ");
217
218     gets(buf);
219
220     // create remote endpoint
221     CARemoteEndpoint_t *endpoint = NULL;
222     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
223
224     if (res != CA_STATUS_OK)
225     {
226         printf("create remote endpoint error!!");
227         CADestroyRemoteEndpoint(endpoint);
228         return;
229     }
230
231     // create token
232     CAToken_t token = NULL;
233     res = CAGenerateToken(&token);
234
235     if (res != CA_STATUS_OK)
236     {
237         printf("token generate error!!");
238         token = NULL;
239     }
240
241     printf("generated token %s\n", (token != NULL) ? token : "");
242
243     CAInfo_t requestData;
244     memset(&requestData, 0, sizeof(CAInfo_t));
245     requestData.token = token;
246     requestData.payload = "Temp Json Payload";
247
248     CARequestInfo_t requestInfo;
249     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
250     requestInfo.method = CA_GET;
251     requestInfo.info = requestData;
252
253     // send request
254     CASendRequest(endpoint, &requestInfo);
255
256     if (token != NULL)
257     {
258         CADestroyToken(token);
259     }
260
261     // destroy remote endpoint
262     if (endpoint != NULL)
263     {
264         CADestroyRemoteEndpoint(endpoint);
265     }
266
267     printf("=============================================\n");
268 }
269
270 void advertise_resource()
271 {
272     char buf[MAX_BUF_LEN];
273
274     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
275
276     printf("\n=============================================\n");
277     printf("uri : ");
278
279     scanf("%s", buf);
280
281     int optionNum = 0;
282     char optionData[MAX_OPT_LEN];
283
284     printf("Option Num : ");
285     scanf("%d", &optionNum);
286     CAHeaderOption_t *headerOpt;
287
288     if (optionNum > 0)
289     {
290         headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
291         if (NULL == headerOpt)
292         {
293             printf("memory allocation failed!\n");
294             return;
295         }
296         memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
297     }
298
299     int i;
300     for (i = 0 ; i < optionNum ; i++)
301     {
302         int optionID = 0;
303         printf("[%d] Option ID : ", i + 1);
304         scanf("%d", &optionID);
305         headerOpt[i].optionID = optionID;
306
307         memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
308         printf("[%d] Option Data : ", i + 1);
309         scanf("%s", optionData);
310         memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
311         printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
312
313         headerOpt[i].optionLength = (uint16_t)strlen(optionData);
314     }
315     printf("\n=============================================\n");
316
317     // create token
318     CAToken_t token = NULL;
319     CAResult_t res = CAGenerateToken(&token);
320     if (res != CA_STATUS_OK)
321     {
322         printf("token generate error!!\n");
323         token = NULL;
324     }
325
326     printf("generated token %s\n", (token != NULL) ? token : "");
327
328     CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
329
330     free(headerOpt);
331
332 }
333
334 void select_network()
335 {
336     char buf[MAX_BUF_LEN];
337
338     printf("\n=============================================\n");
339     printf("\tselect network\n");
340     printf("ETHERNET : 0\n");
341     printf("WIFI : 1\n");
342     printf("EDR : 2\n");
343     printf("LE : 3\n");
344     printf("select : ");
345
346     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
347     gets(buf);
348
349     int number = buf[0] - '0';
350
351     number = (number < 0 || number > 3) ? 1 : number;
352
353     CASelectNetwork(1 << number);
354
355     printf("=============================================\n");
356 }
357
358 void unselect_network()
359 {
360     char buf[MAX_BUF_LEN];
361
362     printf("\n=============================================\n");
363     printf("\tunselect enabled network\n");
364     printf("ETHERNET : 0\n");
365     printf("WIFI : 1\n");
366     printf("EDR : 2\n");
367     printf("LE : 3\n");
368     printf("select : ");
369
370     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
371     gets(buf);
372
373     int number = buf[0] - '0';
374
375     number = (number < 0 || number > 3) ? 1 : number;
376
377     CAUnSelectNetwork(1 << number);
378     printf("Terminating...\n");
379     CATerminate();
380     //pthread_join(thread, NULL);
381     printf("=============================================\n");
382 }
383
384 char get_menu()
385 {
386     char buf[MAX_BUF_LEN];
387
388     printf("\n=============================================\n");
389     printf("\t\tMenu\n");
390     printf("\ti : Initialize\n");
391     printf("\ts : start server\n");
392     printf("\tc : start client\n");
393     printf("\tf : find resource\n");
394     printf("\tr : send request\n");
395     printf("\ta : advertise resource\n");
396     printf("\tn : select network\n");
397     printf("\tx : unselect network\n");
398     printf("\th : handle request response\n");
399     printf("\tt : terminate\n");
400     printf("\tq : quit\n");
401     printf("=============================================\n");
402     printf("select : ");
403
404     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
405
406     gets(buf);
407
408     return buf[0];
409 }
410
411 void handle_request_response()
412 {
413     printf("handle_request_response\n");
414     CAHandleRequestResponse();
415 }
416
417 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
418 {
419
420     printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
421            (object != NULL) ? object->resourceUri : "",
422            (requestInfo != NULL) ? requestInfo->info.payload : "");
423
424     printf("[CALLBACK] request_handler, address : %s\n",
425            (object != NULL) ? object->addressInfo.IP.ipAddress : "");
426
427     if (requestInfo->info.options)
428     {
429         uint32_t len =  requestInfo->info.numOptions;
430         uint32_t i;
431         for (i = 0 ; i < len ; i++)
432         {
433             printf("[CALLBACK] request_handler, option ID : %d\n", requestInfo->info.options[i].optionID);
434             printf("[CALLBACK] request_handler, options data length : %d\n",
435                    requestInfo->info.options[i].optionLength);
436             printf("[CALLBACK] request_handler, options data : %s\n", requestInfo->info.options[i].optionData );
437         }
438     }
439
440     printf("send response with URI\n");
441     send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
442
443 }
444
445 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
446 {
447
448     printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
449            (object != NULL) ? object->resourceUri : "",
450            (responseInfo != NULL) ? responseInfo->info.payload : "");
451
452     printf("[CALLBACK] response_handler, address : %s\n",
453            (object != NULL) ? object->addressInfo.IP.ipAddress : "");
454
455     if (responseInfo->info.options)
456     {
457         uint32_t len =  responseInfo->info.numOptions;
458         uint32_t i;
459         for (i = 0 ; i < len ; i++)
460         {
461             printf("[CALLBACK] response_handler, option ID : %d\n", responseInfo->info.options[i].optionID);
462             printf("[CALLBACK] response_handler, options data length : %d\n",
463                    responseInfo->info.options[i].optionLength);
464             printf("[CALLBACK] response_handler, options data : %s\n",
465                    responseInfo->info.options[i].optionData );
466         }
467     }
468
469     //printf("send request with URI\n");
470     //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
471 }
472
473 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
474 {
475
476     printf("\n=============================================\n");
477
478     CAInfo_t responseData;
479     //responseData = (CAInfo*) malloc(sizeof(CAInfo));
480     memset(&responseData, 0, sizeof(CAInfo_t));
481     responseData.token = request_token;
482     responseData.payload = "response payload";
483
484     CAResponseInfo_t responseInfo;
485     //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
486     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
487     responseInfo.result = 203;
488     responseInfo.info = responseData;
489
490     // send request (connectivityType from remoteEndpoint of request Info)
491     CASendResponse(endpoint, &responseInfo);
492
493     printf("=============================================\n");
494
495 }
496
497 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
498 {
499
500     printf("\n=============================================\n");
501
502     CAInfo_t requestData;
503     memset(&requestData, 0, sizeof(CAInfo_t));
504     requestData.token = token;
505     requestData.payload = "Temp Json Payload";
506
507     CARequestInfo_t requestInfo;
508     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
509     requestInfo.method = CA_GET;
510     requestInfo.info = requestData;
511
512     // send request
513     endpoint->connectivityType = CA_WIFI;
514     CASendRequest(endpoint, &requestInfo);
515
516     printf("=============================================\n");
517
518 }
519
520 void terminate()
521 {
522     unselect_network();
523 }