1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
30 #include "cainterface.h"
32 static GMainLoop *mainloop;
33 static GIOChannel *channel;
34 static guint g_test_io_watch_id;
35 static GError *g_err_Sample;
39 #define MAX_BUF_LEN 1024
40 #define MAX_OPT_LEN 16
46 void start_listening_server();
47 void start_discovery_server();
51 void advertise_resource();
52 void send_notification();
53 void select_network();
54 void unselect_network();
55 void handle_request_response();
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);
64 g_main_loop_run(mainloop);
71 printf("=============================================\n");
72 printf("\t\tsample main\n");
73 printf("=============================================\n");
84 char menu = toupper(get_menu());
88 case 'Q': // quits the sample program
90 g_main_loop_quit(mainloop);
93 case 'I': // Initialize interface
96 case 'S': // start server
97 start_listening_server();
100 case 'C': // start client
101 start_discovery_server();
104 case 'F': // find resource
108 case 'R': // send request
112 case 'A': // advertise resource
113 advertise_resource();
116 case 'N': // select network
120 case 'X': // unselect network
124 case 'H': // handle request response
125 handle_request_response();
128 case 'T': // Terminate interface
133 printf("not supported menu!!\n");
142 mainloop = g_main_loop_new(NULL, FALSE);
143 pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
149 printf("select default network(WIFI)\n");
150 CASelectNetwork(CA_WIFI);
153 CARegisterHandler(request_handler, response_handler);
156 void start_listening_server()
158 printf("start listening server!!\n");
160 CAStartListeningServer();
163 void start_discovery_server()
165 printf("start discovery server at client!!\n");
167 CAStartDiscoveryServer();
172 char buf[MAX_BUF_LEN];
174 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
176 printf("\n=============================================\n");
177 printf("ex) a/light\n");
178 printf("reference uri : ");
182 CAResult_t res = CAFindResource(buf);
184 if (res != CA_STATUS_OK)
186 printf("find resource error:%d !!\n", res);
190 printf("find resource for %s URI\n", buf);
193 printf("=============================================\n");
198 char buf[MAX_BUF_LEN];
200 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
202 printf("\n=============================================\n");
203 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
204 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
209 // create remote endpoint
210 CARemoteEndpoint_t *endpoint = NULL;
211 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
213 if (res != CA_STATUS_OK)
215 printf("create remote endpoint error!!");
216 CADestroyRemoteEndpoint(endpoint);
221 CAToken_t token = NULL;
222 res = CAGenerateToken(&token);
224 if (res != CA_STATUS_OK)
226 printf("token generate error!!");
230 printf("generated token %s\n", (token != NULL) ? token : "");
232 CAInfo_t requestData;
233 memset(&requestData, 0, sizeof(CAInfo_t));
234 requestData.token = token;
235 requestData.payload = "Temp Json Payload";
237 CARequestInfo_t requestInfo;
238 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
239 requestInfo.method = CA_GET;
240 requestInfo.info = requestData;
243 CASendRequest(endpoint, &requestInfo);
247 CADestroyToken(token);
250 // destroy remote endpoint
251 if (endpoint != NULL)
253 CADestroyRemoteEndpoint(endpoint);
256 printf("=============================================\n");
259 void advertise_resource()
261 char buf[MAX_BUF_LEN];
263 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
265 printf("\n=============================================\n");
271 char optionData[MAX_OPT_LEN];
273 printf("Option Num : ");
274 scanf("%d", &optionNum);
275 CAHeaderOption_t *headerOpt;
279 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
280 if (NULL == headerOpt)
282 printf("memory allocation failed!\n");
285 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
289 for (i = 0; i < optionNum; i++)
292 printf("[%d] Option ID : ", i + 1);
293 scanf("%d", &optionID);
294 headerOpt[i].optionID = optionID;
296 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
297 printf("[%d] Option Data : ", i + 1);
298 scanf("%s", optionData);
299 memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
300 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
302 headerOpt[i].optionLength = (uint16_t) strlen(optionData);
304 printf("\n=============================================\n");
306 CAAdvertiseResource(buf, headerOpt, (uint8_t) optionNum);
312 void send_notification()
314 char buf[MAX_BUF_LEN];
316 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
318 printf("\n=============================================\n");
319 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
320 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
325 // create remote endpoint
326 CARemoteEndpoint_t *endpoint = NULL;
327 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
329 if (res != CA_STATUS_OK)
331 printf("create remote endpoint error!!");
332 CADestroyRemoteEndpoint(endpoint);
336 CAInfo_t respondeData;
337 memset(&respondeData, 0, sizeof(CAInfo_t));
338 respondeData.token = "client token";
339 respondeData.payload = "Temp Notification Data";
341 CAResponseInfo_t responseInfo;
342 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
343 responseInfo.result = CA_CONTENT;
344 responseInfo.info = respondeData;
347 CASendNotification(endpoint, &responseInfo);
349 // destroy remote endpoint
350 if (endpoint != NULL)
352 CADestroyRemoteEndpoint(endpoint);
354 printf("\n=============================================\n");
357 void select_network()
359 char buf[MAX_BUF_LEN];
361 printf("\n=============================================\n");
362 printf("\tselect network\n");
363 printf("ETHERNET : 0\n");
364 printf("WIFI : 1\n");
369 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
372 int number = buf[0] - '0';
374 number = (number < 0 || number > 3) ? 1 : number;
376 CASelectNetwork(1 << number);
378 printf("=============================================\n");
381 void unselect_network()
383 char buf[MAX_BUF_LEN];
385 printf("\n=============================================\n");
386 printf("\tunselect enabled network\n");
387 printf("ETHERNET : 0\n");
388 printf("WIFI : 1\n");
393 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
396 int number = buf[0] - '0';
398 number = (number < 0 || number > 3) ? 1 : number;
400 CAUnSelectNetwork(1 << number);
401 printf("Terminating...\n");
403 //pthread_join(thread, NULL);
404 printf("=============================================\n");
409 char buf[MAX_BUF_LEN];
411 printf("\n=============================================\n");
412 printf("\t\tMenu\n");
413 printf("\ti : Initialize\n");
414 printf("\ts : start server\n");
415 printf("\tc : start client\n");
416 printf("\tf : find resource\n");
417 printf("\tr : send request\n");
418 printf("\ta : advertise resource\n");
419 printf("\tn : select network\n");
420 printf("\tx : unselect network\n");
421 printf("\th : handle request response\n");
422 printf("\tt : terminate\n");
423 printf("\tq : quit\n");
424 printf("=============================================\n");
427 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
434 void handle_request_response()
436 printf("handle_request_response\n");
437 CAHandleRequestResponse();
440 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
443 printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
444 (object != NULL) ? object->resourceUri : "",
445 (requestInfo != NULL) ? requestInfo->info.payload : "");
447 printf("[CALLBACK] request_handler, address : %s\n",
448 (object != NULL) ? object->addressInfo.IP.ipAddress : "");
450 if (requestInfo->info.options)
452 uint32_t len = requestInfo->info.numOptions;
454 for (i = 0; i < len; i++)
456 printf("[CALLBACK] request_handler, option ID : %d\n",
457 requestInfo->info.options[i].optionID);
458 printf("[CALLBACK] request_handler, options data length : %d\n",
459 requestInfo->info.options[i].optionLength);
460 printf("[CALLBACK] request_handler, options data : %s\n",
461 requestInfo->info.options[i].optionData);
465 printf("send response with URI\n");
466 send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
470 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
473 printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
474 (object != NULL) ? object->resourceUri : "",
475 (responseInfo != NULL) ? responseInfo->info.payload : "");
477 printf("[CALLBACK] response_handler, address : %s\n",
478 (object != NULL) ? object->addressInfo.IP.ipAddress : "");
480 if (responseInfo->info.options)
482 uint32_t len = responseInfo->info.numOptions;
484 for (i = 0; i < len; i++)
486 printf("[CALLBACK] response_handler, option ID : %d\n",
487 responseInfo->info.options[i].optionID);
488 printf("[CALLBACK] response_handler, options data length : %d\n",
489 responseInfo->info.options[i].optionLength);
490 printf("[CALLBACK] response_handler, options data : %s\n",
491 responseInfo->info.options[i].optionData);
497 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
500 printf("\n=============================================\n");
502 CAInfo_t responseData;
503 //responseData = (CAInfo*) malloc(sizeof(CAInfo));
504 memset(&responseData, 0, sizeof(CAInfo_t));
505 responseData.token = request_token;
506 responseData.payload = "response payload";
508 CAResponseInfo_t responseInfo;
509 //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
510 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
511 responseInfo.result = 203;
512 responseInfo.info = responseData;
514 // send request (connectivityType from remoteEndpoint of request Info)
515 CASendResponse(endpoint, &responseInfo);
517 printf("=============================================\n");
521 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
524 printf("\n=============================================\n");
526 CAInfo_t requestData;
527 memset(&requestData, 0, sizeof(CAInfo_t));
528 requestData.token = token;
529 requestData.payload = "Temp Json Payload";
531 CARequestInfo_t requestInfo;
532 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
533 requestInfo.method = CA_GET;
534 requestInfo.info = requestData;
537 endpoint->connectivityType = CA_WIFI;
538 CASendRequest(endpoint, &requestInfo);
540 printf("=============================================\n");