#ifndef __NB73_H__
#define __NB73_H__

#include "stm32l4xx_hal.h"
#include "stdbool.h"

extern uint32_t msgid_temperature;
extern uint32_t msgid_humidity;
extern uint32_t msgid_led;

extern bool temperature_enable;
extern bool humidity_enable;
extern bool led_enable;
extern int notify_fail_cnt;


#define OBJECT_TEMPERATURE					3303
#define OBJECT_HUMIDITY							3304
#define OBJECT_LED									3201

#define RESOUREC_LED_STA						5550

#define CIS_EVENT_BASE                      (uint8_t)0x00
#define CIS_EVENT_BOOTSTRAP_START           CIS_EVENT_BASE + 1
#define CIS_EVENT_BOOTSTRAP_SUCCESS         CIS_EVENT_BASE + 2
#define CIS_EVENT_BOOTSTRAP_FAILED          CIS_EVENT_BASE + 3
#define CIS_EVENT_CONNECT_SUCCESS           CIS_EVENT_BASE + 4
#define CIS_EVENT_CONNECT_FAILED            CIS_EVENT_BASE + 5

#define CIS_EVENT_REG_SUCCESS               CIS_EVENT_BASE + 6
#define CIS_EVENT_REG_FAILED                CIS_EVENT_BASE + 7
#define CIS_EVENT_REG_TIMEOUT               CIS_EVENT_BASE + 8

#define CIS_EVENT_LIFETIME_TIMEOUT          CIS_EVENT_BASE + 9
#define CIS_EVENT_STATUS_HALT               CIS_EVENT_BASE + 10
#define CIS_EVENT_UPDATE_SUCCESS            CIS_EVENT_BASE + 11
#define CIS_EVENT_UPDATE_FAILED             CIS_EVENT_BASE + 12
#define CIS_EVENT_UPDATE_TIMEOUT            CIS_EVENT_BASE + 13
#define CIS_EVENT_UPDATE_NEED               CIS_EVENT_BASE + 14
#define CIS_EVENT_RESPONSE_FAILED           CIS_EVENT_BASE + 20
#define CIS_EVENT_RESPONSE_SUCCESS          CIS_EVENT_BASE + 21
#define CIS_EVENT_NOTIFY_FAILED             CIS_EVENT_BASE + 25
#define CIS_EVENT_NOTIFY_SUCCESS            CIS_EVENT_BASE + 26

/* Reboot NB73 */
#define CMD_REBOOT_NB73							"AT+NRB\r\n"

/* Init and register */
#define CMD_CLOSE										"AT+MIPLCLOSE=0\r\n"
#define CMD_DELETE									"AT+MIPLDELETE=0\r\n"

#define CMD_CREATE									"AT+MIPLCREATE\r\n"
#define CMD_ADDOBJ_TEMPERATURE			"AT+MIPLADDOBJ=0,3303,1,\"1\",6,1\r\n"	//5700
#define CMD_ADDOBJ_HUMIDITY					"AT+MIPLADDOBJ=0,3304,1,\"1\",6,1\r\n"	//5700
#define CMD_ADDOBJ_LED							"AT+MIPLADDOBJ=0,3201,1,\"1\",3,0\r\n"	//5550
#define CMD_REGISTER								"AT+MIPLOPEN=0,3600,30\r\n"

/* Discover */
#define CMD_DISCOVERRSP_TEMPERATURE	"AT+MIPLDISCOVERRSP=0,%u,1,4,\"5700\"\r\n"
#define CMD_DISCOVERRSP_HUMIDITY		"AT+MIPLDISCOVERRSP=0,%u,1,4,\"5700\"\r\n"
#define CMD_DISCOVERRSP_LED					"AT+MIPLDISCOVERRSP=0,%u,1,4,\"5550\"\r\n"

/* Observe */
#define CMD_OBSERVERSP							"AT+MIPLOBSERVERSP=0,%u,1\r\n"

/* Read */
#define CMD_READRSP_TEMPERATURE			"AT+MIPLREADRSP=0,%u,1,3303,0,5700,4,%d,%s,0,0\r\n"
#define CMD_READRSP_HUMIDITY				"AT+MIPLREADRSP=0,%u,1,3304,0,5700,4,%d,%s,0,0\r\n"
#define CMD_READRSP_LED							"AT+MIPLREADRSP=0,%u,1,3201,0,5550,5,1,%d,0,0\r\n"

/* Write */
#define CMD_WRITERSP_LED						"AT+MIPLWRITERSP=0,%u,2\r\n"

/* Update */
#define CMD_UPDATE_LIFETIME					"AT+MIPLUPDATE=0,0,0\r\n"

#define CMD_NOTIFY_TEMPERATURE			"AT+MIPLNOTIFY=0,%u,3303,0,5700,4,%d,%s,0,0\r\n"
#define CMD_NOTIFY_HUMIDITY					"AT+MIPLNOTIFY=0,%u,3304,0,5700,4,%d,%s,0,0\r\n"
#define CMD_NOTIFY_LED							"AT+MIPLNOTIFY=0,%u,3201,0,5550,5,%d,%d,0,0\r\n"

void nb73_task(void);
int NB73_config(int);
uint32_t NB73_get_id(char *data, uint8_t index);

int NB73_get_led_state(void);
void NB73_set_led_state(bool sta);

#endif
