/* JAVADOC COMMENT ELIDED */
KNIEXPORT KNI_RETURNTYPE_VOID
    Java_com_sun_j2me_location_PlatformLocationProvider_resetImpl() {

    MidpReentryData *info = NULL;
    ProviderInfo *pInfo = NULL;
    jint provider = KNI_GetParameterAsInt(1);
    jsr179_result res;

    info = (MidpReentryData*)SNI_GetReentryData(NULL);
    if (info == NULL) {
        /* reset provider */
        res = jsr179_update_cancel((jsr179_handle)provider);
        switch (res) {
            case JSR179_STATUSCODE_OK:
            case JSR179_STATUSCODE_FAIL:
                break;
            case JSR179_STATUSCODE_INVALID_ARGUMENT:
                /* wrong provider name */
                KNI_ThrowNew(midpIllegalArgumentException, "wrong provider");
                break;
            case JSR179_STATUSCODE_WOULD_BLOCK:
                /* wait for javanotify */
                pInfo = getProviderInfo(provider);
                if(pInfo != NULL) {
                    pInfo->locked = KNI_FALSE;
                }
                lock_thread(JSR179_EVENT_UPDATE_ONCE, provider);
                break;
            default:
                break;
        }
    }

    KNI_ReturnVoid();
}
void Requestor::requestSetField( MemberOwner& owner, co::IField* field, const co::Any arg )
{
    if( !_connected )
        CORAL_THROW( RemotingException, "Trying to request with the node stopped");
    
    InvocationDetails details( owner.instanceID, owner.facetID, field->getIndex(), 
                              owner.inheritanceDepth, false );
    ParameterPusher& pusher = _marshaller.beginInvocation( _publicEndpoint, details );
    
    co::IType* fieldType = field->getType();
    if( fieldType->getKind() != co::TK_INTERFACE )
    {
        pusher.pushValue( arg, fieldType );
    }
    else
    {
        ReferenceType refType;
        getProviderInfo( arg.get<co::IService*>(), refType );
        pusher.pushReference( refType );
    }
    
    std::string msg;
    _marshaller.marshalInvocation( msg );
    _handler->handleAsynchRequest( msg );
}
/* JAVADOC COMMENT ELIDED */
KNIEXPORT KNI_RETURNTYPE_BOOLEAN
    Java_com_sun_j2me_location_PlatformLocationProvider_receiveNewLocationImpl() {

    ProviderInfo *pInfo = NULL;
    jlong timestamp = 0;
    jboolean ret = KNI_FALSE;
    jint provider;

    provider = KNI_GetParameterAsInt(1);
    timestamp = KNI_GetParameterAsLong(2);
    pInfo = getProviderInfo(provider);
    if(pInfo != NULL) {
		if (pInfo->newLocationAvailable == KNI_TRUE) {
			if(jsr179_location_get((jsr179_handle)provider, &pInfo->lastLocation) ==
					JSR179_STATUSCODE_OK) {
				/* get addressInfo if it is present */
				if (pInfo->lastLocation.addressInfoFieldNumber > 0) {
					if(jsr179_get_addressinfo((jsr179_handle)provider,
						&pInfo->lastLocation.addressInfoFieldNumber, 
						pInfo->lastAddressInfo) != JSR179_STATUSCODE_OK) {
						/* drop Address Info for this location */
						pInfo->lastLocation.addressInfoFieldNumber = 0;
					}
				}

				/* get extraInfo if it is present */
				if (pInfo->lastLocation.extraInfoSize > 0) {
					if (jsr179_get_extrainfo((jsr179_handle)provider, 
								pInfo->lastLocation.extraInfoSize, 
								pInfo->lastExtraInfo,
                                pInfo->otherExtraInfoMimeType) != JSR179_STATUSCODE_OK) {
						pInfo->lastLocation.extraInfoSize = 0;
					}
				}
				
				pInfo->lastLocationTimestamp = timestamp;
				pInfo->newLocationAvailable = KNI_FALSE;

				lastUpdatedProvider = provider;

		        ret = KNI_TRUE;
			}
		} else {
			ret = KNI_TRUE;
		}
    }

    KNI_ReturnBoolean(ret);
}
/* JAVADOC COMMENT ELIDED */
KNIEXPORT KNI_RETURNTYPE_INT
    Java_com_sun_j2me_location_PlatformLocationProvider_getLastKnownLocationImpl() {

    jboolean ret = KNI_FALSE;

    KNI_StartHandles(2);
    KNI_DeclareHandle(string_obj);
    KNI_DeclareHandle(locationInfo);

    KNI_GetParameterAsObject(1, locationInfo);

    ret = getLocation(locationInfo, string_obj, getProviderInfo(lastUpdatedProvider));    

    KNI_EndHandles();
    KNI_ReturnBoolean(ret);
}
/* JAVADOC COMMENT ELIDED */
KNIEXPORT KNI_RETURNTYPE_INT
    Java_com_sun_j2me_location_PlatformLocationProvider_getLastLocationImpl() {

    jint provider;
    jboolean ret;

    KNI_StartHandles(2);
    KNI_DeclareHandle(string_obj);
    KNI_DeclareHandle(locationInfo);

    provider = KNI_GetParameterAsInt(1);
    KNI_GetParameterAsObject(2, locationInfo);

    ret = getLocation(locationInfo, string_obj, getProviderInfo(provider));    

    KNI_EndHandles();
    KNI_ReturnBoolean(ret);
}
void Requestor::pushParameters( co::IMethod* method, co::Slice<co::Any> args, 
                                   ParameterPusher& pusher )
{
    co::TSlice<co::IParameter*> params = method->getParameters();
    for( int i = 0; i < params.getSize(); i++ )
    {
		if( params[i]->getIsIn() )
		{
			co::IType* paramType = params[i]->getType();
			if( paramType->getKind() != co::TK_INTERFACE )
			{
				pusher.pushValue( args[i], paramType );
			}
			else
			{
				ReferenceType refType;
				getProviderInfo( args[i].get<co::IService*>(), refType );
				pusher.pushReference( refType );
			}
		}
    }
}
/* JAVADOC COMMENT ELIDED */
KNIEXPORT KNI_RETURNTYPE_BOOLEAN
    Java_com_sun_j2me_location_PlatformLocationProvider_waitForNewLocation() {

    jboolean ret = KNI_FALSE;
    MidpReentryData *info = NULL;
    ProviderInfo *pInfo = NULL;
    jsr179_result res;
    jint provider;
    jlong timeout;
    KNI_StartHandles(1);
    provider = KNI_GetParameterAsInt(1);
    timeout = KNI_GetParameterAsLong(2);
    pInfo = getProviderInfo(provider);
    if(pInfo != NULL) {
        info = (MidpReentryData*)SNI_GetReentryData(NULL);
        if (info == NULL) {
            /* First call -request */
            if(pInfo->locked == KNI_TRUE) {
                lock_thread(JSR179_EVENT_UPDATE_ONCE, provider);
            } else {
                /* request new location */
                res = jsr179_update_set((jsr179_handle)provider, timeout);
                switch (res) {
                    case JSR179_STATUSCODE_WOULD_BLOCK:
                        /* wait for javanotify */
                        pInfo->locked = KNI_TRUE;
                        lock_thread(JSR179_EVENT_UPDATE_ONCE, provider);
                        break;
                    case JSR179_STATUSCODE_OK:
                        /* location updated successfully */
                        pInfo->locked = KNI_FALSE;
			            pInfo->newLocationAvailable = KNI_TRUE;
                        ret = KNI_TRUE;
                        break;
                    case JSR179_STATUSCODE_FAIL:
                        /* fail */
                        pInfo->locked = KNI_FALSE;
                        /* wrong provider name */
                        KNI_ThrowNew(midpIllegalArgumentException, 
                                    "wrong provider");
                        break;
                    default:
                        /* fail */
                        pInfo->locked = KNI_FALSE;
                        break;
                }
            }
        } else {
            /* Response */
            if (info->status == JSR179_STATUSCODE_OK) {
                /* location updated successfully */
				pInfo->newLocationAvailable = KNI_TRUE;
                ret = KNI_TRUE;
            } else {
                /* location updated failed */
                ret = KNI_FALSE;
            }
            pInfo->locked = KNI_FALSE;
        }
    }
    KNI_EndHandles();
    KNI_ReturnBoolean(ret);
}
/* JAVADOC COMMENT ELIDED */
KNIEXPORT KNI_RETURNTYPE_INT
    Java_com_sun_j2me_location_PlatformLocationProvider_open() {

    MidpReentryData *info = NULL;
    ProviderInfo *pInfo = NULL;
    jsr179_result res;
    jsr179_handle pProvider = NULL;
    jint provider = 0;
    
    KNI_StartHandles(1);
    GET_PARAMETER_AS_PCSL_STRING(1, name)
    info = (MidpReentryData*)SNI_GetReentryData(NULL);
    if (info == NULL) {
        res = jsr179_provider_open(name, &pProvider);
        switch (res) {
            case JSR179_STATUSCODE_OK:
                /* handler returned immediatelly */
                provider = (jint)pProvider;
                register_provider(provider);
                break;
            case JSR179_STATUSCODE_INVALID_ARGUMENT:
                /* wrong provider name */
                KNI_ThrowNew(midpIllegalArgumentException, 
                            "wrong provider name");
                break;
            case JSR179_STATUSCODE_FAIL:
                /* fail */
                KNI_ThrowNew(midpIOException, "open failed");
                break;
            case JSR179_STATUSCODE_WOULD_BLOCK:
                /* wait for javanotify */
                provider = (jint)pProvider;
                pInfo = register_provider(provider);
                if (pInfo != NULL) {
                    pInfo->locked = KNI_TRUE;
                }
                lock_thread(JSR179_EVENT_OPEN_COMPLETED, provider);
                break;
            default:
                break;
        }
    } else {
        /* Second call for this thread - finish open */
        if (info->status == JSR179_STATUSCODE_OK) {
            /* Provider opened successfully */
            provider = info->descriptor;
            pInfo = getProviderInfo(provider);
            if (pInfo != NULL) {
                pInfo->locked = KNI_FALSE;
            }
        } else {
            /* Provider open failed*/
            unregister_provider(provider);
            provider = 0;
        }
    }

    RELEASE_PCSL_STRING_PARAMETER
    KNI_EndHandles();
    KNI_ReturnInt(provider);
}