add feature for adconvert and sound sensor 55/157155/1
authorJeonghoon Park <jh1979.park@samsung.com>
Mon, 9 Oct 2017 05:02:58 +0000 (14:02 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Mon, 23 Oct 2017 13:17:54 +0000 (22:17 +0900)
Change-Id: Ia78e258c41d53ad759330197f22bd83eaa91afca

CMakeLists.txt
inc/resource.h
inc/resource/resource_adc_mcp3008.h [new file with mode: 0644]
inc/resource/resource_sound_level_sensor.h [new file with mode: 0644]
inc/resource/resource_sound_level_sensor_internal.h [new file with mode: 0644]
inc/resource_internal.h
packaging/org.tizen.position-finder-server.spec
src/resource.c
src/resource/resource_adc_mcp3008.c [new file with mode: 0644]
src/resource/resource_sound_level_sensor.c [new file with mode: 0644]

index 90fbcd3..bd6c79c 100644 (file)
@@ -18,6 +18,10 @@ pkg_check_modules(APP_PKGS REQUIRED
        eina
        iotcon
        gio-2.0
+       libcurl
+       glib-2.0
+       json-glib-1.0
+       capi-system-info
 )
 
 ADD_DEFINITIONS(-DCBOR_FILE_IN_RES="${INSTALL_RESDIR}/${CBOR_FILE}")
@@ -50,6 +54,8 @@ ADD_EXECUTABLE(${PROJECT_NAME}
        ${PROJECT_ROOT_DIR}/src/resource/resource_sound_detection_sensor.c
        ${PROJECT_ROOT_DIR}/src/resource/resource_tilt_sensor.c
        ${PROJECT_ROOT_DIR}/src/resource/resource_gas_detection_sensor.c
+       ${PROJECT_ROOT_DIR}/src/resource/resource_sound_level_sensor.c
+       ${PROJECT_ROOT_DIR}/src/resource/resource_adc_mcp3008.c
 )
 
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -lm)
index 4b41dfb..783fd8f 100644 (file)
@@ -37,5 +37,6 @@
 #include "resource/resource_sound_detection_sensor.h"
 #include "resource/resource_tilt_sensor.h"
 #include "resource/resource_gas_detection_sensor.h"
+#include "resource/resource_sound_level_sensor.h"
 
 #endif /* __POSITION_FINDER_RESOURCE_H__ */
diff --git a/inc/resource/resource_adc_mcp3008.h b/inc/resource/resource_adc_mcp3008.h
new file mode 100644 (file)
index 0000000..b811c67
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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 Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 __POSITION_FINDER_RESOURCE_ADC_MCP3008_H__
+#define __POSITION_FINDER_RESOURCE_ADC_MCP3008_H__
+
+int resource_adc_mcp3008_init(void);
+int resource_read_adc_mcp3008(int ch_num, unsigned int *out_value);
+void resource_adc_mcp3008_fini(void);
+
+#endif /* __POSITION_FINDER_RESOURCE_ADC_MCP3008_H__ */
+
diff --git a/inc/resource/resource_sound_level_sensor.h b/inc/resource/resource_sound_level_sensor.h
new file mode 100644 (file)
index 0000000..b0c64d5
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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 Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_H__
+#define __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_H__
+
+ /**
+  * @brief Reads the value from sound level sensor through AD converter(MCP3008).
+  * @remarks We assume that only one AD converter is connected with device.
+  * @param[in] ch_num The number of channel connected to the sound level sensor with AD converter
+  * @param[out] out_value The value of a sound level
+  * @return 0 on success, otherwise a negative error value
+  *
+  */
+extern int resource_read_sound_level_sensor(int ch_num, unsigned int *out_value);
+
+#endif /* __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_H__ */
+
diff --git a/inc/resource/resource_sound_level_sensor_internal.h b/inc/resource/resource_sound_level_sensor_internal.h
new file mode 100644 (file)
index 0000000..ccc8830
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * 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 Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_INTERNAL_H__
+#define __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_INTERNAL_H__
+
+extern void resource_close_sound_level_sensor(void);
+
+#endif /* __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_INTERNAL_H__ */
index cd1b4de..ffc6f60 100644 (file)
@@ -37,6 +37,7 @@
 #include "resource/resource_sound_detection_sensor_internal.h"
 #include "resource/resource_tilt_sensor_internal.h"
 #include "resource/resource_gas_detection_sensor_internal.h"
+#include "resource/resource_sound_level_sensor_internal.h"
 
 #define PIN_MAX 40
 
index 6390f14..dbb783b 100644 (file)
@@ -23,6 +23,10 @@ BuildRequires:  pkgconfig(ecore)
 BuildRequires:  pkgconfig(eina)
 BuildRequires:  pkgconfig(iotcon)
 BuildRequires:  pkgconfig(gio-2.0)
+BuildRequires:  pkgconfig(libcurl)
+BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(json-glib-1.0)
+BuildRequires:  pkgconfig(capi-system-info)
 
 %description
 Server for Position Finder
index 2183850..2354522 100644 (file)
@@ -42,4 +42,5 @@ void resource_close_all(void)
                        resource_info[i].close(i);
        }
        resource_close_illuminance_sensor();
+       resource_close_sound_level_sensor();
 }
diff --git a/src/resource/resource_adc_mcp3008.c b/src/resource/resource_adc_mcp3008.c
new file mode 100644 (file)
index 0000000..f170031
--- /dev/null
@@ -0,0 +1,208 @@
+/*
+ * 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 Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 <unistd.h>
+#include <peripheral_io.h>
+#include <tizen.h>
+#include <system_info.h>
+#include <string.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;
+
+int resource_adc_mcp3008_init(void)
+{
+       int ret = 0;
+       int bus = -1;
+       char *model_name = NULL;
+
+       if (MCP3008_H) {
+               _D("SPI device aleady initialized [ref_count : %u]", ref_count);
+               ref_count++;
+               return 0;
+       }
+
+       system_info_get_platform_string(MODEL_NAME_KEY, &model_name);
+       if (!model_name) {
+               _E("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;
+       } else {
+               _E("unknown model name : %s", model_name);
+               free(model_name);
+               return -1;
+       }
+       free(model_name);
+       model_name = NULL;
+
+       ret = peripheral_spi_open(bus, 0, &MCP3008_H);
+       if (PERIPHERAL_ERROR_NONE != ret) {
+               _E("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) {
+               _E("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) {
+               _E("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) {
+               _E("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) {
+               _E("peripheral_spi_set_frequency failed :%s ", get_error_message(ret));
+               goto error_after_open;
+       }
+
+       ref_count++;
+
+       return 0;
+
+error_after_open:
+       peripheral_spi_close(MCP3008_H);
+       MCP3008_H = NULL;
+       return -1;
+}
+
+
+int resource_read_adc_mcp3008(int ch_num, unsigned int *out_value)
+{
+       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;
+       retv_if(rx_w1 != 0, -1);
+
+       rx_w2_nb = rx[1] & MCP3008_RX_WORD2_NULL_BIT_MASK;
+       retv_if(rx_w2_nb != 0, -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;
+
+       // _D("%hu", result);
+
+       *out_value = result;
+
+       return 0;
+}
+
+void resource_adc_mcp3008_fini(void)
+{
+       if (MCP3008_H)
+               ref_count--;
+       else
+               return;
+
+       if (ref_count == 0) {
+               peripheral_spi_close(MCP3008_H);
+               MCP3008_H = NULL;
+       }
+
+       return;
+}
+
diff --git a/src/resource/resource_sound_level_sensor.c b/src/resource/resource_sound_level_sensor.c
new file mode 100644 (file)
index 0000000..9118862
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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 Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 <unistd.h>
+#include <peripheral_io.h>
+#include <sys/time.h>
+
+#include "log.h"
+#include "resource/resource_adc_mcp3008.h"
+
+static bool initialized = false;
+
+void resource_close_sound_level_sensor(void)
+{
+       resource_adc_mcp3008_fini();
+       initialized = false;
+}
+
+int resource_read_sound_level_sensor(int ch_num, unsigned int *out_value)
+{
+       unsigned int read_value = 0;
+       int ret = 0;
+
+       if (!initialized) {
+               ret = resource_adc_mcp3008_init();
+               retv_if(ret != 0, -1);
+               initialized = true;
+       }
+       ret = resource_read_adc_mcp3008(ch_num, &read_value);
+       retv_if(ret != 0, -1);
+
+       *out_value = read_value;
+
+       return 0;
+}
+