maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   read cellID from N900 using python? (https://talk.maemo.org/showthread.php?t=37212)

hypnotik 2009-12-14 21:01

read cellID from N900 using python?
 
anyone have some python code which will allow me to pull the currently connected cell ID?

I want to be able to use this value to query an open cell database to get a rough location of the user (for when indoors/gps not working, etc)

thanks.

qwerty12 2009-12-14 21:07

Re: read cellID from N900 using python?
 
http://talk.maemo.org/showthread.php?p=386188 showed how to do it in C. I know you requested for Python specifically, but I don't know python-dbus and I think python-osso won't help here, either, as the Cell ID is not the first thing returned...

hopbeat 2009-12-14 21:13

Re: read cellID from N900 using python?
 
@hypnotik:

and here you have an example of using DBus in Python (this one is for WLAN scanning):

http://slexy.org/view/s25h3to4Rr

hypnotik 2009-12-14 21:36

Re: read cellID from N900 using python?
 
Thanks qwerty12, hopbeat for the examples. I'll give a go.

hypnotik 2009-12-14 22:18

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by hopbeat (Post 427799)
@hypnotik:

and here you have an example of using DBus in Python (this one is for WLAN scanning):

http://slexy.org/view/s25h3to4Rr

Is there a point of reference for dbus for querying the GSM stack to read the Cell Tower ID?
I'm assuming I need to modify com.nokia.wlancond.signal to something else?

EDIT: Ok, this is slowly starting to make sense now...

qwerty12's post here shows the code for reading cellID. I just need to adapt it somehow to python.

http://talk.maemo.org/showpost.php?p=385947&postcount=2

hypnotik 2009-12-14 22:44

Re: read cellID from N900 using python?
 
Hmm.. I give up on this. Tried to adapt the code from qwerty12 into hopbeats' but I'm doing something wrong.

Code:

from dbus.glib import *
import gobject
import dbus
import array

loop=None

def scan_results(*args):
    loop.quit()

if __name__ ==  "__main__":

        #mandatory to receive dbus signal
        loop=gobject.MainLoop()
        bus = dbus.SystemBus()
        wlancond = bus.get_object('com.nokia.phone.net', '/com/nokia/phone/net')
        request = dbus.Interface(wlancond, 'Phone.Net')
        bus.add_signal_receiver(scan_results, dbus_interface="Phone.Net.get_registration_status", signal_name="get_registration_status")
        request.get_cell_id(dbus.Int32(4), dbus.ByteArray([]), dbus.Int32(2))
        loop.run()


All I get is:

Code:

ERROR:dbus.proxies:Introspect error on :1.16:/com/nokia/phone/net: dbus.exceptions.DBusException: rpc.Error: object /com/nokia/phone/net doesn't have interface org.freedesktop.DBus.Introspectable
Traceback (most recent call last):
  File "test2.py", line 31, in <module>
    request.get_cell_id(dbus.Int32(4), dbus.ByteArray([]), dbus.Int32(2))
  File "/usr/lib/pymodules/python2.5/dbus/proxies.py", line 68, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/pymodules/python2.5/dbus/proxies.py", line 140, in __call__
    **keywords)
  File "/usr/lib/pymodules/python2.5/dbus/connection.py", line 622, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: rpc.Error: can't find method Phone.Net::get_cell_id

Will have to revisit this later.

qwerty12 2009-12-14 23:30

Re: read cellID from N900 using python?
 
I wanted to try, python-dbus, I really did... But you turned out to be a piece of **** that can't do this correctly. So I cheated:

File.c:
Code:

#include <glib.h>
#include <dbus/dbus-glib.h>

#define PHONE_NET_DBUS_NAME  "com.nokia.phone.net"
#define PHONE_NET_DBUS_IFACE "Phone.Net"
#define PHONE_NET_DBUS_PATH  "/com/nokia/phone/net"
#define PHONE_NET_REGISTRATION_METHOD  "get_registration_status"

guint get_cell_id (void)
{
  DBusGConnection *connection;
  DBusGProxy *proxy;
  GError *error = NULL;
  guchar status, network_type, supported_services;
  guint lac, cell_id, operator_code, country_code;
  gint net_err;

  g_type_init();

  connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
  if (!connection)
  {
    g_printerr("Failed to open connection to system bus: %s\n", error->message);
    g_clear_error(&error);
    return 0;
  }
  proxy = dbus_g_proxy_new_for_name(connection, PHONE_NET_DBUS_NAME, PHONE_NET_DBUS_PATH, PHONE_NET_DBUS_IFACE);

  if (!dbus_g_proxy_call(proxy, PHONE_NET_REGISTRATION_METHOD, &error, G_TYPE_INVALID, G_TYPE_UCHAR, &status, G_TYPE_UINT, &lac, G_TYPE_UINT, &cell_id, G_TYPE_UINT, &operator_code, G_TYPE_UINT, &country_code, G_TYPE_UCHAR, &network_type, G_TYPE_UCHAR, &supported_services, G_TYPE_INT, &net_err, G_TYPE_INVALID))
  {
    if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
      g_printerr("Caught remote method exception %s: %s", dbus_g_error_get_name(error), error->message);
    else
      g_printerr("Failed to call method: %s\n", error->message);
    g_clear_error(&error);
    return 0;
  }

  g_object_unref(proxy);
  dbus_g_connection_unref(connection);

  return cell_id;
}

gcc -Wall --shared File.c -o weed.so `pkg-config --cflags --libs glib-2.0 dbus-glib-1

File.py:
Code:

from ctypes import *

weed = CDLL("/sdk/weed.so")
crack = weed.get_cell_id(None)
print "%d" % crack

Which leaves me with the Cell ID number. I know, I know, using ctypes is not a proper solution but python-dbus sucks so bad...

lma 2009-12-15 09:07

Re: read cellID from N900 using python?
 
Not quite what hypnotik wants, but here's a script I'm using to log cell ID changes, hopefully it'll help:

Code:

#!/usr/bin/env python

import sys
import traceback
import gobject
import dbus
import dbus.mainloop.glib

def cell_signal_handler(*args):
    print "%s,%s,%s" % (args[4], args[3], args[2])
   
if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()
    bus.add_signal_receiver(cell_signal_handler, dbus_interface = "Phone.Net", signal_name = "registration_status_change")
    bus.add_signal_receiver(cell_signal_handler, dbus_interface = "Phone.Net", signal_name = "cell_info_change")

    loop = gobject.MainLoop()
    loop.run()


ewan 2009-12-15 09:26

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by hypnotik (Post 427773)
I want to be able to use this value to query an open cell database to get a rough location of the user (for when indoors/gps not working, etc)

I don't know for sure, but I was under the impression that the built-in liblocation would do this for you, rather than it needing to be implemented in each app - is that not the case?

danielwilms 2009-12-15 09:50

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by ewan (Post 428651)
I don't know for sure, but I was under the impression that the built-in liblocation would do this for you, rather than it needing to be implemented in each app - is that not the case?

That's right. Have a look here. Further it is always better in terms of stability of the API to use the high-level APIs if it exists.


Daniel

hopbeat 2009-12-15 11:50

Re: read cellID from N900 using python?
 
@danielwilms, it's getting tricky when you want use gps and read cell id at the same time :)

danielwilms 2009-12-15 11:58

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by hopbeat (Post 428795)
@danielwilms, it's getting tricky when you want use gps and read cell id at the same time :)

If you just want to use the cell id to get the location, then you should use the location framework, because this is what it does. Or what do you want to do with the cell id?

hypnotik 2009-12-15 14:18

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by danielwilms (Post 428670)
That's right. Have a look here. Further it is always better in terms of stability of the API to use the high-level APIs if it exists.


Daniel

That's cool, but I'm not seeing python-liblocation examples on that page.

bitziz 2010-01-21 15:09

Re: read cellID from N900 using python?
 
i tried Ima's script which worked nicely. however, in the information returned, can someone please tell me what is the cid and what is the lac? those two seems to have being returned as a single value...

bitziz 2010-01-21 15:12

Re: read cellID from N900 using python?
 
also, how would one make this something that's running in the background and just log everything to a file?

would something like screen work on the n900? could i just do

python cid.py > logfile.log?

lizardo 2010-01-22 16:05

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by hypnotik (Post 428987)
That's cool, but I'm not seeing python-liblocation examples on that page.

Its on the PyMaemo wiki pages:

http://wiki.maemo.org/PyMaemo/Using_Location_API

Unfortunately the Cell ID information part is still not implemented in python-location (not the correct package name).

PS: as a general tip, you can see Python related wiki pages by checking the Python category:

http://wiki.maemo.org/Category:Python

fred123 2010-01-22 16:36

Re: read cellID from N900 using python?
 
lizardo
I have been looking at the location example you quoted above and get the following error.
Quote:

Traceback (most recent call last):
File "location.py", line 1, in <module>
import location
File "/home/user/location.py", line 25, in <module>
control = location.GPSDControl.get_default()
AttributeError: 'module' object has no attribute 'GPSDControl'
Any sugestions what I am doing wrong?

roose 2010-01-23 07:17

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by fred123 (Post 488959)
lizardo
I have been looking at the location example you quoted above and get the following error.


Any sugestions what I am doing wrong?

rename your script to something else and don't forget to delete the compiled version (if exists)

lizardo 2010-01-27 12:14

Re: read cellID from N900 using python?
 
Quote:

Originally Posted by roose (Post 490088)
rename your script to something else and don't forget to delete the compiled version (if exists)

Correct. Furthermore, I added a small note to that page as this mistake is now common:

http://wiki.maemo.org/PyMaemo/Using_...mplete_example

siperkin 2010-02-12 04:50

Re: read cellID from N900 using python?
 
Hi Guy's

I have been checking out Lma's script from page1, it appear that when in 3G coverage args[2] shows the global cell id (unique with in network) this can be split in the following way to retrieve the RNC Id.

Upper 12 bits RNC/ Lower 16 bits Cell
i.e
52455502
001100100000 - 0110100001001110
800 - 26702

Also args[1] = LAC

Hope this helps anyone working on this.

synthaxx 2010-03-06 22:58

Re: read cellID from N900 using python?
 
I tried the example python file after adding python-location on my n900.

When i run it i get:

Code:

lat = 52.200000, long = 5.300000
quitting

every time i run it, which is not the correct location.

Anyone know what could be wrong?

happy_n900_user 2010-03-08 13:12

Re: read cellID from N900 using python?
 
I also had this problem, it seems that the sample code does not check if the GPS fix is real.

I have changed the line before print lat long to this:
if (device.fix[1] & location.GPS_DEVICE_LATLONG_SET) and not (device.status & location.GPS_DEVICE_STATUS_NO_FIX):

I haven't tested much after the change, but this seems to work better.

Search for GPS_DEVICE_STATUS_NO_FIX in http://wiki.maemo.org/PyMaemo/Using_Location_API

happy_n900_user 2010-03-08 19:53

Re: read cellID from N900 using python?
 
I tried the change on the bus home, and made one more addition to that line:
if (device.fix[1] & location.GPS_DEVICE_LATLONG_SET) and (device.fix[1] & location.GPS_DEVICE_TIME_SET) and not (device.status & location.GPS_DEVICE_STATUS_NO_FIX):

N.B: I don't know if these changes have negative effects on battery drain or something else. Try these changes at your own risk.

synthaxx 2010-03-09 15:59

Re: read cellID from N900 using python?
 
Works like a charm! Thanks!

It also seems to shutdown the GPS chipset after it's done.

The only issue is when it can't get a fix, you'll need to force quit.

tibrisch 2010-07-03 21:19

Re: read cellID from N900 using python?
 
Not sure if anyone cares... but i got cell_info with python like this:

Code:

import dbus

bus = dbus.SystemBus()
proxy = bus.get_object("com.nokia.phone.net","/com/nokia/phone/net",False)

cell_info = proxy.get_registration_status(dbus_interface="Phone.Net")

print cell_info



All times are GMT. The time now is 12:15.

vBulletin® Version 3.8.8