📄 locationacqmodule.cpp
字号:
/*
* ====================================================================
* locationacqmodule.cpp
*
* Python API for getting satellitite and position information
*
* Implements currently following Python type:
*
* PositionServer
* positioner
* - Create (and connect) new Positioner object.
*
* default_module
* - Returns the default module id.
*
* modules
* - Return information about the available positioning modules.
*
* module_info
* - Return information about the specified position module.
*
* Positioner
* set_requestors
* - Set requestors.
* - Requestors must be given in a list.
* - Each requestor is represented as a dictionary.
*
* position
* - Returns position infomation.
*
* stop_position
* - Stops the position feed
*
* last_position
* - returns last position information
*
* Copyright (c) 2007 Nokia Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
#include "locationacqmodule.h"
/*
* Debug print.
*/
/*void
debugprint(const char *f, ...)
{
va_list a;
va_start(a,f);
char buffer[256];
vsprintf(buffer, f, a);
char buffer2[256];
sprintf(buffer2, "print '%s'", buffer);
PyRun_SimpleString(buffer2);
} */
/*
* Python LONG_LONG to TTimeIntervalMicroSeconds.
*/
static TTimeIntervalMicroSeconds lltoTIMS(const LONG_LONG& tv)
{
// from e32dbmodule.cpp:
#ifdef EKA2
unsigned int tv_low, tv_high;
tv_low=*(unsigned int *)(&tv);
tv_high=*((unsigned int *)(&tv)+1);
TInt64 hi_and_lo;
TUint32* lo=(TUint32*)&hi_and_lo;
TUint32* hi=((TUint32*)&hi_and_lo)+1;
*lo=tv_low;
*hi=tv_high;
return hi_and_lo;
#else
unsigned int tv_low, tv_high;
tv_low=*(unsigned int *)(&tv);
tv_high=*((unsigned int *)(&tv)+1);
return TInt64(tv_high, tv_low);
#endif
}
/*
* Create new PositionServer object.
*/
extern "C" PyObject *
new_PositionServer_object()
{
// create PositionServer_object.
PositionServer_object* posServ =
PyObject_New(PositionServer_object, PositionServer_type);
if (posServ == NULL){
return PyErr_NoMemory();
}
// initialize PositionServer_object's pointers.
posServ->posServ=NULL;
// create symbian object RPositionServer and connect it.
RPositionServer* positionServer=NULL;
TInt err=KErrNone;
TRAP(err,{
positionServer=new (ELeave) RPositionServer;
User::LeaveIfError(positionServer->Connect());
posServ->posServ=positionServer;
});
if(err!=KErrNone){
delete positionServer;
Py_DECREF(posServ);
return SPyErr_SetFromSymbianOSErr(err);
}
return (PyObject*)posServ;
}
/*
* Create (and connect) new Positioner object.
*/
extern "C" PyObject *
PositionServer_positioner(PositionServer_object* self, PyObject* args, PyObject* keywds)
{
TInt id=KNullUidValue;
static const char *const kwlist[] =
{"module_id", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|i", (char**)kwlist, &id)){
return NULL;
}
TPositionModuleId moduleUid={id};
return new_Positioner_object(self,moduleUid);
}
/*
* Return the default module id.
*/
extern "C" PyObject *
PositionServer_default_module(PositionServer_object* self, PyObject* /*args*/)
{
TInt err=KErrNone;
TPositionModuleId moduleId;
err = self->posServ->GetDefaultModuleId(moduleId);
if(err!=KErrNone){
return SPyErr_SetFromSymbianOSErr(err);
}
return Py_BuildValue("i",moduleId);
}
/*
* Return information about the available positioning modules.
*/
extern "C" PyObject *
PositionServer_modules(PositionServer_object* self, PyObject* /*args*/)
{
TUint numOfModules=0;
TInt err=KErrNone;
err = self->posServ->GetNumModules(numOfModules);
if(err!=KErrNone){
return SPyErr_SetFromSymbianOSErr(err);
}
TPositionModuleInfo moduleInfo;
TBuf<KPositionMaxModuleName> moduleName;
PyObject* modules=PyList_New(numOfModules);
if(modules==NULL){
return PyErr_NoMemory();
}
for(TInt i=0;i<(TInt)numOfModules;i++){
self->posServ->GetModuleInfoByIndex(i,moduleInfo);
moduleInfo.GetModuleName(moduleName);
PyObject* moduleDict = Py_BuildValue("{s:i,s:i,s:u#}",
"id",moduleInfo.ModuleId(),
"available",moduleInfo.IsAvailable(),
"name",moduleName.Ptr(),moduleName.Length());
if(moduleDict==NULL){
Py_DECREF(modules);
return NULL;
}
if(PyList_SetItem(modules, i, moduleDict)){
Py_DECREF(modules);
return NULL;
};
}
return (PyObject*)modules;
}
/*
* Return information about the specified position module.
*/
extern "C" PyObject *
PositionServer_module_info(PositionServer_object* self, PyObject* args)
{
TInt moduleId = KNullUidValue;
TInt err=KErrNone;
if (!PyArg_ParseTuple(args, "i", &moduleId)){
return NULL;
}
TPositionModuleId moduleUid={moduleId};
TPositionModuleInfo info;
err = self->posServ->GetModuleInfoById(moduleUid, info);
if(KErrNone!=err){
return SPyErr_SetFromSymbianOSErr(err);
}
TBuf<KPositionMaxModuleName> moduleName;
info.GetModuleName(moduleName);
TPositionQuality posQual;
info.GetPositionQuality(posQual);
TPositionModuleStatus status;
self->posServ->GetModuleStatus(status, moduleUid);
if(KErrNone!=err){
return SPyErr_SetFromSymbianOSErr(err);
}
PyObject* positionQuality = Py_BuildValue("{s:d,s:d,s:i,s:i,s:L,s:L}",
"horizontal_accuracy",posQual.HorizontalAccuracy(),
"vertical_accuracy",posQual.HorizontalAccuracy(),
"power_consumption",posQual.PowerConsumption(),
"cost",posQual.CostIndicator(),
"time_to_next_fix",posQual.TimeToNextFix().Int64(),
"time_to_first_fix",posQual.TimeToNextFix().Int64());
if(positionQuality==NULL){
return NULL;
}
PyObject* moduleStatus = Py_BuildValue("{s:i,s:i}",
"device_status",status.DeviceStatus(),
"data_quality",status.DataQualityStatus());
if(moduleStatus==NULL){
Py_DECREF(positionQuality);
return NULL;
}
return Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:u#,s,u#,s:N,s:N}",
"available",info.IsAvailable(),
"id",info.ModuleId(),
"technology",info.TechnologyType(),
"location",info.DeviceLocation(),
"capabilities",info.Capabilities(),
"name",moduleName.Ptr(),moduleName.Length(),
"version",info.Version().Name().Ptr(),info.Version().Name().Length(),
"position_quality",positionQuality,
"status",moduleStatus);
}
/*
* Deallocate the PositionServer object.
*/
extern "C" {
static void PositionServer_dealloc(PositionServer_object *posServ)
{
//debugprint("position server dealloc"); // ??? debug
if (posServ->posServ) {
posServ->posServ->Close();
delete posServ->posServ;
posServ->posServ = NULL;
}
PyObject_Del(posServ);
}
}
/*
* Create new Positioner object.
*/
extern "C" PyObject *
new_Positioner_object(PositionServer_object* posServ, TPositionModuleId moduleUid)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -