Implementation of connectivity abstraction feature Release v0.5
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / arduino / casample.cpp
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <ctype.h>
22 #include <errno.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "cacommon.h"
29 #include "cainterface.h"
30 #include <TimedAction.h>
31 #include "Arduino.h"
32
33 #include "oic_malloc.h"
34
35 #define MAX_BUF_LEN 100 //1024
36 #define MAX_OPT_LEN 100
37
38 #define printf Serial.println
39 //#define printf
40
41 void print_menu();
42 void process();
43
44 void initialize();
45 void start_listening_server();
46 void start_discovery_server();
47 void find_resource();
48 void send_request();
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();
55
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);
59 void terminate();
60
61 void getData(char *readInput, int bufferLength, int *dataLength)
62 {
63     while (!Serial.available());
64     int len = 0;
65     while (Serial.available())
66     {
67         delay(100);
68         char c = Serial.read();
69         if ('\n' != c && '\r' != c && len < bufferLength - 1)
70         {
71             readInput[len++] = c;
72         }
73         else
74         {
75             break;
76         }
77     }
78
79     readInput[len] = '\0';
80     Serial.flush();
81     Serial.print("PD:");
82     Serial.println(readInput);
83     (*dataLength) = len;
84 }
85
86 CAConnectivityType_t getConnectivityType()
87 {
88     char type[2];
89     memset(type, 0, sizeof(char));
90     printf("Select network");
91     printf("ETHERNET: 0");
92     printf("WIFI: 1");
93     printf("EDR: 2");
94     printf("LE: 3");
95
96
97     int16_t typeLen = 0;
98     getData(type, sizeof(type), &typeLen);
99     int num = type[0] - '0';
100     switch (num)
101     {
102         case 0:
103             return CA_ETHERNET;
104         case 1:
105             return CA_WIFI;
106         case 2:
107             return CA_EDR;
108         case 3:
109             return CA_LE;
110     }
111 }
112 void setup()
113 {
114     Serial.begin (115200);
115
116     printf("============");
117     printf("CA SAMPLE");
118     printf("============");
119     print_menu();
120 }
121
122 void loop()
123 {
124     char buffer[5];
125     memset(buffer, 0, sizeof(buffer));
126     int16_t len;
127     if (Serial.available() > 0)
128     {
129         getData(buffer, sizeof(buffer), &len);
130         switch (toupper(buffer[0]))
131         {
132             case 'M': // menu
133                 print_menu();
134                 break;
135
136             case 'Q': // quit
137                 printf("quit");
138                 return;
139
140             case 'I': // Initialize interface
141                 initialize();
142                 break;
143
144             case 'S': // start listening server
145                 start_listening_server();
146                 break;
147
148             case 'D': // start discovery server
149                 start_discovery_server();
150                 break;
151
152             case 'F': // find resource
153                 find_resource();
154                 break;
155
156             case 'R': // send request
157                 send_request();
158                 break;
159
160             case 'A': // advertise resource
161                 advertise_resource();
162                 break;
163
164             case 'B': // send notification
165                 send_notification();
166                 break;
167
168             case 'N': // select network
169                 select_network();
170                 break;
171
172             case 'X': // unselect network
173                 unselect_network();
174                 break;
175
176             case 'H': // handle request response
177                 handle_request_response();
178                 break;
179
180             case 'T': // handle request response
181                 terminate();
182                 break;
183
184             default:
185                 printf("wrong menu");
186                 break;
187         }
188     }
189     //1:Add check for startserver before calling below api
190     handle_request_response();
191 }
192
193 void initialize()
194 {
195     CAInitialize();
196     select_network();
197     // set handler.
198     CARegisterHandler(request_handler, response_handler);
199 }
200
201 void start_listening_server()
202 {
203     printf("start server");
204     CAStartListeningServer();
205 }
206
207 void start_discovery_server()
208 {
209     printf("start server");
210     CAStartDiscoveryServer();
211 }
212
213 void find_resource()
214 {
215     char buf[MAX_BUF_LEN];
216     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
217
218     printf("============");
219     printf("ex) a/light");
220     printf("uri: ");
221     int len = 0;
222     getData(buf, sizeof(buf), &len);
223
224     // create token
225     CAToken_t token = NULL;
226     CAResult_t res = CAGenerateToken(&token);
227     if (res != CA_STATUS_OK)
228     {
229         printf("token generate error!!");
230         token = NULL;
231     }
232
233     printf("token");
234     printf((token != NULL) ? token : "");
235
236     res = CAFindResource(buf, token);
237     if (res != CA_STATUS_OK)
238     {
239         printf("find error");
240     }
241     else
242     {
243         printf("success: ");
244         printf(buf);
245     }
246     CADestroyToken(token);
247 }
248
249 void send_request()
250 {
251     char buf[MAX_BUF_LEN];
252     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
253
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)");
257     printf("uri: ");
258
259     int16_t len = 0;
260     getData(buf, sizeof(buf), &len);
261
262     // create remote endpoint
263     CARemoteEndpoint_t *endpoint = NULL;
264     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
265     if (res != CA_STATUS_OK)
266     {
267         printf("Out of memory");
268         CADestroyRemoteEndpoint(endpoint);
269         return;
270     }
271     endpoint->connectivityType = getConnectivityType();
272     // create token
273     CAToken_t token = NULL;
274     res = CAGenerateToken(&token);
275     if (res != CA_STATUS_OK)
276     {
277         printf("token error");
278         token = NULL;
279     }
280
281     printf((token != NULL) ? token : "");
282
283     CAInfo_t requestData;
284     memset(&requestData, 0, sizeof(CAInfo_t));
285     requestData.token = token;
286     requestData.payload = "Json Payload";
287
288     CARequestInfo_t requestInfo;
289     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
290     requestInfo.method = CA_GET;
291     requestInfo.info = requestData;
292
293     // send request
294     CASendRequest(endpoint, &requestInfo);
295     if (token != NULL)
296     {
297         CADestroyToken(token);
298     }
299
300     // destroy remote endpoint
301     if (endpoint != NULL)
302     {
303         CADestroyRemoteEndpoint(endpoint);
304     }
305
306     printf("============");
307 }
308
309 void advertise_resource()
310 {
311     char buf[MAX_BUF_LEN];
312     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
313
314     printf("============");
315     printf("uri: ");
316
317     int16_t len = 0;
318     getData(buf, sizeof(buf), &len);
319
320     int16_t optionNum = 0;
321     char optionData[MAX_OPT_LEN];
322     char optionNumBuf[2];
323
324     printf("Option Num: ");
325     getData(optionNumBuf, sizeof(optionNumBuf), &len);
326     optionNum = optionNumBuf[0]  - '0';
327     printf(optionNum);
328
329
330     CAHeaderOption_t *headerOpt = NULL;
331     if (optionNum > 0)
332     {
333         headerOpt = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * optionNum);
334         if (NULL == headerOpt)
335         {
336             printf("Out of memory");
337             return;
338         }
339         memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
340     }
341
342     int i;
343     for (i = 0 ; i < optionNum ; i++)
344     {
345         int optionID = 0;
346         char getOptionID[2];
347         getData(getOptionID, sizeof(getOptionID), &len);
348         printf("Option Num: ");
349         printf(i + 1);
350         optionID = getOptionID[0];
351         printf(optionID);
352
353         headerOpt[i].optionID = optionID;
354         memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
355         printf("Option Data itr");
356         printf(i + 1);
357
358         int len = 0;
359         getData(optionData, sizeof(optionData), &len);
360         memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
361         printf(i + 1);
362         printf("data");
363         printf(optionData);
364
365         headerOpt[i].optionLength = (uint16_t)strlen(optionData);
366     }
367
368     printf("============");
369     // create token
370     CAToken_t token = NULL;
371     CAResult_t res = CAGenerateToken(&token);
372     if (res != CA_STATUS_OK)
373     {
374         printf("token generate error!!");
375         token = NULL;
376     }
377
378     printf("token");
379     printf((token != NULL) ? token : "");
380
381     CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
382     if (NULL != headerOpt)
383     {
384         OICFree(headerOpt);
385     }
386     CADestroyToken(token);
387 }
388
389 void send_notification()
390 {
391     char buf[MAX_BUF_LEN];
392     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
393
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)");
397     printf("uri: ");
398
399     int len = 0;
400     getData(buf, sizeof(buf), &len);
401
402     // create remote endpoint
403     CARemoteEndpoint_t *endpoint = NULL;
404     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
405     if (CA_STATUS_OK != res)
406     {
407         printf("Out of memory");
408         CADestroyRemoteEndpoint(endpoint);
409         return;
410     }
411     endpoint->connectivityType = getConnectivityType();
412     CAInfo_t respondeData;
413     memset(&respondeData, 0, sizeof(CAInfo_t));
414     respondeData.token = "token";
415     respondeData.payload = "Notification Data";
416
417     CAResponseInfo_t responseInfo;
418     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
419     responseInfo.result = CA_SUCCESS;
420     responseInfo.info = respondeData;
421
422     // send request
423     CASendNotification(endpoint, &responseInfo);
424     // destroy remote endpoint
425     if (NULL != endpoint)
426     {
427         CADestroyRemoteEndpoint(endpoint);
428     }
429     printf("============");
430 }
431
432 void select_network()
433 {
434     char buf[MAX_BUF_LEN];
435     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
436
437     printf("============");
438     printf("Select network");
439     printf("ETHERNET: 0");
440     printf("WIFI: 1");
441     printf("EDR: 2");
442     printf("LE: 3\n");
443
444     int len = 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("============");
450 }
451
452 void unselect_network()
453 {
454     char buf[MAX_BUF_LEN];
455     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
456
457     printf("============");
458     printf("Unselect network");
459     printf("ETHERNET: 0");
460     printf("WIFI: 1");
461     printf("EDR: 2");
462     printf("LE: 3\n");
463
464     int len = 0;
465     getData(buf, sizeof(buf), &len);
466     int number = buf[0] - '0';
467     printf(number);
468     number = (number < 0 || number > 3) ? 1 : number;
469     CAUnSelectNetwork(1 << number);
470     printf("Terminate");
471     CATerminate();
472     printf("============");
473 }
474
475 void print_menu()
476 {
477
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");
490     printf("q: quit");
491     printf("============");
492 }
493
494 void handle_request_response()
495 {
496     CAHandleRequestResponse();
497 }
498
499 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
500 {
501     printf("uri: ");
502     printf((object != NULL) ? object->resourceUri : "");
503     printf("data: ");
504     printf((requestInfo != NULL) ? requestInfo->info.payload : "");
505
506     printf("send response");
507     send_response((CARemoteEndpoint_t *)object, requestInfo->info.token);
508 }
509
510 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
511 {
512     printf("uri: ");
513     printf((object != NULL) ? object->resourceUri : "");
514     printf("data: ");
515     printf((responseInfo != NULL) ? responseInfo->info.payload : "");
516 }
517
518 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
519 {
520
521     printf("============");
522
523     CAInfo_t responseData;
524     memset(&responseData, 0, sizeof(CAInfo_t));
525     responseData.token = request_token;
526     responseData.payload = "response payload";
527
528     CAResponseInfo_t responseInfo;
529     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
530     responseInfo.result = (CAResponseResult_t)203;
531     responseInfo.info = responseData;
532
533     // send request (connectivityType from remoteEndpoint of request Info)
534     CASendResponse(endpoint, &responseInfo);
535
536     printf("============");
537
538 }
539
540 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
541 {
542
543     printf("============");
544
545     CAInfo_t requestData;
546     memset(&requestData, 0, sizeof(CAInfo_t));
547     requestData.token = token;
548     requestData.payload = "Json Payload";
549
550     CARequestInfo_t requestInfo;
551     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
552     requestInfo.method = CA_GET;
553     requestInfo.info = requestData;
554
555     // send request
556     endpoint->connectivityType = CA_WIFI;
557     CASendRequest(endpoint, &requestInfo);
558
559     printf("============");
560
561 }
562
563 void terminate()
564 {
565     unselect_network();
566 }