/* * Copyright (c) 2019 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic */ #include <zephyr.h> #include <kernel.h> #include <drivers/gpio.h> #include <drivers/flash.h> #include <bsd.h> #include <lte_lc.h> #include <at_cmd.h> #include <at_notif.h> #include <net/bsdlib.h> #include <net/fota_download.h> #include <dfu/mcuboot.h> #include <logging/log.h> #include <sms.h> LOG_MODULE_REGISTER(main_,4); #define LED_PORT DT_ALIAS_LED0_GPIOS_CONTROLLER static struct device *gpiob; static struct gpio_callback gpio_cb; static struct k_work fota_work; /**@brief Recoverable BSD library error. */ void bsd_recoverable_error_handler(uint32_t err) { printk("bsdlib recoverable error: %u\n", err); } /**@brief Start transfer of the file. */ static void app_dfu_transfer_start(struct k_work *unused) { int retval; retval = fota_download_start(CONFIG_DOWNLOAD_HOST, CONFIG_DOWNLOAD_FILE); if (retval != 0) { /* Re-enable button callback */ gpio_pin_enable_callback(gpiob, DT_ALIAS_SW0_GPIOS_PIN); printk("fota_download_start() failed, err %d\n", retval); } } /**@brief Turn on LED0 and LED1 if CONFIG_APPLICATION_VERSION * is 2 and LED0 otherwise. */ static int led_app_version(void) { struct device *dev; dev = device_get_binding(LED_PORT); if (dev == 0) { printk("Nordic nRF GPIO driver was not found!\n"); return 1; } gpio_pin_configure(dev, DT_ALIAS_LED0_GPIOS_PIN, GPIO_DIR_OUT); gpio_pin_write(dev, DT_ALIAS_LED0_GPIOS_PIN, 1); #if CONFIG_APPLICATION_VERSION == 2 gpio_pin_configure(dev, DT_ALIAS_LED1_GPIOS_PIN, GPIO_DIR_OUT); gpio_pin_write(dev, DT_ALIAS_LED1_GPIOS_PIN, 1); #endif return 0; } void dfu_button_pressed(struct device *gpiob, struct gpio_callback *cb, u32_t pins) { k_work_submit(&fota_work); gpio_pin_disable_callback(gpiob, DT_ALIAS_SW0_GPIOS_PIN); } static int dfu_button_init(void) { int err; gpiob = device_get_binding(DT_ALIAS_SW0_GPIOS_CONTROLLER); if (gpiob == 0) { printk("Nordic nRF GPIO driver was not found!\n"); return 1; } err = gpio_pin_configure(gpiob, DT_ALIAS_SW0_GPIOS_PIN, GPIO_DIR_IN | GPIO_INT | GPIO_PUD_PULL_UP | GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW); if (err == 0) { gpio_init_callback(&gpio_cb, dfu_button_pressed, BIT(DT_ALIAS_SW0_GPIOS_PIN)); err = gpio_add_callback(gpiob, &gpio_cb); } if (err == 0) { err = gpio_pin_enable_callback(gpiob, DT_ALIAS_SW0_GPIOS_PIN); } if (err != 0) { printk("Unable to configure SW0 GPIO pin!\n"); return 1; } return 0; } void fota_dl_handler(const struct fota_download_evt *evt) { switch (evt->id) { case FOTA_DOWNLOAD_EVT_ERROR: printk("Received error from fota_download\n"); /* Fallthrough */ case FOTA_DOWNLOAD_EVT_FINISHED: /* Re-enable button callback */ gpio_pin_enable_callback(gpiob, DT_ALIAS_SW0_GPIOS_PIN); break; default: break; } } /**@brief Configures modem to provide LTE link. * * Blocks until link is successfully established. */ static void modem_configure(void) { #if defined(CONFIG_LTE_LINK_CONTROL) BUILD_ASSERT_MSG(!IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT), "This sample does not support auto init and connect"); int err; err = at_notif_init(); __ASSERT(err == 0, "AT Notify could not be initialized."); err = at_cmd_init(); __ASSERT(err == 0, "AT CMD could not be established."); printk("LTE Link Connecting ...\n"); err = lte_lc_init_and_connect(); __ASSERT(err == 0, "LTE link could not be established."); printk("LTE Link Connected!\n"); #endif } static int application_init(void) { int err; k_work_init(&fota_work, app_dfu_transfer_start); err = dfu_button_init(); if (err != 0) { return err; } err = led_app_version(); if (err != 0) { return err; } err = fota_download_init(fota_dl_handler); if (err != 0) { return err; } return 0; } void sms_callback_process(struct sms_data *const data, void *context) { if (NULL == data) return; LOG_DBG("sms: %s",data); LOG_DBG("alpha: %s",data->alpha); LOG_DBG("leng: %d",data->length); LOG_DBG("pdu: %d",data->pdu); } void sms_send_test1(void) { //Testing a sms messaging over lte at_cmd_write("AT+CMGS=42\r0011000B912103000100F500000B20D4F29C9E769F4161D0BC3D07B5CBF379F89C769F416F7B590E62D3CB\x1A",NULL,0,NULL); //Hello Nordic // at_cmd_write("AT+CMGS=24\r0001000B912103000100F500000CC8329BFD0639DF72727A0C\x1A",NULL,0,NULL); } static int test_sms = 0; static int test_sms1 = 0; void main(void) { int err; printk("Initializing bsdlib\n"); err = bsdlib_init(); switch (err) { case MODEM_DFU_RESULT_OK: printk("Modem firmware update successful!\n"); printk("Modem will run the new firmware after reboot\n"); k_thread_suspend(k_current_get()); break; case MODEM_DFU_RESULT_UUID_ERROR: case MODEM_DFU_RESULT_AUTH_ERROR: printk("Modem firmware update failed\n"); printk("Modem will run non-updated firmware on reboot.\n"); break; case MODEM_DFU_RESULT_HARDWARE_ERROR: case MODEM_DFU_RESULT_INTERNAL_ERROR: printk("Modem firmware update failed\n"); printk("Fatal error.\n"); break; case -1: printk("Could not initialize bsdlib.\n"); printk("Fatal error.\n"); return; default: break; } LOG_INF("Initialized bsdlib\n"); modem_configure(); boot_write_img_confirmed(); err = application_init(); if (err != 0) { return; } LOG_DBG("Press Button 1 to start the FOTA download\n"); sms_init(); at_cmd_write("AT+CMFG=0\r\n",NULL,0,NULL); sms_register_listener(sms_callback_process,NULL); if (err != 0) { LOG_ERR("Can't register handler rc=%d", err); } while(1) { // LOG_INF("main thread run"); if (0xff == test_sms1) { sms_send_test1(); test_sms1 = 0; } k_sleep(1000); } }
Initializing bsdlib
[00:00:00.189,239] <inf> main_: Initialized bsdlib
LTE Link Connecting ...
[00:00:00.197,082] <inf> at_cmd: AT%XSYSTEMMODE=1,0,0,0
[00:00:00.204,711] <inf> at_cmd: OK
[00:00:00.208,923] <inf> at_cmd: AT+CEREG=5
[00:00:00.220,031] <inf> at_cmd: OK
[00:00:00.224,304] <inf> at_cmd: AT%XSYSTEMMODE=1,0,0,0
[00:00:00.237,976] <inf> at_cmd: OK
[00:00:00.242,218] <inf> at_cmd: AT+CFUN=1
[00:00:00.284,851] <inf> at_cmd: OK
[00:00:05.684,600] <inf> at_cmd: +CEREG: 2,"0001","01A2D001",7,0,0,"11100000","11100000"
[00:00:06.296,142] <inf> at_cmd: +CEREG: 1,"0001","01A2D001",7,,,"11100000","11100000"
LTE Link Connected!
[00:00:06.307,098] <dbg> main_.main: Press Button 1 to start the FOTA download
[00:00:06.314,971] <inf> at_cmd: AT+CNMI?
[00:00:06.319,824] <inf> at_cmd: +CNMI: 0,0,0,0,1
OK
[00:00:06.325,744] <inf> at_cmd: AT+CNMI=3,2,0,1
[00:00:06.331,237] <inf> at_cmd: OK
[00:00:06.335,449] <inf> sms: SMS client successfully registered
0011000B912103000100F500000B20D4F29C9E769F4161D0BC3D07B5CBF379F89C769F416F7B590E62D3CB
[00:00:21.775,329] <inf> at_cmd: +CMS ERROR: 300
04800000000410D0C176589E9EBFCD7400000211010154752303ECF718
[00:07:22.368,103] <dbg> main_.sms_callback_process: alpha: 1C6785E9E9FBDC47
[00:07:22.375,701] <dbg> main_.sms_callback_process: leng: 24
[00:07:22.381,958] <dbg> main_.sms_callback_process: pdu: 537010464
[00:07:22.388,824] <inf> at_cmd: AT+CNMA=1
[00:07:22.420,776] <inf> at_cmd: OK