1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // Do not remove the include below
30 #include "oic_lanLib.h"
36 #define ARDUINO_AVR_MEGA2560 1
37 /// This is the port which Arduino Server will use for all unicast communication with it's peers
38 #define OC_WELL_KNOWN_PORT 5683
40 PROGMEM const char TAG[] = "TrackeeSensor";
42 OCResourceHandle m_handle;
45 char trackeeID[13] = "9059AF16FEF7";
47 //char slaveList[SLAVER_EA][13]={"9059AF1700EE"};
48 char slaveList[SLAVER_EA][13] = {"9059AF1700EE", "34B1F7D004D2"};
49 int g_PROXIUnderObservation = 0;
51 const char *getResult(OCStackResult result);
52 void createResource();
55 bool JsonGenerator( char *jsonBuf, uint16_t buf_length )
60 // On Arduino Atmel boards with Harvard memory architecture, the stack grows
61 // downwards from the top and the heap grows upwards. This method will print
62 // the distance(in terms of bytes) between those two.
63 // See here for more details :
64 // http://www.atmel.com/webdoc/AVRLibcReferenceManual/malloc_1malloc_intro.html
65 void PrintArduinoMemoryStats()
67 #ifdef ARDUINO_AVR_MEGA2560
68 //This var is declared in avr-libc/stdlib/malloc.c
69 //It keeps the largest address not allocated for heap
70 extern char *__brkval;
71 //address of tmp gives us the current stack boundry
73 OC_LOG_V(INFO, TAG, "Stack: %u Heap: %u", (unsigned int)&tmp, (unsigned int)__brkval);
74 OC_LOG_V(INFO, TAG, "Unallocated Memory between heap and stack: %u",
75 ((unsigned int)&tmp - (unsigned int)__brkval));
79 // This is the entity handler for the registered resource.
80 // This is invoked by OCStack whenever it recevies a request for this resource.
81 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
82 OCEntityHandlerRequest *entityHandlerRequest )
84 OCEntityHandlerResult ehRet = OC_EH_OK;
86 if (entityHandlerRequest )
88 if (flag & OC_REQUEST_FLAG)
90 OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
91 if (OC_REST_GET == entityHandlerRequest->method)
93 if ( JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
94 entityHandlerRequest->resJSONPayloadLen ) == false )
99 if (OC_REST_PUT == entityHandlerRequest->method)
101 if (JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
102 entityHandlerRequest->resJSONPayloadLen ) == false )
109 else if (flag & OC_OBSERVE_FLAG)
111 OC_LOG (INFO, TAG, PCF("Flag includes OC_OBSERVE_FLAG"));
112 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo->action)
114 OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client"));
115 g_PROXIUnderObservation = 1;
117 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo->action)
119 OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client"));
123 Serial.println((char *)entityHandlerRequest->resJSONPayload);
129 //The setup function is called once at startup of the sketch
132 Serial.begin(115200);
134 // Add your initialization code here
137 OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
138 // uint16_t port = OC_WELL_KNOWN_PORT;
140 // Connect to Ethernet or WiFi network
141 if (ConnectToNetwork() != 0)
143 OC_LOG(ERROR, TAG, "Unable to connect to network");
147 // Initialize the OC Stack in Server mode
148 if (OCInit(NULL, OC_WELL_KNOWN_PORT, OC_SERVER) != OC_STACK_OK)
150 OC_LOG(ERROR, TAG, PCF("OCStack init error"));
154 // Declare and create the example resource
157 // This call displays the amount of free SRAM available on Arduino
158 PrintArduinoMemoryStats();
160 ble.init( (long)115200, BLE_SLAVER, slaveList[0]);
164 OC_LOG_V(INFO, TAG, "Program Start-\r\n");
168 // The loop function is called in an endless loop
171 // This artificial delay is kept here to avoid endless spinning
172 // of Arduino microcontroller. Modify it as per specfic application needs.
173 if (OCProcess() != OC_STACK_OK)
175 OC_LOG(ERROR, TAG, PCF("OCStack process error"));
179 char *user_cmd = NULL;
181 // ble.pollingDisconnect();
183 user_cmd = ble.Debug2BLE(true);
184 ble.BLE2Debug( true );
194 void createResource()
197 OCStackResult res = OCCreateResource(&m_handle,
199 OC_RSRVD_INTERFACE_DEFAULT,
202 OC_DISCOVERABLE | OC_OBSERVABLE);
203 OC_LOG_V(INFO, TAG, "Created PROXI resource with result: %s", getResult(res));
206 const char *getResult(OCStackResult result)
211 return "OC_STACK_OK";
212 case OC_STACK_INVALID_URI:
213 return "OC_STACK_INVALID_URI";
214 case OC_STACK_INVALID_QUERY:
215 return "OC_STACK_INVALID_QUERY";
216 case OC_STACK_INVALID_IP:
217 return "OC_STACK_INVALID_IP";
218 case OC_STACK_INVALID_PORT:
219 return "OC_STACK_INVALID_PORT";
220 case OC_STACK_INVALID_CALLBACK:
221 return "OC_STACK_INVALID_CALLBACK";
222 case OC_STACK_INVALID_METHOD:
223 return "OC_STACK_INVALID_METHOD";
224 case OC_STACK_NO_MEMORY:
225 return "OC_STACK_NO_MEMORY";
226 case OC_STACK_COMM_ERROR:
227 return "OC_STACK_COMM_ERROR";
228 case OC_STACK_INVALID_PARAM:
229 return "OC_STACK_INVALID_PARAM";
230 case OC_STACK_NOTIMPL:
231 return "OC_STACK_NOTIMPL";
232 case OC_STACK_NO_RESOURCE:
233 return "OC_STACK_NO_RESOURCE";
234 case OC_STACK_RESOURCE_ERROR:
235 return "OC_STACK_RESOURCE_ERROR";
236 case OC_STACK_SLOW_RESOURCE:
237 return "OC_STACK_SLOW_RESOURCE";
238 case OC_STACK_NO_OBSERVERS:
239 return "OC_STACK_NO_OBSERVERS";
241 return "OC_STACK_ERROR";