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 ******************************************************************/
26 #include "cainterface.h"
28 #define MAX_BUF_LEN 1024
29 #define MAX_OPT_LEN 16
36 void start_listening_server();
37 void start_discovery_server();
41 void advertise_resource();
42 void send_notification();
43 void select_network();
44 void unselect_network();
45 void handle_request_response();
46 void find_fixed_resource();
47 void get_network_info();
49 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
50 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
51 void send_request_tmp(CARemoteEndpoint_t* endpoint, CAToken_t token);
54 static CAToken_t gLastRequestToken = NULL;
60 printf("=============================================\n");
61 printf("\t\tsample main\n");
62 printf("=============================================\n");
64 CAResult_t res = CAInitialize();
65 if (res != CA_STATUS_OK)
67 printf("CAInitialize fail\n");
73 printf("select default network(WIFI)\n");
74 res = CASelectNetwork(CA_WIFI);
75 if (res != CA_STATUS_OK)
77 printf("CASelectNetwork fail\n");
82 res = CARegisterHandler(request_handler, response_handler);
83 if (res != CA_STATUS_OK)
85 printf("CARegisterHandler fail\n");
100 char menu = get_menu();
110 printf("quit..!!\n");
113 case 's': // start server
115 start_listening_server();
118 case 'c': // start client
120 start_discovery_server();
123 case 'f': // find resource
128 case 'r': // send request
133 case 'a': // advertise resource
135 advertise_resource();
138 case 'b': // send notification
143 case 'n': // select network
148 case 'x': // unselect network
153 case 'h': // handle request response
155 handle_request_response();
162 find_fixed_resource();
163 while (received == FALSE)
166 handle_request_response();
173 start_listening_server();
177 handle_request_response();
181 case 'g': // get network information
187 printf("not supported menu!!\n");
194 void start_listening_server()
196 printf("start listening server!!\n");
198 CAResult_t res = CAStartListeningServer();
199 if (res != CA_STATUS_OK)
201 printf("start listening server fail\n");
205 printf("start listening server success\n");
209 void start_discovery_server()
211 printf("start discovery client!!\n");
213 CAResult_t res = CAStartDiscoveryServer();
214 if (res != CA_STATUS_OK)
216 printf("start discovery client fail\n");
220 printf("start discovery client success\n");
224 void find_fixed_resource()
226 char buf[MAX_BUF_LEN] =
228 strcpy(buf, "a/light");
233 CAToken_t token = NULL;
234 CAResult_t res = CAGenerateToken(&token);
235 if (res != CA_STATUS_OK)
237 printf("token generate error!!");
241 printf("generated token %s\n", (token != NULL) ? token : "");
243 res = CAFindResource(buf, token);
244 if (res != CA_STATUS_OK)
246 printf("find resource error!!\n");
250 printf("find resource to %s URI\n", buf);
257 CADestroyToken(token);
261 printf("=============================================\n");
266 char buf[MAX_BUF_LEN];
268 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
270 printf("\n=============================================\n");
271 printf("ex) a/light\n");
272 printf("reference uri : ");
277 CAToken_t token = NULL;
278 CAResult_t res = CAGenerateToken(&token);
279 if (res != CA_STATUS_OK)
281 printf("token generate error!!\n");
285 printf("generated token %s\n", (token != NULL) ? token : "");
287 res = CAFindResource(buf, token);
288 if (res != CA_STATUS_OK)
290 printf("find resource error!!\n");
294 printf("find resource to %s URI\n", buf);
296 gLastRequestToken = token;
303 CADestroyToken(token);
307 printf("=============================================\n");
312 char buf[MAX_BUF_LEN];
314 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
316 printf("\n=============================================\n");
317 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
318 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
323 // create remote endpoint
324 CARemoteEndpoint_t* endpoint = NULL;
325 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
327 if (res != CA_STATUS_OK)
329 printf("create remote endpoint error!!\n");
330 CADestroyRemoteEndpoint(endpoint);
335 CAToken_t token = NULL;
336 res = CAGenerateToken(&token);
338 if (res != CA_STATUS_OK)
340 printf("token generate error!!\n");
344 printf("generated token %s\n", (token != NULL) ? token : "");
346 CAInfo_t requestData;
347 memset(&requestData, 0, sizeof(CAInfo_t));
348 requestData.token = token;
349 requestData.payload = "Temp Json Payload";
351 CARequestInfo_t requestInfo;
352 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
353 requestInfo.method = CA_GET;
354 requestInfo.info = requestData;
357 CASendRequest(endpoint, &requestInfo);
361 CADestroyToken(token);
364 // destroy remote endpoint
365 if (endpoint != NULL)
367 CADestroyRemoteEndpoint(endpoint);
370 printf("=============================================\n");
373 void advertise_resource()
375 char buf[MAX_BUF_LEN];
377 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
379 printf("\n=============================================\n");
385 char optionData[MAX_OPT_LEN];
387 printf("Option Num : ");
388 scanf("%d", &optionNum);
389 CAHeaderOption_t *headerOpt;
390 headerOpt = (CAHeaderOption_t*) malloc(sizeof(CAHeaderOption_t) * optionNum);
391 if (NULL == headerOpt)
393 printf("Memory allocation failed!\n");
396 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
399 for (i = 0; i < optionNum; i++)
402 printf("[%d] Option ID : ", i + 1);
403 scanf("%d", &optionID);
404 headerOpt[i].optionID = optionID;
406 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
407 printf("[%d] Option Data : ", i + 1);
408 scanf("%s", optionData);
409 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
410 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
412 headerOpt[i].optionLength = (uint16_t) strlen(optionData);
414 printf("\n=============================================\n");
417 CAToken_t token = NULL;
418 CAResult_t res = CAGenerateToken(&token);
419 if (res != CA_STATUS_OK)
421 printf("token generate error!!\n");
425 printf("generated token %s\n", (token != NULL) ? token : "");
427 CAAdvertiseResource(buf, token, headerOpt, (uint8_t) optionNum);
433 CADestroyToken(token);
441 void send_notification()
443 char buf[MAX_BUF_LEN];
445 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
447 printf("\n=============================================\n");
448 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
449 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
454 // create remote endpoint
455 CARemoteEndpoint_t* endpoint = NULL;
456 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
458 if (res != CA_STATUS_OK)
460 printf("create remote endpoint error!!\n");
461 CADestroyRemoteEndpoint(endpoint);
465 CAInfo_t respondeData;
466 memset(&respondeData, 0, sizeof(CAInfo_t));
467 respondeData.token = "client token";
468 respondeData.payload = "Temp Notification Data";
470 CAResponseInfo_t responseInfo;
471 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
472 responseInfo.result = CA_CONTENT;
473 responseInfo.info = respondeData;
476 res = CASendNotification(endpoint, &responseInfo);
477 if (res != CA_STATUS_OK)
479 printf("send notification error\n");
483 printf("send notification success\n");
486 // destroy remote endpoint
487 if (endpoint != NULL)
489 CADestroyRemoteEndpoint(endpoint);
492 printf("\n=============================================\n");
495 void select_network()
497 char buf[MAX_BUF_LEN];
499 printf("\n=============================================\n");
500 printf("\tselect network\n");
501 printf("ETHERNET : 0\n");
502 printf("WIFI : 1\n");
507 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
510 int number = buf[0] - '0';
512 number = (number < 0 || number > 3) ? 1 : number;
514 CAResult_t res = CASelectNetwork(1 << number);
515 if (res != CA_STATUS_OK)
517 printf("select network error\n");
521 printf("select network success\n");
524 printf("=============================================\n");
527 void unselect_network()
529 char buf[MAX_BUF_LEN];
531 printf("\n=============================================\n");
532 printf("\tunselect enabled network\n");
533 printf("ETHERNET : 0\n");
534 printf("WIFI : 1\n");
539 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
542 int number = buf[0] - '0';
544 number = (number < 0 || number > 3) ? 1 : number;
546 CAResult_t res = CAUnSelectNetwork(1 << number);
547 if (res != CA_STATUS_OK)
549 printf("unselect network error\n");
553 printf("unselect network success\n");
556 printf("=============================================\n");
561 char buf[MAX_BUF_LEN];
563 printf("\n=============================================\n");
564 printf("\t\tMenu\n");
565 printf("\ts : start server\n");
566 printf("\tc : start client\n");
567 printf("\tf : find resource\n");
568 printf("\tr : send request\n");
569 printf("\ta : advertise resource\n");
570 printf("\tb : send notification\n");
571 printf("\tn : select network\n");
572 printf("\tx : unselect network\n");
573 printf("\tg : get network information\n");
574 printf("\th : handle request response\n");
575 printf("\ty : run static client\n");
576 printf("\tz : run static server\n");
577 printf("\tq : quit\n");
578 printf("=============================================\n");
581 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
588 void handle_request_response()
590 printf("handle_request_response\n");
592 CAResult_t res = CAHandleRequestResponse();
593 if (res != CA_STATUS_OK)
595 printf("handle request error\n");
599 printf("handle request success\n");
603 void get_network_info()
607 CALocalConnectivity_t* tempInfo;
608 uint32_t tempSize = 0;
610 tempInfo = (CALocalConnectivity_t*) malloc(sizeof(CALocalConnectivity_t));
612 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
613 if (res != CA_STATUS_OK)
615 printf("get network information error\n");
619 printf("network info total size is %d\n", tempSize);
620 for (index = 0; index < tempSize; index++)
622 if (tempInfo == NULL)
627 printf("type : %d, address : %s\n",
629 tempInfo->addressInfo.IP.ipAddress);
635 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
638 printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
639 (object != NULL) ? object->resourceUri : "",
640 (requestInfo != NULL) ? requestInfo->info.payload : "");
642 printf("[CALLBACK] request_handler, uri: %s, data: %s, token: %s \n",
643 (object != NULL) ? object->resourceUri : "",
644 (requestInfo != NULL) ? requestInfo->info.payload : "",
645 (requestInfo->info.token != NULL) ? requestInfo->info.token : "");
647 if (gLastRequestToken != NULL && requestInfo->info.token != NULL
648 && (strcmp((char*)gLastRequestToken, requestInfo->info.token) == 0))
650 printf("token is same. received request of it's own. skip.. \n");
655 printf("[CALLBACK] request_handler, address : %s port:%d \n",
656 (object != NULL) ? object->addressInfo.IP.ipAddress : "",
657 (object != NULL) ? object->addressInfo.IP.port : 0);
659 if (requestInfo->info.options)
661 uint32_t len = requestInfo->info.numOptions;
663 for (i = 0; i < len; i++)
665 printf("[CALLBACK] request_handler, option ID : %d\n",
666 requestInfo->info.options[i].optionID);
667 printf("[CALLBACK] request_handler, options data length : %d\n",
668 requestInfo->info.options[i].optionLength);
669 printf("[CALLBACK] request_handler, options data : %s\n",
670 requestInfo->info.options[i].optionData);
674 printf("send response with URI\n");
675 send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
681 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
684 printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
685 (object != NULL) ? object->resourceUri : "",
686 (responseInfo != NULL) ? responseInfo->info.payload : "");
688 printf("[CALLBACK] response_handler, address : %s port :%d\n",
689 (object != NULL) ? object->addressInfo.IP.ipAddress : "",
690 (object != NULL) ? object->addressInfo.IP.port : 0);
692 if (responseInfo->info.options)
694 uint32_t len = responseInfo->info.numOptions;
696 for (i = 0; i < len; i++)
698 printf("[CALLBACK] response_handler, option ID : %d\n",
699 responseInfo->info.options[i].optionID);
700 printf("[CALLBACK] response_handler, options data length : %d\n",
701 responseInfo->info.options[i].optionLength);
702 printf("[CALLBACK] response_handler, options data : %s\n",
703 responseInfo->info.options[i].optionData);
708 //printf("send request with URI\n");
709 //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
712 void send_response(CARemoteEndpoint_t* endpoint, CAToken_t request_token)
715 printf("\n=============================================\n");
717 CAInfo_t responseData;
718 //responseData = (CAInfo*) malloc(sizeof(CAInfo));
719 memset(&responseData, 0, sizeof(CAInfo_t));
720 responseData.token = request_token;
721 responseData.payload = "response payload";
723 CAResponseInfo_t responseInfo;
724 //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
725 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
726 responseInfo.result = 203;
727 responseInfo.info = responseData;
729 // send request (connectivityType from remoteEndpoint of request Info)
730 CAResult_t res = CASendResponse(endpoint, &responseInfo);
731 if (res != CA_STATUS_OK)
733 printf("send response error\n");
737 printf("send response success\n");
740 printf("=============================================\n");
744 void send_request_tmp(CARemoteEndpoint_t* endpoint, CAToken_t token)
747 printf("\n=============================================\n");
749 CAInfo_t requestData;
750 memset(&requestData, 0, sizeof(CAInfo_t));
751 requestData.token = token;
752 requestData.payload = "Temp Json Payload";
754 CARequestInfo_t requestInfo;
755 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
756 requestInfo.method = CA_GET;
757 requestInfo.info = requestData;
760 endpoint->connectivityType = CA_WIFI;
762 CAResult_t res = CASendRequest(endpoint, &requestInfo);
763 if (res != CA_STATUS_OK)
765 printf("send request error\n");
769 printf("send request success\n");
772 printf("=============================================\n");