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 ******************************************************************/
29 #include "cainterface.h"
30 #include <TimedAction.h>
33 #include "oic_malloc.h"
35 #define MAX_BUF_LEN 100 //1024
36 #define MAX_OPT_LEN 100
38 #define printf Serial.println
45 void start_listening_server();
46 void start_discovery_server();
49 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token);
50 void advertise_resource();
51 void send_notification();
52 void select_network();
53 void unselect_network();
54 void handle_request_response();
56 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
57 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
58 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
61 void getData(char *readInput, int bufferLength, int *dataLength)
63 while (!Serial.available());
65 while (Serial.available())
68 char c = Serial.read();
69 if ('\n' != c && '\r' != c && len < bufferLength - 1)
79 readInput[len] = '\0';
82 Serial.println(readInput);
86 CAConnectivityType_t getConnectivityType()
89 memset(type, 0, sizeof(char));
90 printf("Select network");
91 printf("ETHERNET: 0");
98 getData(type, sizeof(type), &typeLen);
99 int num = type[0] - '0';
114 Serial.begin (115200);
116 printf("============");
118 printf("============");
125 memset(buffer, 0, sizeof(buffer));
127 if (Serial.available() > 0)
129 getData(buffer, sizeof(buffer), &len);
130 switch (toupper(buffer[0]))
140 case 'I': // Initialize interface
144 case 'S': // start listening server
145 start_listening_server();
148 case 'D': // start discovery server
149 start_discovery_server();
152 case 'F': // find resource
156 case 'R': // send request
160 case 'A': // advertise resource
161 advertise_resource();
164 case 'B': // send notification
168 case 'N': // select network
172 case 'X': // unselect network
176 case 'H': // handle request response
177 handle_request_response();
180 case 'T': // handle request response
185 printf("wrong menu");
189 //1:Add check for startserver before calling below api
190 handle_request_response();
198 CARegisterHandler(request_handler, response_handler);
201 void start_listening_server()
203 printf("start server");
204 CAStartListeningServer();
207 void start_discovery_server()
209 printf("start server");
210 CAStartDiscoveryServer();
215 char buf[MAX_BUF_LEN];
216 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
218 printf("============");
219 printf("ex) a/light");
222 getData(buf, sizeof(buf), &len);
225 CAToken_t token = NULL;
226 CAResult_t res = CAGenerateToken(&token);
227 if (res != CA_STATUS_OK)
229 printf("token generate error!!");
234 printf((token != NULL) ? token : "");
236 res = CAFindResource(buf, token);
237 if (res != CA_STATUS_OK)
239 printf("find error");
246 CADestroyToken(token);
251 char buf[MAX_BUF_LEN];
252 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
254 printf("============");
255 printf("10.11.12.13:4545/res_uri (for IP)");
256 printf("10:11:12:13:45:45/res_uri (for BT)");
260 getData(buf, sizeof(buf), &len);
262 // create remote endpoint
263 CARemoteEndpoint_t *endpoint = NULL;
264 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
265 if (res != CA_STATUS_OK)
267 printf("Out of memory");
268 CADestroyRemoteEndpoint(endpoint);
271 endpoint->connectivityType = getConnectivityType();
273 CAToken_t token = NULL;
274 res = CAGenerateToken(&token);
275 if (res != CA_STATUS_OK)
277 printf("token error");
281 printf((token != NULL) ? token : "");
283 CAInfo_t requestData;
284 memset(&requestData, 0, sizeof(CAInfo_t));
285 requestData.token = token;
286 requestData.payload = "Json Payload";
288 CARequestInfo_t requestInfo;
289 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
290 requestInfo.method = CA_GET;
291 requestInfo.info = requestData;
294 CASendRequest(endpoint, &requestInfo);
297 CADestroyToken(token);
300 // destroy remote endpoint
301 if (endpoint != NULL)
303 CADestroyRemoteEndpoint(endpoint);
306 printf("============");
309 void advertise_resource()
311 char buf[MAX_BUF_LEN];
312 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
314 printf("============");
318 getData(buf, sizeof(buf), &len);
320 int16_t optionNum = 0;
321 char optionData[MAX_OPT_LEN];
322 char optionNumBuf[2];
324 printf("Option Num: ");
325 getData(optionNumBuf, sizeof(optionNumBuf), &len);
326 optionNum = optionNumBuf[0] - '0';
330 CAHeaderOption_t *headerOpt = NULL;
333 headerOpt = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * optionNum);
334 if (NULL == headerOpt)
336 printf("Out of memory");
339 memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
343 for (i = 0 ; i < optionNum ; i++)
347 getData(getOptionID, sizeof(getOptionID), &len);
348 printf("Option Num: ");
350 optionID = getOptionID[0];
353 headerOpt[i].optionID = optionID;
354 memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
355 printf("Option Data itr");
359 getData(optionData, sizeof(optionData), &len);
360 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
365 headerOpt[i].optionLength = (uint16_t)strlen(optionData);
368 printf("============");
370 CAToken_t token = NULL;
371 CAResult_t res = CAGenerateToken(&token);
372 if (res != CA_STATUS_OK)
374 printf("token generate error!!");
379 printf((token != NULL) ? token : "");
381 CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
382 if (NULL != headerOpt)
386 CADestroyToken(token);
389 void send_notification()
391 char buf[MAX_BUF_LEN];
392 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
394 printf("============");
395 printf("10.11.12.13:4545/res_uri (for IP)");
396 printf("10:11:12:13:45:45/res_uri (for BT)");
400 getData(buf, sizeof(buf), &len);
402 // create remote endpoint
403 CARemoteEndpoint_t *endpoint = NULL;
404 CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
405 if (CA_STATUS_OK != res)
407 printf("Out of memory");
408 CADestroyRemoteEndpoint(endpoint);
411 endpoint->connectivityType = getConnectivityType();
412 CAInfo_t respondeData;
413 memset(&respondeData, 0, sizeof(CAInfo_t));
414 respondeData.token = "token";
415 respondeData.payload = "Notification Data";
417 CAResponseInfo_t responseInfo;
418 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
419 responseInfo.result = CA_SUCCESS;
420 responseInfo.info = respondeData;
423 CASendNotification(endpoint, &responseInfo);
424 // destroy remote endpoint
425 if (NULL != endpoint)
427 CADestroyRemoteEndpoint(endpoint);
429 printf("============");
432 void select_network()
434 char buf[MAX_BUF_LEN];
435 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
437 printf("============");
438 printf("Select network");
439 printf("ETHERNET: 0");
445 getData(buf, sizeof(buf), &len);
446 int number = buf[0] - '0';
447 number = (number < 0 || number > 3) ? 1 : number;
448 CASelectNetwork(1 << number);
449 printf("============");
452 void unselect_network()
454 char buf[MAX_BUF_LEN];
455 memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
457 printf("============");
458 printf("Unselect network");
459 printf("ETHERNET: 0");
465 getData(buf, sizeof(buf), &len);
466 int number = buf[0] - '0';
468 number = (number < 0 || number > 3) ? 1 : number;
469 CAUnSelectNetwork(1 << number);
472 printf("============");
478 printf("============");
479 printf("i: Initialize");
480 printf("s: start listening server");
481 printf("d: start discovery server");
482 printf("f: find resource");
483 printf("r: send request");
484 printf("a: advertise resource");
485 printf("b: send notification");
486 printf("n: select network");
487 printf("x: unselect network");
488 printf("h: handle request response");
489 printf("t: terminate");
491 printf("============");
494 void handle_request_response()
496 CAHandleRequestResponse();
499 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
502 printf((object != NULL) ? object->resourceUri : "");
504 printf((requestInfo != NULL) ? requestInfo->info.payload : "");
506 printf("send response");
507 send_response((CARemoteEndpoint_t *)object, requestInfo->info.token);
510 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
513 printf((object != NULL) ? object->resourceUri : "");
515 printf((responseInfo != NULL) ? responseInfo->info.payload : "");
518 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
521 printf("============");
523 CAInfo_t responseData;
524 memset(&responseData, 0, sizeof(CAInfo_t));
525 responseData.token = request_token;
526 responseData.payload = "response payload";
528 CAResponseInfo_t responseInfo;
529 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
530 responseInfo.result = (CAResponseResult_t)203;
531 responseInfo.info = responseData;
533 // send request (connectivityType from remoteEndpoint of request Info)
534 CASendResponse(endpoint, &responseInfo);
536 printf("============");
540 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
543 printf("============");
545 CAInfo_t requestData;
546 memset(&requestData, 0, sizeof(CAInfo_t));
547 requestData.token = token;
548 requestData.payload = "Json Payload";
550 CARequestInfo_t requestInfo;
551 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
552 requestInfo.method = CA_GET;
553 requestInfo.info = requestData;
556 endpoint->connectivityType = CA_WIFI;
557 CASendRequest(endpoint, &requestInfo);
559 printf("============");