Skip to content

Commit 0bc668c

Browse files
authored
Merge pull request #34 from tmobile/tmo-CFSPDK-722-The-tmo-sntp-command-does-not-return-the-proper-date
samples: Fix tmo shell SNTP
2 parents df4c89c + c728040 commit 0bc668c

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

samples/tmo_shell/src/tmo_shell.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,14 +1506,21 @@ SHELL_STATIC_SUBCMD_SET_CREATE(tmo_wifi_commands,
15061506

15071507
int cmd_sntp(const struct shell *shell, size_t argc, char **argv)
15081508
{
1509-
if (argc < 2) {
1509+
if (argc < 3) {
15101510
shell_error(shell, "Missing required arguments");
1511-
shell_print(shell, "Usage: tmo sntp <ip or domain> <server>");
1511+
shell_print(shell, "Usage: tmo sntp <iface> <server>");
15121512
return -EINVAL;
15131513
}
15141514

1515-
1516-
return tmo_update_time(shell, argv[1]);
1515+
int status = -1;
1516+
1517+
for (int i = 0; i < 3; i++) {
1518+
status = tmo_update_time(shell, argv[2], strtol(argv[1], NULL, 10));
1519+
if (!status || errno != -EAGAIN) {
1520+
return status;
1521+
}
1522+
}
1523+
return -1;
15171524
}
15181525
int cmd_gnss(const struct shell *shell, size_t argc, char **argv)
15191526
{

samples/tmo_shell/src/tmo_sntp.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ int isValidIpAddress(char *ipAddress)
144144
}
145145

146146

147-
int tmo_update_time(const struct shell *shell, char *host)
147+
int tmo_update_time(const struct shell *shell, char *host, int iface_idx)
148148
{
149149
#if defined(CONFIG_NET_IPV6)
150150
#ifdef DEBUG
@@ -161,14 +161,18 @@ int tmo_update_time(const struct shell *shell, char *host)
161161
// Set the first byte's bits to 00,011,011 for li = 0, vn = 3, and mode = 3. The rest will be left set to zero.
162162
*( ( char * ) &packet + 0 ) = 0x1b; // Represents 27 in base 10 or 00011011 in base 2.
163163

164-
int iface_idx = 2;
165164
struct net_if *iface = net_if_get_by_index(iface_idx);
165+
166+
if (tmo_offload_init(iface_idx)) {
167+
return -1;
168+
}
169+
166170
if (iface == NULL) {
167171
shell_error(shell, "Interface %d not found", iface_idx);
168172
return -EINVAL;
169173
}
170174

171-
int sd = zsock_socket_ext(AF_INET, SOCK_DGRAM, IPPROTO_UDP, iface );
175+
int sd = zsock_socket_ext(AF_INET, SOCK_DGRAM, IPPROTO_UDP, iface);
172176
if (sd == -1) {
173177
shell_error(shell, "Socket creation failed, errno = %d", errno);
174178
return 0;
@@ -215,12 +219,9 @@ int tmo_update_time(const struct shell *shell, char *host)
215219
while (total < recvsize || recvsize == 0) {
216220
stat = zsock_recv(sd, (( char* ) &packet) + total, recvsize - total, ZSOCK_MSG_DONTWAIT);
217221
if (stat == -1) {
218-
if ((total == 0) || (errno != EAGAIN)) {
219-
shell_error(shell, "recv failed, errno = %d", errno);
220-
zsock_close(sd);
221-
return -1;
222-
}
223-
break;
222+
shell_error(shell, "recv failed, errno = %d", errno);
223+
zsock_close(sd);
224+
return -1;
224225
}
225226
total += stat;
226227
}

samples/tmo_shell/src/tmo_sntp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ typedef struct
4848

4949
} ntp_packet; // Total: 384 bits or 48 bytes.
5050

51-
int tmo_update_time(const struct shell *shell, char * server);
51+
int tmo_update_time(const struct shell *shell, char * server, int iface_idx);
5252
#endif

0 commit comments

Comments
 (0)