~ci-train-bot/location-service/location-service-ubuntu-yakkety-1895

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/providers/gps/provider.cpp

  • Committer: Thomas Voß
  • Date: 2013-05-28 14:20:45 UTC
  • Revision ID: thomas.voss@canonical.com-20130528142045-kq5umqdmm4o53vwk
Initial push.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "com/ubuntu/location/providers/gps/provider.h"
 
2
 
 
3
#include "com/ubuntu/location/logging.h"
 
4
 
 
5
#include <ubuntu/application/ubuntu_application_gps.h>
 
6
 
 
7
namespace cul = com::ubuntu::location;
 
8
namespace culg = com::ubuntu::location::providers::gps;
 
9
 
 
10
namespace
 
11
{
 
12
static const std::map<uint16_t, std::string> status_lut = 
 
13
{
 
14
    {UBUNTU_GPS_STATUS_NONE, "UBUNTU_GPS_STATUS_NONE"},
 
15
    {UBUNTU_GPS_STATUS_SESSION_BEGIN, "UBUNTU_GPS_STATUS_SESSION_BEGIN"},
 
16
    {UBUNTU_GPS_STATUS_SESSION_END, "UBUNTU_GPS_STATUS_SESSION_END"},
 
17
    {UBUNTU_GPS_STATUS_ENGINE_ON, "UBUNTU_GPS_STATUS_ENGINE_ON"},
 
18
    {UBUNTU_GPS_STATUS_ENGINE_OFF, "UBUNTU_GPS_STATUS_ENGINE_OFF"}
 
19
};
 
20
}
 
21
 
 
22
struct culg::Provider::Private
 
23
{
 
24
 
 
25
static void on_location_update(UbuntuGpsLocation* location, void* context)
 
26
{
 
27
    auto thiz = static_cast<culg::Provider*>(context);
 
28
        
 
29
    if (location->flags & UBUNTU_GPS_LOCATION_HAS_LAT_LONG)
 
30
    {
 
31
        VLOG(1) << "location->flags & UBUNTU_GPS_LOCATION_HAS_LAT_LONG";
 
32
 
 
33
        cul::Position pos;
 
34
        pos.latitude(cul::wgs84::Latitude{location->latitude * cul::units::Degrees});
 
35
        pos.longitude(cul::wgs84::Longitude{location->longitude * cul::units::Degrees});
 
36
        if(location->flags & UBUNTU_GPS_LOCATION_HAS_ALTITUDE)
 
37
            pos.altitude(cul::wgs84::Altitude{location->altitude * cul::units::Meters});
 
38
        
 
39
        thiz->deliver_position_updates(cul::Update<cul::Position>{pos, cul::Clock::now()});
 
40
    }
 
41
    
 
42
    if (location->flags & UBUNTU_GPS_LOCATION_HAS_SPEED)
 
43
    {
 
44
        VLOG(1) << "location->flags & UBUNTU_GPS_LOCATION_HAS_SPEED";
 
45
        
 
46
        cul::Velocity v{location->speed * cul::units::MetersPerSecond};
 
47
        thiz->deliver_velocity_updates(cul::Update<cul::Velocity>{v, cul::Clock::now()});
 
48
    }
 
49
 
 
50
    if (location->flags & UBUNTU_GPS_LOCATION_HAS_BEARING)
 
51
    {
 
52
        VLOG(1) << "location->flags & UBUNTU_GPS_LOCATION_HAS_BEARING";
 
53
        
 
54
        cul::Heading h{location->bearing * cul::units::Degrees};
 
55
        thiz->deliver_heading_updates(cul::Update<cul::Heading>{h, cul::Clock::now()});
 
56
    }
 
57
}
 
58
 
 
59
static void on_status_update(uint16_t status, void* /*context*/)
 
60
{
 
61
    VLOG(1) << "Status = " << status_lut.at(status);
 
62
}
 
63
 
 
64
static void on_sv_status_update(UbuntuGpsSvStatus* sv_info, void* /*context*/)
 
65
{
 
66
    VLOG(1) << "SV status update: [#svs: " << sv_info->num_svs << "]";
 
67
}
 
68
    
 
69
static void on_nmea_update(int64_t /*timestamp*/, const char* /*nmea*/, int /*length*/, void* /*context*/)
 
70
{
 
71
}
 
72
 
 
73
static void on_set_capabilities(uint32_t capabilities, void* /*context*/)
 
74
{
 
75
    VLOG(1) << __PRETTY_FUNCTION__ << ": " << capabilities;
 
76
}
 
77
 
 
78
static void on_request_utc_time(void* /*context*/)
 
79
{
 
80
    VLOG(1) << __PRETTY_FUNCTION__;
 
81
}
 
82
 
 
83
static void on_agps_status_update(UbuntuAgpsStatus* /*status*/, void* /*context*/)
 
84
{
 
85
    VLOG(1) << __PRETTY_FUNCTION__;
 
86
}
 
87
 
 
88
static void on_gps_ni_notification(UbuntuGpsNiNotification* /*notification*/, void* /*context*/)
 
89
{
 
90
    VLOG(1) << __PRETTY_FUNCTION__;
 
91
}
 
92
    
 
93
static void on_agps_ril_request_set_id(uint32_t /*flags*/, void* /*context*/)
 
94
{
 
95
    VLOG(1) << __PRETTY_FUNCTION__;
 
96
}
 
97
 
 
98
static void on_agps_ril_request_ref_lock(uint32_t /*flags*/, void* /*context*/)
 
99
{
 
100
    VLOG(1) << __PRETTY_FUNCTION__;
 
101
}
 
102
 
 
103
    void start() 
 
104
    { 
 
105
        ubuntu_gps_start(gps_handle);
 
106
    }
 
107
    
 
108
    void stop() 
 
109
    { 
 
110
        ubuntu_gps_stop(gps_handle);
 
111
    }
 
112
 
 
113
    UbuntuGpsParams gps_params;
 
114
    UbuntuGps gps_handle;
 
115
};
 
116
 
 
117
std::string culg::Provider::class_name()
 
118
{
 
119
    return "gps::Provider";
 
120
}
 
121
 
 
122
cul::Provider::Ptr culg::Provider::create_instance(const cul::ProviderFactory::Configuration&)
 
123
{
 
124
    return cul::Provider::Ptr{new culg::Provider{}};
 
125
}
 
126
 
 
127
const cul::Provider::FeatureFlags& culg::Provider::default_feature_flags()
 
128
{
 
129
    static const cul::Provider::FeatureFlags flags{"001"};
 
130
    return flags;
 
131
}
 
132
 
 
133
const cul::Provider::RequirementFlags& culg::Provider::default_requirement_flags()
 
134
{
 
135
    static const cul::Provider::RequirementFlags flags{"1010"};
 
136
    return flags;
 
137
}
 
138
 
 
139
culg::Provider::Provider()
 
140
        : cul::Provider(
 
141
              culg::Provider::default_feature_flags(), 
 
142
              culg::Provider::default_requirement_flags()),
 
143
          d(new Private())
 
144
{
 
145
    d->gps_params.location_cb = culg::Provider::Private::on_location_update;
 
146
    d->gps_params.status_cb = culg::Provider::Private::on_status_update;
 
147
    d->gps_params.sv_status_cb = culg::Provider::Private::on_sv_status_update;
 
148
    d->gps_params.nmea_cb = culg::Provider::Private::on_nmea_update;
 
149
    d->gps_params.set_capabilities_cb = culg::Provider::Private::on_set_capabilities;
 
150
    d->gps_params.request_utc_time_cb = culg::Provider::Private::on_request_utc_time;
 
151
    d->gps_params.xtra_download_request_cb = nullptr;
 
152
    
 
153
    d->gps_params.agps_status_cb = culg::Provider::Private::on_agps_status_update;
 
154
    
 
155
    d->gps_params.gps_ni_notify_cb = culg::Provider::Private::on_gps_ni_notification;
 
156
    
 
157
    d->gps_params.request_setid_cb = culg::Provider::Private::on_agps_ril_request_set_id;
 
158
    d->gps_params.request_refloc_cb = culg::Provider::Private::on_agps_ril_request_ref_lock;
 
159
    d->gps_params.context = this;
 
160
    
 
161
    d->gps_handle = ubuntu_gps_new(std::addressof(d->gps_params));
 
162
}
 
163
 
 
164
culg::Provider::~Provider() noexcept
 
165
{
 
166
    d->stop();
 
167
    ubuntu_gps_delete(d->gps_handle);
 
168
}
 
169
 
 
170
bool culg::Provider::matches_criteria(const cul::Criteria&)
 
171
{
 
172
    return true;
 
173
}
 
174
 
 
175
void culg::Provider::start_position_updates()
 
176
{
 
177
    d->start();
 
178
}
 
179
 
 
180
void culg::Provider::stop_position_updates()
 
181
{
 
182
    d->stop();
 
183
}
 
184
 
 
185
void culg::Provider::start_velocity_updates()
 
186
{
 
187
    d->start();
 
188
}
 
189
 
 
190
void culg::Provider::stop_velocity_updates()
 
191
{
 
192
    d->stop();
 
193
}    
 
194
 
 
195
void culg::Provider::start_heading_updates()
 
196
{
 
197
    d->start();
 
198
}
 
199
 
 
200
void culg::Provider::stop_heading_updates()
 
201
{
 
202
    d->stop();
 
203
}