This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use socket?

<Environment>
- Windows10
- nrf v1.3.0
- modem fw 1.2.0

I'm attempting to make a socket and communicate with the modem using it.

However, sending data doesn't work out. It was working in v1.2.0. Any tip?

I don't want to use at_cmd.c becuase I need a exclusive  socket for SMS notification.

<main.c>

#include <zephyr.h>
#include <stdio.h>
#include <net/socket.h>
#include <modem/lte_lc.h>

#define BUF_LEN 128

int fd;

void main(){
    int ret;
    char at_cmd[] = "AT\n";
    char buf[BUF_LEN];
    
	/* Configure LTE-M */
    printk("LTE Link Connecting ...\n");
    ret = lte_lc_init_and_connect();
    printk("LTE Link Connected!\n");
	if (ret) {
		printk("Error: modem_configure\n");
		return;
	}

	/* SMS socket set up*/
	fd = socket(AF_LTE, 0, NPROTO_AT); /* init fd */
	if (fd == -1) {
		return;
	}

	// Send AT command => ERROR !!
	if (send(fd, at_cmd, strlen(at_cmd), 0) <= 0) {
        printk("Error: send\n");
        return;
    }

	// blocking receive
	if (recv(fd, buf, BUF_LEN, MSG_DONTWAIT) <= 0) {
        printk("Error: send\n");
        return;
    }

    printk("Socket works out!\n");
    
    return;
}

Parents Reply Children
  • Thank you for your reply! I read this document

    What is User context in sms_register_listener arguments?

  • Do you know any sample program using sms.c?

    I really don't know what arguments should be included in sms listener...

  • Can't receive SMS by this program. Any tip?

    <prj.conf>

    #
    # Copyright (c) 2019 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    # General config
    CONFIG_ASSERT=y
    
    # Network
    CONFIG_NETWORKING=y
    CONFIG_NET_NATIVE=n
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_OFFLOAD=y
    
    # SMS
    CONFIG_SMS=y
    
    # LTE link control
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_LOCK_BANDS=y
    CONFIG_LTE_LOCK_BAND_MASK="10000000"
    CONFIG_LTE_NETWORK_MODE_LTE_M=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    
    # BSD library
    CONFIG_BSD_LIBRARY=y
    
    # AT host library
    CONFIG_AT_HOST_LIBRARY=n
    CONFIG_UART_INTERRUPT_DRIVEN=y
    
    # Stacks and heaps
    CONFIG_MAIN_STACK_SIZE=3072
    CONFIG_HEAP_MEM_POOL_SIZE=16384
    
    # SEGGER RTT for Dev
    CONFIG_PRINTK=y
    CONFIG_CONSOLE=y
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_UART_CONSOLE=n
    CONFIG_RTT_CONSOLE=y
    CONFIG_USE_SEGGER_RTT=y

    <main.c>

    #include <zephyr.h>
    #include <stdio.h>
    #include <modem/sms.h>
    #include <modem/lte_lc.h>
    
    void sms_handler(struct sms_data *const data, void *context)
    {
        printk("here\n");
    }
    
    void main(){
        int ret;
        
    	/* Configure LTE-M */
        printk("LTE Link Connecting ...\n");
        ret = lte_lc_init_and_connect();
        printk("LTE Link Connected!\n");
    	if (ret) {
    		printk("Error: modem_configure\n");
    		return;
    	}
    
    	/* SMS socket set up */
    	if (sms_init() != 0) {
            printk("ERROR: sms_init\n");
    		return;
    	}
    
        /* Set up listener */
    	if (sms_register_listener(sms_handler, NULL) != 0) {
            printk("ERROR: sms_register_listener\n");
    		return;
    	}
    
        printk("SMS waiting...");
        
        while(1){}
    }

Related