Skip to content

Commit

Permalink
Merge pull request #153 from pcppcp/sntp
Browse files Browse the repository at this point in the history
Add sntp sync shell command
  • Loading branch information
pcppcp authored Nov 30, 2018
2 parents 2ba4b9c + 7d4ed5e commit 3bde08e
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#set(BUILD_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install CACHE STRING "install destination for 3rd party libs and plugins")
set(BUILD_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install CACHE STRING "")
set(WALLET_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR} CACHE STRING "")
if ((${CMAKE_BUILD_TYPE} MATCHES "Debug"))
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
endif()

include(cmake/solc.cmake)
if(BUILD_XCOMPILE)
Expand Down
5 changes: 3 additions & 2 deletions examples/wallet/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

/* system includes */
#include <zephyr.h>
#include <board.h>
#include <device.h>
#include <gpio.h>
#include <string.h>

/* local includes */
#include "zephyr-wallet/wallet.h"
#include "zephyr-wallet/shell_modules.h"

#include "zephyr-wallet/lwm2m_client.h"
/* 1000 msec = 1 sec */
Expand Down Expand Up @@ -54,11 +54,12 @@ void main(void)
{
int cnt = 0;
struct device *dev = init_led();
lwm2m_init();
/* lwm2m_init();*/
privkey_t pk;
memcpy(&pk.k, g_zephyr_private_key, 32);

wallet_set_global_privkey(&pk);
wallet_register_shell_modules();

while (1) {
/* Set pin to HIGH/LOW every 1 second */
Expand Down
3 changes: 3 additions & 0 deletions prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CONFIG_HTTP_PARSER_URL=y

CONFIG_NETWORKING=y
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_CONFIG_SETTINGS=y
Expand Down Expand Up @@ -40,3 +41,5 @@ CONFIG_DHT_NAME="DHT11"
CONFIG_DHT_GPIO_DEV_NAME="GPIO_1"
CONFIG_DHT_GPIO_PIN_NUM=10
CONFIG_SENSOR=y
CONFIG_SNTP=y
CONFIG_POSIX_API=y
2 changes: 2 additions & 0 deletions prj_qemu_x86.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ CONFIG_LWM2M_IPSO_SUPPORT=y
CONFIG_LWM2M_IPSO_TEMP_SENSOR=y

CONFIG_POSIX_API=y

CONFIG_SNTP=y
3 changes: 3 additions & 0 deletions zephyr-wallet/erc20_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,6 @@ SHELL_CREATE_STATIC_SUBCMD_SET(sub_erc20) {
SHELL_SUBCMD_SET_END
};
SHELL_CMD_REGISTER(erc20, &sub_erc20, "erc20 utils", NULL);

void erc20_shell_register()
{}
1 change: 1 addition & 0 deletions zephyr-wallet/erc20_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifdef __cplusplus
extern "C" {
#endif
void erc20_shell_register();

#ifdef __cplusplus
}
Expand Down
4 changes: 1 addition & 3 deletions zephyr-wallet/http_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

/* system includes */
#define LOG_MODULE_NAME http_service
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
Expand Down Expand Up @@ -54,18 +55,15 @@ static int on_headers_complete(struct http_parser *parser)
if ((ctx->http.req.method == HTTP_HEAD ||
ctx->http.req.method == HTTP_OPTIONS)
&& ctx->http.rsp.content_length > 0) {
NET_DBG("No body expected");
return 1;
}

if ((ctx->http.req.method == HTTP_PUT ||
ctx->http.req.method == HTTP_POST)
&& ctx->http.rsp.content_length == 0) {
NET_DBG("No body expected");
return 1;
}

NET_DBG("Headers complete");

return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions zephyr-wallet/sensor_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ SHELL_CREATE_STATIC_SUBCMD_SET(sub_sensor) {
SHELL_SUBCMD_SET_END
};
SHELL_CMD_REGISTER(sensor, &sub_sensor, "temperature sensor", NULL);

void sensor_shell_register()
{}
2 changes: 2 additions & 0 deletions zephyr-wallet/sensor_service.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#ifndef _SENSOR_SERVICE_H_
#define _SENSOR_SERVICE_H_
/* system includes */
#include <stdint.h>
/* local includes */


#ifdef __cplusplus
extern "C" {
#endif

void sensor_shell_register();
int get_sensor_data(int32_t *temperature, int32_t *humidity);

#ifdef __cplusplus
Expand Down
25 changes: 25 additions & 0 deletions zephyr-wallet/shell_modules.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @brief
* @file shell_modules.c
* @author J.H.
* @date 2018-11-29
*/

/* system includes */

/* local includes */
#include "shell_modules.h"
#include "web3_shell.h"
#include "sensor_service.h"
#include "upload.h"
#include "erc20_shell.h"
#include "sntp_shell.h"

void wallet_register_shell_modules()
{
web3_shell_register();
sensor_shell_register();
upload_shell_register();
erc20_shell_register();
sntp_shell_register();
}
18 changes: 18 additions & 0 deletions zephyr-wallet/shell_modules.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _SHELL_MODULES_H_
#define _SHELL_MODULES_H_
/* system includes */
/* local includes */


#ifdef __cplusplus
extern "C" {
#endif

void wallet_register_shell_modules();

#ifdef __cplusplus
}
#endif

#endif /* _SHELL_MODULES_H_ */

80 changes: 80 additions & 0 deletions zephyr-wallet/sntp_shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @brief
* @file sntp_shell.c
* @author J.H.
* @date 2018-11-29
*/

/* system includes */
#define LOG_MODULE_NAME sntp_shell
#include <net/sntp.h>
#include <shell/shell.h>
#include <posix/time.h>

/* local includes */

#define SNTP_PORT 123


#include <misc/printk.h>
void resp_callback(struct sntp_ctx *ctx,
int status,
u64_t epoch_time,
void *user_data)
{
struct timespec ts = {
.tv_sec = epoch_time,
.tv_nsec = 0
};
clock_settime(CLOCK_REALTIME, &ts);
struct k_sem *sync_sem = (struct k_sem *)user_data;
k_sem_give(sync_sem);
}

static int shell_sntp_sync(const struct shell *shell, size_t argc, char *argv[])
{
struct sntp_ctx ctx;
int rv;
// semaphore to keep the app alive until the reply is received or timeouts
struct k_sem sync_sem;
k_sem_init(&sync_sem, 0, 1);

/* ipv4 */
rv = sntp_init(&ctx,
CONFIG_NET_CONFIG_PEER_IPV4_ADDR,
SNTP_PORT,
K_FOREVER);
if (rv < 0) {
shell_error(shell, "Failed to init sntp ctx: %d\n", rv);
goto end;
}
rv = sntp_request(&ctx, K_FOREVER, resp_callback, &sync_sem);
if (rv < 0) {
shell_warn(shell, "Failed to send sntp request: %d\n", rv);
goto end;
}
k_sem_take(&sync_sem, K_FOREVER);
end:
sntp_close(&ctx);
return 0;
}

static int shell_sntp_time(const struct shell *shell, size_t argc, char *argv[])
{
ARG_UNUSED(argv);
ARG_UNUSED(argc);
struct timeval tv;
gettimeofday(&tv, NULL);
shell_print(shell, "%ld", tv.tv_sec);
return 0;
}

SHELL_CREATE_STATIC_SUBCMD_SET(sub_sntp) {
SHELL_CMD(sync, NULL, "sync", shell_sntp_sync),
SHELL_CMD(time, NULL, "time", shell_sntp_time),
SHELL_SUBCMD_SET_END
};
SHELL_CMD_REGISTER(sntp, &sub_sntp, "sntp utils", NULL);

void sntp_shell_register()
{}
17 changes: 17 additions & 0 deletions zephyr-wallet/sntp_shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _SNTP_SHELL_H_
#define _SNTP_SHELL_H_
/* system includes */
/* local includes */


#ifdef __cplusplus
extern "C" {
#endif
void sntp_shell_register();

#ifdef __cplusplus
}
#endif

#endif /* _SNTP_SHELL_H_ */

2 changes: 2 additions & 0 deletions zephyr-wallet/upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,5 @@ SHELL_CREATE_STATIC_SUBCMD_SET(sub_upload) {
SHELL_SUBCMD_SET_END
};
SHELL_CMD_REGISTER(upload, &sub_upload, "http upload service", NULL);

void upload_shell_register() {}
1 change: 1 addition & 0 deletions zephyr-wallet/upload.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifdef __cplusplus
extern "C" {
#endif
void upload_shell_register();

#ifdef __cplusplus
}
Expand Down
7 changes: 3 additions & 4 deletions zephyr-wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <zephyr.h>
#include <helpers/hextobin.h>
#include <string.h>
#include <stdlib.h>
#include <shell/shell.h>
#pragma GCC diagnostic pop
#include <string.h>
#include <stdlib.h>

/* local includes */
#include "wallet.h"
#include "helpers/hextobin.h"
#include "zephyr-wallet/utils.h"
#include "zephyr-wallet/web3_rpc.h"

Expand Down Expand Up @@ -234,4 +234,3 @@ SHELL_CREATE_STATIC_SUBCMD_SET(sub_wallet) {
SHELL_SUBCMD_SET_END
};
SHELL_CMD_REGISTER(wallet, &sub_wallet, "crypto eth", NULL);

4 changes: 3 additions & 1 deletion zephyr-wallet/web3_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ SHELL_CREATE_STATIC_SUBCMD_SET(sub_web3) {
SHELL_CMD(eth_sendRawTransaction, NULL, "send a raw transaction", web3_shell_sendRawTransaction),
SHELL_SUBCMD_SET_END
};
SHELL_CMD_REGISTER(web3, &sub_web3, "http upload service", NULL);
SHELL_CMD_REGISTER(web3, &sub_web3, "web3 utils", NULL);

void web3_shell_register() {}
1 change: 1 addition & 0 deletions zephyr-wallet/web3_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifdef __cplusplus
extern "C" {
#endif
void web3_shell_register();

#ifdef __cplusplus
}
Expand Down

0 comments on commit 3bde08e

Please sign in to comment.