24
24
#include "memfault/core/platform/device_info.h"
25
25
#include "memfault/http/http_client.h"
26
26
#include "memfault/nrfconnect_port/http.h"
27
+ #include "memfault/ports/ncs/version.h"
28
+ #include <modem/modem_info.h>
27
29
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
46
31
#if (NCS_VERSION_MAJOR == 1 ) && (NCS_VERSION_MINOR < 3 )
47
32
48
33
#include <lte_lc.h>
51
36
#include <modem_key_mgmt.h>
52
37
#include <net/bsdlib.h>
53
38
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 )
55
41
56
42
#include <modem/lte_lc.h>
57
43
#include <modem/at_cmd.h>
58
44
#include <modem/at_notif.h>
59
45
#include <modem/modem_key_mgmt.h>
60
46
47
+ // nRF Connect SDK >= 1.9
48
+ #else
49
+
50
+ #include <modem/lte_lc.h>
51
+ #include <nrf_modem_at.h>
52
+
61
53
#endif
62
54
63
55
#if (NCS_VERSION_MAJOR == 1 ) && (NCS_VERSION_MINOR <= 2 )
@@ -107,8 +99,6 @@ sMfltHttpClientConfig g_mflt_http_client_config = {
107
99
};
108
100
109
101
void memfault_platform_get_device_info (sMemfaultDeviceInfo * info ) {
110
- static bool s_init = false;
111
-
112
102
// platform specific version information
113
103
* info = (sMemfaultDeviceInfo ) {
114
104
.device_serial = s_device_serial ,
@@ -125,30 +115,46 @@ static int memfault_ncs_device_id_set(const char *device_id, size_t len) {
125
115
126
116
#endif
127
117
118
+ #if !MEMFAULT_NCS_VERSION_GT (1 , 8 )
128
119
static int query_modem (const char * cmd , char * buf , size_t buf_len ) {
129
120
enum at_cmd_state at_state ;
130
121
int ret = at_cmd_write (cmd , buf , buf_len , & at_state );
131
-
132
122
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 );
135
124
}
136
125
137
126
return ret ;
138
127
}
128
+ #endif
139
129
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 );
146
138
}
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
+ }
147
145
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 ];
150
149
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
+ }
152
158
153
159
// register the device id with memfault port so it is used for reporting
154
160
memfault_ncs_device_id_set (s_device_serial , IMEI_LEN );
@@ -169,10 +175,12 @@ void main(void) {
169
175
170
176
int err = prv_init_modem_lib ();
171
177
if (err ) {
172
- printk ("Failed to initialize bsdlib !" );
178
+ printk ("Failed to initialize modem !" );
173
179
return ;
174
180
}
175
181
182
+ // These libraries were removed in ncs 1.9
183
+ #if !MEMFAULT_NCS_VERSION_GT (1 , 8 )
176
184
err = at_cmd_init ();
177
185
if (err ) {
178
186
printk ("Failed to initialize AT commands, err %d\n" , err );
@@ -184,6 +192,7 @@ void main(void) {
184
192
printk ("Failed to initialize AT notifications, err %d\n" , err );
185
193
return ;
186
194
}
195
+ #endif
187
196
188
197
// requires AT modem interface to be up
189
198
prv_init_device_info ();
0 commit comments