bool
NfcMessageHandler::WriteNDEFMessage(Parcel& aParcel, const CommandOptions& aOptions)
{
  int recordCount = aOptions.mRecords.Length();
  aParcel.writeInt32(recordCount);
  for (int i = 0; i < recordCount; i++) {
    const NDEFRecordStruct& record = aOptions.mRecords[i];
    aParcel.writeInt32(static_cast<int32_t>(record.mTnf));

    void* data;

    aParcel.writeInt32(record.mType.Length());
    data = aParcel.writeInplace(record.mType.Length());
    memcpy(data, record.mType.Elements(), record.mType.Length());

    aParcel.writeInt32(record.mId.Length());
    data = aParcel.writeInplace(record.mId.Length());
    memcpy(data, record.mId.Elements(), record.mId.Length());

    aParcel.writeInt32(record.mPayload.Length());
    data = aParcel.writeInplace(record.mPayload.Length());
    memcpy(data, record.mPayload.Elements(), record.mPayload.Length());
  }

  return true;
}
static void android_os_Parcel_writeNative(JNIEnv* env, jclass clazz, jlong nativePtr, jobject data,
                                          jint offset, jint length)
{
    Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
    if (parcel == NULL) {
        return;
    }

    const status_t err = parcel->writeInt32(length);
    if (err != NO_ERROR) {
        signalExceptionForError(env, clazz, err);
        return;
    }

    void* dest = parcel->writeInplace(length);
    if (dest == NULL) {
        signalExceptionForError(env, clazz, NO_MEMORY);
        return;
    }

    jbyte* ar = (jbyte*)env->GetPrimitiveArrayCritical((jarray)data, 0);
    if (ar) {
        memcpy(dest, ar + offset, length);
        env->ReleasePrimitiveArrayCritical((jarray)data, ar, 0);
    }
}
int PhoneMachine::incomingThread()
{
	char boardname[PROPERTY_VALUE_MAX];
	if (property_get("ro.product.board", boardname, "") > 0) {
		SLOGV("%s: -------------Board %s -----------------\n", __FUNCTION__, boardname);
		// QCOM Version 4.1.2 onwards, supports multiple clients and needs a SUB1/2 string to
		// identify client
		// For this example code we will just use "SUB1" for client 0
		if ( !strcasecmp(boardname, "msm8960")) {
			char *sub = "SUB1";
			int ret = ::send(mRILfd, sub, sizeof(sub), 0);
			if (ret != (int) sizeof(sub)) {
	    		perror("Socket write error when sending parcel"); 
	    		return ret;
			}
			SLOGV("%s: sent SUB1\n", __FUNCTION__);
		}
	}
	else SLOGE("%s: could not get device name\n", __FUNCTION__);

    while (1) {
	uint32_t header;
	int ret = read(mRILfd, &header, sizeof(header));

	if (ret != sizeof(header)) {
	    SLOGW("Read %d bytes instead of %d\n", ret, sizeof(header));
	    perror("PhoneMachine::incomingThread read on header");
	    return ret;
	}
	int data_size = ntohl(header);
	Parcel data;
	ret = read(mRILfd, data.writeInplace(data_size), data_size);
	if (ret != data_size) {
	    perror("PhoneMachine::incomingThread read on payload");
	    return ret;
	}
	if (mDebug & DEBUG_INCOMING) {
	    SLOGV("<<<<<<< INCOMING <<<<<<<<<<\n");
	    const uint8_t *ptr = data.data();
	    for (int i = 0 ; i < data_size ; i++ ) {
		SLOGV("%02x ", *ptr++);
		if ((i+1) % 8 == 0 || (i+1 >= data_size))
		    SLOGV("\n");
	    }
	    SLOGV("<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
	}
	data.setDataPosition(0);
	int type = data.readInt32();
//	SLOGV("New message received: %d bytes type=%s\n", data_size, 
//	       (type ==RESPONSE_SOLICITED ? "solicited" : "unsolicited"));
	if (type == RESPONSE_SOLICITED) 
	    receiveSolicited(data);
	else
	    receiveUnsolicited(data);
    }
    return NO_ERROR;
}
bool
NfcMessageHandler::TransceiveRequest(Parcel& aParcel, const CommandOptions& aOptions)
{
  aParcel.writeInt32(static_cast<int32_t>(NfcRequestType::Transceive));
  aParcel.writeInt32(aOptions.mSessionId);
  aParcel.writeInt32(aOptions.mTechnology);

  uint32_t length = aOptions.mCommand.Length();
  aParcel.writeInt32(length);

  void* data = aParcel.writeInplace(length);
  memcpy(data, aOptions.mCommand.Elements(), length);

  mRequestIdQueue.AppendElement(aOptions.mRequestId);
  return true;
}
static void android_os_Parcel_unmarshall(JNIEnv* env, jclass clazz, jlong nativePtr,
                                         jbyteArray data, jint offset, jint length)
{
    Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
    if (parcel == NULL || length < 0) {
       return;
    }

    jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(data, 0);
    if (array)
    {
        parcel->setDataSize(length);
        parcel->setDataPosition(0);

        void* raw = parcel->writeInplace(length);
        memcpy(raw, (array + offset), length);

        env->ReleasePrimitiveArrayCritical(data, array, 0);
    }
}
status_t layer_state_t::write(Parcel& output) const
{
    output.writeStrongBinder(surface);
    output.writeUint32(what);
    output.writeFloat(x);
    output.writeFloat(y);
    output.writeUint32(z);
    output.writeUint32(w);
    output.writeUint32(h);
    output.writeUint32(layerStack);
    output.writeFloat(alpha);
    output.writeUint32(flags);
    output.writeUint32(mask);
    *reinterpret_cast<layer_state_t::matrix22_t *>(
            output.writeInplace(sizeof(layer_state_t::matrix22_t))) = matrix;
    output.write(crop);
    output.write(finalCrop);
    output.writeStrongBinder(handle);
    output.writeUint64(frameNumber);
    output.writeInt32(overrideScalingMode);
    output.write(transparentRegion);
    return NO_ERROR;
}