diff options
author | jk7744.park <jk7744.park@samsung.com> | 2015-10-24 16:39:14 +0900 |
---|---|---|
committer | jk7744.park <jk7744.park@samsung.com> | 2015-10-24 16:39:14 +0900 |
commit | 1b52d024163296564409909ecaccdc863ec92bcc (patch) | |
tree | 34d6ac2eb849da63fa475a1221be40df597e022e | |
parent | 29a4e8fe1b6267d525dd449c3be43b80609464b2 (diff) | |
download | device-context-provider-accepted/tizen_2.4_mobile.tar.gz device-context-provider-accepted/tizen_2.4_mobile.tar.bz2 device-context-provider-accepted/tizen_2.4_mobile.zip |
tizen 2.4 releasetizen_2.4_mobile_releasesubmit/tizen_2.4/20151028.064104accepted/tizen/2.4/mobile/20151029.033346tizen_2.4accepted/tizen_2.4_mobile
39 files changed, 3237 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a01ee28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp @@ -0,0 +1,2 @@ +Sanggun Lee <sanggun7.lee@samsung.com> +Mu-Woong Lee <muwoong.lee@samsung.com> diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bf440a4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,93 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(device-context-provider) +INCLUDE(GNUInstallDirs) + +SET(API_PACKAGE device-context-api) + +# Targets +SET(target_provider "ctx-device-provider") + +# Source Lists +FILE(GLOB SRCS src/*.cpp) + +# Dependencies (Base) +SET(provider_deps "context-common vconf capi-system-info capi-system-device capi-system-runtime-info") +SET(provider_deps "${provider_deps} capi-network-bluetooth capi-network-wifi") + +# Mobile profile +IF("${PROFILE}" STREQUAL "mobile") + ADD_DEFINITIONS("-D_MOBILE_") + SET(provider_deps "${provider_deps} capi-telephony tapi msg-service capi-messaging-email motion") + FILE(GLOB_RECURSE SRCS ${SRCS} src/activity/*.cpp) + FILE(GLOB_RECURSE SRCS ${SRCS} src/device_status/*.cpp) + FILE(GLOB_RECURSE SRCS ${SRCS} src/social_status/*.cpp) +ENDIF("${PROFILE}" STREQUAL "mobile") + +# Wearable profile +IF("${PROFILE}" STREQUAL "wearable") + ADD_DEFINITIONS("-D_WEARABLE_") + SET(provider_deps "${provider_deps} capi-telephony tapi msg-service motion") + FILE(GLOB_RECURSE SRCS ${SRCS} src/activity/*.cpp) + FILE(GLOB_RECURSE SRCS ${SRCS} src/device_status/*.cpp) + SET(SRCS ${SRCS} src/social_status/call.cpp) + SET(SRCS ${SRCS} src/social_status/message.cpp) +ENDIF("${PROFILE}" STREQUAL "wearable") + +# TV profile +IF("${PROFILE}" STREQUAL "tv") + ADD_DEFINITIONS("-D_TV_") + SET(SRCS + ${SRCS} + src/device_status/headphone.cpp + src/device_status/wifi.cpp + ) +ENDIF("${PROFILE}" STREQUAL "tv") + +MESSAGE("Sources: ${SRCS}") + +# Common Options +INCLUDE(FindPkgConfig) +INCLUDE_DIRECTORIES( + ${CMAKE_CURRENT_SOURCE_DIR}/include +) +ADD_DEFINITIONS(-g -O2 -Wall -fPIC -fvisibility=hidden -Wl,--as-needed) + + +# Build +pkg_check_modules(provider_pkg REQUIRED ${provider_deps}) + +FOREACH(flag ${provider_pkg_CFLAGS}) + SET(PROVIDER_EXTRA_CFLAGS "${PROVIDER_EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +ADD_LIBRARY(${target_provider} SHARED ${SRCS}) +TARGET_LINK_LIBRARIES(${target_provider} ${provider_pkg_LDFLAGS}) +SET_TARGET_PROPERTIES(${target_provider} PROPERTIES COMPILE_FLAGS ${PROVIDER_EXTRA_CFLAGS}) +SET_TARGET_PROPERTIES(${target_provider} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT-DEVICE\"") +SET_TARGET_PROPERTIES(${target_provider} PROPERTIES SOVERSION ${MAJORVER}) +SET_TARGET_PROPERTIES(${target_provider} PROPERTIES VERSION ${FULLVER}) + + +# Installing +INSTALL(TARGETS ${target_provider} DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT RuntimeLibraries) +INSTALL( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/context-service/internal + FILES_MATCHING PATTERN "*.h" +) + +SET(VERSION ${FULLVER}) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(PC_NAME ${PROJECT_NAME}) +SET(PC_INCLUDE "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/context-service") +SET(PC_LIBDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") +SET(PC_DESCRIPTION "Tizen Device Context Provider") +SET(PC_REQUIRED ${provider_deps}) +SET(PC_LDFLAGS -l${target_provider}) +SET(PC_CFLAGS -I\${includedir}/context-service) + +CONFIGURE_FILE( + ${PROJECT_NAME}.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) @@ -0,0 +1,204 @@ +Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + + 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. + diff --git a/device-context-provider.pc.in b/device-context-provider.pc.in new file mode 100644 index 0000000..378b991 --- /dev/null +++ b/device-context-provider.pc.in @@ -0,0 +1,13 @@ +#Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=@PREFIX@ +libdir=@PC_LIBDIR@ +includedir=@PC_INCLUDE@ + +Name: @PC_NAME@ +Description: @PC_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} diff --git a/include/device_context_provider.h b/include/device_context_provider.h new file mode 100644 index 0000000..09ef1e7 --- /dev/null +++ b/include/device_context_provider.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_DEVICE_CONTEXT_PROVIDER_H__ +#define __CONTEXT_DEVICE_CONTEXT_PROVIDER_H__ + +namespace ctx { + bool init_device_context_provider(); +} + +#endif //__CONTEXT_DEVICE_CONTEXT_PROVIDER_H__ diff --git a/packaging/device-context-provider.manifest b/packaging/device-context-provider.manifest new file mode 100644 index 0000000..a76fdba --- /dev/null +++ b/packaging/device-context-provider.manifest @@ -0,0 +1,5 @@ +<manifest> + <request> + <domain name="_" /> + </request> +</manifest> diff --git a/packaging/device-context-provider.spec b/packaging/device-context-provider.spec new file mode 100644 index 0000000..cbcf6eb --- /dev/null +++ b/packaging/device-context-provider.spec @@ -0,0 +1,92 @@ +Name: device-context-provider +Summary: Device Context Provider +Version: 0.6.2 +Release: 1 +Group: System/Libraries +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz + +%define BUILD_PROFILE %{?profile}%{!?profile:%{?tizen_profile_name}} + +BuildRequires: cmake +BuildRequires: pkgconfig(context-common) +BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(capi-system-device) +BuildRequires: pkgconfig(capi-system-runtime-info) + +BuildRequires: pkgconfig(capi-network-bluetooth) +BuildRequires: pkgconfig(capi-network-wifi) + +%if "%{?BUILD_PROFILE}" == "mobile" +BuildRequires: pkgconfig(capi-telephony) +BuildRequires: pkgconfig(tapi) +BuildRequires: pkgconfig(msg-service) +BuildRequires: pkgconfig(capi-messaging-email) +BuildRequires: pkgconfig(motion) +%endif + +%if "%{?BUILD_PROFILE}" == "wearable" +BuildRequires: pkgconfig(capi-telephony) +BuildRequires: pkgconfig(tapi) +BuildRequires: pkgconfig(msg-service) +BuildRequires: pkgconfig(motion) +%endif + +%description +Device Context Provider + +%prep +%setup -q + +%build +MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` + +export CFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default" +export CXXFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default -Wnon-virtual-dtor -Wno-c++0x-compat" + +export CFLAGS+=" -Wno-unused-parameter -Wno-empty-body" +export CXXFLAGS+=" -Wno-unused-parameter -Wno-empty-body" +export CXXFLAGS+=" -std=c++0x" + +export CFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow -fno-common" +export CXXFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow" + +export CFLAGS+=" -DTIZEN_ENGINEER_MODE" +export CXXFLAGS+=" -DTIZEN_ENGINEER_MODE" +export FFLAGS+=" -DTIZEN_ENGINEER_MODE" + +cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DMAJORVER=${MAJORVER} -DFULLVER=%{version} -DPROFILE=%{?BUILD_PROFILE} +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +mkdir -p %{buildroot}/usr/share/license +cp LICENSE %{buildroot}/usr/share/license/%{name} + +%post +/sbin/ldconfig + +%postun +/sbin/ldconfig + +%files +%manifest packaging/%{name}.manifest +%defattr(-,root,root,-) +%{_libdir}/*.so* +/usr/share/license/%{name} + +%package devel +Summary: Device Context Provider (Development) +Group: System/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Device Context Provider (Development) + +%files devel +%defattr(-,root,root,-) +%{_includedir}/context-service/internal/*.h +%{_libdir}/pkgconfig/%{name}.pc diff --git a/src/activity/activity.h b/src/activity/activity.h new file mode 100644 index 0000000..d0e0951 --- /dev/null +++ b/src/activity/activity.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_ACTIVITY_H_ +#define _DEVICE_STATUS_ACTIVITY_H_ + +#include <system_info.h> +#include "activity_base.h" + +#define GENERATE_ACTIVITY_PROVIDER(act_prvd, act_subj, act_type) \ + class act_prvd : public user_activity_base { \ + public: \ + static context_provider_iface *create(void *data) \ + { \ + CREATE_INSTANCE(ctx::act_prvd); \ + } \ + static void destroy(void *data) \ + { \ + DESTROY_INSTANCE(); \ + } \ + static bool is_supported() \ + { \ + return get_system_info_bool("tizen.org/feature/sensor.activity_recognition"); \ + } \ + static void submit_trigger_item() \ + { \ + context_manager::register_trigger_item((act_subj), OPS_SUBSCRIBE, \ + "{\"Event\":{\"type\":\"string\", \"values\":[\"Detected\"]}}", \ + "{\"Accuracy\":{\"type\":\"string\", \"values\":[\"Low\", \"Normal\", \"High\"]}}" \ + ); \ + } \ + protected: \ + void destroy_self() \ + { \ + destroy(NULL); \ + } \ + private: \ + static act_prvd *__instance; \ + act_prvd() : user_activity_base((act_subj), (act_type)) {} \ + }; \ + ctx::act_prvd *ctx::act_prvd::__instance = NULL; \ + +namespace ctx { + GENERATE_ACTIVITY_PROVIDER(user_activity_stationary, USER_ACT_SUBJ_STATIONARY, ACTIVITY_STATIONARY); + GENERATE_ACTIVITY_PROVIDER(user_activity_walking, USER_ACT_SUBJ_WALKING, ACTIVITY_WALK); + GENERATE_ACTIVITY_PROVIDER(user_activity_running, USER_ACT_SUBJ_RUNNING, ACTIVITY_RUN); + GENERATE_ACTIVITY_PROVIDER(user_activity_in_vehicle, USER_ACT_SUBJ_IN_VEHICLE, ACTIVITY_IN_VEHICLE); +} + +#endif // _DEVICE_STATUS_ACTIVITY_H_ diff --git a/src/activity/activity_base.cpp b/src/activity/activity_base.cpp new file mode 100644 index 0000000..1b834fd --- /dev/null +++ b/src/activity/activity_base.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <types_internal.h> +#include <context_mgr.h> +#include "activity_types.h" +#include "activity_base.h" + +ctx::user_activity_base::user_activity_base(const char *subj, activity_type_e type) + : activity_type(type) + , activity_handle(NULL) + , subject(subj) +{ +} + +ctx::user_activity_base::~user_activity_base() +{ + if (activity_handle) + activity_release(activity_handle); +} + +void ctx::user_activity_base::event_cb(activity_type_e activity, const activity_data_h data, double timestamp, activity_error_e error, void* user_data) +{ + IF_FAIL_VOID_TAG(error == ACTIVITY_ERROR_NONE, _E, "Error: %d", error); + + user_activity_base *instance = static_cast<user_activity_base*>(user_data); + instance->handle_event(activity, data, timestamp); +} + +void ctx::user_activity_base::handle_event(activity_type_e activity, const activity_data_h data, double timestamp) +{ + IF_FAIL_VOID_TAG(activity == activity_type, _E, "Invalid activity: %d", activity); + + ctx::json data_read; + data_read.set(NULL, USER_ACT_EVENT, USER_ACT_DETECTED); + + activity_accuracy_e accuracy = ACTIVITY_ACCURACY_LOW; + activity_get_accuracy(data, &accuracy); + + switch (accuracy) { + case ACTIVITY_ACCURACY_HIGH: + data_read.set(NULL, USER_ACT_ACCURACY, USER_ACT_HIGH); + break; + case ACTIVITY_ACCURACY_MID: + data_read.set(NULL, USER_ACT_ACCURACY, USER_ACT_NORMAL); + break; + default: + data_read.set(NULL, USER_ACT_ACCURACY, USER_ACT_LOW); + break; + } + + context_manager::publish(subject.c_str(), NULL, ERR_NONE, data_read); +} + +int ctx::user_activity_base::subscribe() +{ + IF_FAIL_RETURN(activity_handle == NULL, ERR_NONE); + + _D("Starting to monitor %s", subject.c_str()); + + activity_create(&activity_handle); + IF_FAIL_RETURN_TAG(activity_handle, ERR_OPERATION_FAILED, _E, "Memory allocation failed"); + + int ret = activity_start_recognition(activity_handle, activity_type, event_cb, this); + if (ret != ACTIVITY_ERROR_NONE) { + _E("Recognition starting failed"); + activity_release(activity_handle); + activity_handle = NULL; + return ERR_OPERATION_FAILED; + } + + return ERR_NONE; +} + +int ctx::user_activity_base::unsubscribe() +{ + IF_FAIL_RETURN(activity_handle, ERR_NONE); + + _D("Stop monitoring %s", subject.c_str()); + + activity_stop_recognition(activity_handle); + activity_release(activity_handle); + activity_handle = NULL; + + return ERR_NONE; +} diff --git a/src/activity/activity_base.h b/src/activity/activity_base.h new file mode 100644 index 0000000..45fcc8d --- /dev/null +++ b/src/activity/activity_base.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_ACTIVITY_BASE_H_ +#define _DEVICE_STATUS_ACTIVITY_BASE_H_ + +#include <string> +#include <activity_recognition.h> +#include "../provider_base.h" + +namespace ctx { + + class user_activity_base : public device_provider_base { + public: + int subscribe(); + int unsubscribe(); + + protected: + activity_type_e activity_type; + activity_h activity_handle; + std::string subject; + + user_activity_base(const char *subj, activity_type_e type); + virtual ~user_activity_base(); + + private: + void handle_event(activity_type_e activity, const activity_data_h data, double timestamp); + static void event_cb(activity_type_e activity, const activity_data_h data, double timestamp, activity_error_e error, void* user_data); + }; + +} + +#endif // _DEVICE_STATUS_ACTIVITY_BASE_H_ diff --git a/src/activity/activity_types.h b/src/activity/activity_types.h new file mode 100644 index 0000000..c15bbb4 --- /dev/null +++ b/src/activity/activity_types.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_USER_ACTIVITY_TYPES_H__ +#define __CONTEXT_USER_ACTIVITY_TYPES_H__ + +// Subject +#define USER_ACT_SUBJ_IN_VEHICLE "activity/in_vehicle" +#define USER_ACT_SUBJ_RUNNING "activity/running" +#define USER_ACT_SUBJ_STATIONARY "activity/stationary" +#define USER_ACT_SUBJ_WALKING "activity/walking" + +// Data Key +#define USER_ACT_EVENT "Event" +#define USER_ACT_ACCURACY "Accuracy" + +// Data Value +#define USER_ACT_DETECTED "Detected" +#define USER_ACT_LOW "Low" +#define USER_ACT_NORMAL "Normal" +#define USER_ACT_HIGH "High" + +#endif //__CONTEXT_USER_ACTIVITY_TYPES_H__ diff --git a/src/device_context_provider.cpp b/src/device_context_provider.cpp new file mode 100644 index 0000000..c2c7fba --- /dev/null +++ b/src/device_context_provider.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <types_internal.h> +#include <context_mgr.h> +#include <provider_iface.h> +#include <device_context_provider.h> + +#include "device_status/device_status_types.h" +#include "social_status/social_status_types.h" +#include "activity/activity_types.h" + +#include "device_status/wifi.h" +#include "device_status/headphone.h" + +#ifdef _MOBILE_ +#include "device_status/runtime-info/charger.h" +#include "device_status/runtime-info/gps.h" +#include "device_status/runtime-info/usb.h" +#include "device_status/battery.h" +#include "device_status/psmode.h" +#include "social_status/call.h" +#include "social_status/email.h" +#include "social_status/message.h" +#include "activity/activity.h" +#endif + +#ifdef _WEARABLE_ +#include "device_status/runtime-info/charger.h" +#include "device_status/runtime-info/gps.h" +#include "device_status/runtime-info/usb.h" +#include "device_status/battery.h" +#include "device_status/psmode.h" +#include "social_status/call.h" +#include "social_status/message.h" +#include "activity/activity.h" +#endif + +#define PRIV_NETWORK "network.get" +#define PRIV_TELEPHONY "telephony" +#define PRIV_MESSAGE "message.read" + +template<typename provider> +void register_provider(const char *subject, const char *privilege) +{ + if (!provider::is_supported()) + return; + + ctx::context_provider_info provider_info(provider::create, provider::destroy, NULL, privilege); + ctx::context_manager::register_provider(subject, provider_info); + provider::submit_trigger_item(); +} + +EXTAPI bool ctx::init_device_context_provider() +{ + register_provider<device_status_wifi>(DEVICE_ST_SUBJ_WIFI, PRIV_NETWORK); + register_provider<device_status_headphone>(DEVICE_ST_SUBJ_HEADPHONE, NULL); + +#ifdef _MOBILE_ + register_provider<device_status_charger>(DEVICE_ST_SUBJ_CHARGER, NULL); + register_provider<device_status_gps>(DEVICE_ST_SUBJ_GPS, NULL); + register_provider<device_status_usb>(DEVICE_ST_SUBJ_USB, NULL); + register_provider<device_status_battery>(DEVICE_ST_SUBJ_BATTERY, NULL); + register_provider<device_status_psmode>(DEVICE_ST_SUBJ_PSMODE, NULL); + + register_provider<social_status_call>(SOCIAL_ST_SUBJ_CALL, PRIV_TELEPHONY); + register_provider<social_status_email>(SOCIAL_ST_SUBJ_EMAIL, NULL); + register_provider<social_status_message>(SOCIAL_ST_SUBJ_MESSAGE, PRIV_MESSAGE); + + register_provider<user_activity_stationary>(USER_ACT_SUBJ_STATIONARY, NULL); + register_provider<user_activity_walking>(USER_ACT_SUBJ_WALKING, NULL); + register_provider<user_activity_running>(USER_ACT_SUBJ_RUNNING, NULL); + register_provider<user_activity_in_vehicle>(USER_ACT_SUBJ_IN_VEHICLE, NULL); +#endif + +#ifdef _WEARABLE_ + register_provider<device_status_charger>(DEVICE_ST_SUBJ_CHARGER, NULL); + register_provider<device_status_gps>(DEVICE_ST_SUBJ_GPS, NULL); + register_provider<device_status_usb>(DEVICE_ST_SUBJ_USB, NULL); + register_provider<device_status_battery>(DEVICE_ST_SUBJ_BATTERY, NULL); + register_provider<device_status_psmode>(DEVICE_ST_SUBJ_PSMODE, NULL); + + register_provider<social_status_call>(SOCIAL_ST_SUBJ_CALL, PRIV_TELEPHONY); + register_provider<social_status_message>(SOCIAL_ST_SUBJ_MESSAGE, PRIV_MESSAGE); + + register_provider<user_activity_stationary>(USER_ACT_SUBJ_STATIONARY, NULL); + register_provider<user_activity_walking>(USER_ACT_SUBJ_WALKING, NULL); + register_provider<user_activity_running>(USER_ACT_SUBJ_RUNNING, NULL); + register_provider<user_activity_in_vehicle>(USER_ACT_SUBJ_IN_VEHICLE, NULL); +#endif + + /* Create context providers, which need to be initiated before being subscribed */ + device_status_wifi::create(NULL); + + return true; +} diff --git a/src/device_status/battery.cpp b/src/device_status/battery.cpp new file mode 100644 index 0000000..8a11f8c --- /dev/null +++ b/src/device_status/battery.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <context_mgr.h> +#include "device_status_types.h" +#include "battery.h" + +GENERATE_PROVIDER_COMMON_IMPL(device_status_battery); + +ctx::device_status_battery::device_status_battery() +{ +} + +ctx::device_status_battery::~device_status_battery() +{ +} + +bool ctx::device_status_battery::is_supported() +{ + return true; +} + +void ctx::device_status_battery::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_BATTERY, OPS_SUBSCRIBE | OPS_READ, + "{" + "\"Level\":{\"type\":\"string\",\"values\":[\"Empty\",\"Critical\",\"Low\",\"Normal\",\"High\",\"Full\"]}," + TRIG_BOOL_ITEM_DEF("IsCharging") + "}", + NULL); +} + +void ctx::device_status_battery::update_cb(device_callback_e device_type, void* value, void* user_data) +{ + IF_FAIL_VOID(device_type == DEVICE_CALLBACK_BATTERY_LEVEL); + + device_status_battery *instance = static_cast<device_status_battery*>(user_data); + instance->handle_update(device_type, value); +} + +void ctx::device_status_battery::handle_update(device_callback_e device_type, void* value) +{ + intptr_t level = (intptr_t)value; + + const char* level_string = trans_to_string(level); + IF_FAIL_VOID(level_string); + + ctx::json data_read; + data_read.set(NULL, DEVICE_ST_LEVEL, level_string); + + bool charging_state = false; + int ret = device_battery_is_charging(&charging_state); + IF_FAIL_VOID_TAG(ret == DEVICE_ERROR_NONE, _E, "Getting state failed"); + + data_read.set(NULL, DEVICE_ST_IS_CHARGING, charging_state ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + ctx::context_manager::publish(DEVICE_ST_SUBJ_BATTERY, NULL, ERR_NONE, data_read); +} + +const char* ctx::device_status_battery::trans_to_string(intptr_t level) +{ + switch (level) { + case DEVICE_BATTERY_LEVEL_EMPTY: + return DEVICE_ST_EMPTY; + + case DEVICE_BATTERY_LEVEL_CRITICAL: + return DEVICE_ST_CRITICAL; + + case DEVICE_BATTERY_LEVEL_LOW: + return DEVICE_ST_LOW; + + case DEVICE_BATTERY_LEVEL_HIGH: + return DEVICE_ST_NORMAL; + + case DEVICE_BATTERY_LEVEL_FULL: + { + int percent; + device_battery_get_percent(&percent); + + if (percent == 100) { + return DEVICE_ST_FULL; + } else { + return DEVICE_ST_HIGH; + } + } + break; + + default: + _E("Invalid battery level"); + return NULL; + } +} + +int ctx::device_status_battery::subscribe() +{ + int ret = device_add_callback(DEVICE_CALLBACK_BATTERY_LEVEL, update_cb, this); + IF_FAIL_RETURN(ret == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::device_status_battery::unsubscribe() +{ + int ret = device_remove_callback(DEVICE_CALLBACK_BATTERY_LEVEL, update_cb); + IF_FAIL_RETURN(ret == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::device_status_battery::read() +{ + device_battery_level_e level; + ctx::json data_read; + + int ret = device_battery_get_level_status(&level); + IF_FAIL_RETURN(ret == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED); + + const char* level_string = trans_to_string(level); + IF_FAIL_RETURN(level_string, ERR_OPERATION_FAILED); + + data_read.set(NULL, DEVICE_ST_LEVEL, level_string); + + bool charging_state = false; + ret = device_battery_is_charging(&charging_state); + IF_FAIL_RETURN(ret == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED); + + data_read.set(NULL, DEVICE_ST_IS_CHARGING, charging_state ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_BATTERY, NULL, ERR_NONE, data_read); + return ERR_NONE; +} diff --git a/src/device_status/battery.h b/src/device_status/battery.h new file mode 100644 index 0000000..6fc3fdf --- /dev/null +++ b/src/device_status/battery.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_BATTERY_LEVEL_H_ +#define _DEVICE_STATUS_BATTERY_LEVEL_H_ + +#include <device/callback.h> +#include <device/battery.h> +#include "../provider_base.h" + +namespace ctx { + + class device_status_battery : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(device_status_battery); + + public: + int subscribe(); + int unsubscribe(); + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + device_status_battery(); + ~device_status_battery(); + const char* trans_to_string(intptr_t level); + void handle_update(device_callback_e device_type, void* value); + static void update_cb(device_callback_e device_type, void* value, void* user_data); + }; +} + +#endif // _DEVICE_STATUS_BATTERY_LEVEL_H_ diff --git a/src/device_status/device_status_types.h b/src/device_status/device_status_types.h new file mode 100644 index 0000000..3abff42 --- /dev/null +++ b/src/device_status/device_status_types.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_DEVICESTATUS_TYPES_H__ +#define __CONTEXT_DEVICESTATUS_TYPES_H__ + +// Subject +#define DEVICE_ST_SUBJ_BATTERY "system/battery" +#define DEVICE_ST_SUBJ_CHARGER "system/charger" +#define DEVICE_ST_SUBJ_HEADPHONE "system/headphone" +#define DEVICE_ST_SUBJ_WIFI "system/wifi" +#define DEVICE_ST_SUBJ_USB "system/usb" +#define DEVICE_ST_SUBJ_GPS "system/gps" +#define DEVICE_ST_SUBJ_PSMODE "system/psmode" + +// Data Key +#define DEVICE_ST_EVENT "Event" +#define DEVICE_ST_STATE "State" +#define DEVICE_ST_TYPE "Type" +#define DEVICE_ST_LEVEL "Level" +#define DEVICE_ST_BSSID "BSSID" +#define DEVICE_ST_IS_CONNECTED "IsConnected" +#define DEVICE_ST_IS_ENABLED "IsEnabled" +#define DEVICE_ST_IS_CHARGING "IsCharging" +#define DEVICE_ST_DETECTED "Detected" + +// Data Value +#define DEVICE_ST_TRUE 1 +#define DEVICE_ST_FALSE 0 +#define DEVICE_ST_ENTER "Enter" +#define DEVICE_ST_EXIT "Exit" +#define DEVICE_ST_DISABLED "Disabled" +#define DEVICE_ST_CONNECTED "Connected" +#define DEVICE_ST_UNCONNECTED "Unconnected" +#define DEVICE_ST_SEARCHING "Searching" +#define DEVICE_ST_EMPTY "Empty" +#define DEVICE_ST_CRITICAL "Critical" +#define DEVICE_ST_LOW "Low" +#define DEVICE_ST_NORMAL "Normal" +#define DEVICE_ST_HIGH "High" +#define DEVICE_ST_FULL "Full" +#define DEVICE_ST_HEADSET "Headset" +#define DEVICE_ST_BLUETOOTH "Bluetooth" + +#define TRIG_BOOL_ITEM_DEF(sbj) "\"" sbj "\":{\"type\":\"integer\",\"min\":0,\"max\":1}" + +#endif //__CONTEXT_DEVICESTATUS_TYPES_H__ diff --git a/src/device_status/headphone.cpp b/src/device_status/headphone.cpp new file mode 100644 index 0000000..eb237ee --- /dev/null +++ b/src/device_status/headphone.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <context_mgr.h> +#include "device_status_types.h" +#include "headphone.h" + +#define HANDLING_DELAY 2000 +#define MAX_HANDLING_COUNT 3 + +GENERATE_PROVIDER_COMMON_IMPL(device_status_headphone); + +ctx::device_status_headphone::device_status_headphone() + : connected(false) + , audio_jack_state(RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED) + , bt_audio_state(false) + , bt_audio_callback_on(false) + , bt_event_handler_added(false) + , bt_event_handling_count(0) +{ +} + +ctx::device_status_headphone::~device_status_headphone() +{ +} + +bool ctx::device_status_headphone::is_supported() +{ + return true; +} + +void ctx::device_status_headphone::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_HEADPHONE, OPS_SUBSCRIBE | OPS_READ, + "{" + TRIG_BOOL_ITEM_DEF("IsConnected") "," + "\"Type\":{\"type\":\"string\",\"values\":[\"Normal\",\"Headset\",\"Bluetooth\"]}" + "}", + NULL); +} + +int ctx::device_status_headphone::subscribe() +{ + connected = get_current_status(); + + // Wired headphone + int ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, on_audio_jack_state_changed, this); + IF_FAIL_RETURN(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED); + + // Bluetooth headphone + set_bt_audio_callback(); + + return ERR_NONE; +} + +int ctx::device_status_headphone::unsubscribe() +{ + runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS); + unset_bt_audio_callback(); + + return ERR_NONE; +} + +int ctx::device_status_headphone::read() +{ + if (!being_subscribed) + connected = get_current_status(); + + json data; + generate_data_packet(data); + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_HEADPHONE, NULL, ERR_NONE, data); + + return ERR_NONE; +} + +void ctx::device_status_headphone::set_bt_audio_callback() +{ + IF_FAIL_VOID(!bt_audio_callback_on); + int ret; + + ret = bt_initialize(); + if (ret != BT_ERROR_NONE) { + _W("Bluetooth initialization failed"); + return; + } + + ret = bt_device_set_connection_state_changed_cb(on_bt_connection_changed, this); + if (ret != BT_ERROR_NONE) { + bt_deinitialize(); + return; + } + + bt_audio_callback_on = true; +} + +void ctx::device_status_headphone::unset_bt_audio_callback() +{ + IF_FAIL_VOID(bt_audio_callback_on); + + bt_device_unset_connection_state_changed_cb(); + bt_deinitialize(); + + bt_audio_callback_on = false; +} + +void ctx::device_status_headphone::set_bt_audio_state(bool state) +{ + bt_audio_state = state; +} + +bool ctx::device_status_headphone::get_current_status() +{ + int ret; + + // Wired audio + ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audio_jack_state); + IF_FAIL_RETURN(ret == ERR_NONE, connected); + + // Bluetooth audio + bt_audio_state = false; + ret = bt_initialize(); + if (ret == BT_ERROR_NONE) { + bt_adapter_foreach_bonded_device(on_bt_bond, this); + bt_deinitialize(); + } + + return ((audio_jack_state != RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED) || bt_audio_state); +} + +void ctx::device_status_headphone::generate_data_packet(ctx::json &data) +{ + data.set(NULL, DEVICE_ST_IS_CONNECTED, connected ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + + switch (audio_jack_state) { + case RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE: + data.set(NULL, DEVICE_ST_TYPE, DEVICE_ST_NORMAL); + break; + case RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE: + data.set(NULL, DEVICE_ST_TYPE, DEVICE_ST_HEADSET); + break; + default: + if (bt_audio_state) + data.set(NULL, DEVICE_ST_TYPE, DEVICE_ST_BLUETOOTH); + break; + } +} + +bool ctx::device_status_headphone::handle_event() +{ + bool prev_state = connected; + connected = ((audio_jack_state != RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED) || bt_audio_state); + + IF_FAIL_RETURN(prev_state != connected, false); + + ctx::json data; + generate_data_packet(data); + ctx::context_manager::publish(DEVICE_ST_SUBJ_HEADPHONE, NULL, ERR_NONE, data); + return true; +} + +void ctx::device_status_headphone::handle_audio_jack_event() +{ + int ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audio_jack_state); + IF_FAIL_VOID_TAG(ret == ERR_NONE, _E, "Getting runtime info failed"); + handle_event(); +} + +void ctx::device_status_headphone::on_audio_jack_state_changed(runtime_info_key_e runtime_key, void* user_data) +{ + _D("EarJack"); + ctx::device_status_headphone *instance = static_cast<ctx::device_status_headphone*>(user_data); + instance->handle_audio_jack_event(); +} + +void ctx::device_status_headphone::on_bt_connection_changed(bool connected, bt_device_connection_info_s *conn_info, void *user_data) +{ + ctx::device_status_headphone *instance = static_cast<ctx::device_status_headphone*>(user_data); + IF_FAIL_VOID(connected != instance->bt_audio_state); + IF_FAIL_VOID(!instance->bt_event_handler_added); + + if (connected) { + _D("BT state checking scheduled"); + instance->bt_event_handler_added = true; + instance->bt_event_handling_count = 0; + g_timeout_add(HANDLING_DELAY, handle_bt_event, user_data); + } else { + handle_bt_event(user_data); + } +} + +gboolean ctx::device_status_headphone::handle_bt_event(gpointer data) +{ + _D("BT state checking started"); + ctx::device_status_headphone *instance = static_cast<ctx::device_status_headphone*>(data); + instance->bt_event_handler_added = false; + + instance->set_bt_audio_state(false); + int err = bt_adapter_foreach_bonded_device(on_bt_bond, data); + IF_FAIL_RETURN_TAG(err == BT_ERROR_NONE, FALSE, _E, "bt_adapter_foreach_bonded_device() failed"); + + instance->bt_event_handling_count++; + + if (instance->handle_event() || instance->bt_event_handling_count >= MAX_HANDLING_COUNT) + return FALSE; + + return TRUE; +} + +bool ctx::device_status_headphone::on_bt_bond(bt_device_info_s *device_info, void* user_data) +{ + if (device_info->bt_class.major_device_class != BT_MAJOR_DEVICE_CLASS_AUDIO_VIDEO) + return true; + + bool st = false; + int err = bt_device_is_profile_connected(device_info->remote_address, BT_PROFILE_A2DP, &st); + IF_FAIL_RETURN_TAG(err == BT_ERROR_NONE, false, _E, "bt_device_is_profile_connected() failed"); + + if (st) { + ctx::device_status_headphone *instance = static_cast<ctx::device_status_headphone*>(user_data); + instance->set_bt_audio_state(true); + return false; + } + + return true; +} diff --git a/src/device_status/headphone.h b/src/device_status/headphone.h new file mode 100644 index 0000000..16c2f1e --- /dev/null +++ b/src/device_status/headphone.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_HEADPHONE_H_ +#define _DEVICE_STATUS_HEADPNOHE_H_ + +#include <glib.h> +#include <runtime_info.h> +#include <bluetooth.h> +#include "../provider_base.h" + +namespace ctx { + + class device_status_headphone : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(device_status_headphone); + + public: + int subscribe(); + int unsubscribe(); + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + bool connected; + int audio_jack_state; + bool bt_audio_state; + bool bt_audio_callback_on; + bool bt_event_handler_added; + int bt_event_handling_count; + + device_status_headphone(); + ~device_status_headphone(); + + bool get_current_status(); + void set_bt_audio_callback(); + void unset_bt_audio_callback(); + void set_bt_audio_state(bool state); + + void generate_data_packet(json &data); + bool handle_event(); + void handle_audio_jack_event(); + + static gboolean handle_bt_event(gpointer data); + static void on_audio_jack_state_changed(runtime_info_key_e runtime_key, void* user_data); + static void on_bt_connection_changed(bool connected, bt_device_connection_info_s *conn_info, void *user_data); + static bool on_bt_bond(bt_device_info_s *device_info, void* user_data); + }; +} + +#endif // _DEVICE_STATUS_HEADPHONE_H_ diff --git a/src/device_status/psmode.cpp b/src/device_status/psmode.cpp new file mode 100644 index 0000000..1806c2b --- /dev/null +++ b/src/device_status/psmode.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <context_mgr.h> +#include "device_status_types.h" +#include "psmode.h" + +GENERATE_PROVIDER_COMMON_IMPL(device_status_psmode); + +ctx::device_status_psmode::device_status_psmode() +{ +} + +ctx::device_status_psmode::~device_status_psmode() +{ +} + +bool ctx::device_status_psmode::is_supported() +{ + return true; +} + +void ctx::device_status_psmode::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_PSMODE, OPS_SUBSCRIBE | OPS_READ, + "{" TRIG_BOOL_ITEM_DEF("IsEnabled") "}", NULL); +} + +void ctx::device_status_psmode::update_cb(keynode_t *node, void* user_data) +{ + device_status_psmode *instance = static_cast<device_status_psmode*>(user_data); + instance->handle_update(node); +} + +void ctx::device_status_psmode::handle_update(keynode_t *node) +{ + int status; + ctx::json data_read; + + status = vconf_keynode_get_int(node); + IF_FAIL_VOID_TAG(status >= 0, _E, "Getting state failed"); + + data_read.set(NULL, DEVICE_ST_IS_ENABLED, status == 0 ? DEVICE_ST_FALSE : DEVICE_ST_TRUE); + + context_manager::publish(DEVICE_ST_SUBJ_PSMODE, NULL, ERR_NONE, data_read); +} + +int ctx::device_status_psmode::subscribe() +{ + int ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, update_cb, this); + IF_FAIL_RETURN(ret == VCONF_OK, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::device_status_psmode::unsubscribe() +{ + int ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, update_cb); + IF_FAIL_RETURN(ret == VCONF_OK, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::device_status_psmode::read() +{ + int mode; + int ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &mode); + IF_FAIL_RETURN(ret == VCONF_OK, ERR_OPERATION_FAILED); + + ctx::json data_read; + data_read.set(NULL, DEVICE_ST_IS_ENABLED, mode == 0 ? DEVICE_ST_FALSE : DEVICE_ST_TRUE); + + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_PSMODE, NULL, ERR_NONE, data_read); + return ERR_NONE; +} diff --git a/src/device_status/psmode.h b/src/device_status/psmode.h new file mode 100644 index 0000000..2cc1660 --- /dev/null +++ b/src/device_status/psmode.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_POWER_SAVING_MODE_H_ +#define _DEVICE_STATUS_POWER_SAVING_MODE_H_ + +#include <vconf.h> +#include "../provider_base.h" + +namespace ctx { + + class device_status_psmode : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(device_status_psmode); + + public: + int subscribe(); + int unsubscribe(); + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + device_status_psmode(); + ~device_status_psmode(); + void handle_update(keynode_t *node); + static void update_cb(keynode_t *node, void* user_data); + }; +} + +#endif // _DEVICE_STATUS_POWER_SAVING_H_ diff --git a/src/device_status/runtime-info/base_rtinfo.cpp b/src/device_status/runtime-info/base_rtinfo.cpp new file mode 100644 index 0000000..2d3f0e3 --- /dev/null +++ b/src/device_status/runtime-info/base_rtinfo.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 "base_rtinfo.h" + +ctx::device_status_runtime_info::device_status_runtime_info(runtime_info_key_e key) + : info_key(key) +{ +} + +runtime_info_key_e ctx::device_status_runtime_info::get_info_key() +{ + return info_key; +} + +void ctx::device_status_runtime_info::update_cb(runtime_info_key_e key, void* user_data) +{ + device_status_runtime_info *instance = static_cast<device_status_runtime_info*>(user_data); + IF_FAIL_VOID_TAG(key == instance->get_info_key(), _W, "Runtime info key mismatch"); + instance->handle_update(); +} + +int ctx::device_status_runtime_info::subscribe() +{ + int ret = runtime_info_set_changed_cb(info_key, update_cb, this); + IF_FAIL_RETURN(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::device_status_runtime_info::unsubscribe() +{ + int ret = runtime_info_unset_changed_cb(info_key); + IF_FAIL_RETURN(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED); + return ERR_NONE; +} diff --git a/src/device_status/runtime-info/base_rtinfo.h b/src/device_status/runtime-info/base_rtinfo.h new file mode 100644 index 0000000..29480c5 --- /dev/null +++ b/src/device_status/runtime-info/base_rtinfo.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_DEVICE_STATUS_RUNTIME_INFO_H__ +#define __CONTEXT_DEVICE_STATUS_RUNTIME_INFO_H__ + +#include <runtime_info.h> +#include "../../provider_base.h" + +namespace ctx { + + class device_status_runtime_info : public device_provider_base { + public: + device_status_runtime_info(runtime_info_key_e key); + + int subscribe(); + int unsubscribe(); + virtual int read() = 0; + + protected: + runtime_info_key_e info_key; + + virtual ~device_status_runtime_info(){} + static void update_cb(runtime_info_key_e runtime_key, void* user_data); + virtual void handle_update() = 0; + + private: + runtime_info_key_e get_info_key(); + }; +} + +#endif diff --git a/src/device_status/runtime-info/charger.cpp b/src/device_status/runtime-info/charger.cpp new file mode 100644 index 0000000..85cd290 --- /dev/null +++ b/src/device_status/runtime-info/charger.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <context_mgr.h> +#include "../device_status_types.h" +#include "charger.h" + +GENERATE_PROVIDER_COMMON_IMPL(device_status_charger); + +ctx::device_status_charger::device_status_charger() + : device_status_runtime_info(RUNTIME_INFO_KEY_CHARGER_CONNECTED) +{ +} + +ctx::device_status_charger::~device_status_charger() +{ +} + +bool ctx::device_status_charger::is_supported() +{ + return true; +} + +void ctx::device_status_charger::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_CHARGER, OPS_SUBSCRIBE | OPS_READ, + "{" TRIG_BOOL_ITEM_DEF("IsConnected") "}", NULL); +} + +void ctx::device_status_charger::handle_update() +{ + bool charger_status = false; + + int ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &charger_status); + IF_FAIL_VOID_TAG(ret == RUNTIME_INFO_ERROR_NONE, _E, "Getting runtime info failed"); + + ctx::json data_read; + data_read.set(NULL, DEVICE_ST_IS_CONNECTED, charger_status ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + + context_manager::publish(DEVICE_ST_SUBJ_CHARGER, NULL, ERR_NONE, data_read); +} + +int ctx::device_status_charger::read() +{ + bool charger_status = false; + ctx::json data_read; + + int ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &charger_status); + IF_FAIL_RETURN_TAG(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Getting runtime info failed"); + + data_read.set(NULL, DEVICE_ST_IS_CONNECTED, charger_status ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_CHARGER, NULL, ERR_NONE, data_read); + return ERR_NONE; +} diff --git a/src/device_status/runtime-info/charger.h b/src/device_status/runtime-info/charger.h new file mode 100644 index 0000000..52aeb4c --- /dev/null +++ b/src/device_status/runtime-info/charger.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_CHARGER_H_ +#define _DEVICE_STATUS_CHARGER_H_ + +#include "base_rtinfo.h" + +namespace ctx { + + class device_status_charger : public device_status_runtime_info { + + GENERATE_PROVIDER_COMMON_DECL(device_status_charger); + + public: + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + device_status_charger(); + ~device_status_charger(); + void handle_update(); + }; +} + +#endif // _DEVICE_STATUS_CHARGER_H_ diff --git a/src/device_status/runtime-info/gps.cpp b/src/device_status/runtime-info/gps.cpp new file mode 100644 index 0000000..bf8b251 --- /dev/null +++ b/src/device_status/runtime-info/gps.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <context_mgr.h> +#include "../device_status_types.h" +#include "gps.h" + +GENERATE_PROVIDER_COMMON_IMPL(device_status_gps); + +static const char* get_state_string(int gps_state) +{ + switch (gps_state) { + case RUNTIME_INFO_GPS_STATUS_DISABLED: + return DEVICE_ST_DISABLED; + + case RUNTIME_INFO_GPS_STATUS_SEARCHING: + return DEVICE_ST_SEARCHING; + + case RUNTIME_INFO_GPS_STATUS_CONNECTED: + return DEVICE_ST_CONNECTED; + + default: + _E("Unknown GPS status: %d", gps_state); + return NULL; + } +} + +ctx::device_status_gps::device_status_gps() + : device_status_runtime_info(RUNTIME_INFO_KEY_GPS_STATUS) +{ +} + +ctx::device_status_gps::~device_status_gps() +{ +} + +bool ctx::device_status_gps::is_supported() +{ + return get_system_info_bool("tizen.org/feature/location.gps"); +} + +void ctx::device_status_gps::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_GPS, OPS_SUBSCRIBE | OPS_READ, + "{" + "\"State\":{\"type\":\"string\",\"values\":[\"Disabled\",\"Searching\",\"Connected\"]}" + "}", + NULL); +} + +void ctx::device_status_gps::handle_update() +{ + int gps_status; + int ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_GPS_STATUS, &gps_status); + IF_FAIL_VOID_TAG(ret == RUNTIME_INFO_ERROR_NONE, _E, "Getting runtime info failed"); + + ctx::json data_read; + + const char* state_str = get_state_string(gps_status); + IF_FAIL_VOID(state_str); + + data_read.set(NULL, DEVICE_ST_STATE, state_str); + + context_manager::publish(DEVICE_ST_SUBJ_GPS, NULL, ERR_NONE, data_read); +} + +int ctx::device_status_gps::read() +{ + int gps_status; + ctx::json data_read; + + int ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_GPS_STATUS, &gps_status); + IF_FAIL_RETURN_TAG(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Getting runtime info failed"); + + const char* state_str = get_state_string(gps_status); + IF_FAIL_RETURN(state_str, ERR_OPERATION_FAILED); + + data_read.set(NULL, DEVICE_ST_STATE, state_str); + + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_GPS, NULL, ERR_NONE, data_read); + return ERR_NONE; +} diff --git a/src/device_status/runtime-info/gps.h b/src/device_status/runtime-info/gps.h new file mode 100644 index 0000000..4d94cc0 --- /dev/null +++ b/src/device_status/runtime-info/gps.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_GPS_H_ +#define _DEVICE_STATUS_GPS_H_ + +#include "base_rtinfo.h" + +namespace ctx { + + class device_status_gps : public device_status_runtime_info { + + GENERATE_PROVIDER_COMMON_DECL(device_status_gps); + + public: + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + device_status_gps(); + ~device_status_gps(); + void handle_update(); + }; +} + +#endif // _DEVICE_STATUS_GPS_H_ diff --git a/src/device_status/runtime-info/usb.cpp b/src/device_status/runtime-info/usb.cpp new file mode 100644 index 0000000..5f92307 --- /dev/null +++ b/src/device_status/runtime-info/usb.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <context_mgr.h> +#include "../device_status_types.h" +#include "usb.h" + +GENERATE_PROVIDER_COMMON_IMPL(device_status_usb); + +ctx::device_status_usb::device_status_usb() + : device_status_runtime_info(RUNTIME_INFO_KEY_USB_CONNECTED) +{ +} + +ctx::device_status_usb::~device_status_usb() +{ +} + +bool ctx::device_status_usb::is_supported() +{ + return get_system_info_bool("tizen.org/feature/usb.host"); +} + +void ctx::device_status_usb::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_USB, OPS_SUBSCRIBE | OPS_READ, + "{" TRIG_BOOL_ITEM_DEF("IsConnected") "}", NULL); +} + +void ctx::device_status_usb::handle_update() +{ + bool status = false; + + int ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &status); + IF_FAIL_VOID_TAG(ret == RUNTIME_INFO_ERROR_NONE, _E, "Getting runtime info failed"); + + ctx::json data_read; + data_read.set(NULL, DEVICE_ST_IS_CONNECTED, status ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + + context_manager::publish(DEVICE_ST_SUBJ_USB, NULL, ERR_NONE, data_read); +} + +int ctx::device_status_usb::read() +{ + bool status = false; + ctx::json data_read; + + int ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &status); + IF_FAIL_RETURN_TAG(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Getting runtime info failed"); + + data_read.set(NULL, DEVICE_ST_IS_CONNECTED, status ? DEVICE_ST_TRUE : DEVICE_ST_FALSE); + + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_USB, NULL, ERR_NONE, data_read); + return ERR_NONE; +} diff --git a/src/device_status/runtime-info/usb.h b/src/device_status/runtime-info/usb.h new file mode 100644 index 0000000..bc01425 --- /dev/null +++ b/src/device_status/runtime-info/usb.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _DEVICE_STATUS_USB_H_ +#define _DEVICE_STATUS_USB_H_ + +#include "base_rtinfo.h" + +namespace ctx { + + class device_status_usb : public device_status_runtime_info { + + GENERATE_PROVIDER_COMMON_DECL(device_status_usb); + + public: + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + device_status_usb(); + ~device_status_usb(); + void handle_update(); + }; +} + +#endif // _DEVICE_STATUS_USB_H_ diff --git a/src/device_status/wifi.cpp b/src/device_status/wifi.cpp new file mode 100644 index 0000000..fa6ae49 --- /dev/null +++ b/src/device_status/wifi.cpp @@ -0,0 +1,266 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <shared_vars.h> +#include <context_mgr.h> +#include "device_status_types.h" +#include "wifi.h" + +ctx::device_status_wifi *ctx::device_status_wifi::__instance = NULL; + +ctx::device_status_wifi::device_status_wifi() + : last_state(_UNKNOWN) + , is_initialized(false) + , is_activated(false) +{ + IF_FAIL_VOID_TAG(start_monitor(), _W, "WiFi monitor initialization failed"); + + if (!get_current_state()) { + stop_monitor(); + _W("Getting current WiFi status failed"); + } +} + +ctx::device_status_wifi::~device_status_wifi() +{ +} + +ctx::context_provider_iface *ctx::device_status_wifi::create(void *data) +{ + CREATE_INSTANCE(device_status_wifi); +} + +void ctx::device_status_wifi::destroy(void *data) +{ + __instance->stop_monitor(); + DESTROY_INSTANCE(); +} + +void ctx::device_status_wifi::destroy_self() +{ + /* WiFi status will be monitored continuously, even if no client is subscribing it */ +} + +bool ctx::device_status_wifi::is_supported() +{ + return get_system_info_bool("tizen.org/feature/network.wifi"); +} + +void ctx::device_status_wifi::submit_trigger_item() +{ + context_manager::register_trigger_item(DEVICE_ST_SUBJ_WIFI, OPS_SUBSCRIBE | OPS_READ, + "{" + "\"State\":{\"type\":\"string\",\"values\":[\"Disabled\",\"Unconnected\",\"Connected\"]}," + "\"BSSID\":{\"type\":\"string\"}" + "}", + NULL); +} + +bool ctx::device_status_wifi::get_current_state() +{ + int err; + + if (!is_initialized) { + err = wifi_initialize(); + IF_FAIL_RETURN_TAG(err == WIFI_ERROR_NONE, false, _E, "wifi_initialize() failed"); + } + + err = wifi_is_activated(&is_activated); + IF_FAIL_RETURN_TAG(err == WIFI_ERROR_NONE, false, _E, "wifi_is_activated() failed"); + + err = wifi_get_connection_state(&conn_state); + IF_FAIL_RETURN_TAG(err == WIFI_ERROR_NONE, false, _E, "wifi_get_connection_state() failed"); + + if (is_activated) { + if (conn_state == WIFI_CONNECTION_STATE_CONNECTED) { + last_state = _CONNECTED; + get_bssid(); + } else { + last_state = _UNCONNECTED; + clear_bssid(); + } + } else { + last_state = _DISABLED; + clear_bssid(); + } + + if (!is_initialized) + wifi_deinitialize(); + + return true; +} + +bool ctx::device_status_wifi::get_bssid() +{ + int err; + char *str_buf = NULL; + wifi_ap_h ap = NULL; + + err = wifi_get_connected_ap(&ap); + IF_FAIL_RETURN_TAG(err == WIFI_ERROR_NONE, false, _E, "wifi_get_connected_ap() failed"); + + wifi_ap_get_bssid(ap, &str_buf); + bssid = (str_buf != NULL ? str_buf : ""); + g_free(str_buf); + + wifi_ap_destroy(ap); + + if (bssid.empty()) + _W("Failed to get BSSID"); + + ctx::shared::wifi_bssid = bssid; + _D("BSSID: %s", bssid.c_str()); + + return !bssid.empty(); +} + +void ctx::device_status_wifi::clear_bssid() +{ + bssid.clear(); + ctx::shared::wifi_bssid.clear(); + _D("No WiFi connection"); +} + +bool ctx::device_status_wifi::get_response_packet(ctx::json &data) +{ + switch (last_state) { + case _DISABLED: + data.set(NULL, DEVICE_ST_STATE, DEVICE_ST_DISABLED); + break; + + case _UNCONNECTED: + data.set(NULL, DEVICE_ST_STATE, DEVICE_ST_UNCONNECTED); + break; + + case _CONNECTED: + data.set(NULL, DEVICE_ST_STATE, DEVICE_ST_CONNECTED); + data.set(NULL, DEVICE_ST_BSSID, bssid); + break; + + default: + return false; + } + + return true; +} + +int ctx::device_status_wifi::read() +{ + IF_FAIL_RETURN(get_current_state(), ERR_OPERATION_FAILED); + + ctx::json data_read; + + if (get_response_packet(data_read)) { + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_WIFI, NULL, ERR_NONE, data_read); + return ERR_NONE; + } + + return ERR_OPERATION_FAILED; +} + +bool ctx::device_status_wifi::start_monitor() +{ + IF_FAIL_RETURN(!is_initialized, true); + + int err; + err = wifi_initialize(); + IF_FAIL_RETURN_TAG(err == WIFI_ERROR_NONE, false, _E, "wifi_initialize() failed"); + + err = wifi_set_device_state_changed_cb(device_state_changed_cb, this); + IF_FAIL_CATCH_TAG(err == WIFI_ERROR_NONE, _E, "wifi_set_device_state_changed_cb() failed"); + + err = wifi_set_connection_state_changed_cb(connection_state_changed_cb, this); + IF_FAIL_CATCH_TAG(err == WIFI_ERROR_NONE, _E, "wifi_set_connection_state_changed_cb() failed"); + + is_initialized = true; + return true; + +CATCH: + wifi_deinitialize(); + return false; +} + +void ctx::device_status_wifi::stop_monitor() +{ + IF_FAIL_VOID(is_initialized); + + wifi_unset_device_state_changed_cb(); + wifi_unset_connection_state_changed_cb(); + wifi_deinitialize(); + is_initialized = false; +} + +int ctx::device_status_wifi::subscribe() +{ +#if 0 + IF_FAIL_RETURN(start_monitor(), ERR_OPERATION_FAILED); + if (!get_current_state()) { + stop_monitor(); + return ERR_OPERATION_FAILED; + } +#endif + + return ERR_NONE; +} + +int ctx::device_status_wifi::unsubscribe() +{ +#if 0 + stop_monitor(); +#endif + return ERR_NONE; +} + +void ctx::device_status_wifi::aggregate_updated_data() +{ + int prev_state = last_state; + + if (is_activated) { + if (conn_state == WIFI_CONNECTION_STATE_CONNECTED) { + last_state = _CONNECTED; + } else { + last_state = _UNCONNECTED; + } + } else { + last_state = _DISABLED; + } + + if (last_state != prev_state) { + if (last_state == _CONNECTED) { + get_bssid(); + } else { + clear_bssid(); + } + + ctx::json data; + if (being_subscribed && get_response_packet(data)) + context_manager::publish(DEVICE_ST_SUBJ_WIFI, NULL, ERR_NONE, data); + } +} + +void ctx::device_status_wifi::device_state_changed_cb(wifi_device_state_e state, void *user_data) +{ + device_status_wifi *instance = static_cast<device_status_wifi*>(user_data); + instance->is_activated = (state == WIFI_DEVICE_STATE_ACTIVATED); + instance->aggregate_updated_data(); +} + +void ctx::device_status_wifi::connection_state_changed_cb(wifi_connection_state_e state, wifi_ap_h ap, void *user_data) +{ + device_status_wifi *instance = static_cast<device_status_wifi*>(user_data); + instance->conn_state = state; + instance->aggregate_updated_data(); +} diff --git a/src/device_status/wifi.h b/src/device_status/wifi.h new file mode 100644 index 0000000..6dc505d --- /dev/null +++ b/src/device_status/wifi.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_DEVICE_STATUS_WIFI_H__ +#define __CONTEXT_DEVICE_STATUS_WIFI_H__ + +#include <string> +#include <wifi.h> +#include "../provider_base.h" + +namespace ctx { + + class device_status_wifi : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(device_status_wifi); + + public: + int subscribe(); + int unsubscribe(); + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + enum _state_e { + _UNKNOWN = -1, + _DISABLED = 0, + _UNCONNECTED, + _CONNECTED, + }; + + int last_state; + bool is_initialized; + bool is_activated; + wifi_connection_state_e conn_state; + std::string bssid; + + device_status_wifi(); + ~device_status_wifi(); + + bool get_current_state(); + bool get_bssid(); + void clear_bssid(); + bool get_response_packet(json &data); + void aggregate_updated_data(); + bool start_monitor(); + void stop_monitor(); + static void device_state_changed_cb(wifi_device_state_e state, void *user_data); + static void connection_state_changed_cb(wifi_connection_state_e state, wifi_ap_h ap, void *user_data); + }; +} + +#endif diff --git a/src/provider_base.cpp b/src/provider_base.cpp new file mode 100644 index 0000000..acc61cd --- /dev/null +++ b/src/provider_base.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <system_info.h> +#include "provider_base.h" + +ctx::device_provider_base::device_provider_base() + : being_subscribed(false) +{ +} + +int ctx::device_provider_base::subscribe(const char *subject, ctx::json option, ctx::json *request_result) +{ + IF_FAIL_RETURN(!being_subscribed, ERR_NONE); + + int ret = subscribe(); + + if (ret == ERR_NONE) + being_subscribed = true; + else + destroy_self(); + + return ret; +} + +int ctx::device_provider_base::unsubscribe(const char *subject, ctx::json option) +{ + int ret = ERR_NONE; + + if (being_subscribed) + ret = unsubscribe(); + + destroy_self(); + return ret; +} + +int ctx::device_provider_base::read(const char *subject, ctx::json option, ctx::json *request_result) +{ + int ret = read(); + + if (!being_subscribed) + destroy_self(); + + return ret; +} + +int ctx::device_provider_base::write(const char *subject, ctx::json data, ctx::json *request_result) +{ + int ret = write(); + + if (!being_subscribed) + destroy_self(); + + return ret; +} + +int ctx::device_provider_base::subscribe() +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::device_provider_base::unsubscribe() +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::device_provider_base::read() +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::device_provider_base::write() +{ + return ERR_NOT_SUPPORTED; +} + +bool ctx::device_provider_base::get_system_info_bool(const char *key) +{ + bool supported = false; + int ret = system_info_get_platform_bool(key, &supported); + IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, false, _E, "system_info_get_platform_bool() failed"); + return supported; +} diff --git a/src/provider_base.h b/src/provider_base.h new file mode 100644 index 0000000..c05f4c7 --- /dev/null +++ b/src/provider_base.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_DEVICE_PROVIDER_BASE_H__ +#define __CONTEXT_DEVICE_PROVIDER_BASE_H__ + +#include <types_internal.h> +#include <json.h> +#include <provider_iface.h> + +#define CREATE_INSTANCE(prvd) \ + do { \ + IF_FAIL_RETURN(!__instance, __instance); \ + __instance = new(std::nothrow) prvd(); \ + IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed"); \ + _I(BLUE("Created")); \ + return __instance; \ + } while (0) \ + +#define DESTROY_INSTANCE() \ + do { \ + IF_FAIL_VOID(__instance); \ + delete __instance; \ + __instance = NULL; \ + _I(BLUE("Destroyed")); \ + } while (0) \ + +#define GENERATE_PROVIDER_COMMON_DECL(prvd) \ + public: \ + static context_provider_iface *create(void *data); \ + static void destroy(void *data); \ + protected: \ + void destroy_self(); \ + private: \ + static prvd *__instance; \ + +#define GENERATE_PROVIDER_COMMON_IMPL(prvd) \ + ctx::prvd *ctx::prvd::__instance = NULL; \ + ctx::context_provider_iface *ctx::prvd::create(void *data) \ + { \ + CREATE_INSTANCE(prvd); \ + } \ + void ctx::prvd::destroy(void *data) \ + { \ + DESTROY_INSTANCE(); \ + } \ + void ctx::prvd::destroy_self() \ + { \ + destroy(NULL); \ + } \ + +namespace ctx { + + class device_provider_base : public context_provider_iface { + public: + int subscribe(const char *subject, ctx::json option, ctx::json *request_result); + int unsubscribe(const char *subject, ctx::json option); + int read(const char *subject, ctx::json option, ctx::json *request_result); + int write(const char *subject, ctx::json data, ctx::json *request_result); + + protected: + bool being_subscribed; + + device_provider_base(); + virtual ~device_provider_base() {} + + virtual int subscribe(); + virtual int unsubscribe(); + virtual int read(); + virtual int write(); + virtual void destroy_self() = 0; + + static bool get_system_info_bool(const char *key); + }; +} + +#endif diff --git a/src/social_status/call.cpp b/src/social_status/call.cpp new file mode 100644 index 0000000..39d793b --- /dev/null +++ b/src/social_status/call.cpp @@ -0,0 +1,387 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <stdlib.h> +#include <json.h> +#include <context_mgr.h> +#include "social_status_types.h" +#include "call.h" + +#define TELEPHONY_NOTI_ID_CNT 8 +GENERATE_PROVIDER_COMMON_IMPL(social_status_call); + +static bool telephony_initialized = false; +static telephony_noti_e call_noti_ids[] = +{ + TELEPHONY_NOTI_VOICE_CALL_STATUS_IDLE, + TELEPHONY_NOTI_VOICE_CALL_STATUS_ACTIVE, +// TELEPHONY_NOTI_VOICE_CALL_STATUS_HELD, +// TELEPHONY_NOTI_VOICE_CALL_STATUS_DIALING, + TELEPHONY_NOTI_VOICE_CALL_STATUS_ALERTING, + TELEPHONY_NOTI_VOICE_CALL_STATUS_INCOMING, + TELEPHONY_NOTI_VIDEO_CALL_STATUS_IDLE, + TELEPHONY_NOTI_VIDEO_CALL_STATUS_ACTIVE, +// TELEPHONY_NOTI_VIDEO_CALL_STATUS_DIALING, + TELEPHONY_NOTI_VIDEO_CALL_STATUS_ALERTING, + TELEPHONY_NOTI_VIDEO_CALL_STATUS_INCOMING, +}; +static ctx::json latest; + +ctx::social_status_call::social_status_call() +{ +} + +ctx::social_status_call::~social_status_call() +{ +} + +bool ctx::social_status_call::is_supported() +{ + return get_system_info_bool("tizen.org/feature/network.telephony"); +} + +void ctx::social_status_call::submit_trigger_item() +{ + context_manager::register_trigger_item(SOCIAL_ST_SUBJ_CALL, OPS_SUBSCRIBE | OPS_READ, + "{" + "\"Medium\":{\"type\":\"string\",\"values\":[\"Voice\",\"Video\"]}," + "\"State\":{\"type\":\"string\",\"values\":[\"Idle\",\"Connecting\",\"Connected\"]}," + "\"Address\":{\"type\":\"string\"}" + "}", + NULL); + //TODO remove Connecting, Connected +} + +void ctx::social_status_call::call_event_cb(telephony_h handle, telephony_noti_e noti_id, void *data, void *user_data) +{ + social_status_call *instance = static_cast<social_status_call*>(user_data); + instance->handle_call_event(handle, noti_id, data); +} + +void ctx::social_status_call::handle_call_event(telephony_h handle, telephony_noti_e noti_id, void* id) +{ + + json data; + unsigned int count; + telephony_call_h *call_list; + + // Call state + switch (noti_id) { + case TELEPHONY_NOTI_VOICE_CALL_STATUS_IDLE: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_IDLE: + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_IDLE); + break; + case TELEPHONY_NOTI_VOICE_CALL_STATUS_ACTIVE: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_ACTIVE: + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_ACTIVE); + break; + case TELEPHONY_NOTI_VOICE_CALL_STATUS_ALERTING: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_ALERTING: + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_ALERTING); + break; + case TELEPHONY_NOTI_VOICE_CALL_STATUS_INCOMING: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_INCOMING: + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_INCOMING); + break; +/* // Ignore below cases + case TELEPHONY_NOTI_VOICE_CALL_STATUS_HELD: + case TELEPHONY_NOTI_VOICE_CALL_STATUS_DIALING: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_DIALING:*/ + default: + _E("Unkown noti id: %d", noti_id); + return; + } + + // Call type + switch (noti_id) { + case TELEPHONY_NOTI_VOICE_CALL_STATUS_IDLE: + case TELEPHONY_NOTI_VOICE_CALL_STATUS_ACTIVE: + case TELEPHONY_NOTI_VOICE_CALL_STATUS_ALERTING: + case TELEPHONY_NOTI_VOICE_CALL_STATUS_INCOMING: + data.set(NULL, SOCIAL_ST_TYPE, SOCIAL_ST_VOICE); + break; + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_IDLE: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_ACTIVE: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_ALERTING: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_INCOMING: + data.set(NULL, SOCIAL_ST_TYPE, SOCIAL_ST_VIDEO); + break; +/* // Ignore below cases + case TELEPHONY_NOTI_VOICE_CALL_STATUS_HELD: + case TELEPHONY_NOTI_VOICE_CALL_STATUS_DIALING: + case TELEPHONY_NOTI_VIDEO_CALL_STATUS_DIALING:*/ + default: + _E("Unkown noti id: %d", noti_id); + return; + } + + int err = telephony_call_get_call_list(handle, &count, &call_list); + IF_FAIL_VOID_TAG(err == TELEPHONY_ERROR_NONE, _E, "Getting call list failed"); + + unsigned int call_id = *static_cast<unsigned int *>(id); + for (unsigned int i = 0; i < count; i++) { + unsigned int tmp_id; + // Handle id + if (!get_call_handle_id(call_list[i], tmp_id)) { + continue; + } + + if (call_id != tmp_id) { + continue; + } + + // Address + std::string address; + if (get_call_address(call_list[i], address)) { + data.set(NULL, SOCIAL_ST_ADDRESS, address); + break; + } + } + + if (latest != data) { + context_manager::publish(SOCIAL_ST_SUBJ_CALL, NULL, ERR_NONE, data); + latest = data.str(); + } + telephony_call_release_call_list(count, &call_list); +} + +bool ctx::social_status_call::init_telephony() +{ + IF_FAIL_RETURN(!telephony_initialized, true); + + int err = telephony_init(&handle_list); + IF_FAIL_RETURN_TAG(err == TELEPHONY_ERROR_NONE, false, _E, "Initialization failed"); + + telephony_initialized = true; + return true; +} + +void ctx::social_status_call::release_telephony() +{ + IF_FAIL_VOID(telephony_initialized); + + telephony_deinit(&handle_list); + + telephony_initialized = false; +} + +bool ctx::social_status_call::set_callback() +{ + //TODO: Consider dual-sim devices + IF_FAIL_RETURN_TAG(init_telephony(), false, _E, "Initialization failed"); + + int err; + + for (unsigned int i = 0; i < handle_list.count; i++) { + for (unsigned int j = 0; j < TELEPHONY_NOTI_ID_CNT; j++) { + err = telephony_set_noti_cb(handle_list.handle[i], call_noti_ids[j], call_event_cb, this); + IF_FAIL_CATCH(err == TELEPHONY_ERROR_NONE); + } + } + + return true; + +CATCH: + _E("Initialization failed"); + release_telephony(); + return false; +} + +void ctx::social_status_call::unset_callback() +{ + for (unsigned int i = 0; i < handle_list.count; i++) { + for (unsigned int j = 0; j < TELEPHONY_NOTI_ID_CNT; j++) { + telephony_unset_noti_cb(handle_list.handle[i], call_noti_ids[j]); + } + } + + release_telephony(); +} + +bool ctx::social_status_call::get_call_state(telephony_call_h& handle, std::string& state) +{ + state.clear(); + + telephony_call_status_e st; + int err = telephony_call_get_status(handle, &st); + IF_FAIL_RETURN_TAG(err == TELEPHONY_ERROR_NONE, false, _E, "Getting state failed"); + + switch (st) { + case TELEPHONY_CALL_STATUS_ACTIVE: + state = SOCIAL_ST_ACTIVE; + break; + case TELEPHONY_CALL_STATUS_HELD: + state = SOCIAL_ST_HELD; + break; + case TELEPHONY_CALL_STATUS_DIALING: + state = SOCIAL_ST_DIALING; + break; + case TELEPHONY_CALL_STATUS_ALERTING: + state = SOCIAL_ST_ALERTING; + break; + case TELEPHONY_CALL_STATUS_INCOMING: + state = SOCIAL_ST_INCOMING; + break; + default: + state = SOCIAL_ST_IDLE; + } + + IF_FAIL_RETURN_TAG(!state.empty(), false, _W, "State is empty"); + + return true; +} + +bool ctx::social_status_call::get_call_type(telephony_call_h& handle, std::string& type) +{ + type.clear(); + + telephony_call_type_e t; + int err = telephony_call_get_type(handle, &t); + IF_FAIL_RETURN_TAG(err == TELEPHONY_ERROR_NONE, false, _E, "Getting type failed"); + + switch (t) { + case TELEPHONY_CALL_TYPE_VOICE: + type = SOCIAL_ST_VOICE; + break; + case TELEPHONY_CALL_TYPE_VIDEO: + type = SOCIAL_ST_VIDEO; + break; + default: + _E("Unknown type: %d", t); + return false; + } + + IF_FAIL_RETURN_TAG(!type.empty(), false, _W, "Type is empty"); + + return true; +} + +bool ctx::social_status_call::get_call_address(telephony_call_h& handle, std::string& address) +{ + address.clear(); + + char* number = NULL; + int err = telephony_call_get_number(handle, &number); + IF_FAIL_RETURN_TAG(err == TELEPHONY_ERROR_NONE, false, _E, "Getting address failed"); + + if (number) { + address = number; + free(number); + number = NULL; + } + + IF_FAIL_RETURN_TAG(!address.empty(), false, _W, "Address is empty"); + + return true; +} + +bool ctx::social_status_call::get_call_handle_id(telephony_call_h& handle, unsigned int& id) +{ + int err = telephony_call_get_handle_id(handle, &id); + IF_FAIL_RETURN_TAG(err == TELEPHONY_ERROR_NONE, false, _E, "Getting handle id failed"); + + return true; +} + +int ctx::social_status_call::subscribe() +{ + bool ret = set_callback(); + IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::social_status_call::unsubscribe() +{ + unset_callback(); + return ERR_NONE; +} + +bool ctx::social_status_call::read_current_status(telephony_h& handle, ctx::json& data) +{ + unsigned int count = 0; + telephony_call_h *call_list = NULL; + telephony_call_get_call_list(handle, &count, &call_list); + + // Default data + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_IDLE); + + // Held & Dialing are ignored + for (unsigned int i = 0; i < count; i++) { + // Call state + std::string state; + if (get_call_state(call_list[i], state)) { + // Skip Held & Dialing + if (state.compare(SOCIAL_ST_HELD) == 0 || state.compare(SOCIAL_ST_DIALING) == 0) + continue; + + data.set(NULL, SOCIAL_ST_STATE, state); + } + + // Call type + std::string type; + if (get_call_type(call_list[i], type)) { + data.set(NULL, SOCIAL_ST_MEDIUM, type); + } + + // Address + std::string address; + if (get_call_address(call_list[i], address)) { + data.set(NULL, SOCIAL_ST_ADDRESS, address); + } + + if (state == SOCIAL_ST_ACTIVE) { + break; + } + } + + telephony_call_release_call_list(count, &call_list); + return true; +} + +int ctx::social_status_call::read() +{ + bool temporary_handle = false; + if (!telephony_initialized) { + IF_FAIL_RETURN(init_telephony(), ERR_OPERATION_FAILED); + temporary_handle = true; + } + + bool ret = true; + json data; + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_IDLE); + + for (unsigned int i = 0; i < handle_list.count; i++) { + telephony_sim_state_e state; + int err = telephony_sim_get_state(handle_list.handle[i], &state); + IF_FAIL_RETURN_TAG(err == TELEPHONY_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Getting SIM status failed"); + + if (state != TELEPHONY_SIM_STATE_AVAILABLE) + continue; + + ret = read_current_status(handle_list.handle[i], data); + break; + } + + if (temporary_handle) + release_telephony(); + + if (ret) { + ctx::context_manager::reply_to_read(SOCIAL_ST_SUBJ_CALL, NULL, ERR_NONE, data); + return ERR_NONE; + } + + return ERR_OPERATION_FAILED; +} diff --git a/src/social_status/call.h b/src/social_status/call.h new file mode 100644 index 0000000..a67eaa1 --- /dev/null +++ b/src/social_status/call.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _CONTEXT_SOCIAL_STATUS_CALL_H_ +#define _CONTEXT_SOCIAL_STATUS_CALL_H_ + +#include <telephony.h> +#include "../provider_base.h" + +namespace ctx { + + class social_status_call : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(social_status_call); + + public: + int subscribe(); + int unsubscribe(); + int read(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + telephony_handle_list_s handle_list; + + social_status_call(); + ~social_status_call(); + + bool init_telephony(); + void release_telephony(); + bool set_callback(); + void unset_callback(); + bool read_current_status(telephony_h& handle, ctx::json& data); + + bool get_call_state(telephony_call_h& handle, std::string& state); + bool get_call_type(telephony_call_h& handle, std::string& type); + bool get_call_address(telephony_call_h& handle, std::string& address); + bool get_call_handle_id(telephony_call_h& handle, unsigned int& id); + + void handle_call_event(telephony_h handle, telephony_noti_e noti_id, void* id); + static void call_event_cb(telephony_h handle, telephony_noti_e noti_id, void *data, void *user_data); + }; +} + +#endif // _CONTEXT_SOCIAL_STATUS_CALL_H_ diff --git a/src/social_status/email.cpp b/src/social_status/email.cpp new file mode 100644 index 0000000..8c3d93a --- /dev/null +++ b/src/social_status/email.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <gio/gio.h> +#include <email-api-etc.h> + +#include <context_mgr.h> +#include "social_status_types.h" +#include <dbus_server.h> +#include "email.h" + +GENERATE_PROVIDER_COMMON_IMPL(social_status_email); + +ctx::social_status_email::social_status_email() +{ +} + +ctx::social_status_email::~social_status_email() +{ +} + +bool ctx::social_status_email::is_supported() +{ + return get_system_info_bool("tizen.org/feature/network.telephony"); +} + +void ctx::social_status_email::submit_trigger_item() +{ + context_manager::register_trigger_item(SOCIAL_ST_SUBJ_EMAIL, OPS_SUBSCRIBE, + "{" + "\"Event\":{\"type\":\"string\",\"values\":[\"Received\",\"Sent\"]}" + "}", + NULL); +} + +void ctx::social_status_email::on_signal_received(const char* sender, const char* path, const char* iface, const char* name, GVariant* param) +{ + gint sub_type = 0; + gint gi1 = 0; + const gchar *gc = NULL; + gint gi2 = 0; + gint gi3 = 0; + + g_variant_get(param, "(ii&sii)", &sub_type, &gi1, &gc, &gi2, &gi3); + + if (sub_type == NOTI_DOWNLOAD_FINISH) { + //TODO: Check if this signal actually means that there are new mails + _D("sub type: %d, gi1: %d, gc: %s, gi2: %d, gi3: %d", sub_type, gi1, gc, gi2, gi3); + ctx::json data_updated; + data_updated.set(NULL, SOCIAL_ST_EVENT, SOCIAL_ST_RECEIVED); + context_manager::publish(SOCIAL_ST_SUBJ_EMAIL, NULL, ERR_NONE, data_updated); + + } else if (sub_type == NOTI_SEND_FINISH) { + _D("sub type: %d, gi1: %d, gc: %s, gi2: %d, gi3: %d", sub_type, gi1, gc, gi2, gi3); + ctx::json data_updated; + data_updated.set(NULL, SOCIAL_ST_EVENT, SOCIAL_ST_SENT); + context_manager::publish(SOCIAL_ST_SUBJ_EMAIL, NULL, ERR_NONE, data_updated); + } +} + + +int ctx::social_status_email::subscribe() +{ + dbus_signal_id = ctx::dbus_server::signal_subscribe(NULL, NULL, "User.Email.NetworkStatus", "email", this); + IF_FAIL_RETURN_TAG(dbus_signal_id >= 0, ERR_OPERATION_FAILED, _E, "Email dbus signal subscription failed"); + return ERR_NONE; +} + + +int ctx::social_status_email::unsubscribe() +{ + ctx::dbus_server::signal_unsubscribe(dbus_signal_id); + return ERR_NONE; +} diff --git a/src/social_status/email.h b/src/social_status/email.h new file mode 100644 index 0000000..cbfc671 --- /dev/null +++ b/src/social_status/email.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _CONTEXT_SOCIAL_STATUS_EMAIL_H_ +#define _CONTEXT_SOCIAL_STATUS_EMAIL_H_ + +#include <dbus_listener_iface.h> +#include "../provider_base.h" + +namespace ctx { + + class social_status_email : public device_provider_base, public dbus_listener_iface { + + GENERATE_PROVIDER_COMMON_DECL(social_status_email); + + public: + int subscribe(); + int unsubscribe(); + void on_signal_received(const char* sender, const char* path, const char* iface, const char* name, GVariant* param); + static bool is_supported(); + static void submit_trigger_item(); + + private: + int64_t dbus_signal_id; + + social_status_email(); + ~social_status_email(); + }; +} + +#endif // _CONTEXT_SOCIAL_STATUS_EMAIL_H_ diff --git a/src/social_status/message.cpp b/src/social_status/message.cpp new file mode 100644 index 0000000..8c41232 --- /dev/null +++ b/src/social_status/message.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 <json.h> +#include <context_mgr.h> +#include "social_status_types.h" +#include "message.h" + +#define MAX_ADDR_SIZE 20 + +GENERATE_PROVIDER_COMMON_IMPL(social_status_message); + +ctx::social_status_message::social_status_message() + : message_handle(NULL) +{ +} + +ctx::social_status_message::~social_status_message() +{ +} + +bool ctx::social_status_message::is_supported() +{ + return get_system_info_bool("tizen.org/feature/network.telephony"); +} + +void ctx::social_status_message::submit_trigger_item() +{ + context_manager::register_trigger_item(SOCIAL_ST_SUBJ_MESSAGE, OPS_SUBSCRIBE, + "{" + "\"Event\":{\"type\":\"string\",\"values\":[\"Received\"]}," + "\"Type\":{\"type\":\"string\",\"values\":[\"SMS\",\"MMS\"]}," + "\"Address\":{\"type\":\"string\"}" + "}", + NULL); +} + +void ctx::social_status_message::state_change_cb(msg_handle_t handle, msg_struct_t msg, void* user_data) +{ + social_status_message *instance = static_cast<social_status_message*>(user_data); + instance->handle_state_change(msg); +} + +void ctx::social_status_message::handle_state_change(msg_struct_t msg) +{ + int err; + int type; + char address[MAX_ADDR_SIZE]; + ctx::json data; + + err = msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &type); + IF_FAIL_VOID_TAG(err == MSG_SUCCESS, _W, "Getting message type failed"); + + err = msg_get_str_value(msg, MSG_MESSAGE_REPLY_ADDR_STR, address, MAX_ADDR_SIZE); + IF_FAIL_VOID_TAG(err == MSG_SUCCESS, _W, "Getting reply address failed"); + + switch (type) { + case MSG_TYPE_SMS_CB : + case MSG_TYPE_SMS_JAVACB : + case MSG_TYPE_SMS_WAPPUSH : + case MSG_TYPE_SMS_MWI : + case MSG_TYPE_SMS_SYNCML : + case MSG_TYPE_SMS_REJECT : + case MSG_TYPE_SMS_ETWS_PRIMARY : + case MSG_TYPE_SMS : + data.set(NULL, SOCIAL_ST_TYPE, SOCIAL_ST_SMS); + break; + case MSG_TYPE_MMS_NOTI : + case MSG_TYPE_MMS_JAVA : + case MSG_TYPE_MMS : + data.set(NULL, SOCIAL_ST_TYPE, SOCIAL_ST_MMS); + break; + default : + _W("Unknown message type"); + return; + } + + data.set(NULL, SOCIAL_ST_EVENT, SOCIAL_ST_RECEIVED); + data.set(NULL, SOCIAL_ST_ADDRESS, address); + + context_manager::publish(SOCIAL_ST_SUBJ_MESSAGE, NULL, ERR_NONE, data); +} + +bool ctx::social_status_message::set_callback() +{ + int err; + + err = msg_open_msg_handle(&message_handle); + IF_FAIL_RETURN_TAG(err == MSG_SUCCESS, false, _E, "Handle creation failed"); + + err = msg_reg_sms_message_callback(message_handle, state_change_cb, 0, this); + if (err != MSG_SUCCESS) { + msg_close_msg_handle(&message_handle); + _E("Setting SMS event callback failed"); + return false; + } + + msg_reg_mms_conf_message_callback(message_handle, state_change_cb, NULL, this); + return true; +} + +void ctx::social_status_message::unset_callback() +{ + if (message_handle) + msg_close_msg_handle(&message_handle); + + message_handle = NULL; +} + +int ctx::social_status_message::subscribe() +{ + bool ret = set_callback(); + IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED); + return ERR_NONE; +} + +int ctx::social_status_message::unsubscribe() +{ + unset_callback(); + return ERR_NONE; +} diff --git a/src/social_status/message.h b/src/social_status/message.h new file mode 100644 index 0000000..1c1d15b --- /dev/null +++ b/src/social_status/message.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 _CONTEXT_SOCIAL_STATUS_SMS_H_ +#define _CONTEXT_SOCIAL_STATUS_SMS_H_ + +#include <msg.h> +#include <msg_transport.h> +#include "../provider_base.h" + +namespace ctx { + + class social_status_message : public device_provider_base { + + GENERATE_PROVIDER_COMMON_DECL(social_status_message); + + public: + int subscribe(); + int unsubscribe(); + static bool is_supported(); + static void submit_trigger_item(); + + private: + msg_handle_t message_handle; + bool being_subscribed; + + social_status_message(); + ~social_status_message(); + + bool set_callback(); + void unset_callback(); + void handle_state_change(msg_struct_t msg); + static void state_change_cb(msg_handle_t handle, msg_struct_t msg, void* user_data); + }; +} + +#endif // _CONTEXT_SOCIAL_STATUS_SMS_H_ diff --git a/src/social_status/social_status_types.h b/src/social_status/social_status_types.h new file mode 100644 index 0000000..55c015f --- /dev/null +++ b/src/social_status/social_status_types.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_SOCIAL_STATUS_TYPES_INTERNAL_H__ +#define __CONTEXT_SOCIAL_STATUS_TYPES_INTERNAL_H__ + +// Subject +#define SOCIAL_ST_SUBJ_CALL "social/call" +#define SOCIAL_ST_SUBJ_EMAIL "social/email" +#define SOCIAL_ST_SUBJ_MESSAGE "social/message" + +// Data Key +#define SOCIAL_ST_STATE "State" +#define SOCIAL_ST_EVENT "Event" +#define SOCIAL_ST_TYPE "Type" +#define SOCIAL_ST_MEDIUM "Medium" +#define SOCIAL_ST_ADDRESS "Address" + +// Data Values +#define SOCIAL_ST_IDLE "Idle" +#define SOCIAL_ST_CONNECTING "Connecting" +#define SOCIAL_ST_CONNECTED "Connected" +#define SOCIAL_ST_ACTIVE SOCIAL_ST_CONNECTED +#define SOCIAL_ST_HELD "Held" +#define SOCIAL_ST_DIALING "Dialing" +#define SOCIAL_ST_ALERTING SOCIAL_ST_CONNECTING +#define SOCIAL_ST_INCOMING SOCIAL_ST_CONNECTING +#define SOCIAL_ST_VOICE "Voice" +#define SOCIAL_ST_VIDEO "Video" +#define SOCIAL_ST_SENT "Sent" +#define SOCIAL_ST_RECEIVED "Received" +#define SOCIAL_ST_SMS "SMS" +#define SOCIAL_ST_MMS "MMS" + +#endif |