diff options
author | Ruifeng Xu <ruifeng@codeaurora.org> | 2017-08-23 17:36:20 -0700 |
---|---|---|
committer | Gerrit - the friendly Code Review server <code-review@localhost> | 2017-10-05 11:33:37 -0700 |
commit | b0982b4476d852410658b307afdeacccd26fb981 (patch) | |
tree | 66ac025eba90552cad12090d7768fbb887a3f638 /utils/LocIpc.h | |
parent | a5c7e2c53da6919499fa29d9bcd77a0b80be16e1 (diff) | |
download | gps-b0982b4476d852410658b307afdeacccd26fb981.tar.gz |
add HAL socket
this patch contains the following changes:
1. added header and implemenation files of socket util apis
in libgps.utils; 2. updated XtraSystemStatusObserver to use
the new apis; 3. added HAL socket and new thread to listen to it.
Change-Id: If1f6b4b4d6ea2d03640f68e96f0286300404f42b
CRs-fixed: 2108635
Diffstat (limited to 'utils/LocIpc.h')
-rw-r--r-- | utils/LocIpc.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/utils/LocIpc.h b/utils/LocIpc.h new file mode 100644 index 0000000..258fd42 --- /dev/null +++ b/utils/LocIpc.h @@ -0,0 +1,88 @@ +/* Copyright (c) 2017 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_SOCKET__ +#define __LOC_SOCKET__ + +#include <string> +#include <memory> +#include <LocThread.h> + +namespace loc_util { + +class LocIpc { +public: + inline LocIpc() : mIpcFd(-1), mStopRequested(false), mRunnable(nullptr) {} + inline virtual ~LocIpc() { stopListening(); } + + // Listen for new messages in current thread. Calling this funciton will + // block current thread. The listening can be stopped by calling stopListening(). + // + // Argument name is the path of the unix local socket to be listened. + // The function will return true on success, and false on failure. + bool startListeningBlocking(const std::string& name); + + // Create a new LocThread and listen for new messages in it. + // Calling this function will return immediately and won't block current thread. + // The listening can be stopped by calling stopListening(). + // + // Argument name is the path of the unix local socket to be be listened. + // The function will return true on success, and false on failure. + bool startListeningNonBlocking(const std::string& name); + + // Stop listening to new messages. + void stopListening(); + + // Callback function for receiving incoming messages. + // Override this function in your derived class to process incoming messages. + // For each received message, this callback function will be called once. + // This callback function will be called in the calling thread of startListeningBlocking + // or in the new LocThread created by startListeningNonBlocking. + // + // Argument data contains the received message. You need to parse it. + virtual void onReceive(const std::string& /*data*/) {} + + // Send out a message. + // Call this function to send a message in argument data to socket in argument name. + // + // Argument name contains the name of the target unix socket. data contains the + // message to be sent out. Convert your message to a string before calling this function. + // The function will return true on success, and false on failure. + bool send(const char name[], const std::string& data); + +private: + int mIpcFd; + bool mStopRequested; + LocThread mThread; + std::unique_ptr<LocRunnable> mRunnable; +}; + +} + +#endif //__LOC_SOCKET__ |