Hello dear People,
I would be very happy if anyone has an idea how to solve this.
The Problem is the following: I am using nordics lte link controller to connect to nb-iot(<modem/lte_lc.h>), but when opening a socket, it times out... (error 116).
Basically for this testing scenario i adapted the udp sample to make tcp connection and send/receive some data:
The code for opening the socket:
ip to local port and establish tcp-connection:
int tcp_create_conn_socket(const char * host, int port){
// conn. init: defining socket parameters
struct sockaddr_in *server4 = ((struct sockaddr_in *)&host_addr);
server4->sin_family = AF_INET;
server4->sin_port = htons(port);
inet_pton(AF_INET, host, &server4->sin_addr);
// bind server ip to local port and establish tcp-connection:
int err, sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Creation of TCP-Socket
if (sockfd < 0) {
printk("Failed to create TCP socket: %d\n", errno);
err = -errno;
goto error;
}
err = connect(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr_in)); // establishing connection
if (err < 0) {
printk("Connect failed : %d\n", errno);
goto error;
}
return sockfd;
error:
tcp_disconnect(sockfd);
return err;
}
my proj.conf file looks the following:
# # Copyright (c) 2020 Nordic Semiconductor ASA # # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause # # General config CONFIG_NEWLIB_LIBC=y CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y CONFIG_NCS_SAMPLES_DEFAULTS=y CONFIG_LOG=y CONFIG_LOG_DEFAULT_LEVEL=4 CONFIG_DEBUG_OPTIMIZATIONS=y CONFIG_LOG_PRINTK=y CONFIG_ASSERT=y CONFIG_STACK_SENTINEL=y CONFIG_LOG_PROCESS_THREAD=y CONFIG_USE_SEGGER_RTT=y CONFIG_RTT_CONSOLE=y CONFIG_UART_CONSOLE=n CONFIG_LOG_BACKEND_RTT=y CONFIG_LOG_BACKEND_UART=n # Network CONFIG_NETWORKING=y CONFIG_NET_NATIVE=n CONFIG_NET_SOCKETS=y CONFIG_NET_SOCKETS_OFFLOAD=y CONFIG_NET_SOCKETS_POSIX_NAMES=y # LTE link control CONFIG_LTE_LINK_CONTROL=y CONFIG_LTE_AUTO_INIT_AND_CONNECT=n # Modem library CONFIG_NRF_MODEM_LIB=y # Heap and stacks CONFIG_HEAP_MEM_POOL_SIZE=1024 CONFIG_MAIN_STACK_SIZE=4096 CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 # LTE parameters ## Network Mode / LTE category CONFIG_LTE_NETWORK_MODE_LTE_M=n CONFIG_LTE_NETWORK_MODE_NBIOT=y #CONFIG_LTE_NETWORK_MODE_NBIOT_GPS=y ## PSM #CONFIG_UDP_PSM_ENABLE=y #CONFIG_LTE_PSM_REQ_RPTAU="00100001" #CONFIG_LTE_PSM_REQ_RAT="00000000" ## eDRX CONFIG_UDP_EDRX_ENABLE=n CONFIG_LTE_EDRX_REQ_VALUE_NBIOT="1001" ## RAI #CONFIG_UDP_RAI_ENABLE=n #CONFIG_LTE_RAI_REQ_VALUE="4"
the debug output:
*** Booting Zephyr OS build v2.7.99-ncs1 *** [*] NIS TCP-WRAP TEST-APP lte-mode: irgend n anderer D: AT notif: +CEREG: 2,"C37C","01921F0C",9,0,0,"11100000","11100000" D: Dispatching to 0x178fd LTE cell changed: Cell ID: 26353420, Tracking area: 50044 D: AT notif: +CSCON: 1 D: Dispatching to 0x17479 RRC mode: Connected D: AT notif: +CEREG: 1,"C37C","01921F0C",9,,,"00000010","00011111" D: Dispatching to 0x178fd Network registration status: Connected - home network PSM parameter update: TAU: 18600, Active time: 4 lte-mode: nbiot-lte cesq-response: ERROR selected band: ERROR Connect failed : 116 [!] Err: Not able to initialize and establish TCP server connection D: AT notif: +CSCON: 0 D: Dispatching to 0x17479 RRC mode: Idle
my main.c:
void main(void)
{
printk("[*] NIS TCP-WRAP TEST-APP\n");
k_work_init_delayable(&server_transmission_work, transmit_msg); // init worker thread, since modem conn is not reliable from isr
if(init_lte() < 0) return;
//if(init_button() < 0) return;
//if(init_led() < 0) return;
if (ask_for_current_band() < 0) return;
sockfd = tcp_create_conn_socket("194.13.83.174", 4444);
if (sockfd < 0) {
printk("[!] Err: Not able to initialize and establish TCP server connection\n");
return;
}
printk("[*] Connected to server\n");
server_connected = true;
tcp_send_msg(sockfd, "hallo was geht?", 15);
wait_for_incoming();
tcp_disconnect(sockfd);
return;
}
the connection to the server used to work on my thingy, but on the custom devboard my employer wants me to work with there the tcp-connection always times out.
One big difference is on the thingy i was using lte-m and the devboard sim only supports nb-iot which is why I configured the project to use nb-iot only.
this is the code i use to connect to the nb-iot network:
int lte_modem_connect(){
int err;
enum lte_lc_lte_mode a;
k_work_init_delayable(<e_at_work, get_at);
if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
/* Do nothing, modem is already configured and LTE connected. */
} else {
lte_lc_lte_mode_get(&a);
switch(a){
case LTE_LC_SYSTEM_MODE_PREFER_AUTO:
printk("lte_mode: prefer auto\n");
break;
case LTE_LC_SYSTEM_MODE_PREFER_LTEM:
printk("lte_mode: prefer lte-m\n");
break;
case LTE_LC_SYSTEM_MODE_PREFER_NBIOT:
printk("lte-mode: prefer-nbiot");
break;
default:
printk("lte-mode: irgend n anderer\n");
break;
}
printk("");
err = lte_lc_connect_async(lte_handler);
if (err) {
printk("Connecting to LTE network failed, error: %d\n",
err);
return;
}
//k_msleep(500);
//k_work_schedule(<e_at_work, K_NO_WAIT);
}
k_sem_take(<e_connected, K_FOREVER);
lte_lc_lte_mode_get(&a);
switch(a){
case LTE_LC_SYSTEM_MODE_PREFER_AUTO:
printk("lte_mode: prefer auto\n");
break;
case LTE_LC_LTE_MODE_LTEM:
printk("lte_mode: prefer lte-m\n");
break;
case LTE_LC_LTE_MODE_NBIOT:
printk("lte-mode: nbiot-lte\n");
break;
default:
printk("lte-mode: irgend n anderer\n");
break;
}
}
Now when I call some at commands (using nrf_modem_at_cmd from <nrf_modem_at.h>) for example to see which band is in use/available on the modem:
int ask_for_current_band(){
char response[64];
nrf_modem_at_cmd(response, 64, "AT%CESQ=1");
printk("cesq-response: %s\n", response);
if (nrf_modem_at_cmd_async(band_at_callback, "AT%XCBAND=?")) return -1;
}
I always get an error (see debug output above), but only when using nordics proprietary at-commands (AT%<command>, like mentioned in https://infocenter.nordicsemi.com/pdf/nrf91_at_commands_v1.0.pdf). Standard AT-Commands (those with a +) seem to work fine....
Do i have to do extra configuration when I want to use the nordic proprietary at-commands like XCBAND or when I am using NB-IOT in general?
My guess so far is that the connection time out is occuring, because nordics lte link controller is eventually also not able to use those to open a socket.
Any help is appreciated and I would be very grateful.
Have a nice day folks,
Jannis
PS: my setup:
Modem firmware version 1.2.3
nrf-connect sdk version 1.9.0
Zephyr OS build v2.7.99-ncs1
Nordic nrf9160 rev2