~thomas-voss/location-service/fix-1347887

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/service/ichnaea_reporter.h

This MP consolidates multiple related changes together, with the goal of:

(1.) Make the service instance accessible via a cli. Useful for testing scenarios.
(2.) To cut down time-to-first-fix (ttff) by:
  (2.1) Leveraging SUPL and other supplementary data downloaded over ordinary data connections.
  (2.2) Enabling network-based positioning providers to acquire fast position estimates.

In more detail:

* Added tests for daemon and cli.
* Unified daemon and cli header and implementation files.
* Add a command-line interface to the service.
* Split up provider selection policy to rely on an interface ProviderEnumerator to ease in testing.
* Trimmed down on types.
* Removed connectivity API draft to prepare for simpler approach.
* Refactored includes.
* Added a configuration option to handle cell and wifi ID reporting.
* Add a mock for a connectivity API exposed to providers and reporters.
* Add units for connectivity api.
* Refactor cell class into namespace radio. Fixes: 1226204, 1248973, 1281817

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012-2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_ICHNAEA_REPORTER_H_
 
19
#define LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_ICHNAEA_REPORTER_H_
 
20
 
 
21
#include <com/ubuntu/location/service/harvester.h>
 
22
 
 
23
#include <core/net/http/client.h>
 
24
#include <core/net/http/content_type.h>
 
25
#include <core/net/http/request.h>
 
26
#include <core/net/http/response.h>
 
27
#include <core/net/http/status.h>
 
28
 
 
29
#include <json/json.h>
 
30
 
 
31
#include <thread>
 
32
 
 
33
namespace json = Json;
 
34
 
 
35
namespace com{namespace ubuntu{namespace location{namespace service
 
36
{
 
37
/**
 
38
 * @brief All types and functions that are used to communicate with instances
 
39
 * of the Mozilla location service go here. Please see for further details:
 
40
 *
 
41
 *   - https://mozilla-ichnaea.readthedocs.org/en/latest/api/index.html#submit
 
42
 *   - https://github.com/mozilla/MozStumbler
 
43
 */
 
44
namespace ichnaea
 
45
{
 
46
namespace submit
 
47
{
 
48
/** @brief Resource path for wifi- and cell-id submissions.  */
 
49
constexpr const char* resource
 
50
{
 
51
    "/v1/submit?key="
 
52
};
 
53
/** @brief Http code marking errors for Mozilla location service instances. */
 
54
const core::net::http::Status error
 
55
{
 
56
    core::net::http::Status::bad_request
 
57
};
 
58
/** @brief Http code marking a successful submission request to Mozilla location service instances. */
 
59
const core::net::http::Status success
 
60
{
 
61
    core::net::http::Status::no_content
 
62
};
 
63
}
 
64
 
 
65
struct Reporter : public Harvester::Reporter
 
66
{
 
67
    /** @brief Submissions can be tagged with a nick-name for tracking on leaderboards. */
 
68
    static constexpr const char* nick_name_header{"X-Nickname"};
 
69
 
 
70
    /** @brief The JSON-dialect of the Mozilla location service is described here. */
 
71
    struct Json
 
72
    {
 
73
        static constexpr const char* radio{"radio"};
 
74
        static constexpr const char* lat{"lat"};
 
75
        static constexpr const char* lon{"lon"};
 
76
        static constexpr const char* accuracy{"accuracy"};
 
77
        static constexpr const char* altitude{"altitude"};
 
78
        static constexpr const char* altitude_accuracy{"altitude_accuracy"};
 
79
 
 
80
        static constexpr const char* items{"items"};
 
81
 
 
82
        static constexpr const char* cell{"cell"};
 
83
        static constexpr const char* wifi{"wifi"};
 
84
 
 
85
        struct Cell
 
86
        {
 
87
            static constexpr const char* radio{"radio"};
 
88
            static constexpr const char* mcc{"mcc"};
 
89
            static constexpr const char* mnc{"mnc"};
 
90
            static constexpr const char* lac{"lac"};
 
91
            static constexpr const char* cid{"cid"};
 
92
            static constexpr const char* psc{"psc"};
 
93
            static constexpr const char* asu{"asu"};
 
94
        };
 
95
 
 
96
        struct Wifi
 
97
        {
 
98
            static constexpr const char* channel{"channel"};
 
99
            static constexpr const char* frequency{"frequency"};
 
100
            static constexpr const char* key{"key"};
 
101
            static constexpr const char* signal{"signal"};
 
102
        };
 
103
    };
 
104
 
 
105
    /** Creation-time options for the ICHNAEA reporter */
 
106
    struct Configuration
 
107
    {
 
108
        /** Uri of the ICHNAEA instance we want to submit to. */
 
109
        std::string uri;
 
110
        /** API key for the submission */
 
111
        std::string key;
 
112
        /** Nickname for the submission */
 
113
        std::string nick_name;
 
114
    };
 
115
 
 
116
    /** @brief Constructs a new instance with the given parameters. */
 
117
    Reporter(const Configuration& configuration);
 
118
    /** @brief Stops the reporter instance and frees all related resources. */
 
119
    ~Reporter();
 
120
 
 
121
    /** @brief Starts the reporter and prepares for submission. */
 
122
    void start() override;    
 
123
    /** @brief Stops the reporter. */
 
124
    void stop() override;
 
125
 
 
126
    /**
 
127
     * @brief Announced a position update, together with visible wifis and cells to the reporter.
 
128
     * @throws std::runtime_error if wifis and cells are empty.
 
129
     */
 
130
    void report(
 
131
            const Update<Position>& update,
 
132
            const std::vector<connectivity::WirelessNetwork::Ptr>& wifis,
 
133
            const std::vector<connectivity::RadioCell::Ptr>& cells) override;
 
134
 
 
135
    /** @brief Encodes a collection of wifis into the Mozilla loation service JSON dialect. */
 
136
    static void convert_wifis_to_json(
 
137
            const std::vector<connectivity::WirelessNetwork::Ptr>& wifis,
 
138
            json::Value& destination);
 
139
 
 
140
    /** @brief Encodes a collection of radio cells into the Mozilla loation service JSON dialect. */
 
141
    static void convert_cells_to_json(
 
142
            const std::vector<connectivity::RadioCell::Ptr>& cells,
 
143
            json::Value& destination);
 
144
 
 
145
    /** @brief The http request configuration for submissions to the mozilla location service. */
 
146
    core::net::http::Request::Configuration submit_request_config;
 
147
    /** @brief The http client instance used to talk to Mozilla location service instances. */
 
148
    std::shared_ptr<core::net::http::Client> http_client;
 
149
    /** @brief Worker thread for dispatching the http client instance. */
 
150
    std::thread http_client_worker;
 
151
};
 
152
}
 
153
}}}}
 
154
 
 
155
#endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_ICHNAEA_REPORTER_H_