-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgoogle-maps-device-locator.cpp
268 lines (195 loc) · 6.07 KB
/
google-maps-device-locator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "Particle.h"
#include "google-maps-device-locator.h"
#if Wiring_Cellular
# include "CellularHelper.h"
#endif
static char requestBuf[256];
static char *requestCur;
static int numAdded = 0;
GoogleMapsDeviceLocator::GoogleMapsDeviceLocator() : locatorMode(LOCATOR_MODE_MANUAL), periodMs(10000), eventName("deviceLocator"),
stateTime(0), state(CONNECT_WAIT_STATE), callback(NULL), waitAfterConnect(8000) {
}
GoogleMapsDeviceLocator::~GoogleMapsDeviceLocator() {
}
GoogleMapsDeviceLocator &GoogleMapsDeviceLocator::withLocateOnce() {
locatorMode = LOCATOR_MODE_ONCE;
return *this;
}
GoogleMapsDeviceLocator &GoogleMapsDeviceLocator::withLocatePeriodic(unsigned long secondsPeriodic) {
locatorMode = LOCATOR_MODE_PERIODIC;
if (secondsPeriodic < 5) {
secondsPeriodic = 5;
}
periodMs = secondsPeriodic * 1000;
return *this;
}
GoogleMapsDeviceLocator &GoogleMapsDeviceLocator::withEventName(const char *name) {
this->eventName = name;
return *this;
}
GoogleMapsDeviceLocator &GoogleMapsDeviceLocator::withSubscribe(GoogleMapsDeviceLocatorSubscriptionCallback callback) {
this->callback = callback;
snprintf(requestBuf, sizeof(requestBuf), "hook-response/%s/%s", eventName.c_str(), System.deviceID().c_str());
Particle.subscribe(requestBuf, &GoogleMapsDeviceLocator::subscriptionHandler, this, MY_DEVICES);
return *this;
}
void GoogleMapsDeviceLocator::loop() {
switch(state) {
case CONNECT_WAIT_STATE:
if (Particle.connected()) {
state = CONNECTED_WAIT_STATE;
stateTime = millis();
}
break;
case CONNECTED_WAIT_STATE:
if (millis() - stateTime >= waitAfterConnect) {
// Wait several seconds after connecting before doing the location
if (locatorMode == LOCATOR_MODE_ONCE) {
publishLocation();
state = IDLE_STATE;
}
else
if (locatorMode == LOCATOR_MODE_MANUAL) {
state = IDLE_STATE;
}
else {
state = CONNECTED_STATE;
stateTime = millis() - periodMs;
}
}
break;
case CONNECTED_STATE:
if (Particle.connected()) {
if (millis() - stateTime >= periodMs) {
stateTime = millis();
publishLocation();
}
}
else {
// We have disconnected, rec
state = CONNECT_WAIT_STATE;
}
break;
case IDLE_STATE:
// Just hang out here forever (entered only on LOCATOR_MODE_ONCE)
break;
}
}
const char *GoogleMapsDeviceLocator::scan() {
#if Wiring_WiFi
return wifiScan();
#endif
#if Wiring_Cellular
return cellularScan();
#endif
}
void GoogleMapsDeviceLocator::publishLocation() {
Serial.println("publishLocation");
const char *scanData = scan();
Serial.printlnf("scanData=%s", scanData);
if (scanData[0]) {
if (Particle.connected()) {
Particle.publish(eventName, scanData, PRIVATE);
}
}
}
void GoogleMapsDeviceLocator::subscriptionHandler(const char *event, const char *data) {
// event: hook-response/deviceLocator/<deviceid>/0
if (callback) {
// float lat, float lon, float accuracy
char *mutableCopy = strdup(data);
char *part, *end;
float lat, lon, accuracy;
part = strtok_r(mutableCopy, ",", &end);
if (part) {
lat = atof(part);
part = strtok_r(NULL, ",", &end);
if (part) {
lon = atof(part);
part = strtok_r(NULL, ",", &end);
if (part) {
accuracy = atof(part);
(*callback)(lat, lon, accuracy);
}
}
}
free(mutableCopy);
}
}
#if Wiring_WiFi
static void wifiScanCallback(WiFiAccessPoint* wap, void* data) {
// The - 3 factor here to leave room for the closing JSON array ] object }} and the trailing null
size_t spaceLeft = &requestBuf[sizeof(requestBuf) - 3] - requestCur;
if (spaceLeft < 30) {
return;
}
int sizeNeeded = snprintf(requestCur, spaceLeft,
"%s{\"m\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"s\":%d,\"c\":%d}",
(requestCur[-1] == '[' ? "" : ","),
wap->bssid[0], wap->bssid[1], wap->bssid[2], wap->bssid[3], wap->bssid[4], wap->bssid[5],
wap->rssi, wap->channel);
if (sizeNeeded > 0 && sizeNeeded < (int)spaceLeft) {
// There is enough space to store the whole entry, so save it
requestCur += sizeNeeded;
numAdded++;
}
}
const char *GoogleMapsDeviceLocator::wifiScan() {
requestCur = requestBuf;
numAdded = 0;
requestCur += sprintf(requestCur, "{\"w\":{\"a\":");
*requestCur++ = '[';
WiFi.scan(wifiScanCallback);
*requestCur++ = ']';
*requestCur++ = '}';
*requestCur++ = '}';
*requestCur++ = 0;
if (numAdded == 0) {
requestBuf[0] = 0;
}
return requestBuf;
}
#endif /* Wiring_WiFi */
#if Wiring_Cellular
static void cellularAddTower(const CellularHelperEnvironmentCellData *cellData) {
// The - 4 factor here to leave room for the closing JSON array ], object }}, and the trailing null
size_t spaceLeft = &requestBuf[sizeof(requestBuf) - 4] - requestCur;
int sizeNeeded = snprintf(requestCur, spaceLeft,
"%s{\"i\":%d,\"l\":%u,\"c\":%d,\"n\":%d}",
(requestCur[-1] == '[' ? "" : ","),
cellData->ci, cellData->lac, cellData->mcc, cellData->mnc);
if (sizeNeeded > 0 && sizeNeeded < (int)spaceLeft && cellData->lac != 0 && cellData->lac != 65535 && cellData->mcc != 65535 && cellData->mnc != 65535) {
// There is enough space to store the whole entry, so save it
requestCur += sizeNeeded;
numAdded++;
}
}
const char *GoogleMapsDeviceLocator::cellularScan() {
// First try to get info on neighboring cells. This doesn't work for me using the U260
CellularHelperEnvironmentResponseStatic<4> envResp;
CellularHelper.getEnvironment(5, envResp);
if (envResp.resp != RESP_OK) {
// We couldn't get neighboring cells, so try just the receiving cell
CellularHelper.getEnvironment(3, envResp);
}
// envResp.serialDebug();
requestCur = requestBuf;
numAdded = 0;
// We know these things fit, so just using sprintf instead of snprintf here
requestCur += sprintf(requestCur, "{\"c\":{\"o\":\"%s\",",
CellularHelper.getOperatorName().c_str());
requestCur += sprintf(requestCur, "\"a\":[");
cellularAddTower(&envResp.service);
for(size_t ii = 0; ii < envResp.getNumNeighbors(); ii++) {
cellularAddTower(&envResp.neighbors[ii]);
}
*requestCur++ = ']';
*requestCur++ = '}';
*requestCur++ = '}';
*requestCur++ = 0;
if (numAdded == 0) {
requestBuf[0] = 0;
}
return requestBuf;
}
#endif /* Wiring_Cellular */