upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
rn2903.h
Go to the documentation of this file.
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2017 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 
31 #include <upm.h>
32 #include <mraa/uart.h>
33 
34 #include "rn2903_defs.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
50  typedef struct _rn2903_context {
51  mraa_uart_context uart;
52  // store the baudrate
53  int baudrate;
54 
55  // response data buffer, stripped of CR/LF
56  char resp_data[RN2903_MAX_BUFFER];
57  // length of response data
58  size_t resp_len;
59 
60  // these are allocated buffers we use manage and reuse
61  // internally to store conversions from/to hex and back
62  char *to_hex_buf;
63  char *from_hex_buf;
64 
65  // maximum time to wait for a response after a command is
66  // submitted
67  int cmd_resp_wait_ms;
68 
69  // maximum time to wait for a second response after a command is
70  // submitted
71  int cmd_resp2_wait_ms;
72 
73  // debugging output
74  bool debug;
75 
76  // our hardware hex encoded EUI + terminating NULL
77  char hardware_eui[RN2903_MAX_HEX_EUI64 + 1];
78 
79  // 16b mac status word
80  uint16_t mac_status_word;
81  // this is the mac_status bitfield of the mac status word
82  RN2903_MAC_STATUS_T mac_mac_status;
83  } *rn2903_context;
84 
93  rn2903_context rn2903_init(unsigned int uart, unsigned int baudrate);
94 
105  rn2903_context rn2903_init_tty(const char *uart_tty, unsigned int baudrate);
106 
112  void rn2903_close(rn2903_context dev);
113 
124  unsigned int wait_ms);
125 
136  unsigned int wait_ms);
137 
148  bool rn2903_data_available(const rn2903_context dev,
149  unsigned int millis);
150 
159  void rn2903_drain(const rn2903_context dev);
160 
174  RN2903_RESPONSE_T rn2903_command(const rn2903_context dev,
175  const char *cmd);
176 
188  RN2903_RESPONSE_T rn2903_command_with_arg(const rn2903_context dev,
189  const char *cmd,
190  const char *arg);
191 
214  RN2903_RESPONSE_T rn2903_waitfor_response(const rn2903_context dev,
215  int wait_ms);
216 
228  const char *rn2903_get_response(const rn2903_context dev);
229 
241  size_t rn2903_get_response_len(const rn2903_context dev);
242 
252  upm_result_t rn2903_set_device_eui(const rn2903_context dev,
253  const char *str);
254 
263  upm_result_t rn2903_get_device_eui(const rn2903_context dev);
264 
274  upm_result_t rn2903_set_application_eui(const rn2903_context dev,
275  const char *str);
276 
285  upm_result_t rn2903_get_application_eui(const rn2903_context dev);
286 
296  upm_result_t rn2903_set_application_key(const rn2903_context dev,
297  const char *str);
298 
307  upm_result_t rn2903_get_application_key(const rn2903_context dev);
308 
322  upm_result_t rn2903_set_device_addr(const rn2903_context dev,
323  const char *str);
324 
333  upm_result_t rn2903_get_device_addr(const rn2903_context dev);
334 
349  upm_result_t rn2903_set_network_session_key(const rn2903_context dev,
350  const char *str);
351 
366  upm_result_t rn2903_set_application_session_key(const rn2903_context dev,
367  const char *str);
368 
383  const char *rn2903_to_hex(const rn2903_context dev,
384  const char *src, int len);
385 
402  const char *rn2903_from_hex(const rn2903_context dev,
403  const char *src);
404 
423  RN2903_JOIN_STATUS_T rn2903_join(const rn2903_context dev,
424  RN2903_JOIN_TYPE_T type);
425 
452  RN2903_MAC_TX_STATUS_T rn2903_mac_tx(const rn2903_context dev,
453  RN2903_MAC_MSG_TYPE_T type,
454  int port, const char *payload);
455 
469  RN2903_RESPONSE_T rn2903_radio_tx(const rn2903_context dev,
470  const char *payload);
471 
490  RN2903_RESPONSE_T rn2903_radio_rx(const rn2903_context dev,
491  int window_size);
492 
502  const char *rn2903_get_hardware_eui(const rn2903_context dev);
503 
512  upm_result_t rn2903_update_mac_status(const rn2903_context dev);
513 
522  uint16_t rn2903_get_mac_status_word(const rn2903_context dev);
523 
533  RN2903_MAC_STATUS_T rn2903_get_mac_status(const rn2903_context dev);
534 
547  upm_result_t rn2903_mac_save(const rn2903_context dev);
548 
577  upm_result_t rn2903_mac_pause(const rn2903_context dev);
578 
587  upm_result_t rn2903_mac_resume(const rn2903_context dev);
588 
600  upm_result_t rn2903_reset(const rn2903_context dev);
601 
617  upm_result_t rn2903_mac_set_battery(const rn2903_context dev, int level);
618 
628  void rn2903_set_debug(const rn2903_context dev, bool enable);
629 
638  int rn2903_read(const rn2903_context dev, char *buffer, size_t len);
639 
648  int rn2903_write(const rn2903_context dev, const char *buffer, size_t len);
649 
662  upm_result_t rn2903_set_baudrate(const rn2903_context dev,
663  unsigned int baudrate);
664 
678  upm_result_t rn2903_set_flow_control(const rn2903_context dev,
679  RN2903_FLOW_CONTROL_T fc);
680 
691  bool rn2903_find(const rn2903_context dev, const char *str);
692 
703  const char *rn2903_get_radio_rx_payload(const rn2903_context dev);
704 
714  bool rn2903_autobaud(const rn2903_context dev, int retries);
715 
716 #ifdef __cplusplus
717 }
718 #endif
const char * rn2903_to_hex(const rn2903_context dev, const char *src, int len)
Definition: rn2903.c:421
upm_result_t rn2903_set_device_addr(const rn2903_context dev, const char *str)
Definition: rn2903.c:799
upm_result_t rn2903_set_application_eui(const rn2903_context dev, const char *str)
Definition: rn2903.c:735
const char * rn2903_get_response(const rn2903_context dev)
Definition: rn2903.c:407
const char * rn2903_from_hex(const rn2903_context dev, const char *src)
Definition: rn2903.c:459
upm_result_t rn2903_get_device_eui(const rn2903_context dev)
Definition: rn2903.c:725
upm_result_t rn2903_set_device_eui(const rn2903_context dev, const char *str)
Definition: rn2903.c:704
struct _rn2903_context * rn2903_context
rn2903_context rn2903_init_tty(const char *uart_tty, unsigned int baudrate)
Definition: rn2903.c:180
upm_result_t rn2903_mac_pause(const rn2903_context dev)
Definition: rn2903.c:885
upm_result_t rn2903_set_flow_control(const rn2903_context dev, RN2903_FLOW_CONTROL_T fc)
Definition: rn2903.c:669
upm_result_t rn2903_reset(const rn2903_context dev)
Definition: rn2903.c:562
void rn2903_set_debug(const rn2903_context dev, bool enable)
Definition: rn2903.c:269
int rn2903_write(const rn2903_context dev, const char *buffer, size_t len)
Definition: rn2903.c:223
upm_result_t rn2903_get_device_addr(const rn2903_context dev)
Definition: rn2903.c:821
int rn2903_read(const rn2903_context dev, char *buffer, size_t len)
Definition: rn2903.c:215
upm_result_t rn2903_get_application_eui(const rn2903_context dev)
Definition: rn2903.c:757
upm_result_t rn2903_get_application_key(const rn2903_context dev)
Definition: rn2903.c:789
rn2903_context rn2903_init(unsigned int uart, unsigned int baudrate)
Definition: rn2903.c:159
RN2903_RESPONSE_T rn2903_radio_rx(const rn2903_context dev, int window_size)
Definition: rn2903.c:1043
bool rn2903_find(const rn2903_context dev, const char *str)
Definition: rn2903.c:696
upm_result_t rn2903_mac_resume(const rn2903_context dev)
Definition: rn2903.c:895
void rn2903_drain(const rn2903_context dev)
Definition: rn2903.c:292
void rn2903_set_response_wait_time(const rn2903_context dev, unsigned int wait_ms)
Definition: rn2903.c:276
uint16_t rn2903_get_mac_status_word(const rn2903_context dev)
Definition: rn2903.c:548
Definition: rn2903.h:50
upm_result_t rn2903_set_network_session_key(const rn2903_context dev, const char *str)
Definition: rn2903.c:831
RN2903_RESPONSE_T rn2903_command_with_arg(const rn2903_context dev, const char *cmd, const char *arg)
Definition: rn2903.c:390
void rn2903_close(rn2903_context dev)
Definition: rn2903.c:200
upm_result_t rn2903_mac_save(const rn2903_context dev)
Definition: rn2903.c:875
bool rn2903_autobaud(const rn2903_context dev, int retries)
Definition: rn2903.c:1103
RN2903_RESPONSE_T rn2903_waitfor_response(const rn2903_context dev, int wait_ms)
Definition: rn2903.c:312
RN2903_MAC_TX_STATUS_T rn2903_mac_tx(const rn2903_context dev, RN2903_MAC_MSG_TYPE_T type, int port, const char *payload)
Definition: rn2903.c:905
RN2903_MAC_STATUS_T rn2903_get_mac_status(const rn2903_context dev)
Definition: rn2903.c:555
size_t rn2903_get_response_len(const rn2903_context dev)
Definition: rn2903.c:414
bool rn2903_data_available(const rn2903_context dev, unsigned int millis)
Definition: rn2903.c:233
upm_result_t rn2903_mac_set_battery(const rn2903_context dev, int level)
Definition: rn2903.c:1080
upm_result_t rn2903_set_application_key(const rn2903_context dev, const char *str)
Definition: rn2903.c:767
RN2903_RESPONSE_T rn2903_radio_tx(const rn2903_context dev, const char *payload)
Definition: rn2903.c:1001
RN2903_JOIN_STATUS_T rn2903_join(const rn2903_context dev, RN2903_JOIN_TYPE_T type)
Definition: rn2903.c:588
upm_result_t rn2903_update_mac_status(const rn2903_context dev)
Definition: rn2903.c:507
const char * rn2903_get_hardware_eui(const rn2903_context dev)
Definition: rn2903.c:500
void rn2903_set_response2_wait_time(const rn2903_context dev, unsigned int wait_ms)
Definition: rn2903.c:284
upm_result_t rn2903_set_baudrate(const rn2903_context dev, unsigned int baudrate)
Definition: rn2903.c:243
const char * rn2903_get_radio_rx_payload(const rn2903_context dev)
Definition: rn2903.c:1151
RN2903_RESPONSE_T rn2903_command(const rn2903_context dev, const char *cmd)
Definition: rn2903.c:364
upm_result_t rn2903_set_application_session_key(const rn2903_context dev, const char *str)
Definition: rn2903.c:853