2 * Copyright 2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
7 #include <Geolocation.h>
9 #include <HttpRequest.h>
11 #include <NetworkDevice.h>
12 #include <NetworkInterface.h>
13 #include <NetworkRoster.h>
15 #include <UrlProtocolRoster.h>
16 #include <UrlRequest.h>
22 BGeolocation::BGeolocation()
23 : fService(kDefaultService
)
28 BGeolocation::BGeolocation(const BUrl
& service
)
35 BGeolocation::LocateSelf(float& latitude
, float& longitude
)
37 // Enumerate wifi network and build JSON message
38 BNetworkRoster
& roster
= BNetworkRoster::Default();
39 uint32 interfaceCookie
= 0;
40 BNetworkInterface interface
;
42 BString
query("{\n\t\"wifiAccessPoints\": [");
45 while (roster
.GetNextInterface(&interfaceCookie
, interface
) == B_OK
) {
46 uint32 networkCookie
= 0;
47 wireless_network network
;
49 BNetworkDevice
device(interface
.Name());
50 // TODO is that the correct way to enumerate devices?
52 while (device
.GetNextNetwork(networkCookie
, network
) == B_OK
) {
58 query
+= "\n\t\t{ \"macAddress\": \"";
59 query
+= network
.address
.ToString().ToUpper();
60 query
+= "\", \"signalStrength\": ";
61 query
<< (int)network
.signal_strength
;
62 query
+= ", \"signalToNoiseRatio\": ";
63 query
<< (int)network
.noise_level
;
69 query
+= "\n\t]\n}\n";
71 // Check that we have enough data (we need at least 2 networks)
73 return B_DEVICE_NOT_FOUND
;
75 class GeolocationListener
: public BUrlProtocolListener
78 virtual ~GeolocationListener() {};
80 void DataReceived(BUrlRequest
*, const char* data
, off_t position
,
82 result
.WriteAt(position
, data
, size
);
87 GeolocationListener listener
;
89 // Send Request (POST JSON message)
90 BUrlRequest
* request
= BUrlProtocolRoster::MakeRequest(fService
, &listener
);
94 BHttpRequest
* http
= dynamic_cast<BHttpRequest
*>(request
);
100 http
->SetMethod(B_HTTP_POST
);
101 BMemoryIO
* io
= new BMemoryIO(query
.String(), query
.Length());
102 http
->AdoptInputData(io
, query
.Length());
104 status_t result
= http
->Run();
110 while (http
->IsRunning())
114 const BHttpResult
& reply
= (const BHttpResult
&)http
->Result();
115 if (reply
.StatusCode() != 200) {
121 result
= BJson::Parse((char*)listener
.result
.Buffer(), data
);
123 if (result
!= B_OK
) {
128 result
= data
.FindMessage("location", &location
);
133 result
= location
.FindDouble("lat", &lat
);
135 result
= location
.FindDouble("lng", &lon
);
144 #ifdef HAVE_DEFAULT_GEOLOCATION_SERVICE_KEY
146 #include "DefaultGeolocationServiceKey.h"
148 const char* BGeolocation::kDefaultService
149 = "https://location.services.mozilla.com/v1/geolocate?key="
150 DEFAULT_GEOLOCATION_SERVICE_KEY
;
154 const char* BGeolocation::kDefaultService
= "";
159 } // namespace BPrivate