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

nrf_socket og nrf_close

When I have opened a nrf socket with nrf_socket to extract GPS data, when the GPS acquisition phase is over, I want to close the socket, since I'm turning the GPS off. calling nrfclose with the socket handle as input parameter,, returns -1, normally an indication that somthing has gone wrong. When I try to reopen the socket again, the open socket command also fails. Please advice on how to proceed.  

Parents
  • Hello, can you please provide the code so I can debug it?

  • bool gpsPower(bool state) {
        char response[80];
        u16_t fix_retry = 0;
        int modemResult;
        u16_t fix_interval = 1;
        u16_t nmea_mask = NRF_CONFIG_NMEA_GSV_MASK |
                          NRF_CONFIG_NMEA_GSA_MASK |
                          NRF_CONFIG_NMEA_GLL_MASK |
                          NRF_CONFIG_NMEA_GGA_MASK |
                          NRF_CONFIG_NMEA_RMC_MASK;
    
        int retval = modem_at_open_socket();
        if (retval < 0) {
            printk("opening AT socket failed\n");
            return false;
        }
        modemSetState(MODEM_FLIGHT_MODE);
    
        modemResult = modem_at_command(state ? AT_GPS_ON : AT_LTE_ON, response);
        printk("Modem result %s: %d\n", state ? AT_GPS_ON : AT_LTE_ON, modemResult);
    
        modemSetState(MODEM_ON);
        setLNAState(state);
        modem_at_close_socket();
    
        if (state) {
            fd = nrf_socket(NRF_AF_LOCAL, NRF_SOCK_DGRAM, NRF_PROTO_GNSS);
    
            if (fd >= 0) {
                printk(" Socket created \n");
            } else {
                printk("Could not init socket (err: %d ) \n", fd);
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_FIX_RETRY,
                &fix_retry,
                sizeof(uint16_t));
    
            if (retval != 0) {
                printk("Failed to set fix retry value \n");
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_FIX_INTERVAL,
                &fix_interval,
                sizeof(uint16_t));
    
            if (retval != 0) {
                printk("Failed to set fix interval value \n");
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_NMEA_MASK,
                &nmea_mask,
                sizeof(uint16_t));
    
            if (retval != 0) {
                printk("Failed to set nmea mask\n");
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_START,
                NULL,
                0);
    
            if (retval != 0) {
                printk("Failed to start GPS \n");
                return false;
            }
        } else {
            modemResult = close(fd);
            printk("socket_close result: %d\n", modemResult);
        }
        printk("GNSS %s ok\n", state ? "ON" : "OFF");
        return true;
    }

    THis is my gps power on/off code. 

    It is basically built on the gps sample code. 

    THe code is running on a custom board, with a low noise amplifier. 

    The following code is not attached: 

    modemSetState(int mode) - sends the AT+CFUN=mode to the modem

    I have the following extra definitions: 

    #define AT_LTE_ON "AT\%XSYSTEMMODE=1,0,0,1"
    #define AT_GPS_ON "AT\%XSYSTEMMODE=0,0,1,0"

    Opening the socket goes well the first time around, but closing it and reopening it returns -1, indicating that something is wrong. 

    Tron

Reply
  • bool gpsPower(bool state) {
        char response[80];
        u16_t fix_retry = 0;
        int modemResult;
        u16_t fix_interval = 1;
        u16_t nmea_mask = NRF_CONFIG_NMEA_GSV_MASK |
                          NRF_CONFIG_NMEA_GSA_MASK |
                          NRF_CONFIG_NMEA_GLL_MASK |
                          NRF_CONFIG_NMEA_GGA_MASK |
                          NRF_CONFIG_NMEA_RMC_MASK;
    
        int retval = modem_at_open_socket();
        if (retval < 0) {
            printk("opening AT socket failed\n");
            return false;
        }
        modemSetState(MODEM_FLIGHT_MODE);
    
        modemResult = modem_at_command(state ? AT_GPS_ON : AT_LTE_ON, response);
        printk("Modem result %s: %d\n", state ? AT_GPS_ON : AT_LTE_ON, modemResult);
    
        modemSetState(MODEM_ON);
        setLNAState(state);
        modem_at_close_socket();
    
        if (state) {
            fd = nrf_socket(NRF_AF_LOCAL, NRF_SOCK_DGRAM, NRF_PROTO_GNSS);
    
            if (fd >= 0) {
                printk(" Socket created \n");
            } else {
                printk("Could not init socket (err: %d ) \n", fd);
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_FIX_RETRY,
                &fix_retry,
                sizeof(uint16_t));
    
            if (retval != 0) {
                printk("Failed to set fix retry value \n");
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_FIX_INTERVAL,
                &fix_interval,
                sizeof(uint16_t));
    
            if (retval != 0) {
                printk("Failed to set fix interval value \n");
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_NMEA_MASK,
                &nmea_mask,
                sizeof(uint16_t));
    
            if (retval != 0) {
                printk("Failed to set nmea mask\n");
                return false;
            }
    
            retval = nrf_setsockopt(fd,
                NRF_SOL_GNSS,
                NRF_SO_GNSS_START,
                NULL,
                0);
    
            if (retval != 0) {
                printk("Failed to start GPS \n");
                return false;
            }
        } else {
            modemResult = close(fd);
            printk("socket_close result: %d\n", modemResult);
        }
        printk("GNSS %s ok\n", state ? "ON" : "OFF");
        return true;
    }

    THis is my gps power on/off code. 

    It is basically built on the gps sample code. 

    THe code is running on a custom board, with a low noise amplifier. 

    The following code is not attached: 

    modemSetState(int mode) - sends the AT+CFUN=mode to the modem

    I have the following extra definitions: 

    #define AT_LTE_ON "AT\%XSYSTEMMODE=1,0,0,1"
    #define AT_GPS_ON "AT\%XSYSTEMMODE=0,0,1,0"

    Opening the socket goes well the first time around, but closing it and reopening it returns -1, indicating that something is wrong. 

    Tron

Children
No Data
Related