[Non-ACR][TFIVE-357]
[platform/core/system/sensord.git] / src / sensorctl / loopback.cpp
1 /*
2  * sensorctl
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sensor_internal.h>
24
25 #include "log.h"
26 #include "loopback.h"
27
28 #define SHUB_INST_LIB_ADD      ((char)-79)
29 #define SHUB_INST_LIB_REMOVE   ((char)-78)
30 #define SHUB_LOOP_BACK_LIB     12
31 #define DEFAULT_COMMAND_SIZE   3
32 #define MAX_COMMAND_SIZE       82
33 #define MAX_DATA_SIZE          84       /* -79, 12, 4byte Delaytime + 78 bytes data */
34
35 static void int_to_bytes(int32_t value, int length, char cmd[])
36 {
37         /* Convert to Big-endian */
38         for (int i = length - 1; i >= 0; i--) {
39                 cmd[i] = (value & 0xff);
40                 value = value >> 8;
41         }
42 }
43
44 bool loopback_manager::run(int argc, char *argv[])
45 {
46         if (argc < DEFAULT_COMMAND_SIZE || argc > MAX_COMMAND_SIZE) {
47                 usage();
48                 return false;
49         }
50
51         if (!strcmp(argv[2], "start")) {
52                 int handle;
53                 sensor_t sensor;
54                 sensor = sensord_get_sensor(CONTEXT_SENSOR);
55                 handle = sensord_connect(sensor);
56
57                 /*
58                  *  sensorhub (bytes) command [0~1] delaytime [2~5] data[6~83]
59                  *      shell (argv)  command [0~2] delaytime [3]   data[4~81]
60                  */
61                 char test_data[MAX_DATA_SIZE] = {SHUB_INST_LIB_ADD, SHUB_LOOP_BACK_LIB, };
62                 int_to_bytes(atoi(argv[3]), sizeof(int), &(test_data[2]));
63
64                 for (int i = 4; i < argc; i++)
65                         test_data[i+2] = atoi(argv[i]);
66
67                 sensord_set_attribute_str(handle, 0, test_data, sizeof(test_data));
68                 sensord_disconnect(handle);
69                 return true;
70         }
71
72         if (!strcmp(argv[2], "stop")) {
73                 int handle;
74                 sensor_t sensor;
75                 sensor = sensord_get_sensor(CONTEXT_SENSOR);
76                 handle = sensord_connect(sensor);
77
78                 char test_data[4] = {SHUB_INST_LIB_REMOVE, SHUB_LOOP_BACK_LIB, };
79
80                 sensord_set_attribute_str(handle, 0, test_data, sizeof(test_data));
81                 sensord_disconnect(handle);
82                 return true;
83         }
84
85         usage();
86
87         return false;
88 }
89
90 void loopback_manager::usage(void)
91 {
92         _N("usage: sensorctl loopback [start/stop] [14byte sensor char data]\n");
93         _N("ex: sensorctl loopback start 15000 1 1 19 1 (after 15000ms, wrist up event)\n");
94         _N("ex: sensorctl loopback stop\n");
95         _N(" * if not enought 14byte, remain bytes are filled with 0\n");
96         _N("\n");
97 }
98