82780a55b74994ff1a98080477abfc46bef6775b
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SampleApp / arduino / Reference_Thing / src / trackee.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 #include "bleLib.h"
24 #include <stdio.h>
25
26 #include "logger.h"
27 #include "ocstack.h"
28 #include <string.h>
29
30 #include "oic_lanLib.h"
31
32 // proximity code s
33 #define DATA_EA     400
34 #define SLAVER_EA   2
35
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
39
40 PROGMEM const char TAG[] = "TrackeeSensor";
41
42 OCResourceHandle m_handle;
43
44
45
46 Cble ble;
47 char trackeeID[13] = "9059AF16FEF7";
48 int slaver_num = 0;
49 //char slaveList[SLAVER_EA][13]={"9059AF1700EE"};
50 char slaveList[SLAVER_EA][13] = {"9059AF1700EE", "34B1F7D004D2"};
51 int g_PROXIUnderObservation = 0;
52
53
54
55 const char *getResult(OCStackResult result);
56 void createResource();
57
58
59 #define LENGTH_VAR      50
60 bool JsonGenerator( char *jsonBuf, uint16_t buf_length )
61 {
62
63     return true;
64
65 }
66
67
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()
74 {
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
80     int tmp;
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));
84 #endif
85 }
86
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 )
91 {
92     OCEntityHandlerResult ehRet = OC_EH_OK;
93
94     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
95     {
96         OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
97         if (OC_REST_GET == entityHandlerRequest->method)
98         {
99             if ( JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
100                                 entityHandlerRequest->resJSONPayloadLen ) == false )
101             {
102                 ehRet  = OC_EH_ERROR;
103             }
104         }
105         if (OC_REST_PUT == entityHandlerRequest->method)
106         {
107             if (JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
108                                entityHandlerRequest->resJSONPayloadLen ) == false )
109             {
110                 ehRet  = OC_EH_ERROR;
111             }
112         }
113     }
114     else if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
115     {
116         if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo->action)
117         {
118             OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client"));
119             g_PROXIUnderObservation = 1;
120         }
121         else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo->action)
122         {
123             OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client"));
124         }
125     }
126
127     Serial.println((char *)entityHandlerRequest->resJSONPayload);
128
129     return ehRet;
130 }
131
132
133
134
135
136
137
138 //The setup function is called once at startup of the sketch
139 void setup()
140 {
141     Serial.begin(115200);
142
143     // Add your initialization code here
144     OC_LOG_INIT();
145
146     OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
147     //    uint16_t port = OC_WELL_KNOWN_PORT;
148
149     // Connect to Ethernet or WiFi network
150     if (ConnectToNetwork() != 0)
151     {
152         OC_LOG(ERROR, TAG, "Unable to connect to network");
153         return;
154     }
155
156     // Initialize the OC Stack in Server mode
157     if (OCInit(NULL, OC_WELL_KNOWN_PORT, OC_SERVER) != OC_STACK_OK)
158     {
159         OC_LOG(ERROR, TAG, PCF("OCStack init error"));
160         return;
161     }
162
163     // Declare and create the example resource
164     createResource();
165
166     // This call displays the amount of free SRAM available on Arduino
167     PrintArduinoMemoryStats();
168
169     ble.init( (long)115200, BLE_SLAVER, slaveList[0]);
170
171
172 //  ble.StatusRead();
173
174     OC_LOG_V(INFO, TAG, "Program Start-\r\n");
175 }
176
177
178 // The loop function is called in an endless loop
179 void loop()
180 {
181     // This artificial delay is kept here to avoid endless spinning
182     // of Arduino microcontroller. Modify it as per specfic application needs.
183
184     if (OCProcess() != OC_STACK_OK)
185     {
186         OC_LOG(ERROR, TAG, PCF("OCStack process error"));
187         return;
188     }
189
190     char *user_cmd = NULL;
191
192 //  ble.pollingDisconnect();
193
194     user_cmd = ble.Debug2BLE(true);
195     ble.BLE2Debug( true );
196
197     if ( user_cmd )
198     {
199         free( user_cmd );
200         user_cmd = NULL;
201     }
202
203 }
204
205
206
207
208
209
210
211 void createResource()
212 {
213
214     OCStackResult res = OCCreateResource(&m_handle,
215                                          "SSManager.Sensor",
216                                          OC_RSRVD_INTERFACE_DEFAULT,
217                                          "/Tracker_Thing",
218                                          OCEntityHandlerCb,
219                                          OC_DISCOVERABLE | OC_OBSERVABLE);
220     OC_LOG_V(INFO, TAG, "Created PROXI resource with result: %s", getResult(res));
221 }
222
223 const char *getResult(OCStackResult result)
224 {
225     switch (result)
226     {
227         case OC_STACK_OK:
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";
257         case OC_STACK_ERROR:
258             return "OC_STACK_ERROR";
259         default:
260             return "UNKNOWN";
261     }
262 }
263