Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 1 | /* Siemens BS-11 microBTS configuration tool */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This software is based on ideas (but not code) of BS11Config |
| 7 | * (C) 2009 by Dieter Spaar <spaar@mirider.augusta.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <errno.h> |
| 29 | #include <string.h> |
| 30 | #include <getopt.h> |
| 31 | #include <fcntl.h> |
Harald Welte | f186c46 | 2009-01-29 08:45:19 +0000 | [diff] [blame] | 32 | #include <termios.h> |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 33 | #include <signal.h> |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | |
| 38 | #include <openbsc/gsm_data.h> |
| 39 | #include <openbsc/abis_nm.h> |
| 40 | #include <openbsc/msgb.h> |
| 41 | #include <openbsc/tlv.h> |
| 42 | #include <openbsc/debug.h> |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 43 | #include <openbsc/select.h> |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 44 | |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 45 | /* state of our bs11_config application */ |
| 46 | enum bs11cfg_state { |
| 47 | STATE_NONE, |
| 48 | STATE_LOGON_WAIT, |
| 49 | STATE_LOGON_ACK, |
| 50 | STATE_SWLOAD, |
| 51 | }; |
| 52 | static enum bs11cfg_state bs11cfg_state = STATE_NONE; |
| 53 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 54 | static const u_int8_t obj_li_attr[] = { |
| 55 | 0xa0, 0x09, 0x00, |
| 56 | 0xab, 0x00, |
| 57 | 0xac, 0x00, |
| 58 | }; |
| 59 | static const u_int8_t obj_bbsig0_attr[] = { |
| 60 | 0x3d, 0x02, 0x00, 0x00, |
| 61 | 0x3f, 0x01, 0x00, |
| 62 | }; |
| 63 | static const u_int8_t obj_pa0_attr[] = { |
| 64 | NM_ATT_BS11_TXPWR, 0x01, BS11_TRX_POWER_30mW, |
| 65 | }; |
| 66 | static const char *trx1_password = "1111111111"; |
| 67 | #define TEI_OML 25 |
| 68 | |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 69 | static const u_int8_t too_fast[] = { 0x12, 0x80, 0x00, 0x00, 0x02, 0x02 }; |
| 70 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 71 | struct serial_handle { |
| 72 | struct bsc_fd fd; |
| 73 | struct llist_head tx_queue; |
| 74 | |
| 75 | struct msgb *rx_msg; |
| 76 | unsigned int rxmsg_bytes_missing; |
| 77 | }; |
| 78 | |
| 79 | /* FIXME: this needs to go */ |
| 80 | static struct serial_handle _ser_handle, *ser_handle = &_ser_handle; |
| 81 | |
| 82 | static int handle_serial_msg(struct msgb *rx_msg); |
| 83 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 84 | /* create all objects for an initial configuration */ |
| 85 | static int create_objects(struct gsm_bts *bts, int trx1) |
| 86 | { |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 87 | abis_nm_bs11_factory_logon(bts, 1); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 88 | abis_nm_bs11_create_object(bts, BS11_OBJ_LI, 0, sizeof(obj_li_attr), |
| 89 | obj_li_attr); |
| 90 | abis_nm_bs11_create_object(bts, BS11_OBJ_GPSU, 0, 0, NULL); |
| 91 | abis_nm_bs11_create_object(bts, BS11_OBJ_ALCO, 0, 0, NULL); |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 92 | abis_nm_bs11_create_object(bts, BS11_OBJ_CCLK, 0, 0, NULL); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 93 | abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 0, |
| 94 | sizeof(obj_bbsig0_attr), obj_bbsig0_attr); |
| 95 | abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 0, |
| 96 | sizeof(obj_pa0_attr), obj_pa0_attr); |
| 97 | if (trx1) { |
| 98 | u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12]; |
| 99 | u_int8_t *cur = bbsig1_attr; |
| 100 | |
| 101 | abis_nm_bs11_set_trx1_pw(bts, trx1_password); |
| 102 | |
| 103 | cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10, |
| 104 | (u_int8_t *)trx1_password); |
| 105 | memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr)); |
| 106 | abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1, |
| 107 | sizeof(bbsig1_attr), bbsig1_attr); |
| 108 | |
| 109 | abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1, |
| 110 | sizeof(obj_pa0_attr), obj_pa0_attr); |
| 111 | } |
| 112 | |
| 113 | abis_nm_bs11_create_envaBTSE(bts, 0); |
| 114 | abis_nm_bs11_create_envaBTSE(bts, 1); |
| 115 | abis_nm_bs11_create_envaBTSE(bts, 2); |
| 116 | abis_nm_bs11_create_envaBTSE(bts, 3); |
| 117 | |
| 118 | abis_nm_bs11_conn_oml(bts, 0, 1, 0xff); |
| 119 | abis_nm_bs11_set_oml_tei(bts, TEI_OML); |
| 120 | |
| 121 | abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_30mW); |
| 122 | |
| 123 | if (trx1) |
| 124 | abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_30mW); |
| 125 | |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 126 | abis_nm_bs11_factory_logon(bts, 0); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static char *serial_port = "/dev/ttyUSB0"; |
| 132 | static char *fname_safety = "BTSBMC76.SWI"; |
| 133 | static char *fname_software = "HS011106.SWL"; |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 134 | static int delay_ms = 0; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 135 | static int serial_fd = -1; |
| 136 | static int have_trx1 = 0; |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 137 | static int win_size = 8; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 138 | static struct gsm_bts *g_bts; |
| 139 | |
| 140 | /* adaption layer from GSM 08.59 + 12.21 to RS232 */ |
| 141 | |
| 142 | #define LAPD_HDR_LEN 10 |
| 143 | |
| 144 | /* callback from abis_nm */ |
| 145 | int _abis_nm_sendmsg(struct msgb *msg) |
| 146 | { |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 147 | struct serial_handle *sh = ser_handle; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 148 | u_int8_t *lapd; |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 149 | unsigned int len; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 150 | |
| 151 | msg->l2h = msg->data; |
| 152 | |
| 153 | /* prepend LAPD header */ |
| 154 | lapd = msgb_push(msg, LAPD_HDR_LEN); |
| 155 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 156 | len = msg->len - 2; |
| 157 | |
| 158 | lapd[0] = (len >> 8) & 0xff; |
| 159 | lapd[1] = len & 0xff; /* length of bytes startign at lapd[2] */ |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 160 | lapd[2] = 0x00; |
| 161 | lapd[3] = 0x07; |
| 162 | lapd[4] = 0x01; |
| 163 | lapd[5] = 0x3e; |
| 164 | lapd[6] = 0x00; |
| 165 | lapd[7] = 0x00; |
| 166 | lapd[8] = msg->len - 10; /* length of bytes starting at lapd[10] */ |
| 167 | lapd[9] = lapd[8] ^ 0x38; |
| 168 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 169 | msgb_enqueue(&sh->tx_queue, msg); |
| 170 | sh->fd.when |= BSC_FD_WRITE; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int handle_ser_write(struct bsc_fd *bfd) |
| 176 | { |
| 177 | struct serial_handle *sh = bfd->data; |
| 178 | struct msgb *msg; |
| 179 | int written; |
| 180 | |
| 181 | msg = msgb_dequeue(&sh->tx_queue); |
| 182 | if (!msg) { |
| 183 | bfd->when &= ~BSC_FD_WRITE; |
| 184 | return 0; |
| 185 | } |
| 186 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 187 | fprintf(stdout, "TX: "); |
| 188 | hexdump(msg->data, msg->len); |
| 189 | |
| 190 | /* send over serial line */ |
| 191 | written = write(serial_fd, msg->data, msg->len); |
| 192 | if (written < msg->len) { |
| 193 | perror("short write:"); |
| 194 | msgb_free(msg); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | msgb_free(msg); |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 199 | usleep(delay_ms*1000); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | #define SERIAL_ALLOC_SIZE 300 |
| 205 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 206 | static int handle_ser_read(struct bsc_fd *bfd) |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 207 | { |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 208 | struct serial_handle *sh = bfd->data; |
| 209 | struct msgb *msg; |
| 210 | int rc = 0; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 211 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 212 | if (!sh->rx_msg) { |
| 213 | sh->rx_msg = msgb_alloc(SERIAL_ALLOC_SIZE); |
| 214 | sh->rx_msg->l2h = NULL; |
| 215 | } |
| 216 | msg = sh->rx_msg; |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 217 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 218 | /* first read two byes to obtain length */ |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 219 | if (msg->len < 2) { |
| 220 | rc = read(sh->fd.fd, msg->tail, 2 - msg->len); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 221 | if (rc < 0) { |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 222 | perror("ERROR reading from serial port"); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 223 | msgb_free(msg); |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 224 | return rc; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 225 | } |
| 226 | msgb_put(msg, rc); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 227 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 228 | if (msg->len >= 2) { |
| 229 | /* parse LAPD payload length */ |
| 230 | if (msg->data[0] != 0) |
| 231 | fprintf(stderr, "Suspicious header byte 0: 0x%02x\n", |
| 232 | msg->data[0]); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 233 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 234 | sh->rxmsg_bytes_missing = msg->data[0] << 8; |
| 235 | sh->rxmsg_bytes_missing += msg->data[1]; |
| 236 | |
| 237 | if (sh->rxmsg_bytes_missing < LAPD_HDR_LEN -2) |
| 238 | fprintf(stderr, "Invalid length in hdr: %u\n", |
| 239 | sh->rxmsg_bytes_missing); |
| 240 | } |
| 241 | } else { |
| 242 | /* try to read as many of the missing bytes as are available */ |
| 243 | rc = read(sh->fd.fd, msg->tail, sh->rxmsg_bytes_missing); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 244 | if (rc < 0) { |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 245 | perror("ERROR reading from serial port"); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 246 | msgb_free(msg); |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 247 | return rc; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 248 | } |
| 249 | msgb_put(msg, rc); |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 250 | sh->rxmsg_bytes_missing -= rc; |
| 251 | |
| 252 | if (sh->rxmsg_bytes_missing == 0) { |
| 253 | /* we have one complete message now */ |
| 254 | sh->rx_msg = NULL; |
| 255 | |
| 256 | if (msg->len > LAPD_HDR_LEN) |
| 257 | msg->l2h = msg->data + LAPD_HDR_LEN; |
| 258 | |
| 259 | fprintf(stdout, "RX: "); |
| 260 | hexdump(msg->data, msg->len); |
| 261 | rc = handle_serial_msg(msg); |
| 262 | } |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 265 | return rc; |
| 266 | } |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 267 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 268 | static int serial_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 269 | { |
| 270 | int rc = 0; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 271 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 272 | if (what & BSC_FD_READ) |
| 273 | rc = handle_ser_read(bfd); |
| 274 | |
| 275 | if (rc < 0) |
| 276 | return rc; |
| 277 | |
| 278 | if (what & BSC_FD_WRITE) |
| 279 | rc = handle_ser_write(bfd); |
| 280 | |
| 281 | return rc; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static int file_is_readable(const char *fname) |
| 285 | { |
| 286 | int rc; |
| 287 | struct stat st; |
| 288 | |
| 289 | rc = stat(fname, &st); |
| 290 | if (rc < 0) |
| 291 | return 0; |
| 292 | |
| 293 | if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR)) |
| 294 | return 1; |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | static int handle_state_resp(u_int8_t state) |
| 301 | { |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 302 | int rc = 0; |
| 303 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 304 | printf("STATE: "); |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 305 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 306 | switch (state) { |
| 307 | case BS11_STATE_WARM_UP: |
| 308 | printf("Warm Up...\n"); |
| 309 | sleep(5); |
| 310 | break; |
| 311 | case BS11_STATE_LOAD_SMU_SAFETY: |
| 312 | printf("Load SMU Safety...\n"); |
| 313 | sleep(5); |
| 314 | break; |
| 315 | case BS11_STATE_SOFTWARE_RQD: |
| 316 | printf("Software required...\n"); |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 317 | bs11cfg_state = STATE_SWLOAD; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 318 | /* send safety load */ |
| 319 | if (file_is_readable(fname_safety)) |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 320 | rc = abis_nm_software_load(g_bts, fname_safety, |
| 321 | win_size); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 322 | else |
Harald Welte | 071f34d | 2009-01-29 09:24:38 +0000 | [diff] [blame] | 323 | fprintf(stderr, "No valid Safety Load file \"%s\"\n", |
| 324 | fname_safety); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 325 | break; |
| 326 | case BS11_STATE_WAIT_MIN_CFG: |
| 327 | case BS11_STATE_WAIT_MIN_CFG_2: |
| 328 | printf("Wait minimal config...\n"); |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 329 | bs11cfg_state = STATE_SWLOAD; |
| 330 | rc = create_objects(g_bts, have_trx1); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 331 | break; |
| 332 | case BS11_STATE_MAINTENANCE: |
| 333 | printf("Maintenance...\n"); |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 334 | bs11cfg_state = STATE_SWLOAD; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 335 | /* send software (FIXME: over A-bis?) */ |
| 336 | if (file_is_readable(fname_software)) |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 337 | rc = abis_nm_software_load(g_bts, fname_software, |
| 338 | win_size); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 339 | else |
Harald Welte | 071f34d | 2009-01-29 09:24:38 +0000 | [diff] [blame] | 340 | fprintf(stderr, "No valid Software file \"%s\"\n", |
| 341 | fname_software); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 342 | break; |
| 343 | case BS11_STATE_NORMAL: |
| 344 | printf("Normal...\n"); |
| 345 | return 1; |
| 346 | default: |
| 347 | printf("Unknown state 0x%02u\n", state); |
| 348 | sleep(5); |
| 349 | break; |
| 350 | } |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 351 | return rc; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 354 | static int handle_serial_msg(struct msgb *rx_msg) |
| 355 | { |
| 356 | struct abis_om_hdr *oh; |
| 357 | struct abis_om_fom_hdr *foh; |
| 358 | int rc = -1; |
| 359 | |
| 360 | if (rx_msg->len < LAPD_HDR_LEN |
| 361 | + sizeof(struct abis_om_fom_hdr) |
| 362 | + sizeof(struct abis_om_hdr)) { |
| 363 | if (!memcmp(rx_msg->data + 2, too_fast, |
| 364 | sizeof(too_fast))) { |
| 365 | fprintf(stderr, "BS11 tells us we're too " |
| 366 | "fast, try --delay bigger than %u\n", |
| 367 | delay_ms); |
| 368 | return -E2BIG; |
| 369 | } else |
| 370 | fprintf(stderr, "unknown BS11 message\n"); |
| 371 | } |
| 372 | |
| 373 | oh = (struct abis_om_hdr *) msgb_l2(rx_msg); |
| 374 | foh = (struct abis_om_fom_hdr *) oh->data; |
| 375 | switch (foh->msg_type) { |
| 376 | case NM_MT_BS11_FACTORY_LOGON_ACK: |
| 377 | printf("FACTORY LOGON: ACK\n"); |
| 378 | if (bs11cfg_state == STATE_NONE) |
| 379 | bs11cfg_state = STATE_LOGON_ACK; |
| 380 | rc = 0; |
| 381 | break; |
| 382 | case NM_MT_BS11_GET_STATE_ACK: |
| 383 | rc = handle_state_resp(foh->data[2]); |
| 384 | break; |
| 385 | default: |
| 386 | rc = abis_nm_rcvmsg(rx_msg); |
| 387 | } |
| 388 | if (rc < 0) { |
| 389 | perror("ERROR in main loop"); |
| 390 | //break; |
| 391 | } |
| 392 | if (rc == 1) |
| 393 | return rc; |
| 394 | |
| 395 | switch (bs11cfg_state) { |
| 396 | case STATE_NONE: |
| 397 | abis_nm_bs11_factory_logon(g_bts, 1); |
| 398 | break; |
| 399 | case STATE_LOGON_ACK: |
| 400 | abis_nm_bs11_get_state(g_bts); |
| 401 | break; |
| 402 | default: |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | return rc; |
| 407 | } |
| 408 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 409 | static void print_banner(void) |
| 410 | { |
| 411 | printf("bs11_config (C) 2009 by Harald Welte and Dieter Spaar\n"); |
| 412 | printf("THIS SOFTWARE IS FREE SOFTWARE WIH NO WARRANTY\n\n"); |
| 413 | } |
| 414 | |
| 415 | static void print_help(void) |
| 416 | { |
| 417 | printf("Supported arguments:\n"); |
| 418 | printf("\t--help\t\t\t-h\tPrint this help text\n"); |
| 419 | printf("\t--port /dev/ttyXXX\t-p\tSpecify serial port\n"); |
| 420 | printf("\t--with-trx1\t\t-t\tAssume the BS-11 has 2 TRX\n"); |
| 421 | printf("\t--software file\t\t-s\tSpecify Software file\n"); |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 422 | printf("\t--safety file\t\t-S\tSpecify Safety Load file\n"); |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 423 | printf("\t--delay file\t\t-d\tSpecify delay\n"); |
| 424 | printf("\t--win-size num\t\t-w\tSpecify Window Size\n"); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void handle_options(int argc, char **argv) |
| 428 | { |
| 429 | print_banner(); |
| 430 | |
| 431 | while (1) { |
| 432 | int option_index = 0, c; |
| 433 | static struct option long_options[] = { |
| 434 | { "help", 0, 0, 'h' }, |
| 435 | { "port", 1, 0, 'p' }, |
| 436 | { "with-trx1", 0, 0, 't' }, |
| 437 | { "software", 1, 0, 's' }, |
| 438 | { "safety", 1, 0, 'S' }, |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 439 | { "delay", 1, 0, 'd' }, |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 440 | { "win-size", 1, 0, 'w' }, |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 443 | c = getopt_long(argc, argv, "hp:s:S:td:w:", |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 444 | long_options, &option_index); |
| 445 | |
| 446 | if (c == -1) |
| 447 | break; |
| 448 | |
| 449 | switch (c) { |
| 450 | case 'h': |
| 451 | print_help(); |
| 452 | exit(0); |
| 453 | case 'p': |
| 454 | serial_port = optarg; |
| 455 | break; |
| 456 | case 't': |
| 457 | have_trx1 = 1; |
| 458 | break; |
| 459 | case 's': |
| 460 | fname_software = optarg; |
| 461 | break; |
| 462 | case 'S': |
| 463 | fname_safety = optarg; |
| 464 | break; |
Harald Welte | 3b8ba21 | 2009-01-29 12:27:58 +0000 | [diff] [blame] | 465 | case 'd': |
| 466 | delay_ms = atoi(optarg); |
| 467 | break; |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 468 | case 'w': |
| 469 | win_size = atoi(optarg); |
| 470 | break; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 471 | default: |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 477 | static void signal_handler(int signal) |
| 478 | { |
| 479 | fprintf(stdout, "signal %u received\n", signal); |
| 480 | |
| 481 | switch (signal) { |
| 482 | case SIGABRT: |
| 483 | abis_nm_bs11_factory_logon(g_bts, 0); |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 488 | int main(int argc, char **argv) |
| 489 | { |
| 490 | struct gsm_network *gsmnet; |
Harald Welte | f186c46 | 2009-01-29 08:45:19 +0000 | [diff] [blame] | 491 | struct termios tio; |
| 492 | int rc; |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 493 | |
| 494 | handle_options(argc, argv); |
| 495 | |
| 496 | serial_fd = open(serial_port, O_RDWR); |
| 497 | if (serial_fd < 0) { |
| 498 | perror("cannot open serial port:"); |
| 499 | exit(1); |
| 500 | } |
| 501 | |
Harald Welte | f186c46 | 2009-01-29 08:45:19 +0000 | [diff] [blame] | 502 | /* set baudrate */ |
| 503 | rc = tcgetattr(serial_fd, &tio); |
| 504 | if (rc < 0) { |
| 505 | perror("tcgetattr()"); |
| 506 | exit(1); |
| 507 | } |
| 508 | cfsetispeed(&tio, B19200); |
| 509 | cfsetospeed(&tio, B19200); |
Harald Welte | 071f34d | 2009-01-29 09:24:38 +0000 | [diff] [blame] | 510 | tio.c_cflag |= (CREAD | CLOCAL | CS8); |
| 511 | tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 512 | tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 513 | tio.c_iflag |= (INPCK | ISTRIP); |
| 514 | tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR); |
Harald Welte | 623d531 | 2009-01-29 21:14:05 +0000 | [diff] [blame^] | 515 | tio.c_oflag &= ~(OPOST); |
Harald Welte | f186c46 | 2009-01-29 08:45:19 +0000 | [diff] [blame] | 516 | rc = tcsetattr(serial_fd, TCSADRAIN, &tio); |
| 517 | if (rc < 0) { |
| 518 | perror("tcsetattr()"); |
| 519 | exit(1); |
| 520 | } |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 521 | |
| 522 | gsmnet = gsm_network_init(1, 1, 1); |
| 523 | if (!gsmnet) { |
| 524 | fprintf(stderr, "Unable to allocate gsm network\n"); |
| 525 | exit(1); |
| 526 | } |
| 527 | g_bts = &gsmnet->bts[0]; |
| 528 | |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 529 | INIT_LLIST_HEAD(&ser_handle->tx_queue); |
| 530 | ser_handle->fd.fd = serial_fd; |
| 531 | ser_handle->fd.when = BSC_FD_READ; |
| 532 | ser_handle->fd.cb = serial_fd_cb; |
| 533 | ser_handle->fd.data = ser_handle; |
| 534 | rc = bsc_register_fd(&ser_handle->fd); |
| 535 | if (rc < 0) { |
| 536 | fprintf(stderr, "could not register FD: %s\n", |
| 537 | strerror(rc)); |
| 538 | exit(1); |
| 539 | } |
| 540 | |
| 541 | signal(SIGABRT, &signal_handler); |
| 542 | |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 543 | abis_nm_bs11_factory_logon(g_bts, 1); |
| 544 | |
| 545 | while (1) { |
Harald Welte | 14f0934 | 2009-01-29 19:28:38 +0000 | [diff] [blame] | 546 | bsc_select_main(); |
Harald Welte | 61e42ad | 2009-01-18 19:10:46 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | abis_nm_bs11_factory_logon(g_bts, 0); |
| 550 | |
| 551 | close(serial_fd); |
| 552 | exit(0); |
| 553 | } |