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);
70 printf("=============================================\n");
71 printf("\t\tsample main\n");
72 printf("=============================================\n");
83 char menu = toupper(get_menu());
87 case 'Q': // quits the sample program
89 g_main_loop_quit(mainloop);
92 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 : ");
183 CAToken_t token = NULL;
184 CAResult_t res = CAGenerateToken(&token);
185 if (res != CA_STATUS_OK)
187 printf("token generate error!!\n");
191 printf("generated token %s\n", (token != NULL) ? token : "");
193 res = CAFindResource(buf, token);
195 if (res != CA_STATUS_OK)
197 printf("find resource error:%d !!\n", res);
201 printf("find resource for %s URI\n", buf);
204 printf("=============================================\n");
209 char buf[MAX_BUF_LEN];
211 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
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");
220 // create remote endpoint
221 CARemoteEndpoint_t *endpoint = NULL;
222 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
224 if (res != CA_STATUS_OK)
226 printf("create remote endpoint error!!");
227 CADestroyRemoteEndpoint(endpoint);
232 CAToken_t token = NULL;
233 res = CAGenerateToken(&token);
235 if (res != CA_STATUS_OK)
237 printf("token generate error!!");
241 printf("generated token %s\n", (token != NULL) ? token : "");
243 CAInfo_t requestData;
244 memset(&requestData, 0, sizeof(CAInfo_t));
245 requestData.token = token;
246 requestData.payload = "Temp Json Payload";
248 CARequestInfo_t requestInfo;
249 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
250 requestInfo.method = CA_GET;
251 requestInfo.info = requestData;
254 CASendRequest(endpoint, &requestInfo);
258 CADestroyToken(token);
261 // destroy remote endpoint
262 if (endpoint != NULL)
264 CADestroyRemoteEndpoint(endpoint);
267 printf("=============================================\n");
270 void advertise_resource()
272 char buf[MAX_BUF_LEN];
274 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
276 printf("\n=============================================\n");
282 char optionData[MAX_OPT_LEN];
284 printf("Option Num : ");
285 scanf("%d", &optionNum);
286 CAHeaderOption_t *headerOpt;
290 headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
291 if (NULL == headerOpt)
293 printf("memory allocation failed!\n");
296 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
300 for (i = 0 ; i < optionNum ; i++)
303 printf("[%d] Option ID : ", i + 1);
304 scanf("%d", &optionID);
305 headerOpt[i].optionID = optionID;
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 );
313 headerOpt[i].optionLength = (uint16_t)strlen(optionData);
315 printf("\n=============================================\n");
318 CAToken_t token = NULL;
319 CAResult_t res = CAGenerateToken(&token);
320 if (res != CA_STATUS_OK)
322 printf("token generate error!!\n");
326 printf("generated token %s\n", (token != NULL) ? token : "");
328 CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
334 void select_network()
336 char buf[MAX_BUF_LEN];
338 printf("\n=============================================\n");
339 printf("\tselect network\n");
340 printf("ETHERNET : 0\n");
341 printf("WIFI : 1\n");
346 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
349 int number = buf[0] - '0';
351 number = (number < 0 || number > 3) ? 1 : number;
353 CASelectNetwork(1 << number);
355 printf("=============================================\n");
358 void unselect_network()
360 char buf[MAX_BUF_LEN];
362 printf("\n=============================================\n");
363 printf("\tunselect enabled network\n");
364 printf("ETHERNET : 0\n");
365 printf("WIFI : 1\n");
370 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
373 int number = buf[0] - '0';
375 number = (number < 0 || number > 3) ? 1 : number;
377 CAUnSelectNetwork(1 << number);
378 printf("Terminating...\n");
380 //pthread_join(thread, NULL);
381 printf("=============================================\n");
386 char buf[MAX_BUF_LEN];
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");
404 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
411 void handle_request_response()
413 printf("handle_request_response\n");
414 CAHandleRequestResponse();
417 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
420 printf("[CALLBACK] request_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
421 (requestInfo != NULL) ? requestInfo->info.payload : "");
423 printf("[CALLBACK] request_handler, address : %s\n", (object != NULL) ? object->addressInfo.IP.ipAddress : "");
425 if(requestInfo->info.options)
427 uint32_t len = requestInfo->info.numOptions;
429 for(i = 0 ; i < len ; i++)
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 );
437 printf("send response with URI\n");
438 send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
442 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
445 printf("[CALLBACK] response_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
446 (responseInfo != NULL) ? responseInfo->info.payload : "");
448 printf("[CALLBACK] response_handler, address : %s\n", (object != NULL) ? object->addressInfo.IP.ipAddress : "");
450 if(responseInfo->info.options)
452 uint32_t len = responseInfo->info.numOptions;
454 for(i = 0 ; i < len ; i++)
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 );
462 //printf("send request with URI\n");
463 //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
466 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
469 printf("\n=============================================\n");
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";
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;
483 // send request (connectivityType from remoteEndpoint of request Info)
484 CASendResponse(endpoint, &responseInfo);
486 printf("=============================================\n");
490 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
493 printf("\n=============================================\n");
495 CAInfo_t requestData;
496 memset(&requestData, 0, sizeof(CAInfo_t));
497 requestData.token = token;
498 requestData.payload = "Temp Json Payload";
500 CARequestInfo_t requestInfo;
501 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
502 requestInfo.method = CA_GET;
503 requestInfo.info = requestData;
506 endpoint->connectivityType = CA_WIFI;
507 CASendRequest(endpoint, &requestInfo);
509 printf("=============================================\n");