Host driver/HAL to build a LoRa Picocell Gateway which communicates through USB with a concentrator board based on Semtech SX1308 multi-channel modem and SX1257/SX1255 RF transceivers.
libloragw/src/loragw_com.c@0:102b50f941d0, 2018-04-11 (annotated)
- Committer:
- dgabino
- Date:
- Wed Apr 11 14:38:42 2018 +0000
- Revision:
- 0:102b50f941d0
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgabino | 0:102b50f941d0 | 1 | /* |
dgabino | 0:102b50f941d0 | 2 | / _____) _ | | |
dgabino | 0:102b50f941d0 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
dgabino | 0:102b50f941d0 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
dgabino | 0:102b50f941d0 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
dgabino | 0:102b50f941d0 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
dgabino | 0:102b50f941d0 | 7 | (C)2017 Semtech-Cycleo |
dgabino | 0:102b50f941d0 | 8 | |
dgabino | 0:102b50f941d0 | 9 | Description: |
dgabino | 0:102b50f941d0 | 10 | A communication bridge layer to abstract linux/windows OS or others. |
dgabino | 0:102b50f941d0 | 11 | The current project support only linux os |
dgabino | 0:102b50f941d0 | 12 | |
dgabino | 0:102b50f941d0 | 13 | License: Revised BSD License, see LICENSE.TXT file include in the project |
dgabino | 0:102b50f941d0 | 14 | */ |
dgabino | 0:102b50f941d0 | 15 | |
dgabino | 0:102b50f941d0 | 16 | |
dgabino | 0:102b50f941d0 | 17 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 18 | /* --- DEPENDANCIES --------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 19 | |
dgabino | 0:102b50f941d0 | 20 | #include <stdint.h> /* C99 types */ |
dgabino | 0:102b50f941d0 | 21 | #include <stdio.h> /* printf fprintf */ |
dgabino | 0:102b50f941d0 | 22 | #include <stdlib.h> /* malloc free */ |
dgabino | 0:102b50f941d0 | 23 | #include <unistd.h> /* lseek, close */ |
dgabino | 0:102b50f941d0 | 24 | #include <fcntl.h> /* open */ |
dgabino | 0:102b50f941d0 | 25 | #include <string.h> /* memset */ |
dgabino | 0:102b50f941d0 | 26 | #include <errno.h> /* Error number definitions */ |
dgabino | 0:102b50f941d0 | 27 | #include <termios.h> /* POSIX terminal control definitions */ |
dgabino | 0:102b50f941d0 | 28 | #include <sys/ioctl.h> |
dgabino | 0:102b50f941d0 | 29 | #include <pthread.h> |
dgabino | 0:102b50f941d0 | 30 | #include <time.h> |
dgabino | 0:102b50f941d0 | 31 | #include <sys/select.h> |
dgabino | 0:102b50f941d0 | 32 | |
dgabino | 0:102b50f941d0 | 33 | #include "loragw_aux.h" |
dgabino | 0:102b50f941d0 | 34 | #include "loragw_reg.h" |
dgabino | 0:102b50f941d0 | 35 | #include "loragw_com.h" |
dgabino | 0:102b50f941d0 | 36 | #include "loragw_com_linux.h" |
dgabino | 0:102b50f941d0 | 37 | |
dgabino | 0:102b50f941d0 | 38 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 39 | /* --- PRIVATE MACROS ------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 40 | |
dgabino | 0:102b50f941d0 | 41 | #if DEBUG_COM == 1 |
dgabino | 0:102b50f941d0 | 42 | #define DEBUG_MSG(str) fprintf(stderr, str) |
dgabino | 0:102b50f941d0 | 43 | #define DEBUG_PRINTF(fmt, args...) fprintf(stderr,"%s:%d: "fmt, __FUNCTION__, __LINE__, args) |
dgabino | 0:102b50f941d0 | 44 | #define CHECK_NULL(a) if(a==NULL){fprintf(stderr,"%s:%d: ERROR: NULL POINTER AS ARGUMENT\n", __FUNCTION__, __LINE__);return LGW_COM_ERROR;} |
dgabino | 0:102b50f941d0 | 45 | #else |
dgabino | 0:102b50f941d0 | 46 | #define DEBUG_MSG(str) |
dgabino | 0:102b50f941d0 | 47 | #define DEBUG_PRINTF(fmt, args...) |
dgabino | 0:102b50f941d0 | 48 | #define CHECK_NULL(a) if(a==NULL){return LGW_COM_ERROR;} |
dgabino | 0:102b50f941d0 | 49 | #endif |
dgabino | 0:102b50f941d0 | 50 | |
dgabino | 0:102b50f941d0 | 51 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 52 | /* --- PRIVATE SHARED VARIABLES (GLOBAL) ------------------------------------ */ |
dgabino | 0:102b50f941d0 | 53 | |
dgabino | 0:102b50f941d0 | 54 | extern void *lgw_com_target; /*! generic pointer to the COM device */ |
dgabino | 0:102b50f941d0 | 55 | |
dgabino | 0:102b50f941d0 | 56 | pthread_mutex_t mx_usbbridgesync = PTHREAD_MUTEX_INITIALIZER; /* control access to usbbridge sync offsets */ |
dgabino | 0:102b50f941d0 | 57 | |
dgabino | 0:102b50f941d0 | 58 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 59 | /* --- PRIVATE CONSTANTS ---------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 60 | |
dgabino | 0:102b50f941d0 | 61 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 62 | /* --- PRIVATE FUNCTIONS DEFINITION ----------------------------------------- */ |
dgabino | 0:102b50f941d0 | 63 | |
dgabino | 0:102b50f941d0 | 64 | int lgw_com_send_cmd(lgw_com_cmd_t cmd, lgw_handle_t handle) { |
dgabino | 0:102b50f941d0 | 65 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 66 | return lgw_com_send_cmd_win(cmd, handle); |
dgabino | 0:102b50f941d0 | 67 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 68 | return lgw_com_send_cmd_linux(cmd, handle); |
dgabino | 0:102b50f941d0 | 69 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 70 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 71 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 72 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 73 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 74 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 75 | #else |
dgabino | 0:102b50f941d0 | 76 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 77 | #endif |
dgabino | 0:102b50f941d0 | 78 | } |
dgabino | 0:102b50f941d0 | 79 | |
dgabino | 0:102b50f941d0 | 80 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 81 | |
dgabino | 0:102b50f941d0 | 82 | int lgw_com_receive_ans(lgw_com_ans_t *ans, lgw_handle_t handle) { |
dgabino | 0:102b50f941d0 | 83 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 84 | return lgw_com_receive_ans_win(ans, handle); |
dgabino | 0:102b50f941d0 | 85 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 86 | return lgw_com_receive_ans_linux(ans, handle); |
dgabino | 0:102b50f941d0 | 87 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 88 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 89 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 90 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 91 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 92 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 93 | #else |
dgabino | 0:102b50f941d0 | 94 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 95 | #endif |
dgabino | 0:102b50f941d0 | 96 | } |
dgabino | 0:102b50f941d0 | 97 | |
dgabino | 0:102b50f941d0 | 98 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 99 | /* --- PUBLIC FUNCTIONS DEFINITION ------------------------------------------ */ |
dgabino | 0:102b50f941d0 | 100 | |
dgabino | 0:102b50f941d0 | 101 | int lgw_com_open(void **com_target_ptr, const char *com_path) { |
dgabino | 0:102b50f941d0 | 102 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 103 | return lgw_com_open_win(com_target_ptr); |
dgabino | 0:102b50f941d0 | 104 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 105 | return lgw_com_open_linux(com_target_ptr, com_path); |
dgabino | 0:102b50f941d0 | 106 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 107 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 108 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 109 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 110 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 111 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 112 | #else |
dgabino | 0:102b50f941d0 | 113 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 114 | #endif |
dgabino | 0:102b50f941d0 | 115 | } |
dgabino | 0:102b50f941d0 | 116 | |
dgabino | 0:102b50f941d0 | 117 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 118 | |
dgabino | 0:102b50f941d0 | 119 | int lgw_com_close(void *com_target) { |
dgabino | 0:102b50f941d0 | 120 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 121 | return lgw_com_close_win(com_target); |
dgabino | 0:102b50f941d0 | 122 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 123 | return lgw_com_close_linux(com_target); |
dgabino | 0:102b50f941d0 | 124 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 125 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 126 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 127 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 128 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 129 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 130 | #else |
dgabino | 0:102b50f941d0 | 131 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 132 | #endif |
dgabino | 0:102b50f941d0 | 133 | } |
dgabino | 0:102b50f941d0 | 134 | |
dgabino | 0:102b50f941d0 | 135 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 136 | |
dgabino | 0:102b50f941d0 | 137 | int lgw_com_w(void *com_target, uint8_t com_mux_mode, uint8_t com_mux_target, uint8_t address, uint8_t data) { |
dgabino | 0:102b50f941d0 | 138 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 139 | return lgw_com_w_win(com_target, com_mux_mode, com_mux_target, address, data); |
dgabino | 0:102b50f941d0 | 140 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 141 | return lgw_com_w_linux(com_target, com_mux_mode, com_mux_target, address, data); |
dgabino | 0:102b50f941d0 | 142 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 143 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 144 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 145 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 146 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 147 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 148 | #else |
dgabino | 0:102b50f941d0 | 149 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 150 | #endif |
dgabino | 0:102b50f941d0 | 151 | } |
dgabino | 0:102b50f941d0 | 152 | |
dgabino | 0:102b50f941d0 | 153 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 154 | |
dgabino | 0:102b50f941d0 | 155 | int lgw_com_r(void *com_target, uint8_t com_mux_mode, uint8_t com_mux_target, uint8_t address, uint8_t *data) { |
dgabino | 0:102b50f941d0 | 156 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 157 | return lgw_com_r_win(com_target, com_mux_mode, com_mux_target, address, data); |
dgabino | 0:102b50f941d0 | 158 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 159 | return lgw_com_r_linux(com_target, com_mux_mode, com_mux_target, address, data); |
dgabino | 0:102b50f941d0 | 160 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 161 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 162 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 163 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 164 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 165 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 166 | #else |
dgabino | 0:102b50f941d0 | 167 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 168 | #endif |
dgabino | 0:102b50f941d0 | 169 | } |
dgabino | 0:102b50f941d0 | 170 | |
dgabino | 0:102b50f941d0 | 171 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 172 | |
dgabino | 0:102b50f941d0 | 173 | int lgw_com_wb(void *com_target, uint8_t com_mux_mode, uint8_t com_mux_target, uint8_t address, uint8_t *data, uint16_t size) { |
dgabino | 0:102b50f941d0 | 174 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 175 | return lgw_com_wb_win(com_target, com_mux_mode, com_mux_target, address, data, size); |
dgabino | 0:102b50f941d0 | 176 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 177 | return lgw_com_wb_linux(com_target, com_mux_mode, com_mux_target, address, data, size); |
dgabino | 0:102b50f941d0 | 178 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 179 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 180 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 181 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 182 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 183 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 184 | #else |
dgabino | 0:102b50f941d0 | 185 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 186 | #endif |
dgabino | 0:102b50f941d0 | 187 | } |
dgabino | 0:102b50f941d0 | 188 | |
dgabino | 0:102b50f941d0 | 189 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 190 | |
dgabino | 0:102b50f941d0 | 191 | int lgw_com_rb(void *com_target, uint8_t com_mux_mode, uint8_t com_mux_target, uint8_t address, uint8_t *data, uint16_t size) { |
dgabino | 0:102b50f941d0 | 192 | #ifdef _WIN32 |
dgabino | 0:102b50f941d0 | 193 | return lgw_com_rb_win(com_target, com_mux_mode, com_mux_target, address, data, size); |
dgabino | 0:102b50f941d0 | 194 | #elif __linux__ |
dgabino | 0:102b50f941d0 | 195 | return lgw_com_rb_linux(com_target, com_mux_mode, com_mux_target, address, data, size); |
dgabino | 0:102b50f941d0 | 196 | #elif __APPLE__ |
dgabino | 0:102b50f941d0 | 197 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 198 | #elif __unix__ |
dgabino | 0:102b50f941d0 | 199 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 200 | #elif __posix__ |
dgabino | 0:102b50f941d0 | 201 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 202 | #else |
dgabino | 0:102b50f941d0 | 203 | DEBUG_PRINTF("System is not recognized."); |
dgabino | 0:102b50f941d0 | 204 | #endif |
dgabino | 0:102b50f941d0 | 205 | } |
dgabino | 0:102b50f941d0 | 206 | |
dgabino | 0:102b50f941d0 | 207 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
dgabino | 0:102b50f941d0 | 208 | |
dgabino | 0:102b50f941d0 | 209 | int lgw_com_send_command(void *com_target, lgw_com_cmd_t cmd, lgw_com_ans_t *ans) { |
dgabino | 0:102b50f941d0 | 210 | lgw_handle_t handle; |
dgabino | 0:102b50f941d0 | 211 | |
dgabino | 0:102b50f941d0 | 212 | CHECK_NULL(com_target); |
dgabino | 0:102b50f941d0 | 213 | CHECK_NULL(ans); |
dgabino | 0:102b50f941d0 | 214 | |
dgabino | 0:102b50f941d0 | 215 | handle = LGW_GET_HANDLE(com_target); |
dgabino | 0:102b50f941d0 | 216 | |
dgabino | 0:102b50f941d0 | 217 | pthread_mutex_lock(&mx_usbbridgesync); |
dgabino | 0:102b50f941d0 | 218 | |
dgabino | 0:102b50f941d0 | 219 | if (lgw_com_send_cmd(cmd, handle) == LGW_COM_ERROR) { |
dgabino | 0:102b50f941d0 | 220 | pthread_mutex_unlock(&mx_usbbridgesync); |
dgabino | 0:102b50f941d0 | 221 | return LGW_COM_ERROR; |
dgabino | 0:102b50f941d0 | 222 | } |
dgabino | 0:102b50f941d0 | 223 | if (lgw_com_receive_ans(ans, handle) == LGW_COM_ERROR) { |
dgabino | 0:102b50f941d0 | 224 | pthread_mutex_unlock(&mx_usbbridgesync); |
dgabino | 0:102b50f941d0 | 225 | return LGW_COM_ERROR; |
dgabino | 0:102b50f941d0 | 226 | } |
dgabino | 0:102b50f941d0 | 227 | |
dgabino | 0:102b50f941d0 | 228 | pthread_mutex_unlock(&mx_usbbridgesync); |
dgabino | 0:102b50f941d0 | 229 | |
dgabino | 0:102b50f941d0 | 230 | return LGW_COM_SUCCESS; |
dgabino | 0:102b50f941d0 | 231 | } |
dgabino | 0:102b50f941d0 | 232 | |
dgabino | 0:102b50f941d0 | 233 | /* --- EOF ------------------------------------------------------------------ */ |