Skip to content

Commit

Permalink
feat: implement ntp, tcp, eth, wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
wjsan committed Oct 18, 2023
1 parent 69fcc93 commit 9892596
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 17 deletions.
18 changes: 17 additions & 1 deletion main/ciot_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
#define CIOT_CONFIG_FEATURE_SERIAL 1 ///!< Enable Serial
#define CIOT_CONFIG_FEATURE_ETHERNET 1 ///!< Enable Ethernet
#define CIOT_CONFIG_FEATURE_WIFI 1 ///!< Enable WiFi
#define CIOT_CONFIG_FEATURE_NTP 1 ///!< Enable NTP
#define CIOT_CONFIG_FEATURE_OTA 1 ///!< Enable OTA
#define CIOT_CONFIG_FEATURE_HTTPS 1 ///!< Enable HTTP Server
#define CIOT_CONFIG_FEATURE_HTTPC 1 ///!< Enable HTTP Client
#define CIOT_CONFIG_FEATURE_MQTTC 1 ///!< Enable MQTT Client
#define CIOT_CONFIG_MESSAGE_LEN 240 ///!< CIOT expected message size
#define CIOT_CONFIG_MESSAGE_LEN 334 ///!< CIOT expected message size

// #define CIOT_CONFIG_MESSAGE_LEN sizeof(ciot_msg_u)

#define CIOT_CONFIG_ETH_PHY_KSZ8081 1
Expand All @@ -36,4 +39,17 @@
#define CIOT_CONFIG_WIFI_STA_SSID "CABO CANAVERAL"
#define CIOT_CONFIG_WIFI_STA_PASS "16192020"

#define CIOT_CONFIG_NTP_OP_MODE 0
#define CIOT_CONFIG_NTP_SYNC_MODE 0
#define CIOT_CONFIG_NTP_SYNC_INTERVAL (3600 * 1000)
#define CIOT_CONFIG_NTP_TZ "<-03>3"
#define CIOT_CONFIG_NTP_SERVERS_COUNT 3
#define CIOT_CONFIG_NTP_SERVER1 "pool.ntp.org"
#define CIOT_CONFIG_NTP_SERVER2 "time.google.com"
#define CIOT_CONFIG_NTP_SERVER3 "gps.ntp.br"

#define CIOT_CONFIG_OTA_TASK_STACK_SIZE 8192
#define CIOT_CONFIG_OTA_TASK_TASK_PRIORITY (tskIDLE_PRIORITY + 4)
#define CIOT_CONFIG_OTA_TASK_CORE_ID 1

#endif //!__CIOT_CONFIG__H__
6 changes: 6 additions & 0 deletions main/data/ciot_msg_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// #include "ciot_serial_data.h"
#include "ciot_tcp_data.h"
#include "ciot_wifi_data.h"
// #include "ciot_ble_data.h"
#include "ciot_ntp_data.h"
#include "ciot_https_data.h"
#include "ciot_httpc_data.h"
#include "ciot_mqttc_data.h"
Expand Down Expand Up @@ -46,6 +48,8 @@ typedef enum __attribute__((packed))
CIOT_IFACE_TYPE_ETH,
CIOT_IFACE_TYPE_WIFI,
CIOT_IFACE_TYPE_BLE,
CIOT_IFACE_TYPE_NTP,
CIOT_IFACE_TYPE_OTA,
CIOT_IFACE_TYPE_HTTP_CLIENT,
CIOT_IFACE_TYPE_HTTP_SERVER,
CIOT_IFACE_TYPE_MQTT,
Expand All @@ -63,6 +67,8 @@ typedef union __attribute__((packed))
// ciot_serial_data_u serial;
ciot_tcp_data_u eth;
ciot_wifi_data_u wifi;
// ciot_ble_data_u ble;
ciot_ntp_data_u ntp;
ciot_https_data_u https;
ciot_httpc_data_u httpc;
ciot_mqttc_data_u mqtt;
Expand Down
72 changes: 72 additions & 0 deletions main/data/ciot_ntp_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @file ciot_ntp_data.h
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2023-10-18
*
* @copyright Copyright (c) 2023
*
*/

#ifndef __CIOT_NTP_DATA__H__
#define __CIOT_NTP_DATA__H__

#include <inttypes.h>
#include <time.h>

#include "ciot_config.h"

typedef enum __attribute__((packed))
{
HG_NTP_STATE_RESET,
HG_NTP_STATE_COMPLETED,
HG_NTP_STATE_IN_PROGRESS,
} ciot_ntp_state_t;

typedef enum __attribute__((packed))
{
CIOT_NTP_REQ_UNKNOWN,
} ciot_ntp_req_id_t;

typedef struct __attribute__((packed))
{
uint8_t op_mode;
uint8_t sync_mode;
uint32_t sync_interval;
char timezone[16];
char server[CIOT_CONFIG_NTP_SERVERS_COUNT][64];
} ciot_ntp_cfg_t;

typedef struct __attribute__((packed))
{
ciot_ntp_state_t state;
time_t last_sync;
uint16_t sync_count;
uint8_t init :1;
uint8_t sync :1;
uint8_t reserve :6;
} ciot_ntp_status_t;

typedef union __attribute__((packed))
{

} ciot_ntp_req_data_u;

typedef struct __attribute__((packed))
{
ciot_ntp_req_id_t id;
ciot_ntp_req_data_u data;
} ciot_ntp_req_t;

typedef union __attribute__((packed))
{
#if CIOT_CONFIG_FEATURE_NTP
ciot_ntp_cfg_t config;
ciot_ntp_status_t status;
ciot_ntp_req_t request;
#endif
} ciot_ntp_data_u;


#endif //!__CIOT_NTP_DATA__H__
78 changes: 78 additions & 0 deletions main/data/ciot_ota_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @file ciot_ota_data.h
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2023-10-18
*
* @copyright Copyright (c) 2023
*
*/

#ifndef __CIOT_OTA_DATA__H__
#define __CIOT_OTA_DATA__H__

#include <inttypes.h>
#include <stddef.h>

#include "ciot_err.h"
#include "ciot_config.h"

typedef enum __attribute__((packed))
{
CIOT_OTA_STATE_ERROR=-1,
CIOT_OTA_STATE_IDLE,
CIOT_OTA_STATE_INIT,
CIOT_OTA_STATE_IN_PROGRESS,
CIOT_OTA_STATE_START,
CIOT_OTA_STATE_CONNECTED,
CIOT_OTA_STATE_CHECKING_DATA,
CIOT_OTA_STATE_DECRYPTING,
CIOT_OTA_STATE_FLASHING,
CIOT_OTA_STATE_UPDATE_BOOT_PARTITION,
CIOT_OTA_STATE_DONE,
} ciot_ota_state_t;

typedef enum __attribute__((packed))
{
CIOT_OTA_REQ_UNKNOWN,
} ciot_ota_req_id_t;

typedef struct __attribute__((packed))
{
char url[128];
uint8_t force :1;
uint8_t encrypted :1;
uint8_t restart :1;
uint8_t reserverd :5;
} ciot_ota_cfg_t;

typedef struct __attribute__((packed))
{
ciot_ota_state_t state;
ciot_err_t error;
size_t image_size;
size_t image_read;
} ciot_ota_status_t;

typedef union __attribute__((packed))
{

} ciot_ota_req_data_u;

typedef struct __attribute__((packed))
{
ciot_ota_req_id_t id;
ciot_ota_req_data_u data;
} ciot_ota_req_t;

typedef union __attribute__((packed))
{
#if CIOT_CONFIG_FEATURE_OTA
ciot_ota_cfg_t config;
ciot_ota_status_t status;
ciot_ota_req_t request;
#endif
} ciot_ota_data_u;

#endif //!__CIOT_OTA_DATA__H__
4 changes: 3 additions & 1 deletion main/data/ciot_sys_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ typedef struct __attribute__((packed))

typedef struct __attribute__((packed))
{
uint32_t ntp : 1;
uint32_t ota: 1;
uint32_t http_client : 1;
uint32_t http_server : 1;
uint32_t mqtt_client : 1;
uint32_t mqtt_server : 1;
uint32_t reserved : 28;
uint32_t reserved : 26;
} ciot_sys_sw_features_t;

typedef struct __attribute__((packed))
Expand Down
13 changes: 13 additions & 0 deletions main/infra/ciot_ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
#ifndef __CIOT_NTP__H__
#define __CIOT_NTP__H__

#include "ciot_ntp_data.h"
#include "ciot_iface.h"
#include "ciot_err.h"

typedef struct ciot_ntp *ciot_ntp_t;

ciot_ntp_t ciot_ntp_new(void *handle);

ciot_err_t ciot_ntp_start(ciot_ntp_t this, ciot_ntp_cfg_t *cfg);
ciot_err_t ciot_ntp_stop(ciot_ntp_t this);
ciot_err_t ciot_ntp_process_req(ciot_ntp_t this, ciot_ntp_req_t *req);
ciot_err_t ciot_ntp_send_data(ciot_ntp_t this, uint8_t *data, int size);

ciot_err_t ciot_ntp_set(ciot_ntp_t ntp);

#endif //!__CIOT_NTP__H__
10 changes: 10 additions & 0 deletions main/infra/ciot_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
#ifndef __CIOT_OTA__H__
#define __CIOT_OTA__H__

#include "ciot_ota_data.h"
#include "ciot_iface.h"
#include "ciot_err.h"

typedef struct ciot_ota *ciot_ota_t;

ciot_ota_t ciot_ota_new(void *handle);
ciot_err_t ciot_ota_start(ciot_ota_t this, ciot_ota_cfg_t *cfg);
ciot_err_t ciot_ota_stop(ciot_ota_t this);
ciot_err_t ciot_ota_process_req(ciot_ota_t this, ciot_ota_req_t *req);
ciot_err_t ciot_ota_send_data(ciot_ota_t this, uint8_t *data, int size);

#endif //!__CIOT_OTA__H__
13 changes: 8 additions & 5 deletions main/infra/idf/ciot_eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ struct ciot_eth

static const char *TAG = "ciot_eth";

static ciot_err_t ciot_eth_init(ciot_eth_t this);
static ciot_err_t ciot_eth_tcp_event_handler(void *sender, ciot_iface_event_t *event, void *event_args);
static void ciot_eth_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data);

ciot_eth_t ciot_eth_new(void *handle)
{
ciot_eth_t this = calloc(1, sizeof(struct ciot_eth));
Expand Down Expand Up @@ -119,15 +123,14 @@ static ciot_err_t ciot_eth_init(ciot_eth_t this)
return CIOT_OK;
}

static ciot_err_t ciot_eth_tcp_event_handler(ciot_tcp_t tcp, ciot_iface_event_t *event, void *event_args)
static ciot_err_t ciot_eth_tcp_event_handler(void *sender, ciot_iface_event_t *event, void *args)
{
CIOT_NULL_CHECK(tcp);
CIOT_NULL_CHECK(event_args);
CIOT_NULL_CHECK(args);

ciot_eth_t this = (ciot_eth_t)event_args;
ciot_eth_t this = (ciot_eth_t)args;
if (this->iface.event_handler != NULL)
{
return this->iface.event_handler(this, event, event_args);
return this->iface.event_handler(this, event, this->iface.event_args);
}

return CIOT_ERR_INVALID_STATE;
Expand Down
Loading

0 comments on commit 9892596

Please sign in to comment.