Merge branch 'master' into easysetup & CBOR changes
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / arduino / thinserver / thinserver.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 // Do not remove the include below
22 #include "Arduino.h"
23
24 #include "logger.h"
25 #include "ocstack.h"
26 #include <string.h>
27
28 #ifdef ARDUINOWIFI
29 // Arduino WiFi Shield
30 #include <SPI.h>
31 #include <WiFi.h>
32 #include <WiFiUdp.h>
33 #else
34 // Arduino Ethernet Shield
35 #include <EthernetServer.h>
36 #include <Ethernet.h>
37 #include <Dns.h>
38 #include <EthernetClient.h>
39 #include <util.h>
40 #include <EthernetUdp.h>
41 #include <Dhcp.h>
42 #endif
43
44 #include "easysetup.h"
45
46 const char *getResult(OCStackResult result);
47
48 PROGMEM const char TAG[] = "ThinServer";
49
50 char ssid[] = "EasySetup123";
51 char passwd[] = "EasySetup123";
52
53 void EventCallbackInApp(ES_RESULT eventFlag)
54 {
55     Serial.println("callback!!! in app");
56 }
57
58 // On Arduino Atmel boards with Harvard memory architecture, the stack grows
59 // downwards from the top and the heap grows upwards. This method will print
60 // the distance(in terms of bytes) between those two.
61 // See here for more details :
62 // http://www.atmel.com/webdoc/AVRLibcReferenceManual/malloc_1malloc_intro.html
63 void PrintArduinoMemoryStats()
64 {
65 #ifdef ARDUINO_AVR_MEGA2560
66     //This var is declared in avr-libc/stdlib/malloc.c
67     //It keeps the largest address not allocated for heap
68     extern char *__brkval;
69     //address of tmp gives us the current stack boundry
70     int tmp;
71     OC_LOG_V(INFO, TAG, "Stack: %u         Heap: %u", (unsigned int)&tmp, (unsigned int)__brkval);
72     OC_LOG_V(INFO, TAG, "Unallocated Memory between heap and stack: %u",
73             ((unsigned int)&tmp - (unsigned int)__brkval));
74 #endif
75 }
76 //The setup function is called once at startup of the sketch
77 void setup()
78 {
79     // Add your initialization code here
80     // Note : This will initialize Serial port on Arduino at 115200 bauds
81     OC_LOG_INIT();
82     OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
83
84     FindNetworkForOnboarding(ES_WIFI, ssid, passwd, EventCallbackInApp);
85
86     // Initialize the OC Stack in Server mode
87     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
88     {
89         OC_LOG(ERROR, TAG, PCF("OCStack init error"));
90         return;
91     }
92
93     InitializeProvisioning(EventCallbackInApp);
94
95    /* if (OCStartPresence(0) != OC_STACK_OK)
96     {
97         OC_LOG(ERROR, TAG, PCF("Start Presence init error"));
98         return;
99     }
100         */
101 }
102
103 // The loop function is called in an endless loop
104 void loop()
105 {
106     // This artificial delay is kept here to avoid endless spinning
107     // of Arduino microcontroller. Modify it as per specific application needs.
108     delay(2000);
109
110     // This call displays the amount of free SRAM available on Arduino
111     PrintArduinoMemoryStats();
112
113     // Give CPU cycles to OCStack to perform send/recv and other OCStack stuff
114     if (OCProcess() != OC_STACK_OK)
115     {
116         OC_LOG(ERROR, TAG, PCF("OCStack process error"));
117         return;
118     }
119 }