From: Mu-Woong Date: Fri, 12 Jun 2015 08:05:53 +0000 (+0900) Subject: Migrate from 2.4 code repo X-Git-Tag: accepted/tizen/mobile/20150616.011114~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a783197a813256fb37f971bc7d870ee5e5888e58;p=platform%2Fcore%2Fcontext%2Fdevice-context-provider.git Migrate from 2.4 code repo Change-Id: I307b8f02a5c2a1bcaba3ddd3538c323ff91b4336 Signed-off-by: Mu-Woong --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a01ee28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..3e2965b --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Sanggun Lee +Mu-Woong Lee diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..63c1de6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,92 @@ +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) +FILE(GLOB_RECURSE SRCS ${SRCS} src/device_status/*.cpp) + +# Dependencies +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 libcore-context-manager") + +# Mobile profile +IF("${PROFILE}" STREQUAL "mobile") + ADD_DEFINITIONS("-D_MOBILE_") + FILE(GLOB_RECURSE SRCS ${SRCS} src/social_status/*.cpp) + SET(provider_deps "${provider_deps} capi-telephony tapi msg-service capi-messaging-email") +ENDIF("${PROFILE}" STREQUAL "mobile") + +# Wearable profile +IF("${PROFILE}" STREQUAL "wearable") + ADD_DEFINITIONS("-D_WEARABLE_") + SET(SRCS ${SRCS} src/social_status/call.cpp) + SET(SRCS ${SRCS} src/social_status/message.cpp) + SET(provider_deps "${provider_deps} capi-telephony tapi msg-service") +ENDIF("${PROFILE}" STREQUAL "wearable") + +# TV profile +IF("${PROFILE}" STREQUAL "tv") + ADD_DEFINITIONS("-D_TV_") +ENDIF("${PROFILE}" STREQUAL "tv") + +MESSAGE("Sources: ${SRCS}") + +# Target vs Emulator +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-D_TARGET") +ELSE("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-D_EMULATOR") +ENDIF("${ARCH}" STREQUAL "arm") + +# 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) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1da314d --- /dev/null +++ b/LICENSE @@ -0,0 +1,204 @@ +Copyright (c) 2000 - 2014 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) 2014 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..f4513db --- /dev/null +++ b/include/device_context_provider.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014 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__ + +#include + +namespace ctx { + + class device_context_provider : public ctx::context_provider_iface { + public: + device_context_provider(); + ~device_context_provider(); + + bool init(); + bool is_supported(const char* subject, const char* zone); + int subscribe(const char* subject, ctx::json option, ctx::json* request_result, const char* zone); + int unsubscribe(const char* subject, ctx::json option, const char* zone); + int read(const char* subject, ctx::json option, ctx::json* request_result, const char* zone); + int write(const char* subject, ctx::json data, ctx::json* request_result, const char* zone); + }; + +} + +#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 @@ + + + + + diff --git a/packaging/device-context-provider.spec b/packaging/device-context-provider.spec new file mode 100644 index 0000000..cc6cddb --- /dev/null +++ b/packaging/device-context-provider.spec @@ -0,0 +1,96 @@ +Name: device-context-provider +Summary: Device Context Provider +Version: 0.4.3 +Release: 1 +Group: Framework/system +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz + +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) +BuildRequires: pkgconfig(libcore-context-manager) + +%if "%{?tizen_profile_name}" == "mobile" +BuildRequires: pkgconfig(capi-telephony) +BuildRequires: pkgconfig(tapi) +BuildRequires: pkgconfig(msg-service) +BuildRequires: pkgconfig(capi-messaging-email) +%endif + +%if "%{?tizen_profile_name}" == "wearable" +BuildRequires: pkgconfig(capi-telephony) +BuildRequires: pkgconfig(tapi) +BuildRequires: pkgconfig(msg-service) +%endif + +%ifarch %{arm} +%define ARCH arm +%else +%define ARCH i586 +%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" + +%define BINTYPE engineer +export CFLAGS+=" -DTIZEN_ENGINEER_MODE" +export CXXFLAGS+=" -DTIZEN_ENGINEER_MODE" +export FFLAGS+=" -DTIZEN_ENGINEER_MODE" + +cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DARCH=%{ARCH} -DMAJORVER=${MAJORVER} -DFULLVER=%{version} -DPROFILE=%{?tizen_profile_name} -DBINTYPE=%{BINTYPE} +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: Framework/system +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/device_context_provider.cpp b/src/device_context_provider.cpp new file mode 100644 index 0000000..c667133 --- /dev/null +++ b/src/device_context_provider.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2014 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 +#include +#include +#include + +#include +#include +#include +#include +#include "device_status/device_status_types.h" +#include "social_status/social_status_types.h" + +#include "device_status/runtime-info/charger.h" +#include "device_status/runtime-info/gps.h" +#include "device_status/runtime-info/usb.h" +#include "device_status/wifi.h" +#include "device_status/battery.h" +#include "device_status/headphone.h" +#include "device_status/psmode.h" +#include "device_status/activity.h" + +#ifdef _MOBILE_ +#include "social_status/call.h" +#include "social_status/email.h" +#include "social_status/message.h" +#endif + +#ifdef _WEARABLE_ +#include "social_status/call.h" +#include "social_status/message.h" +#endif + +#define CREATE_SUB_PROVIDER(subject, subprovider) do { \ + ctx::sub_provider_base *sub = new ctx::subprovider(); \ + if (sub->is_supported(subject, NULL)) { \ + subject_map[(subject)] = sub; \ + } else { \ + _E("%s is not supported", subject); \ + delete sub; \ + }} while (0) + +typedef std::map subject_map_t; +static subject_map_t subject_map; + +static void load_sub_providers() +{ + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_CHARGER, device_status_charger); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_GPS, device_status_gps); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_USB, device_status_usb); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_WIFI, device_status_wifi); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_BATTERY, device_status_battery_level); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_HEADPHONE, device_status_headphone); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_POWER_SAVING_MODE, device_status_power_saving_mode); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_ACTIVITY_WALKING, device_status_activity); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_ACTIVITY_RUNNING, device_status_activity); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_ACTIVITY_STATIONARY, device_status_activity); + CREATE_SUB_PROVIDER(DEVICE_ST_SUBJ_ACTIVITY_IN_VEHICLE, device_status_activity); + +#ifdef _MOBILE_ + CREATE_SUB_PROVIDER(SOCIAL_ST_SUBJ_CALL, social_status_call); + CREATE_SUB_PROVIDER(SOCIAL_ST_SUBJ_EMAIL, social_status_email); + CREATE_SUB_PROVIDER(SOCIAL_ST_SUBJ_MESSAGE, social_status_message); +#endif + +#ifdef _WEARABLE_ + CREATE_SUB_PROVIDER(SOCIAL_ST_SUBJ_CALL, social_status_call); + CREATE_SUB_PROVIDER(SOCIAL_ST_SUBJ_MESSAGE, social_status_message); +#endif +} + +static void unload_sub_providers() +{ + for (subject_map_t::iterator it = subject_map.begin(); it != subject_map.end(); ++it) { + delete it->second; + } + + subject_map.clear(); +} + +EXTAPI ctx::device_context_provider::device_context_provider() +{ +} + + +EXTAPI ctx::device_context_provider::~device_context_provider() +{ + unload_sub_providers(); +} + + +bool ctx::device_context_provider::init() +{ + IF_FAIL_RETURN(subject_map.empty(), true); + + try { + load_sub_providers(); + } catch (std::bad_alloc& ba) { + _E("Memory allocation failed"); + return false; + } + + for (subject_map_t::iterator it = subject_map.begin(); it != subject_map.end(); ++it) { + context_manager::register_provider(it->first.c_str(), this); + } + + _D("Done"); + return true; +} + +bool ctx::device_context_provider::is_supported(const char* subject, const char* zone) +{ + subject_map_t::iterator it = subject_map.find(subject); + + if (it != subject_map.end()) + return it->second->is_supported(subject, zone); + + return false; +} + +int ctx::device_context_provider::subscribe(const char* subject, ctx::json option, ctx::json* request_result, const char* zone) +{ + _D("Starts to publish '%s' on '%s'", subject, zone); + + subject_map_t::iterator it = subject_map.find(subject); + + if (it != subject_map.end()) + return it->second->subscribe(subject, zone); + + return ERR_NOT_SUPPORTED; +} + + +int ctx::device_context_provider::unsubscribe(const char* subject, ctx::json option, const char* zone) +{ + _D("Stops publishing '%s' on '%s'", subject, zone); + + subject_map_t::iterator it = subject_map.find(subject); + + if (it != subject_map.end()) + return it->second->unsubscribe(subject, zone); + + return ERR_NOT_SUPPORTED; +} + + +int ctx::device_context_provider::read(const char* subject, ctx::json option, ctx::json* request_result, const char* zone) +{ + _D("Reads '%s' on '%s'", subject, zone); + + subject_map_t::iterator it = subject_map.find(subject); + + if (it != subject_map.end()) + return it->second->read(subject, zone); + + return ERR_NOT_SUPPORTED; +} + + +int ctx::device_context_provider::write(const char* subject, ctx::json data, ctx::json* request_result, const char* zone) +{ + return ERR_NOT_SUPPORTED; +} diff --git a/src/device_status/activity.cpp b/src/device_status/activity.cpp new file mode 100644 index 0000000..be7959d --- /dev/null +++ b/src/device_status/activity.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2014 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 "activity.h" + +#define NUM_ACTIVITY 5 // Activity type enum starts from 1. This number should be greater than the number of activity types. + +struct activity_recog_s { + const char *name; + activity_h handle; + ctx::string_set_t client_zone; + activity_recog_s() : name(NULL), handle(NULL) {} +}; + +static activity_recog_s activity_recog[NUM_ACTIVITY]; + +static int get_activity_id(const char* subject) +{ + for (int i = 1; i < NUM_ACTIVITY; ++i) { + if (STR_EQ(subject, activity_recog[i].name)) + return i; + } + + return 0; +} + +ctx::device_status_activity::device_status_activity() +{ + activity_recog[ACTIVITY_STATIONARY].name = DEVICE_ST_SUBJ_ACTIVITY_STATIONARY; + activity_recog[ACTIVITY_WALK].name = DEVICE_ST_SUBJ_ACTIVITY_WALKING; + activity_recog[ACTIVITY_RUN].name = DEVICE_ST_SUBJ_ACTIVITY_RUNNING; + activity_recog[ACTIVITY_IN_VEHICLE].name = DEVICE_ST_SUBJ_ACTIVITY_IN_VEHICLE; +} + +ctx::device_status_activity::~device_status_activity() +{ + for (int i=0; i 0 && activity < NUM_ACTIVITY, _E, "Unknown activity: %d", activity); + + device_status_activity *instance = static_cast(user_data); + instance->handle_event(activity, data, timestamp); +} + +void ctx::device_status_activity::handle_event(activity_type_e activity, const activity_data_h data, double timestamp) +{ + ctx::json data_read; + data_read.set(NULL, DEVICE_ST_EVENT, DEVICE_ST_DETECTED); + + activity_accuracy_e accuracy = ACTIVITY_ACCURACY_LOW; + activity_get_accuracy(data, &accuracy); + + switch (accuracy) { + case ACTIVITY_ACCURACY_HIGH: + data_read.set(NULL, DEVICE_ST_ACCURACY, DEVICE_ST_HIGH); + break; + case ACTIVITY_ACCURACY_MID: + data_read.set(NULL, DEVICE_ST_ACCURACY, DEVICE_ST_NORMAL); + break; + default: + data_read.set(NULL, DEVICE_ST_ACCURACY, DEVICE_ST_LOW); + break; + } + + publish(activity_recog[activity].name, ERR_NONE, data_read, activity_recog[activity].client_zone); +} + +int ctx::device_status_activity::subscribe(const char* subject, const char* zone) +{ + int act_num = get_activity_id(subject); + IF_FAIL_RETURN(act_num > 0, ERR_INVALID_PARAMETER); + + int ret; + activity_h handle = NULL; + bool supported = false; + + activity_is_supported(static_cast(act_num), &supported); + IF_FAIL_RETURN(supported, ERR_NOT_SUPPORTED); + IF_FAIL_CATCH(activity_recog[act_num].handle == NULL); + + _D("Starting to monitor %s", subject); + + activity_create(&handle); + IF_FAIL_RETURN_TAG(handle, ERR_OPERATION_FAILED, _E, "Memory allocation failed"); + + ret = activity_start_recognition(handle, static_cast(act_num), event_cb, this); + if (ret != ACTIVITY_ERROR_NONE) { + _E("Recognition starting failed"); + activity_release(handle); + return ERR_OPERATION_FAILED; + } + + activity_recog[act_num].handle = handle; + +CATCH: + activity_recog[act_num].client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::device_status_activity::unsubscribe(const char* subject, const char* zone) +{ + int act_num = get_activity_id(subject); + IF_FAIL_RETURN(act_num > 0, ERR_INVALID_PARAMETER); + + activity_recog[act_num].client_zone.erase(zone); + IF_FAIL_RETURN(activity_recog[act_num].client_zone.empty(), ERR_NONE); + + _D("Stop monitoring %s", subject); + + activity_stop_recognition(activity_recog[act_num].handle); + activity_release(activity_recog[act_num].handle); + activity_recog[act_num].handle = NULL; + + return ERR_NONE; +} + +bool ctx::device_status_activity::is_supported(const char* subject, const char* zone) +{ + bool supported = false; + activity_is_supported(static_cast(get_activity_id(subject)), &supported); + return supported; +} diff --git a/src/device_status/activity.h b/src/device_status/activity.h new file mode 100644 index 0000000..36acd0b --- /dev/null +++ b/src/device_status/activity.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014 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 +#include "../sub_provider_base.h" + +namespace ctx { + + class device_status_activity : public sub_provider_base { + public: + device_status_activity(); + ~device_status_activity(); + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + bool is_supported(const char* subject, const char* zone); + + 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_H_ diff --git a/src/device_status/battery.cpp b/src/device_status/battery.cpp new file mode 100644 index 0000000..cdf2ac5 --- /dev/null +++ b/src/device_status/battery.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2014 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 "battery.h" + +static const char* trans_to_string(int 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("battery_level receive abnormal value from device api"); + return NULL; + } +} + +void ctx::device_status_battery_level::update_cb(device_callback_e device_type, void* value, void* user_data) +{ + IF_FAIL_VOID(device_type == DEVICE_CALLBACK_BATTERY_LEVEL); + + device_status_battery_level *instance = static_cast(user_data); + instance->handle_update(device_type, value); +} + +void ctx::device_status_battery_level::handle_update(device_callback_e device_type, void* value) +{ + int level = reinterpret_cast(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); + + publish(DEVICE_ST_SUBJ_BATTERY, ERR_NONE, data_read, client_zone); +} + +int ctx::device_status_battery_level::subscribe(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + int ret = device_add_callback(DEVICE_CALLBACK_BATTERY_LEVEL, update_cb, this); + IF_FAIL_RETURN(ret == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED); + } + + client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::device_status_battery_level::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + 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_level::read(const char* subject, const char* zone) +{ + 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, zone); + return ERR_NONE; +} diff --git a/src/device_status/battery.h b/src/device_status/battery.h new file mode 100644 index 0000000..905e021 --- /dev/null +++ b/src/device_status/battery.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 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 +#include +#include "../sub_provider_base.h" + +namespace ctx { + + class device_status_battery_level : public sub_provider_base { + public: + ~device_status_battery_level(){} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + int read(const char* subject, const char* zone); + + private: + string_set_t client_zone; + + 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..f514a09 --- /dev/null +++ b/src/device_status/device_status_types.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2014 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/state/battery" +#define DEVICE_ST_SUBJ_CHARGER "system/state/charger" +#define DEVICE_ST_SUBJ_HEADPHONE "system/state/headphone" +#define DEVICE_ST_SUBJ_WIFI "system/state/wifi" +#define DEVICE_ST_SUBJ_USB "system/state/usb" +#define DEVICE_ST_SUBJ_GPS "system/state/gps" +#define DEVICE_ST_SUBJ_VIBRATION_MODE "system/state/vibration_mode" +#define DEVICE_ST_SUBJ_POWER_SAVING_MODE "system/state/ps_mode" +#define DEVICE_ST_SUBJ_FLIGHT_MODE "system/state/flight_mode" +#define DEVICE_ST_SUBJ_SILENT_MODE "system/state/silent_mode" +#define DEVICE_ST_SUBJ_ACTIVITY_CYCLING "activity/event/cycling" +#define DEVICE_ST_SUBJ_ACTIVITY_IN_VEHICLE "activity/event/in_vehicle" +#define DEVICE_ST_SUBJ_ACTIVITY_RUNNING "activity/event/running" +#define DEVICE_ST_SUBJ_ACTIVITY_STATIONARY "activity/event/stationary" +#define DEVICE_ST_SUBJ_ACTIVITY_WALKING "activity/event/walking" + +// 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_ACCURACY "Accuracy" +#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" +#define DEVICE_ST_ACCURACY "Accuracy" + +// 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" + +#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..a2c4ac1 --- /dev/null +++ b/src/device_status/headphone.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2014 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 "headphone.h" + +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) +{ +} + +int ctx::device_status_headphone::subscribe(const char* subject, const char* zone) +{ + //FIXME: in case of TV? + if (client_zone.empty()) { + connected = get_current_status(); + + // Wired headphone + int ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, audio_jack_cb, this); + IF_FAIL_RETURN(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED); + + // Bluetooth headphone + set_bt_audio_callback(); + } + + client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::device_status_headphone::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS); + unset_bt_audio_callback(); + + return ERR_NONE; +} + +int ctx::device_status_headphone::read(const char* subject, const char* zone) +{ + if (client_zone.empty()) + connected = get_current_status(); + + json data; + generate_data_packet(data); + ctx::context_manager::reply_to_read(DEVICE_ST_SUBJ_HEADPHONE, NULL, ERR_NONE, data, zone); + + 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_audio_initialize(); + if (ret != BT_ERROR_NONE) { + bt_deinitialize(); + _W("Bluetooth audio initialization failed"); + return; + } + + ret = bt_audio_set_connection_state_changed_cb(bt_audio_cb, this); + if (ret != BT_ERROR_NONE) { + bt_audio_deinitialize(); + 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_audio_unset_connection_state_changed_cb(); + bt_audio_deinitialize(); + bt_deinitialize(); +} + +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(bt_bonded_device_cb, 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; + } +} + +void 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_VOID(prev_state != connected); + + ctx::json data; + generate_data_packet(data); + publish(DEVICE_ST_SUBJ_HEADPHONE, ERR_NONE, data, client_zone); +} + +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::handle_bt_audio_event(bool conn) +{ + bt_audio_state = conn; + handle_event(); +} + +void ctx::device_status_headphone::audio_jack_cb(runtime_info_key_e runtime_key, void* user_data) +{ + _D("EarJack"); + ctx::device_status_headphone *instance = static_cast(user_data); + instance->handle_audio_jack_event(); +} + +void ctx::device_status_headphone::bt_audio_cb(int result, bool connected, const char *remote_address, bt_audio_profile_type_e type, void *user_data) +{ + _D("BlueTooth"); + IF_FAIL_VOID_TAG(result == BT_ERROR_NONE, _E, "Error: %d", result); + + ctx::device_status_headphone *instance = static_cast(user_data); + instance->handle_bt_audio_event(connected); +} + +bool ctx::device_status_headphone::bt_bonded_device_cb(bt_device_info_s *device_info, void* user_data) +{ + if (device_info->bt_class.major_device_class == BT_MAJOR_DEVICE_CLASS_AUDIO_VIDEO) + bt_device_foreach_connected_profiles(device_info->remote_address, bt_profile_cb, user_data); + + return true; +} + +bool ctx::device_status_headphone::bt_profile_cb(bt_profile_e profile, void* user_data) +{ + if (profile == BT_PROFILE_A2DP || profile == BT_PROFILE_HSP) { + ctx::device_status_headphone *instance = static_cast(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..e6bc290 --- /dev/null +++ b/src/device_status/headphone.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014 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 +#include +#include "../sub_provider_base.h" + +namespace ctx { + + class device_status_headphone : public sub_provider_base { + public: + device_status_headphone(); + ~device_status_headphone() {} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + int read(const char* subject, const char* zone); + + private: + string_set_t client_zone; + + bool connected; + int audio_jack_state; + bool bt_audio_state; + bool bt_audio_callback_on; + + 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); + void handle_event(); + void handle_audio_jack_event(); + void handle_bt_audio_event(bool conn); + + static void bt_audio_cb(int result, bool connected, const char *remote_address, bt_audio_profile_type_e type, void *user_data); + static void audio_jack_cb(runtime_info_key_e runtime_key, void* user_data); + static bool bt_bonded_device_cb(bt_device_info_s *device_info, void* user_data); + static bool bt_profile_cb(bt_profile_e profile, 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..122e864 --- /dev/null +++ b/src/device_status/psmode.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014 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 "psmode.h" + +void ctx::device_status_power_saving_mode::update_cb(keynode_t *node, void* user_data) +{ + device_status_power_saving_mode *instance = static_cast(user_data); + instance->handle_update(node); +} + +void ctx::device_status_power_saving_mode::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); + + publish(DEVICE_ST_SUBJ_POWER_SAVING_MODE, ERR_NONE, data_read, client_zone); +} + +int ctx::device_status_power_saving_mode::subscribe(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + int ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, update_cb, this); + IF_FAIL_RETURN(ret == VCONF_OK, ERR_OPERATION_FAILED); + } + + client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::device_status_power_saving_mode::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + 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_power_saving_mode::read(const char* subject, const char* zone) +{ + 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_POWER_SAVING_MODE, NULL, ERR_NONE, data_read, zone); + return ERR_NONE; +} diff --git a/src/device_status/psmode.h b/src/device_status/psmode.h new file mode 100644 index 0000000..0774ee2 --- /dev/null +++ b/src/device_status/psmode.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 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 +#include "../sub_provider_base.h" + +namespace ctx { + + class device_status_power_saving_mode : public sub_provider_base { + public: + ~device_status_power_saving_mode(){} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + int read(const char* subject, const char* zone); + + private: + string_set_t client_zone; + + 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..a05daca --- /dev/null +++ b/src/device_status/runtime-info/base_rtinfo.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2014 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(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(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + int ret = runtime_info_set_changed_cb(info_key, update_cb, this); + IF_FAIL_RETURN(ret == RUNTIME_INFO_ERROR_NONE, ERR_OPERATION_FAILED); + } + + client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::device_status_runtime_info::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + 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..8ea7f48 --- /dev/null +++ b/src/device_status/runtime-info/base_rtinfo.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014 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 +#include "../../sub_provider_base.h" + +namespace ctx { + + typedef std::set string_set_t; + + class device_status_runtime_info : public sub_provider_base { + public: + device_status_runtime_info(runtime_info_key_e key); + virtual ~device_status_runtime_info(){} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + virtual int read(const char* subject, const char* zone) = 0; + + protected: + string_set_t client_zone; + runtime_info_key_e info_key; + + 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..5c56c0a --- /dev/null +++ b/src/device_status/runtime-info/charger.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2014 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 "charger.h" + +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); + + publish(DEVICE_ST_SUBJ_CHARGER, ERR_NONE, data_read, client_zone); +} + +int ctx::device_status_charger::read(const char* subject, const char* zone) +{ + 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, zone); + 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..b8cf08b --- /dev/null +++ b/src/device_status/runtime-info/charger.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014 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 { + public: + device_status_charger() : device_status_runtime_info(RUNTIME_INFO_KEY_CHARGER_CONNECTED) {} + ~device_status_charger(){} + + int read(const char* subject, const char* zone); + + private: + 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..7bace84 --- /dev/null +++ b/src/device_status/runtime-info/gps.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2014 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 +#include "gps.h" + +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; + } +} + +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); + + publish(DEVICE_ST_SUBJ_GPS, ERR_NONE, data_read, client_zone); +} + +int ctx::device_status_gps::read(const char* subject, const char* zone) +{ + 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, zone); + return ERR_NONE; +} + +bool ctx::device_status_gps::is_supported(const char* subject, const char* zone) +{ + bool supported = false; + + int ret = system_info_get_platform_bool("tizen.org/feature/location.gps", &supported); + IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, false, _E, "system_info_get_platform_bool() failed"); + + return supported; +} diff --git a/src/device_status/runtime-info/gps.h b/src/device_status/runtime-info/gps.h new file mode 100644 index 0000000..3a6f551 --- /dev/null +++ b/src/device_status/runtime-info/gps.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014 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 { + public: + device_status_gps() : device_status_runtime_info(RUNTIME_INFO_KEY_GPS_STATUS) {} + ~device_status_gps(){} + + int read(const char* subject, const char* zone); + bool is_supported(const char* subject, const char* zone); + + private: + 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..2d5185d --- /dev/null +++ b/src/device_status/runtime-info/usb.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014 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 +#include "usb.h" + +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); + + publish(DEVICE_ST_SUBJ_USB, ERR_NONE, data_read, client_zone); +} + +int ctx::device_status_usb::read(const char* subject, const char* zone) +{ + 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, zone); + return ERR_NONE; +} + +bool ctx::device_status_usb::is_supported(const char* subject, const char* zone) +{ + bool supported = false; + + int ret = system_info_get_platform_bool("tizen.org/feature/usb.host", &supported); + IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, false, _E, "system_info_get_platform_bool() failed"); + + return supported; +} diff --git a/src/device_status/runtime-info/usb.h b/src/device_status/runtime-info/usb.h new file mode 100644 index 0000000..675054f --- /dev/null +++ b/src/device_status/runtime-info/usb.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014 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 { + public: + device_status_usb() : device_status_runtime_info(RUNTIME_INFO_KEY_USB_CONNECTED) {} + ~device_status_usb(){} + + int read(const char* subject, const char* zone); + bool is_supported(const char* subject, const char* zone); + + private: + 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..b4e267a --- /dev/null +++ b/src/device_status/wifi.cpp @@ -0,0 +1,214 @@ +/* + * 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 +#include "wifi.h" + +ctx::device_status_wifi::device_status_wifi() + : last_state(_UNKNOWN) + , is_initialized(false) + , is_activated(false) +{ +} + +bool ctx::device_status_wifi::is_supported(const char* subject, const char* zone) +{ + bool supported = false; + + int ret = system_info_get_platform_bool("tizen.org/feature/network.wifi", &supported); + IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, false, _E, "wifi is_supported() failed"); + + return supported; +} + +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; + } else { + last_state = _UNCONNECTED; + } + } else { + last_state = _DISABLED; + } + + if (!is_initialized) + wifi_deinitialize(); + + return true; +} + +bool ctx::device_status_wifi::get_bssid(std::string& 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); + + return !bssid.empty(); +} + +bool ctx::device_status_wifi::get_response_packet(ctx::json &data) +{ + std::string bssid; + + 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); + if (get_bssid(bssid)) + data.set(NULL, DEVICE_ST_BSSID, bssid); + break; + + default: + return false; + } + + return true; +} + +int ctx::device_status_wifi::read(const char* subject, const char* zone) +{ + 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, zone); + 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(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + IF_FAIL_RETURN(start_monitor(), ERR_OPERATION_FAILED); + if (!get_current_state()) { + stop_monitor(); + return ERR_OPERATION_FAILED; + } + } + + client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::device_status_wifi::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + stop_monitor(); + 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) { + ctx::json data; + if (get_response_packet(data)) + publish(DEVICE_ST_SUBJ_WIFI, ERR_NONE, data, client_zone); + } +} + +void ctx::device_status_wifi::device_state_changed_cb(wifi_device_state_e state, void *user_data) +{ + device_status_wifi *instance = static_cast(user_data); + instance->is_initialized = (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(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..0fea0dd --- /dev/null +++ b/src/device_status/wifi.h @@ -0,0 +1,62 @@ +/* + * 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 +#include +#include "../sub_provider_base.h" + +namespace ctx { + + class device_status_wifi : public sub_provider_base { + public: + device_status_wifi(); + ~device_status_wifi(){} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + int read(const char* subject, const char* zone); + bool is_supported(const char* subject, const char* zone); + + private: + enum _state_e { + _UNKNOWN = -1, + _DISABLED = 0, + _UNCONNECTED, + _CONNECTED, + }; + + string_set_t client_zone; + + int last_state; + bool is_initialized; + bool is_activated; + wifi_connection_state_e conn_state; + + bool get_current_state(); + bool get_bssid(std::string &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/social_status/call.cpp b/src/social_status/call.cpp new file mode 100644 index 0000000..e3b657b --- /dev/null +++ b/src/social_status/call.cpp @@ -0,0 +1,247 @@ +/* + * Copyright (c) 2014 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 +#include +#include +#include +#include +#include "call.h" + +static bool telephony_initialized = false; +static TapiHandle* tapi_handle = NULL; + +static void call_status_cb(TelCallStatus_t *out, void *user_data) +{ + //TODO: Can this be called multiple times? + + std::string *addr = static_cast(user_data); + *addr = out->pNumber; +} + +ctx::social_status_call::social_status_call() +{ + tapi_handle = NULL; +} + +ctx::social_status_call::~social_status_call() +{ +} + +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(user_data); + instance->handle_call_event(handle, noti_id); +} + +void ctx::social_status_call::handle_call_event(telephony_h handle, telephony_noti_e noti_id) +{ + telephony_call_state_e call_state; + json data; + + switch (noti_id) { + case TELEPHONY_NOTI_VOICE_CALL_STATE: + IF_FAIL_VOID(get_voice_call_state(call_state)); + data.set(NULL, SOCIAL_ST_MEDIUM, SOCIAL_ST_VOICE); + break; + case TELEPHONY_NOTI_VIDEO_CALL_STATE: + IF_FAIL_VOID(get_video_call_state(call_state)); + data.set(NULL, SOCIAL_ST_MEDIUM, SOCIAL_ST_VIDEO); + break; + default: + _E("Unkown noti id: %d", noti_id); + return; + } + + std::string addr; + + switch (call_state) { + case TELEPHONY_CALL_STATE_CONNECTING: + IF_FAIL_VOID(get_call_address(addr)); + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_CONNECTING); + data.set(NULL, SOCIAL_ST_ADDRESS, addr); + break; + case TELEPHONY_CALL_STATE_CONNECTED: + IF_FAIL_VOID(get_call_address(addr)); + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_CONNECTED); + data.set(NULL, SOCIAL_ST_ADDRESS, addr); + break; + default: + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_IDLE); + break; + } + + publish(SOCIAL_ST_SUBJ_CALL, ERR_NONE, data, client_zone); +} + +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"); + + tapi_handle = tel_init(NULL); + if (tapi_handle == NULL) { + _E("Initialization failed"); + return false; + } + + telephony_initialized = true; + return true; +} + +void ctx::social_status_call::release_telephony() +{ + IF_FAIL_VOID(telephony_initialized); + + telephony_deinit(&handle_list); + tel_deinit(tapi_handle); + + tapi_handle = NULL; + 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; + err = telephony_set_noti_cb(handle_list.handle[0], TELEPHONY_NOTI_VOICE_CALL_STATE, call_event_cb, this); + IF_FAIL_CATCH(err == TELEPHONY_ERROR_NONE); + + err = telephony_set_noti_cb(handle_list.handle[0], TELEPHONY_NOTI_VIDEO_CALL_STATE, 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() +{ + telephony_unset_noti_cb(handle_list.handle[0], TELEPHONY_NOTI_VOICE_CALL_STATE); + telephony_unset_noti_cb(handle_list.handle[0], TELEPHONY_NOTI_VIDEO_CALL_STATE); + release_telephony(); +} + +bool ctx::social_status_call::get_voice_call_state(telephony_call_state_e& state) +{ + int ret = telephony_call_get_voice_call_state(handle_list.handle[0], &state); + IF_FAIL_RETURN_TAG(ret == TELEPHONY_ERROR_NONE, false, _E, "Getting status failed"); + return true; +} + +bool ctx::social_status_call::get_video_call_state(telephony_call_state_e& state) +{ + int ret = telephony_call_get_video_call_state(handle_list.handle[0], &state); + IF_FAIL_RETURN_TAG(ret == TELEPHONY_ERROR_NONE, false, _E, "Getting status failed"); + return true; +} + +bool ctx::social_status_call::get_call_address(std::string& address) +{ + address.clear(); + tel_get_call_status_all(tapi_handle, call_status_cb, &address); + IF_FAIL_RETURN_TAG(!address.empty(), false, _W, "Getting address failed"); + return true; +} + +int ctx::social_status_call::subscribe(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + bool ret = set_callback(); + IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED); + } + + client_zone.insert(zone); + return ERR_NONE; +} + + +int ctx::social_status_call::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + unset_callback(); + return ERR_NONE; +} + +bool ctx::social_status_call::read_current_status(ctx::json& data) +{ + telephony_call_state_e voice_state; + telephony_call_state_e video_state; + + if (!get_voice_call_state(voice_state)) + voice_state = TELEPHONY_CALL_STATE_IDLE; + + if (!get_video_call_state(video_state)) + video_state = TELEPHONY_CALL_STATE_IDLE; + + if (voice_state == TELEPHONY_CALL_STATE_IDLE && video_state == TELEPHONY_CALL_STATE_IDLE) { + // Both types of calls are idle + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_IDLE); + return true; + } + + std::string addr; + IF_FAIL_RETURN(get_call_address(addr), false); + + data.set(NULL, SOCIAL_ST_ADDRESS, addr); + + if (voice_state == TELEPHONY_CALL_STATE_CONNECTED || video_state == TELEPHONY_CALL_STATE_CONNECTED) { + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_CONNECTED); + } else { + data.set(NULL, SOCIAL_ST_STATE, SOCIAL_ST_CONNECTING); + } + + return true; +} + +int ctx::social_status_call::read(const char* subject, const char* zone) +{ + bool temporary_handle = false; + if (!telephony_initialized) { + IF_FAIL_RETURN(init_telephony(), ERR_OPERATION_FAILED); + temporary_handle = true; + } + + json data; + bool ret = read_current_status(data); + + if (temporary_handle) + release_telephony(); + + if (ret) { + ctx::context_manager::reply_to_read(SOCIAL_ST_SUBJ_CALL, NULL, ERR_NONE, data, zone); + return ERR_NONE; + } + + return ERR_OPERATION_FAILED; +} + +bool ctx::social_status_call::is_supported(const char* subject, const char* zone) +{ + bool supported = false; + int ret = system_info_get_platform_bool("tizen.org/feature/network.telephony", &supported); + IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, ERR_OPERATION_FAILED, _E, "support check failed"); + return supported; +} diff --git a/src/social_status/call.h b/src/social_status/call.h new file mode 100644 index 0000000..24d2030 --- /dev/null +++ b/src/social_status/call.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014 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 +#include "../sub_provider_base.h" + +namespace ctx { + + class social_status_call : public sub_provider_base { + public: + social_status_call(); + ~social_status_call(); + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + int read(const char* subject, const char* zone); + bool is_supported(const char* subject, const char* zone); + + private: + telephony_handle_list_s handle_list; + string_set_t client_zone; + + bool init_telephony(); + void release_telephony(); + bool set_callback(); + void unset_callback(); + bool read_current_status(ctx::json& data); + + bool get_voice_call_state(telephony_call_state_e& state); + bool get_video_call_state(telephony_call_state_e& state); + bool get_call_address(std::string& address); + + void handle_call_event(telephony_h handle, telephony_noti_e noti_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..e42689e --- /dev/null +++ b/src/social_status/email.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2014 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 +#include +#include + +#include +#include "email.h" + + +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); + publish(SOCIAL_ST_SUBJ_EMAIL, ERR_NONE, data_updated, client_zone); + + } 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); + publish(SOCIAL_ST_SUBJ_EMAIL, ERR_NONE, data_updated, client_zone); + } +} + + +int ctx::social_status_email::subscribe(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + 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"); + } + + client_zone.insert(zone); + return ERR_NONE; +} + + +int ctx::social_status_email::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + 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..8fbbb61 --- /dev/null +++ b/src/social_status/email.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 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 +#include "../sub_provider_base.h" + +namespace ctx { + + class social_status_email : public sub_provider_base, public dbus_listener_iface { + public: + social_status_email() {} + ~social_status_email() {} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + + void on_signal_received(const char* sender, const char* path, const char* iface, const char* name, GVariant* param); + + private: + int64_t dbus_signal_id; + string_set_t client_zone; + }; +} + +#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..70646e2 --- /dev/null +++ b/src/social_status/message.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2014 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 +#include +#include "message.h" + +#define MAX_ADDR_SIZE 20 + +ctx::social_status_message::social_status_message() + : message_handle(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(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); + + publish(SOCIAL_ST_SUBJ_MESSAGE, ERR_NONE, data, client_zone); +} + +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(const char* subject, const char* zone) +{ + if (client_zone.empty()) { + bool ret = set_callback(); + IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED); + } + + client_zone.insert(zone); + return ERR_NONE; +} + +int ctx::social_status_message::unsubscribe(const char* subject, const char* zone) +{ + client_zone.erase(zone); + IF_FAIL_RETURN(client_zone.empty(), ERR_NONE); + + unset_callback(); + return ERR_NONE; +} + +bool ctx::social_status_message::is_supported(const char* subject, const char* zone) +{ + bool supported = false; + int ret = system_info_get_platform_bool("tizen.org/feature/telephony", &supported); + IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, ERR_OPERATION_FAILED, _E, "support check failed"); + return supported; +} diff --git a/src/social_status/message.h b/src/social_status/message.h new file mode 100644 index 0000000..44c8bff --- /dev/null +++ b/src/social_status/message.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014 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 +#include +#include "../sub_provider_base.h" + +namespace ctx { + + class social_status_message : public sub_provider_base { + public: + social_status_message(); + ~social_status_message() {} + + int subscribe(const char* subject, const char* zone); + int unsubscribe(const char* subject, const char* zone); + bool is_supported(const char* subject, const char* zone); + + private: + msg_handle_t message_handle; + string_set_t client_zone; + + 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..c17c261 --- /dev/null +++ b/src/social_status/social_status_types.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014 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/state/call" +#define SOCIAL_ST_SUBJ_EMAIL "social/event/email" +#define SOCIAL_ST_SUBJ_MESSAGE "social/event/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_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 diff --git a/src/sub_provider_base.cpp b/src/sub_provider_base.cpp new file mode 100644 index 0000000..3c7fa8e --- /dev/null +++ b/src/sub_provider_base.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2014 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 "sub_provider_base.h" + +int ctx::sub_provider_base::subscribe(const char* subject, const char* zone) +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::sub_provider_base::unsubscribe(const char* subject, const char* zone) +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::sub_provider_base::read(const char* subject, const char* zone) +{ + return ERR_NOT_SUPPORTED; +} + +bool ctx::sub_provider_base::is_supported(const char* subject, const char* zone) +{ + // Generally, if a sub-analyzer is implemented by inheriting this class, "subject" is supported. + return true; +} + +void ctx::sub_provider_base::publish(const char* subject, int error, ctx::json data, string_set_t& zone_list) +{ + ctx::json option; + for (string_set_t::iterator it = zone_list.begin(); it != zone_list.end(); ++it) { + _D("Publishing '%s' on '%s'", subject, (*it).c_str()); + ctx::context_manager::publish(subject, option, error, data, (*it).c_str()); + } +} diff --git a/src/sub_provider_base.h b/src/sub_provider_base.h new file mode 100644 index 0000000..0fc8e44 --- /dev/null +++ b/src/sub_provider_base.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2014 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_SUB_PROVIDER_BASE_H__ +#define __CONTEXT_DEVICE_SUB_PROVIDER_BASE_H__ + +#include +#include +#include +#include +#include +#include "device_status/device_status_types.h" +#include "social_status/social_status_types.h" + +namespace ctx { + + typedef std::set string_set_t; + + class sub_provider_base { + public: + sub_provider_base(){} + virtual ~sub_provider_base(){} + + virtual int subscribe(const char* subject, const char* zone); + virtual int unsubscribe(const char* subject, const char* zone); + virtual int read(const char* subject, const char* zone); + virtual bool is_supported(const char* subject, const char* zone); + + protected: + void publish(const char* subject, int error, ctx::json data, string_set_t& zone_list); + }; +} + +#endif