| 1 | package Net::Radio::oFono::Roles::Properties; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::Radio::oFono::Roles::Properties - generic property access for remote oFono objects | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This package provides a role for being added to classes which need to access | ||
| 14 | properties of remote dbus objects of oFono. Currently no separate role for | ||
| 15 | read-only access is available. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.001'; | ||
| 20 | |||
| 21 | # Must be a base class of target | ||
| 22 | # use base qw(Net::Radio::oFono::Helpers::EventMgr); | ||
| 23 | |||
| 24 | use Net::DBus qw(:typing); | ||
| 25 | |||
| 26 | use Log::Any qw($log); | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | package Net::Radio::oFono::NewInterface; | ||
| 31 | |||
| 32 | use base qw(Net::Radio::oFono::Helpers::EventMgr? Net::Radio::oFono::Roles::RemoteObj Net::Radio::oFono::Roles::Properties ...); | ||
| 33 | |||
| 34 | use Net::DBus qw(:typing); | ||
| 35 | |||
| 36 | sub new | ||
| 37 | { | ||
| 38 | my ( $class, %events ) = @_; | ||
| 39 | |||
| 40 | my $self = $class->SUPER::new(%events); # SUPER::new finds first - so EventMgr::new | ||
| 41 | |||
| 42 | bless( $self, $class ); | ||
| 43 | |||
| 44 | $self->_init(); | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _init | ||
| 50 | { | ||
| 51 | my $self = $_[0]; | ||
| 52 | |||
| 53 | # initialize roles | ||
| 54 | $self->Net::Radio::oFono::Roles::RemoteObj::_init( "/modem_0", "org.ofono.NewInterface" ); # must be first one | ||
| 55 | $self->Net::Radio::oFono::Roles::Properties::_init(); | ||
| 56 | ... | ||
| 57 | |||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub DESTROY | ||
| 62 | { | ||
| 63 | my $self = $_[0]; | ||
| 64 | |||
| 65 | # destroy roles | ||
| 66 | ... | ||
| 67 | $self->Net::Radio::oFono::Roles::Properties::DESTROY(); # must be last one | ||
| 68 | $self->Net::Radio::oFono::Roles::RemoteObj::DESTROY(); # must be last one | ||
| 69 | |||
| 70 | # destroy base class | ||
| 71 | $self->Net::Radio::oFono::Helpers::EventMgr::DESTROY(); | ||
| 72 | |||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head1 EVENTS | ||
| 77 | |||
| 78 | Following events are triggered by this role: | ||
| 79 | |||
| 80 | =over 4 | ||
| 81 | |||
| 82 | =item ON_PROPERTY_CHANGED | ||
| 83 | |||
| 84 | Triggered when a property has been changed. Submits the name of the | ||
| 85 | changed property to the listener. | ||
| 86 | |||
| 87 | =item ON_PEROPERTY_ . uc($property_name) . _CHANGED | ||
| 88 | |||
| 89 | Triggered when a property has been changed. Submits the value of the | ||
| 90 | changed property to the listener. | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =head1 METHODS | ||
| 95 | |||
| 96 | =head2 _init | ||
| 97 | |||
| 98 | Initializes the properties api (connects to PropertyChanged signal of remote object). | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub _init | ||
| 103 | { | ||
| 104 | my $self = $_[0]; | ||
| 105 | |||
| 106 | my $on_property_changed = sub { return $self->onPropertyChanged(@_); }; | ||
| 107 | $self->{sig_property_changed} = | ||
| 108 | $self->{remote_obj}->connect_to_signal( "PropertyChanged", $on_property_changed ); | ||
| 109 | |||
| 110 | $self->GetProperties(1); | ||
| 111 | |||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub DESTROY | ||
| 116 | { | ||
| 117 | my $self = $_[0]; | ||
| 118 | |||
| 119 | defined( $self->{remote_obj} ) | ||
| 120 | and $self->{remote_obj} | ||
| 121 | ->disconnect_from_signal( "PropertyChanged", $self->{sig_property_changed} ); | ||
| 122 | |||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 onPropertyChanged | ||
| 127 | |||
| 128 | Callback method used when the signal C<PropertyChanged> is received. | ||
| 129 | Can be overwritten to implement other or enhanced behavior. | ||
| 130 | |||
| 131 | =over 4 | ||
| 132 | |||
| 133 | =item * | ||
| 134 | |||
| 135 | Updates properties cache | ||
| 136 | |||
| 137 | =item * | ||
| 138 | |||
| 139 | Triggers signals on property change | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub onPropertyChanged | ||
| 146 | { | ||
| 147 | my ( $self, $property, $value ) = @_; | ||
| 148 | $self->{properties}->{$property} = $value; | ||
| 149 | $self->trigger_event( "ON_PROPERTY_CHANGED", $property ); | ||
| 150 | $self->trigger_event( "ON_PROPERTY_" . uc($property) . "_CHANGED", $value ); | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | =head2 GetProperties(;$force) | ||
| 155 | |||
| 156 | Returns the properties of the remote object. | ||
| 157 | |||
| 158 | When invoked with a true value as first argument, the properties are | ||
| 159 | refreshed from the remote object. | ||
| 160 | |||
| 161 | Returns the properties hash in array more and the reference to the | ||
| 162 | properties hash in scalar mode. | ||
| 163 | |||
| 164 | =over 8 | ||
| 165 | |||
| 166 | =item B<TODO> | ||
| 167 | |||
| 168 | Return cloned properties to avoid dirtying the local cache ... | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub GetProperties | ||
| 175 | { | ||
| 176 | my ( $self, $force ) = @_; | ||
| 177 | |||
| 178 | $force and %{ $self->{properties} } = %{ $self->{remote_obj}->GetProperties() }; | ||
| 179 | |||
| 180 | return wantarray ? %{ $self->{properties} } : $self->{properties}; | ||
| 181 | } | ||
| 182 | |||
| 183 | =head2 GetProperty($property_name;$force) | ||
| 184 | |||
| 185 | Returns the requested property of the remote object. | ||
| 186 | |||
| 187 | When invoked with a true value as second argument, the properties are | ||
| 188 | refreshed from the remote object. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub GetProperty | ||
| 193 | { | ||
| 194 | my ( $self, $property, $force ) = @_; | ||
| 195 | |||
| 196 | $force and $self->GetProperties(1); | ||
| 197 | |||
| 198 | return $self->{properties}->{$property}; | ||
| 199 | } | ||
| 200 | |||
| 201 | =head2 SetProperty($property_name,$new_value) | ||
| 202 | |||
| 203 | Sets the specified property of the remote object to the specified value. | ||
| 204 | Note that some values needs special encapsulation using dbus_I<type>(). | ||
| 205 | The property name is automatically encapsulated using C<dbus_string>. | ||
| 206 | |||
| 207 | See the appropriate interface documentation in oFono to learn the | ||
| 208 | types to use. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub SetProperty | ||
| 213 | { | ||
| 214 | my ( $self, $property, $value ) = @_; | ||
| 215 | |||
| 216 | return $self->{remote_obj}->SetProperty( dbus_string($property), $value ); | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 SetProperties(%property_hash) | ||
| 220 | |||
| 221 | Sets all specified properties using L</SetProperty>. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub SetProperties | ||
| 226 | { | ||
| 227 | my ( $self, %properties ) = @_; | ||
| 228 | |||
| 229 | while ( my ( $property, $value ) = each(%properties) ) | ||
| 230 | { | ||
| 231 | $self->SetProperty( $property, $value ); | ||
| 232 | } | ||
| 233 | |||
| 234 | return; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head1 BUGS | ||
| 238 | |||
| 239 | Please report any bugs or feature requests to C<bug-net-radio-ofono at rt.cpan.org>, or through | ||
| 240 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Radio-oFono>. I will be notified, and then you'll | ||
| 241 | automatically be notified of progress on your bug as I make changes. | ||
| 242 | |||
| 243 | If you think you've found a bug then please read "How to Report Bugs | ||
| 244 | Effectively" by Simon Tatham: | ||
| 245 | L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>. | ||
| 246 | |||
| 247 | =head1 SUPPORT | ||
| 248 | |||
| 249 | You can find documentation for this module with the perldoc command. | ||
| 250 | |||
| 251 | perldoc Net::Radio::oFono | ||
| 252 | |||
| 253 | You can also look for information at: | ||
| 254 | |||
| 255 | =over 4 | ||
| 256 | |||
| 257 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 258 | |||
| 259 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Radio-oFono> | ||
| 260 | |||
| 261 | If you think you've found a bug then please read "How to Report Bugs | ||
| 262 | Effectively" by Simon Tatham: | ||
| 263 | L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>. | ||
| 264 | |||
| 265 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 266 | |||
| 267 | L<http://annocpan.org/dist/Net-Radio-oFono> | ||
| 268 | |||
| 269 | =item * CPAN Ratings | ||
| 270 | |||
| 271 | L<http://cpanratings.perl.org/d/Net-Radio-oFono> | ||
| 272 | |||
| 273 | =item * Search CPAN | ||
| 274 | |||
| 275 | L<http://search.cpan.org/dist/Net-Radio-oFono/> | ||
| 276 | |||
| 277 | =back | ||
| 278 | |||
| 279 | =head2 Where can I go for help with a concrete version? | ||
| 280 | |||
| 281 | Bugs and feature requests are accepted against the latest version | ||
| 282 | only. To get patches for earlier versions, you need to get an | ||
| 283 | agreement with a developer of your choice - who may or not report the | ||
| 284 | issue and a suggested fix upstream (depends on the license you have | ||
| 285 | chosen). | ||
| 286 | |||
| 287 | =head2 Business support and maintenance | ||
| 288 | |||
| 289 | For business support you can contact Jens via his CPAN email | ||
| 290 | address rehsackATcpan.org. Please keep in mind that business | ||
| 291 | support is neither available for free nor are you eligible to | ||
| 292 | receive any support based on the license distributed with this | ||
| 293 | package. | ||
| 294 | |||
| 295 | =head1 ACKNOWLEDGEMENTS | ||
| 296 | |||
| 297 | At first the guys from the oFono-Team shall be named: Marcel Holtmann and | ||
| 298 | Denis Kenzior, the maintainers and all the people named in ofono/AUTHORS. | ||
| 299 | Without their effort, there would no need for a Net::Radio::oFono module. | ||
| 300 | |||
| 301 | Further, Peter "ribasushi" Rabbitson helped a lot by providing hints | ||
| 302 | and support how to make this API accessor a valuable CPAN module. | ||
| 303 | |||
| 304 | =head1 AUTHOR | ||
| 305 | |||
| 306 | Jens Rehsack, C<< <rehsack at cpan.org> >> | ||
| 307 | |||
| 308 | =head1 LICENSE AND COPYRIGHT | ||
| 309 | |||
| 310 | Copyright 2012 Jens Rehsack. | ||
| 311 | |||
| 312 | This program is free software; you can redistribute it and/or modify it | ||
| 313 | under the terms of either: the GNU General Public License as published | ||
| 314 | by the Free Software Foundation; or the Artistic License. | ||
| 315 | |||
| 316 | See http://dev.perl.org/licenses/ for more information. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | 1; |