Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions sns_client_test/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2020 Qualcomm Innovation Center, Inc. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written
permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Where there is uncertainty as to how, or
where, to apply marks, open an OSR to escalate to OSG for review.

SPDX-License-Identifier: BSD-3-Clause-Clear
43 changes: 43 additions & 0 deletions sns_client_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# sns_client_test
```
A sample app based on sns_clent_example
The purpose is helping users to learn how to implement sensor client to connect to see
```

# Get Start (On build machine)

## Connect build machine to board through USB-C
```
$ adb shell
sh-4.4# cd /data
sh-4.4# git clone https://github.com/quic/sample-apps-for-Qualcomm-Robotics-RB5-platform.git

```
## Compile app on baord
```
sh-4.4# cd sns_client_test/src
sh-4.4# make

```
## Test (on board)
On main board, make sure dip switch (DIP_SW_0) settings as below:

DIP switch number
1 2 3 4 5 6
--------------------------------
State | ON | ON | OFF | OFF | OFF | ON |
--------------------------------

The sensor device on board:
ICM-4x6xx, the SUID : 12370169555311111083ull

sh-4.4# chmod +x sns_client_test

Get help informatioon about sns_client_test
sh-4.4# ./sns_client_test -h

Run accel test for 10 senconds
sh-4.4# ./sns_client_test -s0 -t10

## License
This is licensed under the BSD 3-Clause-Clear “New” or “Revised” License. Check out the [LICENSE](LICENSE) for more details.
47 changes: 47 additions & 0 deletions sns_client_test/inc/sensor_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2020 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/

#pragma once
#include <vector>
#include "sensor_connection.h"

struct sensor_uid
{
sensor_uid() : low(0), high(0) {}
sensor_uid(uint64_t low, uint64_t high): low(low), high(high) {}
bool operator==(const sensor_uid& rhs) const
{
return (low == rhs.low && high == rhs.high);
}
uint64_t low, high;
};

using suid_event_function =
std::function<void(const std::string& datatype,
const std::vector<sensor_uid>& suids)>;

class sns_client
{
public:

sns_client(suid_event_function cb);

void request_suid(std::string datatype, bool default_only = false);

private:
suid_event_function _cb;
void handle_ssc_event(const uint8_t *data, size_t size);
std::unique_ptr<sensor_connection> _ssc_connect;

ssc_event_cb_ts get_ssc_event_cb()
{
return [this](const uint8_t *data, size_t size, uint64_t ts)
{
handle_ssc_event(data, size);
};
}
};

148 changes: 148 additions & 0 deletions sns_client_test/inc/sensor_connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2020 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#pragma once
#include <functional>
#include <string>
#include <map>
#include <condition_variable>
#include <utils/Log.h>
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "qmi_client.h"
#include "sns_client_api_v01.h"
#include "utils/SystemClock.h"

using namespace std;
using namespace google::protobuf::io;

#define android_loge ALOGE
#define android_logi ALOGI
#define android_logd ALOGD
#define android_logv ALOGV

enum ssc_error_type
{
SSC_CONNECTION_RESET
};

using ssc_event_cb_ts = std::function<void(const uint8_t *data, size_t size, uint64_t ts)>;

using ssc_error_cb = std::function<void(ssc_error_type error)>;

using ssc_resp_cb = std::function<void(const uint32_t error_value)>;

class see_connection
{
public:
see_connection(ssc_event_cb_ts event_cb_ts):
_event_cb_ts(event_cb_ts),
_connection_closed(false) {
see_connect();
}

~see_connection(){
_connection_closed = true;
see_disconnect();
}

void send_request(const string& pb_req_message_encoded, bool use_qmi_sync);
void register_error_cb(ssc_error_cb cb) { _error_cb = cb; }
void register_resp_cb(ssc_resp_cb cb) { _resp_cb = cb; }

private:
ssc_event_cb_ts _event_cb_ts;
qmi_client_type _qmi_handle = nullptr;
bool _service_ready;
std::mutex _mutex;
std::condition_variable _cv;
qmi_cci_os_signal_type _os_params;

bool _connection_closed;
ssc_error_cb _error_cb;
ssc_resp_cb _resp_cb;
static const uint32_t QMI_RESPONSE_TIMEOUT_MS = 2000;
sns_client_resp_msg_v01 resp_async = {};
void see_connect();
void see_disconnect();
void wait_for_see_service();
static void see_notify_cb(qmi_client_type user_handle,
qmi_idl_service_object_type service_obj,
qmi_client_notify_event_type service_event,
void *notify_cb_data)
{
see_connection *conn = (see_connection *) notify_cb_data;
unique_lock<mutex> lk(conn->_mutex);
conn->_service_ready = true;
conn->_cv.notify_one();
}

static void see_indication_cb(qmi_client_type user_handle,
unsigned int msg_id, void* ind_buf,
unsigned int ind_buf_len,
void* ind_cb_data)
{
struct timespec ts;
if (clock_gettime(CLOCK_BOOTTIME, &ts) == -1) {
perror("clock_gettime");
exit(EXIT_FAILURE);
}

uint64_t sample_received_ts = 1000000000*(ts.tv_sec) + ts.tv_nsec;
see_connection *conn = (see_connection*)ind_cb_data;
conn->handle_indication(msg_id, ind_buf, ind_buf_len, sample_received_ts);
}

static void qmi_response_cb(qmi_client_type user_handle,
unsigned int msg_id,
void* resp_cb,
unsigned int resp_cb_len,
void* resp_cb_data,
qmi_client_error_type qmi_err)
{
see_connection *conn = (see_connection*)resp_cb_data;
sns_client_resp_msg_v01 resp = *((sns_client_resp_msg_v01 *)resp_cb);
if (nullptr != conn){
if(conn->_resp_cb && resp.result_valid) {
conn->_resp_cb(resp.result);
}
}
}

static void qmi_error_cb(qmi_client_type user_handle,
qmi_client_error_type error,
void* err_cb_data)
{
see_connection* conn = (see_connection*)err_cb_data;
android_loge("error=%d", error);
if (error != QMI_NO_ERR) {
if (conn->_error_cb)
conn->_error_cb(SSC_CONNECTION_RESET);
}
}

void handle_indication(unsigned int msg_id,
void *ind_buf,
unsigned int ind_buf_len,
uint64_t ts);
};

class sensor_connection
{
public:

sensor_connection(ssc_event_cb_ts event_cb);

~sensor_connection();

void send_request(const std::string& pb_req_message_encoded, bool use_qmi_sync = true);

void register_error_cb(ssc_error_cb cb);

void register_resp_cb(ssc_resp_cb cb);

private:
see_connection* _see_conn;

};
71 changes: 71 additions & 0 deletions sns_client_test/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2020 Qualcomm Innovation Center, Inc. All Rights Reserved.
#
# SPDX-License-Identifier: BSD-3-Clause-Clear

IDIR =../inc

BIN_PROG = sns_client_test

CC=g++

AM_CPPFLAGS = -Werror \
-Wall \
-Wno-unused-parameter \
-Wno-unused-variable \
-fexceptions \
-I/usr/include/qrb5165 \
-I/usr/include/qrb5165/cutils \
-I/usr/include/qrb5165/qmi-framework

CFLAGS = $(AM_CPPFLAGS) -I$(IDIR)

LDIR =/usr/lib

_SEE_LIBS = libsnsapi.so \
libsensorslog.so

SEE_LIBS = $(patsubst %,$(LDIR)/%,$(_SEE_LIBS))

_QMIFRAMEWORK_LIBS = libqmi_common_so.so.1 \
libqmi_encdec.so.1 \
libqmi_cci.so.1 \
libqmi_csi.so.1 \
libqmi_sap.so.1
QMIFRAMEWORK_LIBS = $(patsubst %,$(LDIR)/%,$(_QMIFRAMEWORK_LIBS))

_ANDROID_LIBS = liblog.so.0 \
libcutils.so.0 \
libtime_genoff.so.1

ANDROID_LIBS = $(patsubst %,$(LDIR)/%,$(_ANDROID_LIBS))

PROTOBUF_LIBS = $(LDIR)/libprotobuf.so.13 \
-pthread \
$(LDIR)/aarch64-linux-gnu/libpthread.so

requiredlibs = $(SEE_LIBS) \
$(ANDROID_LIBS) \
$(QMIFRAMEWORK_LIBS) \
$(PROTOBUF_LIBS)

LIBS = -lm $(requiredlibs)


_DEPS = sensor_client.h sensor_connection.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

OBJ = sensor_client_test.o \
sensor_connection.o \
sensor_client.o

%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

$(BIN_PROG): $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
rm -f *.o $(BIN_PROG)

Loading