upload android base code part6

This commit is contained in:
August 2018-08-08 17:48:24 +08:00
parent 421e214c7d
commit 4e516ec6ed
35396 changed files with 9188716 additions and 0 deletions

View file

@ -0,0 +1,23 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/target_platform/assert.h"
#ifdef GTEST
MockAssert *gMockAssert;
#endif // GTEST

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cinttypes>
#include "chre_api/chre/re.h"
#include "chre/platform/log.h"
#include "chre/util/macros.h"
DLL_EXPORT void chreLog(enum chreLogLevel level, const char *formatStr, ...) {
char logBuf[512];
va_list args;
va_start(args, formatStr);
vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
va_end(args);
switch (level) {
case CHRE_LOG_ERROR:
LOGE("%s", logBuf);
break;
case CHRE_LOG_WARN:
LOGW("%s", logBuf);
break;
case CHRE_LOG_INFO:
LOGI("%s", logBuf);
break;
case CHRE_LOG_DEBUG:
default:
LOGD("%s", logBuf);
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/context.h"
namespace chre {
bool inEventLoopThread() {
// TODO: Implement this.
return true;
}
} // namespace chre

View file

@ -0,0 +1,28 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/target_platform/fatal_error.h"
#include "chre/platform/shared/platform_log.h"
namespace chre {
void preFatalError() {
// Flush logs before fatally quitting.
chre::PlatformLogSingleton::deinit();
}
} // namespace chre

View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/host_link.h"
namespace chre {
void HostLink::flushMessagesSentByNanoapp(uint64_t appId) {
// TODO: implement
}
bool HostLink::sendMessage(const MessageToHost *message) {
// TODO: implement
return false;
}
} // namespace chre

View file

@ -0,0 +1,89 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_ASSERT_H_
#define CHRE_PLATFORM_LINUX_ASSERT_H_
#include <cassert>
#define CHRE_ASSERT_USES_STDLIB_ASSERT
#ifdef GTEST
#include "chre/platform/log.h"
#include "gmock/gmock.h"
class MockAssert;
extern MockAssert *gMockAssert;
class AssertInterface {
public:
virtual void doAssert() = 0;
};
class MockAssert : public AssertInterface {
public:
MockAssert() {
gMockAssert = this;
}
~MockAssert() {
gMockAssert = nullptr;
}
MOCK_METHOD0(doAssert, void());
};
/**
* Helper macro that wraps a statement in a block that sets up the mock for
* CHRE_ASSERT and expects it to be called at least once. This allows for
* verification that the code to be tested throws an expected assertion failure,
* and also handles the failure gracefully when assertions are compiled out.
* Triggered assertions are logged using LOGI, so they can be manually checked
* in the test output.
*
* Example:
* @code{.cpp}
* TEST(DynamicVector, InsertToSparseIndexFails) {
* DynamicVector<int> vector;
* EXPECT_CHRE_ASSERT(EXPECT_FALSE(vector.insert(5));
* }
* @endcode
*/
#define EXPECT_CHRE_ASSERT(statement) \
do { \
ASSERT_EQ(gMockAssert, nullptr); \
MockAssert chreMockAssert; \
EXPECT_CALL(chreMockAssert, doAssert()).Times(::testing::AtLeast(1)); \
statement; \
} while (0)
#define CHRE_ASSERT(condition) \
do { \
if (gMockAssert != nullptr && !(condition)) { \
LOGI("Mocked assertion " #condition " triggered"); \
gMockAssert->doAssert(); \
} else { \
assert(condition); \
} \
} while (0)
#else // if !defined(GTEST)
#define CHRE_ASSERT(condition) assert(condition)
#endif // GTEST
#endif // CHRE_PLATFORM_LINUX_ASSERT_H_

View file

@ -0,0 +1,35 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_CONDITION_VARIABLE_BASE_H_
#define CHRE_PLATFORM_LINUX_CONDITION_VARIABLE_BASE_H_
#include <condition_variable>
namespace chre {
/**
* Storage for the Linux implementation of the condition variable.
*/
class ConditionVariableBase {
protected:
//! Defer to the std::condition_variable_any implementation.
std::condition_variable_any mConditionVariable;
};
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_CONDITION_VARIABLE_BASE_H_

View file

@ -0,0 +1,42 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_CONDITION_VARIABLE_IMPL_H_
#define CHRE_PLATFORM_LINUX_CONDITION_VARIABLE_IMPL_H_
namespace chre {
inline ConditionVariable::ConditionVariable() {}
inline ConditionVariable::~ConditionVariable() {}
inline void ConditionVariable::notify_one() {
mConditionVariable.notify_one();
}
inline void ConditionVariable::wait(Mutex& mutex) {
mConditionVariable.wait(mutex);
}
inline bool ConditionVariable::wait_for(Mutex& mutex, Nanoseconds timeout) {
std::cv_status result = mConditionVariable.wait_for(
mutex, std::chrono::nanoseconds(timeout.toRawNanoseconds()));
return (result != std::cv_status::timeout);
}
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_CONDITION_VARIABLE_IMPL_H_

View file

@ -0,0 +1,39 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_FATAL_ERROR_H_
#define CHRE_PLATFORM_LINUX_FATAL_ERROR_H_
#include <cstdlib>
#define FATAL_ERROR_QUIT() do { \
chre::preFatalError(); \
abort(); \
} while (0)
namespace chre {
/**
* Do preparation for an impending fatal error including flushing logs.
*
* It must not be possible for FATAL_ERROR() to be called by this function or
* any of its callees.
*/
void preFatalError();
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_FATAL_ERROR_H_

View file

@ -0,0 +1,28 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_HOST_LINK_BASE_H_
#define CHRE_PLATFORM_LINUX_HOST_LINK_BASE_H_
namespace chre {
class HostLinkBase {
// Currently unused
};
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_HOST_LINK_BASE_H_

View file

@ -0,0 +1,48 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_LOG_H_
#define CHRE_PLATFORM_LINUX_LOG_H_
#ifndef __FILENAME__
#define __FILENAME__ __FILE__
#endif
#ifdef GTEST
// When using GoogleTest, just output to stdout since tests are single-threaded.
// GoogleTest complains about multiple threads if the PlatformLogSingleton is
// used.
#include <stdio.h>
#define CHRE_LINUX_LOG(level, color, fmt, ...) \
printf("\e[" color "m%s %s:%d\t" fmt "\e[0m\n", \
level, __FILENAME__, __LINE__, ##__VA_ARGS__)
#else
#include "chre/platform/shared/platform_log.h"
#define CHRE_LINUX_LOG(level, color, fmt, ...) \
::chre::PlatformLogSingleton::get()->log( \
"\e[" color "m%s %s:%d\t" fmt "\e[0m", \
level, __FILENAME__, __LINE__, ##__VA_ARGS__)
#endif
#define LOGE(fmt, ...) CHRE_LINUX_LOG("E", "91", fmt, ##__VA_ARGS__)
#define LOGW(fmt, ...) CHRE_LINUX_LOG("W", "93", fmt, ##__VA_ARGS__)
#define LOGI(fmt, ...) CHRE_LINUX_LOG("I", "96", fmt, ##__VA_ARGS__)
#define LOGD(fmt, ...) CHRE_LINUX_LOG("D", "97", fmt, ##__VA_ARGS__)
#endif // CHRE_PLATFORM_LINUX_LOG_H_

View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_MUTEX_BASE_H_
#define CHRE_PLATFORM_LINUX_MUTEX_BASE_H_
#include <mutex>
/**
* The Linux implementation of MutexBase.
*/
struct MutexBase {
//! When running on Linux we map the mutex implementation to std::mutex.
std::mutex mMutex;
};
#endif // CHRE_PLATFORM_LINUX_MUTEX_BASE_H_

View file

@ -0,0 +1,42 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_MUTEX_BASE_IMPL_H_
#define CHRE_PLATFORM_LINUX_MUTEX_BASE_IMPL_H_
#include "chre/platform/mutex.h"
namespace chre {
inline Mutex::Mutex() {}
inline Mutex::~Mutex() {}
inline void Mutex::lock() {
mMutex.lock();
}
inline bool Mutex::try_lock() {
return mMutex.try_lock();
}
inline void Mutex::unlock() {
mMutex.unlock();
}
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_MUTEX_BASE_IMPL_H_

View file

@ -0,0 +1,32 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_PLATFORM_ID_IMPL_H_
#define CHRE_PLATFORM_LINUX_PLATFORM_ID_IMPL_H_
#include "chre/platform/platform_id.h"
namespace chre {
//! The vendor ID of the Google-provided SLPI platform.
constexpr uint64_t kVendorId = kVendorIdGoogle;
//! The platform ID of the Google-provided SLPI platform.
constexpr uint32_t kPlatformId = kGoogleLinuxPlatformId;
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_PLATFORM_ID_IMPL_H_

View file

@ -0,0 +1,58 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_PLATFORM_LOG_BASE_H_
#define CHRE_PLATFORM_LINUX_PLATFORM_LOG_BASE_H_
#include <condition_variable>
#include <mutex>
#include <thread>
#include <queue>
namespace chre {
/**
* Storage for the Linux implementation of the PlatformLog class.
*/
class PlatformLogBase {
protected:
/**
* A looper method that idles on a condition variable on logs becoming
* available. When logs are available, they are output via std::cout.
*/
void logLooper();
//! The thread that waits on incoming log messages and sends them out to
//! std::cout.
std::thread mLoggerThread;
//! A mutex to guard the shared queue and exit condition of this class.
std::mutex mMutex;
//! The condition variable to signal that the log looper has messages
//! available to output.
std::condition_variable mConditionVariable;
//! A queue of incoming log messages.
std::queue<std::unique_ptr<char>> mLogQueue;
//! A flag to indicate that the logger should shut down.
bool mStopLogger = false;
};
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_PLATFORM_LOG_BASE_H_

View file

@ -0,0 +1,101 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_PLATFORM_NANOAPP_BASE_H_
#define CHRE_PLATFORM_LINUX_PLATFORM_NANOAPP_BASE_H_
#include <cstdint>
#include <string>
#include "chre/platform/shared/nanoapp_support_lib_dso.h"
#include "chre/util/entry_points.h"
namespace chre {
/**
* Linux-specific nanoapp functionality.
*/
class PlatformNanoappBase {
public:
/**
* Associate this Nanoapp with a nanoapp included in a .so that is pre-loaded
* onto the filesystem. Actually loading the .so into memory is done when
* start() is called.
*
* @param filename The name of the .so file in /vendor/lib/dsp that holds this
* nanoapp. This string is not deep-copied, so the memory must remain
* valid for the lifetime of this Nanoapp instance.
*/
void loadFromFile(const std::string& filename);
/**
* Associate this Nanoapp instance with a nanoapp that is statically built
* into the CHRE binary with the given app info structure.
*/
void loadStatic(const struct chreNslNanoappInfo *appInfo);
/**
* @return true if the app's binary data is resident in memory, i.e. a
* previous call to loadFromBuffer() or loadStatic() was successful
*/
bool isLoaded() const;
protected:
//! The app ID we received in the metadata alongside the nanoapp binary. This
//! is also included in (and checked against) mAppInfo.
uint64_t mExpectedAppId;
//! The dynamic shared object (DSO) handle returned by dlopen.
void *mDsoHandle = nullptr;
//! Pointer to the app info structure within this nanoapp
const struct chreNslNanoappInfo *mAppInfo = nullptr;
bool mIsStatic = false;
//! If this is a pre-loaded, but non-static nanoapp (i.e. loaded from
//! loadFromFile), this will be set to the filename string to pass to dlopen()
std::string mFilename;
/**
* Calls through to openNanoappFromFile if the nanoapp was loaded from a
* shared object or returns true if the nanoapp is static.
*
* @return true if the nanoapp was loaded successfully.
*/
bool openNanoapp();
/**
* Calls dlopen on the app filename, and fetches and validates the app info
* pointer. This will result in execution of any on-load handlers (e.g.
* static global constructors) in the nanoapp.
*
* @return true if the app was opened successfully and the app info
* structure passed validation
*/
bool openNanoappFromFile();
/**
* Releases the DSO handle if it was active, by calling dlclose(). This will
* result in execution of any unload handlers in the nanoapp.
*/
void closeNanoapp();
};
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_PLATFORM_NANOAPP_BASE_H_

View file

@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_PLATFORM_SENSOR_BASE_H_
#define CHRE_PLATFORM_LINUX_PLATFORM_SENSOR_BASE_H_
namespace chre {
/**
* Storage for the Linux implementation of the PlatformSensor class.
*/
class PlatformSensorBase {
public:
//! The maximum size of a Linux sensor string.
static constexpr size_t kMaxSensorNameSize = 32;
//! The name of this sensor for the Linux platform.
char sensorName[kMaxSensorNameSize];
};
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_PLATFORM_SENSOR_BASE_H_

View file

@ -0,0 +1,26 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_POWER_CONTROL_MANAGER_BASE_H
#define CHRE_PLATFORM_POWER_CONTROL_MANAGER_BASE_H
namespace chre {
class PowerControlManagerBase {};
} // namespace chre
#endif // CHRE_PLATFORM_POWER_CONTROL_MANAGER_BASE_H

View file

@ -0,0 +1,61 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_STATIC_NANOAPP_INIT_H_
#define CHRE_PLATFORM_LINUX_STATIC_NANOAPP_INIT_H_
#include "chre/core/nanoapp.h"
#include "chre/platform/fatal_error.h"
#include "chre/util/unique_ptr.h"
/**
* Initializes a static nanoapp that is based on the Linux implementation of
* PlatformNanoappBase.
*
* @param appName the name of the nanoapp. This will be prefixed by gNanoapp
* when creating the global instance of the nanoapp.
* @param appId the app's unique 64-bit ID
*/
#define CHRE_STATIC_NANOAPP_INIT(appName, appId_, appVersion_) \
namespace chre { \
\
UniquePtr<Nanoapp> initializeStaticNanoapp##appName() { \
UniquePtr<Nanoapp> nanoapp = MakeUnique<Nanoapp>(); \
static struct chreNslNanoappInfo appInfo; \
appInfo.magic = CHRE_NSL_NANOAPP_INFO_MAGIC; \
appInfo.structMinorVersion = \
CHRE_NSL_NANOAPP_INFO_STRUCT_MINOR_VERSION; \
appInfo.targetApiVersion = CHRE_API_VERSION; \
appInfo.vendor = "Google"; /* TODO: make this configurable */ \
appInfo.name = #appName; \
appInfo.isSystemNanoapp = true; \
appInfo.isTcmNanoapp = false; \
appInfo.appId = appId_; \
appInfo.appVersion = appVersion_; \
appInfo.entryPoints.start = nanoappStart; \
appInfo.entryPoints.handleEvent = nanoappHandleEvent; \
appInfo.entryPoints.end = nanoappEnd; \
if (nanoapp.isNull()) { \
FATAL_ERROR("Failed to allocate nanoapp " #appName); \
} else { \
nanoapp->loadStatic(&appInfo); \
} \
\
return nanoapp; \
} \
} /* namespace chre */
#endif // CHRE_PLATFORM_LINUX_STATIC_NANOAPP_INIT_H_

View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHRE_PLATFORM_LINUX_SYSTEM_TIMER_BASE_H_
#define CHRE_PLATFORM_LINUX_SYSTEM_TIMER_BASE_H_
#include <signal.h>
#include <time.h>
namespace chre {
/**
* The Linux base class for the SystemTimer. The Linux implementation uses a
* POSIX timer.
*/
class SystemTimerBase {
protected:
//! The timer id that is generated during the initialization phase.
timer_t mTimerId;
//! Tracks whether the timer has been initialized correctly.
bool mInitialized = false;
//! A static method that is invoked by the underlying POSIX timer.
static void systemTimerNotifyCallback(union sigval cookie);
//! A utility function to set a POSIX timer.
bool setInternal(uint64_t delayNs);
};
} // namespace chre
#endif // CHRE_PLATFORM_LINUX_SYSTEM_TIMER_BASE_H_

View file

@ -0,0 +1,96 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/core/event.h"
#include "chre/core/event_loop.h"
#include "chre/core/event_loop_manager.h"
#include "chre/core/init.h"
#include "chre/core/nanoapp.h"
#include "chre/core/static_nanoapps.h"
#include "chre/platform/context.h"
#include "chre/platform/log.h"
#include "chre/platform/shared/platform_log.h"
#include "chre/platform/system_timer.h"
#include "chre/util/time.h"
#include <csignal>
#include <tclap/CmdLine.h>
#include <thread>
using chre::EventLoopManagerSingleton;
using chre::Milliseconds;
//! A description of the simulator.
constexpr char kSimDescription[] = "A simulation environment for the Context "
"Hub Runtime Environment (CHRE)";
//! The version of the simulator. This is not super important but is assigned by
//! rules of semantic versioning.
constexpr char kSimVersion[] = "0.1.0";
namespace {
extern "C" void signalHandler(int sig) {
(void) sig;
LOGI("Stop request received");
EventLoopManagerSingleton::get()->getEventLoop().stop();
}
}
int main(int argc, char **argv) {
try {
// Parse command-line arguments.
TCLAP::CmdLine cmd(kSimDescription, ' ', kSimVersion);
TCLAP::SwitchArg no_static_nanoapps_arg("", "no_static_nanoapps",
"disable running static nanoapps", cmd, false);
TCLAP::MultiArg<std::string> nanoapps_arg("", "nanoapp",
"a nanoapp shared object to load and execute", false, "path", cmd);
cmd.parse(argc, argv);
// Initialize the system.
chre::PlatformLogSingleton::init();
chre::init();
// Register a signal handler.
std::signal(SIGINT, signalHandler);
// Load any static nanoapps and start the event loop.
std::thread chreThread([&]() {
// Load static nanoapps unless they are disabled by a command-line flag.
if (!no_static_nanoapps_arg.getValue()) {
chre::loadStaticNanoapps();
}
// Load dynamic nanoapps specified on the command-line.
chre::DynamicVector<chre::UniquePtr<chre::Nanoapp>> dynamicNanoapps;
for (const auto& nanoapp : nanoapps_arg.getValue()) {
dynamicNanoapps.push_back(chre::MakeUnique<chre::Nanoapp>());
dynamicNanoapps.back()->loadFromFile(nanoapp);
EventLoopManagerSingleton::get()->getEventLoop()
.startNanoapp(dynamicNanoapps.back());
}
EventLoopManagerSingleton::get()->getEventLoop().run();
});
chreThread.join();
chre::deinit();
chre::PlatformLogSingleton::deinit();
} catch (TCLAP::ExitException) {}
return 0;
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/memory.h"
#include <stdlib.h>
namespace chre {
void *memoryAlloc(size_t size) {
return malloc(size);
}
void *palSystemApiMemoryAlloc(size_t size) {
return malloc(size);
}
void memoryFree(void *pointer) {
free(pointer);
}
void palSystemApiMemoryFree(void *pointer) {
free(pointer);
}
} // namespace chre

View file

@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/memory_manager.h"
#include "chre/util/memory.h"
namespace chre {
void *MemoryManager::doAlloc(Nanoapp *app, uint32_t bytes) {
return chre::memoryAlloc(bytes);
}
void MemoryManager::doFree(Nanoapp *app, void *ptr) {
chre::memoryFree(ptr);
}
} // namespace chre

View file

@ -0,0 +1,89 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/shared/platform_log.h"
#include <cstdarg>
#include <cstdio>
#include <iostream>
namespace chre {
void PlatformLogBase::logLooper() {
while (1) {
std::unique_ptr<char> logMessage;
{
std::unique_lock<std::mutex> lock(mMutex);
mConditionVariable.wait(lock, [this]{
return (!mLogQueue.empty() || mStopLogger);
});
if (!mLogQueue.empty()) {
// Move the log message to avoid holding a lock for longer than
// required.
logMessage = std::move(mLogQueue.front());
mLogQueue.pop();
} else if (mStopLogger) {
// The stop logger is checked in an else-if to allow the main log queue
// to drain when the logger is stopping.
break;
}
}
// If we get here, there must be a log message to output. This is outside of
// the context of the lock which means that the logging thread will only be
// blocked for the minimum amount of time.
std::cerr << logMessage.get() << std::endl;
}
}
PlatformLog::PlatformLog() {
mLoggerThread = std::thread(&PlatformLog::logLooper, this);
}
PlatformLog::~PlatformLog() {
{
std::unique_lock<std::mutex> lock(mMutex);
mStopLogger = true;
mConditionVariable.notify_one();
}
mLoggerThread.join();
}
void PlatformLog::log(const char *formatStr, ...) {
char *formattedStr;
va_list argList;
va_start(argList, formatStr);
int result = vasprintf(&formattedStr, formatStr, argList);
va_end(argList);
if (result >= 0) {
// Wrap the formatted string into a unique_ptr so that it will be free'd
// once it has been logged.
std::unique_ptr<char> log(formattedStr);
std::unique_lock<std::mutex> lock(mMutex);
mLogQueue.push(std::move(log));
mConditionVariable.notify_one();
} else {
std::cerr << "Failed to allocate log message" << std::endl;
abort();
}
}
} // namespace chre

View file

@ -0,0 +1,139 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/platform_nanoapp.h"
#include <cinttypes>
#include <dlfcn.h>
#include "chre_api/chre/version.h"
#include "chre/platform/assert.h"
#include "chre/platform/log.h"
#include "chre/platform/shared/nanoapp_dso_util.h"
namespace chre {
PlatformNanoapp::~PlatformNanoapp() {
closeNanoapp();
}
bool PlatformNanoapp::start() {
return openNanoapp() && mAppInfo->entryPoints.start();
}
void PlatformNanoapp::handleEvent(uint32_t senderInstanceId,
uint16_t eventType,
const void *eventData) {
mAppInfo->entryPoints.handleEvent(senderInstanceId, eventType, eventData);
}
void PlatformNanoapp::end() {
mAppInfo->entryPoints.end();
closeNanoapp();
}
uint64_t PlatformNanoapp::getAppId() const {
return (mAppInfo == nullptr ? 0 : mAppInfo->appId);
}
uint32_t PlatformNanoapp::getAppVersion() const {
return mAppInfo->appVersion;
}
uint32_t PlatformNanoapp::getTargetApiVersion() const {
return CHRE_API_VERSION;
}
bool PlatformNanoapp::isSystemNanoapp() const {
return true;
}
bool PlatformNanoapp::logStateToBuffer(char *buffer, size_t *bufferPos,
size_t bufferSize) const {
return true;
}
void PlatformNanoappBase::loadFromFile(const std::string& filename) {
CHRE_ASSERT(!isLoaded());
mFilename = filename;
}
void PlatformNanoappBase::loadStatic(const struct chreNslNanoappInfo *appInfo) {
CHRE_ASSERT(!isLoaded());
mIsStatic = true;
mAppInfo = appInfo;
}
bool PlatformNanoappBase::isLoaded() const {
return (mIsStatic || mDsoHandle != nullptr);
}
bool PlatformNanoappBase::openNanoapp() {
bool success = false;
if (mIsStatic) {
success = true;
} else if (!mFilename.empty()) {
success = openNanoappFromFile();
} else {
CHRE_ASSERT(false);
}
return success;
}
bool PlatformNanoappBase::openNanoappFromFile() {
CHRE_ASSERT(!mFilename.empty());
CHRE_ASSERT_LOG(mDsoHandle == nullptr, "Re-opening nanoapp");
bool success = false;
mDsoHandle = dlopen(mFilename.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (mDsoHandle == nullptr) {
LOGE("Failed to load nanoapp from file %s: %s",
mFilename.c_str(), dlerror());
} else {
mAppInfo = static_cast<const struct chreNslNanoappInfo *>(
dlsym(mDsoHandle, CHRE_NSL_DSO_NANOAPP_INFO_SYMBOL_NAME));
if (mAppInfo == nullptr) {
LOGE("Failed to find app info symbol in %s: %s",
mFilename.c_str(), dlerror());
} else {
success = validateAppInfo(0 /* skip ID validation */, 0, mAppInfo,
true /* ignoreAppVersion */);
if (!success) {
mAppInfo = nullptr;
} else {
LOGI("Successfully loaded nanoapp %s (0x%016" PRIx64 ") version 0x%"
PRIx32 " uimg %d system %d from file %s", mAppInfo->name,
mAppInfo->appId, mAppInfo->appVersion, mAppInfo->isTcmNanoapp,
mAppInfo->isSystemNanoapp, mFilename.c_str());
}
}
}
return success;
}
void PlatformNanoappBase::closeNanoapp() {
if (mDsoHandle != nullptr) {
if (dlclose(mDsoHandle) != 0) {
LOGE("dlclose failed: %s", dlerror());
}
mDsoHandle = nullptr;
}
}
} // namespace chre

View file

@ -0,0 +1,23 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/shared/platform_pal.h"
namespace chre {
void PlatformPal::prePalApiCall() {}
} // namespace chre

View file

@ -0,0 +1,84 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/platform_sensor.h"
namespace chre {
PlatformSensor::PlatformSensor(PlatformSensor&& other) {
*this = std::move(other);
}
PlatformSensor::~PlatformSensor() {}
void PlatformSensor::init() {
// TODO: Implement this. Probably we would open some files provided to mock
// sensor data. Perhaps from command-line arguemnts.
}
void PlatformSensor::deinit() {
// TODO: Implement this. Probably we would close the files opened previously
// by init.
}
bool PlatformSensor::getSensors(DynamicVector<Sensor> *sensors) {
CHRE_ASSERT(sensors);
// TODO: Implement this. Perhaps look at all sensor trace files provided and
// return the list of sensor data available.
return false;
}
bool PlatformSensor::applyRequest(const SensorRequest& request) {
// TODO: Implement this. Perhaps consider the request and start to pass in
// sensor samples from mock sensor data once the sensor has transitioned to
// being enabled. Maybe consider resampling input data if the provided mock
// data rate is higher than requested.
return false;
}
SensorType PlatformSensor::getSensorType() const {
// TODO: Implement this.
return SensorType::Unknown;
}
uint64_t PlatformSensor::getMinInterval() const {
// TODO: Implement this.
return 0;
}
const char *PlatformSensor::getSensorName() const {
// TODO: Implement this.
return "";
}
PlatformSensor& PlatformSensor::operator=(PlatformSensor&& other) {
// TODO: Implement this.
return *this;
}
ChreSensorData *PlatformSensor::getLastEvent() const {
// TODO: Implement this.
return nullptr;
}
bool PlatformSensor::getSamplingStatus(
struct chreSensorSamplingStatus *status) const {
// TODO: Implement this.
return false;
}
} // namespace chre

View file

@ -0,0 +1,23 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/power_control_manager.h"
namespace chre {
void PowerControlManager::postEventLoopProcess(size_t numPendingEvents) {}
} // namespace chre

View file

@ -0,0 +1,47 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/system_time.h"
#include <cerrno>
#include <cstring>
#include <ctime>
#include "chre/platform/assert.h"
#include "chre/platform/log.h"
namespace chre {
Nanoseconds SystemTime::getMonotonicTime() {
struct timespec timeNow;
if (clock_gettime(CLOCK_MONOTONIC, &timeNow)) {
CHRE_ASSERT_LOG(false, "Failed to obtain time with error: %s",
strerror(errno));
return Nanoseconds(UINT64_MAX);
}
// The C++11 spec guarantees that tv_sec and tv_nsec only have values >= 0 and
// [0, 999999999]. It is safe to static cast these to their unsigned
// counterpart.
return Seconds(static_cast<uint64_t>(timeNow.tv_sec))
+ Nanoseconds(static_cast<uint64_t>(timeNow.tv_nsec));
}
int64_t SystemTime::getEstimatedHostTimeOffset() {
return 0;
}
} // namespace chre

View file

@ -0,0 +1,137 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chre/platform/system_timer.h"
#include "chre/platform/log.h"
#include "chre/util/time.h"
#include <errno.h>
#include <signal.h>
#include <string.h>
namespace chre {
namespace {
constexpr uint64_t kOneSecondInNanoseconds = 1000000000;
void NanosecondsToTimespec(uint64_t ns, struct timespec *ts) {
ts->tv_sec = ns / kOneSecondInNanoseconds;
ts->tv_nsec = ns % kOneSecondInNanoseconds;
}
} // anonymous namespace
void SystemTimerBase::systemTimerNotifyCallback(union sigval cookie) {
SystemTimer *sysTimer = static_cast<SystemTimer*>(cookie.sival_ptr);
sysTimer->mCallback(sysTimer->mData);
}
SystemTimer::SystemTimer() {}
SystemTimer::~SystemTimer() {
if (mInitialized) {
int ret = timer_delete(mTimerId);
if (ret != 0) {
LOGE("Couldn't delete timer: %s", strerror(errno));
}
mInitialized = false;
}
}
bool SystemTimer::init() {
if (mInitialized) {
LOGW("Tried re-initializing timer");
} else {
struct sigevent sigevt = {};
sigevt.sigev_notify = SIGEV_THREAD;
sigevt.sigev_value.sival_ptr = this;
sigevt.sigev_notify_function = systemTimerNotifyCallback;
sigevt.sigev_notify_attributes = nullptr;
int ret = timer_create(CLOCK_MONOTONIC, &sigevt, &mTimerId);
if (ret != 0) {
LOGE("Couldn't create timer: %s", strerror(errno));
} else {
mInitialized = true;
}
}
return mInitialized;
}
bool SystemTimer::set(SystemTimerCallback *callback, void *data,
Nanoseconds delay) {
// 0 has a special meaning in POSIX, i.e. cancel the timer. In our API, a
// value of 0 just means fire right away.
if (delay.toRawNanoseconds() == 0) {
delay = Nanoseconds(1);
}
if (mInitialized) {
mCallback = callback;
mData = data;
return setInternal(delay.toRawNanoseconds());
} else {
return false;
}
}
bool SystemTimer::cancel() {
if (mInitialized) {
// Setting delay to 0 disarms the timer.
return setInternal(0);
} else {
return false;
}
}
bool SystemTimer::isActive() {
bool isActive = false;
if (mInitialized) {
struct itimerspec spec = {};
int ret = timer_gettime(mTimerId, &spec);
if (ret != 0) {
LOGE("Couldn't obtain current timer configuration: %s", strerror(errno));
}
isActive = (spec.it_value.tv_sec > 0 || spec.it_value.tv_nsec > 0);
}
return isActive;
}
bool SystemTimerBase::setInternal(uint64_t delayNs) {
constexpr int kFlags = 0;
struct itimerspec spec = {};
bool success = false;
NanosecondsToTimespec(delayNs, &spec.it_value);
NanosecondsToTimespec(0, &spec.it_interval);
int ret = timer_settime(mTimerId, kFlags, &spec, nullptr);
if (ret != 0) {
LOGE("Couldn't set timer: %s", strerror(errno));
} else {
LOGD("Set timer to expire in %.f ms", (delayNs / 1000000.0));
success = true;
}
return success;
}
} // namespace chre