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