382 lines
13 KiB
C
Executable File
382 lines
13 KiB
C
Executable File
/****************************************************************************
|
||
*
|
||
* Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
|
||
*
|
||
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
|
||
* be copied by any method or incorporated into another program without
|
||
* the express written consent of Aerospace C.Power. This Information or any portion
|
||
* thereof remains the property of Aerospace C.Power. The Information contained herein
|
||
* is believed to be accurate and Aerospace C.Power assumes no responsibility or
|
||
* liability for its use in any way and conveys no license or title under
|
||
* any patent or copyright and makes no representation or warranty that this
|
||
* Information is free from patent or copyright infringement.
|
||
*
|
||
* ****************************************************************************/
|
||
|
||
#ifndef _INCLUDE_IOT_CP_SOCKET_H_
|
||
#define _INCLUDE_IOT_CP_SOCKET_H_
|
||
|
||
/******************************************************************************/
|
||
/**************************** Defination used by Task *************************/
|
||
/******************************************************************************/
|
||
|
||
/* event ID */
|
||
enum cp_socket_status_event_id_e {
|
||
/* in idle status*/
|
||
CP_SOCKET_EVENT_ID_IDLE = 0x00,
|
||
/* confirm event for begaining to charge */
|
||
CP_SOCKET_EVENT_ID_CHARG_START_CFM = 0x01,
|
||
/* confirm event for stopping charging */
|
||
CP_SOCKET_EVENT_ID_CHARG_STOP_CFM = 0x02,
|
||
/* plug in/out */
|
||
CP_SOCKET_EVENT_ID_PLUG_IO = 0x03,
|
||
/* abnormal voltage: 0ver/under volt */
|
||
CP_SOCKET_EVENT_ID_ABNORM_VOLT = 0x04,
|
||
/* abnormal current: overcurrent */
|
||
CP_SOCKET_EVENT_ID_ABNORM_CURR = 0x05,
|
||
/* leakage current */
|
||
CP_SOCKET_EVENT_ID_LEAKAGE = 0x06,
|
||
/* ground connection error */
|
||
CP_SOCKET_EVENT_ID_GROUND_ERR = 0x07,
|
||
|
||
/* ... */
|
||
|
||
CP_SOCKET_EVENT_ID_UNKNOWN = 0xFF,
|
||
};
|
||
|
||
/* List of the command id. */
|
||
enum cp_socket_cmd_id_e {
|
||
/* config/query parameter. */
|
||
CP_SOCKET_CID_CONFIG = 0x00,
|
||
/* query/response/indication the plug state */
|
||
CP_SOCKET_CID_PLUG_INFO = 0x01,
|
||
/* indication when event happes, see CP_SOCKET_EVENT_XX */
|
||
CP_SOCKET_CID_STATUS_EVENT = 0x02,
|
||
/* periodly report data event: indicate */
|
||
CP_SOCKET_CID_DATA_EVENT = 0x03,
|
||
/* set the relay state */
|
||
CP_SOCKET_CID_RELAY_STATE = 0x04,
|
||
/* max command id */
|
||
CP_SOCKET_CID_MAX = 0x05,
|
||
|
||
/* ... */
|
||
|
||
/* invalid opertion code */
|
||
CP_SOCKET_INVALID = 0xFF,
|
||
};
|
||
|
||
/* Operation code. */
|
||
enum cp_socket_cmd_opcode_e {
|
||
/* command for change the meter configuration or relay state */
|
||
CP_SOCKET_OP_CONFIG = 0x00,
|
||
/* response config result error or ok to upper layer */
|
||
CP_SOCKET_OP_CFM = 0x01,
|
||
/* command for query the status or configuration */
|
||
CP_SOCKET_OP_QUERY = 0x02,
|
||
/* response query message to upper layer */
|
||
CP_SOCKET_OP_RESPONSE = 0x03,
|
||
/* indicate message to upper layer */
|
||
CP_SOCKET_OP_INDICATION = 0x04,
|
||
/* the max op number. */
|
||
CP_SOCKET_OP_MAX = CP_SOCKET_OP_INDICATION,
|
||
|
||
/* ... */
|
||
|
||
/* invalid opertion code */
|
||
CP_SOCKET_OP_INVALID = 0xFF,
|
||
};
|
||
|
||
/* Return status of the command */
|
||
enum cp_socket_cmd_response_e {
|
||
CP_SOCKET_RESP_OK = 0x00,
|
||
CP_SOCKET_RESP_ERROR = 0x01,
|
||
CP_SOCKET_RESP_BUSY = 0x02,
|
||
|
||
/* ... */
|
||
|
||
CP_SOCKET_RESP_UNKNOWN = 0xFF,
|
||
};
|
||
|
||
/* reasons of command hand failure */
|
||
enum cp_socket_cmd_fail_reason_e {
|
||
/* no failure */
|
||
CP_SOCKET_NO_FAIL = 0x00,
|
||
/* CID is not supported */
|
||
CP_SOCKET_FAIL_CID_NOT_SUPPORT = 0x01,
|
||
/* op_code is not supported */
|
||
CP_SOCKET_FAIL_OPC_NOT_SUPPORT = 0x02,
|
||
/* busy, not handle */
|
||
CP_SOCKET_FAIL_BUSY = 0x03,
|
||
/* parameter is invaild */
|
||
CP_SOCKET_FAIL_PARAM_ERR = 0x04,
|
||
|
||
/* ... */
|
||
|
||
CP_SOCKET_FAIL_UNKNOWN = 0xFF,
|
||
};
|
||
|
||
/* 0:first socket..., max is the total num of sockets, all select all sockets */
|
||
enum cp_socket_id_e {
|
||
/* the first socket */
|
||
CP_SOCKET_ID_0 = 0,
|
||
CP_SOCKET_ID_1 = 1,
|
||
CP_SOCKET_ID_2 = 2,
|
||
CP_SOCKET_ID_3 = 3,
|
||
/* select all sockets */
|
||
CP_SOCKET_ID_ALL = 4,
|
||
/* the max socket num */
|
||
CP_SOCKET_ID_MAX = CP_SOCKET_ID_ALL,
|
||
};
|
||
|
||
#pragma pack(push) // save the pack status
|
||
#pragma pack(1) // 1 byte align
|
||
|
||
typedef struct _cp_socket_fault_t {
|
||
/* error status */
|
||
uint16_t overvolt :1,
|
||
/* undervolt */
|
||
undervolt :1,
|
||
/* overcurrent */
|
||
overcur :1,
|
||
/* overload */
|
||
overload :1,
|
||
/* over temperature */
|
||
overtemp :1,
|
||
/* connection faluare */
|
||
conn_fail :1,
|
||
/* leak-current protection */
|
||
leak_cur_protec :1,
|
||
/* ground fault */
|
||
gnd_err :1,
|
||
/* charging timeout alarm enable */
|
||
charg_timeout :1,
|
||
/* reserved for future */
|
||
resved :7;
|
||
} cp_socket_fault_t;
|
||
|
||
/* parameter whitch will config into socket */
|
||
typedef struct _cp_socket_cmd_cfg_param_t {
|
||
/* target plug's number, range from 0 to 3, 4 means target
|
||
* plugs are all of the 4 plugs
|
||
*/
|
||
uint8_t plug_id;
|
||
/* the rated voltage, such as 110V or 220v, unit: 1V */
|
||
uint32_t rated_volt;
|
||
/* the current threshold when charge full, unit:1mA(1~1000)*/
|
||
uint16_t charge_full_i_th;
|
||
/* the current threshold when load is over, unit:0.1A(1~160)*/
|
||
uint16_t overload_i_th;
|
||
/* the current threshold when leak,unit:1 mA(1~255)*/
|
||
uint8_t leak_i_th;
|
||
/* threshold of open circuit */
|
||
uint8_t discon_cur_th;
|
||
union {
|
||
/* overvolt alarm enable */
|
||
uint16_t alarm_en_overvolt :1,
|
||
/* undervolt alarm enable */
|
||
alarm_en_undervolt :1,
|
||
/* overcurrent alarm enable */
|
||
alarm_en_overcur :1,
|
||
/* overload alarm enable */
|
||
alarm_en_overload :1,
|
||
/* over temperature enable */
|
||
alarm_en_overtemp :1,
|
||
/* connection failure temperature enable */
|
||
alarm_en_conn_fail :1,
|
||
/* leak-current protection alarm enable */
|
||
alarm_en_leak_cur :1,
|
||
/* ground fault alarm enable */
|
||
alarm_en_gnd_err :1,
|
||
/* charging timeout alarm enable */
|
||
alarm_en_charge_timeout :1,
|
||
/* reserved for future */
|
||
resved :7;
|
||
uint16_t alarm_en_data;
|
||
} alarm;
|
||
} cp_socket_cmd_cfg_param_t;
|
||
|
||
/* relay state */
|
||
typedef struct _cp_relay_state_t {
|
||
/* target plug's ID, range from 0 to 3, 4 means target
|
||
* plugs are all of the 4 plugs
|
||
*/
|
||
uint8_t plug_id;
|
||
bool_t relay_state;
|
||
} cp_relay_state_t;
|
||
|
||
/* query the assigned plug's configration parameters */
|
||
typedef struct _cp_socket_cmd_query_param_t {
|
||
/* target plug's ID, range from 0 to 3, 4 means target
|
||
* plugs are all of the 4 plugs
|
||
*/
|
||
uint8_t plug_id;
|
||
} cp_socket_cmd_query_param_t;
|
||
|
||
/* response the configuration prameter: from socket task to cp_task */
|
||
typedef struct _cp_socket_cmd_param_resp_t {
|
||
/* how many plug's config parameter containd by the pkt */
|
||
uint8_t plug_cnt;
|
||
/* plug's parameter setting data */
|
||
cp_socket_cmd_cfg_param_t param[0];
|
||
} cp_socket_cmd_param_resp_t;
|
||
|
||
/* single plug's meter data */
|
||
typedef struct _cp_socket_plug_meter_data_t {
|
||
/* charging current, unit: mA */
|
||
uint32_t charging_i;
|
||
/* power rate, unit: w */
|
||
uint32_t power_rate;
|
||
/*The amount of electricity since power on, unit: 0.01kwh */
|
||
uint32_t pwr_consum;
|
||
/* power factor, unit: 0.01 */
|
||
uint8_t pwr_factor;
|
||
} cp_socket_plug_meter_data_t;
|
||
|
||
/* single plug's info include meter data and plugin status,fault status */
|
||
typedef struct _cp_socket_plug_info_t {
|
||
/* plug's ID, range from 0 to 3, 4 means all */
|
||
uint8_t plug_id;
|
||
/* plug's meter data */
|
||
cp_socket_plug_meter_data_t meter_data;
|
||
/* Monitor plug or unplug state. 0--unpluged, 1--plugged */
|
||
bool_t is_plug_in;
|
||
/* incumbent volt of plug, unit: 100mv */
|
||
uint32_t incumbent_v;
|
||
/* fault status */
|
||
cp_socket_fault_t fault_status;
|
||
/* relay state */
|
||
bool_t relay_state;
|
||
} cp_socket_plug_info_t;
|
||
|
||
/* report/response the plug infor from meter_task to cp_task */
|
||
typedef struct _cp_socket_cmd_plug_info_resp_t {
|
||
/* count of plug containd by the pkt */
|
||
uint8_t plug_cnt;
|
||
/* charging volt, unit: 100mV */
|
||
uint32_t volt;
|
||
/* leak-current, unit: mA */
|
||
uint32_t leak_i;
|
||
/* voltage frequnce, unit: 0.01HZ */
|
||
uint32_t freq;
|
||
/* plug info data */
|
||
cp_socket_plug_info_t plug_info[0];
|
||
} cp_socket_cmd_plug_info_resp_t;
|
||
|
||
/* query the assigned plug's info */
|
||
typedef struct _cp_socket_cmd_query_plug_info_t {
|
||
/* target plug's ID, range from 0 to 3, 4 means target
|
||
* plugs are all of the 4 plugs
|
||
*/
|
||
uint8_t plug_id;
|
||
} cp_socket_cmd_query_plug_info_t;
|
||
|
||
/* once a config command was sent, confirm should be rececived */
|
||
typedef struct _cp_socket_cmd_confirm_t {
|
||
/* result of the confirm for cmd, 0--successful, none 0-- failed */
|
||
uint8_t result;
|
||
/* failure reason */
|
||
uint8_t reason;
|
||
} cp_socket_cmd_confirm_t;
|
||
|
||
/* query the assigned plug's info */
|
||
typedef struct _cp_socket_cmd_query_fault_info_t {
|
||
/* target plug's ID, range from 0 to 3, 4 means target
|
||
* plugs are all of the 4 plugs
|
||
*/
|
||
uint8_t plug_id;
|
||
} cp_socket_cmd_query_fault_info_t;
|
||
|
||
/* each socket fault source. */
|
||
typedef struct _cp_socket_cmd_fault_src_t {
|
||
/* target plug's ID, range from 0 to 3, 4 means target
|
||
* plugs are all of the 4 plugs
|
||
*/
|
||
uint8_t plug_id;
|
||
/* source of fault */
|
||
cp_socket_fault_t fault_src;
|
||
} cp_socket_cmd_fault_src_t;
|
||
|
||
/* report when fault status event occer. */
|
||
typedef struct _cp_socket_cmd_fault_rpt_t {
|
||
/* how many sockets fault status response or indicate */
|
||
uint8_t plug_cnt;
|
||
/* fault status data */
|
||
cp_socket_cmd_fault_src_t fault[0];
|
||
} cp_socket_cmd_fault_rpt_t;
|
||
|
||
/* report the plug's data periodcaly */
|
||
typedef struct _cp_socket_cmd_event_data_rpt_t {
|
||
/* count of plug containd by the pkt */
|
||
uint8_t plug_cnt;
|
||
/* charging volt, unit: 100mV */
|
||
uint32_t volt;
|
||
/* leak-current, unit: mA */
|
||
uint32_t leak_i;
|
||
/* voltage frequnce, unit: 0.01HZ */
|
||
uint32_t freq;
|
||
/* plug info data, inlcude meter data and event data */
|
||
cp_socket_plug_info_t plug_info[0];
|
||
} cp_socket_cmd_event_data_rpt_t;
|
||
|
||
/* report the plug's info periodcaly */
|
||
typedef struct _cp_socket_cmd_event_status_t {
|
||
/* event id, see EVENT_ID_XXX */
|
||
uint8_t event_id;
|
||
/* plug id, see CP_SOCKET_ID_X */
|
||
uint8_t plug_id;
|
||
/* source of fault */
|
||
cp_socket_fault_t fault_src;
|
||
/* Monitor plug or unplug state. 0--unpluged, 1--plugged */
|
||
bool_t is_plug_in;
|
||
/* incumbent volt of plug, unit: 100mv */
|
||
uint32_t incumbent_v;
|
||
/* charging volt, unit: 100mV */
|
||
uint32_t volt;
|
||
/* leak-current, unit: mA */
|
||
uint32_t leak_i;
|
||
/* plug's meter data, uplayer may want to known the exact value */
|
||
cp_socket_plug_meter_data_t meter_data;
|
||
} cp_socket_cmd_event_status_t;
|
||
|
||
typedef struct _cp_socket_hdr_t {
|
||
/* Reference to enum cp_socket_cmd_id_e */
|
||
uint8_t cid;
|
||
/* Reference to enum cp_socket_cmd_opcode_e */
|
||
uint8_t opcode;
|
||
} cp_socket_hdr_t;
|
||
|
||
/* The argument for charging pile socket command handle. */
|
||
typedef struct _cp_socket_cmd_arg_t {
|
||
/* command header */
|
||
cp_socket_hdr_t hdr;
|
||
/* the priority of this command. */
|
||
uint16_t prio;
|
||
uint16_t dlen;
|
||
/* if need ack */
|
||
uint16_t need_ack;
|
||
/* the argument for this command. */
|
||
uint8_t arg[0];
|
||
} cp_socket_cmd_arg_t;
|
||
|
||
#pragma pack(pop)
|
||
|
||
typedef uint32_t(*cp_socket_resp_cb)(iot_pkt_t *pkt);
|
||
|
||
/* Initialize the iot charging pile socket module. */
|
||
uint32_t cp_socket_task_init(void);
|
||
|
||
/* De-initializing charging pile socket module. */
|
||
void cp_socket_task_deinit(void);
|
||
|
||
/* API function for charging pile task to send cmd to charging pile socket. */
|
||
/* for cp socket is to receive cmds; for cp task is to send cmds */
|
||
uint32_t cp_socket_cmd_send_mssage(iot_pkt_t *arg);
|
||
|
||
/* Charging pile task registed callback. Charging pile socket use to
|
||
* response command previous received. Cmd-Resp works as ping-pong method.
|
||
* Hook interface with charging pile task.
|
||
*/
|
||
uint32_t cp_socket_register(cp_socket_resp_cb cb);
|
||
|
||
#endif
|