PX4 Firmware
PX4 Autopilot Software http://px4.io
qshell.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file qshell.cpp
36  * Send shell commands to qurt
37  *
38  * @author Nicolas de Palezieux <ndepal@gmail.com>
39  */
40 
41 #include <px4_platform_common/log.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <string>
46 #include <stdlib.h>
47 #include <drivers/drv_hrt.h>
49 
50 #include "qshell.h"
51 
52 px4::AppState QShell::appState;
53 
56 uint32_t QShell::_current_sequence = 0;
57 
58 int QShell::main(std::vector<std::string> argList)
59 {
60  int ret = _send_cmd(argList);
61 
62  if (ret != 0) {
63  PX4_ERR("Could not send command");
64  return -1;
65  }
66 
67  ret = _wait_for_retval();
68 
69  if (ret != 0) {
70  PX4_ERR("Could not get return value");
71  return -1;
72  }
73 
74  return 0;
75 }
76 
77 int QShell::_send_cmd(std::vector<std::string> &argList)
78 {
79  // Let's use a sequence number to check if a return value belongs to the
80  // command that we just sent and not a previous one that we assumed that
81  // it had timed out.
83 
84  struct qshell_req_s qshell_req;
85  std::string cmd;
86 
87  for (size_t i = 0; i < argList.size(); i++) {
88  cmd += argList[i];
89 
90  if (i < argList.size() - 1) {
91  cmd += " ";
92  }
93  }
94 
95  if (cmd.size() >= qshell_req.MAX_STRLEN) {
96  PX4_ERR("Command too long: %d >= %d", (int) cmd.size(), (int) qshell_req.MAX_STRLEN);
97  return -1;
98  }
99 
100  PX4_INFO("Send cmd: '%s'", cmd.c_str());
101 
102  qshell_req.strlen = cmd.size();
103  strcpy((char *)qshell_req.cmd, cmd.c_str());
104  qshell_req.request_sequence = _current_sequence;
105 
106  int instance;
107  orb_publish_auto(ORB_ID(qshell_req), &_pub_qshell_req, &qshell_req, &instance, ORB_PRIO_DEFAULT);
108 
109  return 0;
110 }
111 
113 {
114  if (_sub_qshell_retval < 0) {
115  _sub_qshell_retval = orb_subscribe(ORB_ID(qshell_retval));
116 
117  if (_sub_qshell_retval < 0) {
118  PX4_ERR("could not subscribe to retval");
119  return -1;
120  }
121  }
122 
123  const hrt_abstime time_started_us = hrt_absolute_time();
124 
125  while (hrt_elapsed_time(&time_started_us) < 3000000) {
126  bool updated;
127  orb_check(_sub_qshell_retval, &updated);
128 
129  if (updated) {
130 
131  struct qshell_retval_s retval;
132  orb_copy(ORB_ID(qshell_retval), _sub_qshell_retval, &retval);
133 
134  if (retval.return_sequence != _current_sequence) {
135  PX4_WARN("Ignoring return value with wrong sequence");
136 
137  } else {
138  if (retval.return_value) {
139  PX4_WARN("cmd returned with: %d", retval.return_value);
140  }
141 
142  return 0;
143  }
144  }
145 
146  usleep(1000);
147  }
148 
149  PX4_ERR("command timed out");
150  return -1;
151 }
static uint32_t _current_sequence
Definition: qshell.h:64
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Definition: uORB.cpp:90
char cmd[100]
Definition: qshell_req.h:57
int _send_cmd(std::vector< std::string > &argList)
Definition: qshell.cpp:77
int main()
Definition: qshell.cpp:72
static px4::AppState appState
Definition: qshell.h:56
int32_t return_value
Definition: qshell_retval.h:54
LidarLite * instance
Definition: ll40ls.cpp:65
High-resolution timer with callouts and timekeeping.
int orb_subscribe(const struct orb_metadata *meta)
Definition: uORB.cpp:75
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
Definition: drv_hrt.h:102
static orb_advert_t _pub_qshell_req
Definition: qshell.h:62
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
static int _sub_qshell_retval
Definition: qshell.h:63
uint32_t request_sequence
Definition: qshell_req.h:56
int orb_check(int handle, bool *updated)
Definition: uORB.cpp:95
uint32_t strlen
Definition: qshell_req.h:55
uint32_t return_sequence
Definition: qshell_retval.h:55
int _wait_for_retval()
Definition: qshell.cpp:112
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
static int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, int priority)
Advertise as the publisher of a topic.
Definition: uORB.h:177