Imported Upstream version 0.9.2
[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 Cble ble;
45 char trackeeID[13] = "9059AF16FEF7";
46 int slaver_num = 0;
47 //char slaveList[SLAVER_EA][13]={"9059AF1700EE"};
48 char slaveList[SLAVER_EA][13] = {"9059AF1700EE", "34B1F7D004D2"};
49 int g_PROXIUnderObservation = 0;
50
51 const char *getResult(OCStackResult result);
52 void createResource();
53
54 #define LENGTH_VAR      50
55 bool JsonGenerator( char *jsonBuf, uint16_t buf_length )
56 {
57     return true;
58 }
59
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()
66 {
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
72     int tmp;
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));
76 #endif
77 }
78
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 )
83 {
84     OCEntityHandlerResult ehRet = OC_EH_OK;
85
86     if (entityHandlerRequest )
87     {
88         if (flag & OC_REQUEST_FLAG)
89         {
90             OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
91             if (OC_REST_GET == entityHandlerRequest->method)
92             {
93                 if ( JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
94                                     entityHandlerRequest->resJSONPayloadLen ) == false )
95                 {
96                     ehRet  = OC_EH_ERROR;
97                 }
98             }
99             if (OC_REST_PUT == entityHandlerRequest->method)
100             {
101                 if (JsonGenerator( (char *)entityHandlerRequest->resJSONPayload,
102                                    entityHandlerRequest->resJSONPayloadLen ) == false )
103                 {
104                     ehRet  = OC_EH_ERROR;
105                 }
106             }
107     
108         }
109         else if (flag & OC_OBSERVE_FLAG)
110         {
111             OC_LOG (INFO, TAG, PCF("Flag includes OC_OBSERVE_FLAG"));
112             if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo->action)
113             {
114                 OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client"));
115                 g_PROXIUnderObservation = 1;
116             }
117             else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo->action)
118             {
119                 OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client"));
120             }
121         }
122
123         Serial.println((char *)entityHandlerRequest->resJSONPayload);
124     }
125
126     return ehRet;
127 }
128
129 //The setup function is called once at startup of the sketch
130 void setup()
131 {
132     Serial.begin(115200);
133
134     // Add your initialization code here
135     OC_LOG_INIT();
136
137     OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
138     //    uint16_t port = OC_WELL_KNOWN_PORT;
139
140     // Connect to Ethernet or WiFi network
141     if (ConnectToNetwork() != 0)
142     {
143         OC_LOG(ERROR, TAG, "Unable to connect to network");
144         return;
145     }
146
147     // Initialize the OC Stack in Server mode
148     if (OCInit(NULL, OC_WELL_KNOWN_PORT, OC_SERVER) != OC_STACK_OK)
149     {
150         OC_LOG(ERROR, TAG, PCF("OCStack init error"));
151         return;
152     }
153
154     // Declare and create the example resource
155     createResource();
156
157     // This call displays the amount of free SRAM available on Arduino
158     PrintArduinoMemoryStats();
159
160     ble.init( (long)115200, BLE_SLAVER, slaveList[0]);
161
162 //  ble.StatusRead();
163
164     OC_LOG_V(INFO, TAG, "Program Start-\r\n");
165 }
166
167
168 // The loop function is called in an endless loop
169 void loop()
170 {
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)
174     {
175         OC_LOG(ERROR, TAG, PCF("OCStack process error"));
176         return;
177     }
178
179     char *user_cmd = NULL;
180
181 //  ble.pollingDisconnect();
182
183     user_cmd = ble.Debug2BLE(true);
184     ble.BLE2Debug( true );
185
186     if ( user_cmd )
187     {
188         free( user_cmd );
189         user_cmd = NULL;
190     }
191
192 }
193
194 void createResource()
195 {
196
197     OCStackResult res = OCCreateResource(&m_handle,
198                                          "SSManager.Sensor",
199                                          OC_RSRVD_INTERFACE_DEFAULT,
200                                          "/Tracker_Thing",
201                                          OCEntityHandlerCb,
202                                          OC_DISCOVERABLE | OC_OBSERVABLE);
203     OC_LOG_V(INFO, TAG, "Created PROXI resource with result: %s", getResult(res));
204 }
205
206 const char *getResult(OCStackResult result)
207 {
208     switch (result)
209     {
210         case OC_STACK_OK:
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";
240         case OC_STACK_ERROR:
241             return "OC_STACK_ERROR";
242         default:
243             return "UNKNOWN";
244     }
245 }
246