2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <SecurityExceptions.h>
20 #include <JSWebAPIError.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <MultiCallbackUserData.h>
24 #include <PlatformException.h>
27 #include "plugin_config.h"
28 #include "JSSensorService.h"
29 #include "SensorService.h"
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
37 JSClassDefinition JSSensorService::m_classInfo = {
39 kJSClassAttributeNone,
43 m_function, //StaticFunctions
44 initialize, //Initialize
49 NULL, //DeleteProperty,
50 NULL, //GetPropertyNames,
51 NULL, //CallAsFunction,
52 NULL, //CallAsConstructor,
57 JSStaticFunction JSSensorService::m_function[] = {
58 { SENSOR_SERVICE_API_GET_DEFAULT_SENSOR, getDefaultSensor, kJSPropertyAttributeNone },
59 { SENSOR_SERVICE_API_GET_AVAILABLE_SENSOR, getAvailableSensor, kJSPropertyAttributeNone },
63 JSClassRef JSSensorService::m_jsClassRef = JSClassCreate(JSSensorService::getClassInfo());
65 const JSClassRef JSSensorService::getClassRef()
68 m_jsClassRef = JSClassCreate(&m_classInfo);
73 const JSClassDefinition* JSSensorService::getClassInfo()
78 void JSSensorService::initialize(JSContextRef context, JSObjectRef object)
83 void JSSensorService::finalize(JSObjectRef object)
88 JSValueRef JSSensorService::getDefaultSensor(JSContextRef context,
90 JSObjectRef thisObject,
92 const JSValueRef arguments[],
93 JSValueRef* exception)
98 bool supported = false;
99 ArgumentValidator validator(context, argumentCount, arguments);
100 std::string sensorType = validator.toString(0);
101 std::string proximityType = "PROXIMITY";
102 std::string lightType = "LIGHT";
103 std::string magneticType = "MAGNETIC";
104 std::string pressureType = "PRESSURE";
105 std::string ultravioletType = "ULTRAVIOLET";
106 std::string hrmrawType = "HRM_RAW";
109 if(proximityType.compare(sensorType) == 0){
110 ProximitySensor* proximitySensor = ProximitySensor::getInstance();
112 supported = proximitySensor->isSupported();
113 if(false == supported){
114 LOGE("Not supported");
115 throw NotSupportedException("Not supported");
118 JSValueRef proximityRef = JSProximitySensor::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), proximitySensor);
120 }else if(lightType.compare(sensorType) == 0){
121 LightSensor* lightSensor = LightSensor::getInstance();
123 supported = lightSensor->isSupported();
124 if(false == supported){
125 LOGE("Not supported");
126 throw NotSupportedException("Not supported");
129 JSValueRef lightRef = JSLightSensor::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), lightSensor);
131 }else if(magneticType.compare(sensorType) == 0){
132 MagneticSensor* magneticSensor = MagneticSensor::getInstance();
134 supported = magneticSensor->isSupported();
135 if(false == supported){
136 LOGE("Not supported");
137 throw NotSupportedException("Not supported");
140 JSValueRef magneticRef = JSMagneticSensor::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), magneticSensor);
142 }else if(pressureType.compare(sensorType) == 0){
143 PressureSensor* pressureSensor = PressureSensor::getInstance();
145 supported = pressureSensor->isSupported();
146 if(false == supported){
147 LOGE("Not supported");
148 throw NotSupportedException("Not supported");
151 JSValueRef pressureRef = JSPressureSensor::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), pressureSensor);
153 }else if(ultravioletType.compare(sensorType) == 0){
154 UltravioletSensor* ultravioletSensor = UltravioletSensor::getInstance();
156 supported = ultravioletSensor->isSupported();
157 if(false == supported){
158 LOGE("Not supported");
159 throw NotSupportedException("Not supported");
162 JSValueRef uvsensorRef = JSUltravioletSensor::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), ultravioletSensor);
164 }else if (hrmrawType.compare(sensorType) == 0) {
165 AceSecurityStatus status = SENSOR_CHECK_ACCESS(SENSOR_SERVICE_API_GET_DEFAULT_SENSOR);
166 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
167 HRMRawSensor* hrmrawSensor = HRMRawSensor::getInstance();
169 supported = hrmrawSensor->isSupported();
170 if (false == supported){
171 LOGD("Not supported");
172 throw NotSupportedException("Not supported");
174 JSValueRef hrmrawRef = JSHRMRawSensor::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), hrmrawSensor);
177 LOGE("argument type mismatch");
178 throw TypeMismatchException("argument type mismatch");
181 } catch (const TypeMismatchException &err) {
182 LOGE("Type Mismatch Exception");
183 return JSWebAPIErrorFactory::postException(context, exception, err);
184 } catch (const BasePlatformException &err) {
185 LOGE("argument is wrong.");
186 return JSWebAPIErrorFactory::postException(context, exception, err);
191 JSValueRef JSSensorService::getAvailableSensor(JSContextRef context,
193 JSObjectRef thisObject,
194 size_t argumentCount,
195 const JSValueRef arguments[],
196 JSValueRef* exception)
200 return SensorService::getInstance()->getAvailableSensor(GlobalContextManager::getInstance()->getGlobalContext(context));
201 } catch (const TypeMismatchException &err) {
202 LOGE("Type Mismatch Exception");
203 return JSWebAPIErrorFactory::postException(context, exception, err);
204 } catch (const BasePlatformException &err) {
205 LOGE("argument is wrong.");
206 return JSWebAPIErrorFactory::postException(context, exception, err);
212 JSClassDefinition JSProximitySensor::m_classInfo = {
214 kJSClassAttributeNone,
217 m_property, //StaticValues
218 m_function, //StaticFunctions
219 initialize, //Initialize
224 NULL, //DeleteProperty,
225 NULL, //GetPropertyNames,
226 NULL, //CallAsFunction,
227 NULL, //CallAsConstructor,
232 JSStaticValue JSProximitySensor::m_property[] = {
233 { SENSOR_ATTRIBUTE_SENSOR_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
238 JSStaticFunction JSProximitySensor::m_function[] = {
239 { SENSOR_API_START, start, kJSPropertyAttributeNone },
240 { SENSOR_API_STOP, stop, kJSPropertyAttributeNone },
241 { SENSOR_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
242 { SENSOR_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
243 { SENSOR_API_GET_PROXIMITY_SENSOR_DATA, getSensorData, kJSPropertyAttributeNone },
247 JSClassRef JSProximitySensor::m_jsClassRef = JSClassCreate(JSProximitySensor::getClassInfo());
249 const JSClassRef JSProximitySensor::getClassRef()
252 m_jsClassRef = JSClassCreate(&m_classInfo);
257 const JSClassDefinition* JSProximitySensor::getClassInfo()
263 JSValueRef JSProximitySensor::createJSObject(JSContextRef context, ProximitySensor* proximitySensor)
265 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(proximitySensor));
266 if (NULL == jsValueRef) {
267 LOGE("object creation error");
268 return JSValueMakeUndefined(context);
275 void JSProximitySensor::initialize(JSContextRef context, JSObjectRef object)
280 void JSProximitySensor::finalize(JSObjectRef object)
285 JSValueRef JSProximitySensor::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
287 std::string sensorType("");
290 if (JSStringIsEqualToUTF8CString(propertyName, SENSOR_ATTRIBUTE_SENSOR_TYPE)) {
291 sensorType.append("PROXIMITY");
292 return JSUtil::toJSValueRef(context, sensorType);
294 return JSValueMakeUndefined(context);
297 JSValueRef JSProximitySensor::start(JSContextRef context,
299 JSObjectRef thisObject,
300 size_t argumentCount,
301 const JSValueRef arguments[],
302 JSValueRef* exception)
307 ArgumentValidator validator(context, argumentCount, arguments);
308 JSObjectRef successCB = validator.toFunction(0);
310 ProximitySuccessCallbackPtr proximityCB(new ProximitySuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
312 proximityCB->setSuccessCallback(successCB);
315 JSObjectRef errorCB = validator.toFunction(1, true);
317 proximityCB->setErrorCallback(errorCB);
320 ProximitySensor::getInstance()->start(proximityCB);
321 return JSValueMakeUndefined(context);
323 } catch (const BasePlatformException &err) {
324 LOGE("argument is wrong.");
325 return JSWebAPIErrorFactory::postException(context, exception, err);
330 JSValueRef JSProximitySensor::stop(JSContextRef context,
332 JSObjectRef thisObject,
333 size_t argumentCount,
334 const JSValueRef arguments[],
335 JSValueRef* exception)
340 ProximitySensor::getInstance()->stop(GlobalContextManager::getInstance()->getGlobalContext(context));
341 return JSValueMakeUndefined(context);
343 } catch (const BasePlatformException &err) {
344 LOGE("argument is wrong.");
345 return JSWebAPIErrorFactory::postException(context, exception, err);
350 JSValueRef JSProximitySensor::setChangeListener(JSContextRef context,
352 JSObjectRef thisObject,
353 size_t argumentCount,
354 const JSValueRef arguments[],
355 JSValueRef* exception)
360 ArgumentValidator validator(context, argumentCount, arguments);
361 JSObjectRef successCB = validator.toFunction(0);
363 ProximitySuccessCallbackPtr proximityCB(new ProximitySuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
365 proximityCB->setSuccessCallback(successCB);
368 JSObjectRef errorCB = validator.toFunction(1, true);
370 proximityCB->setErrorCallback(errorCB);
373 ProximitySensor::getInstance()->setChangeListener(proximityCB);
374 return JSValueMakeUndefined(context);
376 } catch (const BasePlatformException &err) {
377 LOGE("argument is wrong.");
378 return JSWebAPIErrorFactory::postException(context, exception, err);
385 JSValueRef JSProximitySensor::unsetChangeListener(JSContextRef context,
387 JSObjectRef thisObject,
388 size_t argumentCount,
389 const JSValueRef arguments[],
390 JSValueRef* exception)
395 ProximitySensor::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context));
396 return JSValueMakeUndefined(context);
398 } catch (const BasePlatformException &err) {
399 LOGE("argument is wrong.");
400 return JSWebAPIErrorFactory::postException(context, exception, err);
406 JSValueRef JSProximitySensor::getSensorData(JSContextRef context,
408 JSObjectRef thisObject,
409 size_t argumentCount,
410 const JSValueRef arguments[],
411 JSValueRef* exception)
417 ArgumentValidator validator(context, argumentCount, arguments);
418 JSObjectRef successCB = validator.toFunction(0);
420 ProximitySuccessCallbackPtr proximityCB(new ProximitySuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
422 proximityCB->setSuccessCallback(successCB);
425 JSObjectRef errorCB = validator.toFunction(1, true);
427 proximityCB->setErrorCallback(errorCB);
430 ProximitySensor::getInstance()->getSensorData(proximityCB);
431 return JSValueMakeUndefined(context);
433 } catch (const BasePlatformException &err) {
434 LOGE("argument is wrong.");
435 return JSWebAPIErrorFactory::postException(context, exception, err);
440 JSClassDefinition JSLightSensor::m_classInfo = {
442 kJSClassAttributeNone,
445 m_property, //StaticValues
446 m_function, //StaticFunctions
447 initialize, //Initialize
452 NULL, //DeleteProperty,
453 NULL, //GetPropertyNames,
454 NULL, //CallAsFunction,
455 NULL, //CallAsConstructor,
460 JSStaticFunction JSLightSensor::m_function[] = {
461 { SENSOR_API_START, start, kJSPropertyAttributeNone },
462 { SENSOR_API_STOP, stop, kJSPropertyAttributeNone },
463 { SENSOR_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
464 { SENSOR_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
465 { SENSOR_API_GET_LIGHT_SENSOR_DATA, getSensorData, kJSPropertyAttributeNone },
469 JSStaticValue JSLightSensor::m_property[] = {
470 { SENSOR_ATTRIBUTE_SENSOR_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
474 JSClassRef JSLightSensor::m_jsClassRef = JSClassCreate(JSLightSensor::getClassInfo());
476 const JSClassRef JSLightSensor::getClassRef()
479 m_jsClassRef = JSClassCreate(&m_classInfo);
484 const JSClassDefinition* JSLightSensor::getClassInfo()
490 JSValueRef JSLightSensor::createJSObject(JSContextRef context, LightSensor* sensor)
492 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(sensor));
493 if (NULL == jsValueRef) {
494 LOGE("object creation error");
495 return JSValueMakeUndefined(context);
502 void JSLightSensor::initialize(JSContextRef context, JSObjectRef object)
507 void JSLightSensor::finalize(JSObjectRef object)
512 JSValueRef JSLightSensor::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
514 std::string sensorType("");
517 if (JSStringIsEqualToUTF8CString(propertyName, SENSOR_ATTRIBUTE_SENSOR_TYPE)) {
518 sensorType.append("LIGHT");
519 return JSUtil::toJSValueRef(context, sensorType);
521 return JSValueMakeUndefined(context);
525 JSValueRef JSLightSensor::start(JSContextRef context,
527 JSObjectRef thisObject,
528 size_t argumentCount,
529 const JSValueRef arguments[],
530 JSValueRef* exception)
535 ArgumentValidator validator(context, argumentCount, arguments);
536 JSObjectRef successCB = validator.toFunction(0);
538 LightSuccessCallbackPtr lightCB(new LightSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
540 lightCB->setSuccessCallback(successCB);
543 JSObjectRef errorCB = validator.toFunction(1, true);
545 lightCB->setErrorCallback(errorCB);
548 LightSensor::getInstance()->start(lightCB);
549 return JSValueMakeUndefined(context);
551 } catch (const BasePlatformException &err) {
552 LOGE("argument is wrong.");
553 return JSWebAPIErrorFactory::postException(context, exception, err);
558 JSValueRef JSLightSensor::stop(JSContextRef context,
560 JSObjectRef thisObject,
561 size_t argumentCount,
562 const JSValueRef arguments[],
563 JSValueRef* exception)
568 LightSensor::getInstance()->stop(GlobalContextManager::getInstance()->getGlobalContext(context));
569 return JSValueMakeUndefined(context);
570 } catch (const BasePlatformException &err) {
571 LOGE("argument is wrong.");
572 return JSWebAPIErrorFactory::postException(context, exception, err);
577 JSValueRef JSLightSensor::setChangeListener(JSContextRef context,
579 JSObjectRef thisObject,
580 size_t argumentCount,
581 const JSValueRef arguments[],
582 JSValueRef* exception)
587 ArgumentValidator validator(context, argumentCount, arguments);
588 JSObjectRef successCB = validator.toFunction(0);
590 LightSuccessCallbackPtr lightCB(new LightSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
592 lightCB->setSuccessCallback(successCB);
595 JSObjectRef errorCB = validator.toFunction(1, true);
597 lightCB->setErrorCallback(errorCB);
600 LightSensor::getInstance()->setChangeListener(lightCB);
601 return JSValueMakeUndefined(context);
603 } catch (const BasePlatformException &err) {
604 LOGE("argument is wrong.");
605 return JSWebAPIErrorFactory::postException(context, exception, err);
612 JSValueRef JSLightSensor::unsetChangeListener(JSContextRef context,
614 JSObjectRef thisObject,
615 size_t argumentCount,
616 const JSValueRef arguments[],
617 JSValueRef* exception)
622 LightSensor::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context));
623 return JSValueMakeUndefined(context);
625 } catch (const BasePlatformException &err) {
626 LOGE("argument is wrong.");
627 return JSWebAPIErrorFactory::postException(context, exception, err);
631 JSValueRef JSLightSensor::getSensorData(JSContextRef context,
633 JSObjectRef thisObject,
634 size_t argumentCount,
635 const JSValueRef arguments[],
636 JSValueRef* exception)
642 ArgumentValidator validator(context, argumentCount, arguments);
643 JSObjectRef successCB = validator.toFunction(0);
645 LightSuccessCallbackPtr lightCB(new LightSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
647 lightCB->setSuccessCallback(successCB);
650 JSObjectRef errorCB = validator.toFunction(1, true);
652 lightCB->setErrorCallback(errorCB);
655 LightSensor::getInstance()->getSensorData(lightCB);
656 return JSValueMakeUndefined(context);
658 } catch (const BasePlatformException &err) {
659 LOGE("argument is wrong.");
660 return JSWebAPIErrorFactory::postException(context, exception, err);
666 JSClassDefinition JSMagneticSensor::m_classInfo = {
668 kJSClassAttributeNone,
671 m_property, //StaticValues
672 m_function, //StaticFunctions
673 initialize, //Initialize
678 NULL, //DeleteProperty,
679 NULL, //GetPropertyNames,
680 NULL, //CallAsFunction,
681 NULL, //CallAsConstructor,
686 JSStaticFunction JSMagneticSensor::m_function[] = {
687 { SENSOR_API_START, start, kJSPropertyAttributeNone },
688 { SENSOR_API_STOP, stop, kJSPropertyAttributeNone },
689 { SENSOR_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
690 { SENSOR_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
691 { SENSOR_API_GET_MAGNETIC_SENSOR_DATA, getSensorData, kJSPropertyAttributeNone },
695 JSStaticValue JSMagneticSensor::m_property[] = {
696 { SENSOR_ATTRIBUTE_SENSOR_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
701 JSClassRef JSMagneticSensor::m_jsClassRef = JSClassCreate(JSMagneticSensor::getClassInfo());
703 const JSClassRef JSMagneticSensor::getClassRef()
706 m_jsClassRef = JSClassCreate(&m_classInfo);
711 const JSClassDefinition* JSMagneticSensor::getClassInfo()
717 JSValueRef JSMagneticSensor::createJSObject(JSContextRef context, MagneticSensor* sensor)
719 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(sensor));
720 if (NULL == jsValueRef) {
721 LOGE("object creation error");
722 return JSValueMakeUndefined(context);
729 void JSMagneticSensor::initialize(JSContextRef context, JSObjectRef object)
734 void JSMagneticSensor::finalize(JSObjectRef object)
740 JSValueRef JSMagneticSensor::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
742 std::string sensorType("");
745 if (JSStringIsEqualToUTF8CString(propertyName, SENSOR_ATTRIBUTE_SENSOR_TYPE)) {
746 sensorType.append("MAGNETIC");
747 return JSUtil::toJSValueRef(context, sensorType);
749 return JSValueMakeUndefined(context);
752 JSValueRef JSMagneticSensor::start(JSContextRef context,
754 JSObjectRef thisObject,
755 size_t argumentCount,
756 const JSValueRef arguments[],
757 JSValueRef* exception)
762 ArgumentValidator validator(context, argumentCount, arguments);
763 JSObjectRef successCB = validator.toFunction(0);
765 MagneticSuccessCallbackPtr magneticCB(new MagneticSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
767 magneticCB->setSuccessCallback(successCB);
770 JSObjectRef errorCB = validator.toFunction(1, true);
772 magneticCB->setErrorCallback(errorCB);
775 MagneticSensor::getInstance()->start(magneticCB);
776 return JSValueMakeUndefined(context);
778 } catch (const BasePlatformException &err) {
779 LOGE("argument is wrong.");
780 return JSWebAPIErrorFactory::postException(context, exception, err);
785 JSValueRef JSMagneticSensor::stop(JSContextRef context,
787 JSObjectRef thisObject,
788 size_t argumentCount,
789 const JSValueRef arguments[],
790 JSValueRef* exception)
795 MagneticSensor::getInstance()->stop(GlobalContextManager::getInstance()->getGlobalContext(context));
796 return JSValueMakeUndefined(context);
797 } catch (const BasePlatformException &err) {
798 LOGE("argument is wrong.");
799 return JSWebAPIErrorFactory::postException(context, exception, err);
804 JSValueRef JSMagneticSensor::setChangeListener(JSContextRef context,
806 JSObjectRef thisObject,
807 size_t argumentCount,
808 const JSValueRef arguments[],
809 JSValueRef* exception)
814 ArgumentValidator validator(context, argumentCount, arguments);
815 JSObjectRef successCB = validator.toFunction(0);
817 MagneticSuccessCallbackPtr magneticCB(new MagneticSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
819 magneticCB->setSuccessCallback(successCB);
822 JSObjectRef errorCB = validator.toFunction(1, true);
824 magneticCB->setErrorCallback(errorCB);
827 MagneticSensor::getInstance()->setChangeListener(magneticCB);
828 return JSValueMakeUndefined(context);
830 } catch (const BasePlatformException &err) {
831 LOGE("argument is wrong.");
832 return JSWebAPIErrorFactory::postException(context, exception, err);
839 JSValueRef JSMagneticSensor::unsetChangeListener(JSContextRef context,
841 JSObjectRef thisObject,
842 size_t argumentCount,
843 const JSValueRef arguments[],
844 JSValueRef* exception)
849 MagneticSensor::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context));
850 return JSValueMakeUndefined(context);
852 } catch (const BasePlatformException &err) {
853 LOGE("argument is wrong.");
854 return JSWebAPIErrorFactory::postException(context, exception, err);
860 JSValueRef JSMagneticSensor::getSensorData(JSContextRef context,
862 JSObjectRef thisObject,
863 size_t argumentCount,
864 const JSValueRef arguments[],
865 JSValueRef* exception)
871 ArgumentValidator validator(context, argumentCount, arguments);
872 JSObjectRef successCB = validator.toFunction(0);
874 MagneticSuccessCallbackPtr magneticCB(new MagneticSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
876 magneticCB->setSuccessCallback(successCB);
879 JSObjectRef errorCB = validator.toFunction(1, true);
881 magneticCB->setErrorCallback(errorCB);
884 MagneticSensor::getInstance()->getSensorData(magneticCB);
885 return JSValueMakeUndefined(context);
887 } catch (const BasePlatformException &err) {
888 LOGE("argument is wrong.");
889 return JSWebAPIErrorFactory::postException(context, exception, err);
896 JSClassDefinition JSPressureSensor::m_classInfo = {
898 kJSClassAttributeNone,
901 m_property, //StaticValues
902 m_function, //StaticFunctions
903 initialize, //Initialize
908 NULL, //DeleteProperty,
909 NULL, //GetPropertyNames,
910 NULL, //CallAsFunction,
911 NULL, //CallAsConstructor,
916 JSStaticFunction JSPressureSensor::m_function[] = {
917 { SENSOR_API_START, start, kJSPropertyAttributeNone },
918 { SENSOR_API_STOP, stop, kJSPropertyAttributeNone },
919 { SENSOR_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
920 { SENSOR_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
921 { SENSOR_API_GET_PRESSURE_SENSOR_DATA, getSensorData, kJSPropertyAttributeNone },
925 JSStaticValue JSPressureSensor::m_property[] = {
926 { SENSOR_ATTRIBUTE_SENSOR_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
931 JSClassRef JSPressureSensor::m_jsClassRef = JSClassCreate(JSPressureSensor::getClassInfo());
933 const JSClassRef JSPressureSensor::getClassRef()
936 m_jsClassRef = JSClassCreate(&m_classInfo);
941 const JSClassDefinition* JSPressureSensor::getClassInfo()
947 JSValueRef JSPressureSensor::createJSObject(JSContextRef context, PressureSensor* sensor)
949 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(sensor));
950 if (NULL == jsValueRef) {
951 LOGE("object creation error");
952 return JSValueMakeUndefined(context);
959 void JSPressureSensor::initialize(JSContextRef context, JSObjectRef object)
964 void JSPressureSensor::finalize(JSObjectRef object)
970 JSValueRef JSPressureSensor::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
972 std::string sensorType("");
975 if (JSStringIsEqualToUTF8CString(propertyName, SENSOR_ATTRIBUTE_SENSOR_TYPE)) {
976 sensorType.append("PRESSURE");
977 return JSUtil::toJSValueRef(context, sensorType);
979 return JSValueMakeUndefined(context);
982 JSValueRef JSPressureSensor::start(JSContextRef context,
984 JSObjectRef thisObject,
985 size_t argumentCount,
986 const JSValueRef arguments[],
987 JSValueRef* exception)
992 ArgumentValidator validator(context, argumentCount, arguments);
993 JSObjectRef successCB = validator.toFunction(0);
995 PressureSuccessCallbackPtr pressureCB(new PressureSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
997 pressureCB->setSuccessCallback(successCB);
1000 JSObjectRef errorCB = validator.toFunction(1, true);
1002 pressureCB->setErrorCallback(errorCB);
1005 PressureSensor::getInstance()->start(pressureCB);
1006 return JSValueMakeUndefined(context);
1008 } catch (const BasePlatformException &err) {
1009 LOGE("argument is wrong.");
1010 return JSWebAPIErrorFactory::postException(context, exception, err);
1015 JSValueRef JSPressureSensor::stop(JSContextRef context,
1017 JSObjectRef thisObject,
1018 size_t argumentCount,
1019 const JSValueRef arguments[],
1020 JSValueRef* exception)
1025 PressureSensor::getInstance()->stop(GlobalContextManager::getInstance()->getGlobalContext(context));
1026 return JSValueMakeUndefined(context);
1028 } catch (const BasePlatformException &err) {
1029 LOGE("argument is wrong.");
1030 return JSWebAPIErrorFactory::postException(context, exception, err);
1035 JSValueRef JSPressureSensor::setChangeListener(JSContextRef context,
1037 JSObjectRef thisObject,
1038 size_t argumentCount,
1039 const JSValueRef arguments[],
1040 JSValueRef* exception)
1045 ArgumentValidator validator(context, argumentCount, arguments);
1046 JSObjectRef successCB = validator.toFunction(0);
1048 PressureSuccessCallbackPtr pressureCB(new PressureSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1050 pressureCB->setSuccessCallback(successCB);
1053 JSObjectRef errorCB = validator.toFunction(1, true);
1055 pressureCB->setErrorCallback(errorCB);
1058 PressureSensor::getInstance()->setChangeListener(pressureCB);
1059 return JSValueMakeUndefined(context);
1061 } catch (const BasePlatformException &err) {
1062 LOGE("argument is wrong.");
1063 return JSWebAPIErrorFactory::postException(context, exception, err);
1070 JSValueRef JSPressureSensor::unsetChangeListener(JSContextRef context,
1072 JSObjectRef thisObject,
1073 size_t argumentCount,
1074 const JSValueRef arguments[],
1075 JSValueRef* exception)
1080 PressureSensor::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context));
1081 return JSValueMakeUndefined(context);
1082 } catch (const BasePlatformException &err) {
1083 LOGE("argument is wrong.");
1084 return JSWebAPIErrorFactory::postException(context, exception, err);
1089 JSValueRef JSPressureSensor::getSensorData(JSContextRef context,
1091 JSObjectRef thisObject,
1092 size_t argumentCount,
1093 const JSValueRef arguments[],
1094 JSValueRef* exception)
1099 ArgumentValidator validator(context, argumentCount, arguments);
1100 JSObjectRef successCB = validator.toFunction(0);
1102 PressureSuccessCallbackPtr pressureCB(new PressureSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1104 pressureCB->setSuccessCallback(successCB);
1107 JSObjectRef errorCB = validator.toFunction(1, true);
1109 pressureCB->setErrorCallback(errorCB);
1112 PressureSensor::getInstance()->getSensorData(pressureCB);
1113 return JSValueMakeUndefined(context);
1115 } catch (const BasePlatformException &err) {
1116 LOGE("argument is wrong.");
1117 return JSWebAPIErrorFactory::postException(context, exception, err);
1122 //JSUltravioletSensor
1123 JSClassDefinition JSUltravioletSensor::m_classInfo = {
1125 kJSClassAttributeNone,
1126 "UltravioletSensor",
1128 m_property, //StaticValues
1129 m_function, //StaticFunctions
1130 initialize, //Initialize
1131 finalize, //Finalize
1132 NULL, //HasProperty,
1133 NULL, //GetProperty,
1134 NULL, //SetProperty,
1135 NULL, //DeleteProperty,
1136 NULL, //GetPropertyNames,
1137 NULL, //CallAsFunction,
1138 NULL, //CallAsConstructor,
1139 NULL, //HasInstance,
1140 NULL //ConvertToType
1143 JSStaticValue JSUltravioletSensor::m_property[] = {
1144 { SENSOR_ATTRIBUTE_SENSOR_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
1150 JSStaticFunction JSUltravioletSensor::m_function[] = {
1151 { SENSOR_API_START, start, kJSPropertyAttributeNone },
1152 { SENSOR_API_STOP, stop, kJSPropertyAttributeNone },
1153 { SENSOR_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
1154 { SENSOR_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
1155 { SENSOR_API_GET_ULTRAVIOLET_SENSOR_DATA, getSensorData, kJSPropertyAttributeNone },
1159 JSClassRef JSUltravioletSensor::m_jsClassRef = JSClassCreate(JSUltravioletSensor::getClassInfo());
1161 const JSClassRef JSUltravioletSensor::getClassRef()
1163 if (!m_jsClassRef) {
1164 m_jsClassRef = JSClassCreate(&m_classInfo);
1166 return m_jsClassRef;
1169 const JSClassDefinition* JSUltravioletSensor::getClassInfo()
1171 return &m_classInfo;
1174 JSValueRef JSUltravioletSensor::createJSObject(JSContextRef context, UltravioletSensor* sensor)
1176 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(sensor));
1177 if (NULL == jsValueRef) {
1178 LOGE("object creation error");
1179 return JSValueMakeUndefined(context);
1185 void JSUltravioletSensor::initialize(JSContextRef context, JSObjectRef object)
1190 void JSUltravioletSensor::finalize(JSObjectRef object)
1195 JSValueRef JSUltravioletSensor::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
1197 std::string sensorType("");
1200 if (JSStringIsEqualToUTF8CString(propertyName, SENSOR_ATTRIBUTE_SENSOR_TYPE)) {
1201 sensorType.append("ULTRAVIOLET");
1202 return JSUtil::toJSValueRef(context, sensorType);
1204 return JSValueMakeUndefined(context);
1207 JSValueRef JSUltravioletSensor::start(JSContextRef context,
1209 JSObjectRef thisObject,
1210 size_t argumentCount,
1211 const JSValueRef arguments[],
1212 JSValueRef* exception)
1217 ArgumentValidator validator(context, argumentCount, arguments);
1218 JSObjectRef successCB = validator.toFunction(0);
1220 UltravioletSuccessCallbackPtr ultravioletCB(new UltravioletSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1222 ultravioletCB->setSuccessCallback(successCB);
1225 JSObjectRef errorCB = validator.toFunction(1, true);
1227 ultravioletCB->setErrorCallback(errorCB);
1230 UltravioletSensor::getInstance()->start(ultravioletCB);
1231 return JSValueMakeUndefined(context);
1233 } catch (const BasePlatformException &err) {
1234 LOGE("argument is wrong.");
1235 return JSWebAPIErrorFactory::postException(context, exception, err);
1240 JSValueRef JSUltravioletSensor::stop(JSContextRef context,
1242 JSObjectRef thisObject,
1243 size_t argumentCount,
1244 const JSValueRef arguments[],
1245 JSValueRef* exception)
1249 UltravioletSensor::getInstance()->stop(GlobalContextManager::getInstance()->getGlobalContext(context));
1250 return JSValueMakeUndefined(context);
1252 } catch (const BasePlatformException &err) {
1253 LOGE("argument is wrong.");
1254 return JSWebAPIErrorFactory::postException(context, exception, err);
1259 JSValueRef JSUltravioletSensor::setChangeListener(JSContextRef context,
1261 JSObjectRef thisObject,
1262 size_t argumentCount,
1263 const JSValueRef arguments[],
1264 JSValueRef* exception)
1269 ArgumentValidator validator(context, argumentCount, arguments);
1270 JSObjectRef successCB = validator.toFunction(0);
1272 UltravioletSuccessCallbackPtr ultravioletCB(new UltravioletSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1274 ultravioletCB->setSuccessCallback(successCB);
1277 JSObjectRef errorCB = validator.toFunction(1, true);
1279 ultravioletCB->setErrorCallback(errorCB);
1282 UltravioletSensor::getInstance()->setChangeListener(ultravioletCB);
1283 return JSValueMakeUndefined(context);
1285 } catch (const BasePlatformException &err) {
1286 LOGE("argument is wrong.");
1287 return JSWebAPIErrorFactory::postException(context, exception, err);
1294 JSValueRef JSUltravioletSensor::unsetChangeListener(JSContextRef context,
1296 JSObjectRef thisObject,
1297 size_t argumentCount,
1298 const JSValueRef arguments[],
1299 JSValueRef* exception)
1304 UltravioletSensor::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context));
1305 return JSValueMakeUndefined(context);
1307 } catch (const BasePlatformException &err) {
1308 LOGE("argument is wrong.");
1309 return JSWebAPIErrorFactory::postException(context, exception, err);
1316 JSValueRef JSUltravioletSensor::getSensorData(JSContextRef context,
1318 JSObjectRef thisObject,
1319 size_t argumentCount,
1320 const JSValueRef arguments[],
1321 JSValueRef* exception)
1327 ArgumentValidator validator(context, argumentCount, arguments);
1328 JSObjectRef successCB = validator.toFunction(0);
1330 UltravioletSuccessCallbackPtr ultravioletCB(new UltravioletSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1332 ultravioletCB->setSuccessCallback(successCB);
1335 JSObjectRef errorCB = validator.toFunction(1, true);
1337 ultravioletCB->setErrorCallback(errorCB);
1340 UltravioletSensor::getInstance()->getSensorData(ultravioletCB);
1341 return JSValueMakeUndefined(context);
1343 } catch (const BasePlatformException &err) {
1344 LOGE("argument is wrong.");
1345 return JSWebAPIErrorFactory::postException(context, exception, err);
1351 JSClassDefinition JSHRMRawSensor::m_classInfo = {
1353 kJSClassAttributeNone,
1356 m_property, //StaticValues
1357 m_function, //StaticFunctions
1358 initialize, //Initialize
1359 finalize, //Finalize
1360 NULL, //HasProperty,
1361 NULL, //GetProperty,
1362 NULL, //SetProperty,
1363 NULL, //DeleteProperty,
1364 NULL, //GetPropertyNames,
1365 NULL, //CallAsFunction,
1366 NULL, //CallAsConstructor,
1367 NULL, //HasInstance,
1368 NULL //ConvertToType
1371 JSStaticValue JSHRMRawSensor::m_property[] = {
1372 { SENSOR_ATTRIBUTE_SENSOR_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
1378 JSStaticFunction JSHRMRawSensor::m_function[] = {
1379 { SENSOR_API_START, start, kJSPropertyAttributeNone },
1380 { SENSOR_API_STOP, stop, kJSPropertyAttributeNone },
1381 { SENSOR_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
1382 { SENSOR_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
1383 { SENSOR_API_GET_HRMRAW_SENSOR_DATA, getSensorData, kJSPropertyAttributeNone },
1387 JSClassRef JSHRMRawSensor::m_jsClassRef = JSClassCreate(JSHRMRawSensor::getClassInfo());
1389 const JSClassRef JSHRMRawSensor::getClassRef()
1391 if (!m_jsClassRef) {
1392 m_jsClassRef = JSClassCreate(&m_classInfo);
1394 return m_jsClassRef;
1397 const JSClassDefinition* JSHRMRawSensor::getClassInfo()
1399 return &m_classInfo;
1403 JSValueRef JSHRMRawSensor::createJSObject(JSContextRef context, HRMRawSensor* sensor)
1405 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(sensor));
1406 if (NULL == jsValueRef) {
1407 LOGE("object creation error");
1408 return JSValueMakeUndefined(context);
1415 void JSHRMRawSensor::initialize(JSContextRef context, JSObjectRef object)
1420 void JSHRMRawSensor::finalize(JSObjectRef object)
1425 JSValueRef JSHRMRawSensor::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
1427 std::string sensorType("");
1430 if (JSStringIsEqualToUTF8CString(propertyName, SENSOR_ATTRIBUTE_SENSOR_TYPE)) {
1431 sensorType.append("HRM_RAW");
1432 return JSUtil::toJSValueRef(context, sensorType);
1434 return JSValueMakeUndefined(context);
1438 JSValueRef JSHRMRawSensor::start(JSContextRef context,
1440 JSObjectRef thisObject,
1441 size_t argumentCount,
1442 const JSValueRef arguments[],
1443 JSValueRef* exception)
1448 ArgumentValidator validator(context, argumentCount, arguments);
1449 JSObjectRef successCB = validator.toFunction(0);
1451 HRMRawSuccessCallbackPtr HRMRawCB(new HRMRawSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1453 HRMRawCB->setSuccessCallback(successCB);
1456 JSObjectRef errorCB = validator.toFunction(1, true);
1458 HRMRawCB->setErrorCallback(errorCB);
1461 HRMRawSensor::getInstance()->start(HRMRawCB);
1462 return JSValueMakeUndefined(context);
1464 } catch (const BasePlatformException &err) {
1465 LOGD("argument is wrong.");
1466 return JSWebAPIErrorFactory::postException(context, exception, err);
1471 JSValueRef JSHRMRawSensor::stop(JSContextRef context,
1473 JSObjectRef thisObject,
1474 size_t argumentCount,
1475 const JSValueRef arguments[],
1476 JSValueRef* exception)
1481 HRMRawSensor::getInstance()->stop(GlobalContextManager::getInstance()->getGlobalContext(context));
1482 return JSValueMakeUndefined(context);
1484 } catch (const BasePlatformException &err) {
1485 LOGD("argument is wrong.");
1486 return JSWebAPIErrorFactory::postException(context, exception, err);
1491 JSValueRef JSHRMRawSensor::setChangeListener(JSContextRef context,
1493 JSObjectRef thisObject,
1494 size_t argumentCount,
1495 const JSValueRef arguments[],
1496 JSValueRef* exception)
1501 ArgumentValidator validator(context, argumentCount, arguments);
1502 JSObjectRef successCB = validator.toFunction(0);
1504 HRMRawSuccessCallbackPtr HRMRawCB(new HRMRawSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1506 HRMRawCB->setSuccessCallback(successCB);
1509 JSObjectRef errorCB = validator.toFunction(1, true);
1511 HRMRawCB->setErrorCallback(errorCB);
1514 HRMRawSensor::getInstance()->setChangeListener(HRMRawCB);
1515 return JSValueMakeUndefined(context);
1517 } catch (const BasePlatformException &err) {
1518 LOGD("argument is wrong.");
1519 return JSWebAPIErrorFactory::postException(context, exception, err);
1526 JSValueRef JSHRMRawSensor::unsetChangeListener(JSContextRef context,
1528 JSObjectRef thisObject,
1529 size_t argumentCount,
1530 const JSValueRef arguments[],
1531 JSValueRef* exception)
1536 HRMRawSensor::getInstance()->unsetChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context));
1537 return JSValueMakeUndefined(context);
1539 } catch (const BasePlatformException &err) {
1540 LOGD("argument is wrong.");
1541 return JSWebAPIErrorFactory::postException(context, exception, err);
1548 JSValueRef JSHRMRawSensor::getSensorData(JSContextRef context,
1550 JSObjectRef thisObject,
1551 size_t argumentCount,
1552 const JSValueRef arguments[],
1553 JSValueRef* exception)
1559 ArgumentValidator validator(context, argumentCount, arguments);
1560 JSObjectRef successCB = validator.toFunction(0);
1562 HRMRawSuccessCallbackPtr HRMRawCB(new HRMRawSuccessCallback(GlobalContextManager::getInstance()->getGlobalContext(context)));
1564 HRMRawCB->setSuccessCallback(successCB);
1567 JSObjectRef errorCB = validator.toFunction(1, true);
1569 HRMRawCB->setErrorCallback(errorCB);
1572 HRMRawSensor::getInstance()->getSensorData(HRMRawCB);
1573 return JSValueMakeUndefined(context);
1575 } catch (const BasePlatformException &err) {
1576 LOGD("argument is wrong.");
1577 return JSWebAPIErrorFactory::postException(context, exception, err);