adp.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /**
  2. * \file
  3. *
  4. * \brief ADP service implementation
  5. *
  6. * Copyright (C) 2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include <compiler.h>
  47. #include "adp.h"
  48. #include "adp_interface.h"
  49. #include "status_codes.h"
  50. #include <string.h>
  51. /** ID of last received message */
  52. static uint8_t last_received_message_id = 0xff;
  53. /** true if valid message has been received */
  54. static bool packet_received = false;
  55. /** Number of bytes received */
  56. static uint16_t bytes_received;
  57. /** Judge previous received data */
  58. static uint8_t prev_data = 0;
  59. /** The length of received data */
  60. static uint16_t length_received;
  61. /** Current state */
  62. static enum rx_state_e rx_state;
  63. uint16_t adp_add_send_byte(uint8_t* buffer, uint8_t index, uint8_t* data, uint16_t length)
  64. {
  65. for(uint16_t i = 0; i < length; i++) {
  66. if (*(data + i) == ADP_TOKEN){
  67. *(buffer + index) = ADP_TOKEN;
  68. index++;
  69. }
  70. *(buffer + index) = *(data + i);
  71. index++;
  72. }
  73. return index;
  74. }
  75. /**
  76. * \internal Handle incoming data byte
  77. *
  78. * \param[in] data New data byte to handle
  79. *
  80. * \return
  81. * \retval true Given data byte is part of message data
  82. * \retval false Given data byte is not part of message data
  83. */
  84. static bool adp_add_receive_byte(uint8_t data)
  85. {
  86. static uint8_t message_id;
  87. if ((rx_state == RX_STATE_GOT_SYMBOL) && (data != ADP_TOKEN)) {
  88. /* Abort packet reception, new packet incoming */
  89. rx_state = RX_STATE_WAIT_LENGTH_LSB;
  90. }
  91. switch (rx_state) {
  92. case RX_STATE_IDLE:
  93. packet_received = false;
  94. last_received_message_id = 0xFF;
  95. /* We are waiting for a new packet. */
  96. if (data != ADP_TOKEN) {
  97. return false;
  98. }
  99. /* Got start symbol, wait for message ID */
  100. rx_state = RX_STATE_WAIT_MSG_ID;
  101. return false;
  102. case RX_STATE_WAIT_MSG_ID:
  103. if (data == ADP_TOKEN) {
  104. /* Restart. Don't change state. Wait for new message ID */
  105. return false;
  106. }
  107. message_id = data;
  108. rx_state = RX_STATE_WAIT_LENGTH_LSB;
  109. return false;
  110. case RX_STATE_WAIT_LENGTH_LSB:
  111. if (data == ADP_TOKEN) {
  112. if (prev_data != ADP_TOKEN) {
  113. prev_data = ADP_TOKEN;
  114. return false;
  115. }
  116. }
  117. length_received = data;
  118. rx_state = RX_STATE_WAIT_LENGTH_MSB;
  119. prev_data = 0;
  120. return false;
  121. case RX_STATE_WAIT_LENGTH_MSB:
  122. if (data == ADP_TOKEN) {
  123. if (prev_data != ADP_TOKEN) {
  124. prev_data = ADP_TOKEN;
  125. return false;
  126. }
  127. }
  128. length_received += (uint16_t)data << 8;
  129. prev_data = 0;
  130. /* Got valid length, do we expect data? */
  131. if (length_received == 0) {
  132. /* No data here, wait for next packet */
  133. rx_state = RX_STATE_IDLE;
  134. packet_received = true;
  135. last_received_message_id = message_id;
  136. return false;
  137. }
  138. /* Wait for packet data */
  139. bytes_received = 0;
  140. rx_state = RX_STATE_GET_DATA;
  141. return false;
  142. case RX_STATE_GET_DATA:
  143. case RX_STATE_GOT_SYMBOL:
  144. if ((data == ADP_TOKEN) && (rx_state == RX_STATE_GET_DATA)) {
  145. rx_state = RX_STATE_GOT_SYMBOL;
  146. return false;
  147. }
  148. /* Add new data to rx buffer */
  149. bytes_received++;
  150. /* Are we done yet? */
  151. if (length_received == bytes_received) {
  152. /* Yes we are! */
  153. packet_received = true;
  154. rx_state = RX_STATE_IDLE;
  155. last_received_message_id = message_id;
  156. return true;
  157. }
  158. /* Not done yet.. keep on receiving */
  159. rx_state = RX_STATE_GET_DATA;
  160. return true;
  161. }
  162. return false;
  163. }
  164. static bool adp_is_received(void)
  165. {
  166. if (bytes_received == 0) {
  167. return false;
  168. }
  169. return packet_received;
  170. }
  171. static uint8_t adp_packet_received_get_id(void)
  172. {
  173. return last_received_message_id;
  174. }
  175. static bool adp_protocol_add_byte(uint8_t rx_id, uint8_t* rx_buf, uint8_t length, uint8_t* protocol_buf)
  176. {
  177. uint8_t i;
  178. for (i = 0; i < length; i++)
  179. {
  180. if (adp_add_receive_byte(*(rx_buf + i)) == true) {
  181. /* This is a data byte */
  182. protocol_buf[bytes_received - 1] = *(rx_buf + i);
  183. }
  184. if (adp_is_received() & (adp_packet_received_get_id() == rx_id)) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. static bool adp_check_for_response(uint8_t rx_id, uint8_t* protocol_buf, uint8_t length)
  191. {
  192. uint8_t retry;
  193. bool status = false;
  194. uint8_t rx_buf[ADP_MAX_PACKET_DATA_SIZE] = {0,};
  195. retry = 50;
  196. length = length + ADP_LENGTH_PACKET_HEADER;
  197. packet_received = false;
  198. while((adp_is_received() == false) & (retry-- > 0)) {
  199. if(adp_interface_read_response(rx_buf, length) == STATUS_OK) {
  200. status = adp_protocol_add_byte(rx_id, rx_buf, length, protocol_buf);
  201. if(status == true) {
  202. break;
  203. }
  204. }
  205. }
  206. return status;
  207. }
  208. static void adp_wait_for_response(uint8_t rx_id, uint8_t* protocol_buf, uint8_t length)
  209. {
  210. uint8_t rx_buf[ADP_MAX_PACKET_DATA_SIZE] = {0,};
  211. packet_received = false;
  212. length = length + ADP_LENGTH_PACKET_HEADER;
  213. while((adp_is_received() == false)) {
  214. if(adp_interface_read_response(rx_buf, length) == STATUS_OK) {
  215. adp_protocol_add_byte(rx_id, rx_buf, length, protocol_buf);
  216. }
  217. }
  218. }
  219. /**
  220. * \brief Initialization of the ADP service
  221. *
  222. * \return None
  223. */
  224. void adp_init(void)
  225. {
  226. adp_interface_init();
  227. }
  228. /**
  229. * \brief Send handshake to PC and check for response
  230. *
  231. * \param[in] protocol_version ADP version used in this implementation
  232. * \param[out] status Status returned by PC
  233. *
  234. * \return PC returned valid response to out handshake
  235. * \retval true If we got valid response
  236. * \retval false If we didn't receive a valid handshake response
  237. */
  238. static bool adp_request_handshake(uint8_t protocol_version, uint8_t options, uint8_t* protocol_buf)
  239. {
  240. uint16_t data_length = MSQ_REQ_HANDSHAKE_LEN + ADP_LENGTH_PACKET_HEADER;
  241. uint8_t key[8] = ADP_HANDSHAKE_KEY;
  242. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  243. struct adp_msg_format msg_format;
  244. struct adp_msg_request_handshake msg_request_handshake;
  245. msg_format.protocol_token = ADP_TOKEN;
  246. msg_format.protocol_msg_id = MSG_REQ_HANDSHAKE;
  247. msg_format.data_length = MSQ_REQ_HANDSHAKE_LEN;
  248. msg_request_handshake.protocol_version = protocol_version;
  249. msg_request_handshake.options = options;
  250. memcpy(&msg_request_handshake.key, key, 8);
  251. memcpy((uint8_t*)&msg_format.data, &msg_request_handshake, sizeof(msg_request_handshake));
  252. /* Send the protocol packet data */
  253. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  254. return adp_check_for_response(MSG_RES_HANDSHAKE, protocol_buf, 1);
  255. }
  256. /**
  257. * \brief Send handshake to PC until we get a valid response
  258. *
  259. * \warning Blocking function
  260. *
  261. * \return Handshake status. See /ref something
  262. */
  263. enum adp_handshake_status adp_wait_for_handshake(void)
  264. {
  265. uint8_t handshake_status;
  266. /* Keep sending handshake until we get something back */
  267. while (adp_request_handshake(ADP_VERSION, ADP_HANDSHAKE_OPTIONS_GPIO, &handshake_status) == false) {
  268. }
  269. /* Return status */
  270. return ((enum adp_handshake_status)handshake_status);
  271. }
  272. /**
  273. * \brief Request status from PC
  274. *
  275. * Sends a MSG_REQ_STATUS and waits for response
  276. *
  277. * \return PC Status. See /ref something
  278. */
  279. enum adp_status_code adp_request_status(void)
  280. {
  281. uint16_t data_length = MSG_REQ_STATUS_LEN + ADP_LENGTH_PACKET_HEADER;
  282. uint16_t status;
  283. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  284. struct adp_msg_format msg_format;
  285. msg_format.protocol_token = ADP_TOKEN;
  286. msg_format.protocol_msg_id = MSG_REQ_STATUS;
  287. msg_format.data_length = MSG_REQ_STATUS_LEN;
  288. /* Send the protocol packet data */
  289. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  290. /* Wait for response from PC */
  291. adp_wait_for_response(MSG_RES_STATUS, (uint8_t*)&status, 2);
  292. return ((enum adp_status_code)status);
  293. }
  294. /**
  295. * \brief Send MSG_CONF_INFO message and wait for response
  296. *
  297. * \param[in] title Application title
  298. * \param[in] description Application description
  299. *
  300. * \return Status from PC
  301. * \retval true Message received and accepted
  302. * \retval false Message received but not accepted
  303. */
  304. bool adp_configure_info(const char* title, const char* description)
  305. {
  306. uint16_t title_len;
  307. uint16_t description_len;
  308. uint8_t ack;
  309. uint16_t data_length;
  310. uint16_t index = 0;
  311. struct adp_msg_format msg_format;
  312. /* Add null-termination to length */
  313. title_len = strlen(title) + 1;
  314. description_len = strlen(description) + 1;
  315. /* Make sure the strings are not too long */
  316. Assert(title_len + description_len <= ADP_MAX_PACKET_DATA_SIZE);
  317. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  318. msg_format.protocol_token = ADP_TOKEN;
  319. msg_format.protocol_msg_id = MSG_CONF_INFO;
  320. msg_format.data_length = title_len + description_len;
  321. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)title, title_len);
  322. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)description, description_len);
  323. data_length = ADP_LENGTH_PACKET_HEADER + index;
  324. /* Send the protocol packet data */
  325. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  326. /* Wait for response and return status */
  327. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  328. return (ack == ADP_ACK_OK);
  329. }
  330. /**
  331. * \brief Send MSG_CONF_STREAM and wait for response
  332. *
  333. * \param[in] config Configuration struct
  334. *
  335. * \return Status from PC
  336. * \retval true Message received and accepted
  337. * \retval false Message received but not accepted
  338. */
  339. bool adp_configure_stream(struct adp_msg_configure_stream *const config, const char* label)
  340. {
  341. uint8_t ack;
  342. uint16_t data_length;
  343. uint16_t label_len;
  344. uint16_t index = 0;
  345. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  346. /* Add null-termination to length */
  347. label_len = strlen(label) + 1;
  348. /* Make sure the strings are not too long */
  349. Assert(label_len <= ADP_MAX_PACKET_DATA_SIZE);
  350. struct adp_msg_format msg_format;
  351. msg_format.protocol_token = ADP_TOKEN;
  352. msg_format.protocol_msg_id = MSG_CONF_STREAM;
  353. msg_format.data_length = MSG_CONF_STREAM_LEN + label_len;
  354. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->stream_id, 2);
  355. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->type, 1);
  356. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->mode, 1);
  357. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->state, 1);
  358. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  359. data_length = ADP_LENGTH_PACKET_HEADER + index;
  360. /* Send the protocol packet data */
  361. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  362. /* Wait for response and return status */
  363. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  364. return (ack == ADP_ACK_OK);
  365. }
  366. /**
  367. * \brief Send MSG_CONF_TOGGLE_STREAM and wait for response
  368. *
  369. * \param[in] stream_id ID of stream to toggle
  370. * \param[in] state New state for given stream
  371. *
  372. * \return Status from PC
  373. * \retval true Message received and accepted
  374. * \retval false Message received but not accepted
  375. */
  376. bool adp_toggle_stream(struct adp_msg_toggle_stream *const config)
  377. {
  378. uint8_t ack;
  379. uint16_t data_length;
  380. uint16_t index = 0;
  381. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  382. struct adp_msg_format msg_format;
  383. msg_format.protocol_token = ADP_TOKEN;
  384. msg_format.protocol_msg_id = MSG_CONF_TOGGLE_STREAM;
  385. msg_format.data_length = MSG_CONF_TOGGLE_STREAM_LEN;
  386. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->stream_id, 2);
  387. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->state, 1);
  388. data_length = ADP_LENGTH_PACKET_HEADER + index;
  389. /* Send the protocol packet data */
  390. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  391. /* Wait for response and return status */
  392. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  393. return (ack == ADP_ACK_OK);
  394. }
  395. /**
  396. * \brief Send MSG_CONF_GRAPH and wait for response
  397. *
  398. * \param[in] config Graph configuration struct
  399. *
  400. * \return Status from PC
  401. * \retval true Message received and accepted
  402. * \retval false Message received but not accepted
  403. */
  404. bool adp_configure_graph(struct adp_msg_configure_graph *const config, \
  405. const char* graph_label, const char* x_label)
  406. {
  407. uint8_t ack;
  408. uint16_t graph_label_len, x_label_len;
  409. /* Add 0-termination to label string length */
  410. graph_label_len = strlen(graph_label) + 1;
  411. x_label_len = strlen(x_label) + 1;
  412. /* Make sure label isn't too big */
  413. Assert(MSG_CONF_GRAPH_LEN + graph_label_len + x_label_len <= ADP_MAX_PACKET_DATA_SIZE);
  414. uint16_t data_length;
  415. uint16_t index = 0;
  416. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  417. struct adp_msg_format msg_format;
  418. msg_format.protocol_token = ADP_TOKEN;
  419. msg_format.protocol_msg_id = MSG_CONF_GRAPH;
  420. msg_format.data_length = MSG_CONF_GRAPH_LEN + graph_label_len + x_label_len;
  421. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_id, 2);
  422. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)graph_label, graph_label_len);
  423. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_min, 4);
  424. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_max, 4);
  425. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)x_label, x_label_len);
  426. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_scale_numerator, MSG_CONF_GRAPH_LEN - 10);
  427. data_length = ADP_LENGTH_PACKET_HEADER + index;
  428. /* Send the protocol packet data */
  429. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  430. /* Wait for response and return status */
  431. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  432. return (ack == ADP_ACK_OK);
  433. }
  434. /**
  435. * \brief Send MSG_CONF_TERMINAL and wait for response
  436. *
  437. * \param[in] config Terminal configuration struct
  438. *
  439. * \return Status from PC
  440. * \retval true Message received and accepted
  441. * \retval false Message received but not accepted
  442. */
  443. bool adp_configure_terminal(struct adp_msg_conf_terminal *const config, const char* label)
  444. {
  445. uint8_t ack;
  446. uint16_t label_len;
  447. /* Add 0-termination to label string length */
  448. label_len = strlen(label) + 1;
  449. /* Make sure label isn't too big */
  450. Assert(MSG_CONF_TERMINAL_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  451. uint16_t data_length;
  452. uint16_t index = 0;
  453. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  454. struct adp_msg_format msg_format;
  455. msg_format.protocol_token = ADP_TOKEN;
  456. msg_format.protocol_msg_id = MSG_CONF_TERMINAL;
  457. msg_format.data_length = MSG_CONF_TERMINAL_LEN + label_len;
  458. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->terminal_id, 2);
  459. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  460. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->width, MSG_CONF_TERMINAL_LEN - 1);
  461. data_length = ADP_LENGTH_PACKET_HEADER + index;
  462. /* Send the protocol packet data */
  463. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  464. /* Wait for response and return status */
  465. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  466. return (ack == ADP_ACK_OK);
  467. }
  468. /**
  469. * \brief Send MSG_CONF_ADD_TO_TERMINAL and wait for response
  470. *
  471. * \param[in] config Stream-to-terminal configuration struct
  472. *
  473. * \return Status from PC
  474. * \retval true Message received and accepted
  475. * \retval false Message received but not accepted
  476. */
  477. bool adp_add_stream_to_terminal(struct adp_msg_add_stream_to_terminal *const config, const char* tag_text)
  478. {
  479. uint8_t ack;
  480. uint16_t data_length;
  481. uint16_t tag_text_len;
  482. uint16_t index = 0;
  483. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  484. /* Add 0-termination to label string length */
  485. tag_text_len = strlen(tag_text) + 1;
  486. /* Make sure label isn't too big */
  487. Assert(MSG_CONF_ADD_TO_TERMINAL_LEN + tag_text_len <= ADP_MAX_PACKET_DATA_SIZE);
  488. struct adp_msg_format msg_format;
  489. msg_format.protocol_token = ADP_TOKEN;
  490. msg_format.protocol_msg_id = MSG_CONF_ADD_TO_TERMINAL;
  491. msg_format.data_length = MSG_CONF_ADD_TO_TERMINAL_LEN + tag_text_len;
  492. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->terminal_id, 2);
  493. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->stream_id, 2);
  494. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->mode, 1);
  495. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->text_color, 3);
  496. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)tag_text, tag_text_len);
  497. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->tag_text_color, 3);
  498. data_length = ADP_LENGTH_PACKET_HEADER + index;
  499. /* Send the protocol packet data */
  500. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  501. /* Wait for response and return status */
  502. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  503. return (ack == ADP_ACK_OK);
  504. }
  505. /**
  506. * \brief Send MSG_CONF_AXIS and wait for response
  507. *
  508. * \param[in] config Axis configuration struct
  509. *
  510. * \return Status from PC
  511. * \retval true Message received and accepted
  512. * \retval false Message received but not accepted
  513. */
  514. bool adp_add_axis_to_graph(struct adp_msg_conf_axis *const config, const char* label)
  515. {
  516. uint8_t ack;
  517. uint16_t data_length;
  518. uint16_t index = 0;
  519. uint16_t label_len;
  520. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  521. /* Add 0-termination to label string length */
  522. label_len = strlen(label) + 1;
  523. /* Make sure label isn't too big */
  524. Assert(MSG_CONF_AXIS_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  525. struct adp_msg_format msg_format;
  526. msg_format.protocol_token = ADP_TOKEN;
  527. msg_format.protocol_msg_id = MSG_CONF_AXIS;
  528. msg_format.data_length = MSG_CONF_AXIS_LEN + label_len;
  529. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->axis_id, 2);
  530. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_id, 2);
  531. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  532. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_min, 4);
  533. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_max, 4);
  534. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_scale_numerator, 4);
  535. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_scale_denominator, 4);
  536. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->mode, 1);
  537. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->color, 3);
  538. data_length = ADP_LENGTH_PACKET_HEADER + index;
  539. /* Send the protocol packet data */
  540. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  541. /* Wait for response and return status */
  542. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  543. return (ack == ADP_ACK_OK);
  544. }
  545. /**
  546. * \brief Send MSG_CONF_ADD_STREAM_TO_AXIS and wait for response
  547. *
  548. * \param[in] config Axis configuration struct
  549. *
  550. * \return Status from PC
  551. * \retval true Message received and accepted
  552. * \retval false Message received but not accepted
  553. */
  554. bool adp_add_stream_to_axis(struct adp_msg_add_stream_to_axis *const config)
  555. {
  556. uint8_t ack;
  557. uint16_t data_length;
  558. uint16_t index = 0;
  559. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  560. struct adp_msg_format msg_format;
  561. msg_format.protocol_token = ADP_TOKEN;
  562. msg_format.protocol_msg_id = MSG_CONF_ADD_STREAM_TO_AXIS;
  563. msg_format.data_length = MSG_CONF_ADD_STREAM_TO_AXIS_LEN;
  564. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_id, 2);
  565. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->axis_id, 2);
  566. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->stream_id, 2);
  567. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->sample_rate_numerator, 4);
  568. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->sample_rate_denominator, 4);
  569. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_scale_numerator, 4);
  570. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_scale_denominator, 4);
  571. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_offset, 4);
  572. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->transparency, 1);
  573. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->mode, 1);
  574. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_thickness, 1);
  575. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_color, 3);
  576. data_length = ADP_LENGTH_PACKET_HEADER + index;
  577. /* Send the protocol packet data */
  578. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  579. /* Wait for response and return status */
  580. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  581. return (ack == ADP_ACK_OK);
  582. }
  583. /**
  584. * \brief Send MSG_CONF_CURSOR_TO_GRAPH and wait for response
  585. *
  586. * \param[in] config Cursor configuration struct
  587. *
  588. * \return Status from PC
  589. * \retval true Message received and accepted
  590. * \retval false Message received but not accepted
  591. */
  592. bool adp_add_cursor_to_graph(struct adp_msg_add_cursor_to_graph *const config, const char* label)
  593. {
  594. uint8_t ack;
  595. uint16_t data_length;
  596. uint16_t index = 0;
  597. uint16_t label_len;
  598. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  599. /* Add 0-termination to label string length */
  600. label_len = strlen(label) + 1;
  601. /* Make sure label isn't too big */
  602. Assert(MSG_CONF_CURSOR_TO_GRAPH_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  603. struct adp_msg_format msg_format;
  604. msg_format.protocol_token = ADP_TOKEN;
  605. msg_format.protocol_msg_id = MSG_CONF_CURSOR_TO_GRAPH;
  606. msg_format.data_length = MSG_CONF_CURSOR_TO_GRAPH_LEN;
  607. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->stream_id, 2);
  608. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_id, 2);
  609. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->axis_id, 2);
  610. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  611. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->thickness, 1);
  612. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->color, 3);
  613. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->initial_value, 4);
  614. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->minimum_value, 4);
  615. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->maximum_value, 4);
  616. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->scale_numerator, 4);
  617. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->scale_denominator, 4);
  618. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->scale_offset, 4);
  619. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_style, 1);
  620. data_length = ADP_LENGTH_PACKET_HEADER + index;
  621. /* Send the protocol packet data */
  622. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  623. /* Wait for response and return status */
  624. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  625. return (ack == ADP_ACK_OK);
  626. }
  627. /**
  628. * \brief Send MSG_CONF_CURSOR_TO_GRAPH and wait for response
  629. *
  630. * \param[in] config Cursor configuration struct
  631. * \param[in] tag_high_state Tag text to display when GPIO pin is high.
  632. * \param[in] tag_low_state Tag text to display when GPIO pin is low.
  633. *
  634. * \return Status from PC
  635. * \retval true Message received and accepted
  636. * \retval false Message received but not accepted
  637. */
  638. bool adp_add_gpio_to_graph(struct adp_msg_conf_gpio_to_graph *const config, \
  639. const char* tag_high_state, const char* tag_low_state)
  640. {
  641. uint8_t ack;
  642. uint16_t data_length;
  643. uint16_t index = 0;
  644. uint16_t label_len, tag_high_state_len, tag_low_state_len;
  645. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  646. /* Add 0-termination to label string length */
  647. tag_high_state_len = strlen(tag_high_state) + 1;
  648. tag_low_state_len = strlen(tag_low_state) + 1;
  649. label_len = tag_high_state_len + tag_low_state_len;
  650. /* Make sure label isn't too big */
  651. Assert(MSG_CONF_GPIO_TO_GRAPH_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  652. struct adp_msg_format msg_format;
  653. msg_format.protocol_token = ADP_TOKEN;
  654. msg_format.protocol_msg_id = MSG_CONF_GPIO_TO_GRAPH;
  655. msg_format.data_length = MSG_CONF_GPIO_TO_GRAPH_LEN + label_len;
  656. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_id, 2);
  657. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->gpio_number, 1);
  658. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_id, 1);
  659. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)tag_high_state, tag_high_state_len);
  660. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)tag_low_state, tag_low_state_len);
  661. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->transparency, 1);
  662. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->mode, 2);
  663. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_thickness, 1);
  664. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_color_high_state, 3);
  665. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_color_low_state, 3);
  666. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->line_style, 1);
  667. data_length = ADP_LENGTH_PACKET_HEADER + index;
  668. /* Send the protocol packet data */
  669. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  670. /* Wait for response and return status */
  671. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  672. return (ack == ADP_ACK_OK);
  673. }
  674. /**
  675. * \brief Add a dashboard
  676. *
  677. * \param[in] config Pointer to dashboard configuration data struct
  678. * \param[in] label Dashboard label (0-terminated string)
  679. *
  680. * \return Status from PC
  681. * \retval true Message received and accepted
  682. * \retval false Message received but not accepted
  683. */
  684. bool adp_add_dashboard(struct adp_msg_conf_dashboard *const config, const char* label)
  685. {
  686. uint8_t ack;
  687. uint16_t label_len;
  688. /* Add 0-termination to label string length */
  689. label_len = strlen(label) + 1;
  690. /* Make sure label isn't too big */
  691. Assert(MSG_CONF_DASHBOARD_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  692. uint16_t data_length;
  693. uint16_t index = 0;
  694. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  695. struct adp_msg_format msg_format;
  696. msg_format.protocol_token = ADP_TOKEN;
  697. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD;
  698. msg_format.data_length = MSG_CONF_DASHBOARD_LEN + label_len;
  699. index = adp_add_send_byte((uint8_t*)&msg_format.data, index,(uint8_t*)&config->dashboard_id, 2);
  700. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  701. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->color, 3);
  702. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->height, 2);
  703. data_length = ADP_LENGTH_PACKET_HEADER + index;
  704. /* Send the protocol packet data */
  705. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  706. /* Wait for response and return status */
  707. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  708. return (ack == ADP_ACK_OK);
  709. }
  710. static uint16_t adp_add_dashboard_element_common_send_byte(uint8_t* add_buf, uint16_t index, \
  711. struct adp_msg_conf_dashboard_element_common *const config)
  712. {
  713. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->dashboard_id, 2);
  714. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->element_id, 2);
  715. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->z_index, 1);
  716. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->x, 2);
  717. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->y, 2);
  718. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->width, 2);
  719. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->height, 2);
  720. index = adp_add_send_byte(add_buf, index, (uint8_t *)&config->element_type, 1);
  721. return index;
  722. }
  723. /**
  724. * \brief Add a label to dashboard
  725. *
  726. * \param[in] config Pointer to dashboard label struct
  727. *
  728. * \return Status from PC
  729. * \retval true Message received and accepted
  730. * \retval false Message received but not accepted
  731. */
  732. bool adp_add_label_to_dashboard(struct adp_msg_conf_dashboard_element_label *const config, const char* label)
  733. {
  734. uint8_t ack;
  735. uint16_t label_len;
  736. /* Add 0-termination to label string length */
  737. label_len = strlen(label) + 1;
  738. /* Make sure label isn't too big */
  739. Assert(ADP_ELEMENT_TYPE_LABEL_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  740. uint16_t data_length;
  741. uint16_t index = 0;
  742. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  743. struct adp_msg_format msg_format;
  744. msg_format.protocol_token = ADP_TOKEN;
  745. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  746. msg_format.data_length = ADP_ELEMENT_TYPE_LABEL_LEN + label_len;
  747. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  748. (struct adp_msg_conf_dashboard_element_common *)config);
  749. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->font_size, 1);
  750. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->attribute, 1);
  751. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->horisontal_alignment, 1);
  752. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->vertical_alignment, 1);
  753. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->background_transparency, 1);
  754. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->background_color, 3);
  755. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->foreground_transparency, 1);
  756. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->foreground_color, 3);
  757. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  758. data_length = ADP_LENGTH_PACKET_HEADER + index;
  759. /* Send the protocol packet data */
  760. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  761. /* Wait for response and return status */
  762. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  763. return (ack == ADP_ACK_OK);
  764. }
  765. /**
  766. * \brief Add a button to dashboard
  767. *
  768. * \param[in] config Pointer to dashboard button struct
  769. *
  770. * \return Status from PC
  771. * \retval true Message received and accepted
  772. * \retval false Message received but not accepted
  773. */
  774. bool adp_add_button_to_dashboard(struct adp_msg_conf_dashboard_element_button *const config, const char* label)
  775. {
  776. uint8_t ack;
  777. uint16_t label_len;
  778. /* Add 0-termination to label string length */
  779. label_len = strlen(label) + 1;
  780. /* Make sure label isn't too big */
  781. Assert(ADP_ELEMENT_TYPE_BUTTON_LEN + label_len <= ADP_MAX_PACKET_DATA_SIZE);
  782. uint16_t data_length;
  783. uint16_t index = 0;
  784. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  785. struct adp_msg_format msg_format;
  786. msg_format.protocol_token = ADP_TOKEN;
  787. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  788. msg_format.data_length = ADP_ELEMENT_TYPE_BUTTON_LEN + label_len;
  789. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  790. (struct adp_msg_conf_dashboard_element_common *)config);
  791. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->font_size, 1);
  792. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)label, label_len);
  793. data_length = ADP_LENGTH_PACKET_HEADER + index;
  794. /* Send the protocol packet data */
  795. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  796. /* Wait for response and return status */
  797. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  798. return (ack == ADP_ACK_OK);
  799. }
  800. /**
  801. * \brief Add a slider to dashboard
  802. *
  803. * \param[in] config Pointer to dashboard slider struct
  804. *
  805. * \return Status from PC
  806. * \retval true Message received and accepted
  807. * \retval false Message received but not accepted
  808. */
  809. bool adp_add_slider_to_dashboard(struct adp_msg_conf_dashboard_element_slider *const config)
  810. {
  811. uint8_t ack;
  812. uint16_t data_length;
  813. uint16_t index = 0;
  814. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  815. struct adp_msg_format msg_format;
  816. msg_format.protocol_token = ADP_TOKEN;
  817. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  818. msg_format.data_length = ADP_ELEMENT_TYPE_SLIDER_LEN;
  819. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  820. (struct adp_msg_conf_dashboard_element_common *)config);
  821. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->minimum_value, 4);
  822. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->maximum_value, 4);
  823. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->initial_value, 4);
  824. data_length = ADP_LENGTH_PACKET_HEADER + index;
  825. /* Send the protocol packet data */
  826. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  827. /* Wait for response and return status */
  828. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  829. return (ack == ADP_ACK_OK);
  830. }
  831. /**
  832. * \brief Add a progress bar to dashboard
  833. *
  834. * \param[in] config Pointer to dashboard progress bar struct
  835. *
  836. * \return Status from PC
  837. * \retval true Message received and accepted
  838. * \retval false Message received but not accepted
  839. */
  840. bool adp_add_progress_to_dashboard(struct adp_msg_conf_dashboard_element_progress *const config)
  841. {
  842. uint8_t ack;
  843. uint16_t data_length;
  844. uint16_t index = 0;
  845. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  846. struct adp_msg_format msg_format;
  847. msg_format.protocol_token = ADP_TOKEN;
  848. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  849. msg_format.data_length = ADP_ELEMENT_TYPE_PROGRESS_LEN;
  850. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  851. (struct adp_msg_conf_dashboard_element_common *)config);
  852. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->minimum_value, 4);
  853. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->maximum_value, 4);
  854. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->initial_value, 4);
  855. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->color, 3);
  856. data_length = ADP_LENGTH_PACKET_HEADER + index;
  857. /* Send the protocol packet data */
  858. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  859. /* Wait for response and return status */
  860. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  861. return (ack == ADP_ACK_OK);
  862. }
  863. /**
  864. * \brief Add a signal to dashboard
  865. *
  866. * \param[in] config Pointer to dashboard signal struct
  867. *
  868. * \return Status from PC
  869. * \retval true Message received and accepted
  870. * \retval false Message received but not accepted
  871. */
  872. bool adp_add_signal_to_dashboard(struct adp_msg_conf_dashboard_element_signal *const config)
  873. {
  874. uint8_t ack;
  875. uint16_t data_length;
  876. uint16_t index = 0;
  877. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  878. struct adp_msg_format msg_format;
  879. msg_format.protocol_token = ADP_TOKEN;
  880. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  881. msg_format.data_length = ADP_ELEMENT_TYPE_SIGNAL_LEN;
  882. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  883. (struct adp_msg_conf_dashboard_element_common *)config);
  884. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->on_transparency, 1);
  885. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->on_color, 3);
  886. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->off_transparency, 1);
  887. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->off_color, 3);
  888. data_length = ADP_LENGTH_PACKET_HEADER + index;
  889. /* Send the protocol packet data */
  890. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  891. /* Wait for response and return status */
  892. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  893. return (ack == ADP_ACK_OK);
  894. }
  895. /**
  896. * \brief Add a segment display to dashboard
  897. *
  898. * \param[in] config Pointer to dashboard segment struct
  899. *
  900. * \return Status from PC
  901. * \retval true Message received and accepted
  902. * \retval false Message received but not accepted
  903. */
  904. bool adp_add_segment_to_dashboard(struct adp_msg_conf_dashboard_element_segment *const config)
  905. {
  906. uint8_t ack;
  907. uint16_t data_length;
  908. uint16_t index = 0;
  909. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  910. struct adp_msg_format msg_format;
  911. msg_format.protocol_token = ADP_TOKEN;
  912. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  913. msg_format.data_length = ADP_ELEMENT_TYPE_SEGMENT_LEN;
  914. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  915. (struct adp_msg_conf_dashboard_element_common *)config);
  916. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->segment_count, 1);
  917. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->base, 1);
  918. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->transparency, 1);
  919. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->color, 3);
  920. data_length = ADP_LENGTH_PACKET_HEADER + index;
  921. /* Send the protocol packet data */
  922. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  923. /* Wait for response and return status */
  924. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  925. return (ack == ADP_ACK_OK);
  926. }
  927. /**
  928. * \brief Add a graph to dashboard
  929. *
  930. * \param[in] config Pointer to dashboard graph struct
  931. *
  932. * \return Status from PC
  933. * \retval true Message received and accepted
  934. * \retval false Message received but not accepted
  935. */
  936. bool adp_add_graph_to_dashboard(struct adp_msg_conf_dashboard_element_graph *const config, const char* title)
  937. {
  938. uint8_t ack;
  939. uint16_t title_len;
  940. /* Add 0-termination to label string length */
  941. title_len = strlen(title) + 1;
  942. /* Make sure label isn't too big */
  943. Assert(ADP_ELEMENT_TYPE_GRAPH_LEN + title_len <= ADP_MAX_PACKET_DATA_SIZE);
  944. uint16_t data_length;
  945. uint16_t index = 0;
  946. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  947. struct adp_msg_format msg_format;
  948. msg_format.protocol_token = ADP_TOKEN;
  949. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  950. msg_format.data_length = ADP_ELEMENT_TYPE_GRAPH_LEN + title_len;
  951. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  952. (struct adp_msg_conf_dashboard_element_common *)config);
  953. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->title_color, 3);
  954. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->background_color, 3);
  955. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->graph_background_color, 3);
  956. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)title, title_len);
  957. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->plot_count, 1);
  958. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_min, 4);
  959. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->x_max, 4);
  960. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_min, 4);
  961. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->y_max, 4);
  962. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->mode, 1);
  963. data_length = ADP_LENGTH_PACKET_HEADER + index;
  964. /* Send the protocol packet data */
  965. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  966. /* Wait for response and return status */
  967. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  968. return (ack == ADP_ACK_OK);
  969. }
  970. /**
  971. * \brief Add data fields to dashboard
  972. *
  973. * \param[in] config Pointer to dashboard text struct
  974. *
  975. * \return Status from PC
  976. * \retval true Message received and accepted
  977. * \retval false Message received but not accepted
  978. */
  979. bool adp_add_text_to_dashboard(struct adp_msg_conf_dashboard_element_text *const config)
  980. {
  981. uint8_t ack;
  982. uint16_t data_length;
  983. uint16_t index = 0;
  984. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  985. struct adp_msg_format msg_format;
  986. msg_format.protocol_token = ADP_TOKEN;
  987. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  988. msg_format.data_length = ADP_ELEMENT_TYPE_TEXT_LEN;
  989. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  990. (struct adp_msg_conf_dashboard_element_common *)config);
  991. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->minimum, 4);
  992. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->maximum, 4);
  993. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->value, 4);
  994. data_length = ADP_LENGTH_PACKET_HEADER + index;
  995. /* Send the protocol packet data */
  996. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  997. /* Wait for response and return status */
  998. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  999. return (ack == ADP_ACK_OK);
  1000. }
  1001. /**
  1002. * \brief Add a radio to dashboard
  1003. *
  1004. * \param[in] config Pointer to dashboard radio struct
  1005. *
  1006. * \return Status from PC
  1007. * \retval true Message received and accepted
  1008. * \retval false Message received but not accepted
  1009. */
  1010. bool adp_add_radio_to_dashboard(struct adp_msg_conf_dashboard_element_radio *const config, const char* text)
  1011. {
  1012. uint8_t ack;
  1013. uint16_t text_len;
  1014. /* Add 0-termination to label string length */
  1015. text_len = strlen(text) + 1;
  1016. /* Make sure label isn't too big */
  1017. Assert(ADP_ELEMENT_TYPE_RADIO_LEN + text_len <= ADP_MAX_PACKET_DATA_SIZE);
  1018. uint16_t data_length;
  1019. uint16_t index = 0;
  1020. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  1021. struct adp_msg_format msg_format;
  1022. msg_format.protocol_token = ADP_TOKEN;
  1023. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  1024. msg_format.data_length = ADP_ELEMENT_TYPE_RADIO_LEN + text_len;
  1025. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  1026. (struct adp_msg_conf_dashboard_element_common *)config);
  1027. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->font_size, 1);
  1028. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->number_items, 1);
  1029. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->orientation, 1);
  1030. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)text, text_len);
  1031. data_length = ADP_LENGTH_PACKET_HEADER + index;
  1032. /* Send the protocol packet data */
  1033. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  1034. /* Wait for response and return status */
  1035. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  1036. return (ack == ADP_ACK_OK);
  1037. }
  1038. /**
  1039. * \brief Add a pie to dashboard
  1040. *
  1041. * \param[in] config Pointer to dashboard pie struct
  1042. *
  1043. * \return Status from PC
  1044. * \retval true Message received and accepted
  1045. * \retval false Message received but not accepted
  1046. */
  1047. bool adp_add_pie_to_dashboard(struct adp_msg_conf_dashboard_element_pie *const config, const char* title)
  1048. {
  1049. uint8_t ack;
  1050. uint16_t title_len;
  1051. /* Add 0-termination to label string length */
  1052. title_len = strlen(title) + 1;
  1053. /* Make sure label isn't too big */
  1054. Assert(ADP_ELEMENT_TYPE_PIE_LEN + title_len <= ADP_MAX_PACKET_DATA_SIZE);
  1055. uint16_t data_length;
  1056. uint16_t index = 0;
  1057. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  1058. struct adp_msg_format msg_format;
  1059. msg_format.protocol_token = ADP_TOKEN;
  1060. msg_format.protocol_msg_id = MSG_CONF_DASHBOARD_ELEMENT;
  1061. msg_format.data_length = ADP_ELEMENT_TYPE_PIE_LEN + title_len;
  1062. index = adp_add_dashboard_element_common_send_byte((uint8_t*)&msg_format.data, index, \
  1063. (struct adp_msg_conf_dashboard_element_common *)config);
  1064. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->background_color, 3);
  1065. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->title_color, 3);
  1066. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)title, title_len);
  1067. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->number_slices, 1);
  1068. data_length = ADP_LENGTH_PACKET_HEADER + index;
  1069. /* Send the protocol packet data */
  1070. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  1071. /* Wait for response and return status */
  1072. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  1073. return (ack == ADP_ACK_OK);
  1074. }
  1075. /**
  1076. * \brief Connect a stream to dashboard element
  1077. *
  1078. * \param[in] config Pointer to stream element configuration struct
  1079. *
  1080. * \return Status from PC
  1081. * \retval true Message received and accepted
  1082. * \retval false Message received but not accepted
  1083. */
  1084. bool adp_add_stream_to_element(struct adp_conf_add_stream_to_element *const config)
  1085. {
  1086. uint8_t ack;
  1087. uint16_t data_length;
  1088. uint16_t index = 0;
  1089. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  1090. struct adp_msg_format msg_format;
  1091. msg_format.protocol_token = ADP_TOKEN;
  1092. msg_format.protocol_msg_id = MSG_CONF_ADD_STREAM_TO_ELEMENT;
  1093. msg_format.data_length = MSG_CONF_ADD_STREAM_TO_ELEMENT_LEN;
  1094. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->dashboard_id, 2);
  1095. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->element_id, 2);
  1096. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&config->stream_id, 2);
  1097. data_length = ADP_LENGTH_PACKET_HEADER + index;
  1098. /* Send the protocol packet data */
  1099. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  1100. /* Wait for response and return status */
  1101. adp_wait_for_response(MSG_CONF_ACK, &ack, 1);
  1102. return (ack == ADP_ACK_OK);
  1103. }
  1104. bool adp_transceive_stream(struct adp_msg_data_stream *const stream_data, uint8_t *protocol_buf)
  1105. {
  1106. uint8_t stream_num;
  1107. uint16_t data_length;
  1108. uint16_t index = 0;
  1109. uint8_t rx_buf[ADP_MAX_PACKET_LENGTH] = {0,};
  1110. struct adp_msg_format msg_format;
  1111. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, (uint8_t*)&stream_data->number_of_streams, 1);
  1112. /* find packet size */
  1113. for (stream_num = 0; stream_num < stream_data->number_of_streams; stream_num++) {
  1114. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, \
  1115. (uint8_t*)&stream_data->stream[stream_num].stream_id, 2);
  1116. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, \
  1117. (uint8_t*)&stream_data->stream[stream_num].data_size, 1);
  1118. index = adp_add_send_byte((uint8_t*)&msg_format.data, index, \
  1119. stream_data->stream[stream_num].data, \
  1120. stream_data->stream[stream_num].data_size);
  1121. }
  1122. msg_format.protocol_token = ADP_TOKEN;
  1123. msg_format.protocol_msg_id = MSG_DATA_STREAM;
  1124. msg_format.data_length = index;
  1125. data_length = ADP_LENGTH_PACKET_HEADER + index;
  1126. /* Send the protocol packet data */
  1127. adp_interface_transceive_procotol((uint8_t*)&msg_format, data_length, rx_buf);
  1128. return adp_protocol_add_byte(MSG_RES_DATA, rx_buf, data_length, protocol_buf);
  1129. }
  1130. /**
  1131. * \brief Send and read data on one single stream
  1132. *
  1133. * \param[in] stream_id ID of stream
  1134. * \param[in] data Pointer to data to send
  1135. * \param[in] data_size Size of data to send
  1136. *
  1137. * \return None
  1138. */
  1139. bool adp_transceive_single_stream(uint16_t stream_id, uint8_t* data, uint8_t data_size, uint8_t* protocol_buf)
  1140. {
  1141. struct adp_msg_data_stream data_stream;
  1142. volatile uint8_t status;
  1143. data_stream.number_of_streams = 1;
  1144. data_stream.stream[0].stream_id = stream_id;
  1145. data_stream.stream[0].data_size = data_size;
  1146. data_stream.stream[0].data = data;
  1147. status = adp_transceive_stream(&data_stream, protocol_buf);
  1148. return status;
  1149. }