@@ -19,13 +19,41 @@ FAKE_VALUE_FUNC(int, nrf_cloud_coap_connect, const char * const);
19
19
FAKE_VALUE_FUNC (int , nrf_cloud_coap_disconnect );
20
20
FAKE_VALUE_FUNC (int , nrf_cloud_coap_shadow_device_status_update );
21
21
FAKE_VALUE_FUNC (int , nrf_cloud_coap_bytes_send , uint8_t * , size_t , bool );
22
+ FAKE_VALUE_FUNC (int , nrf_cloud_coap_sensor_send , const char * , double , int64_t , bool );
23
+ FAKE_VALUE_FUNC (int , nrf_cloud_coap_json_message_send , const char * , bool , bool );
24
+
25
+ /* Forward declarations */
26
+ static void dummy_cb (const struct zbus_channel * chan );
27
+ static void cloud_chan_cb (const struct zbus_channel * chan );
28
+ static void error_cb (const struct zbus_channel * chan );
29
+
30
+ /* Define unused subscribers */
31
+ ZBUS_SUBSCRIBER_DEFINE (app , 1 );
32
+ ZBUS_SUBSCRIBER_DEFINE (battery , 1 );
33
+ ZBUS_SUBSCRIBER_DEFINE (environmental , 1 );
34
+ ZBUS_SUBSCRIBER_DEFINE (fota , 1 );
35
+ ZBUS_SUBSCRIBER_DEFINE (led , 1 );
36
+ ZBUS_SUBSCRIBER_DEFINE (location , 1 );
37
+ ZBUS_LISTENER_DEFINE (trigger , dummy_cb );
38
+ ZBUS_LISTENER_DEFINE (cloud , cloud_chan_cb );
39
+ ZBUS_LISTENER_DEFINE (error , error_cb );
40
+
41
+ #define FAKE_DEVICE_ID "test_device"
22
42
23
43
static K_SEM_DEFINE (cloud_disconnected , 0 , 1 ) ;
24
44
static K_SEM_DEFINE (cloud_connected_ready , 0 , 1 ) ;
25
45
static K_SEM_DEFINE (cloud_connected_paused , 0 , 1 ) ;
26
46
static K_SEM_DEFINE (data_sent , 0 , 1 ) ;
27
47
static K_SEM_DEFINE (fatal_error_received , 0 , 1 ) ;
28
48
49
+ static int nrf_cloud_client_id_get_custom_fake (char * buf , size_t len )
50
+ {
51
+ TEST_ASSERT (len >= sizeof (FAKE_DEVICE_ID ));
52
+ memcpy (buf , FAKE_DEVICE_ID , sizeof (FAKE_DEVICE_ID ));
53
+
54
+ return 0 ;
55
+ }
56
+
29
57
static void dummy_cb (const struct zbus_channel * chan )
30
58
{
31
59
ARG_UNUSED (chan );
@@ -57,24 +85,18 @@ static void error_cb(const struct zbus_channel *chan)
57
85
}
58
86
}
59
87
60
- /* Define unused subscribers */
61
- ZBUS_SUBSCRIBER_DEFINE (app , 1 );
62
- ZBUS_SUBSCRIBER_DEFINE (battery , 1 );
63
- ZBUS_SUBSCRIBER_DEFINE (environmental , 1 );
64
- ZBUS_SUBSCRIBER_DEFINE (fota , 1 );
65
- ZBUS_SUBSCRIBER_DEFINE (led , 1 );
66
- ZBUS_SUBSCRIBER_DEFINE (location , 1 );
67
- ZBUS_LISTENER_DEFINE (trigger , dummy_cb );
68
- ZBUS_LISTENER_DEFINE (cloud , cloud_chan_cb );
69
- ZBUS_LISTENER_DEFINE (error , error_cb );
70
-
71
88
void setUp (void )
72
89
{
73
90
const struct zbus_channel * chan ;
74
91
75
92
/* Reset fakes */
76
93
RESET_FAKE (task_wdt_feed );
77
94
RESET_FAKE (task_wdt_add );
95
+ RESET_FAKE (nrf_cloud_client_id_get );
96
+ RESET_FAKE (nrf_cloud_coap_json_message_send );
97
+ RESET_FAKE (nrf_cloud_coap_connect );
98
+
99
+ nrf_cloud_client_id_get_fake .custom_fake = nrf_cloud_client_id_get_custom_fake ;
78
100
79
101
/* Clear all channels */
80
102
zbus_sub_wait (& location , & chan , K_NO_WAIT );
@@ -95,6 +117,36 @@ void test_initial_transition_to_disconnected(void)
95
117
TEST_ASSERT_EQUAL (0 , err );
96
118
}
97
119
120
+ void test_connecting_backoff (void )
121
+ {
122
+ int err ;
123
+ struct network_msg msg = { .type = NETWORK_CONNECTED , };
124
+ uint64_t connect_start_time , connect_duration_sec ;
125
+
126
+ nrf_cloud_coap_connect_fake .return_val = - EAGAIN ;
127
+ connect_start_time = k_uptime_get ();
128
+
129
+ msg .type = NETWORK_CONNECTED ;
130
+
131
+ err = zbus_chan_pub (& NETWORK_CHAN , & msg , K_SECONDS (1 ));
132
+ TEST_ASSERT_EQUAL (0 , err );
133
+
134
+ /* Transport module needs CPU to run state machine */
135
+ k_sleep (K_MSEC (10 ));
136
+
137
+ err = k_sem_take (& cloud_connected_ready , K_SECONDS (60 ));
138
+ TEST_ASSERT_EQUAL (- EAGAIN , err );
139
+
140
+ connect_duration_sec = k_uptime_delta (& connect_start_time ) / MSEC_PER_SEC ;
141
+
142
+ /* Check that the connection attempt took at least
143
+ * CONFIG_APP_TRANSPORT_BACKOFF_INITIAL_SECONDS
144
+ */
145
+
146
+ TEST_ASSERT_GREATER_OR_EQUAL (CONFIG_APP_TRANSPORT_BACKOFF_INITIAL_SECONDS ,
147
+ connect_duration_sec );
148
+ }
149
+
98
150
void test_transition_disconnected_connected_ready (void )
99
151
{
100
152
int err ;
@@ -108,20 +160,23 @@ void test_transition_disconnected_connected_ready(void)
108
160
109
161
void test_sending_payload (void )
110
162
{
163
+ int err ;
111
164
struct payload payload = {
112
- .buffer = "test" ,
113
- .buffer_len = sizeof (payload .buffer ) - 1 ,
165
+ .buffer = "{\" test\": 1} " ,
166
+ .buffer_len = strlen (payload .buffer ),
114
167
};
115
168
116
- zbus_chan_pub (& PAYLOAD_CHAN , & payload , K_NO_WAIT );
169
+ err = zbus_chan_pub (& PAYLOAD_CHAN , & payload , K_SECONDS (1 ));
170
+ TEST_ASSERT_EQUAL (0 , err );
117
171
118
172
/* Transport module needs CPU to run state machine */
119
- k_sleep (K_MSEC (100 ));
173
+ k_sleep (K_MSEC (10 ));
120
174
121
- TEST_ASSERT_EQUAL (1 , nrf_cloud_coap_bytes_send_fake .call_count );
122
- TEST_ASSERT_EQUAL (0 , strncmp (nrf_cloud_coap_bytes_send_fake .arg0_val ,
175
+ TEST_ASSERT_EQUAL (1 , nrf_cloud_coap_json_message_send_fake .call_count );
176
+ TEST_ASSERT_EQUAL (0 , strncmp (nrf_cloud_coap_json_message_send_fake .arg0_val ,
123
177
payload .buffer , payload .buffer_len ));
124
- TEST_ASSERT_EQUAL (payload .buffer_len , nrf_cloud_coap_bytes_send_fake .arg1_val );
178
+ TEST_ASSERT_EQUAL (false, nrf_cloud_coap_json_message_send_fake .arg1_val );
179
+ TEST_ASSERT_EQUAL (false, nrf_cloud_coap_json_message_send_fake .arg2_val );
125
180
}
126
181
127
182
void test_connected_ready_to_paused (void )
@@ -143,30 +198,33 @@ void test_connected_paused_to_ready_send_payload(void)
143
198
int err ;
144
199
enum network_msg_type status = NETWORK_CONNECTED ;
145
200
struct payload payload = {
146
- .buffer = "Another test" ,
147
- .buffer_len = sizeof (payload .buffer ) - 1 ,
201
+ .buffer = "{\" Another\": \" test\"} " ,
202
+ .buffer_len = strlen (payload .buffer ),
148
203
};
149
204
150
205
/* Reset call count */
151
206
nrf_cloud_coap_bytes_send_fake .call_count = 0 ;
152
207
153
- zbus_chan_pub (& NETWORK_CHAN , & status , K_NO_WAIT );
208
+ err = zbus_chan_pub (& NETWORK_CHAN , & status , K_NO_WAIT );
209
+ TEST_ASSERT_EQUAL (0 , err );
154
210
155
211
/* Transport module needs CPU to run state machine */
156
- k_sleep (K_MSEC (100 ));
212
+ k_sleep (K_MSEC (10 ));
157
213
158
214
err = k_sem_take (& cloud_connected_ready , K_SECONDS (1 ));
159
215
TEST_ASSERT_EQUAL (0 , err );
160
216
161
- zbus_chan_pub (& PAYLOAD_CHAN , & payload , K_NO_WAIT );
217
+ err = zbus_chan_pub (& PAYLOAD_CHAN , & payload , K_NO_WAIT );
218
+ TEST_ASSERT_EQUAL (0 , err );
162
219
163
220
/* Transport module needs CPU to run state machine */
164
- k_sleep (K_MSEC (100 ));
221
+ k_sleep (K_MSEC (10 ));
165
222
166
- TEST_ASSERT_EQUAL (1 , nrf_cloud_coap_bytes_send_fake .call_count );
167
- TEST_ASSERT_EQUAL (0 , strncmp (nrf_cloud_coap_bytes_send_fake .arg0_val ,
223
+ TEST_ASSERT_EQUAL (1 , nrf_cloud_coap_json_message_send_fake .call_count );
224
+ TEST_ASSERT_EQUAL (0 , strncmp (nrf_cloud_coap_json_message_send_fake .arg0_val ,
168
225
payload .buffer , payload .buffer_len ));
169
- TEST_ASSERT_EQUAL (payload .buffer_len , nrf_cloud_coap_bytes_send_fake .arg1_val );
226
+ TEST_ASSERT_EQUAL (false, nrf_cloud_coap_json_message_send_fake .arg1_val );
227
+ TEST_ASSERT_EQUAL (false, nrf_cloud_coap_json_message_send_fake .arg2_val );
170
228
}
171
229
172
230
/* This is required to be added to each test. That is because unity's
0 commit comments