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);
61 CAConnectivityType_t get_network_type();
66 g_main_loop_run(mainloop);
72 printf("=============================================\n");
73 printf("\t\tsample main\n");
74 printf("=============================================\n");
85 char menu = toupper(get_menu());
89 case 'Q': // quits the sample program
91 g_main_loop_quit(mainloop);
94 case 'I': // Initialize interface
98 case 'S': // start server
99 start_listening_server();
102 case 'C': // start client
103 start_discovery_server();
106 case 'F': // find resource
110 case 'R': // send request
114 case 'A': // advertise resource
115 advertise_resource();
118 case 'N': // select network
122 case 'X': // unselect network
126 case 'H': // handle request response
127 handle_request_response();
130 case 'T': // Terminate interface
135 printf("not supported menu!!\n");
144 mainloop = g_main_loop_new(NULL, FALSE);
145 pthread_create (&thread, NULL, (void *) &pthread_func, NULL);
151 printf("select default network(WIFI)\n");
152 CASelectNetwork(CA_WIFI);
155 CARegisterHandler(request_handler, response_handler);
158 void start_listening_server()
160 printf("start listening server!!\n");
162 CAStartListeningServer();
165 void start_discovery_server()
167 printf("start discovery server at client!!\n");
169 CAStartDiscoveryServer();
174 char buf[MAX_BUF_LEN];
176 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
178 printf("\n=============================================\n");
179 printf("ex) a/light\n");
180 printf("reference uri : ");
185 CAToken_t token = NULL;
186 CAResult_t res = CAGenerateToken(&token);
187 if (res != CA_STATUS_OK)
189 printf("token generate error!!\n");
193 printf("generated token %s\n", (token != NULL) ? token : "");
195 res = CAFindResource(buf, token);
197 if (res != CA_STATUS_OK)
199 printf("find resource error:%d !!\n", res);
203 printf("find resource for %s URI\n", buf);
206 printf("=============================================\n");
211 char buf[MAX_BUF_LEN];
213 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
214 CAConnectivityType_t selectedNetwork;
216 selectedNetwork = get_network_type();
218 printf("\n=============================================\n");
219 printf("10.11.12.13:4545/resource_uri ( for IP )\n");
220 printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
225 // create remote endpoint
226 CARemoteEndpoint_t *endpoint = NULL;
227 CAResult_t res = CACreateRemoteEndpoint(buf,selectedNetwork, &endpoint);
229 if (res != CA_STATUS_OK)
231 printf("create remote endpoint error!!");
232 CADestroyRemoteEndpoint(endpoint);
237 CAToken_t token = NULL;
238 res = CAGenerateToken(&token);
240 if (res != CA_STATUS_OK)
242 printf("token generate error!!");
246 printf("generated token %s\n", (token != NULL) ? token : "");
248 CAInfo_t requestData;
249 memset(&requestData, 0, sizeof(CAInfo_t));
250 requestData.token = token;
251 requestData.payload = "Temp Json Payload";
253 CARequestInfo_t requestInfo;
254 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
255 requestInfo.method = CA_GET;
256 requestInfo.info = requestData;
259 CASendRequest(endpoint, &requestInfo);
263 CADestroyToken(token);
266 // destroy remote endpoint
267 if (endpoint != NULL)
269 CADestroyRemoteEndpoint(endpoint);
272 printf("=============================================\n");
275 void advertise_resource()
277 char buf[MAX_BUF_LEN];
279 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
281 printf("\n=============================================\n");
287 char optionData[MAX_OPT_LEN];
289 printf("Option Num : ");
290 scanf("%d", &optionNum);
291 CAHeaderOption_t *headerOpt;
295 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
296 if (NULL == headerOpt)
298 printf("memory allocation failed!\n");
301 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
305 for (i = 0 ; i < optionNum ; i++)
308 printf("[%d] Option ID : ", i + 1);
309 scanf("%d", &optionID);
310 headerOpt[i].optionID = optionID;
312 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
313 printf("[%d] Option Data : ", i + 1);
314 scanf("%s", optionData);
315 memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
316 printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
318 headerOpt[i].optionLength = (uint16_t)strlen(optionData);
320 printf("\n=============================================\n");
323 CAToken_t token = NULL;
324 CAResult_t res = CAGenerateToken(&token);
325 if (res != CA_STATUS_OK)
327 printf("token generate error!!\n");
331 printf("generated token %s\n", (token != NULL) ? token : "");
333 CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
339 void select_network()
341 char buf[MAX_BUF_LEN];
343 printf("\n=============================================\n");
344 printf("\tselect network\n");
345 printf("ETHERNET : 0\n");
346 printf("WIFI : 1\n");
351 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
354 int number = buf[0] - '0';
356 number = (number < 0 || number > 3) ? 1 : number;
358 CASelectNetwork(1 << number);
360 printf("=============================================\n");
363 void unselect_network()
365 char buf[MAX_BUF_LEN];
367 printf("\n=============================================\n");
368 printf("\tunselect enabled network\n");
369 printf("ETHERNET : 0\n");
370 printf("WIFI : 1\n");
375 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
378 int number = buf[0] - '0';
380 number = (number < 0 || number > 3) ? 1 : number;
382 CAUnSelectNetwork(1 << number);
383 printf("Terminating...\n");
385 //pthread_join(thread, NULL);
386 printf("=============================================\n");
391 char buf[MAX_BUF_LEN];
393 printf("\n=============================================\n");
394 printf("\t\tMenu\n");
395 printf("\ti : Initialize\n");
396 printf("\ts : start server\n");
397 printf("\tc : start client\n");
398 printf("\tf : find resource\n");
399 printf("\tr : send request\n");
400 printf("\ta : advertise resource\n");
401 printf("\tn : select network\n");
402 printf("\tx : unselect network\n");
403 printf("\th : handle request response\n");
404 printf("\tt : terminate\n");
405 printf("\tq : quit\n");
406 printf("=============================================\n");
409 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
416 void handle_request_response()
418 printf("handle_request_response\n");
419 CAHandleRequestResponse();
422 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
425 printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
426 (object != NULL) ? object->resourceUri : "",
427 (requestInfo != NULL) ? requestInfo->info.payload : "");
429 printf("[CALLBACK] request_handler, address : %s\n",
430 (object != NULL) ? object->addressInfo.IP.ipAddress : "");
432 if (requestInfo->info.options)
434 uint32_t len = requestInfo->info.numOptions;
436 for (i = 0 ; i < len ; i++)
438 printf("[CALLBACK] request_handler, option ID : %d\n", requestInfo->info.options[i].optionID);
439 printf("[CALLBACK] request_handler, options data length : %d\n",
440 requestInfo->info.options[i].optionLength);
441 printf("[CALLBACK] request_handler, options data : %s\n", requestInfo->info.options[i].optionData );
445 printf("send response with URI\n");
446 send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
450 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
453 printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
454 (object != NULL) ? object->resourceUri : "",
455 (responseInfo != NULL) ? responseInfo->info.payload : "");
457 printf("[CALLBACK] response_handler, address : %s\n",
458 (object != NULL) ? object->addressInfo.IP.ipAddress : "");
460 if (responseInfo->info.options)
462 uint32_t len = responseInfo->info.numOptions;
464 for (i = 0 ; i < len ; i++)
466 printf("[CALLBACK] response_handler, option ID : %d\n", responseInfo->info.options[i].optionID);
467 printf("[CALLBACK] response_handler, options data length : %d\n",
468 responseInfo->info.options[i].optionLength);
469 printf("[CALLBACK] response_handler, options data : %s\n",
470 responseInfo->info.options[i].optionData );
474 //printf("send request with URI\n");
475 //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
478 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
481 printf("\n=============================================\n");
483 CAInfo_t responseData;
484 //responseData = (CAInfo*) malloc(sizeof(CAInfo));
485 memset(&responseData, 0, sizeof(CAInfo_t));
486 responseData.token = request_token;
487 responseData.payload = "response payload";
489 CAResponseInfo_t responseInfo;
490 //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
491 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
492 responseInfo.result = 203;
493 responseInfo.info = responseData;
495 // send request (connectivityType from remoteEndpoint of request Info)
496 CASendResponse(endpoint, &responseInfo);
498 printf("=============================================\n");
502 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
505 printf("\n=============================================\n");
507 CAInfo_t requestData;
508 memset(&requestData, 0, sizeof(CAInfo_t));
509 requestData.token = token;
510 requestData.payload = "Temp Json Payload";
512 CARequestInfo_t requestInfo;
513 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
514 requestInfo.method = CA_GET;
515 requestInfo.info = requestData;
518 endpoint->connectivityType = CA_WIFI;
519 CASendRequest(endpoint, &requestInfo);
521 printf("=============================================\n");
530 CAConnectivityType_t get_network_type()
532 char buf[MAX_BUF_LEN];
534 printf("\n=============================================\n");
535 printf("\tselect network type\n");
536 printf("ETHERNET : 0\n");
537 printf("WIFI : 1\n");
542 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
545 int number = buf[0] - '0';
547 number = (number < 0 || number > 3) ? 0 : 1 << number;
549 if (number & CA_ETHERNET)
553 if (number & CA_WIFI)
566 printf("\n=============================================\n");