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;
47 char trackeeID[13] = "9059AF16FEF7";
49 //char slaveList[SLAVER_EA][13]={"9059AF1700EE"};
50 char slaveList[SLAVER_EA][13] = {"9059AF1700EE", "34B1F7D004D2"};
51 int g_PROXIUnderObservation = 0;
55 const char *getResult(OCStackResult result);
56 void createResource();
60 bool JsonGenerator( char *jsonBuf, uint16_t buf_length )
68 // On Arduino Atmel boards with Harvard memory architecture, the stack grows
69 // downwards from the top and the heap grows upwards. This method will print
70 // the distance(in terms of bytes) between those two.
71 // See here for more details :
72 // http://www.atmel.com/webdoc/AVRLibcReferenceManual/malloc_1malloc_intro.html
73 void PrintArduinoMemoryStats()
75 #ifdef ARDUINO_AVR_MEGA2560
76 //This var is declared in avr-libc/stdlib/malloc.c
77 //It keeps the largest address not allocated for heap
78 extern char *__brkval;
79 //address of tmp gives us the current stack boundry
81 OC_LOG_V(INFO, TAG, "Stack: %u Heap: %u", (unsigned int)&tmp, (unsigned int)__brkval);
82 OC_LOG_V(INFO, TAG, "Unallocated Memory between heap and stack: %u",
83 ((unsigned int)&tmp - (unsigned int)__brkval));
87 // This is the entity handler for the registered resource.
88 // This is invoked by OCStack whenever it recevies a request for this resource.
89 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
90 OCEntityHandlerRequest *entityHandlerRequest )
92 OCEntityHandlerResult ehRet = OC_EH_OK;
94 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
96 OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
97 if (OC_REST_GET == entityHandlerRequest->method)
99 if ( JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
100 entityHandlerRequest->resJSONPayloadLen ) == false )
105 if (OC_REST_PUT == entityHandlerRequest->method)
107 if (JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
108 entityHandlerRequest->resJSONPayloadLen ) == false )
114 else if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
116 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo->action)
118 OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client"));
119 g_PROXIUnderObservation = 1;
121 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo->action)
123 OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client"));
127 Serial.println((char *)entityHandlerRequest->resJSONPayload);
138 //The setup function is called once at startup of the sketch
141 Serial.begin(115200);
143 // Add your initialization code here
146 OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
147 // uint16_t port = OC_WELL_KNOWN_PORT;
149 // Connect to Ethernet or WiFi network
150 if (ConnectToNetwork() != 0)
152 OC_LOG(ERROR, TAG, "Unable to connect to network");
156 // Initialize the OC Stack in Server mode
157 if (OCInit(NULL, OC_WELL_KNOWN_PORT, OC_SERVER) != OC_STACK_OK)
159 OC_LOG(ERROR, TAG, PCF("OCStack init error"));
163 // Declare and create the example resource
166 // This call displays the amount of free SRAM available on Arduino
167 PrintArduinoMemoryStats();
169 ble.init( (long)115200, BLE_SLAVER, slaveList[0]);
174 OC_LOG_V(INFO, TAG, "Program Start-\r\n");
178 // The loop function is called in an endless loop
181 // This artificial delay is kept here to avoid endless spinning
182 // of Arduino microcontroller. Modify it as per specfic application needs.
184 if (OCProcess() != OC_STACK_OK)
186 OC_LOG(ERROR, TAG, PCF("OCStack process error"));
190 char *user_cmd = NULL;
192 // ble.pollingDisconnect();
194 user_cmd = ble.Debug2BLE(true);
195 ble.BLE2Debug( true );
211 void createResource()
214 OCStackResult res = OCCreateResource(&m_handle,
216 OC_RSRVD_INTERFACE_DEFAULT,
219 OC_DISCOVERABLE | OC_OBSERVABLE);
220 OC_LOG_V(INFO, TAG, "Created PROXI resource with result: %s", getResult(res));
223 const char *getResult(OCStackResult result)
228 return "OC_STACK_OK";
229 case OC_STACK_INVALID_URI:
230 return "OC_STACK_INVALID_URI";
231 case OC_STACK_INVALID_QUERY:
232 return "OC_STACK_INVALID_QUERY";
233 case OC_STACK_INVALID_IP:
234 return "OC_STACK_INVALID_IP";
235 case OC_STACK_INVALID_PORT:
236 return "OC_STACK_INVALID_PORT";
237 case OC_STACK_INVALID_CALLBACK:
238 return "OC_STACK_INVALID_CALLBACK";
239 case OC_STACK_INVALID_METHOD:
240 return "OC_STACK_INVALID_METHOD";
241 case OC_STACK_NO_MEMORY:
242 return "OC_STACK_NO_MEMORY";
243 case OC_STACK_COMM_ERROR:
244 return "OC_STACK_COMM_ERROR";
245 case OC_STACK_INVALID_PARAM:
246 return "OC_STACK_INVALID_PARAM";
247 case OC_STACK_NOTIMPL:
248 return "OC_STACK_NOTIMPL";
249 case OC_STACK_NO_RESOURCE:
250 return "OC_STACK_NO_RESOURCE";
251 case OC_STACK_RESOURCE_ERROR:
252 return "OC_STACK_RESOURCE_ERROR";
253 case OC_STACK_SLOW_RESOURCE:
254 return "OC_STACK_SLOW_RESOURCE";
255 case OC_STACK_NO_OBSERVERS:
256 return "OC_STACK_NO_OBSERVERS";
258 return "OC_STACK_ERROR";