--- /dev/null
+Copyright (c) 2000 - 2011 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 [yyyy] [name of copyright owner]
+
+ 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.
+
--- /dev/null
+/*
+ * Copyright (c) 2018 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 __LOG_H__
+#define __LOG_H__
+
+#include <dlog.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "co2"
+
+#define ERR(fmt, args...) dlog_print(DLOG_ERROR, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args)
+#define WARN(fmt, args...) dlog_print(DLOG_WARN, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args)
+#define INFO(fmt, args...) dlog_print(DLOG_INFO, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args)
+#define DBG(fmt, args...) dlog_print(DLOG_DEBUG, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args)
+
+#define FN_CALL dlog_print(DLOG_DEBUG, LOG_TAG, ">>>>>>>> called")
+#define FN_END dlog_print(DLOG_DEBUG, LOG_TAG, "<<<<<<<< ended")
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __LOG_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2018 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 _RESOURCE_CO2_SENSOR_H_
+#define _RESOURCE_CO2_SENSOR_H_
+
+#include <pthread.h>
+
+extern pthread_mutex_t mutex;
+
+#define UNUSED(x) (void)(x) // unused argument
+#define MUTEX_LOCK pthread_mutex_lock(&mutex)
+#define MUTEX_UNLOCK pthread_mutex_unlock(&mutex)
+#define ADC_MAX_SIZE 1024 // adc max array size
+
+typedef struct __co2_sensor_data__ { // common adc variable structure
+ int index;
+ int count; // adc read count per one secound
+ int bsize; // sensor_value buffer size (up to ADC_MAX_SIZE)
+ short sensor_value[ADC_MAX_SIZE]; // save adc buffer in round-robin method
+ float co2_sensor_average; // co2 average value(ppm) per one secound
+} co2_sensor_data_t;
+
+typedef struct sensor_mg811__ {
+ float zero_point_volts; // refer to datasheet, start co2 valtage
+ float max_point_volts; // refer to datasheet, max co2 voltage
+} sensor_mg811_t;
+
+#endif /* _RESOURCE_CO2_SENSOR_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2018 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 __ST_THINGS_H__
+#define __ST_THINGS_H__
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __ST_THINGS_RTOS__
+#include <st_things/st_things_types.h>
+#else
+#include "st_things_types.h"
+#endif // __ST_THINGS_RTOS__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @brief Set prefix paths (ReadOnly and ReadWrite) for configuration files for the device.
+ * This is Optional API, and should be used if relative location is used in
+ * filePath variable in JSON Configuration file.
+ * @param[in] ro_path Prefix Path for Read Only directory location.
+ * @param[in] rw_path Prefix Path for Read Write directory location.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter(both ro_path and rw_path are NULL).
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_ALREADY_INITIALIZED Stack already initialized.
+ * To set Prefix Paths, stack should be deinitilized first by calling st_things_deinitialize().
+ * @retval #ST_THINGS_ERROR_STACK_RUNNING Stack is currently running.
+ * To set Prefix Paths, stack should be stopped first by calling st_things_stop()
+ * and then deinitialized by calling st_things_deinitialize().
+ */
+int st_things_set_configuration_prefix_path(const char* ro_path, const char* rw_path);
+
+/**
+ * @brief Initializes things stack and returns whether easy-setup is completed or not.
+ * Easy-setup enable users to acquire the ownership of things and to connect the things with the cloud.
+ * After performing easy-setup, users can access things from anywhere through the cloud.
+ * In things stack, easy-setup is a primary and the first operation to be performed on the thing.
+ * Application running on the thing can know whether easy-setup is done already or not.
+ * If easy-setup is done, app can start the things stack by calling st_things_start().
+ * If easy-setup is not done, app can either wait for the user interaction before starting the things stack or
+ * start the things stack directly without waiting for any events(This case is for those things which doesn't
+ * support input capability and for all other unknown cases).
+ * To use a new json file after initialization, stack should be deinitialized
+ * and stopped(if its started already).
+ * @param[in] json_path Path to Json file which defines a thing. Definition includes the device information,
+ * resources and their properties, configuration info for connectivity and easy-setup, etc.
+ * @param[out] easysetup_complete Indicates whether easysetup is completed already or not.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_ALREADY_INITIALIZED Stack already initialized.
+ * To initialize again, stack should be deinitilized first by calling st_things_deinitialize().
+ * @retval #ST_THINGS_ERROR_STACK_RUNNING Stack is currently running.
+ * To initialize again, stack should be stopped first by calling st_things_stop()
+ * and then deinitialized by calling st_things_deinitialize().
+ */
+int st_things_initialize(const char *json_path, bool *easysetup_complete);
+
+/**
+ * @brief Deinitializes things stack.
+ * Stack should have been initialized before calling this API.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_NOT_INITIALIZED Stack is not initialized.
+ * Initialize the stack by calling st_things_initialize().
+ * @retval #ST_THINGS_ERROR_STACK_RUNNING Stack is currently running.
+ * Before deinitialize, stack needs to be stopped by calling st_things_stop().
+ */
+int st_things_deinitialize(void);
+
+/**
+ * @brief Callback for handling GET request.
+ * @param[in] req_msg GET request message.
+ * @param[out] resp_rep Representation that will be set to payload of response.
+ * @return @c true in case of success, otherwise @c false
+ */
+typedef bool (*st_things_get_request_cb)(st_things_get_request_message_s *req_msg, st_things_representation_s *resp_rep);
+
+/**
+ * @brief Callback for handling SET(POST) request.
+ * @param[in] req_msg SET request message.
+ * @param[out] resp_rep Representation that will be set to payload of response.
+ * @return @c true in case of success, otherwise @c false
+ */
+typedef bool (*st_things_set_request_cb)(st_things_set_request_message_s *req_msg, st_things_representation_s *resp_rep);
+
+/**
+ * @brief Callback registration function for handling request messages.
+ * @details The callbacks ensure that a request message will be carried with one of the resource uris from json file of st_things_start().
+ * @remarks Only one callback function can be set with this API.\n
+ * If multiple callbacks are set, the last one is registered only.\n
+ * And the callbacks are called in the internal thread, which is not detached,\n
+ * so application should return it to get the next callbacks.
+ * @param[in] get_cb Reference of the callback function to handle GET request.
+ * @param[in] set_cb Reference of the callback function to handle SET(POST) request.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ */
+int st_things_register_request_cb(st_things_get_request_cb get_cb, st_things_set_request_cb set_cb);
+
+/**
+ * @brief Starts things stack.
+ * Parses the thing definition(whose path is passed to st_things_initialize(), configures the thing,
+ * creates the resources and prepares it for easy-setup.
+ * If easy-setup is not done yet, onboarding will be started using either SoftAP or BLE connection.
+ * Onboarding creates an ad-hoc network between the thing and the client for performing easy-setup.
+ * If easy-setup is already done, thing will be connected with the cloud.
+ * Application can know whether easy-setup is done or not through st_things_initialize API.
+ * Stack should have been initialized before calling this API.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful.
+ * It is also used for the case that the stack is started already.
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_NOT_INITIALIZED Stack is not initialized.
+ * Initialize the stack by calling st_things_initialize().
+ */
+int st_things_start(void);
+
+/**
+ * @brief Stops things stack.
+ * Removes all the data being used internally and releases all the memory allocated for the stack.
+ * Stack should have been initialized and started before calling this API.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_NOT_INITIALIZED Stack is not initialized.
+ * Initialize the stack by calling st_things_initialize().
+ * @retval #ST_THINGS_ERROR_STACK_NOT_STARTED Stack is not started.
+ * Start the stack by calling st_things_start().
+ */
+int st_things_stop(void);
+
+/**
+ * @brief Callback for getting user's opinion regarding device reset.
+ * @return @c true to confirm, otherwise @c to deny
+ */
+typedef bool (*st_things_reset_confirm_cb)(void);
+
+/**
+ * @brief Callback for carrying the result of reset.
+ * @param[in] is_success Result of Stack-reset. (true : success, false : failure)
+ */
+typedef void (*st_things_reset_result_cb)(bool is_success);
+
+/**
+ * @brief Callback registration function for Reset-Confirmation and Reset-Result functions.
+ * @remarks Only one callback function can be set with this API.\n
+ * If multiple callbacks are set, the last one is registered only.\n
+ And the callbacks are called in the internal thread, which is not detached,\n
+ * so application should return it to get the next callbacks.
+ * @param[in] confirm_cb Callback function that will be called to get the user's input when reset is triggered.
+ * @param[in] result_cb Callback function that will be called after the reset process is done.
+ * This parameter can be NULL if notification for result of reset is not needed.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ */
+int st_things_register_reset_cb(st_things_reset_confirm_cb confirm_cb, st_things_reset_result_cb result_cb);
+
+/**
+ * @brief Reset all the data related to security and cloud being used in the stack.
+ * Stack should have been initialized and started before calling this API.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_NOT_INITIALIZED Stack is not intialized.
+ * Initialize the stack by calling st_things_initialize().
+ * @retval #ST_THINGS_ERROR_STACK_NOT_STARTED Stack is not started.
+ * Start the stack by calling st_things_start().
+ */
+int st_things_reset(void);
+
+/**
+ * @brief Callback for carrying the randomly generated PIN info.
+ * @details Device should show the PIN on display.
+ * @param[in] pin_data PIN data in string format.
+ * @param[in] pin_size Length of the PIN String.
+ */
+typedef void (*st_things_pin_generated_cb)(const char *pin_data, const size_t pin_size);
+
+/**
+ * @brief Callback for informing the application to close the PIN display.
+ */
+typedef void (*st_things_pin_display_close_cb)(void);
+
+/**
+ * @brief Callback registration function for getting randomly generated PIN for the PIN-Based Ownership Transfer Request.
+ * @remarks Only one callback function can be set with this API.\n
+ * If multiple callbacks are set, the last one is registered only.\n
+ * And the callbacks are called in the internal thread, which is not detached,\n
+ * so application should return it to get the next callbacks.
+ * @param[in] generated_cb Callback function that will be called when device receives a Ownership Transfer request from client.
+ * @param[in] close_cb Callback function that will be called when Ownership Transfer is done so device can stop showing PIN on display.
+ * This parameter can be NULL if stop triggering is not needed.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ */
+int st_things_register_pin_handling_cb(st_things_pin_generated_cb generated_cb, st_things_pin_display_close_cb close_cb);
+
+/**
+ * @brief Callback for getting user's input regarding mutual verification.
+ * @return @c true true in cse of confirmed, otherwise @c false
+ */
+typedef bool (*st_things_user_confirm_cb)(void);
+
+/**
+ * @brief Callback registration function for getting user confirmation for MUTUAL VERIFICATION BASED JUST WORK Ownership transfer.
+ * @remarks Only one callback function can be set with this API.\n
+ * If multiple callbacks are set, the last one is registered only.\n
+ * And the callbacks are called in the internal thread, which is not detached,\n
+ * so application should return it to get the next callbacks.
+ * @param[in] confirm_cb Callback function that will be called when device receives a confirm request from client.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ */
+int st_things_register_user_confirm_cb(st_things_user_confirm_cb confirm_cb);
+
+/**
+ * @brief Callback for getting the current state of ST Things.
+ * @param[in] things_status ST Things State
+ */
+typedef void (*st_things_status_change_cb)(st_things_status_e things_status);
+
+/**
+ * @brief Callback registration function for getting notified when ST Things state changes.
+ * @remarks Only one callback function can be set with this API.\n
+ * If multiple callbacks are set, the last one is registered only.\n
+ * And the callbacks are called in the internal thread, which is not detached,\n
+ * so application should return it to get the next callbacks.
+ * @param[in] status_cb Refernce of the callback function to get ST Things status
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ */
+int st_things_register_things_status_change_cb(st_things_status_change_cb status_cb);
+
+/**
+ * @brief Notify the observers of a specific resource.
+ * Stack should have been initialized and started before calling this API.
+ * @param[in] resource_uri Resource URI of the resource which will be notified to observers.
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #ST_THINGS_ERROR_NONE Successful
+ * @retval #ST_THINGS_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ST_THINGS_ERROR_OPERATION_FAILED Operation failed
+ * @retval #ST_THINGS_ERROR_STACK_NOT_INITIALIZED Stack is not intialized.
+ * Initialize the stack by calling st_things_initialize().
+ * @retval #ST_THINGS_ERROR_STACK_NOT_STARTED Stack is not started.
+ * Start the stack by calling st_things_start().
+ */
+int st_things_notify_observers(const char *resource_uri);
+
+/**
+ * @brief Create an instance of representation.
+ * @remarks To destroy an instance, st_things_destroy_representation_inst() should be used.
+ * @return a pointer of the created representation, otherwise a null pointer if the memory is insufficient.
+ */
+st_things_representation_s *st_things_create_representation_inst(void);
+
+/**
+ * @brief Destroy an instance of representation.
+ * @param[in] rep Representation that will be destroyed.
+ */
+void st_things_destroy_representation_inst(st_things_representation_s *rep);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __ST_THINGS_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2018 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 __ST_THINGS_TYPES_H__
+#define __ST_THINGS_TYPES_H__
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+/**
+ * @brief Enumeration for ST Things error code.
+ */
+typedef enum {
+ ST_THINGS_ERROR_NONE = 0, /**< Successful */
+ ST_THINGS_ERROR_INVALID_PARAMETER = -1, /**< Invalid parameter (If parameter is null or empty)*/
+ ST_THINGS_ERROR_OPERATION_FAILED = -2, /**< Operation Failed */
+ ST_THINGS_ERROR_STACK_NOT_INITIALIZED = -3, /**< Stack is not yet initialized*/
+ ST_THINGS_ERROR_STACK_ALREADY_INITIALIZED = -4, /**< Stack is already initialized*/
+ ST_THINGS_ERROR_STACK_NOT_STARTED = -5, /**< Stack is not yet started*/
+ ST_THINGS_ERROR_STACK_RUNNING = -6, /**< Stack is currently running*/
+} st_things_error_e;
+
+/**
+ * @brief Enumeration for ST Things status.
+ */
+typedef enum {
+ ST_THINGS_STATUS_INIT = 0, /**< Initial state of ST Things */
+ ST_THINGS_STATUS_ES_STARTED, /**< Easy-setup is started */
+ ST_THINGS_STATUS_ES_DONE, /**< Easy-setup is done */
+ ST_THINGS_STATUS_ES_FAILED_ON_OWNERSHIP_TRANSFER, /**< Easy-setup failed due to Ownership-Transfer failure */
+ ST_THINGS_STATUS_CONNECTING_TO_AP, /**< Connecting to target Wi-Fi access point */
+ ST_THINGS_STATUS_CONNECTED_TO_AP, /**< Connected to target Wi-Fi access point */
+ ST_THINGS_STATUS_CONNECTING_TO_AP_FAILED, /**< Failed to connect to target Wi-Fi access point */
+ ST_THINGS_STATUS_REGISTERING_TO_CLOUD, /**< Trying to Sign-up/Sign-in/Publish-Resource(s) to Cloud */
+ ST_THINGS_STATUS_REGISTERED_TO_CLOUD, /**< Publish resource(s) to cloud is complete. Now the Thing is ready to be controlled via Cloud */
+ ST_THINGS_STATUS_REGISTERING_FAILED_ON_SIGN_IN, /**< Failed to sign-in to Cloud */
+ ST_THINGS_STATUS_REGISTERING_FAILED_ON_PUB_RES /**< Failed to publish resources to Cloud */
+} st_things_status_e;
+
+/**
+ * @brief Structure for Representation.
+ */
+typedef struct _st_things_representation
+{
+ void* payload; /**< Payload of representation */
+
+ /**
+ * @brief API for getting the value of string type property with a key.
+ * @remarks This API will return deep-copied string value as out parameter, so application must free it after use.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[out] value String value
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_str_value) (struct _st_things_representation* rep, const char* key, char** value);
+
+ /**
+ * @brief API for getting the value of boolean type property with a key.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[out] value Bool value
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_bool_value) (struct _st_things_representation* rep, const char* key, bool* value);
+
+ /**
+ * @brief API for getting the value of integer type property with a key.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[out] value Integer value
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_int_value) (struct _st_things_representation* rep, const char* key, int64_t* value);
+
+ /**
+ * @brief API for getting the value of double type property with a key.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[out] value Double value
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_double_value) (struct _st_things_representation* rep, const char* key, double* value);
+
+ /**
+ * @brief API for getting the value of byte array type property with a key.
+ * @remarks This API will return deep-copied byte value as out parameter, so application must free it after use.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[out] value Byte value
+ * @param[out] size Size of Byte value
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_byte_value) (struct _st_things_representation* rep, const char* key, uint8_t** value, size_t* size);
+
+ /**
+ * @brief API for getting the value of object type property with a key.
+ * @remarks This API will return deep-copied object value as out parameter, so application must free it after use.\n
+ * To free an object, st_things_destroy_representation_inst() in st_things.h should be used.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[out] value Object value
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_object_value) (struct _st_things_representation* rep, const char* key, struct _st_things_representation** value);
+
+ /**
+ * @brief API for setting the value of string type property with a key.
+ * @remarks This API will deep-copy the string value inside, so application still has an ownership of memory for the string value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the value.
+ * @param[in] value String value.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_str_value) (struct _st_things_representation* rep, const char* key, const char* value);
+
+ /**
+ * @brief API for setting the value of boolean type property with a key.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the value.
+ * @param[in] value Bool value.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_bool_value) (struct _st_things_representation* rep, const char* key, bool value);
+
+ /**
+ * @brief API for setting the value of integer type property with a key.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the value.
+ * @param[in] value Integer value.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_int_value) (struct _st_things_representation* rep, const char* key, int64_t value);
+
+ /**
+ * @brief API for setting the value of double type property with a key.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the value.
+ * @param[in] value Double value.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_double_value) (struct _st_things_representation* rep, const char* key, double value);
+
+ /**
+ * @brief API for setting the value of byte array type property with a key.
+ * @remarks This API will deep-copy the byte value inside, so application still has an ownership of memory for the byte value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the value.
+ * @param[in] value Byte value.
+ * @param[in] size Size of Byte value.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_byte_value) (struct _st_things_representation* rep, const char* key, const uint8_t* value, size_t size);
+
+ /**
+ * @brief API for setting the value of object type property with a key.
+ * @remarks This API will deep-copy the object value inside, so application still has an ownership of memory for the object value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the value.
+ * @param[in] value Object value.
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*set_object_value) (struct _st_things_representation* rep, const char* key, const struct _st_things_representation* value);
+
+ /**
+ * @brief API for getting the value of string array type property with a key.
+ * @remarks This API will return deep-copied array value as out parameter, so application must free it after use.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the array type of value.
+ * @param[out] array Reference of the string array to where the value will be copied.
+ * @param[out] length Total number of elements in the array.
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_str_array_value) (struct _st_things_representation* rep, const char* key, char*** array, size_t* length);
+
+ /**
+ * @brief API for getting the value of integer array type property with a key.
+ * @remarks This API will return deep-copied array value as out parameter, so application must free it after use.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the array type of value.
+ * @param[out] array Reference of the integer array where the value will be copied.
+ * @param[out] length Total number of elements in the array.
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_int_array_value) (struct _st_things_representation* rep, const char* key, int64_t** array, size_t* length);
+
+ /**
+ * @brief API for getting the value of double array type property with a key.
+ * @remarks This API will return deep-copied array value as out parameter, so application must free it after use.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which will represent the array type of value.
+ * @param[out] array Reference of the double array where the value will be copied.
+ * @param[out] length Total number of elements in the array.
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_double_array_value) (struct _st_things_representation* rep, const char* key, double** array, size_t* length);
+
+ /**
+ * @brief API for getting the value of object array type property with a key.
+ * @remarks This API will return deep-copied array value as out parameter, so application must free it after use.\n
+ * To free each object in array, st_things_destroy_representation_inst() in st_things.h should be used.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the array type of value.
+ * @param[out] array Reference of the object array where the value will be copied.
+ * @param[out] length Total number of elements in the array.
+ * @return @c true if value exist, otherwise @c false
+ */
+ bool (*get_object_array_value) (struct _st_things_representation* rep, const char* key, struct _st_things_representation*** array, size_t* length);
+
+ /**
+ * @brief API for setting the value of string array type property with a key.
+ * @remarks This API will deep-copy the array value inside, so application still has an ownership of memory for the array value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[in] array String array type value.
+ * @param[in] length Total number of elements in the array.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_str_array_value) (struct _st_things_representation* rep, const char* key, const char** array, size_t length);
+
+ /**
+ * @brief API for setting the value of integer array type property with a key.
+ * @remarks This API will deep-copy the array value inside, so application still has an ownership of memory for the array value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[in] array Integer array type value.
+ * @param[in] length Total number of elements in the array.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_int_array_value) (struct _st_things_representation* rep, const char* key, const int64_t* array, size_t length);
+
+ /**
+ * @brief API for setting the value of double array type property with a key.
+ * @remarks This API will deep-copy the array value inside, so application still has an ownership of memory for the array value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[in] array Double array type value.
+ * @param[in] length Total number of elements in the array.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_double_array_value) (struct _st_things_representation* rep, const char* key, const double* array, size_t length);
+
+ /**
+ * @brief API for setting the value of object array type property with a key.
+ * @remarks This API will deep-copy the array value inside, so application still has an ownership of memory for the array value.
+ * @param[in] rep Instance of Representation.
+ * @param[in] key Property Name which represents the value.
+ * @param[in] array Object array type value.
+ * @param[in] length Total number of elements in the array.
+ * @return @c true if setting value is successful, otherwise @c false
+ */
+ bool (*set_object_array_value) (struct _st_things_representation* rep, const char* key, const struct _st_things_representation** array, size_t length);
+
+} st_things_representation_s;
+
+/**
+ * @brief Structure for representing the Get Request Message.
+ */
+typedef struct _st_things_get_request_message
+{
+ char* resource_uri; /**< Resource URI */
+ char* query; /**< One or more query parameters of the request message. Ex: key1=value1;key2=value2;... */
+ char* property_key; /**< One or more property key that application needs to set a value for response. Ex: key1;key2;... */
+
+ /**
+ * @brief API for getting the value of a specific query from the query parameters of the request.
+ * @param[in] req_msg Instance of get request message.
+ * @param[in] key Name of the query.(ex: key1, key2, etc)
+ * @param[out] value Value of the query.(value1, value2, etc)
+ * @return @c true if query exist, otherwise @c false
+ */
+ bool (*get_query_value) (struct _st_things_get_request_message* req_msg, const char* key, char** value);
+
+ /**
+ * @brief API for checking whether the request has a specific property key or not.
+ * @param[in] req_msg Instance of get request message.
+ * @param[in] key Name of the property.
+ * @return @c true if the property key exists, otherwise @c false
+ */
+ bool (*has_property_key) (struct _st_things_get_request_message* req_msg, const char* key);
+
+} st_things_get_request_message_s;
+
+/**
+ * @brief Structure for representing the Set Request Message.
+ */
+typedef struct _st_things_set_request_message
+{
+ char* resource_uri; /**< Resource URI */
+ char* query; /**< One or more query parameters of the request message. Ex: key1=value1?key2=value2?... */
+ struct _st_things_representation* rep; /**< Representation of the set request message */
+
+ /**
+ * @brief API for getting the value of a specific query from the query parameters of the request.
+ * @param[in] req_msg Instance of request message.
+ * @param[in] key Name of the query.(ex: key1, key2, etc)
+ * @param[out] value Value of the query.(value1, value2, etc)
+ * @return @c true if query exist, otherwise @c false
+ */
+ bool (*get_query_value) (struct _st_things_set_request_message* req_msg, const char* key, char** value);
+
+} st_things_set_request_message_s;
+
+#endif /* __ST_THINGS_TYPES_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2018 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.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+void init_thing();
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
--- /dev/null
+APPNAME = co2sensor
+
+type = app
+profile = iot-headless-4.0
+
+USER_SRCS = src/co2sensor.c
+USER_DEFS =
+USER_INC_DIRS = inc, inc/sdk
+USER_OBJS =
+USER_LIBS = lib/liboicdastack.so, lib/libsdkapi.so
+USER_EDCS =
--- /dev/null
+{
+ "device": [
+ {
+ "specification": {
+ "device": {
+ "deviceType": "x.wwst.d.basic",
+ "deviceName": "co2sensor",
+ "specVersion": "core.1.1.0",
+ "dataModelVersion": "res.1.1.0"
+ },
+ "platform": {
+ "manufacturerName": "fC3T",
+ "manufacturerUrl": "http://www.samsung.com/sec/",
+ "manufacturingDate": "2017-11-29",
+ "modelNumber": "NWSP-01",
+ "platformVersion": "1.0",
+ "osVersion": "1.0",
+ "hardwareVersion": "1.0",
+ "firmwareVersion": "1.0",
+ "vendorId": "vid_1001"
+ }
+ },
+ "resources": {
+ "single": [
+ {
+ "uri": "/capability/switch/main/0",
+ "types": [
+ "x.com.st.powerswitch"
+ ],
+ "interfaces": [
+ "oic.if.a",
+ "oic.if.baseline"
+ ],
+ "policy": 3
+ },
+ {
+ "uri": "/capability/airQualitySensor/main/0",
+ "types": [
+ "x.com.st.airqualitylevel"
+ ],
+ "interfaces": [
+ "oic.if.s",
+ "oic.if.baseline"
+ ],
+ "policy": 3
+ },
+ {
+ "uri": "/capability/thermostatCoolingSetpoint/main/0",
+ "types": [
+ "x.com.st.temperature.cooling"
+ ],
+ "interfaces": [
+ "oic.if.a",
+ "oic.if.s",
+ "oic.if.baseline"
+ ],
+ "policy": 3
+ }
+ ]
+ }
+ }
+ ],
+ "resourceTypes": [
+ {
+ "type": "x.com.st.powerswitch",
+ "properties": [
+ {
+ "key": "power",
+ "type": 3,
+ "mandatory": true,
+ "rw": 3
+ }
+ ]
+ },
+ {
+ "type": "x.com.st.airqualitylevel",
+ "properties": [
+ {
+ "key": "airQuality",
+ "type": 2,
+ "mandatory": true,
+ "rw": 1
+ },
+ {
+ "key": "range",
+ "type": 7,
+ "mandatory": true,
+ "rw": 1
+ }
+ ]
+ },
+ {
+ "type": "x.com.st.temperature.cooling",
+ "properties": [
+ {
+ "key": "temperature",
+ "type": 2,
+ "mandatory": true,
+ "rw": 3
+ },
+ {
+ "key": "range",
+ "type": 7,
+ "mandatory": true,
+ "rw": 3
+ },
+ {
+ "key": "units",
+ "type": 3,
+ "mandatory": true,
+ "rw": 3
+ }
+ ]
+ }
+ ],
+ "configuration": {
+ "easySetup": {
+ "connectivity": {
+ "type": 1,
+ "softAP": {
+ "setupId": "001",
+ "artik": false
+ }
+ },
+ "ownershipTransferMethod": 2
+ },
+ "wifi": {
+ "interfaces": 15,
+ "frequency": 1
+ },
+ "filePath": {
+ "svrdb": "artikserversecured.dat",
+ "provisioning": "provisioning.dat",
+ "certificate": "certificate.pem",
+ "privateKey": "privatekey.der"
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2018 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 "st_things.h"
+#include "log.h"
+
+static const char* PROP_AIRQUALITY = "airQuality";
+static const char* PROP_RANGE = "range";
+static double g_range[2] = { 0, 10000. };
+static size_t g_length = 2;
+int32_t g_co2_sensor_value = 400;
+
+extern int resource_update_co2_sensor_value(void);
+
+bool handle_get_request_on_resource_capability_airqualitysensor(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ //DBG("Received a GET request on %s\n", req_msg->resource_uri);
+
+ if (req_msg->has_property_key(req_msg, PROP_AIRQUALITY)) {
+ g_co2_sensor_value = resource_update_co2_sensor_value();
+ resp_rep->set_int_value(resp_rep, PROP_AIRQUALITY, g_co2_sensor_value);
+ }
+ if (req_msg->has_property_key(req_msg, PROP_RANGE)) {
+ resp_rep->set_double_array_value(resp_rep, PROP_RANGE, (double*)&g_range, g_length);
+ }
+
+ return true;
+}
--- /dev/null
+/*
+ * Copyright (c) 2018 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 "st_things.h"
+#include "log.h"
+
+#define VALUE_STR_LEN_MAX 32
+
+static const char* PROP_POWER = "power";
+static const char* VALUE_SWITCH_ON = "on";
+static const char* VALUE_SWITCH_OFF = "off";
+static char g_switch[VALUE_STR_LEN_MAX] = "off";
+bool g_switch_is_on = false;
+
+bool handle_get_request_on_resource_capability_switch(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ DBG("Received a GET request on %s\n", req_msg->resource_uri);
+
+ if (req_msg->has_property_key(req_msg, PROP_POWER)) {
+ resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
+ }
+ else
+ return false;
+ return true;
+}
+
+bool handle_set_request_on_resource_capability_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ DBG("Received a SET request on %s\n", req_msg->resource_uri);
+
+ char *str_value = NULL;
+ req_msg->rep->get_str_value(req_msg->rep, PROP_POWER, &str_value);
+
+ /* check validation */
+ if ((0 != strncmp(str_value, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON)))
+ && (0 != strncmp(str_value, VALUE_SWITCH_OFF, strlen(VALUE_SWITCH_OFF)))) {
+ ERR("Not supported value!!");
+ free(str_value);
+ return false;
+ }
+ if (0 != strncmp(str_value, g_switch, strlen(g_switch))) {
+ strncpy(g_switch, str_value, VALUE_STR_LEN_MAX);
+ if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) {
+ g_switch_is_on = true;
+ }
+ else {
+ g_switch_is_on = false;
+ }
+ }
+ resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
+
+ st_things_notify_observers(req_msg->resource_uri);
+
+ free(str_value);
+
+ return true;
+}
--- /dev/null
+/*
+ * Copyright (c) 2018 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 "st_things.h"
+#include "log.h"
+
+static const char* PROP_TEMPERATURE = "temperature";
+static const char* PROP_RANGE = "range";
+static const char* PROP_UNITS = "units";
+
+static double g_temperature = 0;
+static double g_range[2] = { 0, 5000. };
+static size_t g_length = 2;
+static char g_unit[4] = "mV";
+
+extern void resource_init_co2_sensor(void);
+extern int resource_get_co2_sensor_parameter(void);
+extern void resource_set_sensor_parameter(int zero_volts);
+
+bool handle_get_request_on_resource_capability_thermostatcoolingsetpoint(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ DBG("Received a GET request on %s\n", req_msg->resource_uri);
+
+ if (req_msg->has_property_key(req_msg, PROP_TEMPERATURE)) {
+ DBG("requested temperature");
+ if (0 == g_temperature)
+ g_temperature = (double)resource_get_co2_sensor_parameter();
+ resp_rep->set_double_value(resp_rep, PROP_TEMPERATURE, g_temperature);
+ }
+ if (req_msg->has_property_key(req_msg, PROP_RANGE)) {
+ //DBG("requested range");
+ resp_rep->set_double_array_value(resp_rep, PROP_RANGE, (double*)&g_range, g_length);
+ }
+ if (req_msg->has_property_key(req_msg, PROP_UNITS)) {
+ //DBG("requested units");
+ resp_rep->set_str_value(resp_rep, PROP_UNITS, g_unit);
+ }
+ return true;
+}
+
+bool handle_set_request_on_resource_capability_thermostatcoolingsetpoint(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ DBG("Received a SET request on %s\n", req_msg->resource_uri);
+
+ double dvalue = 0;
+ if (req_msg->rep->get_double_value(req_msg->rep, PROP_TEMPERATURE, &dvalue)) {
+ g_temperature = dvalue;
+ resource_set_sensor_parameter((int32_t)g_temperature);
+ resource_init_co2_sensor();
+ resp_rep->set_double_value(resp_rep, PROP_TEMPERATURE, dvalue);
+ DBG("set requested: %.f\n", g_temperature);
+ }
+
+ char *str_value = NULL;
+ if (req_msg->rep->get_str_value(req_msg->rep, PROP_UNITS, &str_value)) {
+ resp_rep->set_str_value(resp_rep, PROP_UNITS, g_unit);
+ free(str_value);
+ }
+
+ double davalue[2] = { 0, };
+ size_t dlength = 0;
+ if (req_msg->rep->get_double_array_value(req_msg->rep, PROP_RANGE, (double**)&davalue, &dlength )) {
+ resp_rep->set_double_array_value(resp_rep, PROP_RANGE, g_range, g_length);
+ }
+ st_things_notify_observers(req_msg->resource_uri);
+
+ return true;
+}
--- /dev/null
+/*
+ * Copyright (c) 2018 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 <service_app.h>
+#include <stdio.h>
+#include <resource/resource_co2_sensor.h>
+#include "st_things.h"
+#include "log.h"
+#include <signal.h>
+
+#define JSON_PATH "device_def.json"
+
+static const char* RES_CAPABILITY_SWITCH = "/capability/switch/main/0";
+static const char* RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT = "/capability/thermostatCoolingSetpoint/main/0";
+static const char* RES_CAPABILITY_AIRQUALITYSENSOR = "/capability/airQualitySensor/main/0";
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+extern int thread_done; /* resource_co2_sensor.c */
+
+/* get and set request handlers */
+extern bool handle_get_request_on_resource_capability_switch(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
+extern bool handle_set_request_on_resource_capability_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep);
+extern bool handle_get_request_on_resource_capability_thermostatcoolingsetpoint(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
+extern bool handle_set_request_on_resource_capability_thermostatcoolingsetpoint(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep);
+extern bool handle_get_request_on_resource_capability_airqualitysensor(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
+
+extern void *thread_sensor_main(void *arg);
+extern void *thread_sensor_notify(void *arg);
+
+/* handle : for getting request on resources */
+bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ //DBG("resource_uri [%s]", req_msg->resource_uri);
+
+ if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH)) {
+ return handle_get_request_on_resource_capability_switch(req_msg, resp_rep);
+ }
+ if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT)) {
+ return handle_get_request_on_resource_capability_thermostatcoolingsetpoint(req_msg, resp_rep);
+ }
+ if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_AIRQUALITYSENSOR)) {
+ return handle_get_request_on_resource_capability_airqualitysensor(req_msg, resp_rep);
+ }
+
+ ERR("not supported uri");
+ return false;
+}
+
+/* handle : for setting request on resources */
+bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ DBG("resource_uri [%s]", req_msg->resource_uri);
+
+ if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH)) {
+ return handle_set_request_on_resource_capability_switch(req_msg, resp_rep);
+ }
+ if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT)) {
+ return handle_set_request_on_resource_capability_thermostatcoolingsetpoint(req_msg, resp_rep);
+ }
+
+ ERR("not supported uri");
+ return false;
+}
+
+/* callback functions */
+bool handle_reset_request(void)
+{
+ DBG("Received a request for RESET.");
+ return false;
+}
+
+void handle_reset_result(bool result)
+{
+ DBG("Reset %s.\n", result ? "succeeded" : "failed");
+}
+
+bool handle_ownership_transfer_request(void)
+{
+ DBG("Received a request for Ownership-transfer.");
+ return true;
+}
+
+void handle_things_status_change(st_things_status_e things_status)
+{
+ DBG("Things status is changed: %d\n", things_status);
+}
+
+void sig_handler(int sig)
+{
+ switch (sig) {
+ case SIGINT:
+ INFO("SIGINT received");
+ thread_done = 1;
+ break;
+ case SIGSEGV:
+ INFO("SIGSEGV received");
+ thread_done = 1;
+ break;
+ case SIGTERM:
+ INFO("SIGTERM received");
+ thread_done = 1;
+ break;
+ default:
+ ERR("wasn't expecting that sig [%d]", sig);
+ abort();
+ }
+}
+
+/* main loop */
+void handle_main_loop(void)
+{
+ FN_CALL;
+
+ pthread_t p_thread[2];
+ int ret = 0;
+
+ signal(SIGSEGV, sig_handler);
+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+
+ pthread_mutex_init(&mutex, NULL);
+
+ ret = pthread_create(&p_thread[0], NULL, &thread_sensor_main, NULL);
+ if (ret != 0) {
+ ERR("[ERROR] thread_sensor_main create failed, ret=%d", ret);
+ return;
+ }
+ ret = pthread_create(&p_thread[1], NULL, &thread_sensor_notify, NULL);
+ if (ret != 0) {
+ ERR("[ERROR] thread_sensor_notify create failed, ret=%d", ret);
+ return;
+ }
+}
+
+/* initialize */
+void init_thing()
+{
+ FN_CALL;
+ static bool binitialized = false;
+ if (binitialized) {
+ DBG("Already initialized!!");
+ return;
+ }
+
+ bool easysetup_complete = false;
+
+ char app_json_path[128] = {0,};
+ char *app_res_path = NULL;
+ char *app_data_path = NULL;
+
+ app_res_path = app_get_resource_path();
+ if (!app_res_path) {
+ ERR("app_res_path is NULL!!");
+ return;
+ }
+
+ app_data_path = app_get_data_path();
+ if (!app_data_path) {
+ ERR("app_data_path is NULL!!");
+ free(app_res_path);
+ return;
+ }
+
+ snprintf(app_json_path, sizeof(app_json_path), "%s/%s", app_res_path, JSON_PATH);
+
+ if (0 != st_things_set_configuration_prefix_path((const char *)app_res_path, (const char *)app_data_path)) {
+ ERR("st_things_set_configuration_prefix_path() failed!!");
+ free(app_res_path);
+ free(app_data_path);
+ return;
+ }
+
+ free(app_res_path);
+ free(app_data_path);
+
+ if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
+ ERR("st_things_initialize() failed!!");
+ return;
+ }
+
+ binitialized = true;
+
+ DBG("easysetup_complete:[%d] ", easysetup_complete);
+
+ st_things_register_request_cb(handle_get_request, handle_set_request);
+ st_things_register_reset_cb(handle_reset_request, handle_reset_result);
+ st_things_register_user_confirm_cb(handle_ownership_transfer_request);
+ st_things_register_things_status_change_cb(handle_things_status_change);
+
+ st_things_start();
+
+ handle_main_loop();
+
+ FN_END;
+}
+
+static bool service_app_create(void *user_data)
+{
+ UNUSED(user_data);
+
+ return true;
+}
+
+static void service_app_terminate(void *user_data)
+{
+ UNUSED(user_data);
+
+ MUTEX_LOCK;
+ thread_done = 1;
+ MUTEX_UNLOCK;
+
+ int status = pthread_mutex_destroy(&mutex);
+ INFO("mutex destroy status = %d", status);
+}
+
+static void service_app_control(app_control_h app_control, void *user_data)
+{
+ UNUSED(user_data);
+ if (app_control == NULL) {
+ ERR("app_control is NULL");
+ return;
+ }
+
+ init_thing();
+}
+
+int main(int argc, char *argv[])
+{
+ FN_CALL;
+
+ service_app_lifecycle_callback_s event_callback;
+
+ event_callback.create = service_app_create;
+ event_callback.terminate = service_app_terminate;
+ event_callback.app_control = service_app_control;
+
+ return service_app_main(argc, argv, &event_callback, NULL);
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Contact: Jin Yoon <jinny.yoon@samsung.com>
+ * Geunsun Lee <gs86.lee@samsung.com>
+ * Eunyoung Lee <ey928.lee@samsung.com>
+ * Junkyu Han <junkyu.han@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <peripheral_io.h>
+#include <system_info.h>
+#include "resource/resource_co2_sensor.h"
+#include "log.h"
+
+#define MCP3008_SPEED 3600000
+#define MCP3008_BPW 8
+
+#define MCP3008_TX_WORD1 0x01 /* 0b00000001 */
+#define MCP3008_TX_CH0 0x80 /* 0b10000000 */
+#define MCP3008_TX_CH1 0x90 /* 0b10010000 */
+#define MCP3008_TX_CH2 0xA0 /* 0b10100000 */
+#define MCP3008_TX_CH3 0xB0 /* 0b10110000 */
+#define MCP3008_TX_CH4 0xC0 /* 0b11000000 */
+#define MCP3008_TX_CH5 0xD0 /* 0b11010000 */
+#define MCP3008_TX_CH6 0xE0 /* 0b11100000 */
+#define MCP3008_TX_CH7 0xF0 /* 0b11110000 */
+#define MCP3008_TX_WORD3 0x00 /* 0b00000000 */
+
+#define MCP3008_RX_WORD1_MASK 0x00 /* 0b00000000 */
+#define MCP3008_RX_WORD2_NULL_BIT_MASK 0x04 /* 0b00000100 */
+#define MCP3008_RX_WORD2_MASK 0x03 /* 0b00000011 */
+#define MCP3008_RX_WORD3_MASK 0xFF /* 0b11111111 */
+#define UINT10_VALIDATION_MASK 0x3FF
+
+#define MODEL_NAME_KEY "http://tizen.org/system/model_name"
+#define MODEL_NAME_RPI3 "rpi3"
+#define MODEL_NAME_ARTIK "artik"
+
+static peripheral_spi_h MCP3008_H = NULL;
+static unsigned int ref_count = 0;
+
+#define retv_if(expr, val) do { \
+ if (expr) { \
+ ERR("(%s) -> %s() return", #expr, __FUNCTION__); \
+ return (val); \
+ } \
+} while (0)
+
+int resource_adc_mcp3008_init(void)
+{
+ int ret = 0;
+ int bus = -1;
+ char *model_name = NULL;
+
+ if (MCP3008_H) {
+ DBG("SPI device already initialized [ref_count : %u]", ref_count);
+ MUTEX_LOCK;
+ ref_count++;
+ MUTEX_UNLOCK;
+ return 0;
+ }
+
+ system_info_get_platform_string(MODEL_NAME_KEY, &model_name);
+ if (!model_name) {
+ ERR("fail to get model name");
+ return -1;
+ }
+
+ if (!strcmp(model_name, MODEL_NAME_RPI3)) {
+ bus = 0;
+ } else if (!strcmp(model_name, MODEL_NAME_ARTIK)) {
+ bus = 2; // ARTIK (2)
+ } else {
+ ERR("unknown model name : %s", model_name);
+ free(model_name);
+ return -1;
+ }
+ DBG("%s model_name: %s, bus: %d", __func__, model_name, bus);
+ free(model_name);
+ model_name = NULL;
+
+ ret = peripheral_spi_open(bus, 0, &MCP3008_H);
+ if (PERIPHERAL_ERROR_NONE != ret) {
+ ERR("spi open failed :%s ", get_error_message(ret));
+ return -1;
+ }
+
+ ret = peripheral_spi_set_mode(MCP3008_H, PERIPHERAL_SPI_MODE_0);
+ if (PERIPHERAL_ERROR_NONE != ret) {
+ ERR("peripheral_spi_set_mode failed :%s ", get_error_message(ret));
+ goto error_after_open;
+ }
+ ret = peripheral_spi_set_bit_order(MCP3008_H, PERIPHERAL_SPI_BIT_ORDER_MSB);
+ if (PERIPHERAL_ERROR_NONE != ret) {
+ ERR("peripheral_spi_set_bit_order failed :%s ", get_error_message(ret));
+ goto error_after_open;
+ }
+
+ ret = peripheral_spi_set_bits_per_word(MCP3008_H, MCP3008_BPW);
+ if (PERIPHERAL_ERROR_NONE != ret) {
+ ERR("peripheral_spi_set_bits_per_word failed :%s ", get_error_message(ret));
+ goto error_after_open;
+ }
+
+ ret = peripheral_spi_set_frequency(MCP3008_H, MCP3008_SPEED);
+ if (PERIPHERAL_ERROR_NONE != ret) {
+ ERR("peripheral_spi_set_frequency failed :%s ", get_error_message(ret));
+ goto error_after_open;
+ }
+ DBG("%s success: %d", __func__, ref_count);
+
+ MUTEX_LOCK;
+ ref_count++;
+ MUTEX_UNLOCK;
+
+ return 0;
+
+error_after_open:
+ DBG("%s error: %d", __func__, ref_count);
+ peripheral_spi_close(MCP3008_H);
+ MCP3008_H = NULL;
+ return -1;
+}
+
+int resource_read_adc_mcp3008(int ch_num, unsigned int *out_value)
+{
+ static int spi_read_cnt = 0;
+ static int spi_err_cnt = 0;
+ unsigned char rx[3] = {0, };
+ unsigned char tx[3] = {0, };
+ unsigned char rx_w1 = 0;
+ unsigned char rx_w2 = 0;
+ unsigned char rx_w2_nb = 0;
+ unsigned char rx_w3 = 0;
+ unsigned short int result = 0;
+
+ retv_if(MCP3008_H == NULL, -1);
+ retv_if(out_value == NULL, -1);
+ retv_if((ch_num < 0 || ch_num > 7), -1);
+
+ tx[0] = MCP3008_TX_WORD1;
+ switch (ch_num) {
+ case 0:
+ tx[1] = MCP3008_TX_CH0;
+ break;
+ case 1:
+ tx[1] = MCP3008_TX_CH1;
+ break;
+ case 2:
+ tx[1] = MCP3008_TX_CH2;
+ break;
+ case 3:
+ tx[1] = MCP3008_TX_CH3;
+ break;
+ case 4:
+ tx[1] = MCP3008_TX_CH4;
+ break;
+ case 5:
+ tx[1] = MCP3008_TX_CH5;
+ break;
+ case 6:
+ tx[1] = MCP3008_TX_CH6;
+ break;
+ case 7:
+ tx[1] = MCP3008_TX_CH7;
+ break;
+ default:
+ tx[1] = MCP3008_TX_CH0;
+ break;
+ }
+ tx[2] = MCP3008_TX_WORD3;
+
+ peripheral_spi_transfer(MCP3008_H, tx, rx, 3);
+
+ rx_w1 = rx[0] & MCP3008_RX_WORD1_MASK;
+ rx_w2_nb = rx[1] & MCP3008_RX_WORD2_NULL_BIT_MASK;
+
+ // retv_if(rx_w1 != 0, -1);
+ // retv_if(rx_w2_nb != 0, -1);
+ spi_read_cnt += 1;
+ if (rx_w1 != 0 || rx_w2_nb != 0)
+ {
+ spi_err_cnt++;
+ if (spi_err_cnt >= 100)
+ {
+ DBG("resource_read_adc_mcp3008 rx_w2_nb is not 0 -> rx: %02x, count: %d", rx[1], spi_read_cnt);
+ spi_err_cnt = 0;
+ }
+ return -1;
+ }
+
+ rx_w2 = rx[1] & MCP3008_RX_WORD2_MASK;
+ rx_w3 = rx[2] & MCP3008_RX_WORD3_MASK;
+
+ result = ((rx_w2 << 8) | (rx_w3)) & UINT10_VALIDATION_MASK;
+
+ *out_value = result;
+
+ return 0;
+}
+
+void resource_adc_mcp3008_fini(void)
+{
+ if (MCP3008_H) {
+ MUTEX_LOCK;
+ ref_count--;
+ MUTEX_UNLOCK;
+ }
+ else
+ return;
+
+ if (ref_count == 0) {
+ peripheral_spi_close(MCP3008_H);
+ MCP3008_H = NULL;
+ }
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2018 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 <stdio.h>
+#include <unistd.h>
+#include <math.h>
+#include <app_common.h>
+#include "st_things.h"
+#include "resource/resource_co2_sensor.h"
+#include "log.h"
+
+#define CO2_DATA "co2_data" // save co2 data
+
+#define MAX_PATH_LEN 128
+#define DC_GAIN (8.500) // refer to schematic, (R2 + R3) / R2 = 8.5
+#define DEFAULT_ZERO_VOLTS (2.950)
+#define DEFAULT_RANGE_VOLTS (0.400) // refer to datasheet
+#define DEFAULT_NOTIFY_TIME (100) // 1000msec
+
+#define ADC_PIN 0 // adc pin number
+#define ADC_ERROR (-9999) // adc read error
+#define ADC_MAX_VOLT (4096.f) // 12bit resolution
+#define ADC_REF_VOLT (3300.f + 20.f)
+
+#define SPI_MAX_VOLT (3200.f)
+#define SPI_REF_VOLT (3000.f)
+
+#define POINT_X_ZERO (2.60206) // the start point, log(400)=2.6020606
+#define POINT_X_MAX (4.000) // the start point, log(10000)=4.0
+#define VOLT_V_ZERO (0.3245) // refer to datasheet, 400ppm(V)
+#define VOLT_V_MAX (0.2645) // refer to datasheet, 10000ppm(V)
+#define VOLT_V_REACT (VOLT_V_ZERO - VOLT_V_MAX)
+
+static const char* RES_CAPABILITY_AIRQUALITYSENSOR = "/capability/airQualitySensor/main/0";
+static float mg811_co2_slope; // refer to datasheet, mg811 co2 slope value
+static sensor_mg811_t mg_sensor;
+
+co2_sensor_data_t co2_sensor;
+int thread_done = 0;
+extern int32_t g_co2_sensor_value;
+extern bool g_switch_is_on;
+
+extern int resource_read_adc_mcp3008(int ch_num, unsigned int *out_value); /* resource_adc_mcp3008.c */
+extern int resource_adc_mcp3008_init(void); /* resource_adc_mcp3008.c */
+
+static int _get_sensor_parameter(int index);
+
+/*
+ * initial co2 mg811 sensor for ppm caculation
+ */
+static void _init_co2_mg811_set(float zero_volts, float max_volts)
+{
+ sensor_mg811_t *sen = &mg_sensor;
+ float reaction_volts;
+
+ if (zero_volts == 0.f)
+ zero_volts = VOLT_V_ZERO;
+ if (max_volts == 0.f)
+ max_volts = VOLT_V_MAX;
+
+ sen->zero_point_volts = zero_volts;
+ sen->max_point_volts = max_volts;
+ reaction_volts = VOLT_V_REACT;
+ mg811_co2_slope = reaction_volts / (POINT_X_ZERO - POINT_X_MAX);
+#if defined(__DEBUG__)
+ DBG("CO2Volage zero_volts: %.f mV, max_volts: %.f mV, reaction %.f mV",
+ sen->zero_point_volts * 1000., sen->max_point_volts * 1000., reaction_volts * 1000.);
+ DBG("mg811_ppm: %.3f V, %.3f", POINT_X_ZERO, mg811_co2_slope);
+#endif
+}
+
+/*
+ * get ppm value in co2 mg811 sensor
+ */
+static int _get_co2_mg811_ppm(float volts)
+{
+ static int debug = 0;
+ sensor_mg811_t *sen = &mg_sensor;
+ float log_volts = 0;
+
+ volts = volts / DC_GAIN;
+ if (!(volts <= sen->zero_point_volts && volts >= sen->max_point_volts)) {
+ if ((debug++ % 10) == 0) {
+ DBG("wrong input %.f mV, voltage range %.f ~ %.f (400 ppm ~ 10000 ppm)",
+ volts * 1000., sen->zero_point_volts * 1000., sen->max_point_volts * 1000.);
+ }
+ if (volts < sen->max_point_volts) {
+ return 10000;
+ }
+ return -1;
+ }
+ log_volts = (volts - sen->zero_point_volts) / mg811_co2_slope;
+
+ return pow(10, log_volts + POINT_X_ZERO);
+}
+
+/*
+ * get adc to co2 sensor analog value
+ */
+static short resource_get_co2_sensor_analog(int ch_num)
+{
+ int ret = 0;
+ unsigned int out_value = 0;
+ float sensor_value = 0;
+
+ ret = resource_read_adc_mcp3008(ch_num, &out_value);
+ if (ret < 0)
+ return ret;
+ sensor_value = ((float)out_value * 4.) * (SPI_REF_VOLT / SPI_MAX_VOLT); // 10bit -> 12bit, calibration adc volt
+
+ return (short)sensor_value;
+}
+
+/*
+ * update to average co2 ppm value
+ */
+int resource_update_co2_sensor_value(void)
+{
+ co2_sensor_data_t *sensorp = &co2_sensor;
+ float sensor_value = 0;
+ float sensor_fvalue = 0;
+ int n, percentage;
+ int size = 0;
+ static int debug = 0;
+
+ MUTEX_LOCK;
+ if (sensorp->bsize < ADC_MAX_SIZE)
+ size = sensorp->bsize;
+ else
+ size = ADC_MAX_SIZE;
+ MUTEX_UNLOCK;
+
+ for (n = 0; n < size; n++) {
+ MUTEX_LOCK;
+ sensor_value += (float)sensorp->sensor_value[n];
+ MUTEX_UNLOCK;
+ }
+ sensor_value = sensor_value / (float)size;
+
+ sensor_fvalue = (sensor_value * ADC_REF_VOLT) / ADC_MAX_VOLT;
+ percentage = _get_co2_mg811_ppm(sensor_fvalue / 1000.f);
+
+ if (percentage < 0 || percentage >= 10000) {
+ if ((debug++ % 5) == 0)
+ DBG("sensor: %.f, volt: %.2f mV, CO2: %d ppm", sensor_value, sensor_fvalue, percentage);
+ }
+
+ return percentage;
+}
+
+/*
+ * initial adc funtion
+ */
+void resource_init_co2_sensor(void)
+{
+ float zero_volts; // measurement mutimeter voltage (mV)
+
+ zero_volts = (float)_get_sensor_parameter(0) / 1000.0;
+ if (zero_volts == 0.0)
+ zero_volts = DEFAULT_ZERO_VOLTS;
+
+ _init_co2_mg811_set(zero_volts/DC_GAIN, (zero_volts - DEFAULT_RANGE_VOLTS)/DC_GAIN);
+}
+
+/*
+ * get adc parameters
+ * 0: calibration min voltage
+ * 1: calibration max voltage (no used)
+ * 2: notification loop count (default 100 is 1000msec delay)
+ */
+int _get_sensor_parameter(int index)
+{
+ FILE *fp;
+ char buffer[16];
+ int volts[3];
+
+ char path[MAX_PATH_LEN];
+ char *app_data_path = NULL;
+
+ app_data_path = app_get_data_path();
+ sprintf(path, "%s%s", app_data_path, CO2_DATA);
+
+ if((fp = fopen(path, "r")) == NULL) {
+ ERR("Error: [%s] can't open adc data file", path);
+ if (index == 2)
+ return 0;
+ return (int)(DEFAULT_ZERO_VOLTS * 1000);
+ }
+ fgets(buffer, 16, fp);
+ fclose(fp);
+ sscanf(buffer, "%d %d %d", &volts[0], &volts[1], &volts[2]);
+ DBG("get parameter: %s, zero: %d, max: %d, count: %d", buffer, volts[0], volts[1], volts[2]);
+
+ free(app_data_path);
+
+ return volts[index];
+}
+
+/*
+ * set adc parameters
+ */
+void resource_set_sensor_parameter(int zero_volts)
+{
+ FILE *fp;
+ char buffer[16];
+ char path[MAX_PATH_LEN];
+ char *app_data_path = NULL;
+
+ app_data_path = app_get_data_path();
+ sprintf(path, "%s%s", app_data_path, CO2_DATA);
+
+ if((fp = fopen(path, "w+")) == NULL) {
+ ERR("ERROR: can't fopen file: %s", CO2_DATA);
+ return;
+ }
+ memset(buffer, 0, sizeof(buffer));
+ sprintf(buffer, "%d %d %d", zero_volts, zero_volts - (int)(DEFAULT_RANGE_VOLTS * 1000), DEFAULT_NOTIFY_TIME);
+ fputs(buffer, fp);
+ fclose(fp);
+ free(app_data_path);
+}
+
+/*
+ * get co2 sensor percentage (ppm)
+ */
+int resource_get_co2_sensor_parameter(void)
+{
+ int param = _get_sensor_parameter(0);
+
+ return param;
+}
+
+/*
+ * main thread funtion to get sensor data
+ */
+void *thread_sensor_main(void *arg)
+{
+ int ret = 0;
+ int pin = ADC_PIN;
+ int err_count = 0;
+ short sensor_value;
+ co2_sensor_data_t *sensorp = &co2_sensor;
+
+ DBG("%s starting...\n", __func__);
+
+ memset((void *)sensorp, 0, sizeof(co2_sensor_data_t));
+ resource_init_co2_sensor();
+
+ ret = resource_adc_mcp3008_init();
+ DBG("resource_adc_mcp3008_init ret: %d", ret);
+
+ while (true)
+ {
+ if (thread_done) break;
+
+ sensor_value = resource_get_co2_sensor_analog(pin);
+ if (sensor_value >= 0)
+ {
+ MUTEX_LOCK;
+ sensorp->sensor_value[sensorp->index++] = sensor_value;
+ if (sensorp->index >= ADC_MAX_SIZE)
+ sensorp->index = 0;
+ if (sensorp->bsize < ADC_MAX_SIZE)
+ sensorp->bsize++;
+ sensorp->count++;
+ MUTEX_UNLOCK;
+ err_count = 0;
+ usleep(10); // 10usec
+ }
+ else
+ {
+ if (++err_count >= 100)
+ {
+ memset((void *)sensorp, 0, sizeof(co2_sensor_data_t));
+ }
+ usleep(10 * 1000); // 10msec
+ }
+ }
+ DBG("%s exiting...\n", __func__);
+ pthread_exit((void *) 0);
+}
+
+void *thread_sensor_notify(void *arg)
+ {
+ int count = 0;
+ int nloop = 0;
+
+ count = _get_sensor_parameter(2);
+ if (count < 10)
+ count = DEFAULT_NOTIFY_TIME;
+ while (true) {
+ if (thread_done) break;
+
+ if (nloop++ >= count) {
+ nloop = 0;
+ // notify sensor value to server
+ co2_sensor_data_t *sensorp = &co2_sensor;
+
+ if (g_co2_sensor_value > 0 && g_co2_sensor_value < 10000)
+ DBG("CO2 value: %d, count: %d", g_co2_sensor_value, sensorp->count);
+
+ MUTEX_LOCK;
+ sensorp->count = 0;
+ MUTEX_UNLOCK;
+
+ if (g_switch_is_on)
+ st_things_notify_observers(RES_CAPABILITY_AIRQUALITYSENSOR);
+ }
+ usleep(10 * 1000); // 10msec
+ }
+ DBG("%s exiting...\n", __func__);
+ pthread_exit((void *) 0);
+}
+
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<manifest xmlns="http://tizen.org/ns/packages" api-version="4.0" package="org.example.co2sensor" version="1.0.0">
+ <profile name="iot-headless"/>
+ <service-application appid="org.example.co2sensor" exec="co2sensor" multiple="false" nodisplay="true" on-boot="true" taskmanage="false" type="capp">
+ <label>co2sensor</label>
+ <icon>co2sensor.png</icon>
+ <background-category value="background-network"/>
+ </service-application>
+ <privileges>
+ <privilege>http://tizen.org/privilege/network.get</privilege>
+ <privilege>http://tizen.org/privilege/network.set</privilege>
+ <privilege>http://tizen.org/privilege/internet</privilege>
+ <privilege>http://tizen.org/privilege/peripheralio</privilege>
+ <privilege>http://tizen.org/privilege/alarm.set</privilege>
+ <privilege>http://tizen.org/privilege/network.profile</privilege>
+ </privileges>
+</manifest>