1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 // Arduino WiFi Shield
32 // Arduino Ethernet Shield
33 #include <EthernetServer.h>
36 #include <EthernetClient.h>
38 #include <EthernetUdp.h>
42 #include "easysetup.h"
46 const char *getResult(OCStackResult result);
50 * @brief Target SSID of the Soft Access point to which the device has to connect
52 static char ssid[] = "EasySetup123";
56 * @brief Password of the Soft Access point to which the device has to connect
58 static char passwd[] = "EasySetup123";
61 * @var g_OnBoardingSucceeded
62 * @brief This variable will be set if OnBoarding is successful
64 static bool g_OnBoardingSucceeded = false;
67 * @var g_ProvisioningSucceeded
68 * @brief This variable will be set if Provisioning is successful
70 static bool g_ProvisioningSucceeded = false;
72 static bool g_isInitialized = false;
74 bool is_connected=false;
76 void GetData(char *readInput, size_t bufferLength, size_t *dataLength)
78 if (!readInput || bufferLength == 0 || !dataLength)
80 Serial.println("Invalid buffer");
84 while (!Serial.available())
89 while (Serial.available())
92 char c = Serial.read();
93 if ('\n' != c && '\r' != c && len < bufferLength - 1)
103 readInput[len] = '\0';
105 Serial.print("PD: ");
106 Serial.println(readInput);
112 Serial.println("============");
113 Serial.println("s: start easy setup");
114 Serial.println("p: start provisioning resources");
115 Serial.println("t: terminate");
116 Serial.println("q: quit");
117 Serial.println("============");
120 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
122 Serial.println("callback!!! in app");
124 if(esResult == ES_OK)
126 if(!g_OnBoardingSucceeded){
127 Serial.println("Device is successfully OnBoarded");
128 g_OnBoardingSucceeded = true;
130 else if(g_OnBoardingSucceeded & enrolleeState == ES_ON_BOARDED_STATE){
131 Serial.println("Device is successfully OnBoared with SoftAP");
132 g_ProvisioningSucceeded = true;
135 else if (esResult == ES_ERROR)
137 if(g_OnBoardingSucceeded)
139 OC_LOG_V(ERROR, TAG, "Failure in Provisioning. \
140 Current Enrollee State: %d",enrolleeState);
141 g_OnBoardingSucceeded = false;
143 else if(g_ProvisioningSucceeded)
145 OC_LOG_V(ERROR, TAG, "Failure in connect to target network. \
146 Current Enrollee State: %d",enrolleeState);
147 g_ProvisioningSucceeded = false;
153 // On Arduino Atmel boards with Harvard memory architecture, the stack grows
154 // downwards from the top and the heap grows upwards. This method will print
155 // the distance(in terms of bytes) between those two.
156 // See here for more details :
157 // http://www.atmel.com/webdoc/AVRLibcReferenceManual/malloc_1malloc_intro.html
158 void PrintArduinoMemoryStats()
160 #ifdef ARDUINO_AVR_MEGA2560
161 //This var is declared in avr-libc/stdlib/malloc.c
162 //It keeps the largest address not allocated for heap
163 extern char *__brkval;
164 //address of tmp gives us the current stack boundry
166 OC_LOG_V(INFO, TAG, "Stack: %u Heap: %u", (unsigned int)&tmp, (unsigned int)__brkval);
167 OC_LOG_V(INFO, TAG, "Unallocated Memory between heap and stack: %u",
168 ((unsigned int)&tmp - (unsigned int)__brkval));
172 void StartEasySetup()
174 OC_LOG(DEBUG, TAG, "OCServer is starting...");
176 if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
178 OC_LOG(ERROR, TAG, "OnBoarding Failed");
182 g_isInitialized = true;
184 OC_LOG_V(ERROR, TAG, "OnBoarding succeded. Successfully connected to ssid : %s",ssid);
187 void StartProvisioning()
189 OC_LOG(DEBUG, TAG, "StartProvisioning is invoked...");
191 // Initialize the OC Stack in Server mode
192 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
194 OC_LOG(ERROR, TAG, "OCStack init error!!");
198 if (InitProvisioning() == ES_ERROR)
200 OC_LOG(ERROR, TAG, "Init Provisioning Failed!!");
207 OC_LOG(DEBUG, TAG, "Stopping EasySetup is invoked...");
209 g_isInitialized = false;
211 if (TerminateEasySetup() == ES_ERROR)
213 OC_LOG(ERROR, TAG, "TerminateEasySetup Failed!!");
218 if (OCStop() != OC_STACK_OK)
220 OC_LOG(ERROR, TAG, "OCStack stop failed!!");
224 OC_LOG(DEBUG, TAG, "Stopping EasySetup done");
228 //The setup function is called once at startup of the sketch
231 // Add your initialization code here
232 // Note : This will initialize Serial port on Arduino at 115200 bauds
235 Serial.println("#########################");
236 Serial.println("EasySetup Enrollee SAMPLE");
237 Serial.println("#########################");
241 // The loop function is called in an endless loop
244 char buffer[5] = {0};
246 if (Serial.available() > 0)
248 GetData(buffer, sizeof(buffer), &len);
251 Serial.println("Input Error err");
254 switch (toupper(buffer[0]))
261 Serial.println("quit");
264 case 'S': // start easy setup
268 case 'P': // start provisioning
272 case 'T': // stop easy setup
277 Serial.println("wrong option");
282 //check g_isInitialized to see if stack init is success
285 // This call displays the amount of free SRAM available on Arduino
286 PrintArduinoMemoryStats();
287 if (WiFi.status() == WL_CONNECTED)
289 else if (is_connected)
290 TerminateEasySetup();
292 // Give CPU cycles to OCStack to perform send/recv and other OCStack stuff
293 if (OCProcess() != OC_STACK_OK)
295 OC_LOG(ERROR, TAG, "OCStack process error");
300 // This artificial delay is kept here to avoid endless spinning
301 // of Arduino microcontroller. Modify it as per specific application needs.