2424#include "memfault/core/platform/device_info.h"
2525#include "memfault/http/http_client.h"
2626#include "memfault/nrfconnect_port/http.h"
27+ #include "memfault/ports/ncs/version.h"
28+ #include <modem/modem_info.h>
2729
28- // NCS Version was introduced in nRF SDK >= 1.4
29- #if __has_include ("ncs_version.h" )
30-
31- #include "ncs_version.h"
32-
33- #elif __has_include("modem/bsdlib.h")
34-
35- #define NCS_VERSION_MAJOR 1
36- #define NCS_VERSION_MINOR 3
37-
38- #else
39-
40- // A nrf connect sdk version < 1.3
41- #define NCS_VERSION_MAJOR 1
42- #define NCS_VERSION_MINOR 0
43-
44- #endif
45-
30+ // nRF Connect SDK < 1.3
4631#if (NCS_VERSION_MAJOR == 1 ) && (NCS_VERSION_MINOR < 3 )
4732
4833#include <lte_lc.h>
5136#include <modem_key_mgmt.h>
5237#include <net/bsdlib.h>
5338
54- #else /* nRF Connect SDK >= 1.3 */
39+ // nRF Connect SDK < 1.9
40+ #elif (NCS_VERSION_MAJOR == 1 ) && (NCS_VERSION_MINOR < 9 ) && (NCS_PATCHLEVEL < 99 )
5541
5642#include <modem/lte_lc.h>
5743#include <modem/at_cmd.h>
5844#include <modem/at_notif.h>
5945#include <modem/modem_key_mgmt.h>
6046
47+ // nRF Connect SDK >= 1.9
48+ #else
49+
50+ #include <modem/lte_lc.h>
51+ #include <nrf_modem_at.h>
52+
6153#endif
6254
6355#if (NCS_VERSION_MAJOR == 1 ) && (NCS_VERSION_MINOR <= 2 )
@@ -107,8 +99,6 @@ sMfltHttpClientConfig g_mflt_http_client_config = {
10799};
108100
109101void memfault_platform_get_device_info (sMemfaultDeviceInfo * info ) {
110- static bool s_init = false;
111-
112102 // platform specific version information
113103 * info = (sMemfaultDeviceInfo ) {
114104 .device_serial = s_device_serial ,
@@ -125,30 +115,46 @@ static int memfault_ncs_device_id_set(const char *device_id, size_t len) {
125115
126116#endif
127117
118+ #if !MEMFAULT_NCS_VERSION_GT (1 , 8 )
128119static int query_modem (const char * cmd , char * buf , size_t buf_len ) {
129120 enum at_cmd_state at_state ;
130121 int ret = at_cmd_write (cmd , buf , buf_len , & at_state );
131-
132122 if (ret != 0 ) {
133- printk ("at_cmd_write [%s] error:%d, at_state: %d" ,
134- cmd , ret , at_state );
123+ printk ("at_cmd_write [%s] error: %d, at_state: %d\n" , cmd , ret , at_state );
135124 }
136125
137126 return ret ;
138127}
128+ #endif
139129
140- static void prv_init_device_info (void ) {
141- // we'll use the IMEI as the device serial
142- char imei_buf [IMEI_LEN + 2 /* for \r\n */ + 1 /* \0 */ ];
143- if (query_modem ("AT+CGSN" , imei_buf , sizeof (imei_buf )) != 0 ) {
144- strcat (s_device_serial , "Unknown" );
145- return ;
130+ static int prv_get_imei (char * buf , size_t buf_len ) {
131+ #if MEMFAULT_NCS_VERSION_GT (1 , 8 )
132+ // use the cached modem info to fetch the IMEI
133+ int err = modem_info_init ();
134+ if (err != 0 ) {
135+ printk ("Modem info Init error: %d\n" , err );
136+ } else {
137+ modem_info_string_get (MODEM_INFO_IMEI , buf , buf_len );
146138 }
139+ return err ;
140+ #else
141+ // use an AT command to read IMEI
142+ return query_modem ("AT+CGSN" , buf , buf_len );
143+ #endif
144+ }
147145
148- imei_buf [IMEI_LEN ] = '\0' ;
149- strcat (s_device_serial , imei_buf );
146+ static void prv_init_device_info (void ) {
147+ // we'll use the IMEI as the device serial
148+ char modem_info [MODEM_INFO_MAX_RESPONSE_SIZE ];
150149
151- printk ("Device Serial: %s\n" , s_device_serial );
150+ int ret = prv_get_imei (modem_info , sizeof (modem_info ));
151+ if (ret != 0 ) {
152+ printk ("Failed to get IMEI\n\r" );
153+ } else {
154+ modem_info [IMEI_LEN ] = '\0' ;
155+ strcpy (s_device_serial , modem_info );
156+ printk ("IMEI: %s\n" , s_device_serial );
157+ }
152158
153159 // register the device id with memfault port so it is used for reporting
154160 memfault_ncs_device_id_set (s_device_serial , IMEI_LEN );
@@ -169,10 +175,12 @@ void main(void) {
169175
170176 int err = prv_init_modem_lib ();
171177 if (err ) {
172- printk ("Failed to initialize bsdlib !" );
178+ printk ("Failed to initialize modem !" );
173179 return ;
174180 }
175181
182+ // These libraries were removed in ncs 1.9
183+ #if !MEMFAULT_NCS_VERSION_GT (1 , 8 )
176184 err = at_cmd_init ();
177185 if (err ) {
178186 printk ("Failed to initialize AT commands, err %d\n" , err );
@@ -184,6 +192,7 @@ void main(void) {
184192 printk ("Failed to initialize AT notifications, err %d\n" , err );
185193 return ;
186194 }
195+ #endif
187196
188197 // requires AT modem interface to be up
189198 prv_init_device_info ();
0 commit comments