summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorDeven Patel <cdevenp@codeaurora.org>2016-03-07 10:44:34 -0800
committerDeven Patel <cdevenp@codeaurora.org>2016-03-07 10:55:20 -0800
commita29688ff347be4972133eb11ccecaf03b0d3445e (patch)
tree45dc8adb0d08a30bc542075042884cef831bcfdf /utils
parentfa7a874eb0586c7844fe3a4cb8d0063ce53464f0 (diff)
downloadgps-a29688ff347be4972133eb11ccecaf03b0d3445e.tar.gz
Revert "Merging m_master changes to oe_master".
This reverts commit 1aeb6bad84c0afd0e032c3d4d7e33959501e18f1 Change-Id: Iec0a96e7cdfe55ef5836c92a2ae1cce407f6cd5e
Diffstat (limited to 'utils')
-rw-r--r--utils/Android.mk33
-rw-r--r--utils/LocHeap.cpp354
-rw-r--r--utils/LocHeap.h96
-rw-r--r--utils/LocSharedLock.h59
-rw-r--r--utils/LocThread.cpp264
-rw-r--r--utils/LocThread.h92
-rw-r--r--utils/LocTimer.cpp737
-rw-r--r--utils/LocTimer.h74
-rw-r--r--utils/MsgTask.cpp100
-rw-r--r--utils/MsgTask.h67
-rw-r--r--utils/linked_list.c6
-rw-r--r--utils/loc_cfg.cpp456
-rw-r--r--utils/loc_cfg.h26
-rw-r--r--utils/loc_log.cpp82
-rw-r--r--utils/loc_log.h17
-rw-r--r--utils/loc_misc_utils.cpp114
-rw-r--r--utils/loc_misc_utils.h99
-rw-r--r--utils/loc_target.cpp62
-rw-r--r--utils/loc_target.h16
-rw-r--r--utils/loc_timer.c187
-rw-r--r--utils/loc_timer.h36
-rw-r--r--utils/msg_q.c8
-rw-r--r--utils/platform_lib_abstractions/elapsed_millis_since_boot.cpp46
-rw-r--r--utils/platform_lib_abstractions/platform_lib_includes.h35
-rw-r--r--utils/platform_lib_abstractions/platform_lib_macros.h81
-rw-r--r--utils/platform_lib_abstractions/platform_lib_time.h35
26 files changed, 421 insertions, 2761 deletions
diff --git a/utils/Android.mk b/utils/Android.mk
index 37512a5..24ab8a1 100644
--- a/utils/Android.mk
+++ b/utils/Android.mk
@@ -18,27 +18,19 @@ LOCAL_SRC_FILES += \
msg_q.c \
linked_list.c \
loc_target.cpp \
- platform_lib_abstractions/elapsed_millis_since_boot.cpp \
- LocHeap.cpp \
- LocTimer.cpp \
- LocThread.cpp \
- MsgTask.cpp \
- loc_misc_utils.cpp
+ loc_timer.c \
+ ../platform_lib_abstractions/elapsed_millis_since_boot.cpp
+
LOCAL_CFLAGS += \
-fno-short-enums \
- -D_ANDROID_ \
- -std=c++11
-
-ifeq ($(TARGET_BUILD_VARIANT),user)
- LOCAL_CFLAGS += -DTARGET_BUILD_VARIANT_USER
-endif
+ -D_ANDROID_
LOCAL_LDFLAGS += -Wl,--export-dynamic
## Includes
LOCAL_C_INCLUDES:= \
- $(LOCAL_PATH)/platform_lib_abstractions
+ $(LOCAL_PATH)/../platform_lib_abstractions
LOCAL_COPY_HEADERS_TO:= gps.utils/
LOCAL_COPY_HEADERS:= \
@@ -47,25 +39,20 @@ LOCAL_COPY_HEADERS:= \
log_util.h \
linked_list.h \
msg_q.h \
- MsgTask.h \
- LocHeap.h \
- LocThread.h \
- LocTimer.h \
loc_target.h \
loc_timer.h \
- LocSharedLock.h \
- platform_lib_abstractions/platform_lib_includes.h \
- platform_lib_abstractions/platform_lib_time.h \
- platform_lib_abstractions/platform_lib_macros.h \
- loc_misc_utils.h
+ ../platform_lib_abstractions/platform_lib_includes.h \
+ ../platform_lib_abstractions/platform_lib_time.h \
+ ../platform_lib_abstractions/platform_lib_macros.h
+
LOCAL_MODULE := libgps.utils
-LOCAL_CLANG := false
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)
include $(BUILD_SHARED_LIBRARY)
endif # not BUILD_TINY_ANDROID
endif # BOARD_VENDOR_QCOM_GPS_LOC_API_HARDWARE
diff --git a/utils/LocHeap.cpp b/utils/LocHeap.cpp
deleted file mode 100644
index d667f14..0000000
--- a/utils/LocHeap.cpp
+++ /dev/null
@@ -1,354 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#include <LocHeap.h>
-
-class LocHeapNode {
- friend class LocHeap;
-
- // size of of the subtree, excluding self, 1 if no subtree
- int mSize;
- LocHeapNode* mLeft;
- LocHeapNode* mRight;
- LocRankable* mData;
-public:
- inline LocHeapNode(LocRankable& data) :
- mSize(1), mLeft(NULL), mRight(NULL), mData(&data) {}
- ~LocHeapNode();
-
- // this only swaps the data of the two nodes, so no
- // detach / re-attached is necessary
- void swap(LocHeapNode& node);
-
- LocRankable* detachData();
-
- // push a node into the tree stucture, keeping sorted by rank
- void push(LocHeapNode& node);
-
- // pop the head node out of the tree stucture. keeping sorted by rank
- static LocHeapNode* pop(LocHeapNode*& top);
-
- // remove a specific node from the tree
- // returns the pointer to the node removed, which would be either the
- // same as input (if successfully removed); or NULL (if failed).
- static LocHeapNode* remove(LocHeapNode*& top, LocRankable& data);
-
- // convenience method to compare data ranking
- inline bool outRanks(LocHeapNode& node) { return mData->outRanks(*node.mData); }
- inline bool outRanks(LocRankable& data) { return mData->outRanks(data); }
-
- // checks if mSize is correct, AND this node is the highest ranking
- // of the entire subtree
- bool checkNodes();
-
- inline int getSize() { return mSize; }
-};
-
-inline
-LocHeapNode::~LocHeapNode() {
- if (mLeft) {
- delete mLeft;
- mLeft = NULL;
- }
- if (mRight) {
- delete mRight;
- mRight = NULL;
- }
- if (mData) {
- mData = NULL;
- }
-}
-
-inline
-void LocHeapNode::swap(LocHeapNode& node) {
- LocRankable* tmpData = node.mData;
- node.mData = mData;
- mData = tmpData;
-}
-
-inline
-LocRankable* LocHeapNode::detachData() {
- LocRankable* data = mData;
- mData = NULL;
- return data;
-}
-
-// push keeps the tree sorted by rank, it also tries to balance the
-// tree by adding the new node to the smaller of the subtrees.
-// The pointer to the tree and internal links never change. If the
-// mData of tree top ranks lower than that of the incoming node,
-// mData will be swapped with that of the incoming node to ensure
-// ranking, no restructuring the container nodes.
-void LocHeapNode::push(LocHeapNode& node) {
- // ensure the current node ranks higher than in the incoming one
- if (node.outRanks(*this)) {
- swap(node);
- }
-
- // now drop the new node (ensured lower than *this) into a subtree
- if (NULL == mLeft) {
- mLeft = &node;
- } else if (NULL == mRight) {
- mRight = &node;
- } else if (mLeft->mSize <= mRight->mSize) {
- mLeft->push(node);
- } else {
- mRight->push(node);
- }
- mSize++;
-}
-
-// pop keeps the tree sorted by rank, but it does not try to balance
-// the tree. It recursively swaps with the higher ranked top of the
-// subtrees.
-// The return is a popped out node from leaf level, that has the data
-// swapped all the way down from the top. The pinter to the tree and
-// internal links will not be changed or restructured, except for the
-// node that is popped out.
-// If the return pointer == this, this the last node in the tree.
-LocHeapNode* LocHeapNode::pop(LocHeapNode*& top) {
- // we know the top has the highest ranking at this point, else
- // the tree is broken. This top will be popped out. But we need
- // a node from the left or right child, whichever ranks higher,
- // to replace the current top. This then will need to be done
- // recursively to the leaf level. So we swap the mData of the
- // current top node all the way down to the leaf level.
- LocHeapNode* poppedNode = top;
- // top is losing a node in its subtree
- top->mSize--;
- if (top->mLeft || top->mRight) {
- // if mLeft is NULL, mRight for sure is NOT NULL, take that;
- // else if mRight is NULL, mLeft for sure is NOT, take that;
- // else we take the address of whatever has higher ranking mData
- LocHeapNode*& subTop = (NULL == top->mLeft) ? top->mRight :
- ((NULL == top->mRight) ? top->mLeft :
- (top->mLeft->outRanks(*(top->mRight)) ? top->mLeft : top->mRight));
- // swap mData, the tree top gets updated with the new data.
- top->swap(*subTop);
- // pop out from the subtree
- poppedNode = pop(subTop);
- } else {
- // if the top has only single node
- // detach the poppedNode from the tree
- // subTop is the reference of ether mLeft or mRight
- // NOT a local stack pointer. so it MUST be NULL'ed here.
- top = NULL;
- }
-
- return poppedNode;
-}
-
-// navigating through the tree and find the node that hass the input
-// data. Since this is a heap, we do recursive linear search.
-// returns the pointer to the node removed, which would be either the
-// same as input (if successfully removed); or NULL (if failed).
-LocHeapNode* LocHeapNode::remove(LocHeapNode*& top, LocRankable& data) {
- LocHeapNode* removedNode = NULL;
- // this is the node, by address
- if (&data == (LocRankable*)(top->mData)) {
- // pop this node out
- removedNode = pop(top);
- } else if (!data.outRanks(*top->mData)) {
- // subtrees might have this node
- if (top->mLeft) {
- removedNode = remove(top->mLeft, data);
- }
- // if we did not find in mLeft, and mRight is not empty
- if (!removedNode && top->mRight) {
- removedNode = remove(top->mRight, data);
- }
-
- // top lost a node in its subtree
- if (removedNode) {
- top->mSize--;
- }
- }
-
- return removedNode;
-}
-
-// checks if mSize is correct, AND this node is the highest ranking
-// of the entire subtree
-bool LocHeapNode::checkNodes() {
- // size of the current subtree
- int totalSize = mSize;
- if (mLeft) {
- // check the consistency of left subtree
- if (mLeft->outRanks(*this) || !mLeft->checkNodes()) {
- return false;
- }
- // subtract the size of left subtree (with subtree head)
- totalSize -= mLeft->mSize;
- }
-
- if (mRight) {
- // check the consistency of right subtree
- if (mRight->outRanks(*this) || !mRight->checkNodes()) {
- return false;
- }
- // subtract the size of right subtree (with subtree head)
- totalSize -= mRight->mSize;
- }
-
- // for the tree nodes to consistent, totalSize must be 1 now
- return totalSize == 1;
-}
-
-LocHeap::~LocHeap() {
- if (mTree) {
- delete mTree;
- }
-}
-
-void LocHeap::push(LocRankable& node) {
- LocHeapNode* heapNode = new LocHeapNode(node);
- if (!mTree) {
- mTree = heapNode;
- } else {
- mTree->push(*heapNode);
- }
-}
-
-LocRankable* LocHeap::peek() {
- LocRankable* top = NULL;
- if (mTree) {
- top = mTree->mData;
- }
- return top;
-}
-
-LocRankable* LocHeap::pop() {
- LocRankable* locNode = NULL;
- if (mTree) {
- // mTree may become NULL after this call
- LocHeapNode* heapNode = LocHeapNode::pop(mTree);
- locNode = heapNode->detachData();
- delete heapNode;
- }
- return locNode;
-}
-
-LocRankable* LocHeap::remove(LocRankable& rankable) {
- LocRankable* locNode = NULL;
- if (mTree) {
- // mTree may become NULL after this call
- LocHeapNode* heapNode = LocHeapNode::remove(mTree, rankable);
- if (heapNode) {
- locNode = heapNode->detachData();
- delete heapNode;
- }
- }
- return locNode;
-}
-
-#ifdef __LOC_UNIT_TEST__
-bool LocHeap::checkTree() {
- return ((NULL == mTree) || mTree->checkNodes());
-}
-uint32_t LocHeap::getTreeSize() {
- return (NULL == mTree) ? 0 : mTree->getSize();
-}
-#endif
-
-#ifdef __LOC_DEBUG__
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-
-class LocHeapDebug : public LocHeap {
-public:
- bool checkTree() {
- return ((NULL == mTree) || mTree->checkNodes());
- }
-
- uint32_t getTreeSize() {
- return (NULL == mTree) ? 0 : (mTree->getSize());
- }
-};
-
-class LocHeapDebugData : public LocRankable {
- const int mID;
-public:
- LocHeapDebugData(int id) : mID(id) {}
- inline virtual int ranks(LocRankable& rankable) {
- LocHeapDebugData* testData = dynamic_cast<LocHeapDebugData*>(&rankable);
- return testData->mID - mID;
- }
-};
-
-// For Linux command line testing:
-// compilation: g++ -D__LOC_HOST_DEBUG__ -D__LOC_DEBUG__ -g -I. -I../../../../vendor/qcom/proprietary/gps-internal/unit-tests/fakes_for_host -I../../../../system/core/include LocHeap.cpp
-// test: valgrind --leak-check=full ./a.out 100
-int main(int argc, char** argv) {
- srand(time(NULL));
- int tries = atoi(argv[1]);
- int checks = tries >> 3;
- LocHeapDebug heap;
- int treeSize = 0;
-
- for (int i = 0; i < tries; i++) {
- if (i % checks == 0 && !heap.checkTree()) {
- printf("tree check failed before %dth op\n", i);
- }
- int r = rand();
-
- if (r & 1) {
- LocHeapDebugData* data = new LocHeapDebugData(r >> 1);
- heap.push(dynamic_cast<LocRankable&>(*data));
- treeSize++;
- } else {
- LocRankable* rankable = heap.pop();
- if (rankable) {
- delete rankable;
- }
- treeSize ? treeSize-- : 0;
- }
-
- printf("%s: %d == %d\n", (r&1)?"push":"pop", treeSize, heap.getTreeSize());
- if (treeSize != heap.getTreeSize()) {
- printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
- tries = i+1;
- break;
- }
- }
-
- if (!heap.checkTree()) {
- printf("!!!!!!!!!!tree check failed at the end after %d ops!!!!!!!\n", tries);
- } else {
- printf("success!\n");
- }
-
- for (LocRankable* data = heap.pop(); NULL != data; data = heap.pop()) {
- delete data;
- }
-
- return 0;
-}
-
-#endif
diff --git a/utils/LocHeap.h b/utils/LocHeap.h
deleted file mode 100644
index b491948..0000000
--- a/utils/LocHeap.h
+++ /dev/null
@@ -1,96 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#ifndef __LOC_HEAP__
-#define __LOC_HEAP__
-
-#include <stddef.h>
-#include <string.h>
-
-// abstract class to be implemented by client to provide a rankable class
-class LocRankable {
-public:
- virtual inline ~LocRankable() {}
-
- // method to rank objects of such type for sorting purposes.
- // The pointer of the input node would be stored in the heap.
- // >0 if ranks higher than the input;
- // ==0 if equally ranks with the input;
- // <0 if ranks lower than the input
- virtual int ranks(LocRankable& rankable) = 0;
-
- // convenient method to rank objects of such type for sorting purposes.
- inline bool outRanks(LocRankable& rankable) { return ranks(rankable) > 0; }
-};
-
-// opaque class to provide service implementation.
-class LocHeapNode;
-
-// a heap whose left and right children are not sorted. It is sorted only vertically,
-// i.e. parent always ranks higher than children, if they exist. Ranking algorithm is
-// implemented in Rankable. The reason that there is no sort between children is to
-// help beter balance the tree with lower cost. When a node is pushed to the tree,
-// it is guaranteed that the subtree that is smaller gets to have the new node.
-class LocHeap {
-protected:
- LocHeapNode* mTree;
-public:
- inline LocHeap() : mTree(NULL) {}
- ~LocHeap();
-
- // push keeps the tree sorted by rank, it also tries to balance the
- // tree by adding the new node to the smaller of the subtrees.
- // node is reference to an obj that is managed by client, that client
- // creates and destroyes. The destroy should happen after the
- // node is popped out from the heap.
- void push(LocRankable& node);
-
- // Peeks the node data on tree top, which has currently the highest ranking
- // There is no change the tree structure with this operation
- // Returns NULL if the tree is empty, otherwise pointer to the node data of
- // the tree top.
- LocRankable* peek();
-
- // pop keeps the tree sorted by rank, but it does not try to balance
- // the tree.
- // Return - pointer to the node popped out, or NULL if heap is already empty
- LocRankable* pop();
-
- // navigating through the tree and find the node that ranks the same
- // as the input data, then remove it from the tree. Rank is implemented
- // by rankable obj.
- // returns the pointer to the node removed; or NULL (if failed).
- LocRankable* remove(LocRankable& rankable);
-
-#ifdef __LOC_UNIT_TEST__
- bool checkTree();
- uint32_t getTreeSize();
-#endif
-};
-
-#endif //__LOC_HEAP__
diff --git a/utils/LocSharedLock.h b/utils/LocSharedLock.h
deleted file mode 100644
index 7fe6237..0000000
--- a/utils/LocSharedLock.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#ifndef __LOC_SHARED_LOCK__
-#define __LOC_SHARED_LOCK__
-
-#include <stddef.h>
-#include <cutils/atomic.h>
-#include <pthread.h>
-
-// This is a utility created for use cases such that there are more than
-// one client who need to share the same lock, but it is not predictable
-// which of these clients is to last to go away. This shared lock deletes
-// itself when the last client calls its drop() method. To add a cient,
-// this share lock's share() method has to be called, so that the obj
-// can maintain an accurate client count.
-class LocSharedLock {
- volatile int32_t mRef;
- pthread_mutex_t mMutex;
- inline ~LocSharedLock() { pthread_mutex_destroy(&mMutex); }
-public:
- // first client to create this LockSharedLock
- inline LocSharedLock() : mRef(1) { pthread_mutex_init(&mMutex, NULL); }
- // following client(s) are to *share()* this lock created by the first client
- inline LocSharedLock* share() { android_atomic_inc(&mRef); return this; }
- // whe a client no longer needs this shared lock, drop() shall be called.
- inline void drop() { if (1 == android_atomic_dec(&mRef)) delete this; }
- // locking the lock to enter critical section
- inline void lock() { pthread_mutex_lock(&mMutex); }
- // unlocking the lock to leave the critical section
- inline void unlock() { pthread_mutex_unlock(&mMutex); }
-};
-
-#endif //__LOC_SHARED_LOCK__
diff --git a/utils/LocThread.cpp b/utils/LocThread.cpp
deleted file mode 100644
index 19bf101..0000000
--- a/utils/LocThread.cpp
+++ /dev/null
@@ -1,264 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#include <LocThread.h>
-#include <string.h>
-#include <pthread.h>
-
-class LocThreadDelegate {
- LocRunnable* mRunnable;
- bool mJoinable;
- pthread_t mThandle;
- pthread_mutex_t mMutex;
- int mRefCount;
- ~LocThreadDelegate();
- LocThreadDelegate(LocThread::tCreate creator, const char* threadName,
- LocRunnable* runnable, bool joinable);
- void destroy();
-public:
- static LocThreadDelegate* create(LocThread::tCreate creator,
- const char* threadName, LocRunnable* runnable, bool joinable);
- void stop();
- // bye() is for the parent thread to go away. if joinable,
- // parent must stop the spawned thread, join, and then
- // destroy(); if detached, the parent can go straight
- // ahead to destroy()
- inline void bye() { mJoinable ? stop() : destroy(); }
- inline bool isRunning() { return (NULL != mRunnable); }
- static void* threadMain(void* arg);
-};
-
-// it is important to note that internal members must be
-// initialized to values as if pthread_create succeeds.
-// This is to avoid the race condition between the threads,
-// once the thread is created, some of these values will
-// be check in the spawned thread, and must set correctly
-// then and there.
-// However, upon pthread_create failure, the data members
-// must be set to indicate failure, e.g. mRunnable, and
-// threashold approprietly for destroy(), e.g. mRefCount.
-LocThreadDelegate::LocThreadDelegate(LocThread::tCreate creator,
- const char* threadName, LocRunnable* runnable, bool joinable) :
- mRunnable(runnable), mJoinable(joinable), mThandle(NULL),
- mMutex(PTHREAD_MUTEX_INITIALIZER), mRefCount(2) {
-
- // set up thread name, if nothing is passed in
- if (!threadName) {
- threadName = "LocThread";
- }
-
- // create the thread here, then if successful
- // and a name is given, we set the thread name
- if (creator) {
- mThandle = creator(threadName, threadMain, this);
- } else if (pthread_create(&mThandle, NULL, threadMain, this)) {
- // pthread_create() failed
- mThandle = NULL;
- }
-
- if (mThandle) {
- // set thread name
- char lname[16];
- int len = sizeof(lname) - 1;
- memcpy(lname, threadName, len);
- lname[len] = 0;
- // set the thread name here
- pthread_setname_np(mThandle, lname);
-
- // detach, if not joinable
- if (!joinable) {
- pthread_detach(mThandle);
- }
- } else {
- // must set these values upon failure
- mRunnable = NULL;
- mJoinable = false;
- mRefCount = 1;
- }
-}
-
-inline
-LocThreadDelegate::~LocThreadDelegate() {
- // at this point nothing should need done any more
-}
-
-// factory method so that we could return NULL upon failure
-LocThreadDelegate* LocThreadDelegate::create(LocThread::tCreate creator,
- const char* threadName, LocRunnable* runnable, bool joinable) {
- LocThreadDelegate* thread = NULL;
- if (runnable) {
- thread = new LocThreadDelegate(creator, threadName, runnable, joinable);
- if (thread && !thread->isRunning()) {
- thread->destroy();
- thread = NULL;
- }
- }
-
- return thread;
-}
-
-// The order is importang
-// NULLing mRunnalbe stops the while loop in threadMain()
-// join() if mJoinble must come before destroy() call, as
-// the obj must remain alive at this time so that mThandle
-// remains valud.
-void LocThreadDelegate::stop() {
- // mRunnable and mJoinable are reset on different triggers.
- // mRunnable may get nulled on the spawned thread's way out;
- // or here.
- // mJouinable (if ever been true) gets falsed when client
- // thread triggers stop, with either a stop()
- // call or the client releases thread obj handle.
- if (mRunnable) {
- mRunnable = NULL;
- }
- if (mJoinable) {
- mJoinable = false;
- pthread_join(mThandle, NULL);
- }
- // call destroy() to possibly delete the obj
- destroy();
-}
-
-// method for clients to call to release the obj
-// when it is a detached thread, the client thread
-// and the spawned thread can both try to destroy()
-// asynchronously. And we delete this obj when
-// mRefCount becomes 0.
-void LocThreadDelegate::destroy() {
- // else case shouldn't happen, unless there is a
- // leaking obj. But only our code here has such
- // obj, so if we test our code well, else case
- // will never happen
- if (mRefCount > 0) {
- // we need a flag on the stack
- bool callDelete = false;
-
- // critical section between threads
- pthread_mutex_lock(&mMutex);
- // last destroy() call
- callDelete = (1 == mRefCount--);
- pthread_mutex_unlock(&mMutex);
-
- // upon last destroy() call we delete this obj
- if (callDelete) {
- delete this;
- }
- }
-}
-
-void* LocThreadDelegate::threadMain(void* arg) {
- LocThreadDelegate* locThread = (LocThreadDelegate*)(arg);
-
- if (locThread) {
- LocRunnable* runnable = locThread->mRunnable;
-
- if (runnable) {
- if (locThread->isRunning()) {
- runnable->prerun();
- }
-
- while (locThread->isRunning() && runnable->run());
-
- if (locThread->isRunning()) {
- runnable->postrun();
- }
-
- // at this time, locThread->mRunnable may or may not be NULL
- // NULL it just to be safe and clean, as we want the field
- // in the released memory slot to be NULL.
- locThread->mRunnable = NULL;
- delete runnable;
- }
- locThread->destroy();
- }
-
- return NULL;
-}
-
-LocThread::~LocThread() {
- if (mThread) {
- mThread->bye();
- mThread = NULL;
- }
-}
-
-bool LocThread::start(tCreate creator, const char* threadName, LocRunnable* runnable, bool joinable) {
- bool success = false;
- if (!mThread) {
- mThread = LocThreadDelegate::create(creator, threadName, runnable, joinable);
- // true only if thread is created successfully
- success = (NULL != mThread);
- }
- return success;
-}
-
-void LocThread::stop() {
- if (mThread) {
- mThread->stop();
- mThread = NULL;
- }
-}
-
-#ifdef __LOC_DEBUG__
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-class LocRunnableTest1 : public LocRunnable {
- int mID;
-public:
- LocRunnableTest1(int id) : LocRunnable(), mID(id) {}
- virtual bool run() {
- printf("LocRunnableTest1: %d\n", mID++);
- sleep(1);
- return true;
- }
-};
-
-// on linux command line:
-// compile: g++ -D__LOC_HOST_DEBUG__ -D__LOC_DEBUG__ -g -std=c++0x -I. -I../../../../vendor/qcom/proprietary/gps-internal/unit-tests/fakes_for_host -I../../../../system/core/include -lpthread LocThread.cpp
-// test detached thread: valgrind ./a.out 0
-// test joinable thread: valgrind ./a.out 1
-int main(int argc, char** argv) {
- LocRunnableTest1 test(10);
-
- LocThread thread;
- thread.start("LocThreadTest", test, atoi(argv[1]));
-
- sleep(10);
-
- thread.stop();
-
- sleep(5);
-
- return 0;
-}
-
-#endif
diff --git a/utils/LocThread.h b/utils/LocThread.h
deleted file mode 100644
index 2a65d8f..0000000
--- a/utils/LocThread.h
+++ /dev/null
@@ -1,92 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#ifndef __LOC_THREAD__
-#define __LOC_THREAD__
-
-#include <stddef.h>
-#include <pthread.h>
-
-// abstract class to be implemented by client to provide a runnable class
-// which gets scheduled by LocThread
-class LocRunnable {
-public:
- inline LocRunnable() {}
- inline virtual ~LocRunnable() {}
-
- // The method to be implemented by thread clients
- // and be scheduled by LocThread
- // This method will be repeated called until it returns false; or
- // until thread is stopped.
- virtual bool run() = 0;
-
- // The method to be run before thread loop (conditionally repeatedly)
- // calls run()
- inline virtual void prerun() {}
-
- // The method to be run after thread loop (conditionally repeatedly)
- // calls run()
- inline virtual void postrun() {}
-};
-
-// opaque class to provide service implementation.
-class LocThreadDelegate;
-
-// A utility class to create a thread and run LocRunnable
-// caller passes in.
-class LocThread {
- LocThreadDelegate* mThread;
-public:
- inline LocThread() : mThread(NULL) {}
- virtual ~LocThread();
-
- typedef pthread_t (*tCreate)(const char* name, void* (*start)(void*), void* arg);
- // client starts thread with a runnable, which implements
- // the logics to fun in the created thread context.
- // The thread could be either joinable or detached.
- // runnable is an obj managed by client. Client creates and
- // frees it (but must be after stop() is called, or
- // this LocThread obj is deleted).
- // The obj will be deleted by LocThread if start()
- // returns true. Else it is client's responsibility
- // to delete the object
- // Returns 0 if success; false if failure.
- bool start(tCreate creator, const char* threadName, LocRunnable* runnable, bool joinable = true);
- inline bool start(const char* threadName, LocRunnable* runnable, bool joinable = true) {
- return start(NULL, threadName, runnable, joinable);
- }
-
- // NOTE: if this is a joinable thread, this stop may block
- // for a while until the thread is joined.
- void stop();
-
- // thread status check
- inline bool isRunning() { return NULL != mThread; }
-};
-
-#endif //__LOC_THREAD__
diff --git a/utils/LocTimer.cpp b/utils/LocTimer.cpp
deleted file mode 100644
index 70904b2..0000000
--- a/utils/LocTimer.cpp
+++ /dev/null
@@ -1,737 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-#include <errno.h>
-#include <loc_timer.h>
-#include <sys/timerfd.h>
-#include <sys/epoll.h>
-#include <LocTimer.h>
-#include <LocHeap.h>
-#include <LocThread.h>
-#include <LocSharedLock.h>
-#include <MsgTask.h>
-
-#ifdef __HOST_UNIT_TEST__
-#define EPOLLWAKEUP 0
-#define CLOCK_BOOTTIME CLOCK_MONOTONIC
-#define CLOCK_BOOTTIME_ALARM CLOCK_MONOTONIC
-#endif
-
-/*
-There are implementations of 5 classes in this file:
-LocTimer, LocTimerDelegate, LocTimerContainer, LocTimerPollTask, LocTimerWrapper
-
-LocTimer - client front end, interface for client to start / stop timers, also
- to provide a callback.
-LocTimerDelegate - an internal timer entity, which also is a LocRankable obj.
- Its life cycle is different than that of LocTimer. It gets
- created when LocTimer::start() is called, and gets deleted
- when it expires or clients calls the hosting LocTimer obj's
- stop() method. When a LocTimerDelegate obj is ticking, it
- stays in the corresponding LocTimerContainer. When expired
- or stopped, the obj is removed from the container. Since it
- is also a LocRankable obj, and LocTimerContainer also is a
- heap, its ranks() implementation decides where it is placed
- in the heap.
-LocTimerContainer - core of the timer service. It is a container (derived from
- LocHeap) for LocTimerDelegate (implements LocRankable) objs.
- There are 2 of such containers, one for sw timers (or Linux
- timers) one for hw timers (or Linux alarms). It adds one of
- each (those that expire the soonest) to kernel via services
- provided by LocTimerPollTask. All the heap management on the
- LocTimerDelegate objs are done in the MsgTask context, such
- that synchronization is ensured.
-LocTimerPollTask - is a class that wraps timerfd and epoll POXIS APIs. It also
- both implements LocRunnalbe with epoll_wait() in the run()
- method. It is also a LocThread client, so as to loop the run
- method.
-LocTimerWrapper - a LocTimer client itself, to implement the existing C API with
- APIs, loc_timer_start() and loc_timer_stop().
-
-*/
-
-class LocTimerPollTask;
-
-// This is a multi-functaional class that:
-// * extends the LocHeap class for the detection of head update upon add / remove
-// events. When that happens, soonest time out changes, so timerfd needs update.
-// * contains the timers, and add / remove them into the heap
-// * provides and maps 2 of such containers, one for timers (or mSwTimers), one
-// for alarms (or mHwTimers);
-// * provides a polling thread;
-// * provides a MsgTask thread for synchronized add / remove / timer client callback.
-class LocTimerContainer : public LocHeap {
- // mutex to synchronize getters of static members
- static pthread_mutex_t mMutex;
- // Container of timers
- static LocTimerContainer* mSwTimers;
- // Container of alarms
- static LocTimerContainer* mHwTimers;
- // Msg task to provider msg Q, sender and reader.
- static MsgTask* mMsgTask;
- // Poll task to provide epoll call and threading to poll.
- static LocTimerPollTask* mPollTask;
- // timer / alarm fd
- int mDevFd;
- // ctor
- LocTimerContainer(bool wakeOnExpire);
- // dtor
- ~LocTimerContainer();
- static MsgTask* getMsgTaskLocked();
- static LocTimerPollTask* getPollTaskLocked();
- // extend LocHeap and pop if the top outRanks input
- LocTimerDelegate* popIfOutRanks(LocTimerDelegate& timer);
- // update the timer POSIX calls with updated soonest timer spec
- void updateSoonestTime(LocTimerDelegate* priorTop);
-
-public:
- // factory method to control the creation of mSwTimers / mHwTimers
- static LocTimerContainer* get(bool wakeOnExpire);
-
- LocTimerDelegate* getSoonestTimer();
- int getTimerFd();
- // add a timer / alarm obj into the container
- void add(LocTimerDelegate& timer);
- // remove a timer / alarm obj from the container
- void remove(LocTimerDelegate& timer);
- // handling of timer / alarm expiration
- void expire();
-};
-
-// This class implements the polling thread that epolls imer / alarm fds.
-// The LocRunnable::run() contains the actual polling. The other methods
-// will be run in the caller's thread context to add / remove timer / alarm
-// fds the kernel, while the polling is blocked on epoll_wait() call.
-// Since the design is that we have maximally 2 polls, one for all the
-// timers; one for all the alarms, we will poll at most on 2 fds. But it
-// is possile that all we have are only timers or alarms at one time, so we
-// allow dynamically add / remove fds we poll on. The design decision of
-// having 1 fd per container of timer / alarm is such that, we may not need
-// to make a system call each time a timer / alarm is added / removed, unless
-// that changes the "soonest" time out of that of all the timers / alarms.
-class LocTimerPollTask : public LocRunnable {
- // the epoll fd
- const int mFd;
- // the thread that calls run() method
- LocThread* mThread;
- friend class LocThreadDelegate;
- // dtor
- ~LocTimerPollTask();
-public:
- // ctor
- LocTimerPollTask();
- // this obj will be deleted once thread is deleted
- void destroy();
- // add a container of timers. Each contain has a unique device fd, i.e.
- // either timer or alarm fd, and a heap of timers / alarms. It is expected
- // that container would have written to the device fd with the soonest
- // time out value in the heap at the time of calling this method. So all
- // this method does is to add the fd of the input container to the poll
- // and also add the pointer of the container to the event data ptr, such
- // when poll_wait wakes up on events, we know who is the owner of the fd.
- void addPoll(LocTimerContainer& timerContainer);
- // remove a fd that is assciated with a container. The expectation is that
- // the atual timer would have been removed from the container.
- void removePoll(LocTimerContainer& timerContainer);
- // The polling thread context will call this method. This is where
- // epoll_wait() is blocking and waiting for events..
- virtual bool run();
-};
-
-// Internal class of timer obj. It gets born when client calls LocTimer::start();
-// and gets deleted when client calls LocTimer::stop() or when the it expire()'s.
-// This class implements LocRankable::ranks() so that when an obj is added into
-// the container (of LocHeap), it gets placed in sorted order.
-class LocTimerDelegate : public LocRankable {
- friend class LocTimerContainer;
- friend class LocTimer;
- LocTimer* mClient;
- LocSharedLock* mLock;
- struct timespec mFutureTime;
- LocTimerContainer* mContainer;
- // not a complete obj, just ctor for LocRankable comparisons
- inline LocTimerDelegate(struct timespec& delay)
- : mClient(NULL), mLock(NULL), mFutureTime(delay), mContainer(NULL) {}
- inline ~LocTimerDelegate() { if (mLock) { mLock->drop(); mLock = NULL; } }
-public:
- LocTimerDelegate(LocTimer& client, struct timespec& futureTime, bool wakeOnExpire);
- void destroyLocked();
- // LocRankable virtual method
- virtual int ranks(LocRankable& rankable);
- void expire();
- inline struct timespec getFutureTime() { return mFutureTime; }
-};
-
-/***************************LocTimerContainer methods***************************/
-
-// Most of these static recources are created on demand. They however are never
-// destoyed. The theory is that there are processes that link to this util lib
-// but never use timer, then these resources would never need to be created.
-// For those processes that do use timer, it will likely also need to every
-// once in a while. It might be cheaper keeping them around.
-pthread_mutex_t LocTimerContainer::mMutex = PTHREAD_MUTEX_INITIALIZER;
-LocTimerContainer* LocTimerContainer::mSwTimers = NULL;
-LocTimerContainer* LocTimerContainer::mHwTimers = NULL;
-MsgTask* LocTimerContainer::mMsgTask = NULL;
-LocTimerPollTask* LocTimerContainer::mPollTask = NULL;
-
-// ctor - initialize timer heaps
-// A container for swTimer (timer) is created, when wakeOnExpire is true; or
-// HwTimer (alarm), when wakeOnExpire is false.
-LocTimerContainer::LocTimerContainer(bool wakeOnExpire) :
- mDevFd(timerfd_create(wakeOnExpire ? CLOCK_BOOTTIME_ALARM : CLOCK_BOOTTIME, 0)) {
-
- if ((-1 == mDevFd) && (errno == EINVAL)) {
- LOC_LOGW("%s: timerfd_create failure, fallback to CLOCK_MONOTONIC - %s",
- __FUNCTION__, strerror(errno));
- mDevFd = timerfd_create(CLOCK_MONOTONIC, 0);
- }
-
- if (-1 != mDevFd) {
- // ensure we have the necessary resources created
- LocTimerContainer::getPollTaskLocked();
- LocTimerContainer::getMsgTaskLocked();
- } else {
- LOC_LOGE("%s: timerfd_create failure - %s", __FUNCTION__, strerror(errno));
- }
-}
-
-// dtor
-// we do not ever destroy the static resources.
-inline
-LocTimerContainer::~LocTimerContainer() {
- close(mDevFd);
-}
-
-LocTimerContainer* LocTimerContainer::get(bool wakeOnExpire) {
- // get the reference of either mHwTimer or mSwTimers per wakeOnExpire
- LocTimerContainer*& container = wakeOnExpire ? mHwTimers : mSwTimers;
- // it is cheap to check pointer first than locking mutext unconditionally
- if (!container) {
- pthread_mutex_lock(&mMutex);
- // let's check one more time to be safe
- if (!container) {
- container = new LocTimerContainer(wakeOnExpire);
- // timerfd_create failure
- if (-1 == container->getTimerFd()) {
- delete container;
- container = NULL;
- }
- }
- pthread_mutex_unlock(&mMutex);
- }
- return container;
-}
-
-MsgTask* LocTimerContainer::getMsgTaskLocked() {
- // it is cheap to check pointer first than locking mutext unconditionally
- if (!mMsgTask) {
- mMsgTask = new MsgTask("LocTimerMsgTask", false);
- }
- return mMsgTask;
-}
-
-LocTimerPollTask* LocTimerContainer::getPollTaskLocked() {
- // it is cheap to check pointer first than locking mutext unconditionally
- if (!mPollTask) {
- mPollTask = new LocTimerPollTask();
- }
- return mPollTask;
-}
-
-inline
-LocTimerDelegate* LocTimerContainer::getSoonestTimer() {
- return (LocTimerDelegate*)(peek());
-}
-
-inline
-int LocTimerContainer::getTimerFd() {
- return mDevFd;
-}
-
-void LocTimerContainer::updateSoonestTime(LocTimerDelegate* priorTop) {
- LocTimerDelegate* curTop = getSoonestTimer();
-
- // check if top has changed
- if (curTop != priorTop) {
- struct itimerspec delay = {0};
- bool toSetTime = false;
- // if tree is empty now, we remove poll and disarm timer
- if (!curTop) {
- mPollTask->removePoll(*this);
- // setting the values to disarm timer
- delay.it_value.tv_sec = 0;
- delay.it_value.tv_nsec = 0;
- toSetTime = true;
- } else if (!priorTop || curTop->outRanks(*priorTop)) {
- // do this first to avoid race condition, in case settime is called
- // with too small an interval
- mPollTask->addPoll(*this);
- delay.it_value = curTop->getFutureTime();
- toSetTime = true;
- }
- if (toSetTime) {
- timerfd_settime(getTimerFd(), TFD_TIMER_ABSTIME, &delay, NULL);
- }
- }
-}
-
-// all the heap management is done in the MsgTask context.
-inline
-void LocTimerContainer::add(LocTimerDelegate& timer) {
- struct MsgTimerPush : public LocMsg {
- LocTimerContainer* mTimerContainer;
- LocHeapNode* mTree;
- LocTimerDelegate* mTimer;
- inline MsgTimerPush(LocTimerContainer& container, LocTimerDelegate& timer) :
- LocMsg(), mTimerContainer(&container), mTimer(&timer) {}
- inline virtual void proc() const {
- LocTimerDelegate* priorTop = mTimerContainer->getSoonestTimer();
- mTimerContainer->push((LocRankable&)(*mTimer));
- mTimerContainer->updateSoonestTime(priorTop);
- }
- };
-
- mMsgTask->sendMsg(new MsgTimerPush(*this, timer));
-}
-
-// all the heap management is done in the MsgTask context.
-void LocTimerContainer::remove(LocTimerDelegate& timer) {
- struct MsgTimerRemove : public LocMsg {
- LocTimerContainer* mTimerContainer;
- LocTimerDelegate* mTimer;
- inline MsgTimerRemove(LocTimerContainer& container, LocTimerDelegate& timer) :
- LocMsg(), mTimerContainer(&container), mTimer(&timer) {}
- inline virtual void proc() const {
- LocTimerDelegate* priorTop = mTimerContainer->getSoonestTimer();
-
- // update soonest timer only if mTimer is actually removed from
- // mTimerContainer AND mTimer is not priorTop.
- if (priorTop == ((LocHeap*)mTimerContainer)->remove((LocRankable&)*mTimer)) {
- // if passing in NULL, we tell updateSoonestTime to update
- // kernel with the current top timer interval.
- mTimerContainer->updateSoonestTime(NULL);
- }
- // all timers are deleted here, and only here.
- delete mTimer;
- }
- };
-
- mMsgTask->sendMsg(new MsgTimerRemove(*this, timer));
-}
-
-// all the heap management is done in the MsgTask context.
-// Upon expire, we check and continuously pop the heap until
-// the top node's timeout is in the future.
-void LocTimerContainer::expire() {
- struct MsgTimerExpire : public LocMsg {
- LocTimerContainer* mTimerContainer;
- inline MsgTimerExpire(LocTimerContainer& container) :
- LocMsg(), mTimerContainer(&container) {}
- inline virtual void proc() const {
- struct timespec now;
- // get time spec of now
- clock_gettime(CLOCK_BOOTTIME, &now);
- LocTimerDelegate timerOfNow(now);
- // pop everything in the heap that outRanks now, i.e. has time older than now
- // and then call expire() on that timer.
- for (LocTimerDelegate* timer = (LocTimerDelegate*)mTimerContainer->pop();
- NULL != timer;
- timer = mTimerContainer->popIfOutRanks(timerOfNow)) {
- // the timer delegate obj will be deleted before the return of this call
- timer->expire();
- }
- mTimerContainer->updateSoonestTime(NULL);
- }
- };
-
- struct itimerspec delay = {0};
- timerfd_settime(getTimerFd(), TFD_TIMER_ABSTIME, &delay, NULL);
- mPollTask->removePoll(*this);
- mMsgTask->sendMsg(new MsgTimerExpire(*this));
-}
-
-LocTimerDelegate* LocTimerContainer::popIfOutRanks(LocTimerDelegate& timer) {
- LocTimerDelegate* poppedNode = NULL;
- if (mTree && !timer.outRanks(*peek())) {
- poppedNode = (LocTimerDelegate*)(pop());
- }
-
- return poppedNode;
-}
-
-
-/***************************LocTimerPollTask methods***************************/
-
-inline
-LocTimerPollTask::LocTimerPollTask()
- : mFd(epoll_create(2)), mThread(new LocThread()) {
- // before a next call returens, a thread will be created. The run() method
- // could already be running in parallel. Also, since each of the objs
- // creates a thread, the container will make sure that there will be only
- // one of such obj for our timer implementation.
- if (!mThread->start("LocTimerPollTask", this)) {
- delete mThread;
- mThread = NULL;
- }
-}
-
-inline
-LocTimerPollTask::~LocTimerPollTask() {
- // when fs is closed, epoll_wait() should fail run() should return false
- // and the spawned thread should exit.
- close(mFd);
-}
-
-void LocTimerPollTask::destroy() {
- if (mThread) {
- LocThread* thread = mThread;
- mThread = NULL;
- delete thread;
- } else {
- delete this;
- }
-}
-
-void LocTimerPollTask::addPoll(LocTimerContainer& timerContainer) {
- struct epoll_event ev;
- memset(&ev, 0, sizeof(ev));
-
- ev.events = EPOLLIN | EPOLLWAKEUP;
- ev.data.fd = timerContainer.getTimerFd();
- // it is important that we set this context pointer with the input
- // timer container this is how we know which container should handle
- // which expiration.
- ev.data.ptr = &timerContainer;
-
- epoll_ctl(mFd, EPOLL_CTL_ADD, timerContainer.getTimerFd(), &ev);
-}
-
-inline
-void LocTimerPollTask::removePoll(LocTimerContainer& timerContainer) {
- epoll_ctl(mFd, EPOLL_CTL_DEL, timerContainer.getTimerFd(), NULL);
-}
-
-// The polling thread context will call this method. If run() method needs to
-// be repetitvely called, it must return true from the previous call.
-bool LocTimerPollTask::run() {
- struct epoll_event ev[2];
-
- // we have max 2 descriptors to poll from
- int fds = epoll_wait(mFd, ev, 2, -1);
-
- // we pretty much want to continually poll until the fd is closed
- bool rerun = (fds > 0) || (errno == EINTR);
-
- if (fds > 0) {
- // we may have 2 events
- for (int i = 0; i < fds; i++) {
- // each fd has a context pointer associated with the right timer container
- LocTimerContainer* container = (LocTimerContainer*)(ev[i].data.ptr);
- if (container) {
- container->expire();
- } else {
- epoll_ctl(mFd, EPOLL_CTL_DEL, ev[i].data.fd, NULL);
- }
- }
- }
-
- // if rerun is true, we are requesting to be scheduled again
- return rerun;
-}
-
-/***************************LocTimerDelegate methods***************************/
-
-inline
-LocTimerDelegate::LocTimerDelegate(LocTimer& client, struct timespec& futureTime, bool wakeOnExpire)
- : mClient(&client),
- mLock(mClient->mLock->share()),
- mFutureTime(futureTime),
- mContainer(LocTimerContainer::get(wakeOnExpire)) {
- // adding the timer into the container
- mContainer->add(*this);
-}
-
-inline
-void LocTimerDelegate::destroyLocked() {
- // client handle will likely be deleted soon after this
- // method returns. Nulling this handle so that expire()
- // won't call the callback on the dead handle any more.
- mClient = NULL;
-
- if (mContainer) {
- LocTimerContainer* container = mContainer;
- mContainer = NULL;
- if (container) {
- container->remove(*this);
- }
- } // else we do not do anything. No such *this* can be
- // created and reached here with mContainer ever been
- // a non NULL. So *this* must have reached the if clause
- // once, and we want it reach there only once.
-}
-
-int LocTimerDelegate::ranks(LocRankable& rankable) {
- int rank = -1;
- LocTimerDelegate* timer = (LocTimerDelegate*)(&rankable);
- if (timer) {
- // larger time ranks lower!!!
- // IOW, if input obj has bigger tv_sec, this obj outRanks higher
- rank = timer->mFutureTime.tv_sec - mFutureTime.tv_sec;
- }
- return rank;
-}
-
-inline
-void LocTimerDelegate::expire() {
- // keeping a copy of client pointer to be safe
- // when timeOutCallback() is called at the end of this
- // method, *this* obj may be already deleted.
- LocTimer* client = mClient;
- // force a stop, which will lead to delete of this obj
- if (client && client->stop()) {
- // calling client callback with a pointer save on the stack
- // only if stop() returns true, i.e. it hasn't been stopped
- // already.
- client->timeOutCallback();
- }
-}
-
-
-/***************************LocTimer methods***************************/
-LocTimer::LocTimer() : mTimer(NULL), mLock(new LocSharedLock()) {
-}
-
-LocTimer::~LocTimer() {
- stop();
- if (mLock) {
- mLock->drop();
- mLock = NULL;
- }
-}
-
-bool LocTimer::start(unsigned int timeOutInMs, bool wakeOnExpire) {
- bool success = false;
- mLock->lock();
- if (!mTimer) {
- struct timespec futureTime;
- clock_gettime(CLOCK_BOOTTIME, &futureTime);
- futureTime.tv_sec += timeOutInMs / 1000;
- futureTime.tv_nsec += (timeOutInMs % 1000) * 1000000;
- if (futureTime.tv_nsec >= 1000000000) {
- futureTime.tv_sec += futureTime.tv_nsec / 1000000000;
- futureTime.tv_nsec %= 1000000000;
- }
- mTimer = new LocTimerDelegate(*this, futureTime, wakeOnExpire);
- // if mTimer is non 0, success should be 0; or vice versa
- success = (NULL != mTimer);
- }
- mLock->unlock();
- return success;
-}
-
-bool LocTimer::stop() {
- bool success = false;
- mLock->lock();
- if (mTimer) {
- LocTimerDelegate* timer = mTimer;
- mTimer = NULL;
- if (timer) {
- timer->destroyLocked();
- success = true;
- }
- }
- mLock->unlock();
- return success;
-}
-
-/***************************LocTimerWrapper methods***************************/
-//////////////////////////////////////////////////////////////////////////
-// This section below wraps for the C style APIs
-//////////////////////////////////////////////////////////////////////////
-class LocTimerWrapper : public LocTimer {
- loc_timer_callback mCb;
- void* mCallerData;
- LocTimerWrapper* mMe;
- static pthread_mutex_t mMutex;
- inline ~LocTimerWrapper() { mCb = NULL; mMe = NULL; }
-public:
- inline LocTimerWrapper(loc_timer_callback cb, void* callerData) :
- mCb(cb), mCallerData(callerData), mMe(this) {
- }
- void destroy() {
- pthread_mutex_lock(&mMutex);
- if (NULL != mCb && this == mMe) {
- delete this;
- }
- pthread_mutex_unlock(&mMutex);
- }
- virtual void timeOutCallback() {
- loc_timer_callback cb = mCb;
- void* callerData = mCallerData;
- if (cb) {
- cb(callerData, 0);
- }
- destroy();
- }
-};
-
-pthread_mutex_t LocTimerWrapper::mMutex = PTHREAD_MUTEX_INITIALIZER;
-
-void* loc_timer_start(uint64_t msec, loc_timer_callback cb_func,
- void *caller_data, bool wake_on_expire)
-{
- LocTimerWrapper* locTimerWrapper = NULL;
-
- if (cb_func) {
- locTimerWrapper = new LocTimerWrapper(cb_func, caller_data);
-
- if (locTimerWrapper) {
- locTimerWrapper->start(msec, wake_on_expire);
- }
- }
-
- return locTimerWrapper;
-}
-
-void loc_timer_stop(void*& handle)
-{
- if (handle) {
- LocTimerWrapper* locTimerWrapper = (LocTimerWrapper*)(handle);
- locTimerWrapper->destroy();
- handle = NULL;
- }
-}
-
-//////////////////////////////////////////////////////////////////////////
-// This section above wraps for the C style APIs
-//////////////////////////////////////////////////////////////////////////
-
-#ifdef __LOC_DEBUG__
-
-double getDeltaSeconds(struct timespec from, struct timespec to) {
- return (double)to.tv_sec + (double)to.tv_nsec / 1000000000
- - from.tv_sec - (double)from.tv_nsec / 1000000000;
-}
-
-struct timespec getNow() {
- struct timespec now;
- clock_gettime(CLOCK_BOOTTIME, &now);
- return now;
-}
-
-class LocTimerTest : public LocTimer, public LocRankable {
- int mTimeOut;
- const struct timespec mTimeOfBirth;
- inline struct timespec getTimerWrapper(int timeout) {
- struct timespec now;
- clock_gettime(CLOCK_BOOTTIME, &now);
- now.tv_sec += timeout;
- return now;
- }
-public:
- inline LocTimerTest(int timeout) : LocTimer(), LocRankable(),
- mTimeOut(timeout), mTimeOfBirth(getTimerWrapper(0)) {}
- inline virtual int ranks(LocRankable& rankable) {
- LocTimerTest* timer = dynamic_cast<LocTimerTest*>(&rankable);
- return timer->mTimeOut - mTimeOut;
- }
- inline virtual void timeOutCallback() {
- printf("timeOutCallback() - ");
- deviation();
- }
- double deviation() {
- struct timespec now = getTimerWrapper(0);
- double delta = getDeltaSeconds(mTimeOfBirth, now);
- printf("%lf: %lf\n", delta, delta * 100 / mTimeOut);
- return delta / mTimeOut;
- }
-};
-
-// For Linux command line testing:
-// compilation:
-// g++ -D__LOC_HOST_DEBUG__ -D__LOC_DEBUG__ -g -I. -I../../../../system/core/include -o LocHeap.o LocHeap.cpp
-// g++ -D__LOC_HOST_DEBUG__ -D__LOC_DEBUG__ -g -std=c++0x -I. -I../../../../system/core/include -lpthread -o LocThread.o LocThread.cpp
-// g++ -D__LOC_HOST_DEBUG__ -D__LOC_DEBUG__ -g -I. -I../../../../system/core/include -o LocTimer.o LocTimer.cpp
-int main(int argc, char** argv) {
- struct timespec timeOfStart=getNow();
- srand(time(NULL));
- int tries = atoi(argv[1]);
- int checks = tries >> 3;
- LocTimerTest** timerArray = new LocTimerTest*[tries];
- memset(timerArray, NULL, tries);
-
- for (int i = 0; i < tries; i++) {
- int r = rand() % tries;
- LocTimerTest* timer = new LocTimerTest(r);
- if (timerArray[r]) {
- if (!timer->stop()) {
- printf("%lf:\n", getDeltaSeconds(timeOfStart, getNow()));
- printf("ERRER: %dth timer, id %d, not running when it should be\n", i, r);
- exit(0);
- } else {
- printf("stop() - %d\n", r);
- delete timer;
- timerArray[r] = NULL;
- }
- } else {
- if (!timer->start(r, false)) {
- printf("%lf:\n", getDeltaSeconds(timeOfStart, getNow()));
- printf("ERRER: %dth timer, id %d, running when it should not be\n", i, r);
- exit(0);
- } else {
- printf("stop() - %d\n", r);
- timerArray[r] = timer;
- }
- }
- }
-
- for (int i = 0; i < tries; i++) {
- if (timerArray[i]) {
- if (!timerArray[i]->stop()) {
- printf("%lf:\n", getDeltaSeconds(timeOfStart, getNow()));
- printf("ERRER: %dth timer, not running when it should be\n", i);
- exit(0);
- } else {
- printf("stop() - %d\n", i);
- delete timerArray[i];
- timerArray[i] = NULL;
- }
- }
- }
-
- delete[] timerArray;
-
- return 0;
-}
-
-#endif
diff --git a/utils/LocTimer.h b/utils/LocTimer.h
deleted file mode 100644
index c146852..0000000
--- a/utils/LocTimer.h
+++ /dev/null
@@ -1,74 +0,0 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef __LOC_TIMER_CPP_H__
-#define __LOC_TIMER_CPP_H__
-
-#include <stddef.h>
-#include <log_util.h>
-
-// opaque class to provide service implementation.
-class LocTimerDelegate;
-class LocSharedLock;
-
-// LocTimer client must extend this class and implementthe callback.
-// start() / stop() methods are to arm / disarm timer.
-class LocTimer
-{
- LocTimerDelegate* mTimer;
- LocSharedLock* mLock;
- // don't really want mLock to be manipulated by clients, yet LocTimer
- // has to have a reference to the lock so that the delete of LocTimer
- // and LocTimerDelegate can work together on their share resources.
- friend class LocTimerDelegate;
-
-public:
- LocTimer();
- virtual ~LocTimer();
-
- // timeOutInMs: timeout delay in ms
- // wakeOnExpire: true if to wake up CPU (if sleeping) upon timer
- // expiration and notify the client.
- // false if to wait until next time CPU wakes up (if
- // sleeping) and then notify the client.
- // return: true on success;
- // false on failure, e.g. timer is already running.
- bool start(uint32_t timeOutInMs, bool wakeOnExpire);
-
- // return: true on success;
- // false on failure, e.g. timer is not running.
- bool stop();
-
- // LocTimer client Should implement this method.
- // This method is used for timeout calling back to client. This method
- // should be short enough (eg: send a message to your own thread).
- virtual void timeOutCallback() = 0;
-};
-
-#endif //__LOC_DELAY_H__
diff --git a/utils/MsgTask.cpp b/utils/MsgTask.cpp
deleted file mode 100644
index f33d602..0000000
--- a/utils/MsgTask.cpp
+++ /dev/null
@@ -1,100 +0,0 @@
-/* Copyright (c) 2011-2013,2015 The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#define LOG_NDDEBUG 0
-#define LOG_TAG "LocSvc_MsgTask"
-
-#include <unistd.h>
-#include <MsgTask.h>
-#include <msg_q.h>
-#include <loc_log.h>
-
-static void LocMsgDestroy(void* msg) {
- delete (LocMsg*)msg;
-}
-
-MsgTask::MsgTask(LocThread::tCreate tCreator,
- const char* threadName, bool joinable) :
- mQ(msg_q_init2()), mThread(new LocThread()) {
- if (!mThread->start(tCreator, threadName, this, joinable)) {
- delete mThread;
- mThread = NULL;
- }
-}
-
-MsgTask::MsgTask(const char* threadName, bool joinable) :
- mQ(msg_q_init2()), mThread(new LocThread()) {
- if (!mThread->start(threadName, this, joinable)) {
- delete mThread;
- mThread = NULL;
- }
-}
-
-MsgTask::~MsgTask() {
- msg_q_flush((void*)mQ);
- msg_q_destroy((void**)&mQ);
-}
-
-void MsgTask::destroy() {
- msg_q_unblock((void*)mQ);
- if (mThread) {
- LocThread* thread = mThread;
- mThread = NULL;
- delete thread;
- } else {
- delete this;
- }
-}
-
-void MsgTask::sendMsg(const LocMsg* msg) const {
- msg_q_snd((void*)mQ, (void*)msg, LocMsgDestroy);
-}
-
-void MsgTask::prerun() {
- // make sure we do not run in background scheduling group
- set_sched_policy(gettid(), SP_FOREGROUND);
-}
-
-bool MsgTask::run() {
- LOC_LOGV("MsgTask::loop() listening ...\n");
- LocMsg* msg;
- msq_q_err_type result = msg_q_rcv((void*)mQ, (void **)&msg);
- if (eMSG_Q_SUCCESS != result) {
- LOC_LOGE("%s:%d] fail receiving msg: %s\n", __func__, __LINE__,
- loc_get_msg_q_status(result));
- return false;
- }
-
- msg->log();
- // there is where each individual msg handling is invoked
- msg->proc();
-
- delete msg;
-
- return true;
-}
diff --git a/utils/MsgTask.h b/utils/MsgTask.h
deleted file mode 100644
index 9eb1f56..0000000
--- a/utils/MsgTask.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/* Copyright (c) 2011-2013,2015 The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#ifndef __MSG_TASK__
-#define __MSG_TASK__
-
-#include <LocThread.h>
-
-struct LocMsg {
- inline LocMsg() {}
- inline virtual ~LocMsg() {}
- virtual void proc() const = 0;
- inline virtual void log() const {}
-};
-
-class MsgTask : public LocRunnable {
- const void* mQ;
- LocThread* mThread;
- friend class LocThreadDelegate;
-protected:
- virtual ~MsgTask();
-public:
- MsgTask(LocThread::tCreate tCreator, const char* threadName = NULL, bool joinable = true);
- MsgTask(const char* threadName = NULL, bool joinable = true);
- // this obj will be deleted once thread is deleted
- void destroy();
- void sendMsg(const LocMsg* msg) const;
- // Overrides of LocRunnable methods
- // This method will be repeated called until it returns false; or
- // until thread is stopped.
- virtual bool run();
-
- // The method to be run before thread loop (conditionally repeatedly)
- // calls run()
- virtual void prerun();
-
- // The method to be run after thread loop (conditionally repeatedly)
- // calls run()
- inline virtual void postrun() {}
-};
-
-#endif //__MSG_TASK__
diff --git a/utils/linked_list.c b/utils/linked_list.c
index acec5b1..1109acd 100644
--- a/utils/linked_list.c
+++ b/utils/linked_list.c
@@ -108,7 +108,7 @@ linked_list_err_type linked_list_destroy(void** list_data)
===========================================================================*/
linked_list_err_type linked_list_add(void* list_data, void *data_obj, void (*dealloc)(void*))
{
- LOC_LOGV("%s: Adding to list data_obj = 0x%08X\n", __FUNCTION__, data_obj);
+ LOC_LOGD("%s: Adding to list data_obj = 0x%08X\n", __FUNCTION__, data_obj);
if( list_data == NULL )
{
LOC_LOGE("%s: Invalid list parameter!\n", __FUNCTION__);
@@ -160,7 +160,7 @@ linked_list_err_type linked_list_add(void* list_data, void *data_obj, void (*dea
===========================================================================*/
linked_list_err_type linked_list_remove(void* list_data, void **data_obj)
{
- LOC_LOGV("%s: Removing from list\n", __FUNCTION__);
+ LOC_LOGD("%s: Removing from list\n", __FUNCTION__);
if( list_data == NULL )
{
LOC_LOGE("%s: Invalid list parameter!\n", __FUNCTION__);
@@ -267,7 +267,7 @@ linked_list_err_type linked_list_search(void* list_data, void **data_p,
bool (*equal)(void* data_0, void* data),
void* data_0, bool rm_if_found)
{
- LOC_LOGV("%s: Search the list\n", __FUNCTION__);
+ LOC_LOGD("%s: Search the list\n", __FUNCTION__);
if( list_data == NULL || NULL == equal )
{
LOC_LOGE("%s: Invalid list parameter! list_data %p equal %p\n",
diff --git a/utils/loc_cfg.cpp b/utils/loc_cfg.cpp
index 967d2f3..8c76d1f 100644
--- a/utils/loc_cfg.cpp
+++ b/utils/loc_cfg.cpp
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -38,9 +38,8 @@
#include <unistd.h>
#include <time.h>
#include <loc_cfg.h>
-#include <log_util.h>
-#include <loc_misc_utils.h>
-#ifdef USE_GLIB
+#include <platform_lib_includes.h>
+#if defined(USE_GLIB) && !defined(OFF_TARGET)
#include <glib.h>
#endif
#include "platform_lib_includes.h"
@@ -52,36 +51,22 @@
*============================================================================*/
/* Parameter data */
-static uint32_t DEBUG_LEVEL = 0xff;
-static uint32_t TIMESTAMP = 0;
+static uint8_t DEBUG_LEVEL = 0xff;
+static uint8_t TIMESTAMP = 0;
/* Parameter spec table */
-static const loc_param_s_type loc_param_table[] =
+static loc_param_s_type loc_parameter_table[] =
{
- {"DEBUG_LEVEL", &DEBUG_LEVEL, NULL, 'n'},
- {"TIMESTAMP", &TIMESTAMP, NULL, 'n'},
+ {"DEBUG_LEVEL", &DEBUG_LEVEL, NULL, 'n'},
+ {"TIMESTAMP", &TIMESTAMP, NULL, 'n'},
};
-static const int loc_param_num = sizeof(loc_param_table) / sizeof(loc_param_s_type);
-
-typedef struct loc_param_v_type
-{
- char* param_name;
- char* param_str_value;
- int param_int_value;
- double param_double_value;
-}loc_param_v_type;
+int loc_param_num = sizeof(loc_parameter_table) / sizeof(loc_param_s_type);
/*===========================================================================
-FUNCTION loc_set_config_entry
+FUNCTION trim_space
DESCRIPTION
- Potentially sets a given configuration table entry based on the passed in
- configuration value. This is done by using a string comparison of the
- parameter names and those found in the configuration file.
-
-PARAMETERS:
- config_entry: configuration entry in the table to possibly set
- config_value: value to store in the entry if the parameter names match
+ Removes leading and trailing spaces of the string
DEPENDENCIES
N/A
@@ -92,267 +77,120 @@ RETURN VALUE
SIDE EFFECTS
N/A
===========================================================================*/
-int loc_set_config_entry(const loc_param_s_type* config_entry, loc_param_v_type* config_value)
+void trim_space(char *org_string)
{
- int ret=-1;
- if(NULL == config_entry || NULL == config_value)
- {
- LOC_LOGE("%s: INVALID config entry or parameter", __FUNCTION__);
- return ret;
- }
-
- if (strcmp(config_entry->param_name, config_value->param_name) == 0 &&
- config_entry->param_ptr)
- {
- switch (config_entry->param_type)
- {
- case 's':
- if (strcmp(config_value->param_str_value, "NULL") == 0)
- {
- *((char*)config_entry->param_ptr) = '\0';
- }
- else {
- strlcpy((char*) config_entry->param_ptr,
- config_value->param_str_value,
- LOC_MAX_PARAM_STRING + 1);
- }
- /* Log INI values */
- LOC_LOGD("%s: PARAM %s = %s", __FUNCTION__,
- config_entry->param_name, (char*)config_entry->param_ptr);
-
- if(NULL != config_entry->param_set)
- {
- *(config_entry->param_set) = 1;
- }
- ret = 0;
- break;
- case 'n':
- *((int *)config_entry->param_ptr) = config_value->param_int_value;
- /* Log INI values */
- LOC_LOGD("%s: PARAM %s = %d", __FUNCTION__,
- config_entry->param_name, config_value->param_int_value);
-
- if(NULL != config_entry->param_set)
- {
- *(config_entry->param_set) = 1;
- }
- ret = 0;
- break;
- case 'f':
- *((double *)config_entry->param_ptr) = config_value->param_double_value;
- /* Log INI values */
- LOC_LOGD("%s: PARAM %s = %f", __FUNCTION__,
- config_entry->param_name, config_value->param_double_value);
-
- if(NULL != config_entry->param_set)
- {
- *(config_entry->param_set) = 1;
- }
- ret = 0;
- break;
- default:
- LOC_LOGE("%s: PARAM %s parameter type must be n, f, or s",
- __FUNCTION__, config_entry->param_name);
- }
- }
- return ret;
+ char *scan_ptr, *write_ptr;
+ char *first_nonspace = NULL, *last_nonspace = NULL;
+
+ scan_ptr = write_ptr = org_string;
+
+ while (*scan_ptr)
+ {
+ if ( !isspace(*scan_ptr) && first_nonspace == NULL)
+ {
+ first_nonspace = scan_ptr;
+ }
+
+ if (first_nonspace != NULL)
+ {
+ *(write_ptr++) = *scan_ptr;
+ if ( !isspace(*scan_ptr))
+ {
+ last_nonspace = write_ptr;
+ }
+ }
+
+ scan_ptr++;
+ }
+
+ if (last_nonspace) { *last_nonspace = '\0'; }
}
-/*===========================================================================
-FUNCTION loc_fill_conf_item
-
-DESCRIPTION
- Takes a line of configuration item and sets defined values based on
- the passed in configuration table. This table maps strings to values to
- set along with the type of each of these values.
-
-PARAMETERS:
- input_buf : buffer contanis config item
- config_table: table definition of strings to places to store information
- table_length: length of the configuration table
-
-DEPENDENCIES
- N/A
-
-RETURN VALUE
- 0: Number of records in the config_table filled with input_buf
-
-SIDE EFFECTS
- N/A
-===========================================================================*/
-int loc_fill_conf_item(char* input_buf,
- const loc_param_s_type* config_table, uint32_t table_length)
+typedef struct loc_param_v_type
{
- int ret = 0;
-
- if (input_buf && config_table) {
- char *lasts;
- loc_param_v_type config_value;
- memset(&config_value, 0, sizeof(config_value));
-
- /* Separate variable and value */
- config_value.param_name = strtok_r(input_buf, "=", &lasts);
- /* skip lines that do not contain "=" */
- if (config_value.param_name) {
- config_value.param_str_value = strtok_r(NULL, "=", &lasts);
-
- /* skip lines that do not contain two operands */
- if (config_value.param_str_value) {
- /* Trim leading and trailing spaces */
- loc_util_trim_space(config_value.param_name);
- loc_util_trim_space(config_value.param_str_value);
-
- /* Parse numerical value */
- if ((strlen(config_value.param_str_value) >=3) &&
- (config_value.param_str_value[0] == '0') &&
- (tolower(config_value.param_str_value[1]) == 'x'))
- {
- /* hex */
- config_value.param_int_value = (int) strtol(&config_value.param_str_value[2],
- (char**) NULL, 16);
- }
- else {
- config_value.param_double_value = (double) atof(config_value.param_str_value); /* float */
- config_value.param_int_value = atoi(config_value.param_str_value); /* dec */
- }
-
- for(uint32_t i = 0; NULL != config_table && i < table_length; i++)
- {
- if(!loc_set_config_entry(&config_table[i], &config_value)) {
- ret += 1;
- }
- }
- }
- }
- }
-
- return ret;
-}
-
-/*===========================================================================
-FUNCTION loc_read_conf_r (repetitive)
+ char* param_name;
-DESCRIPTION
- Reads the specified configuration file and sets defined values based on
- the passed in configuration table. This table maps strings to values to
- set along with the type of each of these values.
- The difference between this and loc_read_conf is that this function returns
- the file pointer position at the end of filling a config table. Also, it
- reads a fixed number of parameters at a time which is equal to the length
- of the configuration table. This functionality enables the caller to
- repeatedly call the function to read data from the same file.
-
-PARAMETERS:
- conf_fp : file pointer
- config_table: table definition of strings to places to store information
- table_length: length of the configuration table
-
-DEPENDENCIES
- N/A
-
-RETURN VALUE
- 0: Table filled successfully
- 1: No more parameters to read
- -1: Error filling table
-
-SIDE EFFECTS
- N/A
-===========================================================================*/
-int loc_read_conf_r(FILE *conf_fp, const loc_param_s_type* config_table, uint32_t table_length)
-{
- int ret=0;
-
- unsigned int num_params=table_length;
- if(conf_fp == NULL) {
- LOC_LOGE("%s:%d]: ERROR: File pointer is NULL\n", __func__, __LINE__);
- ret = -1;
- goto err;
- }
-
- /* Clear all validity bits */
- for(uint32_t i = 0; NULL != config_table && i < table_length; i++)
- {
- if(NULL != config_table[i].param_set)
- {
- *(config_table[i].param_set) = 0;
- }
- }
-
- char input_buf[LOC_MAX_PARAM_LINE]; /* declare a char array */
-
- LOC_LOGD("%s:%d]: num_params: %d\n", __func__, __LINE__, num_params);
- while(num_params)
- {
- if(!fgets(input_buf, LOC_MAX_PARAM_LINE, conf_fp)) {
- LOC_LOGD("%s:%d]: fgets returned NULL\n", __func__, __LINE__);
- break;
- }
-
- num_params -= loc_fill_conf_item(input_buf, config_table, table_length);
- }
-
-err:
- return ret;
-}
+ char* param_str_value;
+ int param_int_value;
+ double param_double_value;
+}loc_param_v_type;
/*===========================================================================
-FUNCTION loc_udpate_conf
+FUNCTION loc_set_config_entry
DESCRIPTION
- Parses the passed in buffer for configuration items, and update the table
- that is also passed in.
-
-Reads the specified configuration file and sets defined values based on
- the passed in configuration table. This table maps strings to values to
- set along with the type of each of these values.
+ Potentially sets a given configuration table entry based on the passed in
+ configuration value. This is done by using a string comparison of the
+ parameter names and those found in the configuration file.
PARAMETERS:
- conf_data: configuration items in bufferas a string
- length: strlen(conf_data)
- config_table: table definition of strings to places to store information
- table_length: length of the configuration table
+ config_entry: configuration entry in the table to possibly set
+ config_value: value to store in the entry if the parameter names match
DEPENDENCIES
N/A
RETURN VALUE
- number of the records in the table that is updated at time of return.
+ None
SIDE EFFECTS
N/A
===========================================================================*/
-int loc_update_conf(const char* conf_data, int32_t length,
- const loc_param_s_type* config_table, uint32_t table_length)
+void loc_set_config_entry(loc_param_s_type* config_entry, loc_param_v_type* config_value)
{
- int ret = -1;
-
- if (conf_data && length && config_table && table_length) {
- // make a copy, so we do not tokenize the original data
- char* conf_copy = (char*)malloc(length+1);
-
- if (conf_copy != NULL)
- {
- memcpy(conf_copy, conf_data, length);
- // we hard NULL the end of string to be safe
- conf_copy[length] = 0;
-
- // start with one record off
- uint32_t num_params = table_length - 1;
- char* saveptr = NULL;
- char* input_buf = strtok_r(conf_copy, "\n", &saveptr);
- ret = 0;
-
- LOC_LOGD("%s:%d]: num_params: %d\n", __func__, __LINE__, num_params);
- while(num_params && input_buf) {
- ret++;
- num_params -= loc_fill_conf_item(input_buf, config_table, table_length);
- input_buf = strtok_r(NULL, "\n", &saveptr);
- }
- free(conf_copy);
- }
- }
-
- return ret;
+ if(NULL == config_entry || NULL == config_value)
+ {
+ LOC_LOGE("%s: INVALID config entry or parameter", __FUNCTION__);
+ return;
+ }
+
+ if (strcmp(config_entry->param_name, config_value->param_name) == 0 &&
+ config_entry->param_ptr)
+ {
+ switch (config_entry->param_type)
+ {
+ case 's':
+ if (strcmp(config_value->param_str_value, "NULL") == 0)
+ {
+ *((char*)config_entry->param_ptr) = '\0';
+ }
+ else {
+ strlcpy((char*) config_entry->param_ptr,
+ config_value->param_str_value,
+ LOC_MAX_PARAM_STRING + 1);
+ }
+ /* Log INI values */
+ LOC_LOGD("%s: PARAM %s = %s", __FUNCTION__, config_entry->param_name, (char*)config_entry->param_ptr);
+
+ if(NULL != config_entry->param_set)
+ {
+ *(config_entry->param_set) = 1;
+ }
+ break;
+ case 'n':
+ *((int *)config_entry->param_ptr) = config_value->param_int_value;
+ /* Log INI values */
+ LOC_LOGD("%s: PARAM %s = %d", __FUNCTION__, config_entry->param_name, config_value->param_int_value);
+
+ if(NULL != config_entry->param_set)
+ {
+ *(config_entry->param_set) = 1;
+ }
+ break;
+ case 'f':
+ *((double *)config_entry->param_ptr) = config_value->param_double_value;
+ /* Log INI values */
+ LOC_LOGD("%s: PARAM %s = %f", __FUNCTION__, config_entry->param_name, config_value->param_double_value);
+
+ if(NULL != config_entry->param_set)
+ {
+ *(config_entry->param_set) = 1;
+ }
+ break;
+ default:
+ LOC_LOGE("%s: PARAM %s parameter type must be n, f, or s", __FUNCTION__, config_entry->param_name);
+ }
+ }
}
/*===========================================================================
@@ -377,24 +215,72 @@ RETURN VALUE
SIDE EFFECTS
N/A
===========================================================================*/
-void loc_read_conf(const char* conf_file_name, const loc_param_s_type* config_table,
- uint32_t table_length)
+void loc_read_conf(const char* conf_file_name, loc_param_s_type* config_table, uint32_t table_length)
{
- FILE *conf_fp = NULL;
- char *lasts;
- loc_param_v_type config_value;
- uint32_t i;
-
- if((conf_fp = fopen(conf_file_name, "r")) != NULL)
- {
- LOC_LOGD("%s: using %s", __FUNCTION__, conf_file_name);
- if(table_length && config_table) {
- loc_read_conf_r(conf_fp, config_table, table_length);
- rewind(conf_fp);
- }
- loc_read_conf_r(conf_fp, loc_param_table, loc_param_num);
- fclose(conf_fp);
- }
- /* Initialize logging mechanism with parsed data */
- loc_logger_init(DEBUG_LEVEL, TIMESTAMP);
+ FILE *gps_conf_fp = NULL;
+ char input_buf[LOC_MAX_PARAM_LINE]; /* declare a char array */
+ char *lasts;
+ loc_param_v_type config_value;
+ uint32_t i;
+
+ if((gps_conf_fp = fopen(conf_file_name, "r")) != NULL)
+ {
+ LOC_LOGD("%s: using %s", __FUNCTION__, conf_file_name);
+ }
+ else
+ {
+ LOC_LOGW("%s: no %s file found", __FUNCTION__, conf_file_name);
+ loc_logger_init(DEBUG_LEVEL, TIMESTAMP);
+ return; /* no parameter file */
+ }
+
+ /* Clear all validity bits */
+ for(i = 0; NULL != config_table && i < table_length; i++)
+ {
+ if(NULL != config_table[i].param_set)
+ {
+ *(config_table[i].param_set) = 0;
+ }
+ }
+
+ while(fgets(input_buf, LOC_MAX_PARAM_LINE, gps_conf_fp) != NULL)
+ {
+ memset(&config_value, 0, sizeof(config_value));
+
+ /* Separate variable and value */
+ config_value.param_name = strtok_r(input_buf, "=", &lasts);
+ if (config_value.param_name == NULL) continue; /* skip lines that do not contain "=" */
+ config_value.param_str_value = strtok_r(NULL, "=", &lasts);
+ if (config_value.param_str_value == NULL) continue; /* skip lines that do not contain two operands */
+
+ /* Trim leading and trailing spaces */
+ trim_space(config_value.param_name);
+ trim_space(config_value.param_str_value);
+
+ /* Parse numerical value */
+ if (config_value.param_str_value[0] == '0' && tolower(config_value.param_str_value[1]) == 'x')
+ {
+ /* hex */
+ config_value.param_int_value = (int) strtol(&config_value.param_str_value[2], (char**) NULL, 16);
+ }
+ else {
+ config_value.param_double_value = (double) atof(config_value.param_str_value); /* float */
+ config_value.param_int_value = atoi(config_value.param_str_value); /* dec */
+ }
+
+ for(i = 0; NULL != config_table && i < table_length; i++)
+ {
+ loc_set_config_entry(&config_table[i], &config_value);
+ }
+
+ for(i = 0; i < loc_param_num; i++)
+ {
+ loc_set_config_entry(&loc_parameter_table[i], &config_value);
+ }
+ }
+
+ fclose(gps_conf_fp);
+
+ /* Initialize logging mechanism with parsed data */
+ loc_logger_init(DEBUG_LEVEL, TIMESTAMP);
}
diff --git a/utils/loc_cfg.h b/utils/loc_cfg.h
index 9045e1d..df83338 100644
--- a/utils/loc_cfg.h
+++ b/utils/loc_cfg.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -30,22 +30,17 @@
#ifndef LOC_CFG_H
#define LOC_CFG_H
-#include <stdio.h>
#include <stdint.h>
-#define LOC_MAX_PARAM_NAME 80
+#define LOC_MAX_PARAM_NAME 48
#define LOC_MAX_PARAM_STRING 80
-#define LOC_MAX_PARAM_LINE (LOC_MAX_PARAM_NAME + LOC_MAX_PARAM_STRING)
-
-#define UTIL_UPDATE_CONF(conf_data, len, config_table) \
- loc_update_conf((conf_data), (len), (config_table), \
- sizeof(config_table) / sizeof(config_table[0]))
+#define LOC_MAX_PARAM_LINE 80
#define UTIL_READ_CONF_DEFAULT(filename) \
loc_read_conf((filename), NULL, 0);
#define UTIL_READ_CONF(filename, config_table) \
- loc_read_conf((filename), (config_table), sizeof(config_table) / sizeof(config_table[0]))
+ loc_read_conf((filename), (config_table), sizeof(config_table) / sizeof(config_table[0]))
/*=============================================================================
*
@@ -54,7 +49,7 @@
*============================================================================*/
typedef struct
{
- const char *param_name;
+ char param_name[LOC_MAX_PARAM_NAME];
void *param_ptr;
uint8_t *param_set; /* was this value set by config file? */
char param_type; /* 'n' for number,
@@ -77,13 +72,10 @@ extern "C" {
* MODULE EXPORTED FUNCTIONS
*
*============================================================================*/
-void loc_read_conf(const char* conf_file_name,
- const loc_param_s_type* config_table,
- uint32_t table_length);
-int loc_read_conf_r(FILE *conf_fp, const loc_param_s_type* config_table,
- uint32_t table_length);
-int loc_update_conf(const char* conf_data, int32_t length,
- const loc_param_s_type* config_table, uint32_t table_length);
+extern void loc_read_conf(const char* conf_file_name,
+ loc_param_s_type* config_table,
+ uint32_t table_length);
+
#ifdef __cplusplus
}
#endif
diff --git a/utils/loc_log.cpp b/utils/loc_log.cpp
index f6b54f9..c64310e 100644
--- a/utils/loc_log.cpp
+++ b/utils/loc_log.cpp
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2012, 2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2012,2014 The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -47,15 +47,14 @@ const char FROM_MODEM[] = "<---";
const char TO_AFW[] = "<===";
const char EXIT_TAG[] = "Exiting";
const char ENTRY_TAG[] = "Entering";
-const char EXIT_ERROR_TAG[] = "Exiting with error";
/* Logging Mechanism */
loc_logger_s_type loc_logger;
/* Get names from value */
-const char* loc_get_name_from_mask(const loc_name_val_s_type table[], size_t table_size, long mask)
+const char* loc_get_name_from_mask(loc_name_val_s_type table[], int table_size, long mask)
{
- size_t i;
+ int i;
for (i = 0; i < table_size; i++)
{
if (table[i].val & (long) mask)
@@ -67,9 +66,9 @@ const char* loc_get_name_from_mask(const loc_name_val_s_type table[], size_t tab
}
/* Get names from value */
-const char* loc_get_name_from_val(const loc_name_val_s_type table[], size_t table_size, long value)
+const char* loc_get_name_from_val(loc_name_val_s_type table[], int table_size, long value)
{
- size_t i;
+ int i;
for (i = 0; i < table_size; i++)
{
if (table[i].val == (long) value)
@@ -80,7 +79,7 @@ const char* loc_get_name_from_val(const loc_name_val_s_type table[], size_t tabl
return UNKNOWN_STR;
}
-static const loc_name_val_s_type loc_msg_q_status[] =
+static loc_name_val_s_type loc_msg_q_status[] =
{
NAME_VAL( eMSG_Q_SUCCESS ),
NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
@@ -89,7 +88,7 @@ static const loc_name_val_s_type loc_msg_q_status[] =
NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
};
-static const size_t loc_msg_q_status_num = LOC_TABLE_SIZE(loc_msg_q_status);
+static int loc_msg_q_status_num = sizeof(loc_msg_q_status) / sizeof(loc_name_val_s_type);
/* Find msg_q status name */
const char* loc_get_msg_q_status(int status)
@@ -103,18 +102,17 @@ const char* log_succ_fail_string(int is_succ)
}
//Target names
-static const loc_name_val_s_type target_name[] =
+loc_name_val_s_type target_name[] =
{
NAME_VAL(GNSS_NONE),
NAME_VAL(GNSS_MSM),
NAME_VAL(GNSS_GSS),
NAME_VAL(GNSS_MDM),
NAME_VAL(GNSS_QCA1530),
- NAME_VAL(GNSS_AUTO),
NAME_VAL(GNSS_UNKNOWN)
};
-static const size_t target_name_num = LOC_TABLE_SIZE(target_name);
+static int target_name_num = sizeof(target_name)/sizeof(loc_name_val_s_type);
/*===========================================================================
@@ -135,7 +133,7 @@ const char *loc_get_target_name(unsigned int target)
static char ret[BUFFER_SIZE];
index = getTargetGnssType(target);
- if( index < 0 || (unsigned)index >= target_name_num )
+ if( index >= target_name_num || index < 0)
index = target_name_num - 1;
if( (target & HAS_SSC) == HAS_SSC ) {
@@ -164,7 +162,7 @@ RETURN VALUE
The time string
===========================================================================*/
-char *loc_get_time(char *time_string, size_t buf_size)
+char *loc_get_time(char *time_string, unsigned long buf_size)
{
struct timeval now; /* sec and usec */
struct tm now_tm; /* broken-down time */
@@ -178,61 +176,3 @@ char *loc_get_time(char *time_string, size_t buf_size)
return time_string;
}
-
-
-/*===========================================================================
-FUNCTION loc_logger_init
-
-DESCRIPTION
- Initializes the state of DEBUG_LEVEL and TIMESTAMP
-
-DEPENDENCIES
- N/A
-
-RETURN VALUE
- None
-
-SIDE EFFECTS
- N/A
-===========================================================================*/
-void loc_logger_init(unsigned long debug, unsigned long timestamp)
-{
- loc_logger.DEBUG_LEVEL = debug;
-#ifdef TARGET_BUILD_VARIANT_USER
- // force user builds to 2 or less
- if (loc_logger.DEBUG_LEVEL > 2) {
- loc_logger.DEBUG_LEVEL = 2;
- }
-#endif
- loc_logger.TIMESTAMP = timestamp;
-}
-
-
-/*===========================================================================
-FUNCTION get_timestamp
-
-DESCRIPTION
- Generates a timestamp using the current system time
-
-DEPENDENCIES
- N/A
-
-RETURN VALUE
- Char pointer to the parameter str
-
-SIDE EFFECTS
- N/A
-===========================================================================*/
-char * get_timestamp(char *str, unsigned long buf_size)
-{
- struct timeval tv;
- struct timezone tz;
- int hh, mm, ss;
- gettimeofday(&tv, &tz);
- hh = tv.tv_sec/3600%24;
- mm = (tv.tv_sec%3600)/60;
- ss = tv.tv_sec%60;
- snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
- return str;
-}
-
diff --git a/utils/loc_log.h b/utils/loc_log.h
index be492b1..82dc636 100644
--- a/utils/loc_log.h
+++ b/utils/loc_log.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2012, 2015 The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -36,13 +36,12 @@ extern "C"
#endif
#include <ctype.h>
-#include <stdlib.h>
#include "loc_target.h"
typedef struct
{
- const char *name;
- long val;
+ char name[128];
+ long val;
} loc_name_val_s_type;
#define NAME_VAL(x) {"" #x "", x }
@@ -50,19 +49,17 @@ typedef struct
#define UNKNOWN_STR "UNKNOWN"
#define CHECK_MASK(type, value, mask_var, mask) \
- (((mask_var) & (mask)) ? (type) (value) : (type) (-1))
-
-#define LOC_TABLE_SIZE(table) (sizeof(table)/sizeof((table)[0]))
+ ((mask_var & mask) ? (type) value : (type) (-1))
/* Get names from value */
-const char* loc_get_name_from_mask(const loc_name_val_s_type table[], size_t table_size, long mask);
-const char* loc_get_name_from_val(const loc_name_val_s_type table[], size_t table_size, long value);
+const char* loc_get_name_from_mask(loc_name_val_s_type table[], int table_size, long mask);
+const char* loc_get_name_from_val(loc_name_val_s_type table[], int table_size, long value);
const char* loc_get_msg_q_status(int status);
const char* loc_get_target_name(unsigned int target);
extern const char* log_succ_fail_string(int is_succ);
-extern char *loc_get_time(char *time_string, size_t buf_size);
+extern char *loc_get_time(char *time_string, unsigned long buf_size);
#ifdef __cplusplus
}
diff --git a/utils/loc_misc_utils.cpp b/utils/loc_misc_utils.cpp
deleted file mode 100644
index 7e96313..0000000
--- a/utils/loc_misc_utils.cpp
+++ /dev/null
@@ -1,114 +0,0 @@
-/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#include <stdio.h>
-#include <string.h>
-#include <log_util.h>
-#include <loc_misc_utils.h>
-#include <ctype.h>
-
-#define LOG_NDDEBUG 0
-#define LOG_TAG "LocSvc_misc_utils"
-
-int loc_util_split_string(char *raw_string, char **split_strings_ptr,
- int max_num_substrings, char delimiter)
-{
- int raw_string_index=0;
- int num_split_strings=0;
- unsigned char end_string=0;
- int raw_string_length=0;
-
- if(!raw_string || !split_strings_ptr) {
- LOC_LOGE("%s:%d]: NULL parameters", __func__, __LINE__);
- num_split_strings = -1;
- goto err;
- }
- LOC_LOGD("%s:%d]: raw string: %s\n", __func__, __LINE__, raw_string);
- raw_string_length = strlen(raw_string) + 1;
- split_strings_ptr[num_split_strings] = &raw_string[raw_string_index];
- for(raw_string_index=0; raw_string_index < raw_string_length; raw_string_index++) {
- if(raw_string[raw_string_index] == '\0')
- end_string=1;
- if((raw_string[raw_string_index] == delimiter) || end_string) {
- raw_string[raw_string_index] = '\0';
- LOC_LOGD("%s:%d]: split string: %s\n",
- __func__, __LINE__, split_strings_ptr[num_split_strings]);
- num_split_strings++;
- if(((raw_string_index + 1) < raw_string_length) &&
- (num_split_strings < max_num_substrings)) {
- split_strings_ptr[num_split_strings] = &raw_string[raw_string_index+1];
- }
- else {
- break;
- }
- }
- if(end_string)
- break;
- }
-err:
- LOC_LOGD("%s:%d]: num_split_strings: %d\n", __func__, __LINE__, num_split_strings);
- return num_split_strings;
-}
-
-void loc_util_trim_space(char *org_string)
-{
- char *scan_ptr, *write_ptr;
- char *first_nonspace = NULL, *last_nonspace = NULL;
-
- if(org_string == NULL) {
- LOC_LOGE("%s:%d]: NULL parameter", __func__, __LINE__);
- goto err;
- }
-
- scan_ptr = write_ptr = org_string;
-
- while (*scan_ptr) {
- //Find the first non-space character
- if ( !isspace(*scan_ptr) && first_nonspace == NULL) {
- first_nonspace = scan_ptr;
- }
- //Once the first non-space character is found in the
- //above check, keep shifting the characters to the left
- //to replace the spaces
- if (first_nonspace != NULL) {
- *(write_ptr++) = *scan_ptr;
- //Keep track of which was the last non-space character
- //encountered
- //last_nonspace will not be updated in the case where
- //the string ends with spaces
- if ( !isspace(*scan_ptr)) {
- last_nonspace = write_ptr;
- }
- }
- scan_ptr++;
- }
- //Add NULL terminator after the last non-space character
- if (last_nonspace) { *last_nonspace = '\0'; }
-err:
- return;
-}
diff --git a/utils/loc_misc_utils.h b/utils/loc_misc_utils.h
deleted file mode 100644
index 7d66d84..0000000
--- a/utils/loc_misc_utils.h
+++ /dev/null
@@ -1,99 +0,0 @@
-/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation, nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-#ifndef _LOC_MISC_UTILS_H_
-#define _LOC_MISC_UTILS_H_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*===========================================================================
-FUNCTION loc_split_string
-
-DESCRIPTION:
- This function is used to split a delimiter separated string into
- sub-strings. This function does not allocate new memory to store the split
- strings. Instead, it places '\0' in places of delimiters and assings the
- starting address of the substring within the raw string as the string address
- The input raw_string no longer remains to be a collection of sub-strings
- after this function is executed.
- Please make a copy of the input string before calling this function if
- necessary
-
-PARAMETERS:
- char *raw_string: is the original string with delimiter separated substrings
- char **split_strings_ptr: is the arraw of pointers which will hold the addresses
- of individual substrings
- int max_num_substrings: is the maximum number of substrings that are expected
- by the caller. The array of pointers in the above parameter
- is usually this long
- char delimiter: is the delimiter that separates the substrings. Examples: ' ', ';'
-
-DEPENDENCIES
- N/A
-
-RETURN VALUE
- int Number of split strings
-
-SIDE EFFECTS
- The input raw_string no longer remains a delimiter separated single string.
-
-EXAMPLE
- delimiter = ' ' //space
- raw_string = "hello new user" //delimiter is space ' '
- addresses = 0123456789abcd
- split_strings_ptr[0] = &raw_string[0]; //split_strings_ptr[0] contains "hello"
- split_strings_ptr[1] = &raw_string[6]; //split_strings_ptr[1] contains "new"
- split_strings_ptr[2] = &raw_string[a]; //split_strings_ptr[2] contains "user"
-
-===========================================================================*/
-int loc_util_split_string(char *raw_string, char **split_strings_ptr, int max_num_substrings,
- char delimiter);
-
-/*===========================================================================
-FUNCTION trim_space
-
-DESCRIPTION
- Removes leading and trailing spaces of the string
-
-DEPENDENCIES
- N/A
-
-RETURN VALUE
- None
-
-SIDE EFFECTS
- N/A
-===========================================================================*/
-void loc_util_trim_space(char *org_string);
-#ifdef __cplusplus
-}
-#endif
-
-#endif //_LOC_MISC_UTILS_H_
diff --git a/utils/loc_target.cpp b/utils/loc_target.cpp
index 30c040c..b5a6297 100644
--- a/utils/loc_target.cpp
+++ b/utils/loc_target.cpp
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2012,2014, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -53,15 +53,11 @@
#define STR_SURF "Surf"
#define STR_MTP "MTP"
#define STR_APQ "apq"
-#define STR_AUTO "auto"
#define IS_STR_END(c) ((c) == '\0' || (c) == '\n' || (c) == '\r')
#define LENGTH(s) (sizeof(s) - 1)
#define GPS_CHECK_NO_ERROR 0
#define GPS_CHECK_NO_GPS_HW 1
-/* When system server is started, it uses 20 seconds as ActivityManager
- * timeout. After that it sends SIGSTOP signal to process.
- */
-#define QCA1530_DETECT_TIMEOUT 15
+#define QCA1530_DETECT_TIMEOUT 30
#define QCA1530_DETECT_PRESENT "yes"
#define QCA1530_DETECT_PROGRESS "detect"
@@ -103,7 +99,7 @@ static int read_a_line(const char * file_path, char * line, int line_size)
*/
static bool is_qca1530(void)
{
- static const char qca1530_property_name[] = "sys.qca1530";
+ static const char qca1530_property_name[] = "persist.qca1530";
bool res = false;
int ret, i;
char buf[PROPERTY_VALUE_MAX];
@@ -146,34 +142,6 @@ static bool is_qca1530(void)
return res;
}
-/*The character array passed to this function should have length
- of atleast PROPERTY_VALUE_MAX*/
-void loc_get_target_baseband(char *baseband, int array_length)
-{
- if(baseband && (array_length >= PROPERTY_VALUE_MAX)) {
- property_get("ro.baseband", baseband, "");
- LOC_LOGD("%s:%d]: Baseband: %s\n", __func__, __LINE__, baseband);
- }
- else {
- LOC_LOGE("%s:%d]: NULL parameter or array length less than PROPERTY_VALUE_MAX\n",
- __func__, __LINE__);
- }
-}
-
-/*The character array passed to this function should have length
- of atleast PROPERTY_VALUE_MAX*/
-void loc_get_platform_name(char *platform_name, int array_length)
-{
- if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) {
- property_get("ro.board.platform", platform_name, "");
- LOC_LOGD("%s:%d]: Target name: %s\n", __func__, __LINE__, platform_name);
- }
- else {
- LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n",
- __func__, __LINE__);
- }
-}
-
unsigned int loc_get_target(void)
{
if (gTarget != (unsigned int)-1)
@@ -196,8 +164,7 @@ unsigned int loc_get_target(void)
goto detected;
}
- loc_get_target_baseband(baseband, sizeof(baseband));
-
+ platform_lib_abstraction_property_get("ro.baseband", baseband, "");
if (!access(hw_platform, F_OK)) {
read_a_line(hw_platform, rd_hw_platform, LINE_LEN);
} else {
@@ -208,13 +175,8 @@ unsigned int loc_get_target(void)
} else {
read_a_line(id_dep, rd_id, LINE_LEN);
}
- if( !memcmp(baseband, STR_AUTO, LENGTH(STR_AUTO)) )
- {
- gTarget = TARGET_AUTO;
- goto detected;
- }
- if( !memcmp(baseband, STR_APQ, LENGTH(STR_APQ)) ){
+ if( !memcmp(baseband, STR_APQ, LENGTH(STR_APQ)) ){
if( !memcmp(rd_id, MPQ8064_ID_1, LENGTH(MPQ8064_ID_1))
&& IS_STR_END(rd_id[LENGTH(MPQ8064_ID_1)]) )
gTarget = TARGET_MPQ;
@@ -245,17 +207,3 @@ detected:
LOC_LOGD("HAL: %s returned %d", __FUNCTION__, gTarget);
return gTarget;
}
-
-/*Reads the property ro.lean to identify if this is a lean target
- Returns:
- 0 if not a lean and mean target
- 1 if this is a lean and mean target
-*/
-int loc_identify_lean_target()
-{
- int ret = 0;
- char lean_target[PROPERTY_VALUE_MAX];
- property_get("ro.lean", lean_target, "");
- LOC_LOGD("%s:%d]: lean target: %s\n", __func__, __LINE__, lean_target);
- return !(strncmp(lean_target, "true", PROPERTY_VALUE_MAX));
-}
diff --git a/utils/loc_target.h b/utils/loc_target.h
index 3bb3b5e..4aebb85 100644
--- a/utils/loc_target.h
+++ b/utils/loc_target.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -35,7 +35,6 @@
#define TARGET_MPQ TARGET_SET(GNSS_NONE,NO_SSC)
#define TARGET_MSM_NO_SSC TARGET_SET(GNSS_MSM, NO_SSC)
#define TARGET_QCA1530 TARGET_SET(GNSS_QCA1530, NO_SSC)
-#define TARGET_AUTO TARGET_SET(GNSS_AUTO, NO_SSC)
#define TARGET_UNKNOWN TARGET_SET(GNSS_UNKNOWN, NO_SSC)
#define getTargetGnssType(target) (target>>1)
@@ -46,18 +45,6 @@ extern "C"
unsigned int loc_get_target(void);
-/*The character array passed to this function should have length
- of atleast PROPERTY_VALUE_MAX*/
-void loc_get_target_baseband(char *baseband, int array_length);
-/*The character array passed to this function should have length
- of atleast PROPERTY_VALUE_MAX*/
-void loc_get_platform_name(char *platform_name, int array_length);
-/*Reads the property ro.lean to identify if this is a lean target
- Returns:
- 0 if not a lean and mean target
- 1 if this is a lean and mean target*/
-int loc_identify_lean_target();
-
/* Please remember to update 'target_name' in loc_log.cpp,
if do any changes to this enum. */
typedef enum {
@@ -66,7 +53,6 @@ typedef enum {
GNSS_GSS,
GNSS_MDM,
GNSS_QCA1530,
- GNSS_AUTO,
GNSS_UNKNOWN
}GNSS_TARGET;
diff --git a/utils/loc_timer.c b/utils/loc_timer.c
new file mode 100644
index 0000000..0e9f6bd
--- /dev/null
+++ b/utils/loc_timer.c
@@ -0,0 +1,187 @@
+/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation, nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include<stdio.h>
+#include<stdlib.h>
+#include<sys/time.h>
+#include "loc_timer.h"
+#include<time.h>
+#include<errno.h>
+#include"platform_lib_includes.h"
+
+enum timer_state {
+ READY = 100,
+ WAITING,
+ DONE,
+ ABORT
+};
+
+typedef struct {
+ loc_timer_callback callback_func;
+ void *user_data;
+ unsigned int time_msec;
+ pthread_cond_t timer_cond;
+ pthread_mutex_t timer_mutex;
+ enum timer_state state;
+}timer_data;
+
+static void *timer_thread(void *thread_data)
+{
+ int ret = -ETIMEDOUT;
+ struct timespec ts;
+ struct timeval tv;
+ timer_data* t = (timer_data*)thread_data;
+
+ LOC_LOGD("%s:%d]: Enter. Delay = %d\n", __func__, __LINE__, t->time_msec);
+
+ gettimeofday(&tv, NULL);
+ clock_gettime(CLOCK_REALTIME, &ts);
+ if(t->time_msec >= 1000) {
+ ts.tv_sec += t->time_msec/1000;
+ t->time_msec = t->time_msec % 1000;
+ }
+ if(t->time_msec)
+ ts.tv_nsec += t->time_msec * 1000000;
+ if(ts.tv_nsec > 999999999) {
+ LOC_LOGD("%s:%d]: Large nanosecs\n", __func__, __LINE__);
+ ts.tv_sec += 1;
+ ts.tv_nsec -= 1000000000;
+ }
+ LOC_LOGD("%s:%d]: ts.tv_sec:%d; ts.tv_nsec:%d\n"
+ "\t Current time: %d sec; %d nsec",
+ __func__, __LINE__, (int)ts.tv_sec, (int)ts.tv_nsec,
+ (int)tv.tv_sec, (int)tv.tv_usec*1000);
+
+ pthread_mutex_lock(&(t->timer_mutex));
+ if (READY == t->state) {
+ t->state = WAITING;
+ ret = pthread_cond_timedwait(&t->timer_cond, &t->timer_mutex, &ts);
+ t->state = DONE;
+ }
+ pthread_mutex_unlock(&(t->timer_mutex));
+
+ switch (ret) {
+ case ETIMEDOUT:
+ LOC_LOGV("%s:%d]: loc_timer timed out", __func__, __LINE__);
+ break;
+ case 0:
+ LOC_LOGV("%s:%d]: loc_timer stopped", __func__, __LINE__);
+ break;
+ case -ETIMEDOUT:
+ LOC_LOGV("%s:%d]: loc_timer cancelled", __func__, __LINE__);
+ break;
+ default:
+ LOC_LOGE("%s:%d]: Call to pthread timedwait failed; ret=%d\n",
+ __func__, __LINE__, ret);
+ break;
+ }
+
+ pthread_mutex_destroy(&t->timer_mutex);
+ pthread_cond_destroy(&t->timer_cond);
+
+ if(ETIMEDOUT == ret)
+ t->callback_func(t->user_data, ret);
+
+ free(t);
+ LOC_LOGD("%s:%d]: Exit\n", __func__, __LINE__);
+ return NULL;
+}
+
+void* loc_timer_start(unsigned int msec, loc_timer_callback cb_func,
+ void* caller_data)
+{
+ timer_data *t=NULL;
+ pthread_attr_t tattr;
+ pthread_t id;
+ LOC_LOGD("%s:%d]: Enter\n", __func__, __LINE__);
+ if(cb_func == NULL || msec == 0) {
+ LOC_LOGE("%s:%d]: Error: Wrong parameters\n", __func__, __LINE__);
+ goto _err;
+ }
+ t = (timer_data *)calloc(1, sizeof(timer_data));
+ if(t == NULL) {
+ LOC_LOGE("%s:%d]: Could not allocate memory. Failing.\n",
+ __func__, __LINE__);
+ goto _err;
+ }
+
+ if(pthread_cond_init(&(t->timer_cond), NULL)) {
+ LOC_LOGE("%s:%d]: Pthread cond init failed\n", __func__, __LINE__);
+ goto t_err;
+ }
+ if(pthread_mutex_init(&(t->timer_mutex), NULL)) {
+ LOC_LOGE("%s:%d]: Pthread mutex init failed\n", __func__, __LINE__);
+ goto cond_err;
+ }
+
+ t->callback_func = cb_func;
+ t->user_data = caller_data;
+ t->time_msec = msec;
+ t->state = READY;
+
+ if (pthread_attr_init(&tattr)) {
+ LOC_LOGE("%s:%d]: Pthread mutex init failed\n", __func__, __LINE__);
+ goto mutex_err;
+ }
+ pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
+
+ if(pthread_create(&(id), &tattr, timer_thread, (void *)t)) {
+ LOC_LOGE("%s:%d]: Could not create thread\n", __func__, __LINE__);
+ goto attr_err;
+ }
+
+ LOC_LOGD("%s:%d]: Created thread with id: %d\n",
+ __func__, __LINE__, (int)id);
+ goto _err;
+
+attr_err:
+ pthread_attr_destroy(&tattr);
+mutex_err:
+ pthread_mutex_destroy(&t->timer_mutex);
+cond_err:
+ pthread_cond_destroy(&t->timer_cond);
+t_err:
+ free(t);
+_err:
+ LOC_LOGD("%s:%d]: Exit\n", __func__, __LINE__);
+ return t;
+}
+
+void loc_timer_stop(void* handle) {
+ timer_data* t = (timer_data*)handle;
+
+ if (NULL != t && (READY == t->state || WAITING == t->state)) {
+ pthread_mutex_lock(&(t->timer_mutex));
+ if (READY == t->state || WAITING == t->state) {
+ pthread_cond_signal(&t->timer_cond);
+ t->state = ABORT;
+ }
+ pthread_mutex_unlock(&(t->timer_mutex));
+ }
+}
diff --git a/utils/loc_timer.h b/utils/loc_timer.h
index 2967858..5ca1586 100644
--- a/utils/loc_timer.h
+++ b/utils/loc_timer.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013,2015 The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -33,38 +33,28 @@
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
-#include <stddef.h>
+#include<pthread.h>
+#include <platform_lib_log_util.h>
/*
- user_data: client context pointer, passthrough. Originally received
- from calling client when loc_timer_start() is called.
- result: 0 if timer successfully timed out; else timer failed.
+ Return values:
+ Success = 0
+ Failure = Non zero
*/
-typedef void (*loc_timer_callback)(void *user_data, int32_t result);
+typedef void(*loc_timer_callback)(void *user_data, int result);
/*
- delay_msec: timeout value for the timer.
- cb_func: callback function pointer, implemented by client.
- Can not be NULL.
- user_data: client context pointer, passthrough. Will be
- returned when loc_timer_callback() is called.
- wakeOnExpire: true if to wake up CPU (if sleeping) upon timer
- expiration and notify the client.
- false if to wait until next time CPU wakes up (if
- sleeping) and then notify the client.
- Returns the handle, which can be used to stop the timer
- NULL, if timer start fails (e.g. if cb_func is NULL).
+ Returns the handle, which can be used to stop the timer
*/
-void* loc_timer_start(uint64_t delay_msec,
- loc_timer_callback cb_func,
- void *user_data,
- bool wake_on_expire=false);
+void* loc_timer_start(unsigned int delay_msec,
+ loc_timer_callback,
+ void* user_data);
/*
- handle becomes invalid upon the return of the callback
+ handle becomes invalid upon the return of the callback
*/
-void loc_timer_stop(void*& handle);
+void loc_timer_stop(void* handle);
#ifdef __cplusplus
}
diff --git a/utils/msg_q.c b/utils/msg_q.c
index e412e78..1555668 100644
--- a/utils/msg_q.c
+++ b/utils/msg_q.c
@@ -198,7 +198,7 @@ msq_q_err_type msg_q_snd(void* msg_q_data, void* msg_obj, void (*dealloc)(void*)
msg_q* p_msg_q = (msg_q*)msg_q_data;
pthread_mutex_lock(&p_msg_q->list_mutex);
- LOC_LOGV("%s: Sending message with handle = 0x%08X\n", __FUNCTION__, msg_obj);
+ LOC_LOGD("%s: Sending message with handle = 0x%08X\n", __FUNCTION__, msg_obj);
if( p_msg_q->unblocked )
{
@@ -214,7 +214,7 @@ msq_q_err_type msg_q_snd(void* msg_q_data, void* msg_obj, void (*dealloc)(void*)
pthread_mutex_unlock(&p_msg_q->list_mutex);
- LOC_LOGV("%s: Finished Sending message with handle = 0x%08X\n", __FUNCTION__, msg_obj);
+ LOC_LOGD("%s: Finished Sending message with handle = 0x%08X\n", __FUNCTION__, msg_obj);
return rv;
}
@@ -241,7 +241,7 @@ msq_q_err_type msg_q_rcv(void* msg_q_data, void** msg_obj)
msg_q* p_msg_q = (msg_q*)msg_q_data;
- LOC_LOGV("%s: Waiting on message\n", __FUNCTION__);
+ LOC_LOGD("%s: Waiting on message\n", __FUNCTION__);
pthread_mutex_lock(&p_msg_q->list_mutex);
@@ -262,7 +262,7 @@ msq_q_err_type msg_q_rcv(void* msg_q_data, void** msg_obj)
pthread_mutex_unlock(&p_msg_q->list_mutex);
- LOC_LOGV("%s: Received message 0x%08X rv = %d\n", __FUNCTION__, *msg_obj, rv);
+ LOC_LOGD("%s: Received message 0x%08X rv = %d\n", __FUNCTION__, *msg_obj, rv);
return rv;
}
diff --git a/utils/platform_lib_abstractions/elapsed_millis_since_boot.cpp b/utils/platform_lib_abstractions/elapsed_millis_since_boot.cpp
deleted file mode 100644
index e8cb93a..0000000
--- a/utils/platform_lib_abstractions/elapsed_millis_since_boot.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <stdlib.h>
-#include <sys/time.h>
-#include "platform_lib_time.h"
-
-int64_t systemTime(int clock)
-{
- struct timeval t;
- t.tv_sec = t.tv_usec = 0;
- gettimeofday(&t, NULL);
- return t.tv_sec*1000000LL + t.tv_usec;
-}
-
-
-int64_t elapsedMillisSinceBoot()
-{
- int64_t t_us = systemTime(0);
- return (int64_t) t_us / 1000LL;
-}
diff --git a/utils/platform_lib_abstractions/platform_lib_includes.h b/utils/platform_lib_abstractions/platform_lib_includes.h
deleted file mode 100644
index 5858674..0000000
--- a/utils/platform_lib_abstractions/platform_lib_includes.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef _PLATFORM_LIB_INCLUDES_H_
-#define _PLATFORM_LIB_INCLUDES_H_
-
-#include "platform_lib_time.h"
-#include "platform_lib_macros.h"
-
-#endif
diff --git a/utils/platform_lib_abstractions/platform_lib_macros.h b/utils/platform_lib_abstractions/platform_lib_macros.h
deleted file mode 100644
index bc48dd9..0000000
--- a/utils/platform_lib_abstractions/platform_lib_macros.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef __PLATFORM_LIB_MACROS_H__
-#define __PLATFORM_LIB_MACROS_H__
-
-#include <sys/time.h>
-
-#define TS_PRINTF(format, x...) \
-{ \
- struct timeval tv; \
- struct timezone tz; \
- int hh, mm, ss; \
- gettimeofday(&tv, &tz); \
- hh = tv.tv_sec/3600%24; \
- mm = (tv.tv_sec%3600)/60; \
- ss = tv.tv_sec%60; \
- fprintf(stdout,"%02d:%02d:%02d.%06ld]" format "\n", hh, mm, ss, tv.tv_usec,##x); \
-}
-
-
-#ifdef USE_GLIB
-
-#define strlcat g_strlcat
-#define strlcpy g_strlcpy
-
-#define ALOGE(format, x...) TS_PRINTF("E/%s (%d): " format , LOG_TAG, getpid(), ##x)
-#define ALOGW(format, x...) TS_PRINTF("W/%s (%d): " format , LOG_TAG, getpid(), ##x)
-#define ALOGI(format, x...) TS_PRINTF("I/%s (%d): " format , LOG_TAG, getpid(), ##x)
-#define ALOGD(format, x...) TS_PRINTF("D/%s (%d): " format , LOG_TAG, getpid(), ##x)
-#define ALOGV(format, x...) TS_PRINTF("V/%s (%d): " format , LOG_TAG, getpid(), ##x)
-
-#define GETTID_PLATFORM_LIB_ABSTRACTION (syscall(SYS_gettid))
-
-#define LOC_EXT_CREATE_THREAD_CB_PLATFORM_LIB_ABSTRACTION createPthread
-#define ELAPSED_MILLIS_SINCE_BOOT_PLATFORM_LIB_ABSTRACTION (elapsedMillisSinceBoot())
-
-
-#else
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-pid_t gettid(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#define GETTID_PLATFORM_LIB_ABSTRACTION (gettid())
-#define LOC_EXT_CREATE_THREAD_CB_PLATFORM_LIB_ABSTRACTION android::AndroidRuntime::createJavaThread
-#define ELAPSED_MILLIS_SINCE_BOOT_PLATFORM_LIB_ABSTRACTION (android::elapsedRealtime())
-
-#endif
-
-#endif
diff --git a/utils/platform_lib_abstractions/platform_lib_time.h b/utils/platform_lib_abstractions/platform_lib_time.h
deleted file mode 100644
index ce013af..0000000
--- a/utils/platform_lib_abstractions/platform_lib_time.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of The Linux Foundation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef _PLATFORM_LIB_TIME_H_
-#define _PLATFORM_LIB_TIME_H_
-
-int64_t systemTime(int clock);
-int64_t elapsedMillisSinceBoot();
-
-#endif