Merge "libbinder: include sys/uio.h instead of cutils/uio.h"
diff --git a/cmds/inputflinger/Android.mk b/cmds/inputflinger/Android.mk
deleted file mode 100644
index b44de44..0000000
--- a/cmds/inputflinger/Android.mk
+++ /dev/null
@@ -1,28 +0,0 @@
-# Copyright (C) 2013 The Android Open Source Project
-#
-# 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.
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
-	main.cpp
-
-LOCAL_SHARED_LIBRARIES := \
-	libbinder \
-	libinputflinger \
-	libutils
-
-LOCAL_MODULE := inputflinger
-
-include $(BUILD_EXECUTABLE)
diff --git a/cmds/sensorservice/Android.mk b/cmds/sensorservice/Android.mk
deleted file mode 100644
index 0811be5..0000000
--- a/cmds/sensorservice/Android.mk
+++ /dev/null
@@ -1,19 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
-	main_sensorservice.cpp
-
-LOCAL_SHARED_LIBRARIES := \
-	libsensorservice \
-	libbinder \
-	libutils
-
-LOCAL_C_INCLUDES := \
-	$(LOCAL_PATH)/../../services/sensorservice
-
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_MODULE:= sensorservice
-
-include $(BUILD_EXECUTABLE)
diff --git a/cmds/surfaceflinger/Android.mk b/cmds/surfaceflinger/Android.mk
deleted file mode 100644
index 1df32bb..0000000
--- a/cmds/surfaceflinger/Android.mk
+++ /dev/null
@@ -1,17 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
-	main_surfaceflinger.cpp 
-
-LOCAL_SHARED_LIBRARIES := \
-	libsurfaceflinger \
-	libbinder \
-	libutils
-
-LOCAL_C_INCLUDES := \
-	$(LOCAL_PATH)/../../services/surfaceflinger
-
-LOCAL_MODULE:= surfaceflinger
-
-include $(BUILD_EXECUTABLE)
diff --git a/data/etc/android.hardware.nfc.hce.xml b/data/etc/android.hardware.nfc.hce.xml
new file mode 100644
index 0000000..10b96b1
--- /dev/null
+++ b/data/etc/android.hardware.nfc.hce.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     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.
+-->
+
+<!-- This feature indicates that the device supports host-based
+     NFC card emulation -->
+<permissions>
+    <feature name="android.hardware.nfc.hce" />
+</permissions>
diff --git a/include/input/InputDevice.h b/include/input/InputDevice.h
index d8256c1..1419b45 100644
--- a/include/input/InputDevice.h
+++ b/include/input/InputDevice.h
@@ -67,10 +67,11 @@
         float resolution;
     };
 
-    void initialize(int32_t id, int32_t generation, const InputDeviceIdentifier& identifier,
-            const String8& alias, bool isExternal);
+    void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
+            const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal);
 
     inline int32_t getId() const { return mId; }
+    inline int32_t getControllerNumber() const { return mControllerNumber; }
     inline int32_t getGeneration() const { return mGeneration; }
     inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
     inline const String8& getAlias() const { return mAlias; }
@@ -111,6 +112,7 @@
 private:
     int32_t mId;
     int32_t mGeneration;
+    int32_t mControllerNumber;
     InputDeviceIdentifier mIdentifier;
     String8 mAlias;
     bool mIsExternal;
diff --git a/libs/binder/Android.mk b/libs/binder/Android.mk
index 4c8820e..f3f8daf 100644
--- a/libs/binder/Android.mk
+++ b/libs/binder/Android.mk
@@ -47,5 +47,6 @@
 include $(CLEAR_VARS)
 LOCAL_LDLIBS += -lpthread
 LOCAL_MODULE := libbinder
+LOCAL_STATIC_LIBRARIES += libutils
 LOCAL_SRC_FILES := $(sources)
 include $(BUILD_STATIC_LIBRARY)
diff --git a/libs/binder/AppOpsManager.cpp b/libs/binder/AppOpsManager.cpp
index 99c2747..61b4f7d 100644
--- a/libs/binder/AppOpsManager.cpp
+++ b/libs/binder/AppOpsManager.cpp
@@ -31,6 +31,7 @@
     if (gToken == NULL) {
         gToken = service->getToken(new BBinder());
     }
+    pthread_mutex_unlock(&gTokenMutex);
     return gToken;
 }
 
diff --git a/libs/gui/BufferQueue.cpp b/libs/gui/BufferQueue.cpp
index 73bd488..ad2f1bb 100644
--- a/libs/gui/BufferQueue.cpp
+++ b/libs/gui/BufferQueue.cpp
@@ -843,13 +843,13 @@
     if (presentWhen != 0 && desiredPresent > presentWhen &&
             desiredPresent - presentWhen < MAX_FUTURE_NSEC)
     {
-        ALOGV("pts defer: des=%lld when=%lld (%lld) now=%lld",
+        ST_LOGV("pts defer: des=%lld when=%lld (%lld) now=%lld",
                 desiredPresent, presentWhen, desiredPresent - presentWhen,
                 systemTime(CLOCK_MONOTONIC));
         return PRESENT_LATER;
     }
     if (presentWhen != 0) {
-        ALOGV("pts accept: %p[%d] sig=%lld des=%lld when=%lld (%lld)",
+        ST_LOGV("pts accept: %p[%d] sig=%lld des=%lld when=%lld (%lld)",
                 mSlots, buf, mSlots[buf].mFence->getSignalTime(),
                 desiredPresent, presentWhen, desiredPresent - presentWhen);
     }
@@ -889,35 +889,31 @@
     ATRACE_CALL();
     ATRACE_BUFFER_INDEX(buf);
 
-    Mutex::Autolock _l(mMutex);
-
     if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
         return BAD_VALUE;
     }
 
-    // Check if this buffer slot is on the queue
-    bool slotQueued = false;
-    Fifo::iterator front(mQueue.begin());
-    while (front != mQueue.end() && !slotQueued) {
-        if (front->mBuf == buf)
-            slotQueued = true;
-        front++;
-    }
+    Mutex::Autolock _l(mMutex);
 
     // If the frame number has changed because buffer has been reallocated,
     // we can ignore this releaseBuffer for the old buffer.
     if (frameNumber != mSlots[buf].mFrameNumber) {
-        // This should only occur if new buffer is still in the queue
-        ALOGE_IF(!slotQueued,
-                "received old buffer(#%lld) after new buffer(#%lld) on same "
-                "slot #%d already acquired", frameNumber,
-                mSlots[buf].mFrameNumber, buf);
         return STALE_BUFFER_SLOT;
     }
-    // this should never happen
-    ALOGE_IF(slotQueued,
-            "received new buffer(#%lld) on slot #%d that has not yet been "
-            "acquired", frameNumber, buf);
+
+
+    // Internal state consistency checks:
+    // Make sure this buffers hasn't been queued while we were owning it (acquired)
+    Fifo::iterator front(mQueue.begin());
+    Fifo::const_iterator const end(mQueue.end());
+    while (front != end) {
+        if (front->mBuf == buf) {
+            LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
+                    "acquired", mConsumerName.string(), frameNumber, buf);
+            break; // never reached
+        }
+        front++;
+    }
 
     // The buffer can now only be released if its in the acquired state
     if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
@@ -1007,8 +1003,7 @@
     return NO_ERROR;
 }
 
-status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
-{
+status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
     ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
     if (!w || !h) {
         ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
diff --git a/libs/input/InputDevice.cpp b/libs/input/InputDevice.cpp
index 54703d4..b11110a 100644
--- a/libs/input/InputDevice.cpp
+++ b/libs/input/InputDevice.cpp
@@ -127,26 +127,25 @@
 // --- InputDeviceInfo ---
 
 InputDeviceInfo::InputDeviceInfo() {
-    initialize(-1, -1, InputDeviceIdentifier(), String8(), false);
+    initialize(-1, 0, -1, InputDeviceIdentifier(), String8(), false);
 }
 
 InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
-        mId(other.mId), mGeneration(other.mGeneration), mIdentifier(other.mIdentifier),
-        mAlias(other.mAlias), mIsExternal(other.mIsExternal), mSources(other.mSources),
-        mKeyboardType(other.mKeyboardType),
-        mKeyCharacterMap(other.mKeyCharacterMap),
-        mHasVibrator(other.mHasVibrator),
-        mHasButtonUnderPad(other.mHasButtonUnderPad),
-        mMotionRanges(other.mMotionRanges) {
+        mId(other.mId), mGeneration(other.mGeneration), mControllerNumber(other.mControllerNumber),
+        mIdentifier(other.mIdentifier), mAlias(other.mAlias), mIsExternal(other.mIsExternal),
+        mSources(other.mSources), mKeyboardType(other.mKeyboardType),
+        mKeyCharacterMap(other.mKeyCharacterMap), mHasVibrator(other.mHasVibrator),
+        mHasButtonUnderPad(other.mHasButtonUnderPad), mMotionRanges(other.mMotionRanges) {
 }
 
 InputDeviceInfo::~InputDeviceInfo() {
 }
 
-void InputDeviceInfo::initialize(int32_t id, int32_t generation,
+void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
         const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal) {
     mId = id;
     mGeneration = generation;
+    mControllerNumber = controllerNumber;
     mIdentifier = identifier;
     mAlias = alias;
     mIsExternal = isExternal;
diff --git a/services/connectivitymanager/Android.mk b/services/connectivitymanager/Android.mk
new file mode 100644
index 0000000..e986abc
--- /dev/null
+++ b/services/connectivitymanager/Android.mk
@@ -0,0 +1,13 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= ConnectivityManager.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+	libutils \
+	libbinder
+
+LOCAL_MODULE:= libconnectivitymanager
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/services/connectivitymanager/ConnectivityManager.cpp b/services/connectivitymanager/ConnectivityManager.cpp
new file mode 100644
index 0000000..949c2ac
--- /dev/null
+++ b/services/connectivitymanager/ConnectivityManager.cpp
@@ -0,0 +1,51 @@
+/**
+ * Copyright (c) 2013, The Android Open Source Project
+ *
+ * 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 <sys/types.h>
+
+#include <utils/Singleton.h>
+
+#include <binder/BinderService.h>
+#include <binder/Parcel.h>
+
+#include "ConnectivityManager.h"
+
+namespace android {
+
+ConnectivityManager::ConnectivityManager() {
+    const sp<IServiceManager> sm(defaultServiceManager());
+    if (sm != NULL) {
+        const String16 name("connectivity");
+        mConnectivityService = sm->getService(name);
+    }
+}
+
+void ConnectivityManager::markSocketAsUserImpl(int fd, uid_t uid) {
+    Parcel data, reply;
+    data.writeInterfaceToken(DESCRIPTOR);
+    // parcelable objects are preceded by a 1 if not null in aidl generated code.
+    // Play nice with the generated Java
+    data.writeInt32(1);
+    data.writeFileDescriptor(fd);
+    data.writeInt32(uid);
+    mConnectivityService->transact(TRANSACTION_markSocketAsUser, data, &reply, 0);
+}
+
+const String16 ConnectivityManager::DESCRIPTOR("android.net.IConnectivityManager");
+
+ANDROID_SINGLETON_STATIC_INSTANCE(ConnectivityManager)
+
+};
diff --git a/services/connectivitymanager/ConnectivityManager.h b/services/connectivitymanager/ConnectivityManager.h
new file mode 100644
index 0000000..37f5d98
--- /dev/null
+++ b/services/connectivitymanager/ConnectivityManager.h
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2013, The Android Open Source Project
+ *
+ * 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 <stdint.h>
+#include <sys/types.h>
+
+#include <utils/Singleton.h>
+
+namespace android {
+
+class ConnectivityManager : public Singleton<ConnectivityManager> {
+    // Keep this in sync with IConnectivityManager.aidl
+    static const int TRANSACTION_markSocketAsUser = IBinder::FIRST_CALL_TRANSACTION;
+    static const String16 DESCRIPTOR;
+
+    friend class Singleton<ConnectivityManager>;
+    sp<IBinder> mConnectivityService;
+
+    ConnectivityManager();
+
+    void markSocketAsUserImpl(int fd, uid_t uid);
+
+public:
+    static void markSocketAsUser(int fd, uid_t uid) {
+        ConnectivityManager::getInstance().markSocketAsUserImpl(fd, uid);
+    }
+};
+
+};
diff --git a/services/inputflinger/Android.mk b/services/inputflinger/Android.mk
index 7148db8..e32d38a 100644
--- a/services/inputflinger/Android.mk
+++ b/services/inputflinger/Android.mk
@@ -33,3 +33,18 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
+########################################################################
+# build input flinger executable
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	main.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libbinder \
+	libinputflinger \
+	libutils
+
+LOCAL_MODULE := inputflinger
+
+include $(BUILD_EXECUTABLE)
diff --git a/cmds/inputflinger/main.cpp b/services/inputflinger/main.cpp
similarity index 96%
rename from cmds/inputflinger/main.cpp
rename to services/inputflinger/main.cpp
index 6de7b24..3209a62 100644
--- a/cmds/inputflinger/main.cpp
+++ b/services/inputflinger/main.cpp
@@ -15,7 +15,7 @@
  */
 
 #include <binder/BinderService.h>
-#include <InputFlinger.h>
+#include "InputFlinger.h"
 
 using namespace android;
 
diff --git a/services/sensorservice/Android.mk b/services/sensorservice/Android.mk
index 14a4e55..4f24ddc 100644
--- a/services/sensorservice/Android.mk
+++ b/services/sensorservice/Android.mk
@@ -31,3 +31,21 @@
 LOCAL_MODULE:= libsensorservice
 
 include $(BUILD_SHARED_LIBRARY)
+
+#####################################################################
+# build executable
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	main_sensorservice.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libsensorservice \
+	libbinder \
+	libutils
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE:= sensorservice
+
+include $(BUILD_EXECUTABLE)
diff --git a/cmds/sensorservice/main_sensorservice.cpp b/services/sensorservice/main_sensorservice.cpp
similarity index 96%
rename from cmds/sensorservice/main_sensorservice.cpp
rename to services/sensorservice/main_sensorservice.cpp
index 8610627..303b65f 100644
--- a/cmds/sensorservice/main_sensorservice.cpp
+++ b/services/sensorservice/main_sensorservice.cpp
@@ -15,7 +15,7 @@
  */
 
 #include <binder/BinderService.h>
-#include <SensorService.h>
+#include "SensorService.h"
 
 using namespace android;
 
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index bb0a2f9..8dbb9c5 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -65,6 +65,22 @@
 include $(BUILD_SHARED_LIBRARY)
 
 ###############################################################
+# build surfaceflinger's executable
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	main_surfaceflinger.cpp 
+
+LOCAL_SHARED_LIBRARIES := \
+	libsurfaceflinger \
+	libbinder \
+	libutils
+
+LOCAL_MODULE:= surfaceflinger
+
+include $(BUILD_EXECUTABLE)
+
+###############################################################
 # uses jni which may not be available in PDK
 ifneq ($(wildcard libnativehelper/include),)
 include $(CLEAR_VARS)
diff --git a/cmds/surfaceflinger/main_surfaceflinger.cpp b/services/surfaceflinger/main_surfaceflinger.cpp
similarity index 96%
rename from cmds/surfaceflinger/main_surfaceflinger.cpp
rename to services/surfaceflinger/main_surfaceflinger.cpp
index ce7fde0..8503d4e 100644
--- a/cmds/surfaceflinger/main_surfaceflinger.cpp
+++ b/services/surfaceflinger/main_surfaceflinger.cpp
@@ -15,7 +15,7 @@
  */
 
 #include <binder/BinderService.h>
-#include <SurfaceFlinger.h>
+#include "SurfaceFlinger.h"
 
 using namespace android;