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


All times are GMT. The time now is 03:32.

vBulletin® Version 3.8.8