Merge "Implementation of connectivity abstraction feature Release v0.5" into connecti...
[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", (object != NULL) ? object->resourceUri : "",
421             (requestInfo != NULL) ? requestInfo->info.payload : "");
422
423     printf("[CALLBACK] request_handler, address : %s\n", (object != NULL) ? object->addressInfo.IP.ipAddress : "");
424
425     if(requestInfo->info.options)
426     {
427         uint32_t len =  requestInfo->info.numOptions;
428         uint32_t i;
429         for(i = 0 ; i < len ; i++)
430         {
431             printf("[CALLBACK] request_handler, option ID : %d\n", requestInfo->info.options[i].optionID);
432             printf("[CALLBACK] request_handler, options data length : %d\n", requestInfo->info.options[i].optionLength);
433             printf("[CALLBACK] request_handler, options data : %s\n", requestInfo->info.options[i].optionData );
434         }
435     }
436
437     printf("send response with URI\n");
438     send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
439
440 }
441
442 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
443 {
444
445     printf("[CALLBACK] response_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
446             (responseInfo != NULL) ? responseInfo->info.payload : "");
447
448     printf("[CALLBACK] response_handler, address : %s\n", (object != NULL) ? object->addressInfo.IP.ipAddress : "");
449
450     if(responseInfo->info.options)
451     {
452         uint32_t len =  responseInfo->info.numOptions;
453         uint32_t i;
454         for(i = 0 ; i < len ; i++)
455         {
456             printf("[CALLBACK] response_handler, option ID : %d\n", responseInfo->info.options[i].optionID);
457             printf("[CALLBACK] response_handler, options data length : %d\n", responseInfo->info.options[i].optionLength);
458             printf("[CALLBACK] response_handler, options data : %s\n", responseInfo->info.options[i].optionData );
459         }
460     }
461
462     //printf("send request with URI\n");
463     //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
464 }
465
466 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
467 {
468
469     printf("\n=============================================\n");
470
471     CAInfo_t responseData;
472     //responseData = (CAInfo*) malloc(sizeof(CAInfo));
473     memset(&responseData, 0, sizeof(CAInfo_t));
474     responseData.token = request_token;
475     responseData.payload = "response payload";
476
477     CAResponseInfo_t responseInfo;
478     //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
479     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
480     responseInfo.result = 203;
481     responseInfo.info = responseData;
482
483     // send request (connectivityType from remoteEndpoint of request Info)
484     CASendResponse(endpoint, &responseInfo);
485
486     printf("=============================================\n");
487
488 }
489
490 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
491 {
492
493     printf("\n=============================================\n");
494
495     CAInfo_t requestData;
496     memset(&requestData, 0, sizeof(CAInfo_t));
497     requestData.token = token;
498     requestData.payload = "Temp Json Payload";
499
500     CARequestInfo_t requestInfo;
501     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
502     requestInfo.method = CA_GET;
503     requestInfo.info = requestData;
504
505     // send request
506     endpoint->connectivityType = CA_WIFI;
507     CASendRequest(endpoint, &requestInfo);
508
509     printf("=============================================\n");
510
511 }
512
513 void terminate()
514 {
515     unselect_network();
516 }