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"
32 #ifdef ETHERNET_ADAPTER
40 #include "oic_malloc.h"
42 #define MAX_BUF_LEN 100 //1024
43 #define MAX_OPT_LEN 16
45 static bool g_isLeSelected = false;
47 static void PrintMenu();
48 static void Process();
49 static void Initialize();
50 static void StartListeningServer();
51 static void StartDiscoveryServer();
52 static void FindResource();
53 static void SendRequest();
54 static void SendRequestAll();
55 static void SendResponse(CARemoteEndpoint_t *endpoint, const CAInfo_t* info);
56 static void AdvertiseResource();
57 static void SendNotification();
58 static void SelectNetwork();
59 static void UnselectNetwork();
60 static void HandleRequestResponse();
62 static void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
63 static void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
64 static void Terminate();
66 void GetData(char *readInput, size_t bufferLength, size_t *dataLength)
68 if (!readInput || bufferLength == 0 || !dataLength)
70 Serial.println("Invalid buffer");
74 while (!Serial.available())
79 while (Serial.available())
82 char c = Serial.read();
83 if ('\n' != c && '\r' != c && len < bufferLength - 1)
93 readInput[len] = '\0';
96 Serial.println(readInput);
100 CAConnectivityType_t GetConnectivityType()
103 Serial.println("Select network");
104 Serial.println("ETHERNET: 0");
105 Serial.println("WIFI: 1");
106 Serial.println("EDR: 2");
107 Serial.println("LE: 3");
110 GetData(type, sizeof(type), &typeLen);
113 Serial.println("i/p err,default ethernet");
132 Serial.begin (115200);
134 Serial.println("============");
135 Serial.println("CA SAMPLE");
136 Serial.println("============");
142 char buffer[5] = {0};
144 if (Serial.available() > 0)
146 GetData(buffer, sizeof(buffer), &len);
149 Serial.println("i/p err");
152 switch (toupper(buffer[0]))
159 Serial.println("quit");
162 case 'I': // Initialize interface
166 case 'S': // start listening server
167 StartListeningServer();
170 case 'D': // start discovery server
171 StartDiscoveryServer();
174 case 'F': // find resource
178 case 'R': // send request
181 case 'E': //send request to all
184 case 'A': // advertise resource
188 case 'B': // send notification
192 case 'N': // select network
196 case 'X': // unselect network
200 case 'H': // handle request response
201 HandleRequestResponse();
204 case 'T': // handle request response
209 Serial.println("wrong menu");
213 //1:Add check for startserver before calling below api
216 HandleRequestResponse();
223 if(CAInitialize() != CA_STATUS_OK)
225 Serial.println("Initialize failed");
230 CARegisterHandler(RequestHandler, ResponseHandler);
233 void StartListeningServer()
235 Serial.println("listening server");
236 CAResult_t ret = CAStartListeningServer();
237 if(ret != CA_STATUS_OK)
239 Serial.print("listening failed: ");
245 void StartDiscoveryServer()
247 Serial.println("discovery server");
248 CAResult_t ret = CAStartDiscoveryServer();
249 if(ret != CA_STATUS_OK)
251 Serial.print("discovery failed: ");
259 char buf[MAX_BUF_LEN] = {0};
260 Serial.println("============");
261 Serial.println("ex) /a/light");
262 Serial.println("uri: ");
264 GetData(buf, sizeof(buf), &len);
267 Serial.println("i/p err");
271 CAToken_t token = NULL;
272 CAResult_t res = CAGenerateToken(&token);
273 if (res != CA_STATUS_OK || token == NULL)
275 Serial.println("token error");
279 Serial.print("token:");
280 Serial.println(token);
282 res = CAFindResource(buf, token);
283 if (res != CA_STATUS_OK)
285 Serial.print("find error: ");
290 Serial.println("success: ");
293 CADestroyToken(token);
298 char buf[MAX_BUF_LEN] = {0};
299 CAConnectivityType_t selectedNetwork;
300 selectedNetwork = GetConnectivityType();
302 Serial.println("============");
303 Serial.println("10.11.12.13:4545/res_uri (for IP)");
304 Serial.println("10:11:12:13:45:45/res_uri (for BT)");
305 Serial.println("uri: ");
308 GetData(buf, sizeof(buf), &len);
311 Serial.println("i/p err");
315 // create remote endpoint
316 CARemoteEndpoint_t *endpoint = NULL;
317 CAResult_t res = CACreateRemoteEndpoint(buf,selectedNetwork,&endpoint);
318 if (res != CA_STATUS_OK)
320 Serial.println("Out of memory");
321 CADestroyRemoteEndpoint(endpoint);
325 memset(buf, 0, sizeof(buf));
327 Serial.println("\n=============================================\n");
328 Serial.println("0:CON, 1:NON\n");
329 Serial.println("select message type : ");
330 GetData(buf, sizeof(buf), &len);
331 CAMessageType_t msgType = CA_MSG_CONFIRM;
335 Serial.println("i/p err,default: 0");
337 else if(buf[0] == '1')
339 msgType = CA_MSG_NONCONFIRM;
343 CAToken_t token = NULL;
344 res = CAGenerateToken(&token);
345 if (res != CA_STATUS_OK)
347 Serial.println("token error");
352 Serial.println((token != NULL) ? token : "");
353 CAInfo_t requestData = {CA_MSG_RESET};
354 requestData.token = token;
355 requestData.payload = (CAPayload_t)"Json Payload";
357 requestData.type = msgType;
359 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
360 requestInfo.method = CA_GET;
361 requestInfo.info = requestData;
364 CASendRequest(endpoint, &requestInfo);
367 CADestroyToken(token);
370 // destroy remote endpoint
371 if (endpoint != NULL)
373 CADestroyRemoteEndpoint(endpoint);
376 Serial.println("============");
379 void SendRequestAll()
381 char buf[MAX_BUF_LEN] = {0};
383 CAConnectivityType_t selectedNetwork;
384 selectedNetwork = GetConnectivityType();
386 Serial.println("=========");
387 Serial.println("10.11.12.13:4545/resource_uri ( for IP )");
388 Serial.println("10:11:12:13:45:45/resource_uri ( for BT )");
389 Serial.println("uri : ");
392 GetData(buf, sizeof(buf), &len);
395 Serial.println("i/p err");
399 // create remote endpoint
400 CARemoteEndpoint_t *endpoint = NULL;
401 CAResult_t res = CACreateRemoteEndpoint(buf, selectedNetwork, &endpoint);
403 if (res != CA_STATUS_OK)
405 Serial.println("create remote endpoint error");
406 CADestroyRemoteEndpoint(endpoint);
410 CAGroupEndpoint_t *group = NULL;
411 group = (CAGroupEndpoint_t *)OICMalloc(sizeof(CAGroupEndpoint_t));
412 group->connectivityType = endpoint->connectivityType;
413 group->resourceUri = endpoint->resourceUri;
416 CAToken_t token = NULL;
417 res = CAGenerateToken(&token);
418 if (res != CA_STATUS_OK)
420 Serial.println("token error");
425 Serial.println((token != NULL) ? token : "");
427 CAInfo_t requestData = {CA_MSG_RESET};
428 requestData.token = token;
429 requestData.payload = "Temp Json Payload";
430 requestData.type = CA_MSG_NONCONFIRM;
432 CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
433 requestInfo.method = CA_GET;
434 requestInfo.info = requestData;
437 // CASendRequest(endpoint, &requestInfo);
438 CASendRequestToAll(group, &requestInfo);
442 CADestroyToken(token);
445 // destroy remote endpoint
446 if (endpoint != NULL)
448 CADestroyRemoteEndpoint(endpoint);
452 Serial.println("==========");
455 void AdvertiseResource()
457 char buf[MAX_BUF_LEN] = {0};
459 Serial.println("============");
460 Serial.println("uri: ");
463 GetData(buf, sizeof(buf), &len);
466 Serial.println("no i/p");
470 int16_t optionNum = 0;
471 char optionData[MAX_OPT_LEN] = {0};
472 char optionNumBuf[2] = {0};
474 Serial.println("Option Num: ");
475 GetData(optionNumBuf, sizeof(optionNumBuf), &len);
478 Serial.println("no i/p,0 option");
482 optionNum = atoi(optionNumBuf);
483 Serial.println(optionNum);
486 CAHeaderOption_t *headerOpt = NULL;
489 headerOpt = (CAHeaderOption_t *) OICCalloc(optionNum, sizeof(CAHeaderOption_t));
490 if (NULL == headerOpt)
492 Serial.println("Out of memory");
498 for (i = 0 ; i < optionNum ; i++)
502 Serial.println("Opt ID:");
503 GetData(getOptionID, sizeof(getOptionID), &len);
506 Serial.println("no i/p");
511 optionID = atoi(getOptionID);
514 memset(optionData, 0, sizeof(optionData));
515 Serial.println("Opt Data:");
516 GetData(optionData, sizeof(optionData), &len);
519 Serial.println("no i/p");
523 headerOpt[i].optionID = optionID;
524 memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
525 Serial.println("ID:");
526 Serial.println(optionID);
527 Serial.println("Data:");
528 Serial.println(optionData);
530 headerOpt[i].optionLength = (uint16_t)strlen(optionData);
533 Serial.println("============");
535 CAToken_t token = NULL;
536 CAResult_t res = CAGenerateToken(&token);
537 if (res != CA_STATUS_OK)
539 Serial.println("token error");
544 Serial.println("token");
545 Serial.println((token != NULL) ? token : "");
547 CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
549 CADestroyToken(token);
552 void SendNotification()
554 char buf[MAX_BUF_LEN] = {0};
555 CAConnectivityType_t selectedNetwork;
556 selectedNetwork = GetConnectivityType();
558 Serial.println("============");
559 Serial.println("10.11.12.13:4545/res_uri (for IP)");
560 Serial.println("10:11:12:13:45:45/res_uri (for BT)");
561 Serial.println("uri: ");
564 GetData(buf, sizeof(buf), &len);
567 Serial.println("i/p err");
571 // create remote endpoint
572 CARemoteEndpoint_t *endpoint = NULL;
573 CAResult_t res = CACreateRemoteEndpoint(buf,selectedNetwork,&endpoint);
574 if (CA_STATUS_OK != res)
576 Serial.println("Out of memory");
577 CADestroyRemoteEndpoint(endpoint);
581 CAInfo_t respondeData = {CA_MSG_RESET};
582 respondeData.token = (CAToken_t)"token";
583 respondeData.payload = (CAPayload_t)"Notification Data";
585 CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}};
586 responseInfo.result = CA_SUCCESS;
587 responseInfo.info = respondeData;
590 CASendNotification(endpoint, &responseInfo);
591 // destroy remote endpoint
592 if (NULL != endpoint)
594 CADestroyRemoteEndpoint(endpoint);
596 Serial.println("============");
601 char buf[MAX_BUF_LEN] = {0};
603 Serial.println("============");
604 Serial.println("Select network");
605 Serial.println("ETHERNET: 0");
606 Serial.println("WIFI: 1");
607 Serial.println("EDR: 2");
608 Serial.println("LE: 3\n");
611 GetData(buf, sizeof(buf), &len);
612 int number = buf[0] - '0';
613 if (0 >= len || number < 0 || number > 3)
615 Serial.println("Wrong i/p. WIFI selected");
623 #ifdef ETHERNET_ADAPTER
624 // Note: ****Update the MAC address here with your shield's MAC address****
625 uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0E, 0xC4, 0x05};
626 uint8_t error = Ethernet.begin(ETHERNET_MAC);
629 Serial.print("Failed: ");
630 Serial.println(error);
639 const char ssid[] = "SSID"; // your network SSID (name)
640 const char pass[] = "SSID_Password"; // your network password
641 int16_t status = WL_IDLE_STATUS; // the Wifi radio's status
643 if (WiFi.status() == WL_NO_SHIELD)
645 Serial.println("ERROR:No Shield");
649 while (status != WL_CONNECTED)
651 Serial.print("connecting: ");
652 Serial.println(ssid);
653 // WiFi.begin api is weird. ssid should also be taken as const char *
654 // Connect to WPA/WPA2 network:
655 status = WiFi.begin((char *)ssid, pass);
664 g_isLeSelected = true;
668 CASelectNetwork(CAConnectivityType_t(1<<number));
669 Serial.println("============");
672 void UnselectNetwork()
674 char buf[MAX_BUF_LEN] = {0};
676 Serial.println("============");
677 Serial.println("Unselect network");
678 Serial.println("ETHERNET: 0");
679 Serial.println("WIFI: 1");
680 Serial.println("EDR: 2");
681 Serial.println("LE: 3\n");
684 GetData(buf, sizeof(buf), &len);
685 int number = buf[0] - '0';
686 Serial.println(number);
687 if (0 >= len || number < 0 || number > 3)
689 Serial.println("Wrong i/p. WIFI selected");
694 g_isLeSelected = false;
696 CAUnSelectNetwork(1 << number);
697 Serial.println("Terminate");
699 Serial.println("============");
705 Serial.println("============");
706 Serial.println("i: Initialize");
707 Serial.println("s: start listening server");
708 Serial.println("d: start discovery server");
709 Serial.println("f: find resource");
710 Serial.println("r: send request");
711 Serial.println("e: send request to all");
712 Serial.println("a: advertise resource");
713 Serial.println("b: send notification");
714 Serial.println("n: select network");
715 Serial.println("x: unselect network");
716 Serial.println("h: handle request response");
717 Serial.println("t: terminate");
718 Serial.println("q: quit");
719 Serial.println("============");
722 void HandleRequestResponse()
724 CAHandleRequestResponse();
727 void RequestHandler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
731 Serial.println("Remote endpoint is NULL!");
737 Serial.println("Request info is NULL!");
741 Serial.println("uri: ");
742 Serial.println(object->resourceUri);
743 Serial.println("RAddr: ");
744 Serial.println(object->addressInfo.IP.ipAddress);
745 Serial.println("Port: ");
746 Serial.println(object->addressInfo.IP.port);
747 Serial.println("data: ");
748 Serial.println(requestInfo->info.payload);
749 Serial.println("Type: ");
750 Serial.println(requestInfo->info.type);
752 if (requestInfo->info.options)
754 uint32_t len = requestInfo->info.numOptions;
756 for (i = 0; i < len; i++)
758 Serial.println("Option:");
760 Serial.println("ID:");
761 Serial.println(requestInfo->info.options[i].optionID);
762 Serial.println("Data:");
763 Serial.println((char*)requestInfo->info.options[i].optionData);
766 Serial.println("send response");
767 SendResponse((CARemoteEndpoint_t *)object, (requestInfo != NULL) ? &requestInfo->info : NULL);
770 void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
772 if (object && object->resourceUri)
774 Serial.print("uri: ");
775 Serial.println(object->resourceUri);
780 Serial.print("data: ");
781 Serial.println(responseInfo->info.payload);
782 Serial.print("Type: ");
783 Serial.println(responseInfo->info.type);
784 Serial.print("res result=");
785 Serial.println(responseInfo->result);
789 void SendResponse(CARemoteEndpoint_t *endpoint, const CAInfo_t* info)
791 Serial.println("============");
792 CAInfo_t responseData = {CA_MSG_RESET};
793 if(info && info->type == CA_MSG_CONFIRM)
795 responseData.type = CA_MSG_ACKNOWLEDGE;
799 responseData.type = CA_MSG_NONCONFIRM;
802 responseData.messageId = (info != NULL) ? info->messageId : 0;
803 responseData.token = (info != NULL) ? (CAToken_t)info->token : (CAToken_t)"";
804 responseData.payload = (CAPayload_t)"response payload";
806 CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}};
807 responseInfo.result = (CAResponseResult_t)203;
808 responseInfo.info = responseData;
810 // send request (connectivityType from remoteEndpoint of request Info)
811 CAResult_t res = CASendResponse(endpoint, &responseInfo);
812 if(res != CA_STATUS_OK)
814 Serial.println("Snd Resp error");
818 Serial.println("Snd Resp success");
821 Serial.println("============");